From d7b8d66052db4c95be4a5a24a240ea1be8f3b268 Mon Sep 17 00:00:00 2001 From: Lilleman auf Larv Date: Thu, 19 Nov 2026 18:27:45 +0100 Subject: [PATCH] =?UTF-8?q?Answer=20stability=20round=201:=200=20?= =?UTF-8?q?=F0=9F=94=B4,=201=20=F0=9F=9F=A1=20(0=20misread);=20keep=20the?= =?UTF-8?q?=20HTML=20property's=20exact=20check=20narrow=20to=20raw=20<=20?= =?UTF-8?q?and=20>=20in=20attribute=20values,=20and=20name=20a=20marker=20?= =?UTF-8?q?parted=20by=20a=20definition=20or=20standing=20inline?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/conformance/adf-property.test.ts | 9 +++++++-- src/markdown/directive/id-marker.test.ts | 10 +++++++++- src/markdown/directive/syntax.ts | 2 +- src/markdown/parse/blocks.ts | 11 ++++++++++- 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/conformance/adf-property.test.ts b/src/conformance/adf-property.test.ts index 5dd49d5..6c13ca6 100644 --- a/src/conformance/adf-property.test.ts +++ b/src/conformance/adf-property.test.ts @@ -76,13 +76,18 @@ function edited(html: string, [at, removed, piece]: readonly [number, number, st return tokens.join('') } -// Without a carry element, HTML spells the document it reads one way, so writing that document gives back the very HTML read; a raw `<` or `>` in an attribute value is a browser's spelling of the entity adfToHtml writes (docs/decisions.md §`htmlToAdf` also reads the dialect as a browser serializes it). +// A raw `<` or `>` inside a quoted attribute value, which a browser's serializer may leave, and adfToHtml writes as an entity. +function escapedInAttributes(html: string): string { + return html.replace(/<[a-z][^\s>/]*(?:\s+[^\s=>/]+(?:="[^"]*")?)*\s*\/?>/g, (tag) => tag.replace(/="[^"]*"/g, (value) => value.replaceAll('<', '<').replaceAll('>', '>'))) +} + +// Without a carry element, HTML spells the document it reads one way, so writing that document gives back the very HTML read. function assertReadsBack(html: string): void { const read = htmlToAdf(html) if (!read.ok) return const rewritten = adfToHtml(read.value) assert.ok(rewritten.ok, rewritten.ok ? '' : `${rewritten.error.code}: ${rewritten.error.message} — reading ${JSON.stringify(html)}`) - if (!html.includes('class="adf-carry"') && !/="[^"]*[<>]/.test(html)) assert.equal(rewritten.value, html, `reading ${JSON.stringify(html)}`) + if (!html.includes('class="adf-carry"')) assert.equal(rewritten.value, escapedInAttributes(html), `reading ${JSON.stringify(html)}`) assert.deepEqual(htmlToAdf(rewritten.value), { ok: true, value: read.value }, `reading ${JSON.stringify(html)}`) } diff --git a/src/markdown/directive/id-marker.test.ts b/src/markdown/directive/id-marker.test.ts index 9efc990..1f627b9 100644 --- a/src/markdown/directive/id-marker.test.ts +++ b/src/markdown/directive/id-marker.test.ts @@ -177,6 +177,14 @@ test('refuses a marker whose blank line does not match its block', () => { refused('!adf:{localId=p}\n\nPart.\n', 'malformed-directive', { line: 1, offset: 0 }, 'right above') refused('!adf:{localId=r}\n---\n', 'malformed-directive', { line: 1, offset: 0 }, 'blank line') refused('!adf:{localId=l}\n2. a\n', 'malformed-directive', { line: 1, offset: 0 }, 'blank line') + refused('!adf:{localId=a}\n[x]: y\n---\n', 'malformed-directive', { line: 1, offset: 0 }, 'blank line') + refused('!adf:{localId=a}\n[x]: y\nPart.\n', 'malformed-directive', { line: 1, offset: 0 }, 'right above') +}) + +test('names a marker standing inline in the wrong place', () => { + refused('a !adf:{localId=a}\nb\n', 'malformed-directive', { line: 1, offset: 0 }, 'marker', 'end of a paragraph') + refused('[a !adf:{localId=a}](u)\n', 'malformed-directive', { line: 1, offset: 0 }, 'marker', 'end of a paragraph') + refused('*a !adf:{localId=a}*\n', 'malformed-directive', { line: 1, offset: 0 }, 'marker', 'end of a paragraph') }) test('refuses a marker line above a block that takes its id another way or has none', () => { @@ -197,7 +205,7 @@ test('names a trailing marker standing where the id takes the line above, or on refused('> Part !adf:{localId=p}\n', 'malformed-directive', { line: 1, offset: 0 }, 'one space') refused('> Part\t !adf:{localId=p}\n', 'malformed-directive', { line: 1, offset: 0 }, 'one space') refused('> ![a](/u) !adf:{localId=p}\n', 'unsupported-node-shape', { line: 1, offset: 0 }, 'mediaSingle', 'directive') - refused('## Part\t!adf:{localId=h}\n', 'malformed-directive', { line: 1, offset: 0 }, 'completes no directive') + refused('## Part\t!adf:{localId=h}\n', 'malformed-directive', { line: 1, offset: 0 }, 'marker') }) test('reads a trailing marker before the link reference definitions and the setext underline of its paragraph', () => { diff --git a/src/markdown/directive/syntax.ts b/src/markdown/directive/syntax.ts index 83b0d88..1688f0a 100644 --- a/src/markdown/directive/syntax.ts +++ b/src/markdown/directive/syntax.ts @@ -251,7 +251,7 @@ function readNestedDirective(text: string, index: number, depth: number, codeSpa function readClaimedDirective(text: string, index: number, depth: number, codeSpanCloser: CodeSpanCloser): Read { const name = inlineDirectiveName(text, index) - if (name === undefined) return { fault: malformedDirective(prefixFault) } + if (name === undefined) return { fault: malformedDirective(text.startsWith(markerOpener, index) ? markerFault : prefixFault) } if (depth > largestNesting) { return { fault: { code: 'unsupported-nesting-depth', message: `the input nests inline directives deeper than the ${largestNesting} levels the parser carries` } } } diff --git a/src/markdown/parse/blocks.ts b/src/markdown/parse/blocks.ts index 6126daa..527f4ba 100644 --- a/src/markdown/parse/blocks.ts +++ b/src/markdown/parse/blocks.ts @@ -611,11 +611,20 @@ function takeParagraph(walk: Walk): Extract | Paragrap const trailed = walk.claims.directives ? readTrailingMarker(lines) : undefined const text = readLinkDefinitions(walk.definitions, trailed?.text ?? lines) if (text === '' && trailed !== undefined) return { fault: malformedDirective(`an ${markerOpener}…} marker ends a paragraph holding no text once its link reference definitions are read; ${directiveEscape}`), kind: 'fault', position: leaf.position } - if (text === '') return undefined + if (text === '') return partMarker(walk) const kept = leaf.lines[leaf.lines.length - text.split('\n').length] return { kind: 'paragraph', position: kept?.position ?? leaf.position, ...trailedText(text, trailed) } } +// Definitions between a marker line and its block are no blank line: the fault stands after the marker, which reads it first. +function partMarker(walk: Walk): undefined { + const previous = currentBlocks(walk).at(-1) + if (previous?.kind !== 'marker' || previous.placement !== 'line') return undefined + const fault = malformedDirective(`only a blank line, never a link reference definition, parts an ${markerOpener}…} marker from the rule or list below it, and no line parts it from any other block; ${directiveEscape}`) + currentBlocks(walk).push({ fault, kind: 'fault', position: previous.position }) + return undefined +} + // A block holding no marker has no `trailing` key. function trailedText(text: string, trailed: TrailingMarker | undefined): { text: string; trailing?: Read } { return trailed === undefined ? { text } : { text, trailing: trailed.attributes }