|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const { |
| 4 | + A, |
| 5 | + Field, |
| 6 | + Flag, |
| 7 | + NodeType, |
| 8 | + Part, |
| 9 | + _attributeList, |
| 10 | + _namespaceOf, |
| 11 | + parseHtml: parseHtmlRefs |
| 12 | +} = require("../../lib/html/syntax-parser"); |
| 13 | + |
| 14 | +/** @import { HtmlNodeRef, HtmlAttribute } from "../../lib/html/syntax-parser" */ |
| 15 | + |
| 16 | +/** |
| 17 | + * Materialized plain-object views of the struct-of-arrays AST — the shape |
| 18 | + * `parseHtml` used to return, rebuilt through the accessor `A`. Shared by the |
| 19 | + * HTML tokenizer, parser and printer unit suites. |
| 20 | + * @typedef {object} MatElement |
| 21 | + * @property {typeof NodeType.Element} type |
| 22 | + * @property {string} tagName |
| 23 | + * @property {number} namespace |
| 24 | + * @property {HtmlAttribute[]} attributes |
| 25 | + * @property {MatNode[]} children |
| 26 | + * @property {boolean} selfClosing |
| 27 | + * @property {number} start |
| 28 | + * @property {number} end |
| 29 | + * @property {number} tagEnd |
| 30 | + * @property {number} nameEnd |
| 31 | + * @property {number} contentEnd |
| 32 | + * @property {MatFragment=} templateContent |
| 33 | + */ |
| 34 | +/** @typedef {{ type: typeof NodeType.Text | typeof NodeType.Comment, data: string, start: number, end: number }} MatText */ |
| 35 | +/** @typedef {MatText} MatComment */ |
| 36 | +/** @typedef {{ type: typeof NodeType.ProcessingInstruction, target: string, data: string, start: number, end: number }} MatProcessingInstruction */ |
| 37 | +/** @typedef {{ type: typeof NodeType.Doctype, name: string, publicId: string | null, systemId: string | null, start: number, end: number }} MatDoctype */ |
| 38 | +/** @typedef {{ type: typeof NodeType.Document, children: MatNode[] }} MatDocument */ |
| 39 | +/** @typedef {{ type: typeof NodeType.DocumentFragment, children: MatNode[] }} MatFragment */ |
| 40 | +/** @typedef {MatElement | MatText | MatComment | MatDoctype | MatProcessingInstruction} MatNode */ |
| 41 | + |
| 42 | +/** |
| 43 | + * @param {number} ref node |
| 44 | + * @returns {number[]} its children, read one by one through the path |
| 45 | + */ |
| 46 | +const childrenOf = (ref) => { |
| 47 | + const out = []; |
| 48 | + for (let i = 0, c = A.child(0, ref); c !== 0; c = A.child(++i, ref)) { |
| 49 | + out.push(c); |
| 50 | + } |
| 51 | + return out; |
| 52 | +}; |
| 53 | + |
| 54 | +/** |
| 55 | + * `parseHtml` hands back integer refs into reused module-level columns, valid |
| 56 | + * only until the next parse, so each tree is materialized eagerly. Every field |
| 57 | + * is read through `A`, which is how this suite reaches the whole accessor |
| 58 | + * surface. |
| 59 | + * @param {HtmlNodeRef} ref node ref |
| 60 | + * @returns {MatNode} plain-object node |
| 61 | + */ |
| 62 | +const materialize = (ref) => { |
| 63 | + const type = A.type(ref); |
| 64 | + switch (type) { |
| 65 | + case NodeType.Element: { |
| 66 | + const tc = A.field(0, Field.content, ref); |
| 67 | + return { |
| 68 | + type, |
| 69 | + tagName: A.name(ref), |
| 70 | + namespace: _namespaceOf(ref), |
| 71 | + attributes: _attributeList(ref), |
| 72 | + children: childrenOf(ref).map(materialize), |
| 73 | + selfClosing: A.flag(Flag.selfClosing, ref), |
| 74 | + start: A.start(ref), |
| 75 | + end: A.end(ref), |
| 76 | + tagEnd: A.startOf(Part.content, ref), |
| 77 | + nameEnd: A.endOf(Part.name, ref), |
| 78 | + contentEnd: A.endOf(Part.content, ref), |
| 79 | + templateContent: |
| 80 | + tc !== 0 |
| 81 | + ? { |
| 82 | + type: NodeType.DocumentFragment, |
| 83 | + children: childrenOf(tc).map(materialize) |
| 84 | + } |
| 85 | + : undefined |
| 86 | + }; |
| 87 | + } |
| 88 | + case NodeType.ProcessingInstruction: |
| 89 | + return { |
| 90 | + type, |
| 91 | + target: A.name(ref), |
| 92 | + data: A.value(ref), |
| 93 | + start: A.range(ref)[0], |
| 94 | + end: A.range(ref)[1] |
| 95 | + }; |
| 96 | + case NodeType.Doctype: |
| 97 | + return { |
| 98 | + type, |
| 99 | + name: A.name(ref), |
| 100 | + publicId: A.textOf(Part.publicId, ref), |
| 101 | + systemId: A.textOf(Part.systemId, ref), |
| 102 | + start: A.range(ref)[0], |
| 103 | + end: A.range(ref)[1] |
| 104 | + }; |
| 105 | + default: |
| 106 | + // Text / Comment |
| 107 | + return { |
| 108 | + type: /** @type {typeof NodeType.Text | typeof NodeType.Comment} */ ( |
| 109 | + type |
| 110 | + ), |
| 111 | + data: A.value(ref), |
| 112 | + start: A.range(ref)[0], |
| 113 | + end: A.range(ref)[1] |
| 114 | + }; |
| 115 | + } |
| 116 | +}; |
| 117 | + |
| 118 | +/** |
| 119 | + * @param {string} src source |
| 120 | + * @param {string=} fragmentContext fragment context |
| 121 | + * @param {import("../../lib/html/syntax-parser").HtmlAstSkip=} skip skip options |
| 122 | + * @returns {MatDocument} materialized document |
| 123 | + */ |
| 124 | +const parseHtml = (src, fragmentContext, skip) => { |
| 125 | + const doc = parseHtmlRefs(src, 0, { fragmentContext, skip }); |
| 126 | + return { |
| 127 | + type: NodeType.Document, |
| 128 | + children: childrenOf(doc).map(materialize) |
| 129 | + }; |
| 130 | +}; |
| 131 | + |
| 132 | +/** |
| 133 | + * @param {MatNode[]} children children |
| 134 | + * @param {string} tagName tag name |
| 135 | + * @returns {MatElement} the element |
| 136 | + */ |
| 137 | +const child = (children, tagName) => |
| 138 | + /** @type {MatElement} */ ( |
| 139 | + children.find((c) => c.type === NodeType.Element && c.tagName === tagName) |
| 140 | + ); |
| 141 | + |
| 142 | +// The tree builder always produces a full document (html > head, body); these |
| 143 | +// helpers reach the interesting subtrees. |
| 144 | +/** |
| 145 | + * @param {string} src source |
| 146 | + * @returns {MatElement} html element |
| 147 | + */ |
| 148 | +const html = (src) => child(parseHtml(src).children, "html"); |
| 149 | +/** |
| 150 | + * @param {string} src source |
| 151 | + * @returns {MatElement[]} body children |
| 152 | + */ |
| 153 | +const body = (src) => |
| 154 | + /** @type {MatElement[]} */ (child(html(src).children, "body").children); |
| 155 | + |
| 156 | +module.exports = { body, child, childrenOf, html, materialize, parseHtml }; |
0 commit comments