UNPKG

4.7 kBTypeScriptView Raw
1// he - "HTML Entities" - A high quality pair of HTML encode and decode functions.
2
3export as namespace he;
4
5export var version: string;
6
7export interface EncodeOptions {
8 /**
9 * The default value for the useNamedReferences option is false. This
10 * means that encode() will not use any named character references
11 * (e.g. ©) in the output — hexadecimal escapes (e.g. ©) will
12 * be used instead. Set it to true to enable the use of named references.
13 */
14 useNamedReferences?: boolean | undefined;
15
16 /**
17 * The default value for the decimal option is false. If the option is
18 * enabled, encode will generally use decimal escapes (e.g. ©)
19 * rather than hexadecimal escapes (e.g. ©). Beside of this
20 * replacement, the basic behavior remains the same when combined with
21 * other options. For example: if both options useNamedReferences and
22 * decimal are enabled, named references (e.g. ©) are used over
23 * decimal escapes. HTML entities without a named reference are encoded
24 * using decimal escapes.
25 */
26 decimal?: boolean | undefined;
27
28 /**
29 * The default value for the encodeEverything option is false. This means
30 * that encode() will not use any character references for printable ASCII
31 * symbols that don’t need escaping. Set it to true to encode every symbol
32 * in the input string. When set to true, this option takes precedence over
33 * allowUnsafeSymbols (i.e. setting the latter to true in such a case has
34 * no effect).
35 */
36 encodeEverything?: boolean | undefined;
37
38 /**
39 * The default value for the strict option is false. This means that
40 * encode() will encode any HTML text content you feed it, even if it
41 * contains any symbols that cause parse errors. To throw an error when such
42 * invalid HTML is encountered, set the strict option to true. This option
43 * makes it possible to use he as part of HTML parsers and HTML validators.
44 */
45 strict?: boolean | undefined;
46
47 /**
48 * The default value for the allowUnsafeSymbols option is false. This means
49 * that characters that are unsafe for use in HTML content (&, <, >, ", ',
50 * and `) will be encoded. When set to true, only non-ASCII characters will
51 * be encoded. If the encodeEverything option is set to true, this option
52 * will be ignored.
53 */
54 allowUnsafeSymbols?: boolean | undefined;
55}
56
57export interface Encode {
58 /**
59 * Encode a string of text
60 *
61 * This function takes a string of text and encodes (by default) any symbols
62 * that aren’t printable ASCII symbols and &, <, >, ", ', and `, replacing
63 * them with character references.
64 *
65 * As long as the input string contains allowed code points only, the return
66 * value of this function is always valid HTML. Any (invalid) code points
67 * that cannot be represented using a character reference in the input are
68 * not encoded.
69 */
70 (text: string, options?: EncodeOptions): string;
71
72 options: EncodeOptions;
73}
74export var encode: Encode;
75
76export interface DecodeOptions {
77 /**
78 * The default value for the isAttributeValue option is false. This means
79 * that decode() will decode the string as if it were used in a text
80 * context in an HTML document. HTML has different rules for parsing
81 * character references in attribute values — set this option to true to
82 * treat the input string as if it were used as an attribute value.
83 */
84 isAttributeValue?: boolean | undefined;
85
86 /**
87 * The default value for the strict option is false. This means that
88 * decode() will decode any HTML text content you feed it, even if it
89 * contains any entities that cause parse errors. To throw an error when
90 * such invalid HTML is encountered, set the strict option to true. This
91 * option makes it possible to use he as part of HTML parsers and HTML
92 * validators.
93 */
94 strict?: boolean | undefined;
95}
96
97export interface Decode {
98 /**
99 * Decode a string of HTML text
100 *
101 * This function takes a string of HTML and decodes any named and numerical
102 * character references in it using the algorithm described in section
103 * 12.2.4.69 of the HTML spec.
104 */
105 (html: string, options?: DecodeOptions): string;
106
107 options: DecodeOptions;
108}
109export var decode: Decode;
110
111/**
112 * Escape XML entities
113 *
114 * This function takes a string of text and escapes it for use in text
115 * contexts in XML or HTML documents. Only the following characters are
116 * escaped: &, <, >, ", ', and `.
117 */
118export function escape(text: string): string;
119
120export var unescape: Decode;