From 35d809d14f356034c59b5d0398b95eb034c5a202 Mon Sep 17 00:00:00 2001 From: yuri Date: Mon, 16 Nov 2026 11:26:26 +0900 Subject: [PATCH 1/4] test: cover attribute escaping and text element serialization (#340, #341) --- test/html/style-element.js | 6 ++++++ test/interface/attr.js | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/test/html/style-element.js b/test/html/style-element.js index f50259b5..4c7b3062 100644 --- a/test/html/style-element.js +++ b/test/html/style-element.js @@ -38,3 +38,9 @@ assert(style.sheet.cssRules[0].style.color, 'blue'); assert(style.sheet.cssRules[0].cssRules.length, 1); assert(style.sheet.cssRules[0].cssRules[0].selectorText, '& .child'); assert(style.sheet.cssRules[0].cssRules[0].style.color, 'red'); + +// https://github.com/WebReflection/linkedom/issues/341 +style = parseHTML('').document.createElement('style'); +style.setAttribute('data-src', 'a>.caption{color:red}', 'style with >< in an attribute value'); diff --git a/test/interface/attr.js b/test/interface/attr.js index f441a8ab..e9f03f40 100644 --- a/test/interface/attr.js +++ b/test/interface/attr.js @@ -28,3 +28,12 @@ assert(xmlDoc.toString(), ''); assert(xmlDoc.firstElementChild.outerHTML, ''); assert(xmlDoc.firstElementChild.attributes.attr.value, 'a"b"c'); + +// https://github.com/WebReflection/linkedom/issues/340 +const {document: htmlDoc} = parseHTML('
'); +const div = htmlDoc.getElementById('t'); +div.setAttribute('data-x', 'A & B'); +assert(div.outerHTML, '
', 'HTML attribute & is escaped'); +assert(parseHTML(`${div.outerHTML}`).document.getElementById('t').getAttribute('data-x'), 'A & B', 'HTML attribute round trip'); +div.setAttribute('data-x', 'a b "c"'); +assert(div.outerHTML, '
', 'HTML attribute U+00A0 is escaped'); From fb3cf233434c52e3d8b40ab347d4848602309396 Mon Sep 17 00:00:00 2001 From: yuri Date: Mon, 16 Nov 2026 11:29:07 +0900 Subject: [PATCH 2/4] fix: escape & and U+00A0 in HTML attribute values, keep TextElement text at the end of the start tag (#340, #341) Co-Authored-By: Claude Sonnet 5.5 --- cjs/html/text-element.js | 3 ++- cjs/interface/attr.js | 4 +++- esm/html/text-element.js | 3 ++- esm/interface/attr.js | 4 +++- test/html/anchor-element.js | 4 ++-- test/html/meta-element.js | 2 +- test/interface/attr.js | 4 ++-- test/interface/element.js | 4 ++-- worker.js | 11 +++++++---- 9 files changed, 24 insertions(+), 15 deletions(-) diff --git a/cjs/html/text-element.js b/cjs/html/text-element.js index d55b5d3c..66210c41 100644 --- a/cjs/html/text-element.js +++ b/cjs/html/text-element.js @@ -10,7 +10,8 @@ class TextElement extends HTMLElement { toString() { const outerHTML = toString.call(this.cloneNode()); - return outerHTML.replace('><', () => `>${this.textContent}<`); + const at = outerHTML.lastIndexOf('><'); + return `${outerHTML.slice(0, at + 1)}${this.textContent}${outerHTML.slice(at + 1)}`; } } exports.TextElement = TextElement diff --git a/cjs/interface/attr.js b/cjs/interface/attr.js index 65af5bac..71315aeb 100644 --- a/cjs/interface/attr.js +++ b/cjs/interface/attr.js @@ -12,6 +12,8 @@ const {Node} = require('./node.js'); const {escape} = require('../shared/text-escaper.js'); const QUOTE = /"/g; +const AMP = /&/g; +const NBSP = /\xA0/g; /** * @implements globalThis.Attr @@ -46,7 +48,7 @@ class Attr extends Node { if (emptyAttributes.has(name) && !value) { return ignoreCase(this) ? name : `${name}=""`; } - const escapedValue = (ignoreCase(this) ? value : escape(value)).replace(QUOTE, '"'); + const escapedValue = (ignoreCase(this) ? value.replace(AMP, '&').replace(NBSP, ' ') : escape(value)).replace(QUOTE, '"'); return `${name}="${escapedValue}"`; } diff --git a/esm/html/text-element.js b/esm/html/text-element.js index ae231795..fa1bc58e 100644 --- a/esm/html/text-element.js +++ b/esm/html/text-element.js @@ -9,6 +9,7 @@ export class TextElement extends HTMLElement { toString() { const outerHTML = toString.call(this.cloneNode()); - return outerHTML.replace('><', () => `>${this.textContent}<`); + const at = outerHTML.lastIndexOf('><'); + return `${outerHTML.slice(0, at + 1)}${this.textContent}${outerHTML.slice(at + 1)}`; } } diff --git a/esm/interface/attr.js b/esm/interface/attr.js index 6c617f69..2eaea7a7 100644 --- a/esm/interface/attr.js +++ b/esm/interface/attr.js @@ -11,6 +11,8 @@ import {Node} from './node.js'; import {escape} from '../shared/text-escaper.js'; const QUOTE = /"/g; +const AMP = /&/g; +const NBSP = /\xA0/g; /** * @implements globalThis.Attr @@ -45,7 +47,7 @@ export class Attr extends Node { if (emptyAttributes.has(name) && !value) { return ignoreCase(this) ? name : `${name}=""`; } - const escapedValue = (ignoreCase(this) ? value : escape(value)).replace(QUOTE, '"'); + const escapedValue = (ignoreCase(this) ? value.replace(AMP, '&').replace(NBSP, ' ') : escape(value)).replace(QUOTE, '"'); return `${name}="${escapedValue}"`; } diff --git a/test/html/anchor-element.js b/test/html/anchor-element.js index 1372b173..dbaee808 100644 --- a/test/html/anchor-element.js +++ b/test/html/anchor-element.js @@ -6,9 +6,9 @@ const {document} = parseHTML('click me< const {lastElementChild: a} = document; -assert(a.toString(), 'click me'); +assert(a.toString(), 'click me'); a.setAttribute('href', 'https://google.com/?q=1&page=2&test="'); -assert(a.toString(), 'click me'); +assert(a.toString(), 'click me'); a.setAttribute('href', 'https://google.com/?q=asd&lol=<2>"'); assert(a.href, 'https://google.com/?q=asd&lol=%3C2%3E%22'); a.setAttribute('href', 'https://google.com/path%20to%20some%20file.pdf'); diff --git a/test/html/meta-element.js b/test/html/meta-element.js index be2b8fa6..13d82144 100644 --- a/test/html/meta-element.js +++ b/test/html/meta-element.js @@ -24,7 +24,7 @@ assert(b.charset, 'utf-8'); const {document: httpEquivRefresh} = parseHTML(''); const {lastElementChild: c} = httpEquivRefresh; // assert toString -assert(c.toString(), ''); +assert(c.toString(), ''); // assert httpEquiv & content attribute assert(c.httpEquiv, 'refresh'); assert(c.content, '0; url=https://google.com/?q=1&page=2'); diff --git a/test/interface/attr.js b/test/interface/attr.js index e9f03f40..403e446f 100644 --- a/test/interface/attr.js +++ b/test/interface/attr.js @@ -33,7 +33,7 @@ assert(xmlDoc.firstElementChild.attributes.attr.value, 'a"b"c'); const {document: htmlDoc} = parseHTML('
'); const div = htmlDoc.getElementById('t'); div.setAttribute('data-x', 'A & B'); -assert(div.outerHTML, '
', 'HTML attribute & is escaped'); +assert(div.outerHTML, '
', 'HTML attribute & is escaped'); assert(parseHTML(`${div.outerHTML}`).document.getElementById('t').getAttribute('data-x'), 'A & B', 'HTML attribute round trip'); div.setAttribute('data-x', 'a b "c"'); -assert(div.outerHTML, '
', 'HTML attribute U+00A0 is escaped'); +assert(div.outerHTML, '
', 'HTML attribute U+00A0 is escaped'); diff --git a/test/interface/element.js b/test/interface/element.js index bff3f829..145d4a04 100644 --- a/test/interface/element.js +++ b/test/interface/element.js @@ -23,8 +23,8 @@ const parser = new DOMParser(); const htmlDoc = parser.parseFromString(`
`, 'text/html').documentElement; assert(htmlDoc.firstChild.getAttribute('content-desc'), 'text3&more'); -assert(htmlDoc.firstChild.outerHTML, ''); -assert(htmlDoc.innerHTML, ''); +assert(htmlDoc.firstChild.outerHTML, ''); +assert(htmlDoc.innerHTML, ''); htmlDoc.firstChild.setAttribute('content-desc', ''); // attribute is not in emptyAttributes set is empty assert(htmlDoc.firstChild.getAttribute('content-desc'), ''); diff --git a/worker.js b/worker.js index fa7dc792..942bff18 100644 --- a/worker.js +++ b/worker.js @@ -2998,6 +2998,8 @@ const pe = m => esca[m]; const escape = es => replace.call(es, ca, pe); const QUOTE = /"/g; +const AMP = /&/g; +const NBSP = /\xA0/g; /** * @implements globalThis.Attr @@ -3032,7 +3034,7 @@ let Attr$1 = class Attr extends Node$1 { if (emptyAttributes.has(name) && !value) { return ignoreCase(this) ? name : `${name}=""`; } - const escapedValue = (ignoreCase(this) ? value : escape(value)).replace(QUOTE, '"'); + const escapedValue = (ignoreCase(this) ? value.replace(AMP, '&').replace(NBSP, ' ') : escape(value)).replace(QUOTE, '"'); return `${name}="${escapedValue}"`; } @@ -8400,7 +8402,8 @@ class TextElement extends HTMLElement { toString() { const outerHTML = toString.call(this.cloneNode()); - return outerHTML.replace('><', () => `>${this.textContent}<`); + const at = outerHTML.lastIndexOf('><'); + return `${outerHTML.slice(0, at + 1)}${this.textContent}${outerHTML.slice(at + 1)}`; } } @@ -14698,7 +14701,7 @@ var CSSStyleSheet = class CSSStyleSheet extends StyleSheet { } } _unregisterProperties() { - for (const name of this._registeredProperties) ParseHooks.unregisterPropertyRule(name); + for (const name of this._registeredProperties) ParseHooks.unregisterPropertyRule(); this._registeredProperties = []; } constructor(options = {}) { @@ -17708,7 +17711,7 @@ var CSSStyleDeclaration = class extends CSSStyleProperties { const expanded = shorthand.expand(compVals); if (expanded) { if (ParseHooks.validatePropertyValue) { - for (const [lh, val] of Object.entries(expanded)) if (!ParseHooks.validatePropertyValue(lh, serialize(val), val)) ; + for (const [lh, val] of Object.entries(expanded)) if (!ParseHooks.validatePropertyValue(lh, serialize(val))) ; } for (const [lh, val] of Object.entries(expanded)) this._setPropertyInternal(lh, serialize(val), normalizedPriority, false, property === "all" || fromAll); if (notify) { From 813e5c8d399c226b9be128a9af485a0f9774cc92 Mon Sep 17 00:00:00 2001 From: Xinlu <80815354+mistyhey@users.noreply.github.com> Date: Tue, 17 Nov 2026 22:00:06 +0900 Subject: [PATCH 3/4] fix: keep a raw & in HTML attributes unless it reads as a character reference (#340) Escaping every & changed the output of common markup such as href="?a=1&b=2". An & is now escaped only when the value would be decoded on reparse (checked with the entities attribute decoder htmlparser2 uses), so plain query strings serialize as before and values like "A & B" still round-trip. U+00A0 is left raw: it already round-trips. Co-Authored-By: Claude Opus 5.5 --- cjs/interface/attr.js | 7 +++- esm/interface/attr.js | 7 +++- package-lock.json | 75 +++++++++---------------------------- package.json | 1 + test/html/anchor-element.js | 4 +- test/html/meta-element.js | 2 +- test/interface/attr.js | 7 +++- test/interface/element.js | 4 +- worker.js | 48 +++++++++++++++++++++++- 9 files changed, 84 insertions(+), 71 deletions(-) diff --git a/cjs/interface/attr.js b/cjs/interface/attr.js index 71315aeb..72c45dc9 100644 --- a/cjs/interface/attr.js +++ b/cjs/interface/attr.js @@ -10,10 +10,13 @@ const {attributeChangedCallback: ceAttributes} = require('./custom-element-regis const {Node} = require('./node.js'); const {escape} = require('../shared/text-escaper.js'); +const {decodeHTMLAttribute} = require('entities/decode'); const QUOTE = /"/g; const AMP = /&/g; -const NBSP = /\xA0/g; + +// HTML keeps a raw & (e.g. ?a=1&b=2) unless the parser would read a character reference. +const escapeAmpersands = value => decodeHTMLAttribute(value) === value ? value : value.replace(AMP, '&'); /** * @implements globalThis.Attr @@ -48,7 +51,7 @@ class Attr extends Node { if (emptyAttributes.has(name) && !value) { return ignoreCase(this) ? name : `${name}=""`; } - const escapedValue = (ignoreCase(this) ? value.replace(AMP, '&').replace(NBSP, ' ') : escape(value)).replace(QUOTE, '"'); + const escapedValue = (ignoreCase(this) ? escapeAmpersands(value) : escape(value)).replace(QUOTE, '"'); return `${name}="${escapedValue}"`; } diff --git a/esm/interface/attr.js b/esm/interface/attr.js index 2eaea7a7..556ea80b 100644 --- a/esm/interface/attr.js +++ b/esm/interface/attr.js @@ -9,10 +9,13 @@ import {attributeChangedCallback as ceAttributes} from './custom-element-registr import {Node} from './node.js'; import {escape} from '../shared/text-escaper.js'; +import {decodeHTMLAttribute} from 'entities/decode'; const QUOTE = /"/g; const AMP = /&/g; -const NBSP = /\xA0/g; + +// HTML keeps a raw & (e.g. ?a=1&b=2) unless the parser would read a character reference. +const escapeAmpersands = value => decodeHTMLAttribute(value) === value ? value : value.replace(AMP, '&'); /** * @implements globalThis.Attr @@ -47,7 +50,7 @@ export class Attr extends Node { if (emptyAttributes.has(name) && !value) { return ignoreCase(this) ? name : `${name}=""`; } - const escapedValue = (ignoreCase(this) ? value.replace(AMP, '&').replace(NBSP, ' ') : escape(value)).replace(QUOTE, '"'); + const escapedValue = (ignoreCase(this) ? escapeAmpersands(value) : escape(value)).replace(QUOTE, '"'); return `${name}="${escapedValue}"`; } diff --git a/package-lock.json b/package-lock.json index d5547f04..4f4c761a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "css-select": "^7.0.0", "cssomnom": "^0.1.11", + "entities": "^7.0.1", "html-escaper": "^3.0.3", "htmlparser2": "^10.1.0", "uhyphen": "^0.2.0" @@ -244,9 +245,6 @@ "x64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -482,9 +480,6 @@ "arm" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -499,9 +494,6 @@ "arm" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -516,9 +508,6 @@ "arm64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -533,9 +522,6 @@ "arm64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -550,9 +536,6 @@ "loong64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -567,9 +550,6 @@ "loong64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -584,9 +564,6 @@ "ppc64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -601,9 +578,6 @@ "ppc64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -618,9 +592,6 @@ "riscv64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -635,9 +606,6 @@ "riscv64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -652,9 +620,6 @@ "s390x" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -669,9 +634,6 @@ "x64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -686,9 +648,6 @@ "x64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -1211,6 +1170,18 @@ "url": "https://github.com/cheeriojs/dom-serializer?sponsor=1" } }, + "node_modules/dom-serializer/node_modules/entities": { + "version": "8.1.0", + "resolved": "https://registry.npmjs.org/entities/-/entities-8.1.0.tgz", + "integrity": "sha512-kxL7msIffSuh9aaFAMD7rxAIuTRMAHMeBtgHW2yUdWw732ZNh4MehkF2gdjvtdmikkaIP9bFDDJOPlsvm7avrA==", + "license": "BSD-2-Clause", + "engines": { + "node": ">=20.19.0" + }, + "funding": { + "url": "https://github.com/fb55/entities?sponsor=1" + } + }, "node_modules/domelementtype": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/domelementtype/-/domelementtype-3.0.0.tgz", @@ -1268,12 +1239,12 @@ "license": "MIT" }, "node_modules/entities": { - "version": "8.1.0", - "resolved": "https://registry.npmjs.org/entities/-/entities-8.1.0.tgz", - "integrity": "sha512-kxL7msIffSuh9aaFAMD7rxAIuTRMAHMeBtgHW2yUdWw732ZNh4MehkF2gdjvtdmikkaIP9bFDDJOPlsvm7avrA==", + "version": "7.0.1", + "resolved": "https://registry.npmjs.org/entities/-/entities-7.0.1.tgz", + "integrity": "sha512-TWrgLOFUQTH994YUyl1yT4uyavY5nNB5muff+RtWaqNVCAK408b5ZnnbNAUEWLTCpum9w6arT70i1XdQ4UeOPA==", "license": "BSD-2-Clause", "engines": { - "node": ">=20.19.0" + "node": ">=0.12" }, "funding": { "url": "https://github.com/fb55/entities?sponsor=1" @@ -1816,18 +1787,6 @@ "url": "https://github.com/fb55/domutils?sponsor=1" } }, - "node_modules/htmlparser2/node_modules/entities": { - "version": "7.0.1", - "resolved": "https://registry.npmjs.org/entities/-/entities-7.0.1.tgz", - "integrity": "sha512-TWrgLOFUQTH994YUyl1yT4uyavY5nNB5muff+RtWaqNVCAK408b5ZnnbNAUEWLTCpum9w6arT70i1XdQ4UeOPA==", - "license": "BSD-2-Clause", - "engines": { - "node": ">=0.12" - }, - "funding": { - "url": "https://github.com/fb55/entities?sponsor=1" - } - }, "node_modules/ignore": { "version": "5.3.2", "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.3.2.tgz", diff --git a/package.json b/package.json index 8c65b991..e576e50a 100644 --- a/package.json +++ b/package.json @@ -72,6 +72,7 @@ "dependencies": { "css-select": "^7.0.0", "cssomnom": "^0.1.11", + "entities": "^7.0.1", "html-escaper": "^3.0.3", "htmlparser2": "^10.1.0", "uhyphen": "^0.2.0" diff --git a/test/html/anchor-element.js b/test/html/anchor-element.js index dbaee808..1372b173 100644 --- a/test/html/anchor-element.js +++ b/test/html/anchor-element.js @@ -6,9 +6,9 @@ const {document} = parseHTML('click me< const {lastElementChild: a} = document; -assert(a.toString(), 'click me'); +assert(a.toString(), 'click me'); a.setAttribute('href', 'https://google.com/?q=1&page=2&test="'); -assert(a.toString(), 'click me'); +assert(a.toString(), 'click me'); a.setAttribute('href', 'https://google.com/?q=asd&lol=<2>"'); assert(a.href, 'https://google.com/?q=asd&lol=%3C2%3E%22'); a.setAttribute('href', 'https://google.com/path%20to%20some%20file.pdf'); diff --git a/test/html/meta-element.js b/test/html/meta-element.js index 13d82144..be2b8fa6 100644 --- a/test/html/meta-element.js +++ b/test/html/meta-element.js @@ -24,7 +24,7 @@ assert(b.charset, 'utf-8'); const {document: httpEquivRefresh} = parseHTML(''); const {lastElementChild: c} = httpEquivRefresh; // assert toString -assert(c.toString(), ''); +assert(c.toString(), ''); // assert httpEquiv & content attribute assert(c.httpEquiv, 'refresh'); assert(c.content, '0; url=https://google.com/?q=1&page=2'); diff --git a/test/interface/attr.js b/test/interface/attr.js index 403e446f..f2b1748f 100644 --- a/test/interface/attr.js +++ b/test/interface/attr.js @@ -35,5 +35,8 @@ const div = htmlDoc.getElementById('t'); div.setAttribute('data-x', 'A & B'); assert(div.outerHTML, '
', 'HTML attribute & is escaped'); assert(parseHTML(`${div.outerHTML}`).document.getElementById('t').getAttribute('data-x'), 'A & B', 'HTML attribute round trip'); -div.setAttribute('data-x', 'a b "c"'); -assert(div.outerHTML, '
', 'HTML attribute U+00A0 is escaped'); +div.setAttribute('data-x', '?a=1&b=2 & d©=1'); +assert(div.outerHTML, '
', 'HTML attribute & kept when it is not a character reference'); +div.setAttribute('data-x', 'x© y& z&'); +assert(div.outerHTML, '
', 'HTML attribute character references are escaped'); +assert(parseHTML(`${div.outerHTML}`).document.getElementById('t').getAttribute('data-x'), 'x© y& z&', 'HTML attribute character reference round trip'); diff --git a/test/interface/element.js b/test/interface/element.js index 145d4a04..bff3f829 100644 --- a/test/interface/element.js +++ b/test/interface/element.js @@ -23,8 +23,8 @@ const parser = new DOMParser(); const htmlDoc = parser.parseFromString(`
`, 'text/html').documentElement; assert(htmlDoc.firstChild.getAttribute('content-desc'), 'text3&more'); -assert(htmlDoc.firstChild.outerHTML, ''); -assert(htmlDoc.innerHTML, ''); +assert(htmlDoc.firstChild.outerHTML, ''); +assert(htmlDoc.innerHTML, ''); htmlDoc.firstChild.setAttribute('content-desc', ''); // attribute is not in emptyAttributes set is empty assert(htmlDoc.firstChild.getAttribute('content-desc'), ''); diff --git a/worker.js b/worker.js index 942bff18..5699365d 100644 --- a/worker.js +++ b/worker.js @@ -603,6 +603,38 @@ class EntityDecoder { } } } +/** + * Creates a function that decodes entities in a string. + * + * @param decodeTree The decode tree. + * @returns A function that decodes entities in a string. + */ +function getDecoder(decodeTree) { + let returnValue = ""; + const decoder = new EntityDecoder(decodeTree, (data) => (returnValue += fromCodePoint(data))); + return function decodeWithTrie(input, decodeMode) { + let lastIndex = 0; + let offset = 0; + while ((offset = input.indexOf("&", offset)) >= 0) { + returnValue += input.slice(lastIndex, offset); + decoder.startEntity(decodeMode); + const length = decoder.write(input, + // Skip the "&" + offset + 1); + if (length < 0) { + lastIndex = offset + decoder.end(); + break; + } + lastIndex = offset + length; + // If `length` is 0, skip the current `&` and continue. + offset = length === 0 ? lastIndex + 1 : lastIndex; + } + const result = returnValue + input.slice(lastIndex); + // Make sure we don't keep a reference to the final string. + returnValue = ""; + return result; + }; +} /** * Determines the branch of the current node that is taken given the current * character. This function is used to traverse the trie. @@ -652,6 +684,16 @@ function determineBranch(decodeTree, current, nodeIndex, char) { } return -1; } +const htmlDecoder = /* #__PURE__ */ getDecoder(htmlDecodeTree); +/** + * Decodes an HTML string in an attribute. + * + * @param htmlAttribute The string to decode. + * @returns The decoded string. + */ +function decodeHTMLAttribute(htmlAttribute) { + return htmlDecoder(htmlAttribute, DecodingMode.Attribute); +} var CharCodes; (function (CharCodes) { @@ -2999,7 +3041,9 @@ const escape = es => replace.call(es, ca, pe); const QUOTE = /"/g; const AMP = /&/g; -const NBSP = /\xA0/g; + +// HTML keeps a raw & (e.g. ?a=1&b=2) unless the parser would read a character reference. +const escapeAmpersands = value => decodeHTMLAttribute(value) === value ? value : value.replace(AMP, '&'); /** * @implements globalThis.Attr @@ -3034,7 +3078,7 @@ let Attr$1 = class Attr extends Node$1 { if (emptyAttributes.has(name) && !value) { return ignoreCase(this) ? name : `${name}=""`; } - const escapedValue = (ignoreCase(this) ? value.replace(AMP, '&').replace(NBSP, ' ') : escape(value)).replace(QUOTE, '"'); + const escapedValue = (ignoreCase(this) ? escapeAmpersands(value) : escape(value)).replace(QUOTE, '"'); return `${name}="${escapedValue}"`; } From 498138e0cbbb438a54c23a00e93664b25abbe636 Mon Sep 17 00:00:00 2001 From: Xinlu <80815354+mistyhey@users.noreply.github.com> Date: Tue, 17 Nov 2026 22:03:28 +0900 Subject: [PATCH 4/4] chore: regenerate types for TextElement.toString (#341) Co-Authored-By: Claude Opus 5.5 --- types/esm/html/text-element.d.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/types/esm/html/text-element.d.ts b/types/esm/html/text-element.d.ts index 0a378cd2..42ebe600 100644 --- a/types/esm/html/text-element.d.ts +++ b/types/esm/html/text-element.d.ts @@ -1,4 +1,3 @@ export class TextElement extends HTMLElement { - toString(): any; } import { HTMLElement } from './element.js';