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); // <div>Hello & Welcome!</div>
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('<div>Hello & Welcome!</div>');
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); // © Δ
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); // Привет!
Other packages similar to html-entities
he
The 'he' package is an HTML entity encoder/decoder written in JavaScript. It is robust and handles a large number of character references. Compared to html-entities, 'he' claims to be the fastest and most comprehensive HTML entity library, and it strictly adheres to the HTML5 specification.
entities
The 'entities' package is another library for encoding and decoding HTML entities. It is used internally by the 'htmlparser2' library, which is a fast and forgiving HTML/XML parser. While 'entities' offers similar functionality to html-entities, it is particularly optimized for use with 'htmlparser2' and may be more suitable for parsing tasks.
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('<>"\'&©®'));
console.log(entities.encodeNonUTF('<>"\'&©®'));
console.log(entities.encodeNonASCII('<>"\'&©®'));
console.log(entities.decode('<>"'&©®∆'));
####All HTML entities encoding/decoding####
var Entities = require('html-entities').AllHtmlEntities;
entities = new Entities();
console.log(entities.encode('<>"&©®∆'));
console.log(entities.encodeNonUTF('<>"&©®∆'));
console.log(entities.encodeNonASCII('<>"&©®∆'));
console.log(entities.decode('<>"&©®'));
####Available classes####
var XmlEntities = require('html-entities').XmlEntities,
Html4Entities = require('html-entities').Html4Entities,
Html5Entities = require('html-entities').Html5Entities,
AllHtmlEntities = require('html-entities').AllHtmlEntities;
Supports four 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.
- encodeNonASCII — encodes, replacing only non-ASCII characters to its numeric entity representations.
- decode — decodes, replacing entities to characters. Unknown entities are left as is.