You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
2.3 KiB
64 lines
2.3 KiB
4 years ago
|
node-html-entities
|
||
|
==================
|
||
|
|
||
|
[![Build Status](https://travis-ci.org/mdevils/node-html-entities.svg?branch=master)](https://travis-ci.org/mdevils/node-html-entities)
|
||
|
[![Coverage Status](https://coveralls.io/repos/mdevils/node-html-entities/badge.svg?branch=master&service=github)](https://coveralls.io/github/mdevils/node-html-entities?branch=master)
|
||
|
|
||
|
Fast html entities library.
|
||
|
|
||
|
|
||
|
Installation
|
||
|
------------
|
||
|
|
||
|
```bash
|
||
|
$ npm install html-entities
|
||
|
```
|
||
|
|
||
|
Usage
|
||
|
-----
|
||
|
|
||
|
**XML entities**
|
||
|
|
||
|
HTML validity and XSS attack prevention you can achieve from XmlEntities class.
|
||
|
|
||
|
```javascript
|
||
|
const Entities = require('html-entities').XmlEntities;
|
||
|
|
||
|
const entities = new Entities();
|
||
|
|
||
|
console.log(entities.encode('<>"\'&©®')); // <>"'&©®
|
||
|
console.log(entities.encodeNonUTF('<>"\'&©®')); // <>"'&©®
|
||
|
console.log(entities.encodeNonASCII('<>"\'&©®')); // <>"\'&©®
|
||
|
console.log(entities.decode('<>"'&©®∆')); // <>"'&©®∆
|
||
|
```
|
||
|
|
||
|
**All HTML entities encoding/decoding**
|
||
|
|
||
|
|
||
|
```javascript
|
||
|
const Entities = require('html-entities').AllHtmlEntities;
|
||
|
|
||
|
const entities = new Entities();
|
||
|
|
||
|
console.log(entities.encode('<>"&©®∆')); // <>"&©®∆
|
||
|
console.log(entities.encodeNonUTF('<>"&©®∆')); // <>"&©®∆
|
||
|
console.log(entities.encodeNonASCII('<>"&©®∆')); // <>"&©®∆
|
||
|
console.log(entities.decode('<>"&©®')); // <>"&©®
|
||
|
```
|
||
|
|
||
|
**Available classes**
|
||
|
|
||
|
```javascript
|
||
|
const 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 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.
|