|
1 |
| -html-entities |
2 |
| -============= |
| 1 | +# html-entities |
3 | 2 |
|
4 |
| -Fast and lightweight HTML entities library supporting all HTML standards. |
| 3 | +[](https://www.npmjs.com/package/html-entities) |
| 4 | +[](https://github.com/mdevils/html-entities/blob/main/LICENSE) |
| 5 | +[](https://www.npmjs.com/package/html-entities) |
| 6 | +[](https://bundlephobia.com/package/html-entities) |
5 | 7 |
|
6 |
| -Comes with both TypeScript and Flow types. |
| 8 | +Fast and lightweight HTML entities library for all HTML standards (HTML5, HTML4, XML). |
7 | 9 |
|
8 |
| -* [Installation](#installation) |
9 |
| -* [Usage](#usage) |
10 |
| - * [encode(text, options)](#encodetext-options) |
11 |
| - * [decode(text, options)](#decodetext-options) |
12 |
| - * [decodeEntity(text, options)](#decodeentitytext-options) |
13 |
| -* [Package Size comparison](#package-size-comparison) |
14 |
| -* [Performance comparison](#performance-comparison) |
| 10 | +## Features |
15 | 11 |
|
16 |
| -Installation |
17 |
| ------------- |
| 12 | +- 🚀 **Fast performance** - optimized for speed (see [benchmarks](#performance-comparison)) |
| 13 | +- 📦 **Lightweight** - small package size (132 kB, see [comparison](#package-size-comparison)) |
| 14 | +- 🔄 **Standards compliant** - supports HTML5, HTML4, and XML entities |
| 15 | +- 📄 **Type safe** - includes TypeScript and Flow types |
| 16 | +- 🔌 **Dual ESM/CJS support** - works with both module systems |
| 17 | + |
| 18 | +## Installation |
18 | 19 |
|
19 | 20 | ```bash
|
20 |
| -$ npm install html-entities |
| 21 | +# NPM |
| 22 | +npm install html-entities |
| 23 | + |
| 24 | +# Yarn |
| 25 | +yarn add html-entities |
| 26 | + |
| 27 | +# pnpm |
| 28 | +pnpm add html-entities |
21 | 29 | ```
|
22 | 30 |
|
23 |
| -Usage |
24 |
| ------ |
| 31 | +## Usage |
25 | 32 |
|
26 | 33 | ### encode(text, options)
|
27 | 34 |
|
28 |
| -Encodes text replacing HTML special characters (`<>&"'`) and/or other character ranges depending on `mode` option value. |
| 35 | +Encodes text by replacing HTML special characters (`<>&"'`) and/or other character ranges depending on `mode` option. |
29 | 36 |
|
30 | 37 | ```js
|
31 |
| -import {encode} from 'html-entities'; |
| 38 | +import { encode } from 'html-entities'; |
32 | 39 |
|
| 40 | +// Basic usage (defaults to HTML5 special chars only) |
33 | 41 | encode('< > " \' & © ∆');
|
34 | 42 | // -> '< > " ' & © ∆'
|
35 | 43 |
|
36 |
| -encode('< ©', {mode: 'nonAsciiPrintable'}); |
| 44 | +// Encode non-ASCII printable characters |
| 45 | +encode('< ©', { mode: 'nonAsciiPrintable' }); |
37 | 46 | // -> '< ©'
|
38 | 47 |
|
39 |
| -encode('< ©', {mode: 'nonAsciiPrintable', level: 'xml'}); |
| 48 | +// Specify encoding level (XML) and mode |
| 49 | +encode('< ©', { mode: 'nonAsciiPrintable', level: 'xml' }); |
40 | 50 | // -> '< ©'
|
41 | 51 |
|
42 |
| -encode('< > " \' & ©', {mode: 'nonAsciiPrintableOnly', level: 'xml'}); |
| 52 | +// Encode only non-ASCII characters, leaving special chars intact |
| 53 | +encode('< > " \' & ©', { mode: 'nonAsciiPrintableOnly', level: 'xml' }); |
43 | 54 | // -> '< > " \' & ©'
|
44 | 55 | ```
|
45 | 56 |
|
46 |
| -Options: |
47 |
| - |
48 |
| -#### level |
49 |
| - |
50 |
| - * `all` alias to `html5` (default). |
51 |
| - * `html5` uses `HTML5` named references. |
52 |
| - * `html4` uses `HTML4` named references. |
53 |
| - * `xml` uses `XML` named references. |
| 57 | +#### Options |
54 | 58 |
|
55 |
| -#### mode |
| 59 | +##### `level` |
| 60 | +- `all` or `html5` (default) - Uses HTML5 named references |
| 61 | +- `html4` - Uses HTML4 named references |
| 62 | +- `xml` - Uses XML named references |
56 | 63 |
|
57 |
| - * `specialChars` encodes only HTML special characters (default). |
58 |
| - * `nonAscii` encodes HTML special characters and everything outside the [ASCII character range](https://en.wikipedia.org/wiki/ASCII). |
59 |
| - * `nonAsciiPrintable` encodes HTML special characters and everything outiside of the [ASCII printable characters](https://en.wikipedia.org/wiki/ASCII#Printable_characters). |
60 |
| - * `nonAsciiPrintableOnly` everything outiside of the [ASCII printable characters](https://en.wikipedia.org/wiki/ASCII#Printable_characters) keeping HTML special characters intact. |
61 |
| - * `extensive` encodes all non-printable characters, non-ASCII characters and all characters with named references. |
62 |
| - |
63 |
| -#### numeric |
64 |
| - |
65 |
| - * `decimal` uses decimal numbers when encoding html entities. i.e. `©` (default). |
66 |
| - * `hexadecimal` uses hexadecimal numbers when encoding html entities. i.e. `©`. |
| 64 | +##### `mode` |
| 65 | +- `specialChars` (default) - Encodes only HTML special characters |
| 66 | +- `nonAscii` - Encodes HTML special characters and everything outside the [ASCII range](https://en.wikipedia.org/wiki/ASCII) |
| 67 | +- `nonAsciiPrintable` - Encodes HTML special characters and everything outside [ASCII printable characters](https://en.wikipedia.org/wiki/ASCII#Printable_characters) |
| 68 | +- `nonAsciiPrintableOnly` - Encodes everything outside ASCII printable range, keeping HTML special characters intact |
| 69 | +- `extensive` - Encodes all non-printable characters, non-ASCII characters and all characters with named references |
67 | 70 |
|
| 71 | +##### `numeric` |
| 72 | +- `decimal` (default) - Uses decimal numbers for HTML entities (e.g., `©`) |
| 73 | +- `hexadecimal` - Uses hexadecimal numbers for HTML entities (e.g., `©`) |
68 | 74 |
|
69 | 75 | ### decode(text, options)
|
70 | 76 |
|
71 |
| -Decodes text replacing entities to characters. Unknown entities are left as is. |
| 77 | +Decodes text by replacing HTML entities with their corresponding characters. Unknown entities are preserved. |
72 | 78 |
|
73 | 79 | ```js
|
74 |
| -import {decode} from 'html-entities'; |
| 80 | +import { decode } from 'html-entities'; |
75 | 81 |
|
| 82 | +// Basic usage (defaults to HTML5) |
76 | 83 | decode('< > " ' & © ∆');
|
77 | 84 | // -> '< > " \' & © ∆'
|
78 | 85 |
|
79 |
| -decode('©', {level: 'html5'}); |
| 86 | +// Specify encoding level |
| 87 | +decode('©', { level: 'html5' }); |
80 | 88 | // -> '©'
|
81 | 89 |
|
82 |
| -decode('©', {level: 'xml'}); |
| 90 | +// XML level doesn't recognize © |
| 91 | +decode('©', { level: 'xml' }); |
83 | 92 | // -> '©'
|
84 | 93 | ```
|
85 | 94 |
|
86 |
| -Options: |
87 |
| - |
88 |
| -#### level |
| 95 | +#### Options |
89 | 96 |
|
90 |
| - * `all` alias to `html5` (default). |
91 |
| - * `html5` uses `HTML5` named references. |
92 |
| - * `html4` uses `HTML4` named references. |
93 |
| - * `xml` uses `XML` named references. |
| 97 | +##### `level` |
| 98 | +- `all` or `html5` (default) - Uses HTML5 named references |
| 99 | +- `html4` - Uses HTML4 named references |
| 100 | +- `xml` - Uses XML named references |
94 | 101 |
|
95 |
| -#### scope |
96 |
| - |
97 |
| - * `body` emulates behavior of browser when parsing tag bodies: entities without semicolon are also replaced (default). |
98 |
| - * `attribute` emulates behavior of browser when parsing tag attributes: entities without semicolon are replaced when not followed by equality sign `=`. |
99 |
| - * `strict` ignores entities without semicolon. |
| 102 | +##### `scope` |
| 103 | +- `body` (default) - Emulates browser behavior when parsing tag bodies: entities without semicolon are also replaced |
| 104 | +- `attribute` - Emulates browser behavior when parsing tag attributes: entities without semicolon are replaced when not followed by `=` |
| 105 | +- `strict` - Ignores entities without semicolon |
100 | 106 |
|
101 | 107 | ### decodeEntity(text, options)
|
102 | 108 |
|
103 |
| -Decodes a single HTML entity. Unknown entitiy is left as is. |
| 109 | +Decodes a single HTML entity. Unknown entities are preserved. |
104 | 110 |
|
105 | 111 | ```js
|
106 |
| -import {decodeEntity} from 'html-entities'; |
| 112 | +import { decodeEntity } from 'html-entities'; |
107 | 113 |
|
108 | 114 | decodeEntity('<');
|
109 | 115 | // -> '<'
|
110 | 116 |
|
111 |
| -decodeEntity('©', {level: 'html5'}); |
| 117 | +decodeEntity('©', { level: 'html5' }); |
112 | 118 | // -> '©'
|
113 | 119 |
|
114 |
| -decodeEntity('©', {level: 'xml'}); |
| 120 | +decodeEntity('©', { level: 'xml' }); |
115 | 121 | // -> '©'
|
116 | 122 | ```
|
117 | 123 |
|
118 |
| -Options: |
119 |
| - |
120 |
| -#### level |
| 124 | +#### Options |
121 | 125 |
|
122 |
| - * `all` alias to `html5` (default). |
123 |
| - * `html5` uses `HTML5` named references. |
124 |
| - * `html4` uses `HTML4` named references. |
125 |
| - * `xml` uses `XML` named references. |
| 126 | +##### `level` |
| 127 | +- `all` or `html5` (default) - Uses HTML5 named references |
| 128 | +- `html4` - Uses HTML4 named references |
| 129 | +- `xml` - Uses XML named references |
126 | 130 |
|
127 |
| -Package Size comparison |
128 |
| ------------------------ |
| 131 | +## Package Size Comparison |
129 | 132 |
|
130 |
| -* `html-entities` - 132 kB |
131 |
| -* `entities` - 540 kB |
132 |
| -* `he` - 124 kB |
| 133 | +| Library | Size | |
| 134 | +|---------|------| |
| 135 | +| **html-entities** | 132 kB | |
| 136 | +| he | 124 kB | |
| 137 | +| entities | 540 kB | |
133 | 138 |
|
134 |
| -Performance comparison |
135 |
| ----------------------- |
| 139 | +## Performance Comparison |
136 | 140 |
|
137 |
| -Statistically significant comparison with other libraries using `benchmark.js`. |
138 |
| -Results by this library are marked with `*`. |
139 |
| -The source code of the benchmark is available at `benchmark/benchmark.ts`. |
| 141 | +Results from benchmarks using `benchmark.js`. This library's results are marked with `*`. |
| 142 | +See benchmark source code at `benchmark/benchmark.ts`. |
140 | 143 |
|
141 | 144 | ```
|
142 | 145 | Common
|
@@ -210,26 +213,16 @@ Escaping
|
210 | 213 | #4: entities.escape x 673,710 ops/sec ±1.30% (94 runs sampled)
|
211 | 214 | ```
|
212 | 215 |
|
213 |
| -License |
214 |
| -------- |
| 216 | +## License |
215 | 217 |
|
216 | 218 | MIT
|
217 | 219 |
|
218 |
| -Security contact information |
219 |
| ----------------------------- |
| 220 | +## Security Contact Information |
220 | 221 |
|
221 |
| -To report a security vulnerability, please use the |
222 |
| -[Tidelift security contact](https://tidelift.com/security). Tidelift will |
223 |
| -coordinate the fix and disclosure. |
| 222 | +To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. |
224 | 223 |
|
225 |
| -`html-entities` for enterprise |
226 |
| ------------------------------- |
| 224 | +## For Enterprise |
227 | 225 |
|
228 |
| -Available as part of the Tidelift Subscription |
| 226 | +Available as part of the Tidelift Subscription. |
229 | 227 |
|
230 |
| -The maintainers of `html-entities` and thousands of other packages are working with |
231 |
| -Tidelift to deliver commercial support and maintenance for the open source |
232 |
| -dependencies you use to build your applications. Save time, reduce risk, and |
233 |
| -improve code health, while paying the maintainers of the exact dependencies you |
234 |
| -use. |
235 |
| -[Learn more.](https://tidelift.com/subscription/pkg/npm-html-entities?utm_source=npm-html-entities&utm_medium=referral&utm_campaign=enterprise) |
| 228 | +The maintainers of `html-entities` and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-html-entities?utm_source=npm-html-entities&utm_medium=referral&utm_campaign=enterprise) |
0 commit comments