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.

122 lines
5.0 KiB

  1. # entities [![NPM version](https://img.shields.io/npm/v/entities.svg)](https://npmjs.org/package/entities) [![Downloads](https://img.shields.io/npm/dm/entities.svg)](https://npmjs.org/package/entities) [![Node.js CI](https://github.com/fb55/entities/actions/workflows/nodejs-test.yml/badge.svg)](https://github.com/fb55/entities/actions/workflows/nodejs-test.yml)
  2. Encode & decode HTML & XML entities with ease & speed.
  3. ## Features
  4. - 😇 Tried and true: `entities` is used by many popular libraries; eg.
  5. [`htmlparser2`](https://github.com/fb55/htmlparser2), the official
  6. [AWS SDK](https://github.com/aws/aws-sdk-js-v3) and
  7. [`commonmark`](https://github.com/commonmark/commonmark.js) use it to
  8. process HTML entities.
  9. - ⚡️ Fast: `entities` is the fastest library for decoding HTML entities (as
  10. of April 2022); see [performance](#performance).
  11. - 🎛 Configurable: Get an output tailored for your needs. You are fine with
  12. UTF8? That'll save you some bytes. Prefer to only have ASCII characters? We
  13. can do that as well!
  14. ## How to…
  15. ### …install `entities`
  16. npm install entities
  17. ### …use `entities`
  18. ```javascript
  19. const entities = require("entities");
  20. // Encoding
  21. entities.escapeUTF8("& ü"); // "& ü"
  22. entities.encodeXML("& ü"); // "& ü"
  23. entities.encodeHTML("& ü"); // "& ü"
  24. // Decoding
  25. entities.decodeXML("asdf & ÿ ü '"); // "asdf & ÿ ü '"
  26. entities.decodeHTML("asdf & ÿ ü '"); // "asdf & ÿ ü '"
  27. ```
  28. ## Performance
  29. This is how `entities` compares to other libraries on a very basic benchmark
  30. (see `scripts/benchmark.ts`, for 10,000,000 iterations; **lower is better**):
  31. | Library | Version | `decode` perf | `encode` perf | `escape` perf |
  32. | -------------- | ------- | ------------- | ------------- | ------------- |
  33. | entities | `3.0.1` | 1.418s | 6.786s | 2.196s |
  34. | html-entities | `2.3.2` | 2.530s | 6.829s | 2.415s |
  35. | he | `1.2.0` | 5.800s | 24.237s | 3.624s |
  36. | parse-entities | `3.0.0` | 9.660s | N/A | N/A |
  37. ---
  38. ## FAQ
  39. > What methods should I actually use to encode my documents?
  40. If your target supports UTF-8, the `escapeUTF8` method is going to be your best
  41. choice. Otherwise, use either `encodeHTML` or `encodeXML` based on whether
  42. you're dealing with an HTML or an XML document.
  43. You can have a look at the options for the `encode` and `decode` methods to see
  44. everything you can configure.
  45. > When should I use strict decoding?
  46. When strict decoding, entities not terminated with a semicolon will be ignored.
  47. This is helpful for decoding entities in legacy environments.
  48. > Why should I use `entities` instead of alternative modules?
  49. As of April 2022, `entities` is a bit faster than other modules. Still, this is
  50. not a very differentiated space and other modules can catch up.
  51. **More importantly**, you might already have `entities` in your dependency graph
  52. (as a dependency of eg. `cheerio`, or `htmlparser2`), and including it directly
  53. might not even increase your bundle size. The same is true for other entity
  54. libraries, so have a look through your `node_modules` directory!
  55. > Does `entities` support tree shaking?
  56. Yes! `entities` ships as both a CommonJS and a ES module. Note that for best
  57. results, you should not use the `encode` and `decode` functions, as they wrap
  58. around a number of other functions, all of which will remain in the bundle.
  59. Instead, use the functions that you need directly.
  60. ---
  61. ## Acknowledgements
  62. This library wouldn't be possible without the work of these individuals. Thanks
  63. to
  64. - [@mathiasbynens](https://github.com/mathiasbynens) for his explanations
  65. about character encodings, and his library `he`, which was one of the
  66. inspirations for `entities`
  67. - [@inikulin](https://github.com/inikulin) for his work on optimized tries for
  68. decoding HTML entities for the `parse5` project
  69. - [@mdevils](https://github.com/mdevils) for taking on the challenge of
  70. producing a quick entity library with his `html-entities` library.
  71. `entities` would be quite a bit slower if there wasn't any competition.
  72. Right now `entities` is on top, but we'll see how long that lasts!
  73. ---
  74. License: BSD-2-Clause
  75. ## Security contact information
  76. To report a security vulnerability, please use the
  77. [Tidelift security contact](https://tidelift.com/security). Tidelift will
  78. coordinate the fix and disclosure.
  79. ## `entities` for enterprise
  80. Available as part of the Tidelift Subscription
  81. The maintainers of `entities` and thousands of other packages are working with
  82. Tidelift to deliver commercial support and maintenance for the open source
  83. dependencies you use to build your applications. Save time, reduce risk, and
  84. improve code health, while paying the maintainers of the exact dependencies you
  85. use.
  86. [Learn more.](https://tidelift.com/subscription/pkg/npm-entities?utm_source=npm-entities&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)