diff --git a/src/html/alignment-element.ts b/src/html/alignment-element.ts index 83c50cb..12877ef 100644 --- a/src/html/alignment-element.ts +++ b/src/html/alignment-element.ts @@ -5,17 +5,19 @@ import { dataAttributes, readDataAttributes } from './data-attributes.ts' import { nodeAttrs, nodeMarks } from '../adf/document.ts' // The `text-align` value each alignment names. -const alignments: ReadonlyMap = new Map([ - ['center', 'center'], - ['end', 'right'], -]) +function alignments(): ReadonlyMap { + return new Map([ + ['center', 'center'], + ['end', 'right'], + ]) +} // A paragraph's or heading's attributes: its alignment mark as a `style` attribute, then the rest as `dataAttributes` writes them; `undefined` where it holds a mark besides one alignment the style spells. export function textBlockAttributes(node: AdfNode, vocabulary: AttributeVocabulary, written: readonly string[]): string | undefined { const [mark, ...others] = nodeMarks(node) if (mark === undefined) return dataAttributes(node, vocabulary, written) const attrs = nodeAttrs(mark) - const textAlign = others.length === 0 && mark.type === 'alignment' && Object.keys(attrs).length === 1 ? alignments.get(String(attrs['align'])) : undefined + const textAlign = others.length === 0 && mark.type === 'alignment' && Object.keys(attrs).length === 1 ? alignments().get(String(attrs['align'])) : undefined const bare: AdfNode = { ...node } delete bare.marks const data = textAlign === undefined ? undefined : dataAttributes(bare, vocabulary, written) @@ -25,7 +27,7 @@ export function textBlockAttributes(node: AdfNode, vocabulary: AttributeVocabula // The attributes and marks a paragraph's or heading's start tag holds, as `textBlockAttributes` writes them. export function readTextBlockAttributes(pairs: readonly (readonly [string, string])[], element: string, vocabulary: AttributeVocabulary, written: readonly string[]): Read<{ attrs: AdfAttributes; marks: AdfMark[] }> { const [first] = pairs - const align = first?.[0] === 'style' ? [...alignments].find(([, textAlign]) => first[1] === `text-align:${textAlign}`)?.[0] : undefined + const align = first?.[0] === 'style' ? [...alignments()].find(([, textAlign]) => first[1] === `text-align:${textAlign}`)?.[0] : undefined if (first?.[0] === 'style' && align === undefined) return { fault: { code: 'unmappable-html', message: `style on <${element}> holds text-align:center or text-align:right` } } const data = readDataAttributes(align === undefined ? pairs : pairs.slice(1), element, vocabulary, written) if (data.fault !== undefined) return data diff --git a/src/html/colour-style.ts b/src/html/colour-style.ts index 23d1a67..cc48e15 100644 --- a/src/html/colour-style.ts +++ b/src/html/colour-style.ts @@ -10,6 +10,7 @@ export function colourDeclaration(property: string, value: JsonValue | undefined // The colour a `property:colour` declaration holds, `undefined` where it holds another property or no such colour. export function readColour(property: string, declaration: string): string | undefined { - const value = declaration.slice(property.length + 1) - return declaration.startsWith(`${property}:`) && colour.test(value) ? value : undefined + const prefix = `${property}:` + const value = declaration.slice(prefix.length) + return declaration.startsWith(prefix) && colour.test(value) ? value : undefined } diff --git a/src/html/emit/adf-to-html.test.ts b/src/html/emit/adf-to-html.test.ts index afcbd29..d0d42b2 100644 --- a/src/html/emit/adf-to-html.test.ts +++ b/src/html/emit/adf-to-html.test.ts @@ -689,6 +689,13 @@ test('writes panels, expands and nested expands nested 500 levels, and refuses t } }) +test('writes a caption as a figcaption, refuses an inline carry nested past 500 in it, and carries what is no caption', () => { + const media: AdfNode = { attrs: { type: 'external', url: 'https://example.com/moon.jpg' }, type: 'media' } + const deep = nested('unknownInline', largestNesting, text('x')) + assert.equal(refusal(adfToHtml(document({ content: [media, { content: [deep], type: 'caption' }], type: 'mediaSingle' }))), 'unsupported-nesting-depth at /content/0/content/1/content/0') + assert.equal(carriedBlock({ content: [media, paragraph(text('a'))], type: 'mediaSingle' }), 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' }) diff --git a/src/html/media-element.ts b/src/html/media-element.ts index bd7b81c..d9c655e 100644 --- a/src/html/media-element.ts +++ b/src/html/media-element.ts @@ -61,7 +61,7 @@ export function readMediaImage(pairs: readonly (readonly [string, string])[]): R // The attributes of the `` a media is, `undefined` where a value has no spelling there. function imageAttributes(media: AdfNode): string | undefined { const { alt, height, type, url, width } = nodeAttrs(media) - const src = url === undefined ? '' : typeof url === 'string' ? srcAttribute(url) : undefined + const src = typeof url === 'string' ? srcAttribute(url) : undefined const own = [url === undefined ? ` class="${imageClass}"` : src, altAttribute(alt), sizeAttribute('height', height), sizeAttribute('width', width)] // A url without a type reads back as external, so only an external media may leave its type out. const written = url !== undefined && type === 'external' ? [...imageWritten, 'type'] : imageWritten @@ -87,7 +87,6 @@ function sizeAttribute(name: string, value: JsonValue | undefined): string | und function markedLink(mark: { attrs?: AdfAttributes; type: string }): { href: string; title?: string } | undefined { const attrs = nodeAttrs(mark) - const keys = Object.keys(attrs) - if (mark.type !== 'link' || keys.length === 0 || keys.some((key) => key !== 'href' && key !== 'title')) return undefined + if (mark.type !== 'link' || Object.keys(attrs).some((key) => key !== 'href' && key !== 'title')) return undefined return linkAttrs(attrs) } diff --git a/src/html/parse/html-to-adf.test.ts b/src/html/parse/html-to-adf.test.ts index 7a73f11..ab5889a 100644 --- a/src/html/parse/html-to-adf.test.ts +++ b/src/html/parse/html-to-adf.test.ts @@ -716,6 +716,12 @@ const pinnedRefusals: readonly (readonly [string, string])[] = [ ['

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 ""…'], + ['

