Skip to content

Commit 8cd325d

Browse files
committed
Initial commit
0 parents  commit 8cd325d

20 files changed

+904
-0
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
insert_final_newline = true
6+
trim_trailing_whitespace = true
7+
charset = utf-8
8+
indent_style = space
9+
indent_size = 2

.eslintrc.json

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
{
2+
"root": true,
3+
"env": {
4+
"node": true,
5+
"es6": true
6+
},
7+
"parserOptions": {
8+
"ecmaVersion": 6
9+
},
10+
"rules": {
11+
// Possible errors
12+
"comma-dangle": ["error", "never"],
13+
"no-cond-assign": ["error", "except-parens"],
14+
"no-console": "error",
15+
"no-constant-condition": "off",
16+
"no-control-regex": "off",
17+
"no-debugger": "error",
18+
"no-dupe-args": "error",
19+
"no-dupe-keys": "error",
20+
"no-duplicate-case": "error",
21+
"no-empty": "error",
22+
"no-empty-character-class": "error",
23+
"no-ex-assign": "error",
24+
"no-extra-boolean-cast": "error",
25+
"no-extra-parens": ["error", "all", { "conditionalAssign": false, "nestedBinaryExpressions": false, "returnAssign": false }],
26+
"no-extra-semi": "error",
27+
"no-func-assign": "error",
28+
"no-inner-declarations": "off",
29+
"no-invalid-regexp": "error",
30+
"no-irregular-whitespace": "error",
31+
"no-negated-in-lhs": "error",
32+
"no-obj-calls": "error",
33+
"no-regex-spaces": "error",
34+
"no-sparse-arrays": "error",
35+
"no-unexpected-multiline": "error",
36+
"no-unreachable": "error",
37+
"no-unsafe-finally": "off",
38+
"use-isnan": "error",
39+
"valid-jsdoc": "off",
40+
"valid-typeof": "error",
41+
42+
// Best practices
43+
"accessor-pairs": "error",
44+
"array-callback-return": "error",
45+
"block-scoped-var": "off",
46+
"complexity": "off",
47+
"consistent-return": "error",
48+
"curly": ["error", "all"],
49+
"default-case": "off",
50+
"dot-location": ["error", "property"],
51+
"dot-notation": "error",
52+
"eqeqeq": "error",
53+
"guard-for-in": "off",
54+
"no-alert": "error",
55+
"no-caller": "error",
56+
"no-case-declarations": "error",
57+
"no-div-regex": "off",
58+
"no-else-return": "error",
59+
"no-empty-function": "error",
60+
"no-empty-pattern": "error",
61+
"no-eq-null": "error",
62+
"no-eval": "error",
63+
"no-extend-native": "error",
64+
"no-extra-bind": "error",
65+
"no-extra-label": "error",
66+
"no-fallthrough": "error",
67+
"no-floating-decimal": "error",
68+
"no-implicit-coercion": "error",
69+
"no-implicit-globals": "error",
70+
"no-implied-eval": "error",
71+
"no-invalid-this": "error",
72+
"no-iterator": "error",
73+
"no-labels": ["error", { "allowLoop": true }],
74+
"no-lone-blocks": "error",
75+
"no-loop-func": "off",
76+
"no-magic-numbers": "off",
77+
"no-multi-spaces": "error",
78+
"no-multi-str": "error",
79+
"no-native-reassign": "error",
80+
"no-new": "error",
81+
"no-new-func": "error",
82+
"no-new-wrappers": "error",
83+
"no-octal": "error",
84+
"no-octal-escape": "error",
85+
"no-param-reassign": "off",
86+
"no-process-env": "error",
87+
"no-proto": "error",
88+
"no-redeclare": "error",
89+
"no-return-assign": ["error", "except-parens"],
90+
"no-script-url": "off",
91+
"no-self-assign": "error",
92+
"no-self-compare": "error",
93+
"no-sequences": "error",
94+
"no-throw-literal": "error",
95+
"no-unmodified-loop-condition": "error",
96+
"no-unused-expressions": "error",
97+
"no-unused-labels": "error",
98+
"no-useless-call": "error",
99+
"no-useless-concat": "error",
100+
"no-useless-escape": "error",
101+
"no-void": "error",
102+
"no-warning-comments": "off",
103+
"no-with": "error",
104+
"radix": ["error", "as-needed"],
105+
"vars-on-top": "off",
106+
"wrap-iife": ["error", "outside"],
107+
"yoda": ["error", "never"],
108+
109+
// Strict Mode
110+
"strict": ["error", "global"],
111+
112+
// Variables
113+
"init-declarations": "off",
114+
"no-catch-shadow": "error",
115+
"no-delete-var": "error",
116+
"no-label-var": "error",
117+
"no-restricted-globals": "off",
118+
"no-shadow": "error",
119+
"no-shadow-restricted-names": "error",
120+
"no-undef": "error",
121+
"no-undef-init": "error",
122+
"no-undefined": "off",
123+
"no-unused-vars": "error",
124+
"no-use-before-define": ["error", "nofunc"],
125+
126+
// Node.js and CommonJS
127+
"callback-return": "off",
128+
"global-require": "error",
129+
"handle-callback-err": "error",
130+
"no-mixed-requires": ["error", true],
131+
"no-new-require": "error",
132+
"no-path-concat": "error",
133+
"no-process-exit": "error",
134+
"no-restricted-imports": "off",
135+
"no-restricted-modules": "off",
136+
"no-sync": "off",
137+
138+
// Stylistic Issues
139+
"array-bracket-spacing": ["error", "never"],
140+
"block-spacing": ["error", "always"],
141+
"brace-style": ["error", "1tbs", { "allowSingleLine": false }],
142+
"camelcase": ["error", { "properties": "always" }],
143+
"comma-spacing": ["error", { "before": false, "after": true }],
144+
"comma-style": ["error", "last"],
145+
"computed-property-spacing": ["error", "never"],
146+
"consistent-this": "off",
147+
"eol-last": "error",
148+
"func-names": "off",
149+
"func-style": ["error", "declaration"],
150+
"id-blacklist": "off",
151+
"id-length": "off",
152+
"id-match": "off",
153+
"indent": ["error", 2, { "SwitchCase": 1 }],
154+
"jsx-quotes": "off",
155+
"key-spacing": ["error", { "beforeColon": false, "afterColon": true, "mode": "strict" }],
156+
"keyword-spacing": ["error", { "before": true, "after": true }],
157+
"linebreak-style": ["error", "unix"],
158+
"lines-around-comment": "off",
159+
"max-depth": "off",
160+
"max-len": ["error", 120, { "ignoreUrls": true }],
161+
"max-nested-callbacks": "off",
162+
"max-params": "off",
163+
"max-statements": "off",
164+
"max-statements-per-line": ["error", { "max": 1 }],
165+
"new-cap": ["error", { "capIsNewExceptions": ["USVString"] }],
166+
"new-parens": "error",
167+
"newline-after-var": "off",
168+
"newline-before-return": "off",
169+
"newline-per-chained-call": "off",
170+
"no-array-constructor": "error",
171+
"no-bitwise": "off",
172+
"no-continue": "off",
173+
"no-inline-comments": "off",
174+
"no-lonely-if": "error",
175+
"no-mixed-spaces-and-tabs": "error",
176+
"no-multiple-empty-lines": "error",
177+
"no-negated-condition": "off",
178+
"no-nested-ternary": "error",
179+
"no-new-object": "error",
180+
"no-plusplus": "off",
181+
"no-restricted-syntax": "off",
182+
"no-spaced-func": "error",
183+
"no-ternary": "off",
184+
"no-trailing-spaces": "error",
185+
"no-underscore-dangle": "off",
186+
"no-unneeded-ternary": "error",
187+
"no-whitespace-before-property": "error",
188+
"object-curly-spacing": ["error", "always"],
189+
"object-property-newline": "off",
190+
"one-var": ["error", "never"],
191+
"one-var-declaration-per-line": ["error", "initializations"],
192+
"operator-assignment": ["error", "always"],
193+
"operator-linebreak": ["error", "after"],
194+
"padded-blocks": ["error", "never"],
195+
"quote-props": ["error", "as-needed"],
196+
"quotes": ["error", "double", { "avoidEscape": true, "allowTemplateLiterals": true }],
197+
"require-jsdoc": "off",
198+
"semi": ["error", "always"],
199+
"semi-spacing": "error",
200+
"sort-imports": "off",
201+
"sort-vars": "off",
202+
"space-before-blocks": ["error", "always"],
203+
"space-before-function-paren": ["error", { "anonymous": "always", "named": "never" }],
204+
"space-in-parens": ["error", "never"],
205+
"space-infix-ops": "error",
206+
"space-unary-ops": ["error", { "words": true, "nonwords": false }],
207+
"spaced-comment": ["error", "always", { "markers": ["///"] }],
208+
"wrap-regex": "off",
209+
210+
// ECMAScript 6
211+
"arrow-body-style": "off", // meh
212+
"arrow-parens": ["error", "as-needed"],
213+
"arrow-spacing": "error",
214+
"constructor-super": "error",
215+
"generator-star-spacing": ["error", "after"],
216+
"no-class-assign": "error",
217+
"no-confusing-arrow": "off",
218+
"no-const-assign": "error",
219+
"no-dupe-class-members": "error",
220+
"no-duplicate-imports": "error",
221+
"no-new-symbol": "error",
222+
"no-this-before-super": "error",
223+
"no-useless-computed-key": "error",
224+
"no-useless-constructor": "error",
225+
"no-var": "error",
226+
"object-shorthand": "error",
227+
"prefer-arrow-callback": "error",
228+
"prefer-const": ["error", { "ignoreReadBeforeAssign": true }],
229+
"prefer-reflect": "off",
230+
"prefer-rest-params": "off",
231+
"prefer-spread": "off", // TODO with new Node versions
232+
"prefer-template": "off",
233+
"require-yield": "error",
234+
"template-curly-spacing": ["error", "never"],
235+
"yield-star-spacing": ["error", "after"]
236+
}
237+
}

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/node_modules/
2+
/npm-debug.log
3+
4+
/lib/labels-to-names.json

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js:
3+
- stable
4+
script:
5+
npm run lint && npm test

