import assert from 'node:assert/strict' import test from 'node:test' import type { AdfAttributes, AdfDocument, AdfMark, AdfNode } from '../../adf/document.ts' import type { Result } from '../../result.ts' import { adfToHtml } from '../../index.ts' import { largestNesting } from '../../nesting.ts' const ids = { collection: '019fa884-c105-7043-ba0c-2992cfbda729', file: '01a0be1b-33b1-7324-b77d-9c2d49dacde7', item: '019ad5af-efd9-7f4e-a7c9-6aef16d4945f', list: '019d8e77-7df8-7a83-91ea-43a7ca2aae3b', paragraph: '01957ad9-e3f7-70aa-a5f3-4c013274f488', quote: '019d2fe4-3e67-7b76-b40a-a776a8f76aea', resource: '019695b0-e8cc-7bf7-8c1d-855237b7a090', } function document(...content: AdfNode[]): AdfDocument { return { content, type: 'doc', version: 1 } } function html(result: Result): string { return result.ok ? result.value : `${result.error.code}: ${result.error.message}` } function refusal(result: Result): string { return result.ok ? 'written' : `${result.error.code} at /${result.error.path.join('/')}` } function paragraph(...content: AdfNode[]): AdfNode { return content.length === 0 ? { type: 'paragraph' } : { content, type: 'paragraph' } } 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 nested(type: string, levels: number, inner: AdfNode): AdfNode { let node = inner for (let level = 0; level < levels; level += 1) node = { content: [node], type } return node } function carriedBlock(node: AdfNode): boolean { return html(adfToHtml(document(node))).startsWith('
') + 2).replace(/<\/(?:div|span)>(?:<\/p>)?\n$/, '') } test('writes paragraphs, headings and rules as their elements, separated by line feeds', () => { const heading = (level: number): AdfNode => ({ attrs: { level }, content: [text(`Level ${level}`)], type: 'heading' }) assert.equal(html(adfToHtml(document(heading(1), paragraph(text('Read the manual.')), { type: 'rule' }, heading(6)))), '

Level 1

\n

Read the manual.

\n
\n
Level 6
\n') assert.equal(html(adfToHtml(document(paragraph(), { attrs: { level: 2 }, type: 'heading' }))), '

\n

\n') }) test('writes the marks CommonMark spells, the first mark outermost', () => { const link: AdfMark = { attrs: { href: 'https://example.com/a?b=1&c=2', title: 'The "A" page' }, type: 'link' } const line = paragraph(text('Read '), text('the', { type: 'em' }), text(' '), text('manual', { type: 'strong' }, { type: 'em' }), text(': '), text('npm ci', { type: 'code' }, link), text(' or '), text('this', link)) assert.equal( html(adfToHtml(document(line))), '

Read the manual: npm ci or this

\n', ) assert.equal(html(adfToHtml(document(paragraph(text('Line one'), { type: 'hardBreak' }, text('Line two'))))), '

Line one
Line two

\n') }) test('escapes text, and keeps tabs, line feeds and form feeds', () => { assert.equal(html(adfToHtml(document(paragraph(text('a < b && c > "d" \'e\'\tf\ng\fh'))))), '

a < b && c > "d" \'e\'\tf\ng\fh

