import assert from 'node:assert/strict'
import test from 'node:test'
import type { AdfDocument, AdfMark, AdfNode } from '../../adf/document.ts'
import type { ParseError, Result } from '../../result.ts'
import { adfToHtml, htmlToAdf } from '../../index.ts'
const ids = {
item: '019ad5af-efd9-7f4e-a7c9-6aef16d4945f',
list: '019d8e77-7df8-7a83-91ea-43a7ca2aae3b',
mention: '0199f4c2-8b1e-7d03-9a6f-2c5e8d71b4a0',
paragraph: '01957ad9-e3f7-70aa-a5f3-4c013274f488',
quote: '019d2fe4-3e67-7b76-b40a-a776a8f76aea',
}
function document(...content: AdfNode[]): AdfDocument {
return { content, type: 'doc', version: 1 }
}
function paragraph(...content: AdfNode[]): AdfNode {
return content.length === 0 ? { type: 'paragraph' } : { content, type: 'paragraph' }
}
function heading(level: number, ...content: AdfNode[]): AdfNode {
return content.length === 0 ? { attrs: { level }, type: 'heading' } : { attrs: { level }, content, type: 'heading' }
}
function text(value: string, ...marks: AdfMark[]): AdfNode {
return marks.length === 0 ? { text: value, type: 'text' } : { marks, text: value, type: 'text' }
}
function item(...content: AdfNode[]): AdfNode {
return { content, type: 'listItem' }
}
function read(result: Result Read the manual. Read the manual: a < b && c > "d" \'e\'\tf\ng\fh < a "b" "c" d e a\nb b Check the collation. a\r a a a\r\r\nb a a b cd a b ${carry('span', said, shown)} ${carry('span', underlined, `${opener}a<bLevel 1
\n
\nLevel 6
\n')), document(heading(1, text('Level 1')), paragraph(text('Read the manual.')), { type: 'rule' }, heading(6, text('Level 6'))))
assert.deepEqual(read(htmlToAdf('\n\n')), document(paragraph(), heading(2)))
})
test('reads text\'s marks from its elements, the outermost first, and each run of text as one text node', () => {
const link: AdfMark = { attrs: { href: 'https://example.com/a?b=1&c=2', title: 'The "A" page' }, type: 'link' }
const html = 'npm ci or this
Line two
\n')), document({ content: [text('say "a" b')], type: 'codeBlock' }))
const link: AdfMark = { attrs: { href: 'https://x.test/?ac&d', title: 'a b' }, type: 'link' }
assert.deepEqual(read(htmlToAdf('\n')), document(paragraph(text('x', link))))
assert.deepEqual(read(htmlToAdf('\n')), document(paragraph(text('x', link))))
assert.deepEqual(read(htmlToAdf('say "a" b
\n')), document({ attrs: { style: 'a "b' }, type: 'rule' }))
})
test('reads a line ending of CRLF as a line feed wherever the dialect has one, and keeps a lone carriage return refused', () => {
const crlf = (html: string): string => html.replaceAll('\n', '\r\n')
assert.deepEqual(read(htmlToAdf(crlf('
\n
\n'))), document(paragraph(text('a\nb')), { type: 'rule' }, { content: [text('c\n d')], type: 'codeBlock' }))
assert.deepEqual(read(htmlToAdf(crlf('c\n d\n
\n'))), document({ content: [{ content: [item(paragraph(text('Bolt')), paragraph(text('b')))], type: 'bulletList' }], type: 'blockquote' }))
assert.deepEqual(read(htmlToAdf(crlf(`${carry('div', panel, '\n
\n
\n')), document({ attrs: { style: 'a\nb' }, type: 'rule' }))
for (const html of ['
A "quote" here
')}\r\n`)), document(block)) }) test('refuses an edit to what a carry element shows even where a browser would spell the same text so', () => { const said: AdfNode = { attrs: { id: ids.mention, text: 'Say "hi"' }, type: 'mention' } const underlined: AdfNode = text('a', { type: 'underline' }, { attrs: { href: 'x', title: 'T"' }, type: 'link' }) assert.equal(refusal(htmlToAdf(`${carry('span', said, 'Say "hi')}
\n`)), 'unmappable-html at /content/0/content/0 line 1 offset 0') assert.equal(refusal(htmlToAdf(`${carry('span', said, 'Say "hi"')}
\n`)), 'unmappable-html at /content/0/content/0 line 1 offset 0') assert.equal(refusal(htmlToAdf(`${carry('span', underlined, 'a')}
\n`)), 'unmappable-html at /content/0/content/0 line 1 offset 0') assert.equal(refusal(htmlToAdf(`${carry('span', underlined, 'a')}
\n`)), 'read') }) test('reads lists, an item\'s first paragraph on its line and its other blocks a line each', () => { assert.deepEqual(read(htmlToAdf('b
\nb
\nBolt
Bolt
\n\n\n`)), document({ attrs: { localId: ids.quote }, content: [paragraph(text('x'))], type: 'blockquote' })) const listed: AdfNode = { attrs: { localId: ids.list, order: 3 }, content: [{ attrs: { localId: ids.item }, content: [{ attrs: localId, content: [text('Bolt')], type: 'paragraph' }], type: 'listItem' }], type: 'orderedList' } assert.deepEqual(read(htmlToAdf(`x
\n
Bolt
x\n`)), document(code))
assert.deepEqual(read(htmlToAdf('\n\n')), document({ content: [paragraph(text('Quoted')), { content: [paragraph(text('Twice'))], type: 'blockquote' }], type: 'blockquote' })) assert.deepEqual(read(htmlToAdf('Quoted
\n\n\nTwice
\n
if (a < b)\n run()\n')), document({ attrs: { language: 'javascript' }, content: [text('if (a < b)\n run()')], type: 'codeBlock' }))
assert.deepEqual(read(htmlToAdf('plain\n\n')), document({ content: [text('plain')], type: 'codeBlock' }, { type: 'codeBlock' }))
assert.deepEqual(read(htmlToAdf('\n
${carry('span', card, '')}
\n`, `${carry('div', { content: [{ content: [card], type: 'futureNode' }], type: 'futureBlock' }, '')}\n`]) { assert.match(message(htmlToAdf(html)), /^a carry element's data-adf holds a node every writer refuses: an inlineCard takes no link mark/) } }) test('refuses a carry element holding a shape every writer refuses, at any depth in the node it carries', () => { const shapes = [ { message: 'a panel carries no text: this one holds text', node: { text: 'x', type: 'panel' } }, { message: 'a rule holds no content: this one holds some', node: { content: [{ type: 'paragraph' }], type: 'rule' } }, { message: 'a status node holds neither content nor text: this one holds text', node: { text: 'x', type: 'status' } }, ] for (const { message: refusal, node } of shapes) { for (const carried of [node, { content: [node], type: 'futureBlock' }]) { assert.equal(message(htmlToAdf(`${carry('div', carried, '')}\n`)), `a carry element's data-adf holds a node every writer refuses: ${refusal}`) } } }) test('reads a table back, its rows, cells and attributes as adfToHtml writes them', () => { const row = (...content: AdfNode[]): AdfNode => ({ content, type: 'tableRow' }) const body = '| Part |
|---|
| Part |
|---|
| Part |
|---|
| a\n b \n | \n \n |
| x |
| a |
| a | b |
|---|
| a |
|---|
| b |
| a |
|---|
| a | \n\n
| a | \nb |
a |
| a |
| a |
| a |
| a |
| a |
| a |
| a |
| a |
| a |
| a |
| a |
\n'.repeat(levels)}${inner}${'\n'.repeat(levels)}\n` const row = '
| x |
Check the collation.
')}\nAsk ${carry('span', mention, '@Mikael')}
\n`)), document(panel, paragraph(text('Ask '), mention))) assert.deepEqual(read(htmlToAdf(`${carry('span', text('a\u0085b', { type: 'underline' }), 'ab').replace('\u0085', '\\u0085')}
\n`)), document(paragraph(text('a\u0085b', { type: 'underline' })))) }) test('reads a carry element holding a node HTML writes as its own element as that node', () => { assert.deepEqual(read(htmlToAdf(`${carry('div', paragraph(text('x')), 'x
')}\n`)), document(paragraph(text('x')))) assert.deepEqual(read(htmlToAdf(`a${carry('span', text('b'), 'b')}
\n`)), document(paragraph(text('a'), text('b')))) assert.deepEqual(read(htmlToAdf(`x
')}\na "b
\n')), document(paragraph({ attrs: { text: 'a\u00a0"b' }, type: 'emoji' }))) }) test('reads a carry element holding an emoji, status, date or card holding a URL as that node', () => { const emoji: AdfNode = { attrs: { text: '🎉' }, type: 'emoji' } assert.deepEqual(read(htmlToAdf(`${carry('span', emoji, '🎉')}
\n`)), document(paragraph(emoji))) }) function adfToHtmlText(doc: AdfDocument): string { const written = adfToHtml(doc) return written.ok ? written.value : written.error.message } // Each group's cases, the HTML and its refusal. const planData = '{"name":"Plan","url":"https://example.com/plan"}' const refusals: ReadonlyBolt
Bolt
\na
a
\n\n\n', 'unmappable-html at /content/0 line 2 offset 13'], ['a
a
b
\n', 'unmappable-html at / line 1 offset 0'], ['x\n', 'unmappable-html at /content/0 line 1 offset 0'],
['x\n', 'unmappable-html at /content/0 line 1 offset 0'],
['x\n', 'unmappable-html at /content/0 line 1 offset 0'],
['x\n', 'unmappable-html at /content/0 line 1 offset 0'], ['
a &bogus; b\n', 'unmappable-html at /content/0 line 1 offset 0'],
['x\n`, 'unmappable-html at /content/0 line 1 offset 0'],
[`\n`, 'unmappable-html at /content/0 line 1 offset 0'],
[`\n`, 'unmappable-html at /content/0 line 1 offset 0'],
[`\n', 'unmappable-html at /content/0 line 1 offset 0'], ['
\n', 'unsupported-node-shape at /content/0/content/0 line 1 offset 0'], ['\n', 'unsupported-node-shape at /content/0 line 1 offset 0'], ['
\n', 'unsupported-node-shape at /content/0/content/0 line 1 offset 0'], ['
a
\n', 'unmappable-html at /content/0/content/0 line 1 offset 0'], ], 'refuses a misspelled attribute value on the line of the character it names': [ ['a
\n${carry('div', panel, 'Check the collation.
')}\n`, 'unmappable-html at /content/1 line 2 offset 9'], [`Ask ${carry('span', mention, '')}
\n`, 'unmappable-html at /content/0/content/1 line 1 offset 0'], ], 'refuses an emoji, status, date or card holding a URL element that is not as adfToHtml writes it': [ ['\n', 'unmappable-html at /content/0/content/0 line 1 offset 0'], ['
ab
\n', 'unmappable-html at /content/0/content/0 line 1 offset 0'], ['a
\n', 'unmappable-html at /content/0/content/0 line 1 offset 0'], ['a&b
\n', 'unmappable-html at /content/0/content/0 line 1 offset 0'], ['a
\n', 'unmappable-html at /content/0/content/0 line 1 offset 0'], ['a
\n', 'unsupported-node-shape at /content/0/content/0 line 1 offset 0'], ['a
\n', 'unsupported-node-shape at /content/0/content/0 line 1 offset 0'], ['a
\n', 'unmappable-html at /content/0/content/0 line 1 offset 0'], ['a
\n', 'unmappable-html at /content/0/content/0 line 1 offset 0'], ['\n', 'unmappable-html at /content/0/content/0 line 1 offset 0'], ['\n', 'unmappable-html at /content/0/content/0 line 1 offset 0'], ['\n', 'unmappable-html at /content/0/content/0 line 1 offset 0'], ['\n', 'unmappable-html at /content/0/content/0 line 1 offset 0'], ['\n', 'unmappable-html at /content/0/content/0 line 1 offset 0'], ['\n', 'unmappable-html at /content/0/content/0 line 1 offset 0'], ['\n', 'unmappable-html at /content/0 line 1 offset 0'], ['\n', 'unmappable-html at /content/0/content/0 line 1 offset 0'], ['\n', 'unmappable-html at /content/0/content/0 line 1 offset 0'], ['\n', 'unmappable-html at /content/0 line 1 offset 0'], ['\n', 'unmappable-html at /content/0 line 1 offset 0'], ['\n', 'unmappable-html at /content/0/content/0 line 1 offset 0'], ['\n', 'unmappable-html at /content/0/content/0 line 1 offset 0'], ['\n', 'unmappable-html at /content/0/content/0 line 1 offset 0'], ['\n', 'unmappable-html at /content/0/content/0 line 1 offset 0'], [`\n`, 'unmappable-html at /content/0/content/0 line 1 offset 0'], [`\n`, 'unmappable-html at /content/0/content/0 line 1 offset 0'], [`\n`, 'unmappable-html at /content/0/content/0 line 1 offset 0'], [`\n`, 'unsupported-node-shape at /content/0/content/0 line 1 offset 0'], ['\n', 'unsupported-node-shape at /content/0/content/0 line 1 offset 0'], ['\n', 'unmappable-html at /content/0/content/0 line 1 offset 0'], ['
x
\n', 'unsupported-node-shape at /content/0/content/0 line 1 offset 0'], ['x
\n', 'unmappable-html at /content/0/content/0 line 1 offset 0'], ['\n', 'unmappable-html at /content/0 line 1 offset 0'], ], 'refuses the document\'s carry anywhere but as the whole document, a line feed ending it': [ [documentCarry, 'unmappable-html at / line 1 offset 0'], [`${documentCarry}\n\n`, `unmappable-html at / line 2 offset ${documentCarry.length + 1}`], [`${documentCarry}X`, 'unmappable-html at / line 1 offset 0'], [`${documentCarry}X\n${documentCarry}\n\n`, 'unsupported-node-shape at /content/0/content/0 line 2 offset 13'], [`${carry('div', { type: 'doc', version: 2 }, '')}\n`, 'unsupported-node-shape at /content/0 line 1 offset 0'], ], 'refuses a data-* attribute out of the order adfToHtml writes as unmappable-html, and one no attribute of its node spells as unsupported-node-shape': [ ['
a
\n', 'unsupported-node-shape at /content/0 line 1 offset 0'], ['x\n', 'unsupported-node-shape at /content/0 line 1 offset 0'],
['a
\n', 'unsupported-node-shape at /content/0 line 1 offset 0'], ['x\n', 'unsupported-node-shape at /content/0 line 1 offset 0'],
['x\n', 'unsupported-node-shape at /content/0 line 1 offset 0'],
['x\n', 'unsupported-node-shape at /content/0 line 1 offset 0'],
['x\n', 'unsupported-node-shape at /content/0 line 1 offset 0'],
['x
', 'unmappable-html at / line 1 offset 0'], ['x
X', 'unmappable-html at / line 1 offset 0'], ['x
\n\n', 'unmappable-html at /content/1 line 2 offset 9'], ['\n', 'unmappable-html at /content/0 line 1 offset 0'], ['a
\na
\nb
\n', 'unmappable-html at /content/1 line 2 offset 9'], ['a
\n', 'unmappable-html at /content/0 line 1 offset 0'], ['ab
\n', 'unmappable-html at /content/0 line 1 offset 0'], ['a
b
it's
\n', 'unmappable-html at /content/0/content/0 line 1 offset 0'], ['a > b
\n', 'unmappable-html at /content/0/content/0 line 1 offset 0'], ['a\rb
\n', 'unmappable-html at /content/0/content/0 line 1 offset 0'], ['\n', 'unmappable-html at /content/0 line 1 offset 0'], ['\n', 'unmappable-html at /content/0 line 1 offset 0'], ['\n', 'unmappable-html at /content/0 line 1 offset 0'], ['a
\nb\nc\nd\n', 'unmappable-html at /content/2 line 4 offset 37'],
],
'refuses an image adfToHtml would not write': [
['b
a
\n', 'unmappable-html at /content/0/content/0 line 1 offset 0'], ['\n', 'unmappable-html at /content/0/content/0 line 1 offset 0'], ['
ab
\n', 'unmappable-html at /content/0/content/0 line 1 offset 0'], ['a
\n', 'unmappable-html at /content/0/content/0 line 1 offset 0'], ['a &bogus; b
\n', 'unmappable-html at /content/0/content/0 line 1 offset 0'], ['\n', 'unmappable-html at /content/0/content/1 line 1 offset 0'], ['a', 'unmappable-html at /content/0/content/0 line 1 offset 0'],
[' a', 'unmappable-html at /content/0 line 1 offset 0'],
],
}
for (const [group, cases] of Object.entries(refusals)) {
for (const [html, expected] of cases) test(`${group}: ${JSON.stringify(html)}`, () => assert.equal(refusal(htmlToAdf(html)), expected))
}
const notStrings: Readonly x ab a > b
&b
\n', 'text escapes & as &, < as < and > as >, and holds no character HTML cannot hold: found "&b\\n"'], ['a&b\n', 'text escapes & as &, < as < and > as >, and holds no character HTML cannot hold: found "&b\\n"'],
['a 
\n', 'text escapes & as &, < as < and > as >, and holds no character HTML cannot hold: found " \\n"'], ['a: found "x\n', ' as written here is no code element of the dialect, which writes or : found "\na\n', 'the line ends at , or at a line feed before the next block, and its text spells < as <: found "\\n"'],
['\n- ab
\n
\n', ' as written here is no inline element of the dialect: found "b\\n\\n"'],
['\n- if a < b
\n
\n', 'the line ends at , or at a line feed before the next block, and its text spells < as <: found "< b\\n\\n"'],
['\n- see x
\n
\n', ' holds its attributes as name="value", each value escaping & as & and " as ", then >: found "&b=2\\">x\\n\\n"'],
['\na
x \n
\n', 'the line ends at , or at a line feed before the next block: found "x\\n"…'],
['\n- Bolt
\n- a
\n
\n
\n', 'the line ends at , or at a line feed before the next block: found "\\n- a
\\n
\na b \n\n', 'the line ends at , or at a line feed before the next block: found "b \\n\\n"'],
['\n- a\n
- b
\n
\n', 'the line ends at , or at a line feed before the next block: found "b \\n\\n"'],
['x
\n', ' as written here is no mark element of the dialect, and a mark element holds text, or one other mark element around text: found "x\\n"'],
['
x
\n', '
as written here is no mark element of the dialect, and a mark element holds text, or one other mark element around text: found "
x\\n"'],
['
\n', 'a mark element holds text, or one other mark element around text: found "\\n"'],
['\n', ' holds its attributes as name="value", each value escaping & as & and " as ", then >: found "&b=2\\">x"…'],
[`a
\n${carry('div', panel, 'Check the collation.
')}\n`, 'a carry element shows what adfToHtml writes for its node, as it or a browser spells it, then ; to change the node, edit its ADF and write the HTML again: found "collation.a\n', 'a 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 "a\\n"'],
['\n', 'a carry element spells its node\'s JSON canonically: compact, keys sorted, each character HTML cannot hold spelled as a JSON escape: found "{ "type":""…'],
[`${documentCarry}\n
\n`, 'a carry element holding the document stands alone, as the whole document: found "\\n
\\n"'],
[`
\n${documentCarry}\n`, 'a carry element holding the document stands alone, as the whole document: found "\nBolt
\n\n', 'the first paragraph of an item or cell holding content and no attribute stands bare on its line, as in Bolt : found "Bolt
\\n\\n"'],
['\n \n
\n', 'the first paragraph of an item or cell holding neither content nor an attribute stands on a line of its own: found "\\n\\n"'],
['\n- \n\n
\n
\n', 'the first paragraph of an item or cell holding an attribute stands as its on its line, straight after
or : found "
b\n', 'an image stands alone in its paragraph as
, alt left out where it holds none, inside where a link marks it: found "b\\n"'],
['
\n', 'an image stands alone in its paragraph as
, alt left out where it holds none, inside where a link marks it: found "
"…'],
['\n- a
\n
\n', 'an ordered list starts at an integer, written as JavaScript writes it: found "03\\">\\na \\n\\n"'],
['a
\n', ' takes no class attribute'],
['
a
\n', ' takes no data-color attribute'],
['
\n- a
\n
\n', ' takes start once, before its data-* attributes: found start after data-local-id'],
['\n- a
\n
\n', ' takes start once, before its data-* attributes: found start after start'],
['x
\n', 'data-wrap holds no boolean written as compact JSON'],
['
\n', ' holds its text, which is not empty: found "\\n"'],
['\n', ' holds one URL, as its href and as its text, or the URL its data-data holds as its href and its name, else that URL, as its text; to change the card, change them together: found "Spec\\n"'],
[`\n`, ' holds one URL, as its href and as its text, or the URL its data-data holds as its href and its name, else that URL, as its text; to change the card, change them together: found "Plan\\n"'],
['\n', '\\n"'],
['a
\n', ' takes no data-size attribute'],
['a&b
\n', 'text escapes & as &, < as < and > as >, and holds no character HTML cannot hold: found "&b\\n"'],
]
for (const [html, expected] of messages) test(`names what it found where reading stopped, a character no one sees as its escape: ${JSON.stringify(html)}`, () => assert.equal(message(htmlToAdf(html)), expected))
test('reads blocks nested 500 levels, and refuses the next level, a list\'s items one level below it', () => {
const quotes = (levels: number): string => `${'\n'.repeat(levels)}x
${'\n
'.repeat(levels)}\n`
assert.equal(refusal(htmlToAdf(quotes(500))), 'read')
assert.equal(refusal(htmlToAdf(quotes(501))).startsWith(`unsupported-nesting-depth at /${'content/0/'.repeat(501)}content/0 line ${502}`), true)
assert.equal(message(htmlToAdf(quotes(501))), 'blocks nest deeper than 500 levels')
const lists = (levels: number): string => `${'\n- x\n'.repeat(levels)}
${'\n \n
'.repeat(levels)}\n`
assert.equal(refusal(htmlToAdf(lists(500))), 'read')
assert.equal(refusal(htmlToAdf(lists(501))).startsWith('unsupported-nesting-depth'), true)
assert.equal(refusal(htmlToAdf('\n'.repeat(1_000_000))).startsWith('unsupported-nesting-depth'), true)
})
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')
assert.equal(refusal(htmlToAdf(`\n${carried}\n
\n`)), 'unsupported-nesting-depth at /content/0/content/0 line 2 offset 13')
const inline = nested({ text: 'x', type: 'text' }, 500, (inner) => ({ content: [inner], type: 'unknownInline' }))
assert.equal(refusal(htmlToAdf(`${carry('span', inline, '')}
\n`)), 'unsupported-nesting-depth at /content/0/content/0 line 1 offset 0')
})
// The documents adfToHtml's own tests write, by what each holds.
const written: Readonly> = {
'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' }),
'adjacent unmarked text': document(paragraph(text('a'), text('b'), text('c', { type: 'em' }), text('d'), text('e'))),
'no content key': { type: 'doc', version: 1 },
}
for (const [holding, adf] of Object.entries(written)) {
test(`reads back what adfToHtml writes for a document holding ${holding}`, () => {
const html = adfToHtml(adf)
assert.ok(html.ok)
assert.deepEqual(read(htmlToAdf(html.value)), adf)
})
}