mirror of
https://github.com/larvit/adf-codec.git
synced 2026-11-18 22:25:44 +01:00
Answer principles round 1: 1 🔴, 5 🟡 (0 misread); the document carry's line feeds checked, element spellings shared with the writer, carried JSON bounded by type, an edit to a carry refused, one test per case, a start tag read one attribute at a time
This commit is contained in:
@@ -1,37 +1,35 @@
|
||||
import type { AdfNode } from '../../adf/document.ts'
|
||||
import type { JsonValue } from '../../json-value.ts'
|
||||
import type { ParseError, Read, Result } from '../../result.ts'
|
||||
import type { Read } from '../../result.ts'
|
||||
import { carriedNesting, carriedNode, carriedValue } from '../../adf/carried-json.ts'
|
||||
import { carryContent } from '../carry-content.ts'
|
||||
import { foundAt, refused, type Place, type ReadNode, type StartTag } from './source.ts'
|
||||
import { holdableJson } from '../escaping.ts'
|
||||
import { readCarryAttributes } from '../carry-attributes.ts'
|
||||
import { refused, type Place, type StartTag } from './source.ts'
|
||||
import { spellsCanonicalJson } from '../../canonical-json.ts'
|
||||
import { success } from '../../result.ts'
|
||||
|
||||
const holder = "a carry element's data-adf"
|
||||
|
||||
// The JSON a carry element holds, and the element's end; what it shows is skipped unread (docs/decisions.md §The HTML dialect).
|
||||
export function readCarry(html: string, tag: StartTag, levels: number, place: Place): Result<{ end: number; held: JsonValue; json: string }, ParseError> {
|
||||
// What the element shows must read back as `adfToHtml` shows its node, so an edit to it is refused rather than dropped (README goal 7).
|
||||
export function readCarry(html: string, tag: StartTag, element: 'div' | 'span', levels: number, place: Place): ReadNode {
|
||||
const json = readCarryAttributes(tag.attributes)
|
||||
if (json === undefined) return refused('unmappable-html', `a <${tag.name}> of the dialect is a carry element, holding class="adf-carry" and then data-adf alone`, html, tag.end, place)
|
||||
const held = heldJson(json, levels)
|
||||
if (held.fault !== undefined) return refused(held.fault.code, held.fault.message, html, tag.end, place)
|
||||
const closer = `</${tag.name}>`
|
||||
const end = html.indexOf(closer, tag.end)
|
||||
return end < 0 ? refused('unmappable-html', `a carry element ends at ${closer}: found none`, html, tag.end, place) : success({ end: end + closer.length, held: held.value, json })
|
||||
const node = heldNode(json, levels)
|
||||
if (node.fault !== undefined) return refused(node.fault.code, node.fault.message, html, tag.end, place)
|
||||
const shown = `${carryContent(node.value, element)}</${element}>`
|
||||
if (html.startsWith(shown, tag.end)) return success({ end: tag.end + shown.length, node: node.value })
|
||||
let at = tag.end
|
||||
while (html[at] === shown[at - tag.end]) at += 1
|
||||
return refused('unmappable-html', `a carry element shows its node's text as adfToHtml writes it, then ends at </${element}>; an edit to it is no edit to the node: ${foundAt(html, at)}`, html, at, place)
|
||||
}
|
||||
|
||||
// Short of the node, which `heldNode` reads once the element's end is found, so a carry missing its closing tag is refused for that first.
|
||||
function heldJson(json: string, levels: number): Read<JsonValue> {
|
||||
function heldNode(json: string, levels: number): Read<AdfNode> {
|
||||
const held = carriedValue(json, holder, { code: 'unmappable-html', message: `${holder} holds invalid JSON` })
|
||||
if (held.fault !== undefined) return held
|
||||
const nested = carriedNesting(held.value, levels)
|
||||
if (nested !== undefined) return { fault: nested }
|
||||
if (spellsCanonicalJson(held.value, 'compact', json, holdableJson)) return held
|
||||
return { fault: { code: 'unsupported-node-shape', message: "a carry element spells its node's JSON canonically: compact, keys sorted, a character HTML cannot hold as a JSON escape" } }
|
||||
}
|
||||
|
||||
export function heldNode(held: JsonValue, html: string, index: number, place: Place): Result<AdfNode, ParseError> {
|
||||
const node = carriedNode(held, holder)
|
||||
return node.fault === undefined ? success(node.value) : refused(node.fault.code, node.fault.message, html, index, place)
|
||||
if (nested.fault !== undefined) return nested
|
||||
if (!spellsCanonicalJson(held.value, 'compact', json, holdableJson)) {
|
||||
return { fault: { code: 'unsupported-node-shape', message: "a carry element spells its node's JSON canonically: compact, keys sorted, a character HTML cannot hold as a JSON escape" } }
|
||||
}
|
||||
return carriedNode(nested.value, holder)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user