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.

пре 4 година
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <h1>
  2. <img src="logo.jpg" width="1280" alt="escape-goat">
  3. </h1>
  4. > Escape a string for use in HTML or the inverse
  5. [![Build Status](https://travis-ci.org/sindresorhus/escape-goat.svg?branch=master)](https://travis-ci.org/sindresorhus/escape-goat)
  6. ## Install
  7. ```
  8. $ npm install escape-goat
  9. ```
  10. ## Usage
  11. ```js
  12. const {htmlEscape, htmlUnescape, htmlEscapeTag, htmlUnescapeTag} = require('escape-goat');
  13. htmlEscape('🦄 & 🐐');
  14. //=> '🦄 &amp; 🐐'
  15. htmlUnescape('🦄 &amp; 🐐');
  16. //=> '🦄 & 🐐'
  17. htmlEscape('Hello <em>World</em>');
  18. //=> 'Hello &lt;em&gt;World&lt;/em&gt;'
  19. const url = 'https://sindresorhus.com?x="🦄"';
  20. htmlEscapeTag`<a href="${url}">Unicorn</a>`;
  21. //=> '<a href="https://sindresorhus.com?x=&quot;🦄&quot;">Unicorn</a>'
  22. const escapedUrl = 'https://sindresorhus.com?x=&quot;🦄&quot;';
  23. htmlUnescapeTag`URL from HTML: ${url}`;
  24. //=> 'URL from HTML: https://sindresorhus.com?x="🦄"'
  25. ```
  26. ## API
  27. ### htmlEscape(string)
  28. Escapes the following characters in the given `string` argument: `&` `<` `>` `"` `'`
  29. ### htmlUnescape(htmlString)
  30. Unescapes the following HTML entities in the given `htmlString` argument: `&amp;` `&lt;` `&gt;` `&quot;` `&#39;`
  31. ### htmlEscapeTag
  32. [Tagged template literal](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Template_literals#Tagged_template_literals) that escapes interpolated values.
  33. ### htmlUnescapeTag
  34. [Tagged template literal](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Template_literals#Tagged_template_literals) that unescapes interpolated values.
  35. ## Tip
  36. Ensure you always quote your HTML attributes to prevent possible [XSS](https://en.wikipedia.org/wiki/Cross-site_scripting).
  37. ## FAQ
  38. ### Why yet another HTML escaping package?
  39. I couldn't find one I liked that was tiny, well-tested, and had both `.escape()` and `.unescape()`.
  40. ## License
  41. MIT © [Sindre Sorhus](https://sindresorhus.com)