Skip to content

Commit 0e6501a

Browse files
test(html): expand XML parser coverage
1 parent d2abe4d commit 0e6501a

4 files changed

Lines changed: 277 additions & 15 deletions

File tree

‎lib/html/syntax-parser.js‎

Lines changed: 77 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4501,6 +4501,9 @@ const buildHeadTags = (opts) => {
45014501
const NS_HTML = 0;
45024502
const NS_MATHML = 1;
45034503
const NS_SVG = 2;
4504+
// XML elements do not participate in HTML, MathML or SVG tree construction.
4505+
// The fourth value fits in the existing two-bit namespace field.
4506+
const NS_XML = 3;
45044507

45054508
/**
45064509
* AST node `type` discriminators. Numeric for the same reason as the CSS
@@ -5390,6 +5393,7 @@ const EMPTY_SKIP = Object.freeze({});
53905393
/**
53915394
* @typedef {object} HtmlParseOptions
53925395
* @property {string=} fragmentContext context element name for fragment parsing (e.g. `td`, `svg path`); omit for a full document
5396+
* @property {boolean=} xml parse as XML instead of applying the HTML tree-construction rules
53935397
* @property {HtmlAstSkip=} skip node kinds to omit from the AST (see `HtmlAstSkip`); omit to build the full tree
53945398
* @property {boolean=} scripting parse as a browser with scripting on does, where a `noscript` holds raw text; off by default, so what it holds is read as markup
53955399
*/
@@ -5685,6 +5689,7 @@ let inputHasNul = true;
56855689
let skipText = false;
56865690
let skipComments = false;
56875691
let skipDoctype = false;
5692+
let xmlMode = false;
56885693
/** @type {HtmlNodeRef} */
56895694
let doc = 0;
56905695
let mode = 0;
@@ -6362,6 +6367,50 @@ const process = (t) => {
63626367
runMode(t);
63636368
};
63646369

6370+
/**
6371+
* XML has no insertion modes: elements are appended to the current element
6372+
* and an end tag closes the element with the same case-sensitive name.
6373+
* @param {Token} t token to process
6374+
* @returns {void}
6375+
*/
6376+
const processXml = (t) => {
6377+
const ty = t.type;
6378+
if (ty === TOKEN_START_TAG || ty === TOKEN_END_TAG) tokenEnd = t.pos.end;
6379+
else if (ty !== TOKEN_EOF) tokenEnd = t.end;
6380+
if (ty === TOKEN_CHAR) {
6381+
if (open.length === 0) return;
6382+
insertCharacters(t.data, t.start, t.end);
6383+
return;
6384+
}
6385+
if (isCommentOrProcessingInstruction(t)) {
6386+
insertCommentOrProcessingInstruction(
6387+
t,
6388+
open.length === 0 ? { parent: doc, beforeNode: 0 } : undefined
6389+
);
6390+
return;
6391+
}
6392+
if (ty === TOKEN_DOCTYPE) {
6393+
if (!skipDoctype) {
6394+
const dt = _allocNode(NodeType.Doctype, t.start, t.end);
6395+
_nodeStrings[dt] = t.name;
6396+
_doctypePublicId = t.publicId;
6397+
_doctypeSystemId = t.systemId;
6398+
_appendChild(doc, dt);
6399+
}
6400+
return;
6401+
}
6402+
if (ty === TOKEN_START_TAG) {
6403+
const el = mkEl(t.name, NS_XML, t.attrs, t.pos);
6404+
if (t.selfClosing) _S._nodeFlags[el] |= FLAG_SELF_CLOSING;
6405+
appendTo(open.length === 0 ? doc : cur(), el);
6406+
if (!t.selfClosing) open.push(el);
6407+
return;
6408+
}
6409+
if (ty === TOKEN_END_TAG && open.length > 0 && _tagNameOf(cur()) === t.name) {
6410+
_nodeEnds[popOpen()] = t.pos.end;
6411+
}
6412+
};
6413+
63656414
/**
63666415
* @param {HtmlElement} ac adjusted current node
63676416
* @param {Token} t token to process
@@ -8017,7 +8066,9 @@ modes.afterAfterFrameset = (t) => {
80178066
};
80188067

80198068
const dispatch = () => {
8020-
process(/** @type {Token} */ (/** @type {unknown} */ (tok)));
8069+
const token = /** @type {Token} */ (/** @type {unknown} */ (tok));
8070+
if (xmlMode) processXml(token);
8071+
else process(token);
80218072
// One boolean per token: only a stack change can complete a subtree.
80228073
if (_streamEligible && (_openStackChanged || _openSpliced)) {
80238074
_openStackChanged = false;
@@ -8042,6 +8093,7 @@ const dispatch = () => {
80428093

80438094
/** @type {() => boolean} */
80448095
const currentNodeIsForeign = () => {
8096+
if (xmlMode) return true;
80458097
const adjustedCurrentNode = adjustedCurrent();
80468098
return (
80478099
open.length > 0 &&
@@ -8057,6 +8109,7 @@ const currentNodeIsForeign = () => {
80578109
* @returns {boolean} true when the tokenizer stays in the data state
80588110
*/
80598111
const vetoesContentMode = (name) => {
8112+
if (xmlMode) return true;
80608113
if (currentNodeIsForeign()) return true;
80618114
return (
80628115
name !== "noframes" &&
@@ -8103,7 +8156,9 @@ const handleDoctypeToken = (
81038156
tok.name =
81048157
nameStart === -1
81058158
? ""
8106-
: _asciiLowerCase(doctypePart(input, nameStart, nameEnd));
8159+
: xmlMode
8160+
? doctypePart(input, nameStart, nameEnd)
8161+
: _asciiLowerCase(doctypePart(input, nameStart, nameEnd));
81078162
tok.publicId =
81088163
publicStart === -1 ? null : doctypePart(input, publicStart, publicEnd);
81098164
tok.systemId =
@@ -8162,8 +8217,8 @@ const _scanProcessingInstruction = (input, start, end) => {
81628217
if (i === end) return PI_SCAN_DROP;
81638218
// Reserved for XML, and common enough in legacy pages to keep as comments.
81648219
if (
8165-
rangeEqualsLowerCase(input, targetStart, i, "xml") ||
8166-
rangeEqualsLowerCase(input, targetStart, i, "xml-stylesheet")
8220+
(!xmlMode && rangeEqualsLowerCase(input, targetStart, i, "xml")) ||
8221+
(!xmlMode && rangeEqualsLowerCase(input, targetStart, i, "xml-stylesheet"))
81678222
) {
81688223
return PI_SCAN_COMMENT;
81698224
}
@@ -8190,9 +8245,9 @@ const handleCommentToken = (input, start, end, dataStart, dataEnd) => {
81908245
// character data, not a comment.
81918246
if (
81928247
input.startsWith("<![CDATA[", start) &&
8193-
open.length > 0 &&
8248+
(xmlMode || open.length > 0) &&
81948249
adjustedCurrent() &&
8195-
_namespaceOf(adjustedCurrent()) !== NS_HTML
8250+
(xmlMode || _namespaceOf(adjustedCurrent()) !== NS_HTML)
81968251
) {
81978252
const data = input.slice(dataStart, dataEnd).replace(/\r\n?/g, "\n");
81988253
if (data !== "") {
@@ -8292,7 +8347,9 @@ const handleAttributeToken = (
82928347
valueEnd,
82938348
quoteType
82948349
) => {
8295-
const name = internLowerName(ATTR_NAME_INTERN, input, nameStart, nameEnd);
8350+
const name = xmlMode
8351+
? input.slice(nameStart, nameEnd)
8352+
: internLowerName(ATTR_NAME_INTERN, input, nameStart, nameEnd);
82968353
if (pendAttrStart === 0) {
82978354
pendAttrStart = _attrCount + 1;
82988355
pendAttrDropped = false;
@@ -8343,7 +8400,9 @@ const handleStartTagToken = (
83438400
pendAttrDropped = false;
83448401
return end;
83458402
}
8346-
const name = internLowerName(TAG_NAME_INTERN, input, nameStart, nameEnd);
8403+
const name = xmlMode
8404+
? input.slice(nameStart, nameEnd)
8405+
: internLowerName(TAG_NAME_INTERN, input, nameStart, nameEnd);
83478406
if (name === "selectedcontent") sawSelectedContent = true;
83488407
tok.type = TOKEN_START_TAG;
83498408
tok.name = name;
@@ -8391,12 +8450,18 @@ const handleEndTagToken = (input, start, end, nameStart, nameEnd) => {
83918450
let name;
83928451
if (open.length > 0) {
83938452
const topName = _nodeStrings[open[open.length - 1]];
8394-
if (rangeEqualsLowerCase(input, nameStart, nameEnd, topName)) {
8453+
if (
8454+
xmlMode
8455+
? input.slice(nameStart, nameEnd) === topName
8456+
: rangeEqualsLowerCase(input, nameStart, nameEnd, topName)
8457+
) {
83958458
name = topName;
83968459
}
83978460
}
83988461
if (name === undefined) {
8399-
name = internLowerName(TAG_NAME_INTERN, input, nameStart, nameEnd);
8462+
name = xmlMode
8463+
? input.slice(nameStart, nameEnd)
8464+
: internLowerName(TAG_NAME_INTERN, input, nameStart, nameEnd);
84008465
}
84018466
// End tags drop any parsed attributes (the slots are orphaned).
84028467
pendAttrStart = 0;
@@ -8443,6 +8508,7 @@ const parseHtml = (input, pos = 0, options = {}) => {
84438508
_dropSourceBackedNames(ATTR_NAME_INTERN);
84448509
const { fragmentContext, skip = EMPTY_SKIP } = options;
84458510
scripting = options.scripting === true;
8511+
xmlMode = options.xml === true;
84468512
sourceShellEnds = 0;
84478513
skipText = skip.text === true;
84488514
skipComments = skip.comments === true;
@@ -13978,6 +14044,7 @@ module.exports.JSON_TYPE = JSON_TYPE;
1397814044
module.exports.NS_HTML = NS_HTML;
1397914045
module.exports.NS_MATHML = NS_MATHML;
1398014046
module.exports.NS_SVG = NS_SVG;
14047+
module.exports.NS_XML = NS_XML;
1398114048
module.exports.NodeType = NodeType;
1398214049
module.exports.QUOTE_DOUBLE = QUOTE_DOUBLE;
1398314050
module.exports.QUOTE_NONE = QUOTE_NONE;

‎lib/html/syntax-printer.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ const CLASSIC_SCRIPT = "script";
109109
/**
110110
* @typedef {object} HtmlProcessOptions
111111
* @property {string=} fragmentContext context element tag name for fragment parsing (see `parseHtml`); the HTML analog of the CSS parser's `as` parse-mode option
112+
* @property {boolean=} xml parse as XML instead of applying the HTML tree-construction rules
112113
* @property {HtmlAstSkip=} skip node kinds to omit from the AST for speed/memory (see `HtmlAstSkip`)
113114
* @property {boolean=} minimize print the safely-minified serialization (nodes rebuilt from source, inert comments dropped, opening-tag whitespace collapsed) as `process` walks, and return it (default false = walk only, return `""`)
114115
* @property {HtmlTransformOptions=} transforms which of the meaning-preserving rewrites the minifying print makes; each is on unless it is `false`

0 commit comments

Comments
 (0)