Socket
Socket
Sign inDemoInstall

html-entities

Package Overview
Dependencies
0
Maintainers
1
Versions
43
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

html-entities

Fastest HTML entities encode/decode library.


Version published
Maintainers
1
Weekly downloads
15,701,632
decreased by-6.8%

Weekly downloads

Package description

What is html-entities?

The html-entities package is a utility for encoding and decoding HTML entities. It can encode and decode a wide range of characters, including special characters, symbols, and emojis, to their corresponding HTML entities and vice versa. This is useful for preventing XSS attacks, rendering special characters in web pages, and working with text that includes characters that need to be escaped in HTML.

What are html-entities's main functionalities?

Encode special characters to HTML entities

This feature allows you to convert characters that have special meaning in HTML into their corresponding entities, making it safe to insert the text into HTML documents.

const { encode } = require('html-entities');
const result = encode('<div>Hello & Welcome!</div>');
console.log(result); // &lt;div&gt;Hello &amp; Welcome!&lt;/div&gt;

Decode HTML entities to their original characters

This feature enables you to convert HTML entities back into their original characters, which is useful when you need to process or display the text as it was originally intended.

const { decode } = require('html-entities');
const result = decode('&lt;div&gt;Hello &amp; Welcome!&lt;/div&gt;');
console.log(result); // <div>Hello & Welcome!</div>

Support for all HTML5 entities

The package includes support for all named HTML5 entities, allowing you to encode and decode a comprehensive set of characters.

const { encode } = require('html-entities');
const result = encode('© ∆');
console.log(result); // &copy; &Delta;

Handling of non-ASCII characters

This feature is specifically for encoding non-ASCII characters into their numerical HTML entity equivalents, which can be important for internationalization and dealing with various character sets.

const { encodeNonAsciiHTML } = require('html-entities');
const result = encodeNonAsciiHTML('Привет!');
console.log(result); // &#x41F;&#x440;&#x438;&#x432;&#x435;&#x442;!

Other packages similar to html-entities

Readme

Source

html-entities

Fastest HTML entities library.

Comes with both TypeScript and Flow types.

Installation

$ npm install html-entities

Usage

encode(text, options)

Encodes text replacing HTML special characters (<>&"') and/or other character ranges depending on mode option value.

import {encode} from 'html-entities';

encode('< > " \' & © ∆');
// -> '&lt; &gt; &quot; &apos; &amp; © ∆'

encode('< ©', {mode: 'nonAsciiPrintable'});
// -> '&lt; &copy;'

encode('< ©', {mode: 'nonAsciiPrintable', level: 'xml'});
// -> '&lt; &#169;'

encode('< > " \' & ©', {mode: 'nonAsciiPrintableOnly', level: 'xml'});
// -> '< > " \' & &#169;'

Options:

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. &#169; (default).
  • hexadecimal uses hexadecimal numbers when encoding html entities. i.e. &#xa9;.

decode(text, options)

Decodes text replacing entities to characters. Unknown entities are left as is.

import {decode} from 'html-entities';

decode('&lt; &gt; &quot; &apos; &amp; &#169; &#8710;');
// -> '< > " \' & © ∆'

decode('&copy;', {level: 'html5'});
// -> '©'

decode('&copy;', {level: 'xml'});
// -> '&copy;'

Options:

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('&lt;');
// -> '<'

decodeEntity('&copy;', {level: 'html5'});
// -> '©'

decodeEntity('&copy;', {level: 'xml'});
// -> '&copy;'

Options:

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

MIT

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

FAQs

Last updated on 04 Mar 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc