Skip to content

Commit 71d562f

Browse files
domeniccodex
andcommitted
Update html-encoding-sniffer to v7
Honor XML encoding declarations and BOM-less UTF-16 signatures when decoding HTML and XML byte input, including documents loaded in frames. The upgrade also fixes exceptions on truncated meta charset parameters and ignores incomplete meta tags. Co-authored-by: codex <[email protected]>
1 parent 364325d commit 71d562f

4 files changed

Lines changed: 21 additions & 17 deletions

File tree

‎package-lock.json‎

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"css-tree": "^3.2.1",
3232
"data-urls": "^7.0.0",
3333
"decimal.js": "^10.6.0",
34-
"html-encoding-sniffer": "^6.0.0",
34+
"html-encoding-sniffer": "^7.0.0",
3535
"is-potential-custom-element-name": "^1.0.1",
3636
"lru-cache": "^11.5.2",
3737
"parse5": "^8.0.1",

‎test/api/encoding.js‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const assert = require("node:assert/strict");
33
const fs = require("node:fs");
44
const path = require("node:path");
55
const { describe, it, before, after } = require("mocha-sugar-free");
6+
const { utf16fromString } = require("@exodus/bytes/utf16.js");
67
const { createServer } = require("./helpers/servers.js");
78

89
const { JSDOM } = require("../..");
@@ -91,7 +92,6 @@ const encodingFixtures = {
9192
}
9293
};
9394

94-
// For XML, we always default to UTF-8 unless there is a BOM. No charset sniffing.
9595
const xmlEncodingFixtures = {
9696
"no-bom-utf8.xml": {
9797
name: "UTF-8",
@@ -107,11 +107,10 @@ const xmlEncodingFixtures = {
107107
bodyWhenOverridden: "¢"
108108
},
109109
"no-bom-with-encoding-decl.xml": {
110-
// Has <?xml encoding="KOI8-R"?> but the declaration should be IGNORED
111-
// Content is valid UTF-8, so it decodes correctly
112-
name: "UTF-8",
110+
// The UTF-8 bytes D0 84 decode as п└ in the declared KOI8-R encoding.
111+
name: "KOI8-R",
113112
nameWhenOverridden: "ISO-8859-8",
114-
body: "Є"
113+
body: "п└"
115114
},
116115
"utf-8-bom.xml": {
117116
name: "UTF-8",
@@ -282,9 +281,17 @@ describe("API: encoding detection", () => {
282281
});
283282
});
284283

285-
// XML encoding tests: For XML, we always default to UTF-8 unless there is a BOM.
286-
// There is no charset sniffing from XML declarations or other sources.
287284
describe("XML: constructor, given binary data", () => {
285+
for (const [encoding, format] of [["UTF-16LE", "uint8-le"], ["UTF-16BE", "uint8-be"]]) {
286+
it(`should detect ${encoding} without a BOM`, () => {
287+
const input = utf16fromString(`<?xml version="1.0" encoding="${encoding}"?><r>Є</r>`, format);
288+
const dom = new JSDOM(input, { contentType: "application/xhtml+xml" });
289+
290+
assert.equal(dom.window.document.characterSet, encoding);
291+
assert.equal(dom.window.document.documentElement.textContent, "Є");
292+
});
293+
}
294+
288295
describe("with contentType application/xhtml+xml (no charset)", () => {
289296
for (const binaryDataType of Object.keys(factories)) {
290297
const factory = factories[binaryDataType];

‎test/web-platform-tests/to-run.yaml‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2916,9 +2916,6 @@ serializing-html-fragments/escaping.html: [fail, Scripting isn't properly inheri
29162916
serializing-html-fragments/processing-instructions.html: [fail, Unknown]
29172917
serializing-html-fragments/serializing-lt-gt.html: [fail, Unknown]
29182918
serializing-html-fragments/serializing.html: [fail, Scripting isn't properly inherited for sub-documents]
2919-
xmldecl/xmldecl-1.html: [fail, Unknown; possibly iframes are inheriting encoding from their parent?]
2920-
xmldecl/xmldecl-2.html: [fail, several encodings misdetected?]
2921-
xmldecl/xmldecl-3.html: [fail, Unknown]
29222919

29232920
---
29242921

0 commit comments

Comments
 (0)