Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion design/author.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ to 3,000. Thirty things the code does not say by itself:
field the editor gives its rich-text widget (`widget: "html"`, or a ShowWhen that draws one) takes
markup with the tags the content check keeps for it (its own, `div`, `span`, `p`, `br`, and what
they imply), checked here rather than filtered later: any other tag, an event handler, a style
the field does not allow, a script address or a comment is a problem by name. Plain text written
the field does not allow, a script address, a comment, or a quote a tag leaves open, read as a
browser reads its attributes, is a problem by name. The last is what a `>` inside an attribute makes: the content check ends the tag there,
while a browser reads on past it and loses the text in between (#222). Plain text written
into such a field is wrapped as that widget wraps typing: in `<p>` where the field's `enterMode`
is `p` or its tags offer headings, `pre`, `p` or `div`, in `<div>` otherwise. Every plain default
is escaped too, English or translated, and a `null` default (Course Presentation's colours) is
Expand Down
30 changes: 30 additions & 0 deletions packages/author/lib/text.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,29 @@ function stylePatterns(field) {
*/
const PIECE = /(<(?=[^a-zA-Z!/])|<!--[\s\S]*?(?:-->|$)|<[^>]*(>|$)|>)/g
const ATTRIBUTE = /([a-zA-Z_:][-a-zA-Z0-9_:.]*)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'>]+)))?/g

/**
* One attribute as a browser reads it in a tag: a name, which may begin with `=` and holds no
* space, `/` or `=`; then, after an `=`, its value, quoted whole, bare to the next space, or a quote
* that opens and does not close, captured.
*/
const ATTRIBUTE_READ = /[\s/]*=?[^\s/=]*\s*(?:=\s*(?:"[^"]*"|'[^']*'|(["'])|\S*))?/y

/**
* Whether a tag's attributes, as the content check cut them at the tag's first `>`, leave a quoted
* value open, read in the order a browser reads them: a quote opens a value only where one begins,
* after an attribute's `=`, so in `title=x='y` it is part of a bare value.
* @param {string} attributes
*/
function quoteLeftOpen(attributes) {
ATTRIBUTE_READ.lastIndex = 0
while (ATTRIBUTE_READ.lastIndex < attributes.length) {
const read = ATTRIBUTE_READ.exec(attributes)
if (!read?.[0]) return false
if (read[1]) return true
}
return false
}
const SAFE_URL = /^(?:https?:|mailto:|ftp:|[^:]*$)/i

/**
Expand Down Expand Up @@ -164,6 +187,13 @@ export function markupText(value, field, report, { saved = false } = {}) {
if (!match[1]) {
// The slash of `<br />` is not part of the last attribute, as the content check reads it.
const attributes = match[3].trimEnd().replace(/\/$/, '')
// A quote the tag leaves open: a `>` inside a quoted value ended the tag here, as the content
// check splits markup, or the quote was never closed. A browser reads the value whole and the
// tag on to a later `>`, so the text in between is lost and what follows is drawn inside the
// tag's element (#222).
if (quoteLeftOpen(attributes)) {
once(`has <${tag}> with a quote it does not close: a > inside an attribute ends the tag there, so write &gt; for it`)
}
for (const [, name, double, single, bare] of attributes.matchAll(ATTRIBUTE)) {
const attribute = name.toLowerCase()
const content = double ?? single ?? bare ?? ''
Expand Down
17 changes: 17 additions & 0 deletions packages/author/tests/text.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,23 @@ describe('text as H5P stores it', () => {
expect(problems).toEqual(['has a comment, which H5P removes: take it out'])
})

it('refuses a quote a tag leaves open, as a > inside an attribute ends the tag there and a browser reads on past it (#222)', () => {
const problems = (value: string) => {
const said: string[] = []
markupText(value, { tags: ['a'] }, (message) => said.push(message))
return said
}
const open = 'has <a> with a quote it does not close: a > inside an attribute ends the tag there, so write &gt; for it'
expect(problems('<p><a title="a>b" href="https://example.org/">link</a> after</p>')).toEqual([open])
expect(problems("<p><a title='x>link</a></p>")).toEqual([open])
// Written as an entity, or quoted whole, or unquoted with an apostrophe in it, a value is no matter.
expect(problems('<p><a title="a&gt;b" data-note=\'it"s\' href="https://example.org/?q=a=b">link</a> <a href=it\'s>x</a></p>')).toEqual([])
// Read as a browser reads attributes: a quote opens a value only where one begins, after an
// attribute's `=`, so one inside a bare value opens nothing, and one after a closed value does.
expect(problems('<p><a title=x=\'y>z</a> <a t=x="y u=v>w</a></p>')).toEqual([])
expect(problems('<p><a title="a"b="c>d</a></p>')).toEqual([open])
})

it('takes time in proportion to the text, whatever it holds', () => {
// The patterns before took three seconds on the first two at 40,000 characters, and four times as long at twice that.
const start = performance.now()
Expand Down
Loading