encode.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. 'use strict';
  2. var __importDefault =
  3. (this && this.__importDefault) ||
  4. function (mod) {
  5. return mod && mod.__esModule
  6. ? mod
  7. : {
  8. default: mod
  9. };
  10. };
  11. Object.defineProperty(exports, '__esModule', {
  12. value: true
  13. });
  14. exports.encodeNonAsciiHTML = exports.encodeHTML = void 0;
  15. var encode_html_js_1 = __importDefault(require('./generated/encode-html.js'));
  16. var escape_js_1 = require('./escape.js');
  17. var htmlReplacer = /[\t\n!-,./:-@[-`\f{-}$\x80-\uFFFF]/g;
  18. /**
  19. * Encodes all characters in the input using HTML entities. This includes
  20. * characters that are valid ASCII characters in HTML documents, such as `#`.
  21. *
  22. * To get a more compact output, consider using the `encodeNonAsciiHTML`
  23. * function, which will only encode characters that are not valid in HTML
  24. * documents, as well as non-ASCII characters.
  25. *
  26. * If a character has no equivalent entity, a numeric hexadecimal reference
  27. * (eg. `ü`) will be used.
  28. */
  29. function encodeHTML(data) {
  30. return encodeHTMLTrieRe(htmlReplacer, data);
  31. }
  32. exports.encodeHTML = encodeHTML;
  33. /**
  34. * Encodes all non-ASCII characters, as well as characters not valid in HTML
  35. * documents using HTML entities. This function will not encode characters that
  36. * are valid in HTML documents, such as `#`.
  37. *
  38. * If a character has no equivalent entity, a numeric hexadecimal reference
  39. * (eg. `ü`) will be used.
  40. */
  41. function encodeNonAsciiHTML(data) {
  42. return encodeHTMLTrieRe(escape_js_1.xmlReplacer, data);
  43. }
  44. exports.encodeNonAsciiHTML = encodeNonAsciiHTML;
  45. function encodeHTMLTrieRe(regExp, str) {
  46. var ret = '';
  47. var lastIdx = 0;
  48. var match;
  49. while ((match = regExp.exec(str)) !== null) {
  50. var i = match.index;
  51. ret += str.substring(lastIdx, i);
  52. var char = str.charCodeAt(i);
  53. var next = encode_html_js_1.default.get(char);
  54. if (typeof next === 'object') {
  55. // We are in a branch. Try to match the next char.
  56. if (i + 1 < str.length) {
  57. var nextChar = str.charCodeAt(i + 1);
  58. var value = typeof next.n === 'number' ? (next.n === nextChar ? next.o : undefined) : next.n.get(nextChar);
  59. if (value !== undefined) {
  60. ret += value;
  61. lastIdx = regExp.lastIndex += 1;
  62. continue;
  63. }
  64. }
  65. next = next.v;
  66. }
  67. // We might have a tree node without a value; skip and use a numeric entitiy.
  68. if (next !== undefined) {
  69. ret += next;
  70. lastIdx = i + 1;
  71. } else {
  72. var cp = (0, escape_js_1.getCodePoint)(str, i);
  73. ret += '&#x'.concat(cp.toString(16), ';');
  74. // Increase by 1 if we have a surrogate pair
  75. lastIdx = regExp.lastIndex += Number(cp !== char);
  76. }
  77. }
  78. return ret + str.substr(lastIdx);
  79. }
  80. //# sourceMappingURL=encode.js.map