Gathering detailed insights and metrics for he
Gathering detailed insights and metrics for he
Gathering detailed insights and metrics for he
Gathering detailed insights and metrics for he
@types/he
TypeScript definitions for he
activity-detector
Detects when a user is really using your page and when he is idle
cytoscape-avsdf
An implementation of the circular layout algorithm by He & Sykora
@codingame/esbuild-import-meta-url-plugin
Esbuild plugin allowing to use t he `new URL(..., import.meta.url)` syntax
A robust HTML entity encoder/decoder written in JavaScript.
npm install he
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
3,457 Stars
140 Commits
254 Forks
63 Watching
2 Branches
7 Contributors
Updated on 22 Nov 2024
Minified
Minified + Gzipped
JavaScript (99.21%)
HTML (0.79%)
Cumulative downloads
Total Downloads
Last day
-3.7%
4,413,838
Compared to previous day
Last week
2.2%
23,826,841
Compared to previous week
Last month
13.3%
98,977,924
Compared to previous month
Last year
9.5%
1,030,525,194
Compared to previous year
he (for “HTML entities”) is a robust HTML entity encoder/decoder written in JavaScript. It supports all standardized named character references as per HTML, handles ambiguous ampersands and other edge cases just like a browser would, has an extensive test suite, and — contrary to many other JavaScript solutions — he handles astral Unicode symbols just fine. An online demo is available.
Via npm:
1npm install he
Via Bower:
1bower install he
Via Component:
1component install mathiasbynens/he
In a browser:
1<script src="he.js"></script>
In Node.js, io.js, Narwhal, and RingoJS:
1var he = require('he');
In Rhino:
1load('he.js');
Using an AMD loader like RequireJS:
1require( 2 { 3 'paths': { 4 'he': 'path/to/he' 5 } 6 }, 7 ['he'], 8 function(he) { 9 console.log(he); 10 } 11);
he.version
A string representing the semantic version number.
he.encode(text, options)
This 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.
1he.encode('foo © bar ≠ baz 𝌆 qux'); 2// → 'foo © bar ≠ baz 𝌆 qux'
As long as the input string contains allowed code points only, the return value of this function is always valid HTML. Any (invalid) code points that cannot be represented using a character reference in the input are not encoded:
1he.encode('foo \0 bar'); 2// → 'foo \0 bar'
However, enabling the strict
option 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.
The options
object is optional. It recognizes the following properties:
useNamedReferences
The default value for the useNamedReferences
option is false
. This means that encode()
will not use any named character references (e.g. ©
) in the output — hexadecimal escapes (e.g. ©
) will be used instead. Set it to true
to enable the use of named references.
Note that if compatibility with older browsers is a concern, this option should remain disabled.
1// Using the global default setting (defaults to `false`): 2he.encode('foo © bar ≠ baz 𝌆 qux'); 3// → 'foo © bar ≠ baz 𝌆 qux' 4 5// Passing an `options` object to `encode`, to explicitly disallow named references: 6he.encode('foo © bar ≠ baz 𝌆 qux', { 7 'useNamedReferences': false 8}); 9// → 'foo © bar ≠ baz 𝌆 qux' 10 11// Passing an `options` object to `encode`, to explicitly allow named references: 12he.encode('foo © bar ≠ baz 𝌆 qux', { 13 'useNamedReferences': true 14}); 15// → 'foo © bar ≠ baz 𝌆 qux'
decimal
The default value for the decimal
option is false
. If the option is enabled, encode
will generally use decimal escapes (e.g. ©
) rather than hexadecimal escapes (e.g. ©
). 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. ©
) are used over decimal escapes. HTML entities without a named reference are encoded using decimal escapes.
1// Using the global default setting (defaults to `false`): 2he.encode('foo © bar ≠ baz 𝌆 qux'); 3// → 'foo © bar ≠ baz 𝌆 qux' 4 5// Passing an `options` object to `encode`, to explicitly disable decimal escapes: 6he.encode('foo © bar ≠ baz 𝌆 qux', { 7 'decimal': false 8}); 9// → 'foo © bar ≠ baz 𝌆 qux' 10 11// Passing an `options` object to `encode`, to explicitly enable decimal escapes: 12he.encode('foo © bar ≠ baz 𝌆 qux', { 13 'decimal': true 14}); 15// → 'foo © bar ≠ baz 𝌆 qux' 16 17// Passing an `options` object to `encode`, to explicitly allow named references and decimal escapes: 18he.encode('foo © bar ≠ baz 𝌆 qux', { 19 'useNamedReferences': true, 20 'decimal': true 21}); 22// → 'foo © bar ≠ baz 𝌆 qux'
encodeEverything
The 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).
1// Using the global default setting (defaults to `false`): 2he.encode('foo © bar ≠ baz 𝌆 qux'); 3// → 'foo © bar ≠ baz 𝌆 qux' 4 5// Passing an `options` object to `encode`, to explicitly encode all symbols: 6he.encode('foo © bar ≠ baz 𝌆 qux', { 7 'encodeEverything': true 8}); 9// → 'foo © bar ≠ baz 𝌆 qux' 10 11// This setting can be combined with the `useNamedReferences` option: 12he.encode('foo © bar ≠ baz 𝌆 qux', { 13 'encodeEverything': true, 14 'useNamedReferences': true 15}); 16// → 'foo © bar ≠ baz 𝌆 qux'
strict
The 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. 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.
1// Using the global default setting (defaults to `false`, i.e. error-tolerant mode): 2he.encode('\x01'); 3// → '' 4 5// Passing an `options` object to `encode`, to explicitly enable error-tolerant mode: 6he.encode('\x01', { 7 'strict': false 8}); 9// → '' 10 11// Passing an `options` object to `encode`, to explicitly enable strict mode: 12he.encode('\x01', { 13 'strict': true 14}); 15// → Parse error
allowUnsafeSymbols
The 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.
1he.encode('foo © and & ampersand', { 2 'allowUnsafeSymbols': true 3}); 4// → 'foo © and & ampersand'
encode
options globallyThe 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.
1// Read the global default setting: 2he.encode.options.useNamedReferences; 3// → `false` by default 4 5// Override the global default setting: 6he.encode.options.useNamedReferences = true; 7 8// Using the global default setting, which is now `true`: 9he.encode('foo © bar ≠ baz 𝌆 qux'); 10// → 'foo © bar ≠ baz 𝌆 qux'
he.decode(html, options)
This 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.
1he.decode('foo © bar ≠ baz 𝌆 qux'); 2// → 'foo © bar ≠ baz 𝌆 qux'
The options
object is optional. It recognizes the following properties:
isAttributeValue
The 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. HTML has different rules for parsing character references in attribute values — set this option to true
to treat the input string as if it were used as an attribute value.
1// Using the global default setting (defaults to `false`, i.e. HTML text context): 2he.decode('foo&bar'); 3// → 'foo&bar' 4 5// Passing an `options` object to `decode`, to explicitly assume an HTML text context: 6he.decode('foo&bar', { 7 'isAttributeValue': false 8}); 9// → 'foo&bar' 10 11// Passing an `options` object to `decode`, to explicitly assume an HTML attribute value context: 12he.decode('foo&bar', { 13 'isAttributeValue': true 14}); 15// → 'foo&bar'
strict
The 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. 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.
1// Using the global default setting (defaults to `false`, i.e. error-tolerant mode): 2he.decode('foo&bar'); 3// → 'foo&bar' 4 5// Passing an `options` object to `decode`, to explicitly enable error-tolerant mode: 6he.decode('foo&bar', { 7 'strict': false 8}); 9// → 'foo&bar' 10 11// Passing an `options` object to `decode`, to explicitly enable strict mode: 12he.decode('foo&bar', { 13 'strict': true 14}); 15// → Parse error
decode
options globallyThe 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.
1// Read the global default setting: 2he.decode.options.isAttributeValue; 3// → `false` by default 4 5// Override the global default setting: 6he.decode.options.isAttributeValue = true; 7 8// Using the global default setting, which is now `true`: 9he.decode('foo&bar'); 10// → 'foo&bar'
he.escape(text)
This 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 `
.
1he.escape('<img src=\'x\' onerror="prompt(1)">'); 2// → '<img src='x' onerror="prompt(1)">'
he.unescape(html, options)
he.unescape
is an alias for he.decode
. It takes a string of HTML and decodes any named and numerical character references in it.
he
binaryTo use the he
binary in your shell, simply install he globally using npm:
1npm install -g he
After that you will be able to encode/decode HTML entities from the command line:
1$ he --encode 'föo ♥ bår 𝌆 baz' 2föo ♥ bår 𝌆 baz 3 4$ he --encode --use-named-refs 'föo ♥ bår 𝌆 baz' 5föo ♥ bår 𝌆 baz 6 7$ he --decode 'föo ♥ bår 𝌆 baz' 8föo ♥ bår 𝌆 baz
Read a local text file, encode it for use in an HTML text context, and save the result to a new file:
1$ he --encode < foo.txt > foo-escaped.html
Or do the same with an online text file:
1$ curl -sL "http://git.io/HnfEaw" | he --encode > escaped.html
Or, 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:
1$ he --decode < foo-escaped.html > foo.txt
Or do the same with an online HTML snippet:
1$ curl -sL "http://git.io/HnfEaw" | he --decode > decoded.txt
See he --help
for the full list of options.
he has been tested in at least:
After cloning this repository, run npm install
to install the dependencies needed for he development and testing. You may want to install Istanbul globally using npm install istanbul -g
.
Once that’s done, you can run the unit tests in Node using npm test
or node tests/tests.js
. To run the tests in Rhino, Ringo, Narwhal, and web browsers as well, use grunt test
.
To generate the code coverage report, use grunt cover
.
Thanks to Simon Pieters (@zcorpan) for the many suggestions.
Mathias Bynens |
he is available under the MIT license.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 1/30 approved changesets -- score normalized to 0
Reason
0 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More