3
0
mirror of https://github.com/larvit/adf-codec.git synced 2026-11-19 06:35:44 +01:00

Refuse an inline card a link marks in every writer, before it writes

This commit is contained in:
2026-11-04 20:16:16 +01:00
committed by Lillem4n
parent 30f45b75d8
commit a27a4453c2
12 changed files with 97 additions and 52 deletions
+29
View File
@@ -4,6 +4,7 @@ import vm from 'node:vm'
import type { AdfDocument } from './document.ts'
import type { JsonValue } from '../json-value.ts'
import { adfToHtml, adfToLosslessMarkdown, adfToPortableMarkdown } from '../index.ts'
import { adfDocumentFault, isAdfDocument, writableContent } from './document.ts'
import { largestNesting } from '../nesting.ts'
@@ -129,3 +130,31 @@ test('opens every writer with the same checks: a document, version 1, and no mor
const text = { content: [{ text: long, type: 'text' }, { text: long, type: 'text' }, { text: long, type: 'text' }], type: 'paragraph' }
assert.equal(checked({ content: [text], type: 'doc', version: 1 }), 'unsupported-node-shape')
})
test('refuses an inline card a link marks, wherever the document holds it and whichever call writes it', () => {
const card = { attrs: { url: 'https://example.com/roadmap' }, marks: [{ attrs: { href: 'https://example.com/' }, type: 'link' }], type: 'inlineCard' }
const paragraph = { content: [{ text: 'See ', type: 'text' }, card], type: 'paragraph' }
const holders = [
{ holder: paragraph, path: ['content', 0, 'content', 1] },
{ holder: { content: [{ content: [paragraph], type: 'listItem' }], type: 'bulletList' }, path: ['content', 0, 'content', 0, 'content', 0, 'content', 1] },
{ holder: { content: [paragraph], type: 'panel' }, path: ['content', 0, 'content', 0, 'content', 1] },
{ holder: { content: [card, { text: 'x', type: 'text' }], marks: [{ attrs: { align: 'center' }, type: 'alignment' }], type: 'paragraph' }, path: ['content', 0, 'content', 0] },
{ holder: { content: [{ content: [card], type: 'futureNode' }], type: 'futureBlock' }, path: ['content', 0, 'content', 0, 'content', 0] },
]
const writers = [adfToHtml, adfToLosslessMarkdown, adfToPortableMarkdown]
for (const { holder, path } of holders) {
for (const write of writers) {
const refused = write({ content: [holder], type: 'doc', version: 1 })
assert.equal(!refused.ok && refused.error.code, 'unsupported-node-shape')
assert.deepEqual(!refused.ok && refused.error.path, path)
assert.match(!refused.ok ? refused.error.message : '', /inlineCard.*link/)
}
}
})
test('writes a card holding no link mark, and a node type it does not know holding one', () => {
const comment = { attrs: { annotationType: 'inlineComment', id: 'c1' }, type: 'annotation' }
const link = { attrs: { href: 'https://example.com/' }, type: 'link' }
const marked = { content: [{ attrs: { url: 'https://example.com/roadmap' }, marks: [comment], type: 'inlineCard' }, { marks: [link], type: 'futureInline' }], type: 'paragraph' }
for (const write of [adfToHtml, adfToLosslessMarkdown, adfToPortableMarkdown]) assert.equal(write({ content: [marked], type: 'doc', version: 1 }).ok, true)
})
+21
View File
@@ -55,6 +55,8 @@ export function writableContent(document: AdfDocument): Result<readonly AdfNode[
if (fault !== undefined) return faulted(fault, [])
if (document.version !== 1) return failure('unsupported-document-version', `no format this library writes carries ADF version ${document.version}`, [])
if (document.content === undefined) return success(undefined)
const refused = refusedShape(document.content)
if (refused !== undefined) return refused
// Text alone past the bound is refused before anything is written (docs/decisions.md §Output past V8's longest string is refused on every engine).
return withinLongest(heldTextLength(document.content), document.content)
}
@@ -145,6 +147,25 @@ export function nodeMarks(node: { marks?: AdfMark[] }): readonly AdfMark[] {
return node.marks ?? []
}
// The writers' refusals of what Atlassian's schema forbids, in document order, at any depth and inside nodes no writer reads. Each rule stands on the schema alone.
function refusedShape(content: readonly AdfNode[]): Result<never> | undefined {
type Step = { index: number; node: AdfNode; parent: Step | undefined }
const pending: Step[] = content.map((node, index) => ({ index, node, parent: undefined })).reverse()
for (let step = pending.pop(); step !== undefined; step = pending.pop()) {
if (step.node.type === 'inlineCard' && nodeMarks(step.node).some(({ type }) => type === 'link')) {
const path: (number | string)[] = []
for (let up: Step | undefined = step; up !== undefined; up = up.parent) path.unshift('content', up.index)
return failure('unsupported-node-shape', 'an inlineCard takes no link mark: this one holds one', path)
}
const children = nodeContent(step.node)
for (let index = children.length - 1; index >= 0; index -= 1) {
const node = children[index]
if (node !== undefined) pending.push({ index, node, parent: step })
}
}
return undefined
}
// Tracks ancestors alone: a tree may share an object, but an object holding itself walks the guards forever.
function holdsItself(root: object): boolean {
const pending = [{ depth: 0, item: root }]