@@ -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;
57325735let eofInTag = false;
57335736let sawSelectedContent = false;
57345737const 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} */
57365784const 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
0 commit comments