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(), '