- html-entities
- level
- mode
- numeric
- decode(text, options)
- level
- scope
- decodeEntity(text, options)
- level
- Performance
- License
- Security contact information
- html-entities for enterprise
- Keywords
- Saved searches
- Use saved searches to filter your results more quickly
- License
- fb55/entities
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- readme.md
- About
- Saved searches
- Use saved searches to filter your results more quickly
- License
- bb87/htmlentities
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
html-entities
Encodes text replacing HTML special characters ( <>&»‘ ) and/or other character ranges depending on mode option value.
import encode> from 'html-entities'; encode(' < >" \' & © ∆'); // -> '< > " ' & © ∆' encode('< ©', mode: 'nonAsciiPrintable'>); // -> '< ©' encode('< ©', mode: 'nonAsciiPrintable', level: 'xml'>); // -> '< ©' encode(' < >" \' & ©', mode: 'nonAsciiPrintableOnly', level: 'xml'>); // -> ' < >" \' & ©'
level
- all alias to html5 (default).
- html5 uses HTML5 named references.
- html4 uses HTML4 named references.
- xml uses XML named references.
mode
- specialChars encodes only HTML special characters (default).
- nonAscii encodes HTML special characters and everything outside the ASCII character range.
- nonAsciiPrintable encodes HTML special characters and everything outiside of the ASCII printable characters.
- nonAsciiPrintableOnly everything outiside of the ASCII printable characters keeping HTML special characters intact.
- extensive encodes all non-printable characters, non-ASCII characters and all characters with named references.
numeric
- decimal uses decimal numbers when encoding html entities. i.e. © (default).
- hexadecimal uses hexadecimal numbers when encoding html entities. i.e. © .
decode(text, options)
Decodes text replacing entities to characters. Unknown entities are left as is.
import decode> from 'html-entities'; decode('< > " ' & © ∆'); // -> ' < >" \' & © ∆' decode('©', level: 'html5'>); // -> '©' decode('©', level: 'xml'>); // -> '©'
level
- all alias to html5 (default).
- html5 uses HTML5 named references.
- html4 uses HTML4 named references.
- xml uses XML named references.
scope
- body emulates behavior of browser when parsing tag bodies: entities without semicolon are also replaced (default).
- attribute emulates behavior of browser when parsing tag attributes: entities without semicolon are replaced when not followed by equality sign = .
- strict ignores entities without semicolon.
decodeEntity(text, options)
Decodes a single HTML entity. Unknown entitiy is left as is.
import decodeEntity> from 'html-entities'; decodeEntity('<'); // -> ' decodeEntity('©', level: 'html5'>); // -> '©' decodeEntity('©', level: 'xml'>); // -> '©'
level
- all alias to html5 (default).
- html5 uses HTML5 named references.
- html4 uses HTML4 named references.
- xml uses XML named references.
Performance
Statistically significant comparison with other libraries using benchmark.js . Results by this library are marked with * . The source code of the benchmark is available at benchmark/benchmark.ts .
Common Initialization / Load speed * #1: html-entities x 2,632,942 ops/sec ±3.71% (72 runs sampled) #2: entities x 1,379,154 ops/sec ±5.87% (75 runs sampled) #3: he x 1,334,035 ops/sec ±3.14% (83 runs sampled) HTML5 Encode test * #1: html-entities.encode - html5, nonAscii x 415,806 ops/sec ±0.73% (85 runs sampled) * #2: html-entities.encode - html5, nonAsciiPrintable x 401,420 ops/sec ±0.35% (93 runs sampled) #3: entities.encodeNonAsciiHTML x 401,235 ops/sec ±0.41% (88 runs sampled) #4: entities.encodeHTML x 284,868 ops/sec ±0.45% (93 runs sampled) * #5: html-entities.encode - html5, extensive x 237,613 ops/sec ±0.42% (93 runs sampled) #6: he.encode x 91,459 ops/sec ±0.50% (84 runs sampled) Decode test #1: entities.decodeHTMLStrict x 614,920 ops/sec ±0.41% (89 runs sampled) #2: entities.decodeHTML x 577,698 ops/sec ±0.44% (90 runs sampled) * #3: html-entities.decode - html5, strict x 323,680 ops/sec ±0.39% (92 runs sampled) * #4: html-entities.decode - html5, body x 297,548 ops/sec ±0.45% (91 runs sampled) * #5: html-entities.decode - html5, attribute x 293,617 ops/sec ±0.37% (94 runs sampled) #6: he.decode x 145,383 ops/sec ±0.36% (94 runs sampled) HTML4 Encode test * #1: html-entities.encode - html4, nonAscii x 379,799 ops/sec ±0.29% (96 runs sampled) * #2: html-entities.encode - html4, nonAsciiPrintable x 350,003 ops/sec ±0.42% (92 runs sampled) * #3: html-entities.encode - html4, extensive x 169,759 ops/sec ±0.43% (90 runs sampled) Decode test * #1: html-entities.decode - html4, attribute x 291,048 ops/sec ±0.42% (92 runs sampled) * #2: html-entities.decode - html4, strict x 287,110 ops/sec ±0.56% (93 runs sampled) * #3: html-entities.decode - html4, body x 285,529 ops/sec ±0.57% (93 runs sampled) XML Encode test #1: entities.encodeXML x 418,561 ops/sec ±0.80% (90 runs sampled) * #2: html-entities.encode - xml, nonAsciiPrintable x 402,868 ops/sec ±0.30% (89 runs sampled) * #3: html-entities.encode - xml, nonAscii x 403,669 ops/sec ±7.87% (83 runs sampled) * #4: html-entities.encode - xml, extensive x 237,766 ops/sec ±0.45% (93 runs sampled) Decode test #1: entities.decodeXML x 888,700 ops/sec ±0.48% (93 runs sampled) * #2: html-entities.decode - xml, strict x 353,127 ops/sec ±0.40% (92 runs sampled) * #3: html-entities.decode - xml, body x 355,796 ops/sec ±1.58% (86 runs sampled) * #4: html-entities.decode - xml, attribute x 369,454 ops/sec ±8.74% (84 runs sampled) Escaping Escape test #1: entities.escapeUTF8 x 1,308,013 ops/sec ±0.37% (91 runs sampled) * #2: html-entities.encode - xml, specialChars x 1,258,760 ops/sec ±1.00% (93 runs sampled) #3: he.escape x 822,569 ops/sec ±0.24% (94 runs sampled) #4: entities.escape x 434,243 ops/sec ±0.34% (91 runs sampled)
License
Security contact information
To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.
html-entities for enterprise
Available as part of the Tidelift Subscription
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.
Keywords
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Encode & decode HTML & XML entities with ease & speed
License
fb55/entities
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
readme.md
Encode & decode HTML & XML entities with ease & speed.
- 😇 Tried and true: entities is used by many popular libraries; eg. htmlparser2 , the official AWS SDK and commonmark use it to process HTML entities.
- ⚡️ Fast: entities is the fastest library for decoding HTML entities (as of April 2022); see performance.
- 🎛 Configurable: Get an output tailored for your needs. You are fine with UTF8? That’ll save you some bytes. Prefer to only have ASCII characters? We can do that as well!
const entities = require("entities"); // Encoding entities.escapeUTF8("& ü"); // "& ü" entities.encodeXML("& ü"); // "& ü" entities.encodeHTML("& ü"); // "&#38; ü" // Decoding entities.decodeXML("asdf & ÿ ü '"); // "asdf & ÿ ü '" entities.decodeHTML("asdf & ÿ ü '"); // "asdf & ÿ ü '"
This is how entities compares to other libraries on a very basic benchmark (see scripts/benchmark.ts , for 10,000,000 iterations; lower is better):
Library | Version | decode perf | encode perf | escape perf |
---|---|---|---|---|
entities | 3.0.1 | 1.418s | 6.786s | 2.196s |
html-entities | 2.3.2 | 2.530s | 6.829s | 2.415s |
he | 1.2.0 | 5.800s | 24.237s | 3.624s |
parse-entities | 3.0.0 | 9.660s | N/A | N/A |
What methods should I actually use to encode my documents?
If your target supports UTF-8, the escapeUTF8 method is going to be your best choice. Otherwise, use either encodeHTML or encodeXML based on whether you’re dealing with an HTML or an XML document.
You can have a look at the options for the encode and decode methods to see everything you can configure.
When should I use strict decoding?
When strict decoding, entities not terminated with a semicolon will be ignored. This is helpful for decoding entities in legacy environments.
Why should I use entities instead of alternative modules?
As of April 2022, entities is a bit faster than other modules. Still, this is not a very differentiated space and other modules can catch up.
More importantly, you might already have entities in your dependency graph (as a dependency of eg. cheerio , or htmlparser2 ), and including it directly might not even increase your bundle size. The same is true for other entity libraries, so have a look through your node_modules directory!
Does entities support tree shaking?
Yes! entities ships as both a CommonJS and a ES module. Note that for best results, you should not use the encode and decode functions, as they wrap around a number of other functions, all of which will remain in the bundle. Instead, use the functions that you need directly.
This library wouldn’t be possible without the work of these individuals. Thanks to
- @mathiasbynens for his explanations about character encodings, and his library he , which was one of the inspirations for entities
- @inikulin for his work on optimized tries for decoding HTML entities for the parse5 project
- @mdevils for taking on the challenge of producing a quick entity library with his html-entities library. entities would be quite a bit slower if there wasn’t any competition. Right now entities is on top, but we’ll see how long that lasts!
Security contact information
To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.
Available as part of the Tidelift Subscription
The maintainers of 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.
About
Encode & decode HTML & XML entities with ease & speed
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Encode and decode HTML entities with javascript
License
bb87/htmlentities
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
htmlentities.js lets you deal with «complex» HTML entities (not just the usual » ‘»‘ and ‘&’ special characters) in an elegant, JS-oriented way, on the server and in the browser, without dependencies.
var htmlentities = require(‘htmlentities’);
Returns the encoded version of string . If omitted, encoding defaults to UTF-8 .
Returns the decoded version of string . If omitted, encoding defaults to UTF-8 .
The following encodings are supported:
I wrote this plugin because I needed to compare two strings: one was encoded, one wasn’t. I didn’t find a library that would easily let me do it. There are nice node modules, there are client-side libraries, and there are jQuery hacks. But if you want a script that is straightforward and runs both in the browser and on the server, you will probably find htmlentities.js useful.
About
Encode and decode HTML entities with javascript