From 9948692ecd6cc9bb25353e519a75041adc2350d7 Mon Sep 17 00:00:00 2001 From: Carlo Goetz Date: Thu, 30 Oct 2025 09:20:52 +0100 Subject: [PATCH] fix: [#1947] Decode ' / numeric character references in attr values - attribute values can contain named and numeric character references per https://html.spec.whatwg.org/multipage/syntax.html#syntax-attribute-value - add decoding for all numeric character references - add decoding for ', this could probably appear in single-quoted attribute values - ignore other named character references, the list is huge --- .../happy-dom/src/utilities/XMLEncodeUtility.ts | 7 ++++++- .../happy-dom/test/html-parser/HTMLParser.test.ts | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/happy-dom/src/utilities/XMLEncodeUtility.ts b/packages/happy-dom/src/utilities/XMLEncodeUtility.ts index 8aef0b063..a268d7ba4 100644 --- a/packages/happy-dom/src/utilities/XMLEncodeUtility.ts +++ b/packages/happy-dom/src/utilities/XMLEncodeUtility.ts @@ -67,7 +67,12 @@ export default class XMLEncodeUtility { return ''; } - return value.replace(/"/gu, '"').replace(/&/gu, '&'); + return value + .replace(/"/gu, '"') + .replace(/&/gu, '&') + .replace(/'/gu, "'") + .replace(/&#(\d+);/gu, (_match, dec) => String.fromCharCode(parseInt(dec, 10))) + .replace(/&#x([A-Fa-f\d]+);/gu, (_match, hex) => String.fromCharCode(parseInt(hex, 16))); } /** diff --git a/packages/happy-dom/test/html-parser/HTMLParser.test.ts b/packages/happy-dom/test/html-parser/HTMLParser.test.ts index d7f9b2394..ef15bc2a3 100644 --- a/packages/happy-dom/test/html-parser/HTMLParser.test.ts +++ b/packages/happy-dom/test/html-parser/HTMLParser.test.ts @@ -2208,5 +2208,20 @@ describe('HTMLParser', () => { `); }); + + it('Handles numeric character references and ' in attribute values for #1947', () => { + const result = new HTMLParser(window).parse(` +
+ + + +
`); + expect(new HTMLSerializer().serializeToString(result)).toBe(` +
+ + + +
`); + }); }); });