Skip to content

Commit

Permalink
Fix labelToName() to work with x-user-defined
Browse files Browse the repository at this point in the history
  • Loading branch information
domenic committed Nov 12, 2023
1 parent 27ffb33 commit ff5d22f
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/whatwg-encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const iconvLite = require("iconv-lite");
const supportedNames = require("./supported-names.json");
const labelsToNames = require("./labels-to-names.json");

const supportedNamesSet = new Set([...supportedNames, "x-user-defined"]);
const supportedNamesSet = new Set(supportedNames);

// https://encoding.spec.whatwg.org/#concept-encoding-get
exports.labelToName = label => {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"lib/"
],
"scripts": {
"pretest": "npm run prepare",
"test": "node --test",
"lint": "eslint .",
"prepare": "node scripts/update.js"
Expand Down
2 changes: 1 addition & 1 deletion scripts/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async function main() {
const supportedNames = [];
for (const entry of body) {
for (const encoding of entry.encodings) {
if (iconvLite.encodingExists(encoding.name)) {
if (iconvLite.encodingExists(encoding.name) || encoding.name === "x-user-defined") {
supportedNames.push(encoding.name);
for (const label of encoding.labels) {
labelsToNames[label] = encoding.name;
Expand Down
4 changes: 4 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,27 +66,31 @@ describe("labelToName", () => {
assert.strictEqual(whatwgEncoding.labelToName("csibm866"), "IBM866");
assert.strictEqual(whatwgEncoding.labelToName("latin3"), "ISO-8859-3");
assert.strictEqual(whatwgEncoding.labelToName("tis-620"), "windows-874");
assert.strictEqual(whatwgEncoding.labelToName("x-user-defined"), "x-user-defined");
});

it("should be case-insensitive", () => {
assert.strictEqual(whatwgEncoding.labelToName("ASCII"), "windows-1252");
assert.strictEqual(whatwgEncoding.labelToName("csIBM866"), "IBM866");
assert.strictEqual(whatwgEncoding.labelToName("laTIn3"), "ISO-8859-3");
assert.strictEqual(whatwgEncoding.labelToName("Tis-620"), "windows-874");
assert.strictEqual(whatwgEncoding.labelToName("x-USER-Defined"), "x-user-defined");
});

it("should trim ASCII whitespace", () => {
assert.strictEqual(whatwgEncoding.labelToName("\u0009ascii\u000A"), "windows-1252");
assert.strictEqual(whatwgEncoding.labelToName("\u000C\u000Ccsibm866"), "IBM866");
assert.strictEqual(whatwgEncoding.labelToName("latin3\u000D\u000D"), "ISO-8859-3");
assert.strictEqual(whatwgEncoding.labelToName("tis-620\u0020"), "windows-874");
assert.strictEqual(whatwgEncoding.labelToName("\u0020x-user-defined"), "x-user-defined");
});

it("should trim ASCII whitespace and be case-insensitive", () => {
assert.strictEqual(whatwgEncoding.labelToName("\u0009ASCII\u000A"), "windows-1252");
assert.strictEqual(whatwgEncoding.labelToName("\u000C\u000CcsIBM866"), "IBM866");
assert.strictEqual(whatwgEncoding.labelToName("laTIn3\u000D\u000D"), "ISO-8859-3");
assert.strictEqual(whatwgEncoding.labelToName("Tis-620\u0020"), "windows-874");
assert.strictEqual(whatwgEncoding.labelToName("\u0020x-USER-Defined"), "x-user-defined");
});

it("should return null for invalid encoding labels", () => {
Expand Down

0 comments on commit ff5d22f

Please sign in to comment.