diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e1c5cc..791878c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/adf/document.test.ts b/src/adf/document.test.ts index af13f46..1a876fb 100644 --- a/src/adf/document.test.ts +++ b/src/adf/document.test.ts @@ -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 = 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' } diff --git a/src/adf/document.ts b/src/adf/document.ts index 7c4060b..290258b 100644 --- a/src/adf/document.ts +++ b/src/adf/document.ts @@ -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, 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, keys: readonly string[]): boolean { return keys.every((key) => !(key in value) || Object.prototype.propertyIsEnumerable.call(value, key)) }