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..72c45dc9 100644 --- a/cjs/interface/attr.js +++ b/cjs/interface/attr.js @@ -10,8 +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; + +// 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 @@ -46,7 +51,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) ? escapeAmpersands(value) : 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..556ea80b 100644 --- a/esm/interface/attr.js +++ b/esm/interface/attr.js @@ -9,8 +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; + +// 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 @@ -45,7 +50,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) ? 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/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..f2b1748f 100644 --- a/test/interface/attr.js +++ b/test/interface/attr.js @@ -28,3 +28,15 @@ 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=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/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'; diff --git a/worker.js b/worker.js index fa7dc792..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) { @@ -2998,6 +3040,10 @@ const pe = m => esca[m]; const escape = es => replace.call(es, ca, pe); const QUOTE = /"/g; +const AMP = /&/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 @@ -3032,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 : escape(value)).replace(QUOTE, '"'); + const escapedValue = (ignoreCase(this) ? escapeAmpersands(value) : escape(value)).replace(QUOTE, '"'); return `${name}="${escapedValue}"`; } @@ -8400,7 +8446,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 +14745,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 +17755,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) {