Projekt

Obecné

Profil

Stáhnout (2.31 KB) Statistiky
| Větev: | Revize:
1
node-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('<>"\'&©®')); // &lt;&gt;&quot;&apos;&amp;©®
30
console.log(entities.encodeNonUTF('<>"\'&©®')); // &lt;&gt;&quot;&apos;&amp;&#169;&#174;
31
console.log(entities.encodeNonASCII('<>"\'&©®')); // <>"\'&©®
32
console.log(entities.decode('&lt;&gt;&quot;&apos;&amp;&copy;&reg;&#8710;')); // <>"'&&copy;&reg;∆
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('<>"&©®∆')); // &lt;&gt;&quot;&amp;&copy;&reg;∆
44
console.log(entities.encodeNonUTF('<>"&©®∆')); // &lt;&gt;&quot;&amp;&copy;&reg;&#8710;
45
console.log(entities.encodeNonASCII('<>"&©®∆')); // <>"&©®&#8710;
46
console.log(entities.decode('&lt;&gt;&quot;&amp;&copy;&reg;')); // <>"&©®
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.
(2-2/4)