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

Faster HTML entities encode/decode library.


Version published
Maintainers
1
Install size
59.2 kB
Created

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

node-html-entities

Fast html entities library.

Installation

npm install html-entities

Usage

####XML entities####

HTML validity and XSS attack prevention you can achieve from XmlEntities class.

var Entities = require('html-entities').XmlEntities;

entities = new Entities();

console.log(entities.encode('<>"\'&©®')); // &lt;&gt;&quot;&apos;&amp;©®
console.log(entities.encodeNonUTF('<>"\'&©®')); // &lt;&gt;&quot;&apos;&amp;&#169;&#174;
console.log(entities.decode('&lt;&gt;&quot;&apos;&amp;&copy;&reg;&#8710;')); // <>"'&&copy;&reg;∆

####All HTML entities encoding/decoding####

var Entities = require('html-entities').AllHtmlEntities;

entities = new Entities();

console.log(entities.encode('<>"&©®∆')); // &lt;&gt;&quot;&amp;&copy;&reg;∆
console.log(entities.encodeNonUTF('<>"&©®∆')); // &lt;&gt;&quot;&amp;&copy;&reg;&#8710;
console.log(entities.decode('&lt;&gt;&quot;&amp;&copy;&reg;')); // <>"&©®

####Available classes####

var XmlEntities = require('html-entities').XmlEntities, // <>"'& + &#...; decoding
    Html4Entities = require('html-entities').Html4Entities, // HTML4 entities.
    Html5Entities = require('html-entities').Html5Entities, // HTML5 entities.
    AllHtmlEntities = require('html-entities').AllHtmlEntities; // Synonym for HTML5 entities.

Supports three methods for every class:

  • encode — encodes, replacing characters to its entity representations. Ignores UTF characters with no entity representation.
  • encodeNonUTF — encodes, replacing characters to its entity representations. Inserts numeric entities for UTF characters.
  • decode — decodes, replacing entities to characters. Unknown entities are left as is.

Keywords

FAQs

Last updated on 23 Feb 2013

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