3
0
mirror of https://github.com/larvit/adf-codec.git synced 2026-11-18 14:15:44 +01:00

Pin the messages and limits of the new HTML blocks, and simplify the guards their mutants showed

This commit is contained in:
2026-11-15 01:18:33 +01:00
committed by Lillem4n
parent f5c8a48706
commit af5c0d7f1c
7 changed files with 142 additions and 16 deletions
+32
View File
@@ -672,6 +672,38 @@ test('carries a task or decision list holding what its elements cannot', () => {
assert.equal(carriedList({ content: [{ attrs: { state: 'DECIDED' }, content: [], type: 'decisionItem' }], type: 'decisionList' }), true)
})
test('refuses, at the item\'s text, an inline carry nested past 500 in a task or decision item', () => {
const deep = nested('unknownInline', largestNesting, text('x'))
const task: AdfNode = { attrs: { state: 'TODO' }, content: [deep], type: 'taskItem' }
assert.equal(refusal(adfToHtml(document({ content: [task], type: 'taskList' }))), 'unsupported-nesting-depth at /content/0/content/0/content/0')
const decision: AdfNode = { attrs: { state: 'DECIDED' }, content: [deep], type: 'decisionItem' }
assert.equal(refusal(adfToHtml(document({ content: [decision], type: 'decisionList' }))), 'unsupported-nesting-depth at /content/0/content/0/content/0')
})
test('writes panels, expands and nested expands nested 500 levels, and refuses the next level', () => {
for (const type of ['panel', 'expand', 'nestedExpand']) {
assert.equal(refusal(adfToHtml(document(nested(type, largestNesting, paragraph(text('x')))))), 'written')
const deep = adfToHtml(document(nested(type, largestNesting + 1, paragraph(text('x')))))
assert.equal(refusal(deep).startsWith('unsupported-nesting-depth at /'), true)
assert.equal(!deep.ok && deep.error.message.includes('500'), true)
}
})
test('carries a media or a paragraph whose marks only look like the ones HTML spells', () => {
const external: AdfAttributes = { type: 'external', url: 'https://example.com/moon.jpg' }
const single = (media: AdfNode): AdfNode => ({ content: [media], type: 'mediaSingle' })
const link: AdfMark = { attrs: { href: 'https://example.com/' }, type: 'link' }
assert.equal(carriedBlock(single({ attrs: external, marks: [], type: 'media' })), true)
assert.equal(carriedBlock(single({ attrs: external, marks: [link, link], type: 'media' })), true)
assert.equal(carriedBlock(single({ attrs: external, marks: [{ attrs: { href: 'https://example.com/' }, type: 'border' }], type: 'media' })), true)
assert.equal(carriedBlock(single({ attrs: external, marks: [{ type: 'link' }], type: 'media' })), true)
assert.equal(carriedBlock(single({ attrs: external, marks: [{ attrs: {}, type: 'link' }], type: 'media' })), true)
assert.equal(carriedBlock(single({ attrs: { ...external, alt: 5 }, type: 'media' })), true)
assert.equal(carriedBlock(single({ attrs: { ...external, height: 0 }, type: 'media' })), true)
assert.equal(carriedBlock(single({ attrs: { ...external, width: 0 }, type: 'media' })), true)
assert.equal(carriedBlock({ content: [text('x')], marks: [{ attrs: { align: 'center' }, type: 'indentation' }], type: 'paragraph' }), true)
})
test('refuses, at the item\'s text, a card a link marks in a task item', () => {
const card: AdfNode = { attrs: { data: { url: 'https://example.com/' }, url: 'https://example.com/' }, marks: [{ attrs: { href: 'https://example.com/' }, type: 'link' }], type: 'inlineCard' }
const refused = adfToHtml(document({ content: [{ attrs: { state: 'TODO' }, content: [card], type: 'taskItem' }], type: 'taskList' }))
+1 -1
View File
@@ -112,7 +112,7 @@ function taskPieces({ depth, node, path }: Block, type: keyof typeof taskLists):
const opener = itemOpener(type, item)
if (opener === undefined) return undefined
if (index > 0) items.push('\n')
items.push({ ...opener, depth: depth + 1, kind: 'item', node: item, path: [...path, 'content', index], tag: 'li' })
items.push({ ...opener, depth, kind: 'item', node: item, path: [...path, 'content', index], tag: 'li' })
}
return containerPieces('ul', ` class="${taskLists[type].listClass}"${attributes}`, node.content === undefined ? undefined : items)
}
+13 -6
View File
@@ -1,4 +1,5 @@
import type { AdfMark } from '../adf/document.ts'
import type { JsonValue } from '../json-value.ts'
import { colourDeclaration, readColour } from './colour-style.ts'
import { nodeAttrs } from '../adf/document.ts'
@@ -17,18 +18,24 @@ const colourElements: ReadonlyMap<string, { element: string; property: string }>
['backgroundColor', { element: 'mark', property: 'background-color' }],
['textColor', { element: 'span', property: 'color' }],
])
const scripts = ['sub', 'sup']
const scripts: ReadonlyMap<JsonValue | undefined, 'sub' | 'sup'> = new Map<JsonValue | undefined, 'sub' | 'sup'>([
['sub', 'sub'],
['sup', 'sup'],
])
// The element a mark but a link writes around text; `undefined` where the mark holds what no element spells.
export function markTag(mark: AdfMark): MarkTag | undefined {
const attrs = nodeAttrs(mark)
const plain = markElements.get(mark.type)
if (plain !== undefined) return mark.attrs === undefined ? tagOf(plain, '') : undefined
const { type } = attrs
if (mark.type === 'subsup') return Object.keys(attrs).length === 1 && typeof type === 'string' && scripts.includes(type) ? tagOf(type, '') : undefined
if (mark.type === 'subsup') {
const script = scripts.get(attrs['type'])
return Object.keys(attrs).length === 1 && script !== undefined ? tagOf(script, '') : undefined
}
const colour = colourElements.get(mark.type)
const declaration = colour === undefined || Object.keys(attrs).length !== 1 ? undefined : colourDeclaration(colour.property, attrs['color'])
return colour === undefined || declaration === undefined ? undefined : tagOf(colour.element, ` style="${declaration}"`)
if (colour === undefined) return undefined
const declaration = Object.keys(attrs).length === 1 ? colourDeclaration(colour.property, attrs['color']) : undefined
return declaration === undefined ? undefined : tagOf(colour.element, ` style="${declaration}"`)
}
// The mark a start tag but an `<a>` opens, as `markTag` writes it; `undefined` where it opens none.
@@ -45,7 +52,7 @@ export function markOfTag(name: string, attributes: readonly (readonly [string,
function markOfElement(name: string): AdfMark | undefined {
for (const [type, element] of markElements) if (element === name) return { type }
return scripts.includes(name) ? { attrs: { type: name }, type: 'subsup' } : undefined
return scripts.has(name) ? { attrs: { type: name }, type: 'subsup' } : undefined
}
function tagOf(element: string, attributes: string): MarkTag {
+5 -4
View File
@@ -30,12 +30,13 @@ export function mediaImage(media: AdfNode): string | undefined {
export function readMediaImage(pairs: readonly (readonly [string, string])[]): Read<AdfAttributes> {
let rest = pairs
const attrs: AdfAttributes = {}
const classed = rest[0]?.[0] === 'class'
if (classed && rest[0]?.[1] !== imageClass) return { fault: { code: 'unmappable-html', message: `an <img> in the dialect has class="${imageClass}" or none` } }
if (classed) rest = rest.slice(1)
const [first] = rest
const classed = first?.[0] === 'class' ? first : undefined
if (classed !== undefined && classed[1] !== imageClass) return { fault: { code: 'unmappable-html', message: `an <img> in the dialect has class="${imageClass}" or none` } }
if (classed !== undefined) rest = rest.slice(1)
const src = rest[0]?.[0] === 'src' ? rest[0][1] : undefined
if (src !== undefined) rest = rest.slice(1)
if (classed === (src !== undefined)) return { fault: { code: 'unmappable-html', message: `an <img> holds src, or class="${imageClass}" where its media holds no url, and never both` } }
if ((classed !== undefined) === (src !== undefined)) return { fault: { code: 'unmappable-html', message: `an <img> holds src, or class="${imageClass}" where its media holds no url, and never both` } }
if (src !== undefined) attrs['url'] = src
const alt = rest[0]?.[0] === 'alt' ? rest[0][1] : undefined
if (alt !== undefined) {
+87
View File
@@ -630,6 +630,21 @@ test('reads blocks nested 500 levels, and refuses the next level, a list\'s item
assert.equal(refusal(htmlToAdf('<blockquote>\n'.repeat(1_000_000))).startsWith('unsupported-nesting-depth'), true)
})
test('reads panels, expands and nested expands nested 500 levels, and refuses the next level', () => {
const panels = (levels: number): string => `${'<div class="adf-panel">\n'.repeat(levels)}<p>x</p>${'\n</div>'.repeat(levels)}\n`
const expands = (levels: number): string => `${'<details class="adf-expand">\n<summary>t</summary>\n'.repeat(levels)}<p>x</p>${'\n</details>'.repeat(levels)}\n`
const bare = (levels: number): string => `${'<details class="adf-nested-expand">\n'.repeat(levels)}<p>x</p>${'\n</details>'.repeat(levels)}\n`
for (const build of [panels, expands, bare]) {
assert.equal(refusal(htmlToAdf(build(500))), 'read')
assert.equal(refusal(htmlToAdf(build(501))).startsWith('unsupported-nesting-depth'), true)
}
})
test('reads a media of no url as an <img> of class adf-media', () => {
const single = htmlToAdf('<figure class="adf-media-single"><img class="adf-media" data-type="external"></figure>\n')
assert.deepEqual(read(single), document({ content: [{ attrs: { type: 'external' }, type: 'media' }], type: 'mediaSingle' }))
})
test('refuses a carried node whose JSON nests past the levels its depth leaves, and an inline carry past 500', () => {
const carried = carry('div', { attrs: { a: nested(1, 498, (inner) => [inner]) }, type: 'unknownBlock' }, '')
assert.equal(refusal(htmlToAdf(`${carried}\n`)), 'read')
@@ -638,6 +653,78 @@ test('refuses a carried node whose JSON nests past the levels its depth leaves,
assert.equal(refusal(htmlToAdf(`<p>${carry('span', inline, '')}</p>\n`)), 'unsupported-nesting-depth at /content/0/content/0 line 1 offset 0')
})
// Each refusal of the blocks and marks adfToHtml writes as HTML, by its code, path and message: the faults of one line share a position.
const pinnedRefusals: readonly (readonly [string, string])[] = [
['<ul class="adf-task-list">\n<li class="adf-task" data-state="DONE"><input type="checkbox" disabled=""> a</li>\n</ul>\n', 'unmappable-html at /content/0/content/0: a task item starts with <input type="checkbox" checked="" disabled="">, checked where its data-state is DONE: found "<input type=\\"checkbox\\" d"…'],
['<ul class="adf-task-list">\n<li class="adf-task" data-state="TODO"><input type="checkbox" checked="" disabled=""> a</li>\n</ul>\n', 'unmappable-html at /content/0/content/0: a task item starts with <input type="checkbox" disabled="">, checked where its data-state is DONE: found "<input type=\\"checkbox\\" c"…'],
['<ul class="adf-task-list">\n<li class="adf-task" data-state="MAYBE"><input type="checkbox" disabled=""> a</li>\n</ul>\n', 'unmappable-html at /content/0/content/0: an <li> of a task list has data-state="DONE" or data-state="TODO"'],
['<ul class="adf-task-list">\n<li class="adf-task"><input type="checkbox" disabled=""> a</li>\n</ul>\n', 'unmappable-html at /content/0/content/0: an <li> of a task list has data-state="DONE" or data-state="TODO"'],
['<ul class="adf-task-list">\n<li data-state="TODO"><input type="checkbox" disabled=""> a</li>\n</ul>\n', 'unmappable-html at /content/0/content/0: an <li> of this list has class="adf-task" first'],
['<ul class="adf-task-list">\n<li class="adf-task" data-state="TODO"><input type="checkbox" disabled="">a</li>\n</ul>\n', 'unmappable-html at /content/0/content/0: a task item\'s text stands a space after its checkbox: found "a</li>\\n</ul>\\n"'],
['<ul class="adf-task-list">\n<li class="adf-task" data-state="TODO"><input type="checkbox" disabled=""> </li>\n</ul>\n', 'unmappable-html at /content/0/content/0: a task item holds text where a space follows its checkbox: found "</li>\\n</ul>\\n"'],
['<ul class="adf-task-list">\n<li class="adf-task" data-state="TODO">a</li>\n</ul>\n', 'unmappable-html at /content/0/content/0: a task item starts with <input type="checkbox" disabled="">, checked where its data-state is DONE: found "a</li>\\n</ul>\\n"'],
['<ul class="adf-task-list">\n<li class="adf-task" data-state="TODO"><input type="checkbox"> a</li>\n</ul>\n', 'unmappable-html at /content/0/content/0: a task item starts with <input type="checkbox" disabled="">, checked where its data-state is DONE: found "<input type=\\"checkbox\\"> "…'],
['<ul class="adf-task-list">\n<li class="adf-task" data-state="TODO"><input type="radio" disabled=""> a</li>\n</ul>\n', 'unmappable-html at /content/0/content/0: a task item starts with <input type="checkbox" disabled="">, checked where its data-state is DONE: found "<input type=\\"radio\\" disa"…'],
['<ul class="adf-task-list">\n<li class="adf-task" data-state="TODO"><input type="checkbox" disabled="x"> a</li>\n</ul>\n', 'unmappable-html at /content/0/content/0: a task item starts with <input type="checkbox" disabled="">, checked where its data-state is DONE: found "<input type=\\"checkbox\\" d"…'],
['<ul class="adf-task-list">\n<li class="adf-task" data-state="TODO"><input type="checkbox" required="" disabled=""> a</li>\n</ul>\n', 'unmappable-html at /content/0/content/0: a task item starts with <input type="checkbox" disabled="">, checked where its data-state is DONE: found "<input type=\\"checkbox\\" r"…'],
['<ul class="adf-task-list">\n<li class="adf-task" data-state="TODO"><input disabled="" type="checkbox"> a</li>\n</ul>\n', 'unmappable-html at /content/0/content/0: a task item starts with <input type="checkbox" disabled="">, checked where its data-state is DONE: found "<input disabled=\\"\\" type="…'],
['<ul class="adf-task-list">\n<li class="adf-task" data-state="TODO"><b>x</b></li>\n</ul>\n', 'unmappable-html at /content/0/content/0: a task item starts with <input type="checkbox" disabled="">, checked where its data-state is DONE: found "<b>x</b></li>\\n</ul>\\n"'],
['<ul class="adf-task-list">\n<li class="adf-task" data-state="TODO"><input type="checkbox" disabled=""> a\n<p>b</p></li>\n</ul>\n', 'unmappable-html at /content/0/content/0: <p> as written here is no inline element of the dialect: found "<p>b</p></li>\\n</ul>\\n"'],
['<ul class="adf-task-list">\n<li class="adf-task" data-state="TODO" data-extra="1"><input type="checkbox" disabled=""> a</li>\n</ul>\n', 'unsupported-node-shape at /content/0/content/0: <li> takes no data-extra attribute'],
['<ul class="adf-task-list">\n<li class="adf-task" data-state="TODO"><input type="checkbox" disabled=""> a</li>\n<li class="adf-decision" data-state="DECIDED">b</li>\n</ul>\n', 'unmappable-html at /content/0/content/1: an <li> of this list has class="adf-task" first'],
['<ul class="adf-decision-list">\n<li class="adf-decision" data-state="DECIDED">a</li>\n<li>b</li>\n</ul>\n', 'unmappable-html at /content/0/content/1: an <li> of this list has class="adf-decision" first'],
['<ul class="adf-decision-list">\n<li class="adf-decision" data-state="DECIDED" data-extra="1">a</li>\n</ul>\n', 'unsupported-node-shape at /content/0/content/0: <li> takes no data-extra attribute'],
['<ul class="adf-task-list" data-extra="1"></ul>\n', 'unsupported-node-shape at /content/0: <ul> takes no data-extra attribute'],
['<ul class="adf-task-list">\n</ul>\n', 'unmappable-html at /content/0/content/0: a list holds <li> items, one to a line: found "</ul>\\n"'],
['<div class="adf-panel" data-panel-type="info" data-extra="1">\n<p>a</p>\n</div>\n', 'unsupported-node-shape at /content/0: <div> takes no data-extra attribute'],
['<div class="adf-panel" data-panel-type="info" data-local-id="p">\n<p>a</p>\n</div>\n', 'unmappable-html at /content/0: the data-* attributes stand once each, sorted by the attribute they name: found data-local-id after data-panel-type'],
['<div class="adf-panel" data-panel-type="info">\n</div>\n', 'unmappable-html at /content/0/content/0: a block element of the dialect stands here: found "</div>\\n"'],
['<div class="adf-panel" data-panel-type="info"><p>a</p></div>\n', 'unmappable-html at /content/0: <div> holds </div> straight after it, or a line feed and its first block: found "<p>a</p></div>\\n"'],
['<div id="x" class="adf-panel">\n<p>a</p>\n</div>\n', 'unmappable-html at /content/0: a <div> of the dialect is a carry element, holding class="adf-carry" and then data-adf alone: found "<div id=\\"x\\" class=\\"adf-p"…'],
['<details class="adf-expand" open="">\n<p>a</p>\n</details>\n', 'unmappable-html at /content/0: <details> takes no open attribute'],
['<details class="adf-expand" data-title="a">\n<p>a</p>\n</details>\n', 'unsupported-node-shape at /content/0: <details> takes no data-title attribute'],
['<details>\n<p>a</p>\n</details>\n', 'unmappable-html at /content/0: <details> starts with class="adf-expand" or class="adf-nested-expand": found "<details>\\n<p>a</p>\\n</det"…'],
['<details class="adf-expand">\n</details>\n', 'unmappable-html at /content/0/content/0: a block element of the dialect stands here: found "</details>\\n"'],
['<details class="adf-expand">\n<summary>a<b>x</b></summary>\n<p>a</p>\n</details>\n', 'unmappable-html at /content/0: text escapes & as &amp;, < as &lt; and > as &gt;, and holds no character HTML cannot hold: found "<b>x</b></summary>\\n<p>a<"…'],
['<details class="adf-expand">\n<summary>a</summary><p>a</p>\n</details>\n', 'unmappable-html at /content/0: <summary> holds the title, then a line feed and the first block, or a line feed and </details>: found "<p>a</p>\\n</details>\\n"'],
['<details class="adf-expand">\n<summary>a</summary>\n<summary>b</summary>\n</details>\n', 'unmappable-html at /content/0/content/0: <summary> as written here is no block element of the dialect: found "<summary>b</summary>\\n</d"…'],
['<details class="adf-expand"><summary>a</summary></details>\n', 'unmappable-html at /content/0: <details> holds </details> straight after it, or a line feed and its first block: found "<summary>a</summary></de"…'],
['<figure>\n</figure>\n', 'unmappable-html at /content/0: <figure> starts with class="adf-media-single": found "<figure>\\n</figure>\\n"'],
['<figure class="x"><img src="u"></figure>\n', 'unmappable-html at /content/0: <figure> starts with class="adf-media-single": found "<figure class=\\"x\\"><img s"…'],
['<figure class="adf-media-single" data-extra="1"><img src="u"></figure>\n', 'unsupported-node-shape at /content/0: <figure> takes no data-extra attribute'],
['<figure class="adf-media-single"><p>x</p></figure>\n', 'unmappable-html at /content/0/content/0: a <figure> holds an <img>, inside <a href="…"> where a link marks it: found "<p>x</p></figure>\\n"'],
['<figure class="adf-media-single"><a><img src="u"></a></figure>\n', 'unmappable-html at /content/0/content/0: a <figure> holds an <img>, inside <a href="…"> where a link marks it: found "<a><img src=\\"u\\"></a></fi"…'],
['<figure class="adf-media-single"><a href="u"><img src="u">x</a></figure>\n', 'unmappable-html at /content/0/content/0: <a> around an image ends at </a>: found "x</a></figure>\\n"'],
['<figure class="adf-media-single"><img src="u" height="12a"></figure>\n', 'unmappable-html at /content/0/content/0: height on <img> holds a positive whole number written as JavaScript writes it'],
['<figure class="adf-media-single"><img src="u" height="0"></figure>\n', 'unmappable-html at /content/0/content/0: height on <img> holds a positive whole number written as JavaScript writes it'],
['<figure class="adf-media-single"><img src="u">x</figure>\n', 'unmappable-html at /content/0: <figure> holds its image, then its <figcaption>, then </figure>: found "x</figure>\\n"'],
['<figure class="adf-media-single"><img src="u"><figcaption>c</figcaption>x</figure>\n', 'unmappable-html at /content/0: <figure> holds its image, then its <figcaption>, then </figure>: found "x</figure>\\n"'],
['<figure class="adf-media-single"><img src="u"><figcaption data-bogus="1">c</figcaption></figure>\n', 'unsupported-node-shape at /content/0/content/1: <figcaption> takes no data-bogus attribute'],
['<figure class="adf-media-single"><img src="u"><figcaption style="text-align:center">c</figcaption></figure>\n', 'unmappable-html at /content/0/content/1: <figcaption> takes no style attribute'],
['<figure class="adf-media-single"><img src="u"><figcaption>c<p>d</p></figcaption></figure>\n', 'unmappable-html at /content/0/content/1: <p> as written here is no inline element of the dialect: found "<p>d</p></figcaption></f"…'],
['<figure class="adf-media-single"><img src="u" class="adf-media"></figure>\n', 'unmappable-html at /content/0/content/0: <img> takes no class attribute'],
['<figure class="adf-media-single"><img></figure>\n', 'unmappable-html at /content/0/content/0: an <img> holds src, or class="adf-media" where its media holds no url, and never both'],
['<figure class="adf-media-single"><img class="x"></figure>\n', 'unmappable-html at /content/0/content/0: an <img> in the dialect has class="adf-media" or none'],
['<figure class="adf-media-single"><img src="u" data-type="external"></figure>\n', 'unmappable-html at /content/0/content/0: an <img> with src leaves out data-type where its media is external'],
['<figure class="adf-media-single"><img src="u" data-bogus="1"></figure>\n', 'unsupported-node-shape at /content/0/content/0: <img> takes no data-bogus attribute'],
['<div class="adf-media-group" data-x="1"></div>\n', 'unmappable-html at /content/0: <div class="adf-media-group"> takes no other attribute: found "<div class=\\"adf-media-gr"…'],
['<div class="adf-media-group"><span></span></div>\n', 'unmappable-html at /content/0/content/0: a media group holds <span class="adf-file"> files: found "<span></span></div>\\n"'],
['<div class="adf-media-group"><span class="x"></span></div>\n', 'unmappable-html at /content/0/content/0: a media group holds <span class="adf-file"> files: found "<span class=\\"x\\"></span><"…'],
['<div class="adf-media-group"><span class="adf-file" data-id="a" data-type="file"></span><span class="adf-file" data-bogus="1"></span></div>\n', 'unsupported-node-shape at /content/0/content/1: <span> takes no data-bogus attribute'],
['<div class="adf-media-group"><span class="adf-file" data-id="a" data-type="file">x</span></div>\n', 'unmappable-html at /content/0/content/0: <span class="adf-file"> holds nothing, and ends at </span>: found "x</span></div>\\n"'],
['<div class="adf-media-group"><span class="adf-file" data-id="a" data-type="file"></span>x</div>\n', 'unmappable-html at /content/0: <div> holds its <span class="adf-file"> files, then </div>: found "x</div>\\n"'],
['<p style="color:red">a</p>\n', 'unmappable-html at /content/0: style on <p> holds text-align:center or text-align:right'],
['<h1 style="text-align:left">a</h1>\n', 'unmappable-html at /content/0: style on <h1> holds text-align:center or text-align:right'],
['<p>a <span title="color:red">b</span></p>\n', 'unmappable-html at /content/0/content/1: a <span> of the dialect is a carry element, holding class="adf-carry" and then data-adf alone, or opens with class="adf-emoji", "adf-mention" or "adf-status": found "<span title=\\"color:red\\">"…'],
]
for (const [html, expected] of pinnedRefusals) {
test(`refuses with its code, path and message: ${JSON.stringify(html)}`, () => {
const result = htmlToAdf(html)
assert.equal(result.ok ? 'read' : `${result.error.code} at /${result.error.path.join('/')}: ${result.error.message}`, expected)
})
}
// The documents adfToHtml's own tests write, by what each holds.
const written: Readonly<Record<string, AdfDocument>> = {
'a panel holding a code block': document({ attrs: { panelType: 'info' }, content: [paragraph(text('Run this:')), { attrs: { language: 'python' }, content: [text('if ready:\n deploy()')], type: 'codeBlock' }], type: 'panel' }),
+1 -2
View File
@@ -47,8 +47,7 @@ function readItemAttributes(list: TaskListType, pairs: readonly (readonly [strin
const data = readDataAttributes(rest, 'li', itemAttributes, [])
if (data.fault !== undefined) return { fault: data.fault }
if (list === 'decisionList') return { value: { attrs: data.value, done: undefined } }
const state = data.value['state']
const done = typeof state === 'string' ? taskStates.get(state) : undefined
const done = taskStates.get(data.value['state'])
if (done === undefined) return { fault: { code: 'unmappable-html', message: 'an <li> of a task list has data-state="DONE" or data-state="TODO"' } }
return { value: { attrs: data.value, done } }
}
+3 -3
View File
@@ -1,10 +1,11 @@
import type { AdfNode } from '../adf/document.ts'
import type { JsonValue } from '../json-value.ts'
import { dataAttributes } from './data-attributes.ts'
import { nodeAttrs } from '../adf/document.ts'
import { taskLists, type TaskListType } from './block-classes.ts'
// A task item's two states, and whether the checkbox before its text is checked.
export const taskStates: ReadonlyMap<string, boolean> = new Map([
export const taskStates: ReadonlyMap<JsonValue | undefined, boolean> = new Map<JsonValue | undefined, boolean>([
['DONE', true],
['TODO', false],
])
@@ -16,7 +17,6 @@ export function itemOpener(list: TaskListType, item: AdfNode): { attributes: str
if (data === undefined) return undefined
const attributes = ` class="${itemClass}"${data}`
if (list === 'decisionList') return { attributes, lead: '' }
const state = nodeAttrs(item)['state']
const done = typeof state === 'string' ? taskStates.get(state) : undefined
const done = taskStates.get(nodeAttrs(item)['state'])
return done === undefined ? undefined : { attributes, lead: `<input type="checkbox"${done ? ' checked=""' : ''} disabled="">` }
}