mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-18 22:25:44 +01:00
Refuse a cyclic input as not-an-adf-document
This commit is contained in:
@@ -82,3 +82,23 @@ test("reads a node's siblings as a walk rather than as one call's arguments", ()
|
||||
assert.equal(isAdfDocument(wide), true)
|
||||
assert.equal(fault(wide), 'accepted')
|
||||
})
|
||||
|
||||
test('refuses a document that holds itself, and accepts one sharing a node or a mark', () => {
|
||||
const notANode = "an ADF document's content holds ADF nodes: one of them is not"
|
||||
const paragraph: { content: unknown[]; type: string } = { content: [], type: 'paragraph' }
|
||||
paragraph.content.push(paragraph)
|
||||
assert.equal(fault({ content: [paragraph], type: 'doc', version: 1 }), notANode)
|
||||
const document: { content: unknown[]; type: string; version: number } = { content: [], type: 'doc', version: 1 }
|
||||
document.content.push(document)
|
||||
assert.equal(fault(document), notANode)
|
||||
const attrs: Record<string, unknown> = {}
|
||||
attrs['a'] = [attrs]
|
||||
assert.equal(fault({ content: [{ attrs, type: 'paragraph' }], type: 'doc', version: 1 }), notANode)
|
||||
assert.equal(fault({ content: [{ marks: [{ attrs, type: 'link' }], text: 'x', type: 'text' }], type: 'doc', version: 1 }), notANode)
|
||||
assert.equal(isAdfDocument({ content: [{ attrs, type: 'paragraph' }], type: 'doc', version: 1 }), false)
|
||||
const bold = { type: 'strong' }
|
||||
const text = { marks: [bold], text: 'x', type: 'text' }
|
||||
const shared = { content: [text, text], type: 'paragraph' }
|
||||
const values = { b: 1 }
|
||||
assert.equal(fault({ content: [shared, shared, { attrs: { a: [values, values] }, type: 'paragraph' }], type: 'doc', version: 1 }), 'accepted')
|
||||
})
|
||||
|
||||
+11
-16
@@ -1,5 +1,5 @@
|
||||
import type { ConvertFault } from '../result.ts'
|
||||
import { isJsonValue, overNested, type JsonValue } from '../json-value.ts'
|
||||
import { everyInTree, isJsonValue, overNested, type JsonValue } from '../json-value.ts'
|
||||
import { largestNesting } from '../nesting.ts'
|
||||
import { serializeCanonicalJson } from '../canonical-json.ts'
|
||||
|
||||
@@ -123,21 +123,16 @@ export function nodeMarks(node: { marks?: AdfMark[] }): readonly AdfMark[] {
|
||||
}
|
||||
|
||||
function isNodeArray(value: readonly unknown[]): value is readonly AdfNode[] {
|
||||
const pending: unknown[] = [...value]
|
||||
while (pending.length > 0) {
|
||||
const node = pending.pop()
|
||||
if (!isRecord(node) || !holdsOnly(node, nodeKeys)) return false
|
||||
if (typeof node['type'] !== 'string') return false
|
||||
if ('attrs' in node && !isAttributes(node['attrs'])) return false
|
||||
if ('marks' in node && !isArrayOf(node['marks'], isAdfMark)) return false
|
||||
if ('text' in node && typeof node['text'] !== 'string') return false
|
||||
if ('content' in node) {
|
||||
const content = node['content']
|
||||
if (!Array.isArray(content)) return false
|
||||
for (const child of content) pending.push(child)
|
||||
}
|
||||
}
|
||||
return true
|
||||
return everyInTree(value, (node) => {
|
||||
if (!isRecord(node) || !holdsOnly(node, nodeKeys)) return undefined
|
||||
if (typeof node['type'] !== 'string') return undefined
|
||||
if ('attrs' in node && !isAttributes(node['attrs'])) return undefined
|
||||
if ('marks' in node && !isArrayOf(node['marks'], isAdfMark)) return undefined
|
||||
if ('text' in node && typeof node['text'] !== 'string') return undefined
|
||||
if (!('content' in node)) return []
|
||||
const content = node['content']
|
||||
return Array.isArray(content) ? [...content] : undefined
|
||||
})
|
||||
}
|
||||
|
||||
function marksKey(marks: readonly AdfMark[]): string {
|
||||
|
||||
Reference in New Issue
Block a user