diff --git a/design/author.md b/design/author.md index b1295956..6c8df1b0 100644 --- a/design/author.md +++ b/design/author.md @@ -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 `

` where the field's `enterMode` is `p` or its tags offer headings, `pre`, `p` or `div`, in `

` otherwise. Every plain default is escaped too, English or translated, and a `null` default (Course Presentation's colours) is diff --git a/packages/author/lib/text.mjs b/packages/author/lib/text.mjs index 5a8b9192..d09400e4 100644 --- a/packages/author/lib/text.mjs +++ b/packages/author/lib/text.mjs @@ -106,6 +106,29 @@ function stylePatterns(field) { */ const PIECE = /(<(?=[^a-zA-Z!/])||$)|<[^>]*(>|$)|>)/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 /** @@ -164,6 +187,13 @@ export function markupText(value, field, report, { saved = false } = {}) { if (!match[1]) { // The slash of `
` 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 > for it`) + } for (const [, name, double, single, bare] of attributes.matchAll(ATTRIBUTE)) { const attribute = name.toLowerCase() const content = double ?? single ?? bare ?? '' diff --git a/packages/author/tests/text.test.ts b/packages/author/tests/text.test.ts index f69630a3..44a0c947 100644 --- a/packages/author/tests/text.test.ts +++ b/packages/author/tests/text.test.ts @@ -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 with a quote it does not close: a > inside an attribute ends the tag there, so write > for it' + expect(problems('

link after

')).toEqual([open]) + expect(problems("

link x

')).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('

z w

')).toEqual([]) + expect(problems('