// The WHATWG HTML grammar is shared mechanics; what a pack keeps of a document is the pack's own policy. // The exact optional dependency belongs to this kernel, so Node and browser bundles resolve one parser. // Source: parse5 7.3.0 (parseFragment, serialize, defaultTreeAdapter). // // A fragment is parsed in an HTML `` context. Elements keep their namespace (HTML, SVG or MathML) and attributes // their prefix and namespace, and serialization reproduces what the tree holds: a foreign subtree survives parsing, and // a pack's policy may drop it. export type HtmlAttribute = { name: string; value: string; prefix?: string; namespace?: string }; export type HtmlElement = { type: 'element'; name: string; namespace: string; attributes: HtmlAttribute[]; children: HtmlNode[] }; export type HtmlText = { type: 'text'; value: string }; export type HtmlNode = HtmlElement | HtmlText; export class HtmlError extends Error { constructor(readonly code: 'parser-unavailable') { super('HTML parser parse5 7.3.0 is unavailable'); this.name = 'HtmlError'; } } type Grammar = typeof import('parse5'); type Tree = import('parse5').DefaultTreeAdapterMap; let grammar: Promise | undefined; function loadGrammar(): Promise { return grammar ??= import('parse5').catch(() => { grammar = undefined; throw new HtmlError('parser-unavailable'); }); } /** * A fragment parsed as WHATWG fragment parsing does in an HTML `` context: elements and text, comments dropped, * an HTML `