mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-13 04:08:29 +01:00
Answer stability round 1: accept a plain object from another realm
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import test from 'node:test'
|
||||
import vm from 'node:vm'
|
||||
|
||||
import type { JsonValue } from '../json-value.ts'
|
||||
import { adfDocumentFault, isAdfDocument } from './document.ts'
|
||||
@@ -76,6 +77,7 @@ test('accepts the JSON values an attribute may hold', () => {
|
||||
assert.equal(isAdfDocument({ content: [{ attrs: { a: [1, 'x', null, true, { b: 2 }] }, type: 'paragraph' }], type: 'doc', version: 1 }), true)
|
||||
assert.equal(isAdfDocument({ content: [{ attrs: { a: [() => 1] }, type: 'paragraph' }], type: 'doc', version: 1 }), false)
|
||||
assert.equal(isAdfDocument({ content: [{ attrs: { a: Object.assign(Object.create(null), { b: 1 }) }, type: 'paragraph' }], type: 'doc', version: 1 }), true)
|
||||
assert.equal(isAdfDocument(vm.runInNewContext("({ content: [{ attrs: { a: { b: [1] } }, type: 'paragraph' }], type: 'doc', version: 1 })")), true)
|
||||
})
|
||||
|
||||
test('refuses an attribute value that is an object JSON does not hold', () => {
|
||||
|
||||
+2
-2
@@ -19,11 +19,11 @@ export function isJsonValue(value: unknown): value is JsonValue {
|
||||
return true
|
||||
}
|
||||
|
||||
// A Date, Map or class instance passes typeof but JSON writes it as {} or a key per element.
|
||||
// Any realm's Object.prototype has a null prototype; a Date, Map or typed array would serialize as {} or a key per element.
|
||||
function isPlainObject(value: unknown): value is object {
|
||||
if (typeof value !== 'object' || value === null) return false
|
||||
const prototype: unknown = Object.getPrototypeOf(value)
|
||||
return prototype === Object.prototype || prototype === null
|
||||
return prototype === null || (typeof prototype === 'object' && Object.getPrototypeOf(prototype) === null)
|
||||
}
|
||||
|
||||
export function nestingDepth(value: unknown): number {
|
||||
|
||||
Reference in New Issue
Block a user