xml-escape.js 936 B

12345678910111213141516171819202122232425262728293031323334
  1. /**
  2. * @fileoverview XML character escaper
  3. * @author George Chung
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Public Interface
  8. //------------------------------------------------------------------------------
  9. /**
  10. * Returns the escaped value for a character
  11. * @param {string} s string to examine
  12. * @returns {string} severity level
  13. * @private
  14. */
  15. module.exports = function(s) {
  16. return (`${s}`).replace(/[<>&"'\x00-\x1F\x7F\u0080-\uFFFF]/gu, c => { // eslint-disable-line no-control-regex
  17. switch (c) {
  18. case "<":
  19. return "&lt;";
  20. case ">":
  21. return "&gt;";
  22. case "&":
  23. return "&amp;";
  24. case "\"":
  25. return "&quot;";
  26. case "'":
  27. return "&apos;";
  28. default:
  29. return `&#${c.charCodeAt(0)};`;
  30. }
  31. });
  32. };