3
0
mirror of https://github.com/larvit/adf-codec.git synced 2026-11-19 14:45:44 +01:00

Answer principles round 2: 0 🔴, 3 🟡 (0 misread); the document carry's two clauses pinned, one test per case, the escaped set spelled once in the entities map, UUID v7 ids, the document carry's tail from carryContent

This commit is contained in:
2026-10-28 01:44:00 +01:00
committed by Lillem4n
parent 74add5c813
commit 3366841adf
5 changed files with 75 additions and 57 deletions
+7 -4
View File
@@ -4,11 +4,14 @@ const entities: ReadonlyMap<string, string> = new Map([
['&lt;', '<'],
['&quot;', '"'],
])
const entity = new RegExp([...entities.keys()].join('|'), 'g')
const escapable = new RegExp(`[${[...entities.values()].join('')}]`, 'g')
const escapes: ReadonlyMap<string, string> = new Map([...entities].map(([spelled, character]) => [character, spelled]))
// What HTML text and attribute values cannot hold (WHATWG HTML §13.1.3): any control but a tab, line feed or form feed, a carriage return among them, since a parser reads it as a line feed; a noncharacter; and a lone surrogate, which UTF-8 cannot encode.
const unwritable = /(?![\t\n\f ])[\p{Cc}\p{Cs}\p{Noncharacter_Code_Point}]/u
const unwritables = new RegExp(unwritable.source, 'gu')
// An `&` opening no entity, a character `escapeHtml` escapes standing raw, or one HTML cannot hold.
const misspelling = new RegExp(`&(?!${[...entities.keys()].map((entity) => entity.slice(1)).join('|')})|[<>"]|${unwritable.source}`, 'u')
// A character `escapeHtml` escapes, other than the `&` opening an entity, or one HTML cannot hold.
const misspelling = new RegExp(`(?!${entity.source})${escapable.source}|${unwritable.source}`, 'u')
// The carry's JSON, which spells each character HTML cannot hold as a JSON escape reading back to the same string.
export function holdableJson(json: string): string {
@@ -37,7 +40,7 @@ export function shownValue(value: string, lineFeed: string): string {
// The text or attribute value escaped HTML spells, `undefined` where `writableValue` would not write it so.
export function unescapedValue(html: string): string | undefined {
return misspelledAt(html) < 0 ? html.replace(/&(?:amp|gt|lt|quot);/g, (entity) => entities.get(entity) ?? entity) : undefined
return misspelledAt(html) < 0 ? html.replace(entity, (spelled) => entities.get(spelled) ?? spelled) : undefined
}
// `undefined` where HTML cannot hold the value, so its node rides the carry element.
@@ -46,7 +49,7 @@ export function writableValue(value: string): string | undefined {
}
function escapeHtml(text: string): string {
return text.replaceAll('&', '&amp;').replaceAll('<', '&lt;').replaceAll('>', '&gt;').replaceAll('"', '&quot;')
return text.replace(escapable, (character) => escapes.get(character) ?? character)
}
// An astral noncharacter spells as its two surrogates' escapes.