source: node_modules/entities/lib/esm/index.js

Last change on this file was 57e58a3, checked in by ste08 <sjovanoska@…>, 4 months ago

Initial commit

  • Property mode set to 100644
File size: 4.0 KB
Line 
1import { decodeXML, decodeHTML, DecodingMode } from "./decode.js";
2import { encodeHTML, encodeNonAsciiHTML } from "./encode.js";
3import { encodeXML, escapeUTF8, escapeAttribute, escapeText, } from "./escape.js";
4/** The level of entities to support. */
5export 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 = {}));
12export 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 */
47export 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 */
62export 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 */
74export 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}
92export { encodeXML, escape, escapeUTF8, escapeAttribute, escapeText, } from "./escape.js";
93export { encodeHTML, encodeNonAsciiHTML,
94// Legacy aliases (deprecated)
95encodeHTML as encodeHTML4, encodeHTML as encodeHTML5, } from "./encode.js";
96export { EntityDecoder, DecodingMode, decodeXML, decodeHTML, decodeHTMLStrict, decodeHTMLAttribute,
97// Legacy aliases (deprecated)
98decodeHTML as decodeHTML4, decodeHTML as decodeHTML5, decodeHTMLStrict as decodeHTML4Strict, decodeHTMLStrict as decodeHTML5Strict, decodeXML as decodeXMLStrict, } from "./decode.js";
99//# sourceMappingURL=index.js.map
Note: See TracBrowser for help on using the repository browser.