3
0
mirror of https://github.com/larvit/adf-codec.git synced 2026-11-20 07:05:44 +01:00

Answer stability round 1: 0 🔴, 1 🟡 (0 misread); keep the HTML property's exact check narrow to raw < and > in attribute values, and name a marker parted by a definition or standing inline

This commit is contained in:
2026-11-19 18:27:45 +01:00
committed by Lillem4n
parent 5945a62b54
commit d7b8d66052
4 changed files with 27 additions and 5 deletions
+7 -2
View File
@@ -76,13 +76,18 @@ function edited(html: string, [at, removed, piece]: readonly [number, number, st
return tokens.join('') 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('<', '&lt;').replaceAll('>', '&gt;')))
}
// 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 { function assertReadsBack(html: string): void {
const read = htmlToAdf(html) const read = htmlToAdf(html)
if (!read.ok) return if (!read.ok) return
const rewritten = adfToHtml(read.value) const rewritten = adfToHtml(read.value)
assert.ok(rewritten.ok, rewritten.ok ? '' : `${rewritten.error.code}: ${rewritten.error.message} — reading ${JSON.stringify(html)}`) 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)}`) assert.deepEqual(htmlToAdf(rewritten.value), { ok: true, value: read.value }, `reading ${JSON.stringify(html)}`)
} }
+9 -1
View File
@@ -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=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=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=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', () => { 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 !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('> 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('> ![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', () => { test('reads a trailing marker before the link reference definitions and the setext underline of its paragraph', () => {
+1 -1
View File
@@ -251,7 +251,7 @@ function readNestedDirective(text: string, index: number, depth: number, codeSpa
function readClaimedDirective(text: string, index: number, depth: number, codeSpanCloser: CodeSpanCloser): Read<DirectiveSpan> { function readClaimedDirective(text: string, index: number, depth: number, codeSpanCloser: CodeSpanCloser): Read<DirectiveSpan> {
const name = inlineDirectiveName(text, index) 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) { if (depth > largestNesting) {
return { fault: { code: 'unsupported-nesting-depth', message: `the input nests inline directives deeper than the ${largestNesting} levels the parser carries` } } return { fault: { code: 'unsupported-nesting-depth', message: `the input nests inline directives deeper than the ${largestNesting} levels the parser carries` } }
} }
+10 -1
View File
@@ -611,11 +611,20 @@ function takeParagraph(walk: Walk): Extract<Block, { kind: 'fault' }> | Paragrap
const trailed = walk.claims.directives ? readTrailingMarker(lines) : undefined const trailed = walk.claims.directives ? readTrailingMarker(lines) : undefined
const text = readLinkDefinitions(walk.definitions, trailed?.text ?? lines) 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 === '' && 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] const kept = leaf.lines[leaf.lines.length - text.split('\n').length]
return { kind: 'paragraph', position: kept?.position ?? leaf.position, ...trailedText(text, trailed) } 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. // A block holding no marker has no `trailing` key.
function trailedText(text: string, trailed: TrailingMarker | undefined): { text: string; trailing?: Read<DirectiveAttributes> } { function trailedText(text: string, trailed: TrailingMarker | undefined): { text: string; trailing?: Read<DirectiveAttributes> } {
return trailed === undefined ? { text } : { text, trailing: trailed.attributes } return trailed === undefined ? { text } : { text, trailing: trailed.attributes }