index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. var __assign = (this && this.__assign) || function () {
  2. __assign = Object.assign || function(t) {
  3. for (var s, i = 1, n = arguments.length; i < n; i++) {
  4. s = arguments[i];
  5. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  6. t[p] = s[p];
  7. }
  8. return t;
  9. };
  10. return __assign.apply(this, arguments);
  11. };
  12. import { bodyRegExps, namedReferences } from './named-references.js';
  13. import { numericUnicodeMap } from './numeric-unicode-map.js';
  14. import { fromCodePoint, getCodePoint } from './surrogate-pairs.js';
  15. var allNamedReferences = __assign(__assign({}, namedReferences), { all: namedReferences.html5 });
  16. var encodeRegExps = {
  17. specialChars: /[<>'"&]/g,
  18. nonAscii: /[<>'"&\u0080-\uD7FF\uE000-\uFFFF\uDC00-\uDFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]?/g,
  19. nonAsciiPrintable: /[<>'"&\x01-\x08\x11-\x15\x17-\x1F\x7f-\uD7FF\uE000-\uFFFF\uDC00-\uDFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]?/g,
  20. nonAsciiPrintableOnly: /[\x01-\x08\x11-\x15\x17-\x1F\x7f-\uD7FF\uE000-\uFFFF\uDC00-\uDFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]?/g,
  21. extensive: /[\x01-\x0c\x0e-\x1f\x21-\x2c\x2e-\x2f\x3a-\x40\x5b-\x60\x7b-\x7d\x7f-\uD7FF\uE000-\uFFFF\uDC00-\uDFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]?/g
  22. };
  23. var defaultEncodeOptions = {
  24. mode: 'specialChars',
  25. level: 'all',
  26. numeric: 'decimal'
  27. };
  28. /** Encodes all the necessary (specified by `level`) characters in the text */
  29. export function encode(text, _a) {
  30. 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;
  31. if (!text) {
  32. return '';
  33. }
  34. var encodeRegExp = encodeRegExps[mode];
  35. var references = allNamedReferences[level].characters;
  36. var isHex = numeric === 'hexadecimal';
  37. return String.prototype.replace.call(text, encodeRegExp, function (input) {
  38. var result = references[input];
  39. if (!result) {
  40. var code = input.length > 1 ? getCodePoint(input, 0) : input.charCodeAt(0);
  41. result = (isHex ? '&#x' + code.toString(16) : '&#' + code) + ';';
  42. }
  43. return result;
  44. });
  45. }
  46. var defaultDecodeOptions = {
  47. scope: 'body',
  48. level: 'all'
  49. };
  50. var strict = /&(?:#\d+|#[xX][\da-fA-F]+|[0-9a-zA-Z]+);/g;
  51. var attribute = /&(?:#\d+|#[xX][\da-fA-F]+|[0-9a-zA-Z]+)[;=]?/g;
  52. var baseDecodeRegExps = {
  53. xml: {
  54. strict: strict,
  55. attribute: attribute,
  56. body: bodyRegExps.xml
  57. },
  58. html4: {
  59. strict: strict,
  60. attribute: attribute,
  61. body: bodyRegExps.html4
  62. },
  63. html5: {
  64. strict: strict,
  65. attribute: attribute,
  66. body: bodyRegExps.html5
  67. }
  68. };
  69. var decodeRegExps = __assign(__assign({}, baseDecodeRegExps), { all: baseDecodeRegExps.html5 });
  70. var fromCharCode = String.fromCharCode;
  71. var outOfBoundsChar = fromCharCode(65533);
  72. var defaultDecodeEntityOptions = {
  73. level: 'all'
  74. };
  75. function getDecodedEntity(entity, references, isAttribute, isStrict) {
  76. var decodeResult = entity;
  77. var decodeEntityLastChar = entity[entity.length - 1];
  78. if (isAttribute && decodeEntityLastChar === '=') {
  79. decodeResult = entity;
  80. }
  81. else if (isStrict && decodeEntityLastChar !== ';') {
  82. decodeResult = entity;
  83. }
  84. else {
  85. var decodeResultByReference = references[entity];
  86. if (decodeResultByReference) {
  87. decodeResult = decodeResultByReference;
  88. }
  89. else if (entity[0] === '&' && entity[1] === '#') {
  90. var decodeSecondChar = entity[2];
  91. var decodeCode = decodeSecondChar == 'x' || decodeSecondChar == 'X'
  92. ? parseInt(entity.substr(3), 16)
  93. : parseInt(entity.substr(2));
  94. decodeResult =
  95. decodeCode >= 0x10ffff
  96. ? outOfBoundsChar
  97. : decodeCode > 65535
  98. ? fromCodePoint(decodeCode)
  99. : fromCharCode(numericUnicodeMap[decodeCode] || decodeCode);
  100. }
  101. }
  102. return decodeResult;
  103. }
  104. /** Decodes a single entity */
  105. export function decodeEntity(entity, _a) {
  106. var _b = _a === void 0 ? defaultDecodeEntityOptions : _a, _c = _b.level, level = _c === void 0 ? 'all' : _c;
  107. if (!entity) {
  108. return '';
  109. }
  110. return getDecodedEntity(entity, allNamedReferences[level].entities, false, false);
  111. }
  112. /** Decodes all entities in the text */
  113. export function decode(text, _a) {
  114. var _b = _a === void 0 ? defaultDecodeOptions : _a, _c = _b.level, level = _c === void 0 ? 'all' : _c, _d = _b.scope, scope = _d === void 0 ? level === 'xml' ? 'strict' : 'body' : _d;
  115. if (!text) {
  116. return '';
  117. }
  118. var decodeRegExp = decodeRegExps[level][scope];
  119. var references = allNamedReferences[level].entities;
  120. var isAttribute = scope === 'attribute';
  121. var isStrict = scope === 'strict';
  122. return text.replace(decodeRegExp, function (entity) { return getDecodedEntity(entity, references, isAttribute, isStrict); });
  123. }
  124. //# sourceMappingURL=index.js.map