| 1 | html-entities
|
|---|
| 2 | =============
|
|---|
| 3 |
|
|---|
| 4 | Fastest HTML entities library.
|
|---|
| 5 |
|
|---|
| 6 | Comes with both TypeScript and Flow types.
|
|---|
| 7 |
|
|---|
| 8 | Installation
|
|---|
| 9 | ------------
|
|---|
| 10 |
|
|---|
| 11 | ```bash
|
|---|
| 12 | $ npm install html-entities
|
|---|
| 13 | ```
|
|---|
| 14 |
|
|---|
| 15 | Usage
|
|---|
| 16 | -----
|
|---|
| 17 |
|
|---|
| 18 | ### encode(text, options)
|
|---|
| 19 |
|
|---|
| 20 | Encodes text replacing HTML special characters (`<>&"'`) and/or other character ranges depending on `mode` option value.
|
|---|
| 21 |
|
|---|
| 22 | ```js
|
|---|
| 23 | import {encode} from 'html-entities';
|
|---|
| 24 |
|
|---|
| 25 | encode('< > " \' & © ∆');
|
|---|
| 26 | // -> '< > " ' & © ∆'
|
|---|
| 27 |
|
|---|
| 28 | encode('< ©', {mode: 'nonAsciiPrintable'});
|
|---|
| 29 | // -> '< ©'
|
|---|
| 30 |
|
|---|
| 31 | encode('< ©', {mode: 'nonAsciiPrintable', level: 'xml'});
|
|---|
| 32 | // -> '< ©'
|
|---|
| 33 |
|
|---|
| 34 | encode('< > " \' & ©', {mode: 'nonAsciiPrintableOnly', level: 'xml'});
|
|---|
| 35 | // -> '< > " \' & ©'
|
|---|
| 36 | ```
|
|---|
| 37 |
|
|---|
| 38 | Options:
|
|---|
| 39 |
|
|---|
| 40 | #### level
|
|---|
| 41 |
|
|---|
| 42 | * `all` alias to `html5` (default).
|
|---|
| 43 | * `html5` uses `HTML5` named references.
|
|---|
| 44 | * `html4` uses `HTML4` named references.
|
|---|
| 45 | * `xml` uses `XML` named references.
|
|---|
| 46 |
|
|---|
| 47 | #### mode
|
|---|
| 48 |
|
|---|
| 49 | * `specialChars` encodes only HTML special characters (default).
|
|---|
| 50 | * `nonAscii` encodes HTML special characters and everything outside the [ASCII character range](https://en.wikipedia.org/wiki/ASCII).
|
|---|
| 51 | * `nonAsciiPrintable` encodes HTML special characters and everything outiside of the [ASCII printable characters](https://en.wikipedia.org/wiki/ASCII#Printable_characters).
|
|---|
| 52 | * `nonAsciiPrintableOnly` everything outiside of the [ASCII printable characters](https://en.wikipedia.org/wiki/ASCII#Printable_characters) keeping HTML special characters intact.
|
|---|
| 53 | * `extensive` encodes all non-printable characters, non-ASCII characters and all characters with named references.
|
|---|
| 54 |
|
|---|
| 55 | #### numeric
|
|---|
| 56 |
|
|---|
| 57 | * `decimal` uses decimal numbers when encoding html entities. i.e. `©` (default).
|
|---|
| 58 | * `hexadecimal` uses hexadecimal numbers when encoding html entities. i.e. `©`.
|
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 | ### decode(text, options)
|
|---|
| 62 |
|
|---|
| 63 | Decodes text replacing entities to characters. Unknown entities are left as is.
|
|---|
| 64 |
|
|---|
| 65 | ```js
|
|---|
| 66 | import {decode} from 'html-entities';
|
|---|
| 67 |
|
|---|
| 68 | decode('< > " ' & © ∆');
|
|---|
| 69 | // -> '< > " \' & © ∆'
|
|---|
| 70 |
|
|---|
| 71 | decode('©', {level: 'html5'});
|
|---|
| 72 | // -> '©'
|
|---|
| 73 |
|
|---|
| 74 | decode('©', {level: 'xml'});
|
|---|
| 75 | // -> '©'
|
|---|
| 76 | ```
|
|---|
| 77 |
|
|---|
| 78 | Options:
|
|---|
| 79 |
|
|---|
| 80 | #### level
|
|---|
| 81 |
|
|---|
| 82 | * `all` alias to `html5` (default).
|
|---|
| 83 | * `html5` uses `HTML5` named references.
|
|---|
| 84 | * `html4` uses `HTML4` named references.
|
|---|
| 85 | * `xml` uses `XML` named references.
|
|---|
| 86 |
|
|---|
| 87 | #### scope
|
|---|
| 88 |
|
|---|
| 89 | * `body` emulates behavior of browser when parsing tag bodies: entities without semicolon are also replaced (default).
|
|---|
| 90 | * `attribute` emulates behavior of browser when parsing tag attributes: entities without semicolon are replaced when not followed by equality sign `=`.
|
|---|
| 91 | * `strict` ignores entities without semicolon.
|
|---|
| 92 |
|
|---|
| 93 | ### decodeEntity(text, options)
|
|---|
| 94 |
|
|---|
| 95 | Decodes a single HTML entity. Unknown entitiy is left as is.
|
|---|
| 96 |
|
|---|
| 97 | ```js
|
|---|
| 98 | import {decodeEntity} from 'html-entities';
|
|---|
| 99 |
|
|---|
| 100 | decodeEntity('<');
|
|---|
| 101 | // -> '<'
|
|---|
| 102 |
|
|---|
| 103 | decodeEntity('©', {level: 'html5'});
|
|---|
| 104 | // -> '©'
|
|---|
| 105 |
|
|---|
| 106 | decodeEntity('©', {level: 'xml'});
|
|---|
| 107 | // -> '©'
|
|---|
| 108 | ```
|
|---|
| 109 |
|
|---|
| 110 | Options:
|
|---|
| 111 |
|
|---|
| 112 | #### level
|
|---|
| 113 |
|
|---|
| 114 | * `all` alias to `html5` (default).
|
|---|
| 115 | * `html5` uses `HTML5` named references.
|
|---|
| 116 | * `html4` uses `HTML4` named references.
|
|---|
| 117 | * `xml` uses `XML` named references.
|
|---|
| 118 |
|
|---|
| 119 | Performance
|
|---|
| 120 | -----------
|
|---|
| 121 |
|
|---|
| 122 | Statistically significant comparison with other libraries using `benchmark.js`.
|
|---|
| 123 | Results by this library are marked with `*`.
|
|---|
| 124 | The source code of the benchmark is available at `benchmark/benchmark.ts`.
|
|---|
| 125 |
|
|---|
| 126 | ```
|
|---|
| 127 | Common
|
|---|
| 128 |
|
|---|
| 129 | Initialization / Load speed
|
|---|
| 130 |
|
|---|
| 131 | #1: he x 516 ops/sec ±5.71% (78 runs sampled)
|
|---|
| 132 | * #2: html-entities x 407 ops/sec ±5.64% (81 runs sampled)
|
|---|
| 133 | #3: entities x 352 ops/sec ±4.16% (80 runs sampled)
|
|---|
| 134 |
|
|---|
| 135 | HTML5
|
|---|
| 136 |
|
|---|
| 137 | Encode test
|
|---|
| 138 |
|
|---|
| 139 | * #1: html-entities.encode - html5, extensive x 437,236 ops/sec ±0.90% (98 runs sampled)
|
|---|
| 140 | #2: entities.encodeHTML x 335,714 ops/sec ±0.87% (92 runs sampled)
|
|---|
| 141 |
|
|---|
| 142 | Encode non-ASCII test
|
|---|
| 143 |
|
|---|
| 144 | * #1: html-entities.encode - html5, nonAscii x 749,246 ops/sec ±0.61% (96 runs sampled)
|
|---|
| 145 | #2: entities.encodeNonAsciiHTML x 706,984 ops/sec ±1.06% (98 runs sampled)
|
|---|
| 146 | * #3: html-entities.encode - html5, nonAsciiPrintable x 691,193 ops/sec ±4.47% (90 runs sampled)
|
|---|
| 147 | #4: he.encode x 141,105 ops/sec ±0.87% (92 runs sampled)
|
|---|
| 148 |
|
|---|
| 149 | Decode test
|
|---|
| 150 |
|
|---|
| 151 | #1: entities.decodeHTML x 678,595 ops/sec ±1.28% (92 runs sampled)
|
|---|
| 152 | #2: entities.decodeHTMLStrict x 684,372 ops/sec ±2.76% (82 runs sampled)
|
|---|
| 153 | * #3: html-entities.decode - html5, strict x 485,664 ops/sec ±0.80% (94 runs sampled)
|
|---|
| 154 | * #4: html-entities.decode - html5, body x 463,074 ops/sec ±1.11% (93 runs sampled)
|
|---|
| 155 | * #5: html-entities.decode - html5, attribute x 456,185 ops/sec ±2.24% (91 runs sampled)
|
|---|
| 156 | #6: he.decode x 302,668 ops/sec ±2.73% (90 runs sampled)
|
|---|
| 157 |
|
|---|
| 158 | HTML4
|
|---|
| 159 |
|
|---|
| 160 | Encode test
|
|---|
| 161 |
|
|---|
| 162 | * #1: html-entities.encode - html4, nonAscii x 737,475 ops/sec ±1.04% (95 runs sampled)
|
|---|
| 163 | * #2: html-entities.encode - html4, nonAsciiPrintable x 649,866 ops/sec ±4.28% (79 runs sampled)
|
|---|
| 164 | * #3: html-entities.encode - html4, extensive x 202,337 ops/sec ±3.66% (64 runs sampled)
|
|---|
| 165 |
|
|---|
| 166 | Decode test
|
|---|
| 167 |
|
|---|
| 168 | * #1: html-entities.decode - html4, attribute x 529,674 ops/sec ±0.90% (90 runs sampled)
|
|---|
| 169 | * #2: html-entities.decode - html4, body x 499,135 ops/sec ±2.27% (80 runs sampled)
|
|---|
| 170 | * #3: html-entities.decode - html4, strict x 489,806 ops/sec ±4.37% (84 runs sampled)
|
|---|
| 171 |
|
|---|
| 172 | XML
|
|---|
| 173 |
|
|---|
| 174 | Encode test
|
|---|
| 175 |
|
|---|
| 176 | * #1: html-entities.encode - xml, nonAscii x 823,097 ops/sec ±0.75% (81 runs sampled)
|
|---|
| 177 | * #2: html-entities.encode - xml, nonAsciiPrintable x 764,638 ops/sec ±0.93% (93 runs sampled)
|
|---|
| 178 | #3: entities.encodeXML x 672,186 ops/sec ±1.51% (92 runs sampled)
|
|---|
| 179 | * #4: html-entities.encode - xml, extensive x 376,870 ops/sec ±0.76% (77 runs sampled)
|
|---|
| 180 |
|
|---|
| 181 | Decode test
|
|---|
| 182 |
|
|---|
| 183 | #1: entities.decodeXML x 930,758 ops/sec ±2.90% (90 runs sampled)
|
|---|
| 184 | * #2: html-entities.decode - xml, body x 617,321 ops/sec ±0.74% (83 runs sampled)
|
|---|
| 185 | * #3: html-entities.decode - xml, attribute x 611,598 ops/sec ±0.50% (92 runs sampled)
|
|---|
| 186 | * #4: html-entities.decode - xml, strict x 607,191 ops/sec ±2.30% (85 runs sampled)
|
|---|
| 187 |
|
|---|
| 188 | Escaping
|
|---|
| 189 |
|
|---|
| 190 | Escape test
|
|---|
| 191 |
|
|---|
| 192 | #1: entities.escapeUTF8 x 1,930,874 ops/sec ±0.80% (95 runs sampled)
|
|---|
| 193 | #2: he.escape x 1,717,522 ops/sec ±0.75% (84 runs sampled)
|
|---|
| 194 | * #3: html-entities.encode - xml, specialChars x 1,611,374 ops/sec ±1.30% (92 runs sampled)
|
|---|
| 195 | #4: entities.escape x 673,710 ops/sec ±1.30% (94 runs sampled)
|
|---|
| 196 | ```
|
|---|
| 197 |
|
|---|
| 198 | License
|
|---|
| 199 | -------
|
|---|
| 200 |
|
|---|
| 201 | MIT
|
|---|
| 202 |
|
|---|
| 203 | Security contact information
|
|---|
| 204 | ----------------------------
|
|---|
| 205 |
|
|---|
| 206 | To report a security vulnerability, please use the
|
|---|
| 207 | [Tidelift security contact](https://tidelift.com/security). Tidelift will
|
|---|
| 208 | coordinate the fix and disclosure.
|
|---|
| 209 |
|
|---|
| 210 | `html-entities` for enterprise
|
|---|
| 211 | ------------------------------
|
|---|
| 212 |
|
|---|
| 213 | Available as part of the Tidelift Subscription
|
|---|
| 214 |
|
|---|
| 215 | The maintainers of `html-entities` and thousands of other packages are working with
|
|---|
| 216 | Tidelift to deliver commercial support and maintenance for the open source
|
|---|
| 217 | dependencies you use to build your applications. Save time, reduce risk, and
|
|---|
| 218 | improve code health, while paying the maintainers of the exact dependencies you
|
|---|
| 219 | use.
|
|---|
| 220 | [Learn more.](https://tidelift.com/subscription/pkg/npm-html-entities?utm_source=npm-html-entities&utm_medium=referral&utm_campaign=enterprise)
|
|---|