\n') }) test('carries text holding a character HTML text cannot hold, escaping it in the JSON', () => { for (const value of ['a\r\nb', 'a\0b', 'a\u0001b', 'a\u007fb', 'a\u0085b', 'a﷐b', 'a￿b', 'a🿾b', 'a\ud800b', 'a\udc00b']) { assert.equal(carriedSpans(text(value)), 1, JSON.stringify(value)) } assert.equal(carriedSpans(text('😄 a b')), 0) assert.equal(html(adfToHtml(document(paragraph(text('a\u0085﷐b', { type: 'underline' }))))).includes('"text":"a\\u0085\\ufdd0b"'), true) assert.equal(html(adfToHtml(document(paragraph(text('a\u{1fffe}b', { type: 'underline' }))))).includes('"text":"a\\ud83f\\udffeb"'), true) }) test('writes lists tight, and an item\'s blocks after its first paragraph as elements on lines of their own', () => { assert.equal(html(adfToHtml(document({ content: [item(paragraph(text('Bolt'))), item(paragraph(text('Nut')))], type: 'bulletList' }))), '
    \n
  • Bolt
  • \n
  • Nut
  • \n
\n') const listed: AdfNode = { attrs: { order: 3 }, content: [item(paragraph(text('Bolt')), { content: [item(paragraph(text('M8')))], type: 'bulletList' }), item(paragraph(text('a')), paragraph(text('b')))], type: 'orderedList' } assert.equal(html(adfToHtml(document(listed))), '
    \n
  1. Bolt\n
      \n
    • M8
    • \n
    \n
  2. \n
  3. a\n

    b

    \n
  4. \n
\n') assert.equal(html(adfToHtml(document({ content: [item({ type: 'rule' }), { type: 'listItem' }], type: 'orderedList' }))), '
    \n
  1. \n
    \n
  2. \n
  3. \n
\n') assert.equal(html(adfToHtml(document({ content: [item(paragraph())], type: 'bulletList' }))), '
    \n
  • \n

    \n
  • \n
\n') }) test('writes an item\'s first paragraph as its element on the item\'s line where it holds an attribute, and as its carry where it holds a mark', () => { const listed = (...blocks: AdfNode[]): string => html(adfToHtml(document({ content: [item(...blocks)], type: 'bulletList' }))) const held: AdfNode = { attrs: { localId: ids.paragraph }, content: [text('Bolt')], type: 'paragraph' } assert.equal(listed(held), `
    \n
  • Bolt

  • \n
\n`) assert.equal(listed(held, { content: [item(paragraph(text('M8')))], type: 'bulletList' }), `
    \n
  • Bolt

    \n
      \n
    • M8
    • \n
    \n
  • \n
\n`) assert.equal(listed({ attrs: { localId: ids.paragraph }, type: 'paragraph' }), `
    \n
  • \n
\n`) assert.equal(listed({ content: [text('Bolt')], marks: [{ attrs: { align: 'center' }, type: 'alignment' }], type: 'paragraph' }).startsWith('
    \n
  • \n
    { const localId = { localId: ids.paragraph } assert.equal(html(adfToHtml(document({ attrs: localId, content: [text('x')], type: 'paragraph' }))), `

    x

    \n`) assert.equal(html(adfToHtml(document({ attrs: { level: 2, ...localId }, content: [text('x')], type: 'heading' }))), `

    x

    \n`) assert.equal(html(adfToHtml(document({ attrs: { localId: ids.quote }, content: [paragraph(text('x'))], type: 'blockquote' }))), `
    \n

    x

    \n
    \n`) 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.equal(html(adfToHtml(document(listed))), `
      \n
    1. Bolt

    2. \n
    \n`) const code: AdfNode = { attrs: { wrap: true, language: 'js', hideLineNumbers: false, ...localId }, content: [text('x')], type: 'codeBlock' } assert.equal(html(adfToHtml(document(code))), `
    x
    \n`) assert.equal(html(adfToHtml(document({ attrs: { weight: -0, color: '#ccc', style: 'a"b' }, type: 'rule' }))), '
    \n') }) test('writes quotes and code blocks, the language a class', () => { assert.equal(html(adfToHtml(document({ content: [paragraph(text('Quoted'))], type: 'blockquote' }))), '
    \n

    Quoted

    \n
    \n') assert.equal(html(adfToHtml(document({ attrs: { language: 'javascript' }, content: [text('if (a < b)\n run()')], type: 'codeBlock' }))), '
    if (a < b)\n  run()
    \n') assert.equal(html(adfToHtml(document({ content: [text('plain')], type: 'codeBlock' }, { type: 'codeBlock' }))), '
    plain
    \n
    \n') }) test('writes an external image standing alone in a paragraph, a link around it where one marks it', () => { const image = (media: AdfNode): AdfNode => ({ attrs: { layout: 'center' }, content: [media], type: 'mediaSingle' }) assert.equal(html(adfToHtml(document(image({ attrs: { alt: 'The moon', type: 'external', url: 'https://example.com/moon.jpg' }, type: 'media' })))), '

    The moon

    \n') const linked = image({ attrs: { type: 'external', url: 'https://example.com/moon.jpg' }, marks: [{ attrs: { href: 'https://example.com/' }, type: 'link' }], type: 'media' }) assert.equal(html(adfToHtml(document(linked))), '

    \n') }) test('carries every other node in an adf-carry element holding its JSON', () => { const panel: AdfNode = { attrs: { panelType: 'info' }, content: [paragraph(text('Check the collation.'))], type: 'panel' } assert.equal( html(adfToHtml(document(panel))), '

    Check the collation.

    \n', ) const mention: AdfNode = { attrs: { id: '5b10a2' }, type: 'mention' } assert.equal(html(adfToHtml(document(paragraph(text('Ask '), mention)))), '

    Ask @5b10a2

    \n') }) test('writes an emoji, mention, status, date and card as their elements, their attributes data-* sorted by name', () => { const inline = (node: AdfNode): string => html(adfToHtml(document(paragraph(text('See '), node)))) assert.equal(inline({ attrs: { id: '1f389', localId: 'e1', shortName: ':tada:', text: '🎉' }, type: 'emoji' }), '

    See 🎉

    \n') assert.equal(inline({ attrs: { text: '<3' }, type: 'emoji' }), '

    See <3

    \n') assert.equal(inline({ attrs: { accessLevel: 'CONTAINER', id: '5b10a2', localId: 'm1', text: '@Mikael', userType: 'DEFAULT' }, type: 'mention' }), '

    See @Mikael

    \n') assert.equal(inline({ attrs: { color: 'yellow', localId: 's1', style: 'bold', text: 'In review' }, type: 'status' }), '

    See In review

    \n') assert.equal(inline({ attrs: { localId: 'd1', timestamp: '1756080000000' }, type: 'date' }), '

    See

    \n') assert.equal(inline({ attrs: { localId: 'c1', url: 'https://example.com/spec?a=1&b=2' }, type: 'inlineCard' }), '

    See https://example.com/spec?a=1&b=2

    \n') const plan = { name: 'Release plan', url: 'https://example.com/plan' } assert.equal(inline({ attrs: { data: plan, localId: 'c2' }, type: 'inlineCard' }), '

    See Release plan

    \n') assert.equal(inline({ attrs: { data: { url: 'https://example.com/plan' } }, type: 'inlineCard' }), '

    See https://example.com/plan

    \n') }) test('carries an emoji, mention, status, date or card holding what its element cannot write', () => { const carried = (node: AdfNode): boolean => html(adfToHtml(document(paragraph(node)))).includes('adf-carry') const comment: AdfMark = { attrs: { annotationType: 'inlineComment', id: 'c1' }, type: 'annotation' } assert.equal(carried({ attrs: { text: '🎉' }, marks: [comment], type: 'emoji' }), true) assert.equal(carried({ attrs: { shortName: ':tada:' }, type: 'emoji' }), true) assert.equal(carried({ attrs: { text: '' }, type: 'emoji' }), true) assert.equal(carried({ attrs: { text: 'a\u0001' }, type: 'emoji' }), true) assert.equal(carried({ attrs: { size: 'big', text: '🎉' }, type: 'emoji' }), true) assert.equal(carried({ attrs: { id: 5, text: '🎉' }, type: 'emoji' }), true) assert.equal(carried({ attrs: { text: '🎉' }, content: [], type: 'emoji' }), true) assert.equal(carried({ attrs: { text: '🎉' }, content: [text('x')], type: 'emoji' }), true) assert.equal(carried({ attrs: { color: 'red' }, type: 'status' }), true) assert.equal(carried({ attrs: { timestamp: 'soon' }, type: 'date' }), true) assert.equal(carried({ attrs: { timestamp: 1756080000000 }, type: 'date' }), true) assert.equal(carried({ type: 'date' }), true) for (const timestamp of ['253402300800000', '-62135596800001', '-62167219200000']) assert.equal(carried({ attrs: { timestamp }, type: 'date' }), true) for (const timestamp of ['253402300799999', '-62135596800000']) assert.equal(carried({ attrs: { timestamp }, type: 'date' }), false) assert.equal(carried({ attrs: { data: { name: 'Roadmap' } }, type: 'inlineCard' }), true) assert.equal(carried({ attrs: { data: { name: 'Roadmap' }, url: 'https://example.com/' }, type: 'inlineCard' }), true) assert.equal(carried({ attrs: { url: '' }, type: 'inlineCard' }), true) assert.equal(carried({ attrs: { url: 'https://example.com/\u0001' }, type: 'inlineCard' }), true) assert.equal(carried({ attrs: { data: { name: 'Roadmap', url: 'https://example.com/' } }, marks: [comment], type: 'inlineCard' }), true) assert.equal(carried({ attrs: { data: { url: 'https://example.com/\u0001' } }, type: 'inlineCard' }), true) assert.equal(carried({ attrs: { data: { name: 'Roadmap', url: 'https://example.com/\u0001' } }, type: 'inlineCard' }), true) assert.equal(carried({ attrs: { url: 7 }, type: 'inlineCard' }), true) assert.equal(carried({ attrs: { data: { name: 'Roadmap', url: ' ' } }, type: 'inlineCard' }), true) assert.equal(carried({ attrs: { data: 'https://example.com/' }, type: 'inlineCard' }), true) assert.equal(carried({ attrs: { data: {} }, type: 'inlineCard' }), true) assert.equal(carried({ attrs: { data: { name: 'Roadmap\u0001', url: 'https://example.com/' } }, type: 'inlineCard' }), true) assert.equal(carried({ attrs: { id: '5b10a2' }, type: 'mention' }), true) assert.equal(carried({ attrs: { id: '5b10a2', text: '' }, type: 'mention' }), true) assert.equal(carried({ attrs: { id: '5b10a2', text: '@Mikael' }, content: [text('x')], type: 'mention' }), true) assert.equal(carried({ attrs: { id: '5b10a2', text: '@Mikael' }, marks: [comment], type: 'mention' }), true) assert.equal(carried({ attrs: { id: 7, text: '@Mikael' }, type: 'mention' }), true) assert.equal(carried({ attrs: { id: '5b10a2', text: 7 }, type: 'mention' }), true) }) test('shows each block a carried node holds as a paragraph in a div', () => { const panel: AdfNode = { attrs: { panelType: 'info' }, content: [paragraph(text('Check the collation.')), paragraph(text('Then import.'))], type: 'panel' } assert.equal(shown(html(adfToHtml(document(panel)))), '

    Check the collation.

    Then import.

    ') }) test('shows a carried node\'s content after a block it holds as a paragraph of its own', () => { assert.equal(shown(html(adfToHtml(document({ content: [paragraph(text('a')), text('b')], type: 'panel' })))), '

    a

    b

    ') }) test('shows text a link marks in a carried node as a link, leaving out a title that is no string', () => { const link: AdfMark = { attrs: { href: 'https://spec.commonmark.org/', title: 'The spec' }, type: 'link' } const panel: AdfNode = { content: [paragraph(text('Read the '), text('CommonMark spec', link), text(' first.'))], type: 'panel' } assert.equal(shown(html(adfToHtml(document(panel)))), '

    Read the CommonMark spec first.

    ') assert.equal(shown(html(adfToHtml(document(paragraph(text('x', { type: 'underline' }, link)))))), 'x') assert.equal(shown(html(adfToHtml(document(paragraph(text('x', { attrs: { href: 'https://spec.commonmark.org/', title: 5 }, type: 'link' })))))), 'x') }) test('shows a hard break, and a line feed in carried text outside a code block, as a line break', () => { assert.equal(shown(html(adfToHtml(document({ content: [paragraph(text('Then'), { type: 'hardBreak' }, text('import.'))], type: 'panel' })))), '

    Then
    import.

    ') assert.equal(shown(html(adfToHtml(document({ content: [paragraph(text('Then\nimport.'))], type: 'panel' })))), '

    Then
    import.

    ') assert.equal(shown(html(adfToHtml(document(paragraph(text('a\nb', { type: 'underline' })))))), 'a
    b') }) test('shows an expand\'s title as a paragraph of its own', () => { assert.equal(shown(html(adfToHtml(document({ attrs: { title: 'Details' }, content: [paragraph(text('Inside'))], type: 'expand' })))), '

    Details

    Inside

    ') }) test('parts the blocks a span carry shows with line breaks', () => { assert.equal(shown(html(adfToHtml(document(paragraph({ content: [paragraph(text('a')), paragraph(text('b'))], type: 'panel' }))))), 'a
    b') }) test('drops from what a carry element shows the characters HTML cannot hold, and the link of a URL holding one', () => { assert.equal(shown(html(adfToHtml(document(paragraph(text('a\u0001b\r', { type: 'underline' }, { attrs: { href: 'https://example.com/\u0001' }, type: 'link' })))))), 'ab') }) test('shows a code block in a div carry as pre and code, keeping its line feeds and spaces', () => { const code: AdfNode = { attrs: { language: 'python' }, content: [text('if ready:\n deploy()')], type: 'codeBlock' } assert.equal(shown(html(adfToHtml(document({ attrs: { panelType: 'info' }, content: [paragraph(text('Run this:')), code], type: 'panel' })))), '

    Run this:

    if ready:\n    deploy()
    ') assert.equal(shown(html(adfToHtml(document({ content: [text('a\r\n b'), text('c')], type: 'codeBlock' })))), '
    a\n  bc
    ') }) test('shows a block inside a code block in a div carry as a paragraph, and the text after it as code', () => { assert.equal(shown(html(adfToHtml(document({ content: [text('x'), paragraph(text('a\nb')), text('z')], type: 'codeBlock' })))), '
    x

    a
    b

    z
    ') assert.equal(shown(html(adfToHtml(document({ content: [text('x'), { attrs: { id: 'u' }, content: [paragraph(text('a'))], type: 'mention' }, text('z')], type: 'codeBlock' })))), '
    x@u

    a

    z
    ') }) test('gives a code block in a div carry the class its element takes, and none where its language is no class token', () => { const carried = (language: AdfAttributes[string]): string => shown(html(adfToHtml(document({ attrs: { language }, content: [text('x')], marks: [{ attrs: { mode: 'wide' }, type: 'breakout' }], type: 'codeBlock' })))) assert.equal(carried('js'), '
    x
    ') assert.equal(carried('c sharp'), '
    x
    ') assert.equal(carried(5), '
    x
    ') }) test('shows a code block in a span carry as text, its line feeds line breaks', () => { assert.equal(shown(html(adfToHtml(document(paragraph({ attrs: { language: 'python' }, content: [text('if ready:\n deploy()')], type: 'codeBlock' }))))), 'if ready:
    deploy()') }) test('shows the content a carried node holds in an attribute, and links a card or an external image to its URL', () => { const inline = (node: AdfNode): string => shown(html(adfToHtml(document(paragraph(node))))) assert.equal(inline({ attrs: { timestamp: '1700000000000' }, marks: [{ attrs: { annotationType: 'inlineComment', id: 'c1' }, type: 'annotation' }], type: 'date' }), '2023-11-14') assert.equal(inline({ attrs: { shortName: ':smile:', text: '😄' }, marks: [{ attrs: { annotationType: 'inlineComment', id: 'c1' }, type: 'annotation' }], type: 'emoji' }), '😄') assert.equal(inline({ attrs: { id: '5b10a2' }, type: 'mention' }), '@5b10a2') assert.equal(inline({ attrs: { color: 'green', text: 'Done' }, marks: [{ attrs: { annotationType: 'inlineComment', id: 'c1' }, type: 'annotation' }], type: 'status' }), 'Done') assert.equal(inline({ attrs: { url: 'https://example.com/' }, marks: [{ attrs: { annotationType: 'inlineComment', id: 'c1' }, type: 'annotation' }], type: 'inlineCard' }), 'https://example.com/<card>') assert.equal(inline({ attrs: { data: { name: 'Roadmap' } }, type: 'inlineCard' }), 'Roadmap') assert.equal(inline(text('underlined', { type: 'underline' })), 'underlined') const image = (media: AdfNode): string => shown(html(adfToHtml(document({ attrs: { layout: 'wide' }, content: [media], type: 'mediaSingle' })))) assert.equal(image({ attrs: { alt: 'The moon', type: 'external', url: 'https://example.com/moon.jpg' }, type: 'media' }), '

    The moon

    ') assert.equal(image({ attrs: { type: 'external', url: 'https://example.com/moon.jpg' }, type: 'media' }), '

    https://example.com/moon.jpg

    ') assert.equal(image({ attrs: { alt: 'The moon', type: 'external', url: 'https://example.com/moon.jpg' }, marks: [{ attrs: { title: 'No target' }, type: 'link' }], type: 'media' }), '

    The moon

    ') }) test('shows a carried image marked by a link to another target as that link, then a link to the image reading image', () => { const link: AdfMark = { attrs: { href: 'https://example.com/' }, type: 'link' } const image = (attrs: AdfAttributes): string => shown(html(adfToHtml(document({ attrs: { layout: 'wide' }, content: [{ attrs, marks: [link], type: 'media' }], type: 'mediaSingle' })))) assert.equal(image({ alt: 'The moon', type: 'external', url: 'https://example.com/moon.jpg' }), '

    The moon (image)

    ') assert.equal(image({ type: 'external', url: 'https://example.com/moon.jpg' }), '

    https://example.com/ (image)

    ') assert.equal(image({ alt: 'The moon', type: 'external', url: 'https://example.com/' }), '

    The moon

    ') const blank = shown(html(adfToHtml(document({ attrs: { layout: 'wide' }, content: [{ attrs: { type: 'external', url: 'https://example.com/moon.jpg' }, marks: [{ attrs: { href: ' ' }, type: 'link' }], type: 'media' }], type: 'mediaSingle' })))) assert.equal(blank, '

    https://example.com/moon.jpg (image)

    ') const texted = shown(html(adfToHtml(document({ attrs: { layout: 'wide' }, content: [{ attrs: { alt: 'The moon', type: 'external', url: 'https://example.com/moon.jpg' }, marks: [link], text: 'Moon', type: 'media' }], type: 'mediaSingle' })))) assert.equal(texted, '

    Moon (image)

    ') const inline = (mark: AdfMark): string => shown(html(adfToHtml(document(paragraph({ attrs: { alt: 'The moon', type: 'external', url: 'https://example.com/moon.jpg' }, marks: [mark], type: 'mediaInline' }))))) assert.equal(inline(link), 'The moon (image)') assert.equal(inline({ attrs: { href: 'https://example.com/', title: 5 }, type: 'link' }), 'The moon (image)') }) test('refuses an inline card a link marks, which Atlassian\'s schema does not allow, inside a carried node too', () => { const marks: AdfMark[] = [{ attrs: { href: 'https://example.com/' }, type: 'link' }] const cards: AdfNode[] = [{ attrs: { url: 'https://example.com/roadmap' }, marks, type: 'inlineCard' }, { attrs: { data: { name: 'Roadmap', url: 'https://example.com/roadmap' } }, marks, type: 'inlineCard' }] for (const card of cards) { const refused = adfToHtml(document(paragraph(text('See '), card))) assert.equal(!refused.ok && refused.error.code, 'unsupported-node-shape') assert.deepEqual(!refused.ok && refused.error.path, ['content', 0, 'content', 1]) assert.match(!refused.ok ? refused.error.message : '', /inlineCard.*link/) } const carried = adfToHtml(document({ content: [paragraph(cards[0] ?? text('x'))], type: 'panel' })) assert.deepEqual(!carried.ok && carried.error.path, ['content', 0, 'content', 0, 'content', 0]) const nested = adfToHtml(document({ content: [{ content: [paragraph(cards[0] ?? text('x'))], type: 'listItem' }], type: 'bulletList' })) assert.deepEqual(!nested.ok && nested.error.path, ['content', 0, 'content', 0, 'content', 0, 'content', 0]) }) test('shows a note naming what a carried node references where it shows no text, linked where a link marks the node', () => { const inline = (node: AdfNode): string => shown(html(adfToHtml(document(paragraph(node))))) const stored = { collection: ids.collection, id: ids.file, type: 'file' } assert.equal(inline({ attrs: stored, type: 'mediaInline' }), '(image)') assert.equal(inline({ attrs: stored, marks: [{ attrs: { href: 'https://example.com/' }, type: 'link' }], type: 'mediaInline' }), '(image)') assert.equal(inline({ attrs: {}, type: 'inlineCard' }), '(link card)') assert.equal(inline({ attrs: { extensionKey: 'jira-issues' }, type: 'inlineExtension' }), '(jira-issues)') assert.equal(inline({ attrs: { extensionType: 'com.example' }, type: 'inlineExtension' }), '(extension)') assert.equal(shown(html(adfToHtml(document({ attrs: { resourceId: ids.resource }, type: 'syncBlock' })))), '

    (synced block)

    ') }) 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) assert.equal(carriedBlock({ content: [], type: 'paragraph' }), true) assert.equal(carriedBlock({ attrs: {}, type: 'rule' }), true) assert.equal(carriedBlock({ content: [text('x')], type: 'rule' }), true) assert.equal(carriedBlock({ attrs: { color: 'a\u0001' }, type: 'rule' }), true) assert.equal(carriedBlock({ attrs: { level: 1 }, type: 'rule' }), true) assert.equal(carriedBlock({ type: 'unknownBlock' }), true) for (const level of [0, 1.5, 7]) assert.equal(carriedBlock({ attrs: { level }, content: [text('x')], type: 'heading' }), true, String(level)) assert.equal(carriedBlock({ marks: [{ attrs: { level: 1 }, type: 'indentation' }], type: 'blockquote' }), true) for (const order of [1.5, -0]) assert.equal(carriedBlock({ attrs: { order }, content: [item(paragraph(text('x')))], type: 'orderedList' }), true, String(order)) assert.equal(carriedBlock({ attrs: { localId: 7 }, content: [item(paragraph(text('x')))], type: 'orderedList' }), true) assert.equal(carriedBlock({ content: [item(paragraph(text('x'))), paragraph(text('y'))], type: 'bulletList' }), true) assert.equal(carriedBlock({ content: [{ attrs: {}, content: [paragraph(text('x'))], type: 'listItem' }], type: 'bulletList' }), true) assert.equal(carriedBlock({ content: [{ content: [paragraph(text('x'))], text: 'y', type: 'listItem' }], type: 'bulletList' }), true) assert.equal(carriedBlock({ content: [], type: 'bulletList' }), true) assert.equal(carriedBlock({ attrs: { language: 'c sharp' }, content: [text('x')], type: 'codeBlock' }), true) assert.equal(carriedBlock({ attrs: { language: 5 }, content: [text('x')], type: 'codeBlock' }), true) assert.equal(carriedBlock({ attrs: { localId: 7 }, content: [text('x')], type: 'codeBlock' }), true) assert.equal(carriedBlock({ content: [text('x')], marks: [{ attrs: { mode: 'wide' }, type: 'breakout' }], type: 'codeBlock' }), true) assert.equal(carriedBlock({ attrs: { language: 'a\0b' }, content: [text('x')], type: 'codeBlock' }), true) assert.equal(carriedBlock({ content: [text('a'), text('b')], type: 'codeBlock' }), true) assert.equal(carriedBlock({ content: [text('x', { type: 'code' })], type: 'codeBlock' }), true) assert.equal(carriedBlock({ content: [text('x\0')], type: 'codeBlock' }), true) assert.equal(carriedBlock(item(paragraph(text('x')))), true) assert.equal(carriedBlock(text('x')), true) assert.equal(carriedBlock({ attrs: { layout: 'wide' }, content: [{ attrs: { type: 'external', url: 'https://example.com/a.png' }, type: 'media' }], type: 'mediaSingle' }), true) assert.equal(carriedBlock({ attrs: { layout: 'center' }, content: [{ attrs: { alt: '', type: 'external', url: 'https://example.com/a.png' }, type: 'media' }], type: 'mediaSingle' }), true) }) test('carries inline what its element cannot hold, and a text node that would join the unmarked one before it', () => { assert.equal(carriedSpans(text('a'), text('b')), 1) assert.equal(carriedSpans(text('a'), text('b'), text('c')), 1) assert.equal(carriedSpans(text('a'), text('b', { type: 'em' }), text('c')), 0) assert.equal(carriedSpans(text('')), 1) assert.equal(carriedSpans({ type: 'text' }), 1) assert.equal(carriedSpans(text('x', { type: 'em' }, { type: 'em' })), 1) assert.equal(carriedSpans({ marks: [], text: 'x', type: 'text' }), 1) assert.equal(carriedSpans(text('x', { attrs: {}, type: 'em' })), 1) assert.equal(carriedSpans(text('x', { attrs: { href: 'https://example.com/' }, type: 'underline' })), 1) assert.equal(carriedSpans(text('x', { attrs: { href: 'https://example.com/', id: ids.item }, type: 'link' })), 1) assert.equal(carriedSpans({ attrs: { text: '\n' }, type: 'hardBreak' }), 1) assert.equal(carriedSpans({ content: [text('x')], type: 'hardBreak' }), 1) assert.equal(carriedSpans(paragraph(text('x'))), 1) }) test('writes every URL as the ADF holds it, escaped, whatever its scheme', () => { const linked = (href: string): string => html(adfToHtml(document(paragraph(text('x', { attrs: { href }, type: 'link' }))))) assert.equal(linked('javascript:alert("x")'), '

    x

    \n') assert.equal(linked('data:text/html,x'), '

    x

    \n') const image: AdfNode = { attrs: { layout: 'center' }, content: [{ attrs: { type: 'external', url: 'vbscript:x' }, type: 'media' }], type: 'mediaSingle' } assert.equal(html(adfToHtml(document(image))), '

    \n') assert.equal(html(adfToHtml(document(paragraph({ attrs: { url: 'javascript:alert(1)' }, type: 'inlineCard' })))), '

    javascript:alert(1)

    \n') }) test('writes a container holding no content key as its element holding nothing', () => { assert.equal(html(adfToHtml(document({ type: 'blockquote' }, { type: 'bulletList' }, { type: 'orderedList' }))), '
    \n
      \n
        \n') }) test('carries a link or image holding a value its element cannot', () => { const carried = (mark: AdfMark): boolean => html(adfToHtml(document(paragraph(text('x', mark))))).startsWith('

        ({ attrs: { type: 'external', url: 'https://example.com/a.png' }, marks: [{ attrs, type: 'link' }], type: 'media' }) const image = (media: AdfNode): AdfNode => ({ attrs: { layout: 'center' }, content: [media], type: 'mediaSingle' }) assert.equal(carriedBlock(image(linked({ href: 'https://example.com/\0' }))), true) assert.equal(carriedBlock(image(linked({ href: 'https://example.com/', title: 'x\0' }))), true) assert.equal(carriedBlock(image({ attrs: { type: 'external', url: 'https://example.com/\0.png' }, type: 'media' })), true) assert.equal(carriedBlock(image({ attrs: { alt: 'The\0moon', type: 'external', url: 'https://example.com/a.png' }, type: 'media' })), true) }) test('writes an empty document as nothing, and one holding no content key as its carry', () => { assert.equal(html(adfToHtml(document())), '') assert.equal(html(adfToHtml({ type: 'doc', version: 1 })), '

        \n') }) test('refuses what is no document, and a version HTML cannot carry', () => { assert.equal(refusal(adfToHtml({ type: 'doc', version: Number.NaN })), 'not-an-adf-document at /') assert.equal(refusal(adfToHtml({ type: 'doc', version: 2 })), 'unsupported-document-version at /') }) test('writes blocks nested 500 levels, and refuses the next level, a list\'s items one level below it', () => { assert.equal(refusal(adfToHtml(document(nested('blockquote', largestNesting, paragraph(text('x')))))), 'written') const deep = adfToHtml(document(nested('blockquote', 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) let items: AdfNode = paragraph(text('x')) for (let level = 0; level < largestNesting; level += 1) items = { content: [item(items)], type: 'bulletList' } assert.equal(refusal(adfToHtml(document(items))), 'written') assert.equal(refusal(adfToHtml(document({ content: [item(items)], type: 'bulletList' }))).startsWith('unsupported-nesting-depth at /'), true) }) test('refuses a carried block whose JSON nests past the levels its depth leaves, and an inline carry past 500', () => { let value: AdfAttributes[string] = 1 for (let level = 0; level < largestNesting - 2; level += 1) value = [value] const carried: AdfNode = { attrs: { a: value }, type: 'unknownBlock' } assert.equal(refusal(adfToHtml(document(carried))), 'written') assert.equal(refusal(adfToHtml(document({ content: [carried], type: 'blockquote' }))), 'unsupported-nesting-depth at /content/0/content/0') const deep = nested('unknownInline', largestNesting, text('x')) assert.equal(refusal(adfToHtml(document(paragraph(deep)))), 'unsupported-nesting-depth at /content/0/content/0') assert.equal(refusal(adfToHtml(document({ attrs: { level: 1 }, content: [deep], type: 'heading' }))), 'unsupported-nesting-depth at /content/0/content/0') assert.equal(refusal(adfToHtml(document({ content: [item(paragraph(deep))], type: 'bulletList' }))), 'unsupported-nesting-depth at /content/0/content/0/content/0/content/0') assert.equal(refusal(adfToHtml(document({ content: [item({ attrs: { localId: ids.paragraph }, content: [deep], type: 'paragraph' })], type: 'bulletList' }))), 'unsupported-nesting-depth at /content/0/content/0/content/0/content/0') assert.equal(refusal(adfToHtml(document({ content: [item(paragraph(text('a')), paragraph(deep)), item(paragraph(deep))], type: 'bulletList' }))), 'unsupported-nesting-depth at /content/0/content/0/content/1/content/0') }) test('refuses HTML past V8\'s longest string, at the block passing it', () => { const long = 'a'.repeat(2 ** 28) const tooLong = (result: Result): boolean => !result.ok && result.error.code === 'unsupported-node-shape' && result.error.message.includes('536870888') assert.equal(tooLong(adfToHtml(document(paragraph(text(long), text(long, { type: 'em' }), text(long))))), true) // The two code blocks' text is the bound exactly, so only what their elements add passes it, before the too-deep block after them. const code: AdfNode = { content: [text('a'.repeat(536870888 / 2))], type: 'codeBlock' } assert.equal(tooLong(adfToHtml(document(code, code, nested('blockquote', largestNesting + 1, { type: 'rule' })))), true) }) test('writes HTML exactly as long as V8\'s longest string', () => { // Eight paragraphs, each `

        `, its text, `

        ` and a line feed, make the bound exactly. const block = paragraph(text('a'.repeat(536870888 / 8 - 8))) const written = adfToHtml(document(block, block, block, block, block, block, block, block)) assert.equal(written.ok && written.value.length, 536870888) })