[57e58a3] | 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 data String to decode.
|
---|
| 45 | * @param options Decoding options.
|
---|
| 46 | */
|
---|
| 47 | export function decode(data, 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(data, mode);
|
---|
| 52 | }
|
---|
| 53 | return decodeXML(data);
|
---|
| 54 | }
|
---|
| 55 | /**
|
---|
| 56 | * Decodes a string with entities. Does not allow missing trailing semicolons for entities.
|
---|
| 57 | *
|
---|
| 58 | * @param data String to decode.
|
---|
| 59 | * @param options Decoding options.
|
---|
| 60 | * @deprecated Use `decode` with the `mode` set to `Strict`.
|
---|
| 61 | */
|
---|
| 62 | export function decodeStrict(data, options = EntityLevel.XML) {
|
---|
| 63 | var _a;
|
---|
| 64 | const opts = typeof options === "number" ? { level: options } : options;
|
---|
| 65 | (_a = opts.mode) !== null && _a !== void 0 ? _a : (opts.mode = DecodingMode.Strict);
|
---|
| 66 | return decode(data, opts);
|
---|
| 67 | }
|
---|
| 68 | /**
|
---|
| 69 | * Encodes a string with entities.
|
---|
| 70 | *
|
---|
| 71 | * @param data String to encode.
|
---|
| 72 | * @param options Encoding options.
|
---|
| 73 | */
|
---|
| 74 | export function encode(data, options = EntityLevel.XML) {
|
---|
| 75 | const opts = typeof options === "number" ? { level: options } : options;
|
---|
| 76 | // Mode `UTF8` just escapes XML entities
|
---|
| 77 | if (opts.mode === EncodingMode.UTF8)
|
---|
| 78 | return escapeUTF8(data);
|
---|
| 79 | if (opts.mode === EncodingMode.Attribute)
|
---|
| 80 | return escapeAttribute(data);
|
---|
| 81 | if (opts.mode === EncodingMode.Text)
|
---|
| 82 | return escapeText(data);
|
---|
| 83 | if (opts.level === EntityLevel.HTML) {
|
---|
| 84 | if (opts.mode === EncodingMode.ASCII) {
|
---|
| 85 | return encodeNonAsciiHTML(data);
|
---|
| 86 | }
|
---|
| 87 | return encodeHTML(data);
|
---|
| 88 | }
|
---|
| 89 | // ASCII and Extensive are equivalent
|
---|
| 90 | return encodeXML(data);
|
---|
| 91 | }
|
---|
| 92 | export { encodeXML, escape, escapeUTF8, escapeAttribute, escapeText, } from "./escape.js";
|
---|
| 93 | export { encodeHTML, encodeNonAsciiHTML,
|
---|
| 94 | // Legacy aliases (deprecated)
|
---|
| 95 | encodeHTML as encodeHTML4, encodeHTML as encodeHTML5, } from "./encode.js";
|
---|
| 96 | export { EntityDecoder, DecodingMode, decodeXML, decodeHTML, decodeHTMLStrict, decodeHTMLAttribute,
|
---|
| 97 | // Legacy aliases (deprecated)
|
---|
| 98 | decodeHTML as decodeHTML4, decodeHTML as decodeHTML5, decodeHTMLStrict as decodeHTML4Strict, decodeHTMLStrict as decodeHTML5Strict, decodeXML as decodeXMLStrict, } from "./decode.js";
|
---|
| 99 | //# sourceMappingURL=index.js.map |
---|