Refuse a cyclic input as not-an-adf-document #159

Merged
lilleman merged 18 commits from cyclic-input into main 2026-10-04 19:36:20 +02:00
3 changed files with 14 additions and 4 deletions
Showing only changes of commit d26e4e8152 - Show all commits
+3 -1
View File
@@ -32,7 +32,9 @@
- Fix an image inside another image's description: it flattens into the alt text, where it was
refused.
- Fix a cyclic document, in which an object holds itself: `adfToMarkdown` and `adfToPlainMarkdown`
refuse it as `not-an-adf-document` and `isAdfDocument` returns `false`, where all three hung.
refuse it as `not-an-adf-document` and `isAdfDocument` returns `false`, where all three hung. A
node or mark whose `attrs`, `content`, `marks`, `text` or `type` is not its own enumerable key is
refused alike, where it was read.
## 0.1.0
+9 -1
View File
@@ -83,7 +83,7 @@ test("reads a node's siblings as a walk rather than as one call's arguments", ()
assert.equal(fault(wide), 'accepted')
})
test('refuses a node or an attribute value holding itself, and accepts one shared', () => {
test('refuses an object holding itself or a key it does not own, and accepts one shared', () => {
const holdsItself = 'an ADF document is a tree: an object in it holds itself'
const paragraph: { content: unknown[]; type: string } = { content: [], type: 'paragraph' }
paragraph.content.push(paragraph)
@@ -108,6 +108,14 @@ test('refuses a node or an attribute value holding itself, and accepts one share
const link: Record<string, unknown> = Object.create({ attrs })
link['type'] = 'link'
assert.equal(fault({ content: [{ marks: [link], text: 'x', type: 'text' }], type: 'doc', version: 1 }), notANode)
const indexed: unknown[] = []
Object.defineProperty(indexed, 0, { value: indexed })
assert.equal(fault({ content: [{ attrs: { a: indexed }, type: 'paragraph' }], type: 'doc', version: 1 }), holdsItself)
const inheritedIndex: { content?: unknown[]; type: string } = { type: 'paragraph' }
const heirs: unknown[] = Object.setPrototypeOf([], [inheritedIndex])
heirs.length = 1
inheritedIndex.content = heirs
assert.equal(fault({ content: [inheritedIndex], type: 'doc', version: 1 }), holdsItself)
const bold = { type: 'strong' }
const text = { marks: [bold], text: 'x', type: 'text' }
const shared = { content: [text, text], type: 'paragraph' }
+2 -2
View File
@@ -135,7 +135,7 @@ function holdsItself(root: object): boolean {
if (onPath.has(entry.item)) return true
ancestors.push(entry.item)
onPath.add(entry.item)
for (const child of Object.values(entry.item)) if (typeof child === 'object' && child !== null) pending.push({ depth: entry.depth + 1, item: child })
for (const child of Array.isArray(entry.item) ? [...entry.item] : Object.values(entry.item)) if (typeof child === 'object' && child !== null) pending.push({ depth: entry.depth + 1, item: child })
}
return false
}
@@ -219,7 +219,7 @@ function extraKey(value: Record<string, unknown>, keys: readonly string[]): stri
return Object.keys(value).find((key) => !keys.includes(key))
}
// holdsItself walks own enumerable keys alone, so a guard reading an inherited one could walk a cycle forever.
// holdsItself walks own enumerable keys alone, so a guard reading any other key could walk a cycle forever.
function ownsWhatItHolds(value: Record<string, unknown>, keys: readonly string[]): boolean {
return keys.every((key) => !(key in value) || Object.prototype.propertyIsEnumerable.call(value, key))
}