5c: match the mark set's nesting on both sides of the round-trip
CI / gate (push) Successful in 8m35s
CI / publish (push) Has been skipped

This commit is contained in:
2026-09-04 08:53:00 +02:00
parent 0dfcc0f9ca
commit 67df1e2f4b
7 changed files with 52 additions and 24 deletions
+6 -4
View File
@@ -61,14 +61,16 @@ test('rejects a node whose shape ProseMirror JSON cannot hold', () => {
})
test('names the attribute nesting past the levels the parser reads one at, and still calls the value a document', () => {
const deeper = (key: string, type: string): string => `the ${key} attribute of ${type} nests deeper than the ${largestNesting} levels an attribute carries`
const deeper = (key: string, type: string, levels: number = largestNesting): string =>
`the ${key} attribute of ${type} nests deeper than the ${levels} levels an attribute carries`
assert.equal(fault(withAttribute(nested(largestNesting))), 'accepted')
assert.equal(fault(withAttribute(nested(largestNesting + 1))), deeper('a', 'paragraph'))
assert.equal(faultCode(withAttribute(nested(largestNesting + 1))), 'unsupported-nesting-depth')
assert.equal(isAdfDocument(withAttribute(nested(largestNesting + 1))), true)
const marked = { content: [{ marks: [{ attrs: { a: nested(largestNesting + 1) }, type: 'link' }], text: 'x', type: 'text' }], type: 'doc', version: 1 }
assert.equal(fault(marked), deeper('a', 'link'))
assert.equal(isAdfDocument(marked), true)
const marked = (levels: number): unknown => ({ content: [{ marks: [{ attrs: { a: nested(levels) }, type: 'link' }], text: 'x', type: 'text' }], type: 'doc', version: 1 })
assert.equal(fault(marked(largestNesting - 3)), 'accepted')
assert.equal(fault(marked(largestNesting - 2)), deeper('a', 'link', largestNesting - 3))
assert.equal(isAdfDocument(marked(largestNesting - 2)), true)
})
test('accepts the JSON values an attribute may hold', () => {
+8 -5
View File
@@ -23,6 +23,9 @@ export type AdfDocument = {
version: number
}
// A block directive spells the whole mark set as one JSON attribute, so a mark's value sits three levels inside it.
const markAttributeNesting = largestNesting - 3
const documentKeys = ['content', 'type', 'version']
const markKeys = ['attrs', 'type']
const nodeKeys = ['attrs', 'content', 'marks', 'text', 'type']
@@ -44,8 +47,8 @@ export function adfDocumentFault(value: unknown): ConvertFault | undefined {
return nestingFault(content)
}
export function attributeNestingMessage(key: string, type: string): string {
return `the ${key} attribute of ${type} nests deeper than the ${largestNesting} levels an attribute carries`
export function attributeNestingMessage(key: string, type: string, levels: number = largestNesting): string {
return `the ${key} attribute of ${type} nests deeper than the ${levels} levels an attribute carries`
}
export function carriesOnly(node: AdfNode, attributes: readonly string[]): boolean {
@@ -101,15 +104,15 @@ function nestingFault(nodes: readonly AdfNode[]): ConvertFault | undefined {
function marksFault(marks: readonly AdfMark[] | undefined): ConvertFault | undefined {
for (const mark of marks ?? []) {
const fault = attributesFault(mark.attrs, mark.type)
const fault = attributesFault(mark.attrs, mark.type, markAttributeNesting)
if (fault !== undefined) return fault
}
return undefined
}
function attributesFault(attrs: AdfAttributes | undefined, type: string): ConvertFault | undefined {
function attributesFault(attrs: AdfAttributes | undefined, type: string, levels: number = largestNesting): ConvertFault | undefined {
for (const [key, value] of Object.entries(attrs ?? {})) {
if (overNested(value)) return { code: 'unsupported-nesting-depth', message: attributeNestingMessage(key, type) }
if (overNested(value, levels)) return { code: 'unsupported-nesting-depth', message: attributeNestingMessage(key, type, levels) }
}
return undefined
}