decode.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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.decodeXML = exports.decodeHTMLStrict = exports.decodeHTML = exports.determineBranch = exports.JUMP_OFFSET_BASE = exports.BinTrieFlags = exports.xmlDecodeTree = exports.htmlDecodeTree = void 0;
  7. var decode_data_html_1 = __importDefault(require("./generated/decode-data-html"));
  8. exports.htmlDecodeTree = decode_data_html_1.default;
  9. var decode_data_xml_1 = __importDefault(require("./generated/decode-data-xml"));
  10. exports.xmlDecodeTree = decode_data_xml_1.default;
  11. var decode_codepoint_1 = __importDefault(require("./decode_codepoint"));
  12. var BinTrieFlags;
  13. (function (BinTrieFlags) {
  14. BinTrieFlags[BinTrieFlags["HAS_VALUE"] = 32768] = "HAS_VALUE";
  15. BinTrieFlags[BinTrieFlags["BRANCH_LENGTH"] = 32512] = "BRANCH_LENGTH";
  16. BinTrieFlags[BinTrieFlags["MULTI_BYTE"] = 128] = "MULTI_BYTE";
  17. BinTrieFlags[BinTrieFlags["JUMP_TABLE"] = 127] = "JUMP_TABLE";
  18. })(BinTrieFlags = exports.BinTrieFlags || (exports.BinTrieFlags = {}));
  19. exports.JUMP_OFFSET_BASE = 48 /* ZERO */ - 1;
  20. function getDecoder(decodeTree) {
  21. return function decodeHTMLBinary(str, strict) {
  22. var ret = "";
  23. var lastIdx = 0;
  24. var strIdx = 0;
  25. while ((strIdx = str.indexOf("&", strIdx)) >= 0) {
  26. ret += str.slice(lastIdx, strIdx);
  27. lastIdx = strIdx;
  28. // Skip the "&"
  29. strIdx += 1;
  30. // If we have a numeric entity, handle this separately.
  31. if (str.charCodeAt(strIdx) === 35 /* NUM */) {
  32. // Skip the leading "&#". For hex entities, also skip the leading "x".
  33. var start = strIdx + 1;
  34. var base = 10;
  35. var cp = str.charCodeAt(start);
  36. if ((cp | 32 /* To_LOWER_BIT */) === 120 /* LOWER_X */) {
  37. base = 16;
  38. strIdx += 1;
  39. start += 1;
  40. }
  41. while (((cp = str.charCodeAt(++strIdx)) >= 48 /* ZERO */ &&
  42. cp <= 57 /* NINE */) ||
  43. (base === 16 &&
  44. (cp | 32 /* To_LOWER_BIT */) >= 97 /* LOWER_A */ &&
  45. (cp | 32 /* To_LOWER_BIT */) <= 102 /* LOWER_F */))
  46. ;
  47. if (start !== strIdx) {
  48. var entity = str.substring(start, strIdx);
  49. var parsed = parseInt(entity, base);
  50. if (str.charCodeAt(strIdx) === 59 /* SEMI */) {
  51. strIdx += 1;
  52. }
  53. else if (strict) {
  54. continue;
  55. }
  56. ret += decode_codepoint_1.default(parsed);
  57. lastIdx = strIdx;
  58. }
  59. continue;
  60. }
  61. var result = null;
  62. var excess = 1;
  63. var treeIdx = 0;
  64. var current = decodeTree[treeIdx];
  65. for (; strIdx < str.length; strIdx++, excess++) {
  66. treeIdx = determineBranch(decodeTree, current, treeIdx + 1, str.charCodeAt(strIdx));
  67. if (treeIdx < 0)
  68. break;
  69. current = decodeTree[treeIdx];
  70. // If the branch is a value, store it and continue
  71. if (current & BinTrieFlags.HAS_VALUE) {
  72. // If we have a legacy entity while parsing strictly, just skip the number of bytes
  73. if (strict && str.charCodeAt(strIdx) !== 59 /* SEMI */) {
  74. // No need to consider multi-byte values, as the legacy entity is always a single byte
  75. treeIdx += 1;
  76. }
  77. else {
  78. // If this is a surrogate pair, combine the higher bits from the node with the next byte
  79. result =
  80. current & BinTrieFlags.MULTI_BYTE
  81. ? String.fromCharCode(decodeTree[++treeIdx], decodeTree[++treeIdx])
  82. : String.fromCharCode(decodeTree[++treeIdx]);
  83. excess = 0;
  84. }
  85. }
  86. }
  87. if (result != null) {
  88. ret += result;
  89. lastIdx = strIdx - excess + 1;
  90. }
  91. }
  92. return ret + str.slice(lastIdx);
  93. };
  94. }
  95. function determineBranch(decodeTree, current, nodeIdx, char) {
  96. if (current <= 128) {
  97. return char === current ? nodeIdx : -1;
  98. }
  99. var branchCount = (current & BinTrieFlags.BRANCH_LENGTH) >> 8;
  100. if (branchCount === 0) {
  101. return -1;
  102. }
  103. if (branchCount === 1) {
  104. return char === decodeTree[nodeIdx] ? nodeIdx + 1 : -1;
  105. }
  106. var jumpOffset = current & BinTrieFlags.JUMP_TABLE;
  107. if (jumpOffset) {
  108. var value = char - exports.JUMP_OFFSET_BASE - jumpOffset;
  109. return value < 0 || value > branchCount
  110. ? -1
  111. : decodeTree[nodeIdx + value] - 1;
  112. }
  113. // Binary search for the character.
  114. var lo = nodeIdx;
  115. var hi = lo + branchCount - 1;
  116. while (lo <= hi) {
  117. var mid = (lo + hi) >>> 1;
  118. var midVal = decodeTree[mid];
  119. if (midVal < char) {
  120. lo = mid + 1;
  121. }
  122. else if (midVal > char) {
  123. hi = mid - 1;
  124. }
  125. else {
  126. return decodeTree[mid + branchCount];
  127. }
  128. }
  129. return -1;
  130. }
  131. exports.determineBranch = determineBranch;
  132. var htmlDecoder = getDecoder(decode_data_html_1.default);
  133. var xmlDecoder = getDecoder(decode_data_xml_1.default);
  134. function decodeHTML(str) {
  135. return htmlDecoder(str, false);
  136. }
  137. exports.decodeHTML = decodeHTML;
  138. function decodeHTMLStrict(str) {
  139. return htmlDecoder(str, true);
  140. }
  141. exports.decodeHTMLStrict = decodeHTMLStrict;
  142. function decodeXML(str) {
  143. return xmlDecoder(str, true);
  144. }
  145. exports.decodeXML = decodeXML;