Skip to content

Commit a3d0216

Browse files
fix(html): isolate XML-specific parsing behavior
1 parent 0e6501a commit a3d0216

2 files changed

Lines changed: 81 additions & 3 deletions

File tree

‎lib/html/syntax-parser.js‎

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4749,7 +4749,10 @@ const _attrValueOf = (i) => {
47494749
* @param {number} i the attribute
47504750
* @returns {string} the value with its character references resolved
47514751
*/
4752-
const _attrValueDecoded = (i) => decodeEntities(_attrValueOf(i), true);
4752+
const _attrValueDecoded = (i) =>
4753+
xmlMode
4754+
? decodeXmlEntities(_attrValueOf(i))
4755+
: decodeEntities(_attrValueOf(i), true);
47534756

47544757
// Linear name lookup in a run — attribute lists are short, a loop beats a Map.
47554758
/** @type {(start: number, count: number, name: string) => number} */
@@ -5732,6 +5735,51 @@ const SHELL_END_BODY = 4;
57325735
let eofInTag = false;
57335736
let sawSelectedContent = false;
57345737
const decode = decodeEntities;
5738+
5739+
const XML_NAMED_REFERENCES = {
5740+
amp: "&",
5741+
apos: "'",
5742+
gt: ">",
5743+
lt: "<",
5744+
quot: '"'
5745+
};
5746+
5747+
/**
5748+
* @param {number} code Unicode code point
5749+
* @returns {boolean} whether XML 1.0 permits the character
5750+
*/
5751+
const isXmlChar = (code) =>
5752+
code === CC_TAB ||
5753+
code === CC_LF ||
5754+
code === 0x0d ||
5755+
(code >= 0x20 && code <= 0xd7ff) ||
5756+
(code >= 0xe000 && code <= 0xfffd) ||
5757+
(code >= 0x10000 && code <= 0x10ffff);
5758+
5759+
/**
5760+
* Decode the five predefined XML entities and numeric character references.
5761+
* Unlike HTML, XML has no built-in entity table beyond those five names.
5762+
* @param {string} value source text
5763+
* @returns {string} decoded text
5764+
*/
5765+
const decodeXmlEntities = (value) =>
5766+
value.replace(
5767+
/&(?:#(?:x([\da-f]+)|(\d+))|([a-z]+));/gi,
5768+
(match, hex, decimal, name) => {
5769+
if (name !== undefined) {
5770+
return Object.prototype.hasOwnProperty.call(XML_NAMED_REFERENCES, name)
5771+
? XML_NAMED_REFERENCES[
5772+
/** @type {keyof typeof XML_NAMED_REFERENCES} */ (name)
5773+
]
5774+
: match;
5775+
}
5776+
const code = Number.parseInt(
5777+
hex === undefined ? decimal : hex,
5778+
hex ? 16 : 10
5779+
);
5780+
return isXmlChar(code) ? String.fromCodePoint(code) : "\uFFFD";
5781+
}
5782+
);
57355783
/** @type {MutableToken} */
57365784
const tok = {
57375785
type: TOKEN_EOF,
@@ -8319,7 +8367,11 @@ const handleTextToken = (input, start, end) => {
83198367
(NO_DECODE_TEXT.has(_tagNameOf(top)) ||
83208368
(scripting && _tagNameOf(top) === "noscript")) &&
83218369
(mode === MODE_TEXT || mode === MODE_IN_BODY);
8322-
let data = noDecode ? normalized : decode(normalized, false);
8370+
let data = noDecode
8371+
? normalized
8372+
: xmlMode
8373+
? decodeXmlEntities(normalized)
8374+
: decode(normalized, false);
83238375
// In RAWTEXT/RCDATA/script/PLAINTEXT, NULL becomes U+FFFD (tokenizer
83248376
// rule); in data state NULLs pass through to be dropped in "in body".
83258377
if (rawMode && inputHasNul && data.includes("\0")) {
@@ -8403,7 +8455,7 @@ const handleStartTagToken = (
84038455
const name = xmlMode
84048456
? input.slice(nameStart, nameEnd)
84058457
: internLowerName(TAG_NAME_INTERN, input, nameStart, nameEnd);
8406-
if (name === "selectedcontent") sawSelectedContent = true;
8458+
if (!xmlMode && name === "selectedcontent") sawSelectedContent = true;
84078459
tok.type = TOKEN_START_TAG;
84088460
tok.name = name;
84098461
// The reused token carries the tag's attribute run; every consumer of

‎test/unitCases/HtmlSyntax.unittest.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3471,6 +3471,32 @@ describe("parseHtml", () => {
34713471
});
34723472
});
34733473

3474+
it("should not decode HTML-only or differently-cased entity names", () => {
3475+
const root = /** @type {MatElement} */ (
3476+
parseXml("<root>&nbsp;&AMP;&amp;</root>").children[0]
3477+
);
3478+
expect(root.children[0]).toMatchObject({
3479+
type: NodeType.Text,
3480+
data: "&nbsp;&AMP;&"
3481+
});
3482+
});
3483+
3484+
it("should not apply selectedcontent HTML behavior", () => {
3485+
const root = /** @type {MatElement} */ (
3486+
parseXml(
3487+
"<root><select><option selected=\"\"><span/></option><selectedcontent/></select></root>"
3488+
).children[0]
3489+
);
3490+
const select = /** @type {MatElement} */ (root.children[0]);
3491+
expect(select.children.map((node) => node.type)).toEqual([
3492+
NodeType.Element,
3493+
NodeType.Element
3494+
]);
3495+
expect(
3496+
/** @type {MatElement} */ (select.children[1]).children
3497+
).toHaveLength(0);
3498+
});
3499+
34743500
it("should honor AST skip options without changing XML structure", () => {
34753501
const root = /** @type {MatElement} */ (
34763502
parseXml("<root>text<!-- comment --><child/></root>", {

0 commit comments

Comments
 (0)