encode-trie.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.getTrie = exports.encodeHTMLTrieRe = exports.getCodePoint = void 0;
  7. var entities_json_1 = __importDefault(require("./maps/entities.json"));
  8. function isHighSurrugate(c) {
  9. return (c & 64512 /* Mask */) === 55296 /* High */;
  10. }
  11. // For compatibility with node < 4, we wrap `codePointAt`
  12. exports.getCodePoint =
  13. // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
  14. String.prototype.codePointAt != null
  15. ? function (str, index) { return str.codePointAt(index); }
  16. : // http://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
  17. function (c, index) {
  18. return isHighSurrugate(c.charCodeAt(index))
  19. ? (c.charCodeAt(index) - 55296 /* High */) * 0x400 +
  20. c.charCodeAt(index + 1) -
  21. 0xdc00 +
  22. 0x10000
  23. : c.charCodeAt(index);
  24. };
  25. var htmlTrie = getTrie(entities_json_1.default);
  26. function encodeHTMLTrieRe(regExp, str) {
  27. var _a;
  28. var ret = "";
  29. var lastIdx = 0;
  30. var match;
  31. while ((match = regExp.exec(str)) !== null) {
  32. var i = match.index;
  33. var char = str.charCodeAt(i);
  34. var next = htmlTrie.get(char);
  35. if (next) {
  36. if (next.next != null && i + 1 < str.length) {
  37. var value = (_a = next.next.get(str.charCodeAt(i + 1))) === null || _a === void 0 ? void 0 : _a.value;
  38. if (value != null) {
  39. ret += str.substring(lastIdx, i) + value;
  40. regExp.lastIndex += 1;
  41. lastIdx = i + 2;
  42. continue;
  43. }
  44. }
  45. ret += str.substring(lastIdx, i) + next.value;
  46. lastIdx = i + 1;
  47. }
  48. else {
  49. ret += str.substring(lastIdx, i) + "&#x" + exports.getCodePoint(str, i).toString(16) + ";";
  50. // Increase by 1 if we have a surrogate pair
  51. lastIdx = regExp.lastIndex += Number(isHighSurrugate(char));
  52. }
  53. }
  54. return ret + str.substr(lastIdx);
  55. }
  56. exports.encodeHTMLTrieRe = encodeHTMLTrieRe;
  57. function getTrie(map) {
  58. var _a, _b, _c, _d;
  59. var trie = new Map();
  60. for (var _i = 0, _e = Object.keys(map); _i < _e.length; _i++) {
  61. var value = _e[_i];
  62. var key = map[value];
  63. // Resolve the key
  64. var lastMap = trie;
  65. for (var i = 0; i < key.length - 1; i++) {
  66. var char = key.charCodeAt(i);
  67. var next = (_a = lastMap.get(char)) !== null && _a !== void 0 ? _a : {};
  68. lastMap.set(char, next);
  69. lastMap = (_b = next.next) !== null && _b !== void 0 ? _b : (next.next = new Map());
  70. }
  71. var val = (_c = lastMap.get(key.charCodeAt(key.length - 1))) !== null && _c !== void 0 ? _c : {};
  72. (_d = val.value) !== null && _d !== void 0 ? _d : (val.value = "&" + value + ";");
  73. lastMap.set(key.charCodeAt(key.length - 1), val);
  74. }
  75. return trie;
  76. }
  77. exports.getTrie = getTrie;