index.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. "use strict";
  2. var __assign = (this && this.__assign) || function () {
  3. __assign = Object.assign || function(t) {
  4. for (var s, i = 1, n = arguments.length; i < n; i++) {
  5. s = arguments[i];
  6. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  7. t[p] = s[p];
  8. }
  9. return t;
  10. };
  11. return __assign.apply(this, arguments);
  12. };
  13. Object.defineProperty(exports, "__esModule", { value: true });
  14. var named_references_1 = require("./named-references");
  15. var numeric_unicode_map_1 = require("./numeric-unicode-map");
  16. var surrogate_pairs_1 = require("./surrogate-pairs");
  17. var allNamedReferences = __assign(__assign({}, named_references_1.namedReferences), { all: named_references_1.namedReferences.html5 });
  18. var encodeRegExps = {
  19. specialChars: /[<>'"&]/g,
  20. nonAscii: /(?:[<>'"&\u0080-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])/g,
  21. nonAsciiPrintable: /(?:[<>'"&\x01-\x08\x11-\x15\x17-\x1F\x7f-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])/g,
  22. extensive: /(?:[\x01-\x0c\x0e-\x1f\x21-\x2c\x2e-\x2f\x3a-\x40\x5b-\x60\x7b-\x7d\x7f-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])/g
  23. };
  24. var defaultEncodeOptions = {
  25. mode: 'specialChars',
  26. level: 'all',
  27. numeric: 'decimal'
  28. };
  29. /** Encodes all the necessary (specified by `level`) characters in the text */
  30. function encode(text, _a) {
  31. var _b = _a === void 0 ? defaultEncodeOptions : _a, _c = _b.mode, mode = _c === void 0 ? 'specialChars' : _c, _d = _b.numeric, numeric = _d === void 0 ? 'decimal' : _d, _e = _b.level, level = _e === void 0 ? 'all' : _e;
  32. if (!text) {
  33. return '';
  34. }
  35. var encodeRegExp = encodeRegExps[mode];
  36. var references = allNamedReferences[level].characters;
  37. var isHex = numeric === 'hexadecimal';
  38. encodeRegExp.lastIndex = 0;
  39. var _b = encodeRegExp.exec(text);
  40. var _c;
  41. if (_b) {
  42. _c = '';
  43. var _d = 0;
  44. do {
  45. if (_d !== _b.index) {
  46. _c += text.substring(_d, _b.index);
  47. }
  48. var _e = _b[0];
  49. var result_1 = references[_e];
  50. if (!result_1) {
  51. var code_1 = _e.length > 1 ? surrogate_pairs_1.getCodePoint(_e, 0) : _e.charCodeAt(0);
  52. result_1 = (isHex ? '&#x' + code_1.toString(16) : '&#' + code_1) + ';';
  53. }
  54. _c += result_1;
  55. _d = _b.index + _e.length;
  56. } while ((_b = encodeRegExp.exec(text)));
  57. if (_d !== text.length) {
  58. _c += text.substring(_d);
  59. }
  60. }
  61. else {
  62. _c =
  63. text;
  64. }
  65. return _c;
  66. }
  67. exports.encode = encode;
  68. var defaultDecodeOptions = {
  69. scope: 'body',
  70. level: 'all'
  71. };
  72. var strict = /&(?:#\d+|#[xX][\da-fA-F]+|[0-9a-zA-Z]+);/g;
  73. var attribute = /&(?:#\d+|#[xX][\da-fA-F]+|[0-9a-zA-Z]+)[;=]?/g;
  74. var baseDecodeRegExps = {
  75. xml: {
  76. strict: strict,
  77. attribute: attribute,
  78. body: named_references_1.bodyRegExps.xml
  79. },
  80. html4: {
  81. strict: strict,
  82. attribute: attribute,
  83. body: named_references_1.bodyRegExps.html4
  84. },
  85. html5: {
  86. strict: strict,
  87. attribute: attribute,
  88. body: named_references_1.bodyRegExps.html5
  89. }
  90. };
  91. var decodeRegExps = __assign(__assign({}, baseDecodeRegExps), { all: baseDecodeRegExps.html5 });
  92. var fromCharCode = String.fromCharCode;
  93. var outOfBoundsChar = fromCharCode(65533);
  94. var defaultDecodeEntityOptions = {
  95. level: 'all'
  96. };
  97. /** Decodes a single entity */
  98. function decodeEntity(entity, _a) {
  99. var _b = (_a === void 0 ? defaultDecodeEntityOptions : _a).level, level = _b === void 0 ? 'all' : _b;
  100. if (!entity) {
  101. return '';
  102. }
  103. var _b = entity;
  104. var decodeEntityLastChar_1 = entity[entity.length - 1];
  105. if (false
  106. && decodeEntityLastChar_1 === '=') {
  107. _b =
  108. entity;
  109. }
  110. else if (false
  111. && decodeEntityLastChar_1 !== ';') {
  112. _b =
  113. entity;
  114. }
  115. else {
  116. var decodeResultByReference_1 = allNamedReferences[level].entities[entity];
  117. if (decodeResultByReference_1) {
  118. _b = decodeResultByReference_1;
  119. }
  120. else if (entity[0] === '&' && entity[1] === '#') {
  121. var decodeSecondChar_1 = entity[2];
  122. var decodeCode_1 = decodeSecondChar_1 == 'x' || decodeSecondChar_1 == 'X'
  123. ? parseInt(entity.substr(3), 16)
  124. : parseInt(entity.substr(2));
  125. _b =
  126. decodeCode_1 >= 0x10ffff
  127. ? outOfBoundsChar
  128. : decodeCode_1 > 65535
  129. ? surrogate_pairs_1.fromCodePoint(decodeCode_1)
  130. : fromCharCode(numeric_unicode_map_1.numericUnicodeMap[decodeCode_1] || decodeCode_1);
  131. }
  132. }
  133. return _b;
  134. }
  135. exports.decodeEntity = decodeEntity;
  136. /** Decodes all entities in the text */
  137. function decode(text, _a) {
  138. var decodeSecondChar_1 = _a === void 0 ? defaultDecodeOptions : _a, decodeCode_1 = decodeSecondChar_1.level, level = decodeCode_1 === void 0 ? 'all' : decodeCode_1, _b = decodeSecondChar_1.scope, scope = _b === void 0 ? level === 'xml' ? 'strict' : 'body' : _b;
  139. if (!text) {
  140. return '';
  141. }
  142. var decodeRegExp = decodeRegExps[level][scope];
  143. var references = allNamedReferences[level].entities;
  144. var isAttribute = scope === 'attribute';
  145. var isStrict = scope === 'strict';
  146. decodeRegExp.lastIndex = 0;
  147. var replaceMatch_1 = decodeRegExp.exec(text);
  148. var replaceResult_1;
  149. if (replaceMatch_1) {
  150. replaceResult_1 = '';
  151. var replaceLastIndex_1 = 0;
  152. do {
  153. if (replaceLastIndex_1 !== replaceMatch_1.index) {
  154. replaceResult_1 += text.substring(replaceLastIndex_1, replaceMatch_1.index);
  155. }
  156. var replaceInput_1 = replaceMatch_1[0];
  157. var decodeResult_1 = replaceInput_1;
  158. var decodeEntityLastChar_2 = replaceInput_1[replaceInput_1.length - 1];
  159. if (isAttribute
  160. && decodeEntityLastChar_2 === '=') {
  161. decodeResult_1 = replaceInput_1;
  162. }
  163. else if (isStrict
  164. && decodeEntityLastChar_2 !== ';') {
  165. decodeResult_1 = replaceInput_1;
  166. }
  167. else {
  168. var decodeResultByReference_2 = references[replaceInput_1];
  169. if (decodeResultByReference_2) {
  170. decodeResult_1 = decodeResultByReference_2;
  171. }
  172. else if (replaceInput_1[0] === '&' && replaceInput_1[1] === '#') {
  173. var decodeSecondChar_2 = replaceInput_1[2];
  174. var decodeCode_2 = decodeSecondChar_2 == 'x' || decodeSecondChar_2 == 'X'
  175. ? parseInt(replaceInput_1.substr(3), 16)
  176. : parseInt(replaceInput_1.substr(2));
  177. decodeResult_1 =
  178. decodeCode_2 >= 0x10ffff
  179. ? outOfBoundsChar
  180. : decodeCode_2 > 65535
  181. ? surrogate_pairs_1.fromCodePoint(decodeCode_2)
  182. : fromCharCode(numeric_unicode_map_1.numericUnicodeMap[decodeCode_2] || decodeCode_2);
  183. }
  184. }
  185. replaceResult_1 += decodeResult_1;
  186. replaceLastIndex_1 = replaceMatch_1.index + replaceInput_1.length;
  187. } while ((replaceMatch_1 = decodeRegExp.exec(text)));
  188. if (replaceLastIndex_1 !== text.length) {
  189. replaceResult_1 += text.substring(replaceLastIndex_1);
  190. }
  191. }
  192. else {
  193. replaceResult_1 =
  194. text;
  195. }
  196. return replaceResult_1;
  197. }
  198. exports.decode = decode;