LICENSE.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright © 2016 Domenic Denicola <[email protected]>
2+
3+
This work is free. You can redistribute it and/or modify it under the
4+
terms of the Do What The Fuck You Want To Public License, Version 2,
5+
as published by Sam Hocevar. See below for more details.
6+
7+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
8+
Version 2, December 2004
9+
10+
Copyright (C) 2004 Sam Hocevar <[email protected]>
11+
12+
Everyone is permitted to copy and distribute verbatim or modified
13+
copies of this license document, and changing it is allowed as long
14+
as the name is changed.
15+
16+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
17+
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
18+
19+
0. You just DO WHAT THE FUCK YOU WANT TO.

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Determine the Encoding of a HTML Byte Stream
2+
3+
This package implements the HTML Standard's [encoding sniffing algorithm](https://html.spec.whatwg.org/multipage/syntax.html#encoding-sniffing-algorithm) in all its glory. The most interesting part of this is how it pre-scans the first 1024 bytes in order to search for certain `<meta charset>`-related patterns.
4+
5+
```js
6+
const htmlEncodingSniffer = require("html-encoding-sniffer");
7+
const fs = require("fs");
8+
9+
const htmlBuffer = fs.readFileSync("./html-page.html");
10+
const sniffedEncoding = htmlEncodingSniffer(htmlBuffer);
11+
```
12+
13+
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 [whatwg-encoding](https://github.com/jsdom/whatwg-encoding) package to decode the result:
14+
15+
```js
16+
const whatwgEncoding = require("whatwg-encoding");
17+
const htmlString = whatwgEncoding.decode(htmlBuffer, sniffedEncoding);
18+
```
19+
20+
## Options
21+
22+
You can pass two potential options to `htmlEncodingSniffer`:
23+
24+
```js
25+
const sniffedEncoding = htmlEncodingSniffer(htmlBuffer, {
26+
transportLayerEncodingLabel,
27+
defaultEncoding
28+
});
29+
```
30+
31+
These represent two possible inputs into the [encoding sniffing algorithm](https://html.spec.whatwg.org/multipage/syntax.html#encoding-sniffing-algorithm):
32+
33+
- `transportLayerEncodingLabel` is an encoding label that is obtained from the "transport layer" (probably a HTTP `Content-Type` header), which overrides everything but a BOM.
34+
- `defaultEncoding` is the ultimate fallback encoding used if no valid encoding is supplied by the transport layer, and no encoding is sniffed from the bytes. It defaults to `"windows-1252"`, as recommended by the algorithm's table of suggested defaults for "All other locales" (including the `en` locale).
35+
36+
## Credits
37+
38+
This package was originally based on the excellent work of [@nicolashenry](https://github.com/nicolashenry), [in jsdom](https://github.com/tmpvar/jsdom/blob/16fd85618f2705d181232f6552125872a37164bc/lib/jsdom/living/helpers/encoding.js). It has since been pulled out into this separate package.

0 commit comments

Comments
 (0)