mirror of
https://github.com/azure/login.git
synced 2026-06-06 19:47:08 +00:00
* Bump lodash from 4.17.15 to 4.17.19 (#52) Bumps [lodash](https://github.com/lodash/lodash) from 4.17.15 to 4.17.19. - [Release notes](https://github.com/lodash/lodash/releases) - [Commits](https://github.com/lodash/lodash/compare/4.17.15...4.17.19) Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Amruta Kawade <65217380+AmrutaKawade@users.noreply.github.com> * Bump @actions/core from 1.1.3 to 1.2.6 (#60) Bumps [@actions/core](https://github.com/actions/toolkit/tree/HEAD/packages/core) from 1.1.3 to 1.2.6. - [Release notes](https://github.com/actions/toolkit/releases) - [Changelog](https://github.com/actions/toolkit/blob/main/packages/core/RELEASES.md) - [Commits](https://github.com/actions/toolkit/commits/HEAD/packages/core) Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Amruta Kawade <65217380+AmrutaKawade@users.noreply.github.com> * updating node_nodules * updated package-lock Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
38 lines
2.1 KiB
Markdown
38 lines
2.1 KiB
Markdown
# Determine the Encoding of a HTML Byte Stream
|
|
|
|
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.
|
|
|
|
```js
|
|
const htmlEncodingSniffer = require("html-encoding-sniffer");
|
|
const fs = require("fs");
|
|
|
|
const htmlBuffer = fs.readFileSync("./html-page.html");
|
|
const sniffedEncoding = htmlEncodingSniffer(htmlBuffer);
|
|
```
|
|
|
|
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:
|
|
|
|
```js
|
|
const whatwgEncoding = require("whatwg-encoding");
|
|
const htmlString = whatwgEncoding.decode(htmlBuffer, sniffedEncoding);
|
|
```
|
|
|
|
## Options
|
|
|
|
You can pass two potential options to `htmlEncodingSniffer`:
|
|
|
|
```js
|
|
const sniffedEncoding = htmlEncodingSniffer(htmlBuffer, {
|
|
transportLayerEncodingLabel,
|
|
defaultEncoding
|
|
});
|
|
```
|
|
|
|
These represent two possible inputs into the [encoding sniffing algorithm](https://html.spec.whatwg.org/multipage/syntax.html#encoding-sniffing-algorithm):
|
|
|
|
- `transportLayerEncodingLabel` is an encoding label that is obtained from the "transport layer" (probably a HTTP `Content-Type` header), which overrides everything but a BOM.
|
|
- `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).
|
|
|
|
## Credits
|
|
|
|
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.
|