escape_html.js 488 B

12345678910111213141516171819202122232425
  1. 'use strict';
  2. const unescapeHTML = require('./unescape_html');
  3. const htmlEntityMap = {
  4. '&': '&',
  5. '<': '&lt;',
  6. '>': '&gt;',
  7. '"': '&quot;',
  8. '\'': '&#39;',
  9. '`': '&#96;',
  10. '/': '&#x2F;',
  11. '=': '&#x3D;'
  12. };
  13. function escapeHTML(str) {
  14. if (typeof str !== 'string') throw new TypeError('str must be a string!');
  15. str = unescapeHTML(str);
  16. // http://stackoverflow.com/a/12034334
  17. return str.replace(/[&<>"'`/=]/g, a => htmlEntityMap[a]);
  18. }
  19. module.exports = escapeHTML;