diff --git a/src/html/emit/adf-to-html.test.ts b/src/html/emit/adf-to-html.test.ts index 0a25078..afcbd29 100644 --- a/src/html/emit/adf-to-html.test.ts +++ b/src/html/emit/adf-to-html.test.ts @@ -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' })) diff --git a/src/html/emit/adf-to-html.ts b/src/html/emit/adf-to-html.ts index dce9d4a..99b0012 100644 --- a/src/html/emit/adf-to-html.ts +++ b/src/html/emit/adf-to-html.ts @@ -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) } diff --git a/src/html/mark-elements.ts b/src/html/mark-elements.ts index d725892..04bbd67 100644 --- a/src/html/mark-elements.ts +++ b/src/html/mark-elements.ts @@ -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 ['backgroundColor', { element: 'mark', property: 'background-color' }], ['textColor', { element: 'span', property: 'color' }], ]) -const scripts = ['sub', 'sup'] +const scripts: ReadonlyMap = new Map([ + ['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 `` 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 { diff --git a/src/html/media-element.ts b/src/html/media-element.ts index e5babf5..bd7b81c 100644 --- a/src/html/media-element.ts +++ b/src/html/media-element.ts @@ -30,12 +30,13 @@ export function mediaImage(media: AdfNode): string | undefined { export function readMediaImage(pairs: readonly (readonly [string, string])[]): Read { 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 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 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 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 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) { diff --git a/src/html/parse/html-to-adf.test.ts b/src/html/parse/html-to-adf.test.ts index 986263d..7a73f11 100644 --- a/src/html/parse/html-to-adf.test.ts +++ b/src/html/parse/html-to-adf.test.ts @@ -630,6 +630,21 @@ test('reads blocks nested 500 levels, and refuses the next level, a list\'s item assert.equal(refusal(htmlToAdf('
\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 => `${'
\n'.repeat(levels)}

x

${'\n
'.repeat(levels)}\n` + const expands = (levels: number): string => `${'
\nt\n'.repeat(levels)}

x

${'\n
'.repeat(levels)}\n` + const bare = (levels: number): string => `${'
\n'.repeat(levels)}

x

${'\n
'.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 of class adf-media', () => { + const single = htmlToAdf('
\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(`

${carry('span', inline, '')}

\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])[] = [ + ['
    \n
  • a
  • \n
\n', 'unmappable-html at /content/0/content/0: a task item starts with , checked where its data-state is DONE: found "\n
  • a
  • \n\n', 'unmappable-html at /content/0/content/0: a task item starts with , checked where its data-state is DONE: found "\n
  • a
  • \n\n', 'unmappable-html at /content/0/content/0: an
  • of a task list has data-state="DONE" or data-state="TODO"'], + ['
      \n
    • a
    • \n
    \n', 'unmappable-html at /content/0/content/0: an
  • of a task list has data-state="DONE" or data-state="TODO"'], + ['
      \n
    • a
    • \n
    \n', 'unmappable-html at /content/0/content/0: an
  • of this list has class="adf-task" first'], + ['
      \n
    • a
    • \n
    \n', 'unmappable-html at /content/0/content/0: a task item\'s text stands a space after its checkbox: found "a
  • \\n\\n"'], + ['
      \n
    • \n
    \n', 'unmappable-html at /content/0/content/0: a task item holds text where a space follows its checkbox: found "\\n\\n"'], + ['
      \n
    • a
    • \n
    \n', 'unmappable-html at /content/0/content/0: a task item starts with , checked where its data-state is DONE: found "a\\n\\n"'], + ['
      \n
    • a
    • \n
    \n', 'unmappable-html at /content/0/content/0: a task item starts with , checked where its data-state is DONE: found " "…'], + ['
      \n
    • a
    • \n
    \n', 'unmappable-html at /content/0/content/0: a task item starts with , checked where its data-state is DONE: found "\n
  • a
  • \n\n', 'unmappable-html at /content/0/content/0: a task item starts with , checked where its data-state is DONE: found "\n
  • a
  • \n\n', 'unmappable-html at /content/0/content/0: a task item starts with , checked where its data-state is DONE: found "\n
  • a
  • \n\n', 'unmappable-html at /content/0/content/0: a task item starts with , checked where its data-state is DONE: found "\n
  • x
  • \n\n', 'unmappable-html at /content/0/content/0: a task item starts with , checked where its data-state is DONE: found "x\\n\\n"'], + ['
      \n
    • a\n

      b

    • \n
    \n', 'unmappable-html at /content/0/content/0:

    as written here is no inline element of the dialect: found "

    b

    \\n\\n"'], + ['
      \n
    • a
    • \n
    \n', 'unsupported-node-shape at /content/0/content/0:
  • takes no data-extra attribute'], + ['
      \n
    • a
    • \n
    • b
    • \n
    \n', 'unmappable-html at /content/0/content/1: an
  • of this list has class="adf-task" first'], + ['
      \n
    • a
    • \n
    • b
    • \n
    \n', 'unmappable-html at /content/0/content/1: an
  • of this list has class="adf-decision" first'], + ['
      \n
    • a
    • \n
    \n', 'unsupported-node-shape at /content/0/content/0:
  • takes no data-extra attribute'], + ['
      \n', 'unsupported-node-shape at /content/0:
        takes no data-extra attribute'], + ['
          \n
        \n', 'unmappable-html at /content/0/content/0: a list holds
      • items, one to a line: found "
      \\n"'], + ['
      \n

      a

      \n
      \n', 'unsupported-node-shape at /content/0:
      takes no data-extra attribute'], + ['
      \n

      a

      \n
      \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'], + ['
      \n
      \n', 'unmappable-html at /content/0/content/0: a block element of the dialect stands here: found "
      \\n"'], + ['

      a

      \n', 'unmappable-html at /content/0:
      holds
      straight after it, or a line feed and its first block: found "

      a

      \\n"'], + ['
      \n

      a

      \n
      \n', 'unmappable-html at /content/0: a
      of the dialect is a carry element, holding class="adf-carry" and then data-adf alone: found "
      \n

      a

      \n\n', 'unmappable-html at /content/0:
      takes no open attribute'], + ['
      \n

      a

      \n
      \n', 'unsupported-node-shape at /content/0:
      takes no data-title attribute'], + ['
      \n

      a

      \n
      \n', 'unmappable-html at /content/0:
      starts with class="adf-expand" or class="adf-nested-expand": found "
      \\n

      a

      \\n\n
      \n', 'unmappable-html at /content/0/content/0: a block element of the dialect stands here: found "
      \\n"'], + ['
      \nax\n

      a

      \n
      \n', 'unmappable-html at /content/0: text escapes & as &, < as < and > as >, and holds no character HTML cannot hold: found "x\\n

      a<"…'], + ['

      \na

      a

      \n
      \n', 'unmappable-html at /content/0: holds the title, then a line feed and the first block, or a line feed and
      : found "

      a

      \\n
      \\n"'], + ['
      \na\nb\n
      \n', 'unmappable-html at /content/0/content/0:
      as written here is no block element of the dialect: found "b\\na\n', 'unmappable-html at /content/0:
      holds
      straight after it, or a line feed and its first block: found "a\n\n', 'unmappable-html at /content/0:
      starts with class="adf-media-single": found "
      \\n
      \\n"'], + ['
      \n', 'unmappable-html at /content/0:
      starts with class="adf-media-single": found "
      \n', 'unsupported-node-shape at /content/0:
      takes no data-extra attribute'], + ['

      x

      \n', 'unmappable-html at /content/0/content/0: a
      holds an , inside where a link marks it: found "

      x

      \\n"'], + ['
      \n', 'unmappable-html at /content/0/content/0: a
      holds an , inside where a link marks it: found "x
      \n', 'unmappable-html at /content/0/content/0: around an image ends at : found "x
      \\n"'], + ['
      \n', 'unmappable-html at /content/0/content/0: height on holds a positive whole number written as JavaScript writes it'], + ['
      \n', 'unmappable-html at /content/0/content/0: height on holds a positive whole number written as JavaScript writes it'], + ['
      x
      \n', 'unmappable-html at /content/0:
      holds its image, then its
      , then
      : found "x
      \\n"'], + ['
      c
      x
      \n', 'unmappable-html at /content/0:
      holds its image, then its
      , then
      : found "x
      \\n"'], + ['
      c
      \n', 'unsupported-node-shape at /content/0/content/1:
      takes no data-bogus attribute'], + ['
      c
      \n', 'unmappable-html at /content/0/content/1:
      takes no style attribute'], + ['
      c

      d

      \n', 'unmappable-html at /content/0/content/1:

      as written here is no inline element of the dialect: found "

      d

      \n', 'unmappable-html at /content/0/content/0: takes no class attribute'], + ['
      \n', 'unmappable-html at /content/0/content/0: an holds src, or class="adf-media" where its media holds no url, and never both'], + ['
      \n', 'unmappable-html at /content/0/content/0: an in the dialect has class="adf-media" or none'], + ['
      \n', 'unmappable-html at /content/0/content/0: an with src leaves out data-type where its media is external'], + ['
      \n', 'unsupported-node-shape at /content/0/content/0: takes no data-bogus attribute'], + ['
      \n', 'unmappable-html at /content/0:
      takes no other attribute: found "
      \n', 'unmappable-html at /content/0/content/0: a media group holds files: found "
      \\n"'], + ['
      \n', 'unmappable-html at /content/0/content/0: a media group holds files: found "<"…'], + ['
      \n', 'unsupported-node-shape at /content/0/content/1: takes no data-bogus attribute'], + ['
      x
      \n', 'unmappable-html at /content/0/content/0: holds nothing, and ends at : found "x
      \\n"'], + ['
      x
      \n', 'unmappable-html at /content/0:
      holds its files, then
      : found "x
      \\n"'], + ['

      a

      \n', 'unmappable-html at /content/0: style on

      holds text-align:center or text-align:right'], + ['

      a

      \n', 'unmappable-html at /content/0: style on

      holds text-align:center or text-align:right'], + ['

      a b

      \n', 'unmappable-html at /content/0/content/1: 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 ""…'], +] + +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> = { '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' }), diff --git a/src/html/parse/task-item.ts b/src/html/parse/task-item.ts index fb89f72..d58a473 100644 --- a/src/html/parse/task-item.ts +++ b/src/html/parse/task-item.ts @@ -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
    • of a task list has data-state="DONE" or data-state="TODO"' } } return { value: { attrs: data.value, done } } } diff --git a/src/html/task-element.ts b/src/html/task-element.ts index 2f05a2d..22abc6a 100644 --- a/src/html/task-element.ts +++ b/src/html/task-element.ts @@ -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 = new Map([ +export const taskStates: ReadonlyMap = new Map([ ['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: `` } }