a

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

a

\\n"'], + ['
  • a
\n', 'unmappable-html at /content/0:
    holds
straight after it, or a line feed and its first item: found "
  • a
  • \\n"'], + ['
    \na\n
    \n', 'unmappable-html at /content/0: ends at : found "a\\n\\n"'], + ['

    a < b

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

    ends the paragraph, and its text spells < as <: found "< b

    \\n"'], + ['
      \nx\n
    \n', 'unmappable-html at /content/0/content/0: as written here is no list item of the dialect: found "x\\n\\n"'], + ['
      \n
    • ax
    • \n
    \n', 'unmappable-html at /content/0/content/0: as written here is no inline element of the dialect: found "x\\n\\n"'], ] for (const [html, expected] of pinnedRefusals) { diff --git a/src/html/parse/task-item.ts b/src/html/parse/task-item.ts index d58a473..b0a27cc 100644 --- a/src/html/parse/task-item.ts +++ b/src/html/parse/task-item.ts @@ -56,6 +56,6 @@ function readItemAttributes(list: TaskListType, pairs: readonly (readonly [strin function checkbox(html: string, index: number, done: boolean): number | undefined { const input = readStartTag(html, index) const expected = [['type', 'checkbox'], ...(done ? [['checked', '']] : []), ['disabled', '']] - const same = input?.name === 'input' && input.attributes.length === expected.length && input.attributes.every(([name, value], at) => name === expected[at]?.[0] && value === expected[at]?.[1]) + const same = input?.name === 'input' && JSON.stringify(input.attributes) === JSON.stringify(expected) return same ? input.end : undefined }