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>