File size: 2,959 Bytes
dbb1bf9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | /**
* Shared single-pass HTML/XML entity decoder for the client SPA.
*
* Why single-pass: sequential `.replace(/&/g, '&')` chains decode TWO
* levels when `&` runs before the other replaces β `<` becomes
* `<` in one call, turning escaped text into live markup. One regex pass
* over an alternation decodes exactly one level for every input.
*
* `String.fromCodePoint` throws `RangeError` on anything outside the Unicode
* range, which would turn one malformed numeric reference (`�`)
* into a crashed render. Out-of-range references are preserved instead.
* `fromCharCode` is not usable here: it truncates to 16 bits, so `😀`
* would decode to U+F600 (a private-use glyph) rather than π.
*
* Mirrors `scripts/_html-entities.mjs` (kept separate because seed scripts
* cannot be imported from `src/`).
*/
/**
* Returns null for anything that is not a Unicode scalar value: out-of-range
* numbers throw RangeError in fromCodePoint, and surrogates (0xD800-0xDFFF)
* would otherwise pass through as lone surrogates into published text.
*/
function decodeNumericReference(codePoint: number): string | null {
return Number.isInteger(codePoint) && codePoint >= 0 && codePoint <= 0x10ffff
&& !(codePoint >= 0xd800 && codePoint <= 0xdfff)
? String.fromCodePoint(codePoint)
: null;
}
// Named entities the decoders historically handled. `nbsp` maps to a plain
// space (matching every prior decoder); curly quotes map to their correct
// Unicode code points.
const NAMED_ENTITIES: Record<string, string> = {
amp: '&',
lt: '<',
gt: '>',
quot: '"',
apos: "'",
nbsp: ' ',
hellip: 'β¦',
mdash: 'β',
ndash: 'β',
lsquo: 'β',
rsquo: 'β',
ldquo: 'β',
rdquo: 'β',
};
const ENTITY_RE = /&(?:#x([0-9a-f]+)|#(\d+)|([a-z][a-z0-9]*));/gi;
/**
* Decode exactly one level of HTML/XML entities.
*
* `unknownEntity` controls unrecognized entities AND invalid numeric
* references: `keep` (default) leaves unknown entities and invalid references
* untouched; `blank` replaces both with a single space (a space keeps
* adjacent digits from welding into one number, e.g. `100�200`).
*/
export function decodeHtmlEntities(
text: unknown,
{ unknownEntity = 'keep' }: { unknownEntity?: 'keep' | 'blank' } = {},
): string {
return String(text ?? '').replace(ENTITY_RE, (match, hex, dec, name) => {
if (hex !== undefined || dec !== undefined) {
const decoded = decodeNumericReference(hex !== undefined ? parseInt(hex as string, 16) : Number(dec));
// Preserve invalid references by default; 'blank' intentionally replaces
// them with a separator so adjacent identifier segments cannot weld.
return decoded ?? (unknownEntity === 'blank' ? ' ' : match);
}
const value = NAMED_ENTITIES[(name as string).toLowerCase()];
if (value !== undefined) return value;
return unknownEntity === 'blank' ? ' ' : match;
});
}
|