escape.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.escapeText = exports.escapeAttribute = exports.escapeUTF8 = exports.escape = exports.encodeXML = exports.getCodePoint = exports.xmlReplacer = void 0;
  6. exports.xmlReplacer = /["&'<>$\x80-\uFFFF]/g;
  7. var xmlCodeMap = new Map([
  8. [34, '&quot;'],
  9. [38, '&amp;'],
  10. [39, '&apos;'],
  11. [60, '&lt;'],
  12. [62, '&gt;']
  13. ]);
  14. // For compatibility with node < 4, we wrap `codePointAt`
  15. exports.getCodePoint =
  16. String.prototype.codePointAt != null
  17. ? function (str, index) {
  18. return str.codePointAt(index);
  19. }
  20. : // http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
  21. function (c, index) {
  22. return (c.charCodeAt(index) & 64512) === 55296 ? (c.charCodeAt(index) - 55296) * 1024 + c.charCodeAt(index + 1) - 56320 + 65536 : c.charCodeAt(index);
  23. };
  24. /**
  25. * Encodes all non-ASCII characters, as well as characters not valid in XML
  26. * documents using XML entities.
  27. *
  28. * If a character has no equivalent entity, a
  29. * numeric hexadecimal reference (eg. `&#xfc;`) will be used.
  30. */
  31. function encodeXML(str) {
  32. var ret = '';
  33. var lastIdx = 0;
  34. var match;
  35. while ((match = exports.xmlReplacer.exec(str)) !== null) {
  36. var i = match.index;
  37. var char = str.charCodeAt(i);
  38. var next = xmlCodeMap.get(char);
  39. if (next !== undefined) {
  40. ret += str.substring(lastIdx, i) + next;
  41. lastIdx = i + 1;
  42. } else {
  43. ret += ''.concat(str.substring(lastIdx, i), '&#x').concat((0, exports.getCodePoint)(str, i).toString(16), ';');
  44. // Increase by 1 if we have a surrogate pair
  45. lastIdx = exports.xmlReplacer.lastIndex += Number((char & 64512) === 55296);
  46. }
  47. }
  48. return ret + str.substr(lastIdx);
  49. }
  50. exports.encodeXML = encodeXML;
  51. /**
  52. * Encodes all non-ASCII characters, as well as characters not valid in XML
  53. * documents using numeric hexadecimal reference (eg. `&#xfc;`).
  54. *
  55. * Have a look at `escapeUTF8` if you want a more concise output at the expense
  56. * of reduced transportability.
  57. *
  58. * @param data String to escape.
  59. */
  60. exports.escape = encodeXML;
  61. function getEscaper(regex, map) {
  62. return function escape(data) {
  63. var match;
  64. var lastIdx = 0;
  65. var result = '';
  66. while ((match = regex.exec(data))) {
  67. if (lastIdx !== match.index) {
  68. result += data.substring(lastIdx, match.index);
  69. }
  70. // We know that this chararcter will be in the map.
  71. result += map.get(match[0].charCodeAt(0));
  72. // Every match will be of length 1
  73. lastIdx = match.index + 1;
  74. }
  75. return result + data.substring(lastIdx);
  76. };
  77. }
  78. /**
  79. * Encodes all characters not valid in XML documents using XML entities.
  80. *
  81. * Note that the output will be character-set dependent.
  82. *
  83. * @param data String to escape.
  84. */
  85. exports.escapeUTF8 = getEscaper(/[&<>'"]/g, xmlCodeMap);
  86. /**
  87. * Encodes all characters that have to be escaped in HTML attributes,
  88. * following {@link https://html.spec.whatwg.org/multipage/parsing.html#escapingString}.
  89. *
  90. * @param data String to escape.
  91. */
  92. exports.escapeAttribute = getEscaper(
  93. /["&\u00A0]/g,
  94. new Map([
  95. [34, '&quot;'],
  96. [38, '&amp;'],
  97. [160, '&nbsp;']
  98. ])
  99. );
  100. /**
  101. * Encodes all characters that have to be escaped in HTML text,
  102. * following {@link https://html.spec.whatwg.org/multipage/parsing.html#escapingString}.
  103. *
  104. * @param data String to escape.
  105. */
  106. exports.escapeText = getEscaper(
  107. /[&<>\u00A0]/g,
  108. new Map([
  109. [38, '&amp;'],
  110. [60, '&lt;'],
  111. [62, '&gt;'],
  112. [160, '&nbsp;']
  113. ])
  114. );
  115. //# sourceMappingURL=escape.js.map