-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathindex.ts
191 lines (165 loc) · 6.25 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import {bodyRegExps, namedReferences} from './named-references';
import {numericUnicodeMap} from './numeric-unicode-map';
import {fromCodePoint, getCodePoint} from './surrogate-pairs';
const allNamedReferences = {
...namedReferences,
all: namedReferences.html5
};
function replaceUsingRegExp(macroText: string, macroRegExp: RegExp, macroReplacer: (input: string) => string): string {
macroRegExp.lastIndex = 0;
let replaceMatch = macroRegExp.exec(macroText);
let replaceResult;
if (replaceMatch) {
replaceResult = '';
let replaceLastIndex = 0;
do {
if (replaceLastIndex !== replaceMatch.index) {
replaceResult += macroText.substring(replaceLastIndex, replaceMatch.index);
}
const replaceInput = replaceMatch[0];
replaceResult += macroReplacer(replaceInput);
replaceLastIndex = replaceMatch.index + replaceInput.length;
} while ((replaceMatch = macroRegExp.exec(macroText)));
if (replaceLastIndex !== macroText.length) {
replaceResult += macroText.substring(replaceLastIndex);
}
} else {
replaceResult = macroText;
}
return replaceResult;
}
export type Level = 'xml' | 'html4' | 'html5' | 'all';
interface CommonOptions {
level?: Level;
}
export type EncodeMode = 'specialChars' | 'nonAscii' | 'nonAsciiPrintable' | 'nonAsciiPrintableOnly' | 'extensive';
export interface EncodeOptions extends CommonOptions {
mode?: EncodeMode;
numeric?: 'decimal' | 'hexadecimal';
}
export type DecodeScope = 'strict' | 'body' | 'attribute';
export interface DecodeOptions extends CommonOptions {
scope?: DecodeScope;
}
const encodeRegExps: Record<EncodeMode, RegExp> = {
specialChars: /[<>'"&]/g,
nonAscii: /[<>'"&\u0080-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]/g,
nonAsciiPrintable: /[<>'"&\x01-\x08\x11-\x15\x17-\x1F\x7f-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]/g,
nonAsciiPrintableOnly: /[\x01-\x08\x11-\x15\x17-\x1F\x7f-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]/g,
extensive: /[\x01-\x0c\x0e-\x1f\x21-\x2c\x2e-\x2f\x3a-\x40\x5b-\x60\x7b-\x7d\x7f-\uD7FF\uE000-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF]/g
};
const defaultEncodeOptions: EncodeOptions = {
mode: 'specialChars',
level: 'all',
numeric: 'decimal'
};
/** Encodes all the necessary (specified by `level`) characters in the text */
export function encode(
text: string | undefined | null,
{mode = 'specialChars', numeric = 'decimal', level = 'all'}: EncodeOptions = defaultEncodeOptions
) {
if (!text) {
return '';
}
const encodeRegExp = encodeRegExps[mode];
const references = allNamedReferences[level].characters;
const isHex = numeric === 'hexadecimal';
return replaceUsingRegExp(text, encodeRegExp, (input) => {
let result = references[input];
if (!result) {
const code = input.length > 1 ? getCodePoint(input, 0)! : input.charCodeAt(0);
result = (isHex ? '&#x' + code.toString(16) : '&#' + code) + ';';
}
return result;
});
}
const defaultDecodeOptions: DecodeOptions = {
scope: 'body',
level: 'all'
};
const strict = /&(?:#\d+|#[xX][\da-fA-F]+|[0-9a-zA-Z]+);/g;
const attribute = /&(?:#\d+|#[xX][\da-fA-F]+|[0-9a-zA-Z]+)[;=]?/g;
const baseDecodeRegExps: Record<Exclude<Level, 'all'>, Record<DecodeScope, RegExp>> = {
xml: {
strict,
attribute,
body: bodyRegExps.xml
},
html4: {
strict,
attribute,
body: bodyRegExps.html4
},
html5: {
strict,
attribute,
body: bodyRegExps.html5
}
};
const decodeRegExps: Record<Level, Record<DecodeScope, RegExp>> = {
...baseDecodeRegExps,
all: baseDecodeRegExps.html5
};
const fromCharCode = String.fromCharCode;
const outOfBoundsChar = fromCharCode(65533);
const defaultDecodeEntityOptions: CommonOptions = {
level: 'all'
};
function getDecodedEntity(
entity: string,
references: Record<string, string>,
isAttribute: boolean,
isStrict: boolean
): string {
let decodeResult = entity;
const decodeEntityLastChar = entity[entity.length - 1];
if (isAttribute && decodeEntityLastChar === '=') {
decodeResult = entity;
} else if (isStrict && decodeEntityLastChar !== ';') {
decodeResult = entity;
} else {
const decodeResultByReference = references[entity];
if (decodeResultByReference) {
decodeResult = decodeResultByReference;
} else if (entity[0] === '&' && entity[1] === '#') {
const decodeSecondChar = entity[2];
const decodeCode =
decodeSecondChar == 'x' || decodeSecondChar == 'X'
? parseInt(entity.substr(3), 16)
: parseInt(entity.substr(2));
decodeResult =
decodeCode >= 0x10ffff
? outOfBoundsChar
: decodeCode > 65535
? fromCodePoint(decodeCode)
: fromCharCode(numericUnicodeMap[decodeCode] || decodeCode);
}
}
return decodeResult;
}
/** Decodes a single entity */
export function decodeEntity(
entity: string | undefined | null,
{level = 'all'}: CommonOptions = defaultDecodeEntityOptions
): string {
if (!entity) {
return '';
}
return getDecodedEntity(entity, allNamedReferences[level].entities, false, false);
}
/** Decodes all entities in the text */
export function decode(
text: string | undefined | null,
{level = 'all', scope = level === 'xml' ? 'strict' : 'body'}: DecodeOptions = defaultDecodeOptions
) {
if (!text) {
return '';
}
const decodeRegExp = decodeRegExps[level][scope];
const references = allNamedReferences[level].entities;
const isAttribute = scope === 'attribute';
const isStrict = scope === 'strict';
return replaceUsingRegExp(text, decodeRegExp, (entity) =>
getDecodedEntity(entity, references, isAttribute, isStrict)
);
}