From c35a5bca5f5db9d4a66b608d19779c2283c1b2e5 Mon Sep 17 00:00:00 2001 From: Nik <84yk8btb9f@privaterelay.appleid.com> Date: Fri, 13 Nov 2026 23:37:47 +0200 Subject: [PATCH] fix: escape HTML attribute values --- cjs/interface/attr.js | 4 +++- esm/interface/attr.js | 4 +++- test/html/anchor-element.js | 4 ++-- test/html/meta-element.js | 2 +- test/interface/attr.js | 18 ++++++++++++++++++ test/interface/element.js | 4 ++-- worker.js | 4 +++- 7 files changed, 32 insertions(+), 8 deletions(-) diff --git a/cjs/interface/attr.js b/cjs/interface/attr.js index 65af5bac..ef48c528 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_OR_NBSP = /[&\xA0]/g; +const escapeHTML = value => value.replace(AMP_OR_NBSP, c => c === '&' ? '&' : ' '); /** * @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) ? escapeHTML(value) : escape(value)).replace(QUOTE, '"'); return `${name}="${escapedValue}"`; } diff --git a/esm/interface/attr.js b/esm/interface/attr.js index 6c617f69..2fc8cac4 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_OR_NBSP = /[&\xA0]/g; +const escapeHTML = value => value.replace(AMP_OR_NBSP, c => c === '&' ? '&' : ' '); /** * @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) ? escapeHTML(value) : 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 f441a8ab..d6d771a3 100644 --- a/test/interface/attr.js +++ b/test/interface/attr.js @@ -28,3 +28,21 @@ assert(xmlDoc.toString(), ''); assert(xmlDoc.firstElementChild.outerHTML, ''); assert(xmlDoc.firstElementChild.attributes.attr.value, 'a"b"c'); + +// attribute values are escaped in HTML documents too (#340) +const {document: htmlDoc} = parseHTML('
'); +const t = htmlDoc.getElementById('t'); + +t.setAttribute('data-x', 'A & B'); +assert(t.outerHTML, '
'); + +t.setAttribute('data-x', 'A & B'); +assert(t.getAttribute('data-x'), 'A & B'); +assert(t.outerHTML, '
'); + +t.setAttribute('data-x', 'a\u00A0b'); +assert(t.getAttribute('data-x'), 'a\u00A0b'); +assert(t.outerHTML, '
'); + +t.setAttribute('data-x', 'say "hi"'); +assert(t.outerHTML, '
'); 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..93fa4aa0 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_OR_NBSP = /[&\xA0]/g; +const escapeHTML = value => value.replace(AMP_OR_NBSP, c => c === '&' ? '&' : ' '); /** * @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) ? escapeHTML(value) : escape(value)).replace(QUOTE, '"'); return `${name}="${escapedValue}"`; }