escape.js 4.2 KB

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