{"version":3,"file":"htmlparser2.mjs","names":[],"sources":["../../src/main/htmlparser2.ts"],"sourcesContent":["/**\n * Optional parse adapter backed by htmlparser2 (fast and lenient, the parser\n * sanitize-html uses). htmlparser2 is an optional peer dependency, imported only\n * when you import this module.\n *\n * Not a full WHATWG tree builder: no foster-parenting, no adoption agency, no\n * foreign-content namespacing. Every element is treated as HTML. Fine for\n * sanitization, but for browser fidelity use the default parser and for strict\n * conformance use neosanitize/parse5.\n */\nimport { parseDocument } from 'htmlparser2';\nimport type { ParseAdapter, ParentNode, ElementNode, TreeNode } from './core';\n\ninterface H2Node {\n  type: string;\n  name?: string;\n  data?: string;\n  attribs?: Record<string, string>;\n  children?: H2Node[];\n}\n\nexport const htmlparser2Adapter: ParseAdapter = (html) => {\n  const doc = parseDocument(html) as unknown as H2Node;\n  const root: ParentNode = { type: 'document', children: [] };\n  for (const child of doc.children ?? []) {\n    const n = to_node(child);\n    if (n) root.children.push(n);\n  }\n  return root;\n};\n\nfunction to_node(h: H2Node): TreeNode | null {\n  switch (h.type) {\n    case 'text':\n      return { type: 'text', value: h.data ?? '', parent: null };\n    case 'comment':\n      return { type: 'comment', value: h.data ?? '', parent: null };\n    case 'tag':\n    case 'script':\n    case 'style':\n      return to_element(h);\n    default:\n      return null; // directives, doctype, cdata: dropped by the serializer anyway\n  }\n}\n\nfunction to_element(h: H2Node): ElementNode {\n  const attrs: Array<[string, string]> = [];\n  const a = h.attribs ?? {};\n  for (const k of Object.keys(a)) attrs.push([k, a[k]]);\n  const children: TreeNode[] = [];\n  for (const c of h.children ?? []) {\n    const n = to_node(c);\n    if (n) children.push(n);\n  }\n  return { type: 'element', name: h.name ?? '', namespace: 'html', attrs, children, parent: null };\n}\n"],"mappings":";;;;;;;;;;;;AAqBA,MAAa,sBAAoC,SAAS;CACxD,MAAM,MAAM,cAAc,IAAI;CAC9B,MAAM,OAAmB;EAAE,MAAM;EAAY,UAAU,CAAC;CAAE;CAC1D,KAAK,MAAM,SAAS,IAAI,YAAY,CAAC,GAAG;EACtC,MAAM,IAAI,QAAQ,KAAK;EACvB,IAAI,GAAG,KAAK,SAAS,KAAK,CAAC;CAC7B;CACA,OAAO;AACT;AAEA,SAAS,QAAQ,GAA4B;CAC3C,QAAQ,EAAE,MAAV;EACE,KAAK,QACH,OAAO;GAAE,MAAM;GAAQ,OAAO,EAAE,QAAQ;GAAI,QAAQ;EAAK;EAC3D,KAAK,WACH,OAAO;GAAE,MAAM;GAAW,OAAO,EAAE,QAAQ;GAAI,QAAQ;EAAK;EAC9D,KAAK;EACL,KAAK;EACL,KAAK,SACH,OAAO,WAAW,CAAC;EACrB,SACE,OAAO;CACX;AACF;AAEA,SAAS,WAAW,GAAwB;CAC1C,MAAM,QAAiC,CAAC;CACxC,MAAM,IAAI,EAAE,WAAW,CAAC;CACxB,KAAK,MAAM,KAAK,OAAO,KAAK,CAAC,GAAG,MAAM,KAAK,CAAC,GAAG,EAAE,EAAE,CAAC;CACpD,MAAM,WAAuB,CAAC;CAC9B,KAAK,MAAM,KAAK,EAAE,YAAY,CAAC,GAAG;EAChC,MAAM,IAAI,QAAQ,CAAC;EACnB,IAAI,GAAG,SAAS,KAAK,CAAC;CACxB;CACA,OAAO;EAAE,MAAM;EAAW,MAAM,EAAE,QAAQ;EAAI,WAAW;EAAQ;EAAO;EAAU,QAAQ;CAAK;AACjG"}