index.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.decodeXMLStrict = exports.decodeHTML5Strict = exports.decodeHTML4Strict = exports.decodeHTML5 = exports.decodeHTML4 = exports.decodeHTMLStrict = exports.decodeHTML = exports.decodeXML = exports.encodeHTML5 = exports.encodeHTML4 = exports.escapeUTF8 = exports.escape = exports.encodeNonAsciiHTML = exports.encodeHTML = exports.encodeXML = exports.encode = exports.decodeStrict = exports.decode = exports.EncodingMode = exports.DecodingMode = exports.EntityLevel = void 0;
  4. var decode_1 = require("./decode");
  5. var encode_1 = require("./encode");
  6. /** The level of entities to support. */
  7. var EntityLevel;
  8. (function (EntityLevel) {
  9. /** Support only XML entities. */
  10. EntityLevel[EntityLevel["XML"] = 0] = "XML";
  11. /** Support HTML entities, which are a superset of XML entities. */
  12. EntityLevel[EntityLevel["HTML"] = 1] = "HTML";
  13. })(EntityLevel = exports.EntityLevel || (exports.EntityLevel = {}));
  14. /** Determines whether some entities are allowed to be written without a trailing `;`. */
  15. var DecodingMode;
  16. (function (DecodingMode) {
  17. /** Support legacy HTML entities. */
  18. DecodingMode[DecodingMode["Legacy"] = 0] = "Legacy";
  19. /** Do not support legacy HTML entities. */
  20. DecodingMode[DecodingMode["Strict"] = 1] = "Strict";
  21. })(DecodingMode = exports.DecodingMode || (exports.DecodingMode = {}));
  22. var EncodingMode;
  23. (function (EncodingMode) {
  24. /**
  25. * The output is UTF-8 encoded. Only characters that need escaping within
  26. * HTML will be escaped.
  27. */
  28. EncodingMode[EncodingMode["UTF8"] = 0] = "UTF8";
  29. /**
  30. * The output consists only of ASCII characters. Characters that need
  31. * escaping within HTML, and characters that aren't ASCII characters will
  32. * be escaped.
  33. */
  34. EncodingMode[EncodingMode["ASCII"] = 1] = "ASCII";
  35. /**
  36. * Encode all characters that have an equivalent entity, as well as all
  37. * characters that are not ASCII characters.
  38. */
  39. EncodingMode[EncodingMode["Extensive"] = 2] = "Extensive";
  40. })(EncodingMode = exports.EncodingMode || (exports.EncodingMode = {}));
  41. /**
  42. * Decodes a string with entities.
  43. *
  44. * @param data String to decode.
  45. * @param options Decoding options.
  46. */
  47. function decode(data, options) {
  48. if (options === void 0) { options = EntityLevel.XML; }
  49. var opts = typeof options === "number" ? { level: options } : options;
  50. if (opts.level === EntityLevel.HTML) {
  51. if (opts.mode === DecodingMode.Strict) {
  52. return decode_1.decodeHTMLStrict(data);
  53. }
  54. return decode_1.decodeHTML(data);
  55. }
  56. return decode_1.decodeXML(data);
  57. }
  58. exports.decode = decode;
  59. /**
  60. * Decodes a string with entities. Does not allow missing trailing semicolons for entities.
  61. *
  62. * @param data String to decode.
  63. * @param options Decoding options.
  64. * @deprecated Use `decode` with the `mode` set to `Strict`.
  65. */
  66. function decodeStrict(data, options) {
  67. if (options === void 0) { options = EntityLevel.XML; }
  68. var opts = typeof options === "number" ? { level: options } : options;
  69. if (opts.level === EntityLevel.HTML) {
  70. if (opts.mode === DecodingMode.Legacy) {
  71. return decode_1.decodeHTML(data);
  72. }
  73. return decode_1.decodeHTMLStrict(data);
  74. }
  75. return decode_1.decodeXML(data);
  76. }
  77. exports.decodeStrict = decodeStrict;
  78. /**
  79. * Encodes a string with entities.
  80. *
  81. * @param data String to encode.
  82. * @param options Encoding options.
  83. */
  84. function encode(data, options) {
  85. if (options === void 0) { options = EntityLevel.XML; }
  86. var opts = typeof options === "number" ? { level: options } : options;
  87. // Mode `UTF8` just escapes XML entities
  88. if (opts.mode === EncodingMode.UTF8)
  89. return encode_1.escapeUTF8(data);
  90. if (opts.level === EntityLevel.HTML) {
  91. if (opts.mode === EncodingMode.ASCII) {
  92. return encode_1.encodeNonAsciiHTML(data);
  93. }
  94. return encode_1.encodeHTML(data);
  95. }
  96. // ASCII and Extensive are equivalent
  97. return encode_1.encodeXML(data);
  98. }
  99. exports.encode = encode;
  100. var encode_2 = require("./encode");
  101. Object.defineProperty(exports, "encodeXML", { enumerable: true, get: function () { return encode_2.encodeXML; } });
  102. Object.defineProperty(exports, "encodeHTML", { enumerable: true, get: function () { return encode_2.encodeHTML; } });
  103. Object.defineProperty(exports, "encodeNonAsciiHTML", { enumerable: true, get: function () { return encode_2.encodeNonAsciiHTML; } });
  104. Object.defineProperty(exports, "escape", { enumerable: true, get: function () { return encode_2.escape; } });
  105. Object.defineProperty(exports, "escapeUTF8", { enumerable: true, get: function () { return encode_2.escapeUTF8; } });
  106. // Legacy aliases (deprecated)
  107. Object.defineProperty(exports, "encodeHTML4", { enumerable: true, get: function () { return encode_2.encodeHTML; } });
  108. Object.defineProperty(exports, "encodeHTML5", { enumerable: true, get: function () { return encode_2.encodeHTML; } });
  109. var decode_2 = require("./decode");
  110. Object.defineProperty(exports, "decodeXML", { enumerable: true, get: function () { return decode_2.decodeXML; } });
  111. Object.defineProperty(exports, "decodeHTML", { enumerable: true, get: function () { return decode_2.decodeHTML; } });
  112. Object.defineProperty(exports, "decodeHTMLStrict", { enumerable: true, get: function () { return decode_2.decodeHTMLStrict; } });
  113. // Legacy aliases (deprecated)
  114. Object.defineProperty(exports, "decodeHTML4", { enumerable: true, get: function () { return decode_2.decodeHTML; } });
  115. Object.defineProperty(exports, "decodeHTML5", { enumerable: true, get: function () { return decode_2.decodeHTML; } });
  116. Object.defineProperty(exports, "decodeHTML4Strict", { enumerable: true, get: function () { return decode_2.decodeHTMLStrict; } });
  117. Object.defineProperty(exports, "decodeHTML5Strict", { enumerable: true, get: function () { return decode_2.decodeHTMLStrict; } });
  118. Object.defineProperty(exports, "decodeXMLStrict", { enumerable: true, get: function () { return decode_2.decodeXML; } });