Answer stability round 3: walk an array's elements as the guards do
This commit is contained in:
+3
-1
@@ -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
|
||||
|
||||
|
||||
@@ -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
@@ -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))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user