mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-18 22:25:44 +01:00
Answer stability round 1: 0 🔴, 10 🟡 (0 misread); pin section tags, cell values and refusal paths with tests, parametrize test loops, drop the markdown tryRule's dead clause
This commit is contained in:
@@ -18,14 +18,13 @@ export function isHeaderRow(row: { content?: AdfNode[] }): boolean {
|
||||
return cells.length > 0 && cells.every(({ type }) => type === 'tableHeader')
|
||||
}
|
||||
|
||||
// A cell's attributes in the order `readCellAttributes` takes them; `undefined` where one spells nothing its element writes, so the whole table rides the carry.
|
||||
// A cell's attributes in the order `readCellAttributes` takes them; `undefined` where a value has no spelling on the element, so the whole table rides the carry.
|
||||
export function cellAttributes(cell: AdfNode): string | undefined {
|
||||
const { background, colspan, colwidth, rowspan } = nodeAttrs(cell)
|
||||
const own = [spanAttribute('colspan', colspan), spanAttribute('rowspan', rowspan), styleAttribute(background), widthsAttribute(colwidth), dataAttributes(cell, blockNodes.tableCell.attributes, elementWritten)]
|
||||
return own.every((attribute) => attribute !== undefined) ? own.join('') : undefined
|
||||
}
|
||||
|
||||
// `style` and `colspan`, `rowspan` and `data-colwidth` stand first, each once, in the order `cellAttributes` writes them.
|
||||
export function readCellAttributes(pairs: readonly (readonly [string, string])[], element: string): Read<AdfAttributes> {
|
||||
const attrs: AdfAttributes = {}
|
||||
let rest = pairs
|
||||
@@ -38,7 +37,7 @@ export function readCellAttributes(pairs: readonly (readonly [string, string])[]
|
||||
rest = rest.slice(1)
|
||||
}
|
||||
const misplaced = rest.find(([name]) => leading.includes(name))
|
||||
if (misplaced !== undefined) return { fault: { code: 'unmappable-html', message: `<${element}> takes colspan, rowspan, style and data-colwidth first, each once and in that order: found ${misplaced[0]} after them` } }
|
||||
if (misplaced !== undefined) return { fault: { code: 'unmappable-html', message: `<${element}> takes colspan, rowspan, style and data-colwidth first, each once and in that order: found ${misplaced[0]} out of place` } }
|
||||
const data = readDataAttributes(rest, element, blockNodes.tableCell.attributes, elementWritten)
|
||||
return data.fault === undefined ? { value: { ...attrs, ...data.value } } : data
|
||||
}
|
||||
|
||||
@@ -325,12 +325,17 @@ test('writes a table\'s attributes as the writer panel picked them', () => {
|
||||
assert.equal(html(adfToHtml(document({ content: [{ content: [{ attrs: { background: 'red', colwidth: [0.5] }, type: 'tableHeader' }], type: 'tableRow' }], type: 'table' }))), '<table>\n<thead>\n<tr><th style="background-color:red" data-colwidth="0.5"></th></tr>\n</thead>\n</table>\n')
|
||||
})
|
||||
|
||||
const carriedCellAttrs: readonly AdfAttributes[] = [{ background: 'url(x)' }, { background: 'red;color:blue' }, { background: '' }, { background: 7 }, { colspan: 0 }, { colspan: 1.5 }, { colspan: '2' }, { rowspan: -1 }, { colwidth: [] }, { colwidth: ['1'] }, { colwidth: 120 }, { valign: 7 }, { size: 'big' }]
|
||||
|
||||
for (const attrs of carriedCellAttrs) {
|
||||
test(`carries the whole table where a cell holds ${JSON.stringify(attrs)}`, () => {
|
||||
assert.equal(carriedBlock({ content: [{ content: [{ attrs, content: [paragraph(text('x'))], type: 'tableCell' }], type: 'tableRow' }], type: 'table' }), true)
|
||||
})
|
||||
}
|
||||
|
||||
test('carries the whole table where it, a row or a cell holds what its element cannot', () => {
|
||||
const cell = (attrs: AdfAttributes): AdfNode => ({ attrs, content: [paragraph(text('x'))], type: 'tableCell' })
|
||||
const inRow = (node: AdfNode): AdfNode => ({ content: [{ content: [node], type: 'tableRow' }], type: 'table' })
|
||||
for (const attrs of [{ background: 'url(x)' }, { background: 'red;color:blue' }, { background: '' }, { background: 7 }, { colspan: 0 }, { colspan: 1.5 }, { colspan: '2' }, { rowspan: -1 }, { colwidth: [] }, { colwidth: ['1'] }, { colwidth: 120 }, { valign: 7 }, { size: 'big' }]) {
|
||||
assert.equal(carriedBlock(inRow(cell(attrs))), true, JSON.stringify(attrs))
|
||||
}
|
||||
assert.equal(carriedBlock({ content: [paragraph(text('x'))], type: 'table' }), true)
|
||||
assert.equal(carriedBlock(inRow(paragraph(text('x')))), true)
|
||||
assert.equal(carriedBlock({ content: [], type: 'table' }), true)
|
||||
@@ -356,6 +361,13 @@ test('counts a table and its rows each a level, and a cell none, as a list and i
|
||||
assert.equal(refusal(adfToHtml(holding(499, table({ content: [cell], type: 'tableRow' })))).startsWith('unsupported-nesting-depth'), true)
|
||||
})
|
||||
|
||||
test('names the path of a row or a cell it refuses', () => {
|
||||
const holding = (levels: number, inner: AdfNode): AdfDocument => document(nested('blockquote', levels, inner))
|
||||
const cell: AdfNode = { content: [paragraph(text('x'))], type: 'tableCell' }
|
||||
assert.equal(refusal(adfToHtml(holding(500, { content: [{ type: 'tableRow' }], type: 'table' }))), `unsupported-nesting-depth at ${'/content/0'.repeat(502)}`)
|
||||
assert.equal(refusal(adfToHtml(holding(499, { content: [{ type: 'tableRow' }, { content: [cell], type: 'tableRow' }], type: 'table' }))), `unsupported-nesting-depth at ${'/content/0'.repeat(500)}/content/1/content/0`)
|
||||
})
|
||||
|
||||
test('carries a node holding what its element cannot', () => {
|
||||
assert.equal(carriedBlock({ attrs: { localId: 7 }, content: [text('x')], type: 'paragraph' }), true)
|
||||
assert.equal(carriedBlock({ attrs: { layout: 'wide' }, content: [text('x')], type: 'paragraph' }), true)
|
||||
|
||||
@@ -189,9 +189,9 @@ test('refuses a carry element holding an inline card a link marks, at any depth
|
||||
|
||||
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' } },
|
||||
{ message: 'a node of type panel holds no text key: this one holds one', node: { text: 'x', type: 'panel' } },
|
||||
{ message: 'a node of type rule holds no content: this one holds some', node: { content: [{ type: 'paragraph' }], type: 'rule' } },
|
||||
{ message: 'a node of type status 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' }]) {
|
||||
@@ -247,6 +247,11 @@ const tableRefusals: readonly (readonly [string, string])[] = [
|
||||
['<table data-size="big"></table>\n', 'unsupported-node-shape'],
|
||||
['<table>\n<tbody>\n<tr data-size="big"></tr>\n</tbody>\n</table>\n', 'unsupported-node-shape'],
|
||||
['<table>\n<tbody>\n<tr><td data-size="big"></td></tr>\n</tbody>\n</table>\n', 'unsupported-node-shape'],
|
||||
['<table>\n<tfoot>\n<tr></tr>\n</tbody>\n</table>\n', 'unmappable-html'],
|
||||
['<table>\n<thead>\n<tr><th>a</th></tr>\n</tfoot>\n<tbody>\n<tr></tr>\n</tbody>\n</table>\n', 'unmappable-html'],
|
||||
['<table>\n<tbody>\n<tr><td colspan="2x">a</td></tr>\n</tbody>\n</table>\n', 'unmappable-html'],
|
||||
['<table>\n<tbody>\n<tr><td style="color:red;background-color:red">a</td></tr>\n</tbody>\n</table>\n', 'unmappable-html'],
|
||||
['<table>\n<tbody>\n<tr><td>a<hr>\n</td></tr>\n</tbody>\n</table>\n', 'unmappable-html'],
|
||||
]
|
||||
|
||||
for (const [html, code] of tableRefusals) {
|
||||
@@ -255,6 +260,10 @@ for (const [html, code] of tableRefusals) {
|
||||
})
|
||||
}
|
||||
|
||||
test('refuses a character between a list item\'s tag and its first block', () => {
|
||||
assert.equal(refusal(htmlToAdf('<ul>\n<li>a<hr>\n</li>\n</ul>\n')).split(' ')[0], 'unmappable-html')
|
||||
})
|
||||
|
||||
test('counts a table and its rows each a level, and a cell none, as adfToHtml does', () => {
|
||||
const holding = (levels: number, inner: string): string => `${'<blockquote>\n'.repeat(levels)}${inner}${'\n</blockquote>'.repeat(levels)}\n`
|
||||
const row = '<table>\n<tbody>\n<tr></tr>\n</tbody>\n</table>'
|
||||
@@ -504,9 +513,9 @@ const messages: readonly (readonly [string, string])[] = [
|
||||
['<div class="adf-carry" data-adf="{ "type":"x"}"></div>\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<hr>\n`, 'a carry element holding the document stands alone, as the whole document: found "\\n<hr>\\n"'],
|
||||
[`<hr>\n${documentCarry}\n`, 'a carry element holding the document stands alone, as the whole document: found "<div class=\\"adf-carry\\" d"…'],
|
||||
['<ul>\n<li><p>Bolt</p></li>\n</ul>\n', 'the first paragraph of an item or cell holding content and no attribute stands bare on its line, as in <li>Bolt</li>: found "<p>Bolt</p></li>\\n</ul>\\n"'],
|
||||
['<ul>\n<li><p></p></li>\n</ul>\n', 'the first paragraph of an item or cell holding neither content nor an attribute stands on a line of its own: found "<p></p></li>\\n</ul>\\n"'],
|
||||
['<ul>\n<li>\n<p data-local-id="x"></p>\n</li>\n</ul>\n', 'the first paragraph of an item or cell holding an attribute stands as its <p> on its line, straight after <li> or <td>: found "<p data-local-id=\\"x\\"></p"…'],
|
||||
['<ul>\n<li><p>Bolt</p></li>\n</ul>\n', 'an item\'s or cell\'s first paragraph holding content and no attribute stands bare on the line of the item\'s or cell\'s start tag, as in <li>Bolt</li>: found "<p>Bolt</p></li>\\n</ul>\\n"'],
|
||||
['<ul>\n<li><p></p></li>\n</ul>\n', 'an item\'s or cell\'s first paragraph holding neither content nor an attribute stands on a line of its own: found "<p></p></li>\\n</ul>\\n"'],
|
||||
['<ul>\n<li>\n<p data-local-id="x"></p>\n</li>\n</ul>\n', 'an item\'s or cell\'s first paragraph holding an attribute stands as its <p> straight after the item\'s or cell\'s start tag: found "<p data-local-id=\\"x\\"></p"…'],
|
||||
['<p><img src="a">b</p>\n', 'an image stands alone in its paragraph as <img src="…" alt="…">, alt left out where it holds none, inside <a> where a link marks it: found "b</p>\\n"'],
|
||||
['<p><img src="a" alt=""></p>\n', 'an image stands alone in its paragraph as <img src="…" alt="…">, alt left out where it holds none, inside <a> where a link marks it: found "<img src=\\"a\\" alt=\\"\\"></p>"…'],
|
||||
['<ol start="03">\n<li>a</li>\n</ol>\n', 'an ordered list starts at an integer, written as JavaScript writes it: found "03\\">\\n<li>a</li>\\n</ol>\\n"'],
|
||||
|
||||
@@ -28,13 +28,10 @@ type BlockReader = (html: string, tag: StartTag, place: Place, open: Open) => Re
|
||||
|
||||
type BlockStep = { end: number; kind: 'node'; node: AdfNode } | { end: number; kind: 'opened'; open: Open }
|
||||
|
||||
// A container whose blocks, items, rows or cells are being read: `node` holds what its opening tag gave, `depth` is its children's as `adfToHtml` counts it, and `closer` ends it. A table's rows are `rows` and its closer turns from `headCloser` to `bodyCloser` at `</thead>`.
|
||||
// A container whose blocks, items, rows or cells are being read: `node` holds what its opening tag gave, `depth` is its children's as `adfToHtml` counts it, and `closer` ends it. An open table has kind `rows`, and its closer turns from `headCloser` to `bodyCloser` at `</thead>`.
|
||||
type Open = { closer: string; content: AdfNode[]; depth: number; kind: 'blocks' | 'item' | 'list' | 'row' | 'rows'; node: AdfNode; place: Place | undefined }
|
||||
|
||||
// No element name opens both a block and an inline node, so the name alone tells an item's line from its next block.
|
||||
const bodyCloser = '\n</tbody>\n</table>'
|
||||
const headCloser = '\n</thead>\n</table>'
|
||||
const headToBody = '</thead>\n<tbody>\n'
|
||||
const blockReaders: ReadonlyMap<string, BlockReader> = new Map([
|
||||
[containerElements.blockquote, containerReader('blockquote')],
|
||||
[containerElements.bulletList, containerReader('bulletList')],
|
||||
@@ -46,15 +43,18 @@ const blockReaders: ReadonlyMap<string, BlockReader> = new Map([
|
||||
['table', readTable],
|
||||
...headingElements.map((name): [string, BlockReader] => [name, readHeading]),
|
||||
])
|
||||
const bodyCloser = '\n</tbody>\n</table>'
|
||||
const codeCloser = '</code></pre>'
|
||||
const documentCarryAlone = 'a carry element holding the document stands alone, as the whole document'
|
||||
const documentCarryEnd = `${carryContent({ type: 'doc' }, 'div')}</div>`
|
||||
const emptyDocument = serializeCanonicalJson({ type: 'doc', version: 1 }, 'compact')
|
||||
const formRefusals: Readonly<Record<FirstParagraphForm, string>> = {
|
||||
bare: "the first paragraph of an item or cell holding content and no attribute stands bare on its line, as in <li>Bolt</li>",
|
||||
block: 'the first paragraph of an item or cell holding neither content nor an attribute stands on a line of its own',
|
||||
element: 'the first paragraph of an item or cell holding an attribute stands as its <p> on its line, straight after <li> or <td>',
|
||||
bare: "an item's or cell's first paragraph holding content and no attribute stands bare on the line of the item's or cell's start tag, as in <li>Bolt</li>",
|
||||
block: "an item's or cell's first paragraph holding neither content nor an attribute stands on a line of its own",
|
||||
element: "an item's or cell's first paragraph holding an attribute stands as its <p> straight after the item's or cell's start tag",
|
||||
}
|
||||
const headCloser = '\n</thead>\n</table>'
|
||||
const headToBody = '</thead>\n<tbody>\n'
|
||||
|
||||
// Reads back what `adfToHtml` writes, as it or a browser spells it.
|
||||
export function htmlToAdf(html: string): Result<AdfDocument, ParseError> {
|
||||
@@ -280,7 +280,7 @@ function readRow(html: string, index: number, place: Place, table: Open): Result
|
||||
}
|
||||
|
||||
function firstRowRefusal<T>(html: string, index: number, place: Place | undefined): Result<T, ParseError> {
|
||||
return refused('unmappable-html', `a table's first row stands in <thead> where every cell is a <th> and it holds one, and in <tbody> otherwise: ${foundAt(html, index)}`, html, index, place)
|
||||
return refused('unmappable-html', `a table's first row stands in <thead> where it holds at least one cell and all its cells are <th>, and in <tbody> otherwise: ${foundAt(html, index)}`, html, index, place)
|
||||
}
|
||||
|
||||
function readCell(html: string, index: number, place: Place, depth: number): Result<BlockStep, ParseError> {
|
||||
|
||||
Reference in New Issue
Block a user