Compare commits
10 Commits
f9f863fb28
...
fb0d2b4eac
| Author | SHA1 | Date | |
|---|---|---|---|
| fb0d2b4eac | |||
| 638881b555 | |||
| d26e4e8152 | |||
| e501b95cb3 | |||
| b9fa9bff85 | |||
| 9778844c11 | |||
| 261dc3a5cf | |||
| addef08fa4 | |||
| 438bf8876b | |||
| bf610fc438 |
+4
-2
@@ -31,8 +31,10 @@
|
|||||||
`collection`, `id` and `occurrenceKey` directly where they rode the opaque carry.
|
`collection`, `id` and `occurrenceKey` directly where they rode the opaque carry.
|
||||||
- Fix an image inside another image's description: it flattens into the alt text, where it was
|
- Fix an image inside another image's description: it flattens into the alt text, where it was
|
||||||
refused.
|
refused.
|
||||||
- Fix a document holding itself, through a node's `content` or an attribute: `adfToMarkdown`,
|
- Fix a cyclic document, in which an object holds itself: `adfToMarkdown` and `adfToPlainMarkdown`
|
||||||
`adfToPlainMarkdown` and `isAdfDocument` refuse it as `not-an-adf-document`, where they 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
|
## 0.1.0
|
||||||
|
|
||||||
|
|||||||
@@ -185,7 +185,7 @@ Emitting — `adfToMarkdown`, and `adfToHtml` at `0.2.0`:
|
|||||||
|
|
||||||
| Code | Fires when | What you can do |
|
| Code | Fires when | What you can do |
|
||||||
| --- | --- | --- |
|
| --- | --- | --- |
|
||||||
| `not-an-adf-document` | the value handed in is no ADF document — a missing or wrong `type`, a stray key, a node that is not a node | guard the boundary you receive JSON at with `isAdfDocument`; the message names the branch that refused |
|
| `not-an-adf-document` | the value handed in is no ADF document — a missing or wrong `type`, a stray key, a node that is not a node, an object in the document holding itself | guard the boundary you receive JSON at with `isAdfDocument`; the message names the branch that refused |
|
||||||
| `unsupported-document-version` | the document's `version` is not 1 | keep the ADF and pass the document over, or show it read-only; the version is the site's, not yours to change |
|
| `unsupported-document-version` | the document's `version` is not 1 | keep the ADF and pass the document over, or show it read-only; the version is the site's, not yours to change |
|
||||||
|
|
||||||
Either direction. The emitter's own refusals are in this group — a parse reaches them by asking it
|
Either direction. The emitter's own refusals are in this group — a parse reaches them by asking it
|
||||||
|
|||||||
+4
-4
@@ -384,10 +384,10 @@ input reads `message`.
|
|||||||
must invert before it reads as a failure — and where the flavour's claim refuses ordinary prose
|
must invert before it reads as a failure — and where the flavour's claim refuses ordinary prose
|
||||||
it names the escape that unclaims the form claimed: `\!adf:` for a directive, block line and
|
it names the escape that unclaims the form claimed: `\!adf:` for a directive, block line and
|
||||||
inline alike, `\|` for every pipe row.
|
inline alike, `\|` for every pipe row.
|
||||||
- `not-an-adf-document` carries the document's own path throughout: seven of the guard's eight
|
- `not-an-adf-document` carries the document's own path throughout: seven of the guard's nine
|
||||||
branches read the document's own shape, and threading a path to the eighth — a malformed node
|
branches read the document's own shape, and threading a path to the other two, which look anywhere
|
||||||
anywhere in the tree — wants the manual stack §Nothing recurses unbounded forces. The message
|
in the tree — a malformed node, or an object holding itself — wants the manual stack §Nothing
|
||||||
names the violation instead.
|
recurses unbounded forces. The message names the violation instead.
|
||||||
|
|
||||||
## Publish on a version bump
|
## Publish on a version bump
|
||||||
|
|
||||||
|
|||||||
@@ -83,19 +83,39 @@ test("reads a node's siblings as a walk rather than as one call's arguments", ()
|
|||||||
assert.equal(fault(wide), 'accepted')
|
assert.equal(fault(wide), 'accepted')
|
||||||
})
|
})
|
||||||
|
|
||||||
test('refuses a document that holds itself, and accepts one sharing a node or a mark', () => {
|
test('refuses an object holding itself or a key it does not own, and accepts one shared', () => {
|
||||||
const notANode = "an ADF document's content holds ADF nodes: one of them is not"
|
const holdsItself = 'an ADF document is a tree: an object in it holds itself'
|
||||||
const paragraph: { content: unknown[]; type: string } = { content: [], type: 'paragraph' }
|
const paragraph: { content: unknown[]; type: string } = { content: [], type: 'paragraph' }
|
||||||
paragraph.content.push(paragraph)
|
paragraph.content.push(paragraph)
|
||||||
assert.equal(fault({ content: [paragraph], type: 'doc', version: 1 }), notANode)
|
assert.equal(fault({ content: [paragraph], type: 'doc', version: 1 }), holdsItself)
|
||||||
const document: { content: unknown[]; type: string; version: number } = { content: [], type: 'doc', version: 1 }
|
const document: { content: unknown[]; type: string; version: number } = { content: [], type: 'doc', version: 1 }
|
||||||
document.content.push(document)
|
document.content.push(document)
|
||||||
assert.equal(fault(document), notANode)
|
assert.equal(fault(document), holdsItself)
|
||||||
const attrs: Record<string, unknown> = {}
|
const attrs: Record<string, unknown> = {}
|
||||||
attrs['a'] = [attrs]
|
attrs['a'] = [attrs]
|
||||||
assert.equal(fault({ content: [{ attrs, type: 'paragraph' }], type: 'doc', version: 1 }), notANode)
|
assert.equal(fault({ content: [{ attrs, type: 'paragraph' }], type: 'doc', version: 1 }), holdsItself)
|
||||||
assert.equal(fault({ content: [{ marks: [{ attrs, type: 'link' }], text: 'x', type: 'text' }], type: 'doc', version: 1 }), notANode)
|
assert.equal(fault({ content: [{ marks: [{ attrs, type: 'link' }], text: 'x', type: 'text' }], type: 'doc', version: 1 }), holdsItself)
|
||||||
assert.equal(isAdfDocument({ content: [{ attrs, type: 'paragraph' }], type: 'doc', version: 1 }), false)
|
assert.equal(isAdfDocument({ content: [{ attrs, type: 'paragraph' }], type: 'doc', version: 1 }), false)
|
||||||
|
const notANode = "an ADF document's content holds ADF nodes: one of them is not"
|
||||||
|
const inherits: { content?: unknown[] } = {}
|
||||||
|
const heir: Record<string, unknown> = Object.create(inherits)
|
||||||
|
heir['type'] = 'paragraph'
|
||||||
|
inherits.content = [heir]
|
||||||
|
assert.equal(fault({ content: [heir], type: 'doc', version: 1 }), notANode)
|
||||||
|
const hidden: Record<string, unknown> = { type: 'paragraph' }
|
||||||
|
Object.defineProperty(hidden, 'content', { value: [hidden] })
|
||||||
|
assert.equal(fault({ content: [hidden], type: 'doc', version: 1 }), notANode)
|
||||||
|
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 bold = { type: 'strong' }
|
||||||
const text = { marks: [bold], text: 'x', type: 'text' }
|
const text = { marks: [bold], text: 'x', type: 'text' }
|
||||||
const shared = { content: [text, text], type: 'paragraph' }
|
const shared = { content: [text, text], type: 'paragraph' }
|
||||||
|
|||||||
+40
-12
@@ -1,5 +1,5 @@
|
|||||||
import type { ConvertFault } from '../result.ts'
|
import type { ConvertFault } from '../result.ts'
|
||||||
import { everyInTree, isJsonValue, overNested, type JsonValue } from '../json-value.ts'
|
import { isJsonValue, overNested, type JsonValue } from '../json-value.ts'
|
||||||
import { largestNesting } from '../nesting.ts'
|
import { largestNesting } from '../nesting.ts'
|
||||||
import { serializeCanonicalJson } from '../canonical-json.ts'
|
import { serializeCanonicalJson } from '../canonical-json.ts'
|
||||||
|
|
||||||
@@ -43,6 +43,7 @@ export function adfDocumentFault(value: unknown): ConvertFault | undefined {
|
|||||||
const held: unknown = value['content']
|
const held: unknown = value['content']
|
||||||
if (!Array.isArray(held)) return notADocument(`an ADF document's content is an array: found ${describe(held)}`)
|
if (!Array.isArray(held)) return notADocument(`an ADF document's content is an array: found ${describe(held)}`)
|
||||||
const content: readonly unknown[] = held
|
const content: readonly unknown[] = held
|
||||||
|
if (holdsItself(content)) return notADocument('an ADF document is a tree: an object in it holds itself')
|
||||||
if (!isNodeArray(content)) return notADocument("an ADF document's content holds ADF nodes: one of them is not")
|
if (!isNodeArray(content)) return notADocument("an ADF document's content holds ADF nodes: one of them is not")
|
||||||
return nestingFault(content)
|
return nestingFault(content)
|
||||||
}
|
}
|
||||||
@@ -105,7 +106,7 @@ export function isAdfNode(value: unknown): value is AdfNode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function isAdfMark(value: unknown): value is AdfMark {
|
export function isAdfMark(value: unknown): value is AdfMark {
|
||||||
if (!isRecord(value) || !holdsOnly(value, markKeys)) return false
|
if (!isRecord(value) || !holdsOnly(value, markKeys) || !ownsWhatItHolds(value, markKeys)) return false
|
||||||
if (typeof value['type'] !== 'string') return false
|
if (typeof value['type'] !== 'string') return false
|
||||||
return !('attrs' in value) || isAttributes(value['attrs'])
|
return !('attrs' in value) || isAttributes(value['attrs'])
|
||||||
}
|
}
|
||||||
@@ -122,17 +123,39 @@ export function nodeMarks(node: { marks?: AdfMark[] }): readonly AdfMark[] {
|
|||||||
return node.marks ?? []
|
return node.marks ?? []
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tracks ancestors alone: a tree may share an object, but an object holding itself walks the guards forever.
|
||||||
|
function holdsItself(root: object): boolean {
|
||||||
|
const pending = [{ depth: 0, item: root }]
|
||||||
|
const ancestors: object[] = []
|
||||||
|
const onPath = new Set<unknown>()
|
||||||
|
while (pending.length > 0) {
|
||||||
|
const entry = pending.pop()
|
||||||
|
if (entry === undefined) continue
|
||||||
|
while (ancestors.length > entry.depth) onPath.delete(ancestors.pop())
|
||||||
|
if (onPath.has(entry.item)) return true
|
||||||
|
ancestors.push(entry.item)
|
||||||
|
onPath.add(entry.item)
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
function isNodeArray(value: readonly unknown[]): value is readonly AdfNode[] {
|
function isNodeArray(value: readonly unknown[]): value is readonly AdfNode[] {
|
||||||
return everyInTree(value, (node) => {
|
const pending: unknown[] = [...value]
|
||||||
if (!isRecord(node) || !holdsOnly(node, nodeKeys)) return undefined
|
while (pending.length > 0) {
|
||||||
if (typeof node['type'] !== 'string') return undefined
|
const node = pending.pop()
|
||||||
if ('attrs' in node && !isAttributes(node['attrs'])) return undefined
|
if (!isRecord(node) || !holdsOnly(node, nodeKeys) || !ownsWhatItHolds(node, nodeKeys)) return false
|
||||||
if ('marks' in node && !isArrayOf(node['marks'], isAdfMark)) return undefined
|
if (typeof node['type'] !== 'string') return false
|
||||||
if ('text' in node && typeof node['text'] !== 'string') return undefined
|
if ('attrs' in node && !isAttributes(node['attrs'])) return false
|
||||||
if (!('content' in node)) return []
|
if ('marks' in node && !isArrayOf(node['marks'], isAdfMark)) return false
|
||||||
const content = node['content']
|
if ('text' in node && typeof node['text'] !== 'string') return false
|
||||||
return Array.isArray(content) ? [...content] : undefined
|
if ('content' in node) {
|
||||||
})
|
const content = node['content']
|
||||||
|
if (!Array.isArray(content)) return false
|
||||||
|
for (const child of content) pending.push(child)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
function marksKey(marks: readonly AdfMark[]): string {
|
function marksKey(marks: readonly AdfMark[]): string {
|
||||||
@@ -196,6 +219,11 @@ function extraKey(value: Record<string, unknown>, keys: readonly string[]): stri
|
|||||||
return Object.keys(value).find((key) => !keys.includes(key))
|
return Object.keys(value).find((key) => !keys.includes(key))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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))
|
||||||
|
}
|
||||||
|
|
||||||
function holdsOnly(value: Record<string, unknown>, keys: readonly string[]): boolean {
|
function holdsOnly(value: Record<string, unknown>, keys: readonly string[]): boolean {
|
||||||
return extraKey(value, keys) === undefined
|
return extraKey(value, keys) === undefined
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-25
@@ -2,36 +2,23 @@ import { largestNesting } from './nesting.ts'
|
|||||||
|
|
||||||
export type JsonValue = JsonValue[] | boolean | null | number | string | { [key: string]: JsonValue }
|
export type JsonValue = JsonValue[] | boolean | null | number | string | { [key: string]: JsonValue }
|
||||||
|
|
||||||
// Tracks ancestors alone: a tree may share an object, but one holding itself would walk forever.
|
export function isJsonValue(value: unknown): value is JsonValue {
|
||||||
export function everyInTree(roots: readonly unknown[], children: (item: unknown) => readonly unknown[] | undefined): boolean {
|
const pending: unknown[] = [value]
|
||||||
const pending = Array.from(roots, (item) => ({ depth: 0, item }))
|
|
||||||
const ancestors: unknown[] = []
|
|
||||||
const onPath = new Set<unknown>()
|
|
||||||
while (pending.length > 0) {
|
while (pending.length > 0) {
|
||||||
const entry = pending.pop()
|
const item = pending.pop()
|
||||||
if (entry === undefined) continue
|
if (item === null || typeof item === 'boolean' || typeof item === 'string') continue
|
||||||
while (ancestors.length > entry.depth) onPath.delete(ancestors.pop())
|
if (typeof item === 'number') {
|
||||||
if (onPath.has(entry.item)) return false
|
if (!Number.isFinite(item)) return false
|
||||||
const held = children(entry.item)
|
continue
|
||||||
if (held === undefined) return false
|
}
|
||||||
if (held.length === 0) continue
|
// A hole is not a JSON value, and Array.prototype methods skip holes — spreading materialises them.
|
||||||
ancestors.push(entry.item)
|
if (Array.isArray(item)) for (const child of [...item]) pending.push(child)
|
||||||
onPath.add(entry.item)
|
else if (typeof item === 'object') for (const child of Object.values(item)) pending.push(child)
|
||||||
for (const child of held) pending.push({ depth: entry.depth + 1, item: child })
|
else return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isJsonValue(value: unknown): value is JsonValue {
|
|
||||||
return everyInTree([value], (item) => {
|
|
||||||
if (item === null || typeof item === 'boolean' || typeof item === 'string') return []
|
|
||||||
if (typeof item === 'number') return Number.isFinite(item) ? [] : undefined
|
|
||||||
// A hole is not a JSON value, and Array.prototype methods skip holes — spreading materialises them.
|
|
||||||
if (Array.isArray(item)) return [...item]
|
|
||||||
return typeof item === 'object' ? Object.values(item) : undefined
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export function nestingDepth(value: unknown): number {
|
export function nestingDepth(value: unknown): number {
|
||||||
const pending: { depth: number; item: unknown }[] = [{ depth: 0, item: value }]
|
const pending: { depth: number; item: unknown }[] = [{ depth: 0, item: value }]
|
||||||
let deepest = 0
|
let deepest = 0
|
||||||
|
|||||||
Reference in New Issue
Block a user