HTML5 entities decoder — strict and lenient, WHATWG-compliant.
Module htmlEntities | Source packages/front/fw/src/io/text/html-entities.js | Deps none | Worker-safe yes
Resolve
const ent = runtime.resolve('htmlEntities');
// Returns: { decodeHtmlStrict, decodeHtml, decodeCodePoint, HTML5_ENTITIES }
API
| Method | Signature | Returns |
|---|---|---|
decodeHtmlStrict |
(s: string) => string |
Decodes all HTML entity references (semicolon-terminated only). Unknown entities left as-is. |
decodeHtml |
(s: string) => string |
Decodes HTML entities in lenient mode (semicolon optional). |
decodeCodePoint |
(cp: number) => string |
Decodes a numeric code point to a string. Returns U+FFFD for forbidden values. |
HTML5_ENTITIES |
Record<string, string> |
Full WHATWG table — 2,125 semicolon-terminated entries. |
Examples
const ent = runtime.resolve('htmlEntities');
// Named entities
ent.decodeHtmlStrict('&'); // → '&'
ent.decodeHtmlStrict('á'); // → 'á'
ent.decodeHtmlStrict('Α'); // → 'Α'
ent.decodeHtmlStrict('&unknown;'); // → '&unknown;' (unchanged)
// Numeric
ent.decodeHtmlStrict('*'); // → '*'
ent.decodeHtmlStrict('*'); // → '*'
ent.decodeHtmlStrict('�'); // → '�' (U+FFFD)
ent.decodeHtmlStrict('€'); // → '€' (Windows-1252 override)
// Lenient (without semicolon)
ent.decodeHtml('&'); // → '&'
// Low level
ent.decodeCodePoint(42); // → '*'
ent.decodeCodePoint(0xD800); // → '�' (surrogate)
Worker Usage
const worker = fw.createWorker(
function ({ libs, args }) {
const ent = libs.htmlEntities;
self.postMessage(ent.decodeHtmlStrict(args[0]));
},
{ dependencies: ['htmlEntities'], args: ['<p>Hello</p>'] }
);
Notes
- Implements CommonMark §6.2: surrogates (
0xD800–0xDFFF),�, and code points >0x10FFFFare all replaced by U+FFFD. - Applies the 27 Windows-1252 remappings defined by the HTML5 spec (e.g.
€→€). - The WHATWG table (2,125 entries) is loaded once at module level and shared across all factory instances — no memory duplication.
- Strict mode only: semicolon-less forms like
&are not decoded bydecodeHtmlStrict(usedecodeHtmlfor lenient mode).