1 | html-entities
|
---|
2 | =============
|
---|
3 |
|
---|
4 | [![Build Status](https://travis-ci.org/mdevils/node-html-entities.svg?branch=master)](https://travis-ci.org/mdevils/node-html-entities)
|
---|
5 | [![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)
|
---|
6 |
|
---|
7 | Fast html entities library.
|
---|
8 |
|
---|
9 |
|
---|
10 | Installation
|
---|
11 | ------------
|
---|
12 |
|
---|
13 | ```bash
|
---|
14 | $ npm install html-entities
|
---|
15 | ```
|
---|
16 |
|
---|
17 | Usage
|
---|
18 | -----
|
---|
19 |
|
---|
20 | **XML entities**
|
---|
21 |
|
---|
22 | HTML validity and XSS attack prevention you can achieve from XmlEntities class.
|
---|
23 |
|
---|
24 | ```javascript
|
---|
25 | const Entities = require('html-entities').XmlEntities;
|
---|
26 |
|
---|
27 | const entities = new Entities();
|
---|
28 |
|
---|
29 | console.log(entities.encode('<>"\'&©®')); // <>"'&©®
|
---|
30 | console.log(entities.encodeNonUTF('<>"\'&©®')); // <>"'&©®
|
---|
31 | console.log(entities.encodeNonASCII('<>"\'&©®')); // <>"\'&©®
|
---|
32 | console.log(entities.decode('<>"'&©®∆')); // <>"'&©®∆
|
---|
33 | ```
|
---|
34 |
|
---|
35 | **All HTML entities encoding/decoding**
|
---|
36 |
|
---|
37 |
|
---|
38 | ```javascript
|
---|
39 | const Entities = require('html-entities').AllHtmlEntities;
|
---|
40 |
|
---|
41 | const entities = new Entities();
|
---|
42 |
|
---|
43 | console.log(entities.encode('<>"&©®∆')); // <>"&©®∆
|
---|
44 | console.log(entities.encodeNonUTF('<>"&©®∆')); // <>"&©®∆
|
---|
45 | console.log(entities.encodeNonASCII('<>"&©®∆')); // <>"&©®∆
|
---|
46 | console.log(entities.decode('<>"&©®')); // <>"&©®
|
---|
47 | ```
|
---|
48 |
|
---|
49 | **Available classes**
|
---|
50 |
|
---|
51 | ```javascript
|
---|
52 | const XmlEntities = require('html-entities').XmlEntities, // <>"'& + &#...; decoding
|
---|
53 | Html4Entities = require('html-entities').Html4Entities, // HTML4 entities.
|
---|
54 | Html5Entities = require('html-entities').Html5Entities, // HTML5 entities.
|
---|
55 | AllHtmlEntities = require('html-entities').AllHtmlEntities; // Synonym for HTML5 entities.
|
---|
56 | ```
|
---|
57 |
|
---|
58 | Supports four methods for every class:
|
---|
59 |
|
---|
60 | * encode — encodes, replacing characters to its entity representations. Ignores UTF characters with no entity representation.
|
---|
61 | * encodeNonUTF — encodes, replacing characters to its entity representations. Inserts numeric entities for UTF characters.
|
---|
62 | * encodeNonASCII — encodes, replacing only non-ASCII characters to its numeric entity representations.
|
---|
63 | * decode — decodes, replacing entities to characters. Unknown entities are left as is.
|
---|