Skip to content

Commit ea7148c

Browse files
committed
Revert back to encoding names as output
Now that our dependency package has labelToName() exported, this is easy enough to do. Additionally, in jsdom we always immediately wrap the result in labelToName() anyway, so this will make it easier to consume.
1 parent fab2967 commit ea7148c

14 files changed

+21
-21
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ const sniffedEncoding = htmlEncodingSniffer(htmlBytes);
1212

1313
The passed bytes are given as a `Uint8Array`; the Node.js `Buffer` subclass of `Uint8Array` will also work, as shown above.
1414

15-
The returned value will be an [encoding label](https://encoding.spec.whatwg.org/#names-and-labels), and in particular, the label which is a lowercased version of the encoding's name. You might then combine this with the [`@exodus/bytes`](https://github.com/ExodusOSS/bytes/) package to decode the result:
15+
The returned value will be a canonical [encoding name](https://encoding.spec.whatwg.org/#names-and-labels) (not a label). You might then combine this with the [`@exodus/bytes`](https://github.com/ExodusOSS/bytes/) package to decode the result:
1616

1717
```js
1818
const { TextDecoder } = require("@exodus/bytes");
19-
const htmlString = (new TextEncoder(sniffedEncoding)).decode(htmlBytes);
19+
const htmlString = (new TextDecoder(sniffedEncoding)).decode(htmlBytes);
2020
```
2121

2222
## Options

lib/html-encoding-sniffer.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"use strict";
2-
const { getBOMEncoding, normalizeEncoding: labelToName } = require("@exodus/bytes/encoding-lite.js");
2+
const { getBOMEncoding, labelToName } = require("@exodus/bytes/encoding-lite.js");
33

44
// https://html.spec.whatwg.org/#encoding-sniffing-algorithm
55
module.exports = (uint8Array, { transportLayerEncodingLabel, defaultEncoding = "windows-1252" } = {}) => {
6-
let encoding = getBOMEncoding(uint8Array);
6+
let encoding = labelToName(getBOMEncoding(uint8Array));
77

88
if (encoding === null && transportLayerEncodingLabel !== undefined) {
99
encoding = labelToName(transportLayerEncodingLabel);
@@ -86,8 +86,8 @@ function prescanMetaCharset(uint8Array) {
8686
continue;
8787
}
8888

89-
if (charset === "utf-16le" || charset === "utf-16be") {
90-
charset = "utf-8";
89+
if (charset === "UTF-16LE" || charset === "UTF-16BE") {
90+
charset = "UTF-8";
9191
}
9292
if (charset === "x-user-defined") {
9393
charset = "windows-1252";

package-lock.json

Lines changed: 4 additions & 4 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
@@ -21,7 +21,7 @@
2121
"lint": "eslint"
2222
},
2323
"dependencies": {
24-
"@exodus/bytes": "^1.2.0"
24+
"@exodus/bytes": "^1.6.0"
2525
},
2626
"devDependencies": {
2727
"@domenic/eslint-config": "^4.0.1",
File renamed without changes.

test/fixtures/normal/charset-short-comment_iso-8859-2.html renamed to test/fixtures/normal/charset-short-comment_ISO-8859-2.html

File renamed without changes.

0 commit comments

Comments
 (0)