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.

63 lines
2.3 KiB

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