UNPKG

14.2 kBMarkdownView Raw
1# he [![Build status](https://travis-ci.org/mathiasbynens/he.svg?branch=master)](https://travis-ci.org/mathiasbynens/he) [![Code coverage status](https://codecov.io/github/mathiasbynens/he/coverage.svg?branch=master)](https://codecov.io/github/mathiasbynens/he?branch=master) [![Dependency status](https://gemnasium.com/mathiasbynens/he.svg)](https://gemnasium.com/mathiasbynens/he)
2
3_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)
4
5## Installation
6
7Via [npm](https://www.npmjs.com/):
8
9```bash
10npm install he
11```
12
13Via [Bower](http://bower.io/):
14
15```bash
16bower install he
17```
18
19Via [Component](https://github.com/component/component):
20
21```bash
22component install mathiasbynens/he
23```
24
25In a browser:
26
27```html
28<script src="he.js"></script>
29```
30
31In [Node.js](https://nodejs.org/), [io.js](https://iojs.org/), [Narwhal](http://narwhaljs.org/), and [RingoJS](http://ringojs.org/):
32
33```js
34var he = require('he');
35```
36
37In [Rhino](http://www.mozilla.org/rhino/):
38
39```js
40load('he.js');
41```
42
43Using an AMD loader like [RequireJS](http://requirejs.org/):
44
45```js
46require(
47 {
48 'paths': {
49 'he': 'path/to/he'
50 }
51 },
52 ['he'],
53 function(he) {
54 console.log(he);
55 }
56);
57```
58
59## API
60
61### `he.version`
62
63A string representing the semantic version number.
64
65### `he.encode(text, options)`
66
67This 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.
68
69```js
70he.encode('foo © bar ≠ baz 𝌆 qux');
71// → 'foo &#xA9; bar &#x2260; baz &#x1D306; qux'
72```
73
74As 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:
75
76```js
77he.encode('foo \0 bar');
78// → 'foo \0 bar'
79```
80
81However, 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.
82
83The `options` object is optional. It recognizes the following properties:
84
85#### `useNamedReferences`
86
87The 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.
88
89**Note that if compatibility with older browsers is a concern, this option should remain disabled.**
90
91```js
92// Using the global default setting (defaults to `false`):
93he.encode('foo © bar ≠ baz 𝌆 qux');
94// → 'foo &#xA9; bar &#x2260; baz &#x1D306; qux'
95
96// Passing an `options` object to `encode`, to explicitly disallow named references:
97he.encode('foo © bar ≠ baz 𝌆 qux', {
98 'useNamedReferences': false
99});
100// → 'foo &#xA9; bar &#x2260; baz &#x1D306; qux'
101
102// Passing an `options` object to `encode`, to explicitly allow named references:
103he.encode('foo © bar ≠ baz 𝌆 qux', {
104 'useNamedReferences': true
105});
106// → 'foo &copy; bar &ne; baz &#x1D306; qux'
107```
108
109#### `decimal`
110
111The 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.
112
113```js
114// Using the global default setting (defaults to `false`):
115he.encode('foo © bar ≠ baz 𝌆 qux');
116// → 'foo &#xA9; bar &#x2260; baz &#x1D306; qux'
117
118// Passing an `options` object to `encode`, to explicitly disable decimal escapes:
119he.encode('foo © bar ≠ baz 𝌆 qux', {
120 'decimal': false
121});
122// → 'foo &#xA9; bar &#x2260; baz &#x1D306; qux'
123
124// Passing an `options` object to `encode`, to explicitly enable decimal escapes:
125he.encode('foo © bar ≠ baz 𝌆 qux', {
126 'decimal': true
127});
128// → 'foo &#169; bar &#8800; baz &#119558; qux'
129
130// Passing an `options` object to `encode`, to explicitly allow named references and decimal escapes:
131he.encode('foo © bar ≠ baz 𝌆 qux', {
132 'useNamedReferences': true,
133 'decimal': true
134});
135// → 'foo &copy; bar &ne; baz &#119558; qux'
136```
137
138#### `encodeEverything`
139
140The 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).
141
142```js
143// Using the global default setting (defaults to `false`):
144he.encode('foo © bar ≠ baz 𝌆 qux');
145// → 'foo &#xA9; bar &#x2260; baz &#x1D306; qux'
146
147// Passing an `options` object to `encode`, to explicitly encode all symbols:
148he.encode('foo © bar ≠ baz 𝌆 qux', {
149 'encodeEverything': true
150});
151// → '&#x66;&#x6F;&#x6F;&#x20;&#xA9;&#x20;&#x62;&#x61;&#x72;&#x20;&#x2260;&#x20;&#x62;&#x61;&#x7A;&#x20;&#x1D306;&#x20;&#x71;&#x75;&#x78;'
152
153// This setting can be combined with the `useNamedReferences` option:
154he.encode('foo © bar ≠ baz 𝌆 qux', {
155 'encodeEverything': true,
156 'useNamedReferences': true
157});
158// → '&#x66;&#x6F;&#x6F;&#x20;&copy;&#x20;&#x62;&#x61;&#x72;&#x20;&ne;&#x20;&#x62;&#x61;&#x7A;&#x20;&#x1D306;&#x20;&#x71;&#x75;&#x78;'
159```
160
161#### `strict`
162
163The 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.
164
165```js
166// Using the global default setting (defaults to `false`, i.e. error-tolerant mode):
167he.encode('\x01');
168// → '&#x1;'
169
170// Passing an `options` object to `encode`, to explicitly enable error-tolerant mode:
171he.encode('\x01', {
172 'strict': false
173});
174// → '&#x1;'
175
176// Passing an `options` object to `encode`, to explicitly enable strict mode:
177he.encode('\x01', {
178 'strict': true
179});
180// → Parse error
181```
182
183#### `allowUnsafeSymbols`
184
185The 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.
186
187```js
188he.encode('foo © and & ampersand', {
189 'allowUnsafeSymbols': true
190});
191// → 'foo &#xA9; and & ampersand'
192```
193
194#### Overriding default `encode` options globally
195
196The 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.
197
198```js
199// Read the global default setting:
200he.encode.options.useNamedReferences;
201// → `false` by default
202
203// Override the global default setting:
204he.encode.options.useNamedReferences = true;
205
206// Using the global default setting, which is now `true`:
207he.encode('foo © bar ≠ baz 𝌆 qux');
208// → 'foo &copy; bar &ne; baz &#x1D306; qux'
209```
210
211### `he.decode(html, options)`
212
213This 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).
214
215```js
216he.decode('foo &copy; bar &ne; baz &#x1D306; qux');
217// → 'foo © bar ≠ baz 𝌆 qux'
218```
219
220The `options` object is optional. It recognizes the following properties:
221
222#### `isAttributeValue`
223
224The 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.
225
226```js
227// Using the global default setting (defaults to `false`, i.e. HTML text context):
228he.decode('foo&ampbar');
229// → 'foo&bar'
230
231// Passing an `options` object to `decode`, to explicitly assume an HTML text context:
232he.decode('foo&ampbar', {
233 'isAttributeValue': false
234});
235// → 'foo&bar'
236
237// Passing an `options` object to `decode`, to explicitly assume an HTML attribute value context:
238he.decode('foo&ampbar', {
239 'isAttributeValue': true
240});
241// → 'foo&ampbar'
242```
243
244#### `strict`
245
246The 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.
247
248```js
249// Using the global default setting (defaults to `false`, i.e. error-tolerant mode):
250he.decode('foo&ampbar');
251// → 'foo&bar'
252
253// Passing an `options` object to `decode`, to explicitly enable error-tolerant mode:
254he.decode('foo&ampbar', {
255 'strict': false
256});
257// → 'foo&bar'
258
259// Passing an `options` object to `decode`, to explicitly enable strict mode:
260he.decode('foo&ampbar', {
261 'strict': true
262});
263// → Parse error
264```
265
266#### Overriding default `decode` options globally
267
268The 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.
269
270```js
271// Read the global default setting:
272he.decode.options.isAttributeValue;
273// → `false` by default
274
275// Override the global default setting:
276he.decode.options.isAttributeValue = true;
277
278// Using the global default setting, which is now `true`:
279he.decode('foo&ampbar');
280// → 'foo&ampbar'
281```
282
283### `he.escape(text)`
284
285This 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 `` ` ``.
286
287```js
288he.escape('<img src=\'x\' onerror="prompt(1)">');
289// → '&lt;img src=&#x27;x&#x27; onerror=&quot;prompt(1)&quot;&gt;'
290```
291
292### `he.unescape(html, options)`
293
294`he.unescape` is an alias for `he.decode`. It takes a string of HTML and decodes any named and numerical character references in it.
295
296### Using the `he` binary
297
298To use the `he` binary in your shell, simply install _he_ globally using npm:
299
300```bash
301npm install -g he
302```
303
304After that you will be able to encode/decode HTML entities from the command line:
305
306```bash
307$ he --encode 'föo ♥ bår 𝌆 baz'
308f&#xF6;o &#x2665; b&#xE5;r &#x1D306; baz
309
310$ he --encode --use-named-refs 'föo ♥ bår 𝌆 baz'
311f&ouml;o &hearts; b&aring;r &#x1D306; baz
312
313$ he --decode 'f&ouml;o &hearts; b&aring;r &#x1D306; baz'
314föo ♥ bår 𝌆 baz
315```
316
317Read a local text file, encode it for use in an HTML text context, and save the result to a new file:
318
319```bash
320$ he --encode < foo.txt > foo-escaped.html
321```
322
323Or do the same with an online text file:
324
325```bash
326$ curl -sL "http://git.io/HnfEaw" | he --encode > escaped.html
327```
328
329Or, 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:
330
331```bash
332$ he --decode < foo-escaped.html > foo.txt
333```
334
335Or do the same with an online HTML snippet:
336
337```bash
338$ curl -sL "http://git.io/HnfEaw" | he --decode > decoded.txt
339```
340
341See `he --help` for the full list of options.
342
343## Support
344
345_he_ has been tested in at least:
346
347* Chrome 27-50
348* Firefox 3-45
349* Safari 4-9
350* Opera 10-12, 15–37
351* IE 6–11
352* Edge
353* Narwhal 0.3.2
354* Node.js v0.10, v0.12, v4, v5
355* PhantomJS 1.9.0
356* Rhino 1.7RC4
357* RingoJS 0.8-0.11
358
359## Unit tests & code coverage
360
361After 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`.
362
363Once 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`.
364
365To generate the code coverage report, use `grunt cover`.
366
367## Acknowledgements
368
369Thanks to [Simon Pieters](https://simon.html5.org/) ([@zcorpan](https://twitter.com/zcorpan)) for the many suggestions.
370
371## Author
372
373| [![twitter/mathias](https://gravatar.com/avatar/24e08a9ea84deb17ae121074d0f17125?s=70)](https://twitter.com/mathias "Follow @mathias on Twitter") |
374|---|
375| [Mathias Bynens](https://mathiasbynens.be/) |
376
377## License
378
379_he_ is available under the [MIT](https://mths.be/mit) license.