3
0
mirror of https://github.com/larvit/adf-codec.git synced 2026-11-18 22:25:44 +01:00

Refuse the schema-forbidden shapes in the first step, carried or not

This commit is contained in:
2026-11-05 11:02:45 +01:00
committed by Lillem4n
parent 24247eff85
commit d55a249ef3
18 changed files with 132 additions and 47 deletions
+35
View File
@@ -152,6 +152,41 @@ test('refuses an inline card a link marks, wherever the document holds it and wh
}
})
test('refuses what Atlassian\'s schema gives no node, wherever the document holds it and whichever call writes it', () => {
const shapes: { message: string; node: AdfNode }[] = [
{ message: 'a panel carries no text: this one holds text', node: { text: 'x', type: 'panel' } },
{ message: 'a paragraph carries no text: this one holds text', node: { text: 'x', type: 'paragraph' } },
{ message: 'a media holds no content: this one holds some', node: { content: [{ type: 'paragraph' }], type: 'media' } },
{ 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 content', node: { content: [{ text: 'x', type: 'text' }], type: 'status' } },
{ message: 'a status node holds neither content nor text: this one holds text', node: { text: 'x', type: 'status' } },
{ message: 'a hardBreak node holds neither content nor text: this one holds text', node: { text: 'x', type: 'hardBreak' } },
]
for (const { message, node } of shapes) {
const holders = [
{ content: [node], type: 'doc' },
{ content: [{ content: [node], type: 'paragraph' }], type: 'doc' },
{ content: [{ content: [node], type: 'futureBlock' }], type: 'doc' },
]
const paths = [['content', 0], ['content', 0, 'content', 0], ['content', 0, 'content', 0]]
for (const [index, { content }] of holders.entries()) {
for (const write of [adfToHtml, adfToLosslessMarkdown, adfToPortableMarkdown]) {
const refused = write({ content, type: 'doc', version: 1 })
assert.deepEqual(refused.ok ? 'written' : refused.error, { code: 'unsupported-node-shape', message, path: paths[index] })
}
}
}
})
test('writes what the schema refusals leave to the carry', () => {
const carried: AdfNode[] = [
{ content: [], type: 'rule' },
{ content: [{ text: 'x', type: 'text' }], text: 'y', type: 'futureBlock' },
{ content: [{ content: [{ type: 'paragraph' }], type: 'text' }], type: 'paragraph' },
]
for (const node of carried) for (const write of [adfToHtml, adfToLosslessMarkdown, adfToPortableMarkdown]) assert.equal(write({ content: [node], type: 'doc', version: 1 }).ok, true)
})
test('names the first link-marked card in document order', () => {
const card = (url: string): AdfNode => ({ attrs: { url }, marks: [{ attrs: { href: 'https://example.com/' }, type: 'link' }], type: 'inlineCard' })
const content = [{ content: [card('https://a.example/'), card('https://b.example/')], type: 'paragraph' }, { content: [card('https://c.example/')], type: 'paragraph' }]
+19 -3
View File
@@ -1,4 +1,6 @@
import { blockNodeModel } from './block-nodes.ts'
import { failure, faulted, success, type ConvertError, type ConvertFault, type Result } from '../result.ts'
import { inlineNodeModel } from './inline-nodes.ts'
import { isJsonValue, overNested, type JsonValue } from '../json-value.ts'
import { largestNesting } from '../nesting.ts'
import { withinLongest } from '../length-bound.ts'
@@ -147,22 +149,36 @@ export function nodeMarks(node: { marks?: AdfMark[] }): readonly AdfMark[] {
return node.marks ?? []
}
// Walks every node, carried or of an unknown type, so a link-marked card is refused wherever it stands (docs/decisions.md §Schema refusals are made in the first step alone).
// Walks every node, carried or of an unknown type, so a shape is refused wherever it stands (docs/decisions.md §Schema refusals are made in the first step alone).
export function refusedShape(content: readonly AdfNode[]): ConvertError | 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 message = shapeRefusal(step.node)
if (message !== undefined) {
const path: (number | string)[] = []
for (let up: Step | undefined = step; up !== undefined; up = up.parent) path.push(up.index, 'content')
path.reverse()
return { code: 'unsupported-node-shape', message: 'an inlineCard takes no link mark: this one holds one', path }
return { code: 'unsupported-node-shape', message, path }
}
for (const child of nodeContent(step.node).map((node, index) => ({ index, node, parent: step })).reverse()) pending.push(child)
}
return undefined
}
function shapeRefusal(node: AdfNode): string | undefined {
if (node.type === 'inlineCard' && nodeMarks(node).some(({ type }) => type === 'link')) return 'an inlineCard takes no link mark: this one holds one'
const holdsContent = nodeContent(node).length > 0
if (inlineNodeModel(node.type) !== undefined) {
if (holdsContent) return `a ${node.type} node holds neither content nor text: this one holds content`
return node.text === undefined ? undefined : `a ${node.type} node holds neither content nor text: this one holds text`
}
const model = blockNodeModel(node.type)
if (model === undefined) return undefined
if (node.text !== undefined) return `a ${node.type} carries no text: this one holds text`
return model.contentModel === 'none' && holdsContent ? `a ${node.type} holds no content: this one holds some` : 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 }]
+1 -2
View File
@@ -1,6 +1,5 @@
import type { AdfNode } from './document.ts'
import type { AttributeVocabulary } from './attribute-vocabulary.ts'
import { nodeMarks } from './document.ts'
export const codeTextMarkTypes: readonly string[] = ['annotation', 'code', 'link']
export const formattedTextMarkTypes: readonly string[] = ['annotation', 'backgroundColor', 'em', 'link', 'strike', 'strong', 'subsup', 'textColor', 'underline']
@@ -40,7 +39,7 @@ export function inlineNodeModel(type: string): InlineNodeModel | undefined {
// Undefined means every mark: a node type outside this module's tables, which the schema restricts nowhere.
export function takenMarkTypes(node: AdfNode): readonly string[] | undefined {
if (node.type === 'text') return nodeMarks(node).some((mark) => mark.type === 'code') ? codeTextMarkTypes : formattedTextMarkTypes
if (node.type === 'text') return node.marks?.some((mark) => mark.type === 'code') === true ? codeTextMarkTypes : formattedTextMarkTypes
return inlineNodeModel(node.type)?.markTypes
}