index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { decodeXML, decodeHTML, DecodingMode } from "./decode.js";
  2. import { encodeHTML, encodeNonAsciiHTML } from "./encode.js";
  3. import { encodeXML, escapeUTF8, escapeAttribute, escapeText, } from "./escape.js";
  4. /** The level of entities to support. */
  5. export var EntityLevel;
  6. (function (EntityLevel) {
  7. /** Support only XML entities. */
  8. EntityLevel[EntityLevel["XML"] = 0] = "XML";
  9. /** Support HTML entities, which are a superset of XML entities. */
  10. EntityLevel[EntityLevel["HTML"] = 1] = "HTML";
  11. })(EntityLevel || (EntityLevel = {}));
  12. export var EncodingMode;
  13. (function (EncodingMode) {
  14. /**
  15. * The output is UTF-8 encoded. Only characters that need escaping within
  16. * XML will be escaped.
  17. */
  18. EncodingMode[EncodingMode["UTF8"] = 0] = "UTF8";
  19. /**
  20. * The output consists only of ASCII characters. Characters that need
  21. * escaping within HTML, and characters that aren't ASCII characters will
  22. * be escaped.
  23. */
  24. EncodingMode[EncodingMode["ASCII"] = 1] = "ASCII";
  25. /**
  26. * Encode all characters that have an equivalent entity, as well as all
  27. * characters that are not ASCII characters.
  28. */
  29. EncodingMode[EncodingMode["Extensive"] = 2] = "Extensive";
  30. /**
  31. * Encode all characters that have to be escaped in HTML attributes,
  32. * following {@link https://html.spec.whatwg.org/multipage/parsing.html#escapingString}.
  33. */
  34. EncodingMode[EncodingMode["Attribute"] = 3] = "Attribute";
  35. /**
  36. * Encode all characters that have to be escaped in HTML text,
  37. * following {@link https://html.spec.whatwg.org/multipage/parsing.html#escapingString}.
  38. */
  39. EncodingMode[EncodingMode["Text"] = 4] = "Text";
  40. })(EncodingMode || (EncodingMode = {}));
  41. /**
  42. * Decodes a string with entities.
  43. *
  44. * @param input String to decode.
  45. * @param options Decoding options.
  46. */
  47. export function decode(input, options = EntityLevel.XML) {
  48. const level = typeof options === "number" ? options : options.level;
  49. if (level === EntityLevel.HTML) {
  50. const mode = typeof options === "object" ? options.mode : undefined;
  51. return decodeHTML(input, mode);
  52. }
  53. return decodeXML(input);
  54. }
  55. /**
  56. * Decodes a string with entities. Does not allow missing trailing semicolons for entities.
  57. *
  58. * @param input String to decode.
  59. * @param options Decoding options.
  60. * @deprecated Use `decode` with the `mode` set to `Strict`.
  61. */
  62. export function decodeStrict(input, options = EntityLevel.XML) {
  63. var _a;
  64. const normalizedOptions = typeof options === "number" ? { level: options } : options;
  65. (_a = normalizedOptions.mode) !== null && _a !== void 0 ? _a : (normalizedOptions.mode = DecodingMode.Strict);
  66. return decode(input, normalizedOptions);
  67. }
  68. /**
  69. * Encodes a string with entities.
  70. *
  71. * @param input String to encode.
  72. * @param options Encoding options.
  73. */
  74. export function encode(input, options = EntityLevel.XML) {
  75. const { mode = EncodingMode.Extensive, level = EntityLevel.XML } = typeof options === "number" ? { level: options } : options;
  76. switch (mode) {
  77. case EncodingMode.UTF8: {
  78. return escapeUTF8(input);
  79. }
  80. case EncodingMode.Attribute: {
  81. return escapeAttribute(input);
  82. }
  83. case EncodingMode.Text: {
  84. return escapeText(input);
  85. }
  86. case EncodingMode.ASCII: {
  87. return level === EntityLevel.HTML
  88. ? encodeNonAsciiHTML(input)
  89. : encodeXML(input);
  90. }
  91. // eslint-disable-next-line unicorn/no-useless-switch-case
  92. case EncodingMode.Extensive:
  93. default: {
  94. return level === EntityLevel.HTML
  95. ? encodeHTML(input)
  96. : encodeXML(input);
  97. }
  98. }
  99. }
  100. export { encodeXML, escape, escapeUTF8, escapeAttribute, escapeText, } from "./escape.js";
  101. export { encodeHTML, encodeNonAsciiHTML,
  102. // Legacy aliases (deprecated)
  103. encodeHTML as encodeHTML4, encodeHTML as encodeHTML5, } from "./encode.js";
  104. export { EntityDecoder, DecodingMode, decodeXML, decodeHTML, decodeHTMLStrict, decodeHTMLAttribute,
  105. // Legacy aliases (deprecated)
  106. decodeHTML as decodeHTML4, decodeHTML as decodeHTML5, decodeHTMLStrict as decodeHTML4Strict, decodeHTMLStrict as decodeHTML5Strict, decodeXML as decodeXMLStrict, } from "./decode.js";
  107. //# sourceMappingURL=index.js.map