提交学习笔记专用
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.

220 lines
7.0 KiB

  1. html-entities
  2. =============
  3. Fastest HTML entities library.
  4. Comes with both TypeScript and Flow types.
  5. Installation
  6. ------------
  7. ```bash
  8. $ npm install html-entities
  9. ```
  10. Usage
  11. -----
  12. ### encode(text, options)
  13. Encodes text replacing HTML special characters (`<>&"'`) and/or other character ranges depending on `mode` option value.
  14. ```js
  15. import {encode} from 'html-entities';
  16. encode('< > " \' & © ∆');
  17. // -> '&lt; &gt; &quot; &apos; &amp; © ∆'
  18. encode('< ©', {mode: 'nonAsciiPrintable'});
  19. // -> '&lt; &copy;'
  20. encode('< ©', {mode: 'nonAsciiPrintable', level: 'xml'});
  21. // -> '&lt; &#169;'
  22. encode('< > " \' & ©', {mode: 'nonAsciiPrintableOnly', level: 'xml'});
  23. // -> '< > " \' & &#169;'
  24. ```
  25. Options:
  26. #### level
  27. * `all` alias to `html5` (default).
  28. * `html5` uses `HTML5` named references.
  29. * `html4` uses `HTML4` named references.
  30. * `xml` uses `XML` named references.
  31. #### mode
  32. * `specialChars` encodes only HTML special characters (default).
  33. * `nonAscii` encodes HTML special characters and everything outside the [ASCII character range](https://en.wikipedia.org/wiki/ASCII).
  34. * `nonAsciiPrintable` encodes HTML special characters and everything outiside of the [ASCII printable characters](https://en.wikipedia.org/wiki/ASCII#Printable_characters).
  35. * `nonAsciiPrintableOnly` everything outiside of the [ASCII printable characters](https://en.wikipedia.org/wiki/ASCII#Printable_characters) keeping HTML special characters intact.
  36. * `extensive` encodes all non-printable characters, non-ASCII characters and all characters with named references.
  37. #### numeric
  38. * `decimal` uses decimal numbers when encoding html entities. i.e. `&#169;` (default).
  39. * `hexadecimal` uses hexadecimal numbers when encoding html entities. i.e. `&#xa9;`.
  40. ### decode(text, options)
  41. Decodes text replacing entities to characters. Unknown entities are left as is.
  42. ```js
  43. import {decode} from 'html-entities';
  44. decode('&lt; &gt; &quot; &apos; &amp; &#169; &#8710;');
  45. // -> '< > " \' & © ∆'
  46. decode('&copy;', {level: 'html5'});
  47. // -> '©'
  48. decode('&copy;', {level: 'xml'});
  49. // -> '&copy;'
  50. ```
  51. Options:
  52. #### level
  53. * `all` alias to `html5` (default).
  54. * `html5` uses `HTML5` named references.
  55. * `html4` uses `HTML4` named references.
  56. * `xml` uses `XML` named references.
  57. #### scope
  58. * `body` emulates behavior of browser when parsing tag bodies: entities without semicolon are also replaced (default).
  59. * `attribute` emulates behavior of browser when parsing tag attributes: entities without semicolon are replaced when not followed by equality sign `=`.
  60. * `strict` ignores entities without semicolon.
  61. ### decodeEntity(text, options)
  62. Decodes a single HTML entity. Unknown entitiy is left as is.
  63. ```js
  64. import {decodeEntity} from 'html-entities';
  65. decodeEntity('&lt;');
  66. // -> '<'
  67. decodeEntity('&copy;', {level: 'html5'});
  68. // -> '©'
  69. decodeEntity('&copy;', {level: 'xml'});
  70. // -> '&copy;'
  71. ```
  72. Options:
  73. #### level
  74. * `all` alias to `html5` (default).
  75. * `html5` uses `HTML5` named references.
  76. * `html4` uses `HTML4` named references.
  77. * `xml` uses `XML` named references.
  78. Performance
  79. -----------
  80. Statistically significant comparison with other libraries using `benchmark.js`.
  81. Results by this library are marked with `*`.
  82. The source code of the benchmark is available at `benchmark/benchmark.ts`.
  83. ```
  84. Common
  85. Initialization / Load speed
  86. #1: he x 516 ops/sec ±5.71% (78 runs sampled)
  87. * #2: html-entities x 407 ops/sec ±5.64% (81 runs sampled)
  88. #3: entities x 352 ops/sec ±4.16% (80 runs sampled)
  89. HTML5
  90. Encode test
  91. * #1: html-entities.encode - html5, extensive x 437,236 ops/sec ±0.90% (98 runs sampled)
  92. #2: entities.encodeHTML x 335,714 ops/sec ±0.87% (92 runs sampled)
  93. Encode non-ASCII test
  94. * #1: html-entities.encode - html5, nonAscii x 749,246 ops/sec ±0.61% (96 runs sampled)
  95. #2: entities.encodeNonAsciiHTML x 706,984 ops/sec ±1.06% (98 runs sampled)
  96. * #3: html-entities.encode - html5, nonAsciiPrintable x 691,193 ops/sec ±4.47% (90 runs sampled)
  97. #4: he.encode x 141,105 ops/sec ±0.87% (92 runs sampled)
  98. Decode test
  99. #1: entities.decodeHTML x 678,595 ops/sec ±1.28% (92 runs sampled)
  100. #2: entities.decodeHTMLStrict x 684,372 ops/sec ±2.76% (82 runs sampled)
  101. * #3: html-entities.decode - html5, strict x 485,664 ops/sec ±0.80% (94 runs sampled)
  102. * #4: html-entities.decode - html5, body x 463,074 ops/sec ±1.11% (93 runs sampled)
  103. * #5: html-entities.decode - html5, attribute x 456,185 ops/sec ±2.24% (91 runs sampled)
  104. #6: he.decode x 302,668 ops/sec ±2.73% (90 runs sampled)
  105. HTML4
  106. Encode test
  107. * #1: html-entities.encode - html4, nonAscii x 737,475 ops/sec ±1.04% (95 runs sampled)
  108. * #2: html-entities.encode - html4, nonAsciiPrintable x 649,866 ops/sec ±4.28% (79 runs sampled)
  109. * #3: html-entities.encode - html4, extensive x 202,337 ops/sec ±3.66% (64 runs sampled)
  110. Decode test
  111. * #1: html-entities.decode - html4, attribute x 529,674 ops/sec ±0.90% (90 runs sampled)
  112. * #2: html-entities.decode - html4, body x 499,135 ops/sec ±2.27% (80 runs sampled)
  113. * #3: html-entities.decode - html4, strict x 489,806 ops/sec ±4.37% (84 runs sampled)
  114. XML
  115. Encode test
  116. * #1: html-entities.encode - xml, nonAscii x 823,097 ops/sec ±0.75% (81 runs sampled)
  117. * #2: html-entities.encode - xml, nonAsciiPrintable x 764,638 ops/sec ±0.93% (93 runs sampled)
  118. #3: entities.encodeXML x 672,186 ops/sec ±1.51% (92 runs sampled)
  119. * #4: html-entities.encode - xml, extensive x 376,870 ops/sec ±0.76% (77 runs sampled)
  120. Decode test
  121. #1: entities.decodeXML x 930,758 ops/sec ±2.90% (90 runs sampled)
  122. * #2: html-entities.decode - xml, body x 617,321 ops/sec ±0.74% (83 runs sampled)
  123. * #3: html-entities.decode - xml, attribute x 611,598 ops/sec ±0.50% (92 runs sampled)
  124. * #4: html-entities.decode - xml, strict x 607,191 ops/sec ±2.30% (85 runs sampled)
  125. Escaping
  126. Escape test
  127. #1: entities.escapeUTF8 x 1,930,874 ops/sec ±0.80% (95 runs sampled)
  128. #2: he.escape x 1,717,522 ops/sec ±0.75% (84 runs sampled)
  129. * #3: html-entities.encode - xml, specialChars x 1,611,374 ops/sec ±1.30% (92 runs sampled)
  130. #4: entities.escape x 673,710 ops/sec ±1.30% (94 runs sampled)
  131. ```
  132. License
  133. -------
  134. MIT
  135. Security contact information
  136. ----------------------------
  137. To report a security vulnerability, please use the
  138. [Tidelift security contact](https://tidelift.com/security). Tidelift will
  139. coordinate the fix and disclosure.
  140. `html-entities` for enterprise
  141. ------------------------------
  142. Available as part of the Tidelift Subscription
  143. The maintainers of `html-entities` and thousands of other packages are working with
  144. Tidelift to deliver commercial support and maintenance for the open source
  145. dependencies you use to build your applications. Save time, reduce risk, and
  146. improve code health, while paying the maintainers of the exact dependencies you
  147. use.
  148. [Learn more.](https://tidelift.com/subscription/pkg/npm-html-entities?utm_source=npm-html-entities&utm_medium=referral&utm_campaign=enterprise)