{"_id":"he@2.0.0","bin":{"he":"bin/he.mjs"},"man":["man/he.1"],"bugs":{"url":"https://github.com/mathiasbynens/he/issues"},"dist":{"shasum":"fcbb797908ada37e168cac3ec27ce8da975ff082","tarball":"http://registry-npm.ywwl.com/he/download/he-2.0.0.tgz","fileCount":7,"integrity":"sha512-0jE+JPv08nhXWmI1dULybzqNt34RbkH6Ul6V4cmDONpjYx1SfyObKF38FJLaJ1K7WWD93LWL9lewzk4E1Ria4A==","signatures":[{"sig":"MEQCIELkLhZkdnfI5QQBYAhDJbcaM6UFkSIobTXhC98Sw9i+AiAsVUgqPsWFxC2NnW7CuRSNr9fi/u91drBRFic12zmqCQ==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"},{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIB7dRfRKzv3aV4ZNQr9M/0/cDzn5QPyRoTqMyW+A2DQbAiEAoeLm8LI3TCecBFyW99dtqy7+FcWMUfPfK9oVo7vdWGk="}],"attestations":{"url":"https://registry.npmjs.org/-/npm/v1/attestations/he@2.0.0","provenance":{"predicateType":"https://slsa.dev/provenance/v1"}},"unpackedSize":141567,"size":40835,"noattachment":false,"key":"/he/-/he-2.0.0.tgz"},"name":"he","type":"module","author":{"url":"https://mathiasbynens.be/","name":"Mathias Bynens"},"engines":{"node":">=22"},"exports":{".":"./src/he.mjs","./package.json":"./package.json"},"gitHead":"e5658eccfecf6dfccee8797ca5126a3f22855609","license":"MIT","scripts":{"test":"node --test \"tests/**/*.test.mjs\"","build":"node scripts/process-data.mjs && node scripts/build.mjs && oxfmt data src/data.mjs","fetch":"npm run fetch:entities && node scripts/fetch-spec.mjs && npm run build","format":"oxfmt","coverage":"node --test --experimental-test-coverage --test-coverage-include=\"src/**\" \"tests/**/*.test.mjs\"","format:check":"oxfmt --check","fetch:entities":"curl --fail --silent --show-error --location https://html.spec.whatwg.org/entities.json --output data/entities.json && oxfmt data/entities.json"},"version":"2.0.0","_npmUser":{"name":"GitHub Actions","email":"npm-oidc-no-reply@github.com","trustedPublisher":{"id":"github","oidcConfigId":"562104c6-b412-45cc-9eef-342fdf72b521"}},"homepage":"https://mths.be/he","keywords":["decode","encode","entities","entity","html","string","unicode"],"repository":{"url":"git+https://github.com/mathiasbynens/he.git","type":"git"},"_npmVersion":"11.19.1","description":"A robust HTML entities encoder/decoder with full Unicode support.","directories":{},"maintainers":[{"name":"mathias","email":"mathias@qiwi.be"},{"name":"google-wombot","email":"node-team-npm+wombot@google.com"}],"_nodeVersion":"26.10.0","_hasShrinkwrap":false,"devDependencies":{"jsesc":"^3.1.0","oxfmt":"^0.71.0","linkedom":"^0.18.13","regenerate":"^1.4.2"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/he_2.0.0_1791112790040_0.5648783317639503"},"readme":"# he [![Test status](https://github.com/mathiasbynens/he/actions/workflows/test.yml/badge.svg)](https://github.com/mathiasbynens/he/actions/workflows/test.yml) [![he on npm](https://img.shields.io/npm/v/he)](https://www.npmjs.com/package/he)\n\n_he_ (for “HTML entities”) is a robust HTML entity encoder/decoder written in JavaScript. It supports [all standardized named character references as per HTML](https://html.spec.whatwg.org/multipage/syntax.html#named-character-references), handles [ambiguous ampersands](https://mathiasbynens.be/notes/ambiguous-ampersands) and other edge cases [just like a browser would](https://html.spec.whatwg.org/multipage/syntax.html#tokenizing-character-references), has an extensive test suite, and — contrary to many other JavaScript solutions — _he_ handles astral Unicode symbols just fine. [An online demo is available.](https://mothereff.in/html-entities)\n\n## Installation\n\nVia [npm](https://www.npmjs.com/):\n\n```bash\nnpm install he\n```\n\n_he_ is published as an ECMAScript module with named exports:\n\n```js\nimport { decode, encode, escape, unescape } from 'he';\n// Or, to use the `he.*` style shown in the examples below:\nimport * as he from 'he';\n```\n\nIn a browser, using a `<script type=\"module\">` and a path to the package’s `src/he.mjs`:\n\n```html\n<script type=\"module\">\n\timport * as he from './node_modules/he/src/he.mjs';\n</script>\n```\n\nIn CommonJS code on Node.js versions that support [`require(esm)`](https://nodejs.org/api/modules.html#loading-ecmascript-modules-using-require), `require('he')` returns the module namespace object, so `require('he').encode(…)` works as well.\n\n## API\n\n### `he.encode(text, options)`\n\nThis function takes a string of text and encodes (by default) any symbols that aren’t printable ASCII symbols and `&`, `<`, `>`, `\"`, `'`, and `` ` ``, replacing them with character references.\n\n```js\nhe.encode('foo © bar ≠ baz 𝌆 qux');\n// → 'foo &#xA9; bar &#x2260; baz &#x1D306; qux'\n```\n\nAs long as the input string contains [allowed code points](https://html.spec.whatwg.org/multipage/parsing.html#preprocessing-the-input-stream) only, the return value of this function is always valid HTML. Any [(invalid) code points that cannot be represented using a character reference](https://html.spec.whatwg.org/multipage/syntax.html#table-charref-overrides) in the input are not encoded:\n\n```js\nhe.encode('foo \\0 bar');\n// → 'foo \\0 bar'\n```\n\nHowever, enabling [the `strict` option](https://github.com/mathiasbynens/he#strict) causes invalid code points to throw an exception. With `strict` enabled, `he.encode` either throws (if the input contains invalid code points) or returns a string of valid HTML.\n\nThe `options` object is optional. It recognizes the following properties:\n\n#### `useNamedReferences`\n\nThe default value for the `useNamedReferences` option is `false`. This means that `encode()` will not use any named character references (e.g. `&copy;`) in the output — hexadecimal escapes (e.g. `&#xA9;`) will be used instead. Set it to `true` to enable the use of named references.\n\n**Note that if compatibility with older browsers is a concern, this option should remain disabled.**\n\n```js\n// Using the global default setting (defaults to `false`):\nhe.encode('foo © bar ≠ baz 𝌆 qux');\n// → 'foo &#xA9; bar &#x2260; baz &#x1D306; qux'\n\n// Passing an `options` object to `encode`, to explicitly disallow named references:\nhe.encode('foo © bar ≠ baz 𝌆 qux', {\n\tuseNamedReferences: false,\n});\n// → 'foo &#xA9; bar &#x2260; baz &#x1D306; qux'\n\n// Passing an `options` object to `encode`, to explicitly allow named references:\nhe.encode('foo © bar ≠ baz 𝌆 qux', {\n\tuseNamedReferences: true,\n});\n// → 'foo &copy; bar &ne; baz &#x1D306; qux'\n```\n\n#### `decimal`\n\nThe default value for the `decimal` option is `false`. If the option is enabled, `encode` will generally use decimal escapes (e.g. `&#169;`) rather than hexadecimal escapes (e.g. `&#xA9;`). Beside of this replacement, the basic behavior remains the same when combined with other options. For example: if both options `useNamedReferences` and `decimal` are enabled, named references (e.g. `&copy;`) are used over decimal escapes. HTML entities without a named reference are encoded using decimal escapes.\n\n```js\n// Using the global default setting (defaults to `false`):\nhe.encode('foo © bar ≠ baz 𝌆 qux');\n// → 'foo &#xA9; bar &#x2260; baz &#x1D306; qux'\n\n// Passing an `options` object to `encode`, to explicitly disable decimal escapes:\nhe.encode('foo © bar ≠ baz 𝌆 qux', {\n\tdecimal: false,\n});\n// → 'foo &#xA9; bar &#x2260; baz &#x1D306; qux'\n\n// Passing an `options` object to `encode`, to explicitly enable decimal escapes:\nhe.encode('foo © bar ≠ baz 𝌆 qux', {\n\tdecimal: true,\n});\n// → 'foo &#169; bar &#8800; baz &#119558; qux'\n\n// Passing an `options` object to `encode`, to explicitly allow named references and decimal escapes:\nhe.encode('foo © bar ≠ baz 𝌆 qux', {\n\tuseNamedReferences: true,\n\tdecimal: true,\n});\n// → 'foo &copy; bar &ne; baz &#119558; qux'\n```\n\n#### `encodeEverything`\n\nThe default value for the `encodeEverything` option is `false`. This means that `encode()` will not use any character references for printable ASCII symbols that don’t need escaping. Set it to `true` to encode every symbol in the input string. When set to `true`, this option takes precedence over `allowUnsafeSymbols` (i.e. setting the latter to `true` in such a case has no effect).\n\n```js\n// Using the global default setting (defaults to `false`):\nhe.encode('foo © bar ≠ baz 𝌆 qux');\n// → 'foo &#xA9; bar &#x2260; baz &#x1D306; qux'\n\n// Passing an `options` object to `encode`, to explicitly encode all symbols:\nhe.encode('foo © bar ≠ baz 𝌆 qux', {\n\tencodeEverything: true,\n});\n// → '&#x66;&#x6F;&#x6F;&#x20;&#xA9;&#x20;&#x62;&#x61;&#x72;&#x20;&#x2260;&#x20;&#x62;&#x61;&#x7A;&#x20;&#x1D306;&#x20;&#x71;&#x75;&#x78;'\n\n// This setting can be combined with the `useNamedReferences` option:\nhe.encode('foo © bar ≠ baz 𝌆 qux', {\n\tencodeEverything: true,\n\tuseNamedReferences: true,\n});\n// → '&#x66;&#x6F;&#x6F;&#x20;&copy;&#x20;&#x62;&#x61;&#x72;&#x20;&ne;&#x20;&#x62;&#x61;&#x7A;&#x20;&#x1D306;&#x20;&#x71;&#x75;&#x78;'\n```\n\n#### `strict`\n\nThe default value for the `strict` option is `false`. This means that `encode()` will encode any HTML text content you feed it, even if it contains any symbols that cause [parse errors](https://html.spec.whatwg.org/multipage/parsing.html#preprocessing-the-input-stream). To throw an error when such invalid HTML is encountered, set the `strict` option to `true`. This option makes it possible to use _he_ as part of HTML parsers and HTML validators.\n\n```js\n// Using the global default setting (defaults to `false`, i.e. error-tolerant mode):\nhe.encode('\\x01');\n// → '&#x1;'\n\n// Passing an `options` object to `encode`, to explicitly enable error-tolerant mode:\nhe.encode('\\x01', {\n\tstrict: false,\n});\n// → '&#x1;'\n\n// Passing an `options` object to `encode`, to explicitly enable strict mode:\nhe.encode('\\x01', {\n\tstrict: true,\n});\n// → Parse error\n```\n\n#### `allowUnsafeSymbols`\n\nThe default value for the `allowUnsafeSymbols` option is `false`. This means that characters that are unsafe for use in HTML content (`&`, `<`, `>`, `\"`, `'`, and `` ` ``) will be encoded. When set to `true`, only non-ASCII characters will be encoded. If the `encodeEverything` option is set to `true`, this option will be ignored.\n\n```js\nhe.encode('foo © and & ampersand', {\n\tallowUnsafeSymbols: true,\n});\n// → 'foo &#xA9; and & ampersand'\n```\n\n#### Overriding default `encode` options globally\n\nThe global default setting can be overridden by modifying the `he.encode.options` object. This saves you from passing in an `options` object for every call to `encode` if you want to use the non-default setting.\n\n```js\n// Read the global default setting:\nhe.encode.options.useNamedReferences;\n// → `false` by default\n\n// Override the global default setting:\nhe.encode.options.useNamedReferences = true;\n\n// Using the global default setting, which is now `true`:\nhe.encode('foo © bar ≠ baz 𝌆 qux');\n// → 'foo &copy; bar &ne; baz &#x1D306; qux'\n```\n\n### `he.decode(html, options)`\n\nThis function takes a string of HTML and decodes any named and numerical character references in it using [the algorithm described in section 12.2.4.69 of the HTML spec](https://html.spec.whatwg.org/multipage/syntax.html#tokenizing-character-references).\n\n```js\nhe.decode('foo &copy; bar &ne; baz &#x1D306; qux');\n// → 'foo © bar ≠ baz 𝌆 qux'\n```\n\nThe `options` object is optional. It recognizes the following properties:\n\n#### `isAttributeValue`\n\nThe default value for the `isAttributeValue` option is `false`. This means that `decode()` will decode the string as if it were used in [a text context in an HTML document](https://html.spec.whatwg.org/multipage/syntax.html#data-state). HTML has different rules for [parsing character references in attribute values](https://html.spec.whatwg.org/multipage/syntax.html#character-reference-in-attribute-value-state) — set this option to `true` to treat the input string as if it were used as an attribute value.\n\n```js\n// Using the global default setting (defaults to `false`, i.e. HTML text context):\nhe.decode('foo&ampbar');\n// → 'foo&bar'\n\n// Passing an `options` object to `decode`, to explicitly assume an HTML text context:\nhe.decode('foo&ampbar', {\n\tisAttributeValue: false,\n});\n// → 'foo&bar'\n\n// Passing an `options` object to `decode`, to explicitly assume an HTML attribute value context:\nhe.decode('foo&ampbar', {\n\tisAttributeValue: true,\n});\n// → 'foo&ampbar'\n```\n\n#### `strict`\n\nThe default value for the `strict` option is `false`. This means that `decode()` will decode any HTML text content you feed it, even if it contains any entities that cause [parse errors](https://html.spec.whatwg.org/multipage/syntax.html#tokenizing-character-references). To throw an error when such invalid HTML is encountered, set the `strict` option to `true`. This option makes it possible to use _he_ as part of HTML parsers and HTML validators.\n\n```js\n// Using the global default setting (defaults to `false`, i.e. error-tolerant mode):\nhe.decode('foo&ampbar');\n// → 'foo&bar'\n\n// Passing an `options` object to `decode`, to explicitly enable error-tolerant mode:\nhe.decode('foo&ampbar', {\n\tstrict: false,\n});\n// → 'foo&bar'\n\n// Passing an `options` object to `decode`, to explicitly enable strict mode:\nhe.decode('foo&ampbar', {\n\tstrict: true,\n});\n// → Parse error\n```\n\n#### Overriding default `decode` options globally\n\nThe global default settings for the `decode` function can be overridden by modifying the `he.decode.options` object. This saves you from passing in an `options` object for every call to `decode` if you want to use a non-default setting.\n\n```js\n// Read the global default setting:\nhe.decode.options.isAttributeValue;\n// → `false` by default\n\n// Override the global default setting:\nhe.decode.options.isAttributeValue = true;\n\n// Using the global default setting, which is now `true`:\nhe.decode('foo&ampbar');\n// → 'foo&ampbar'\n```\n\n### `he.escape(text)`\n\nThis function takes a string of text and escapes it for use in text contexts in XML or HTML documents. Only the following characters are escaped: `&`, `<`, `>`, `\"`, `'`, and `` ` ``.\n\n```js\nhe.escape('<img src=\\'x\\' onerror=\"prompt(1)\">');\n// → '&lt;img src=&#x27;x&#x27; onerror=&quot;prompt(1)&quot;&gt;'\n```\n\n### `he.unescape(html, options)`\n\n`he.unescape` is an alias for `he.decode`. It takes a string of HTML and decodes any named and numerical character references in it.\n\n### Using the `he` binary\n\nTo use the `he` binary in your shell, simply install _he_ globally using npm:\n\n```bash\nnpm install -g he\n```\n\nAfter that you will be able to encode/decode HTML entities from the command line:\n\n```bash\n$ he --encode 'föo ♥ bår 𝌆 baz'\nf&#xF6;o &#x2665; b&#xE5;r &#x1D306; baz\n\n$ he --encode --use-named-refs 'föo ♥ bår 𝌆 baz'\nf&ouml;o &hearts; b&aring;r &#x1D306; baz\n\n$ he --decode 'f&ouml;o &hearts; b&aring;r &#x1D306; baz'\nföo ♥ bår 𝌆 baz\n```\n\nRead a local text file, encode it for use in an HTML text context, and save the result to a new file:\n\n```bash\n$ he --encode < foo.txt > foo-escaped.html\n```\n\nOr do the same with an online text file:\n\n```bash\n$ curl -sL \"http://git.io/HnfEaw\" | he --encode > escaped.html\n```\n\nOr, the opposite — read a local file containing a snippet of HTML in a text context, decode it back to plain text, and save the result to a new file:\n\n```bash\n$ he --decode < foo-escaped.html > foo.txt\n```\n\nOr do the same with an online HTML snippet:\n\n```bash\n$ curl -sL \"http://git.io/HnfEaw\" | he --decode > decoded.txt\n```\n\nSee `he --help` for the full list of options.\n\n## Support\n\n_he_ is an ES2022 module. It works in all modern browsers and in Node.js v22+. The CLI requires Node.js v22+.\n\n## Development\n\nAfter cloning this repository, run `npm install` to install the development dependencies.\n\n- `npm test` runs the test suite using the built-in Node.js test runner.\n- `npm run coverage` prints a code coverage report.\n- `npm run build` regenerates `data/*.json` from `data/entities.json` and writes the generated `src/data.mjs` module, which `src/he.mjs` imports.\n- `npm run fetch` downloads the latest `entities.json`, derives the invalid code points and character reference overrides from the HTML and Infra Standards, and rebuilds. If the relevant spec prose changes, `scripts/fetch-spec.mjs` fails with an explanation instead of writing data.\n- `npm run format` formats the code using [Oxfmt](https://oxc.rs/docs/guide/usage/formatter); `npm run format:check` verifies formatting.\n\n## Acknowledgements\n\nThanks to [Simon Pieters](https://simon.html5.org/) ([@zcorpan](https://twitter.com/zcorpan)) for the many suggestions.\n\n## Author\n\n| [![twitter/mathias](https://gravatar.com/avatar/24e08a9ea84deb17ae121074d0f17125?s=70)](https://twitter.com/mathias 'Follow @mathias on Twitter') |\n| ------------------------------------------------------------------------------------------------------------------------------------------------- |\n| [Mathias Bynens](https://mathiasbynens.be/)                                                                                                       |\n\n## License\n\n_he_ is available under the [MIT](https://mths.be/mit) license.\n","publish_time":1791112790120,"_cnpm_publish_time":1791112790120,"dist-tags":{"latest":"2.0.0"}}