Skip to content

Commit 495bb61

Browse files
test(html): expand XML parser coverage
1 parent 42403cd commit 495bb61

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
@@ -4478,6 +4478,9 @@ const buildHeadTags = (opts) => {
44784478
const NS_HTML = 0;
44794479
const NS_MATHML = 1;
44804480
const NS_SVG = 2;
4481+
// XML elements do not participate in HTML, MathML or SVG tree construction.
4482+
// The fourth value fits in the existing two-bit namespace field.
4483+
const NS_XML = 3;
44814484

44824485
/**
44834486
* AST node `type` discriminators. Numeric for the same reason as the CSS
@@ -5360,6 +5363,7 @@ const EMPTY_SKIP = Object.freeze({});
53605363
/**
53615364
* @typedef {object} HtmlParseOptions
53625365
* @property {string=} fragmentContext context element name for fragment parsing (e.g. `td`, `svg path`); omit for a full document
5366+
* @property {boolean=} xml parse as XML instead of applying the HTML tree-construction rules
53635367
* @property {HtmlAstSkip=} skip node kinds to omit from the AST (see `HtmlAstSkip`); omit to build the full tree
53645368
*/
53655369

@@ -5649,6 +5653,7 @@ let inputHasNul = true;
56495653
let skipText = false;
56505654
let skipComments = false;
56515655
let skipDoctype = false;
5656+
let xmlMode = false;
56525657
/** @type {HtmlNodeRef} */
56535658
let doc = 0;
56545659
let mode = 0;
@@ -6324,6 +6329,50 @@ const process = (t) => {
63246329
runMode(t);
63256330
};
63266331

6332+
/**
6333+
* XML has no insertion modes: elements are appended to the current element
6334+
* and an end tag closes the element with the same case-sensitive name.
6335+
* @param {Token} t token to process
6336+
* @returns {void}
6337+
*/
6338+
const processXml = (t) => {
6339+
const ty = t.type;
6340+
if (ty === TOKEN_START_TAG || ty === TOKEN_END_TAG) tokenEnd = t.pos.end;
6341+
else if (ty !== TOKEN_EOF) tokenEnd = t.end;
6342+
if (ty === TOKEN_CHAR) {
6343+
if (open.length === 0) return;
6344+
insertCharacters(t.data, t.start, t.end);
6345+
return;
6346+
}
6347+
if (isCommentOrProcessingInstruction(t)) {
6348+
insertCommentOrProcessingInstruction(
6349+
t,
6350+
open.length === 0 ? { parent: doc, beforeNode: 0 } : undefined
6351+
);
6352+
return;
6353+
}
6354+
if (ty === TOKEN_DOCTYPE) {
6355+
if (!skipDoctype) {
6356+
const dt = _allocNode(NodeType.Doctype, t.start, t.end);
6357+
_nodeStrings[dt] = t.name;
6358+
_doctypePublicId = t.publicId;
6359+
_doctypeSystemId = t.systemId;
6360+
_appendChild(doc, dt);
6361+
}
6362+
return;
6363+
}
6364+
if (ty === TOKEN_START_TAG) {
6365+
const el = mkEl(t.name, NS_XML, t.attrs, t.pos);
6366+
if (t.selfClosing) _S._nodeFlags[el] |= FLAG_SELF_CLOSING;
6367+
appendTo(open.length === 0 ? doc : cur(), el);
6368+
if (!t.selfClosing) open.push(el);
6369+
return;
6370+
}
6371+
if (ty === TOKEN_END_TAG && open.length > 0 && _tagNameOf(cur()) === t.name) {
6372+
_nodeEnds[popOpen()] = t.pos.end;
6373+
}
6374+
};
6375+
63276376
/**
63286377
* @param {HtmlElement} ac adjusted current node
63296378
* @param {Token} t token to process
@@ -7979,7 +8028,9 @@ modes.afterAfterFrameset = (t) => {
79798028
};
79808029

79818030
const dispatch = () => {
7982-
process(/** @type {Token} */ (/** @type {unknown} */ (tok)));
8031+
const token = /** @type {Token} */ (/** @type {unknown} */ (tok));
8032+
if (xmlMode) processXml(token);
8033+
else process(token);
79838034
// One boolean per token: only a stack change can complete a subtree.
79848035
if (_streamEligible && (_openStackChanged || _openSpliced)) {
79858036
_openStackChanged = false;
@@ -8004,6 +8055,7 @@ const dispatch = () => {
80048055

80058056
/** @type {() => boolean} */
80068057
const currentNodeIsForeign = () => {
8058+
if (xmlMode) return true;
80078059
const adjustedCurrentNode = adjustedCurrent();
80088060
return (
80098061
open.length > 0 &&
@@ -8019,6 +8071,7 @@ const currentNodeIsForeign = () => {
80198071
* @returns {boolean} true when the tokenizer stays in the data state
80208072
*/
80218073
const vetoesContentMode = (name) => {
8074+
if (xmlMode) return true;
80228075
if (currentNodeIsForeign()) return true;
80238076
return (
80248077
name !== "noframes" &&
@@ -8065,7 +8118,9 @@ const handleDoctypeToken = (
80658118
tok.name =
80668119
nameStart === -1
80678120
? ""
8068-
: _asciiLowerCase(doctypePart(input, nameStart, nameEnd));
8121+
: xmlMode
8122+
? doctypePart(input, nameStart, nameEnd)
8123+
: _asciiLowerCase(doctypePart(input, nameStart, nameEnd));
80698124
tok.publicId =
80708125
publicStart === -1 ? null : doctypePart(input, publicStart, publicEnd);
80718126
tok.systemId =
@@ -8124,8 +8179,8 @@ const _scanProcessingInstruction = (input, start, end) => {
81248179
if (i === end) return PI_SCAN_DROP;
81258180
// Reserved for XML, and common enough in legacy pages to keep as comments.
81268181
if (
8127-
rangeEqualsLowerCase(input, targetStart, i, "xml") ||
8128-
rangeEqualsLowerCase(input, targetStart, i, "xml-stylesheet")
8182+
(!xmlMode && rangeEqualsLowerCase(input, targetStart, i, "xml")) ||
8183+
(!xmlMode && rangeEqualsLowerCase(input, targetStart, i, "xml-stylesheet"))
81298184
) {
81308185
return PI_SCAN_COMMENT;
81318186
}
@@ -8152,9 +8207,9 @@ const handleCommentToken = (input, start, end, dataStart, dataEnd) => {
81528207
// character data, not a comment.
81538208
if (
81548209
input.startsWith("<![CDATA[", start) &&
8155-
open.length > 0 &&
8210+
(xmlMode || open.length > 0) &&
81568211
adjustedCurrent() &&
8157-
_namespaceOf(adjustedCurrent()) !== NS_HTML
8212+
(xmlMode || _namespaceOf(adjustedCurrent()) !== NS_HTML)
81588213
) {
81598214
const data = input.slice(dataStart, dataEnd).replace(/\r\n?/g, "\n");
81608215
if (data !== "") {
@@ -8253,7 +8308,9 @@ const handleAttributeToken = (
82538308
valueEnd,
82548309
quoteType
82558310
) => {
8256-
const name = internLowerName(ATTR_NAME_INTERN, input, nameStart, nameEnd);
8311+
const name = xmlMode
8312+
? input.slice(nameStart, nameEnd)
8313+
: internLowerName(ATTR_NAME_INTERN, input, nameStart, nameEnd);
82578314
if (pendAttrStart === 0) {
82588315
pendAttrStart = _attrCount + 1;
82598316
pendAttrDropped = false;
@@ -8304,7 +8361,9 @@ const handleStartTagToken = (
83048361
pendAttrDropped = false;
83058362
return end;
83068363
}
8307-
const name = internLowerName(TAG_NAME_INTERN, input, nameStart, nameEnd);
8364+
const name = xmlMode
8365+
? input.slice(nameStart, nameEnd)
8366+
: internLowerName(TAG_NAME_INTERN, input, nameStart, nameEnd);
83088367
if (name === "selectedcontent") sawSelectedContent = true;
83098368
tok.type = TOKEN_START_TAG;
83108369
tok.name = name;
@@ -8344,12 +8403,18 @@ const handleEndTagToken = (input, start, end, nameStart, nameEnd) => {
83448403
let name;
83458404
if (open.length > 0) {
83468405
const topName = _nodeStrings[open[open.length - 1]];
8347-
if (rangeEqualsLowerCase(input, nameStart, nameEnd, topName)) {
8406+
if (
8407+
xmlMode
8408+
? input.slice(nameStart, nameEnd) === topName
8409+
: rangeEqualsLowerCase(input, nameStart, nameEnd, topName)
8410+
) {
83488411
name = topName;
83498412
}
83508413
}
83518414
if (name === undefined) {
8352-
name = internLowerName(TAG_NAME_INTERN, input, nameStart, nameEnd);
8415+
name = xmlMode
8416+
? input.slice(nameStart, nameEnd)
8417+
: internLowerName(TAG_NAME_INTERN, input, nameStart, nameEnd);
83538418
}
83548419
// End tags drop any parsed attributes (the slots are orphaned).
83558420
pendAttrStart = 0;
@@ -8394,6 +8459,7 @@ const parseHtml = (input, pos = 0, options = {}) => {
83948459
_dropSourceBackedNames(TAG_NAME_INTERN);
83958460
_dropSourceBackedNames(ATTR_NAME_INTERN);
83968461
const { fragmentContext, skip = EMPTY_SKIP } = options;
8462+
xmlMode = options.xml === true;
83978463
sourceShellEnds = 0;
83988464
skipText = skip.text === true;
83998465
skipComments = skip.comments === true;
@@ -13143,6 +13209,7 @@ module.exports.JSON_TYPE = JSON_TYPE;
1314313209
module.exports.NS_HTML = NS_HTML;
1314413210
module.exports.NS_MATHML = NS_MATHML;
1314513211
module.exports.NS_SVG = NS_SVG;
13212+
module.exports.NS_XML = NS_XML;
1314613213
module.exports.NodeType = NodeType;
1314713214
module.exports.QUOTE_DOUBLE = QUOTE_DOUBLE;
1314813215
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
@@ -114,6 +114,7 @@ const CC_RIGHT_CURLY_BRACKET = 0x7d;
114114
/**
115115
* @typedef {object} HtmlProcessOptions
116116
* @property {string=} fragmentContext context element tag name for fragment parsing (see `parseHtml`); the HTML analog of the CSS parser's `as` parse-mode option
117+
* @property {boolean=} xml parse as XML instead of applying the HTML tree-construction rules
117118
* @property {HtmlAstSkip=} skip node kinds to omit from the AST for speed/memory (see `HtmlAstSkip`)
118119
* @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 `""`)
119120
* @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)