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

Read adfToHtml's HTML in htmlToAdf as a browser serializes it too

This commit is contained in:
2026-10-31 01:48:10 +01:00
committed by Lillem4n
parent 088494511d
commit 6cc59f8574
13 changed files with 199 additions and 57 deletions
+48 -12
View File
@@ -1,17 +1,35 @@
const entities: ReadonlyMap<string, string> = new Map([
export type Spelling = 'attribute' | 'text'
const escapable = /[&<>"]/g
const escapes: ReadonlyMap<string, string> = new Map([
['"', '&quot;'],
['&', '&amp;'],
['<', '&lt;'],
['>', '&gt;'],
])
const writtenEntities = [...escapes.values()]
const entity = /&(?:amp|gt|lt|nbsp|quot);/g
const entityValues: ReadonlyMap<string, string> = new Map([
['&amp;', '&'],
['&gt;', '>'],
['&lt;', '<'],
['&nbsp;', '\u00a0'],
['&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')
// 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')
const strayAmpersand = '&(?!(?:amp|gt|lt|nbsp|quot);)'
// What reads wrongly in a value: `&` outside an entity, a character HTML cannot hold, and in text a `<` or `>`. An attribute value may hold `<` and `>` raw, as older browsers write it, and text may hold `"`.
const misspellings: Readonly<Record<Spelling, RegExp>> = {
attribute: new RegExp(`${strayAmpersand}|${unwritable.source}`, 'u'),
text: new RegExp(`${strayAmpersand}|[<>]|${unwritable.source}`, 'u'),
}
// What `adfToHtml` spells with an entity, a browser (WHATWG HTML §13.3) spells raw in text, or in an attribute; a no-break space it spells `&nbsp;` in both.
const browserSpellings: Readonly<Record<Spelling, ReadonlyMap<string, string>>> = {
attribute: new Map([['&gt;', '>'], ['&lt;', '<'], ['\u00a0', '&nbsp;']]),
text: new Map([['&quot;', '"'], ['\u00a0', '&nbsp;']]),
}
// 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 {
@@ -22,9 +40,9 @@ export function jsonValue(json: string): string {
return escapeHtml(holdableJson(json))
}
// The index of the first character `writableValue` would not write so, -1 where none stands in `html`.
export function misspelledAt(html: string): number {
return html.search(misspelling)
// The index of the first character that reads wrongly in `html`, -1 where none stands in it.
export function misspelledAt(html: string, spelling: Spelling): number {
return html.search(misspellings[spelling])
}
// ` name="value"`, `''` where the value is undefined, `undefined` where HTML cannot hold it.
@@ -38,9 +56,27 @@ export function shownValue(value: string, lineFeed: string): string {
return escapeHtml(value.replace(unwritables, '')).replaceAll('\n', lineFeed)
}
// The value an escaped HTML text or attribute stands for, `undefined` where `writableValue` would not write it so.
export function unescapedValue(html: string): string | undefined {
return misspelledAt(html) < 0 ? html.replace(entity, (spelled) => entities.get(spelled) ?? spelled) : undefined
// Where `html` reads as `shown` from `index`: `whole`, and the index after it, or where it first reads otherwise. `shown` is spelled as `adfToHtml` writes it, `html` as a browser may too.
export function readsAsShown(html: string, index: number, shown: string): { at: number; whole: boolean } {
if (html.startsWith(shown, index)) return { at: index + shown.length, whole: true }
let at = index
let spelling: Spelling = 'text'
for (let next = 0; next < shown.length; ) {
const token = writtenEntities.find((spelled) => shown.startsWith(spelled, next)) ?? shown.charAt(next)
// Only a tag holds a raw `<` or `>`, and only its attribute values an escape.
if (token === '<') spelling = 'attribute'
if (token === '>') spelling = 'text'
const read = [token, browserSpellings[spelling].get(token)].find((spelled) => spelled !== undefined && html.startsWith(spelled, at))
if (read === undefined) return { at, whole: false }
at += read.length
next += token.length
}
return { at, whole: true }
}
// The value an escaped HTML text or attribute stands for, `undefined` where `misspelledAt` finds a character that reads wrongly.
export function unescapedValue(html: string, spelling: Spelling): string | undefined {
return misspelledAt(html, spelling) < 0 ? html.replace(entity, (spelled) => entityValues.get(spelled) ?? spelled) : undefined
}
// `undefined` where HTML cannot hold the value, so its node rides the carry element.