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
192
// HTMLRewriter returns raw text and attributes, so decode scraped character references here.
/** common HTML named references; numeric references are handled separately. */
const NAMED_ENTITIES: Record<string, string> = {
aacute: 'á',
Aacute: 'Á',
acirc: 'â',
Acirc: 'Â',
acute: '´',
aelig: 'æ',
AElig: 'Æ',
agrave: 'à',
Agrave: 'À',
amp: '&',
apos: "'",
aring: 'å',
Aring: 'Å',
atilde: 'ã',
Atilde: 'Ã',
auml: 'ä',
Auml: 'Ä',
bdquo: '„',
brvbar: '¦',
bull: '•',
ccedil: 'ç',
Ccedil: 'Ç',
cedil: '¸',
cent: '¢',
copy: '©',
curren: '¤',
dagger: '†',
Dagger: '‡',
deg: '°',
divide: '÷',
eacute: 'é',
Eacute: 'É',
ecirc: 'ê',
Ecirc: 'Ê',
egrave: 'è',
Egrave: 'È',
euml: 'ë',
Euml: 'Ë',
euro: '€',
frac12: '½',
frac14: '¼',
frac34: '¾',
gt: '>',
hellip: '…',
iacute: 'í',
Iacute: 'Í',
icirc: 'î',
Icirc: 'Î',
iexcl: '¡',
igrave: 'ì',
Igrave: 'Ì',
iquest: '¿',
iuml: 'ï',
Iuml: 'Ï',
laquo: '«',
ldquo: '“',
lsaquo: '‹',
lsquo: '‘',
lt: '<',
macr: '¯',
mdash: '—',
micro: 'µ',
middot: '·',
nbsp: ' ',
ndash: '–',
not: '¬',
ntilde: 'ñ',
Ntilde: 'Ñ',
oacute: 'ó',
Oacute: 'Ó',
ocirc: 'ô',
Ocirc: 'Ô',
oelig: 'œ',
OElig: 'Œ',
ograve: 'ò',
Ograve: 'Ò',
ordf: 'ª',
ordm: 'º',
oslash: 'ø',
Oslash: 'Ø',
otilde: 'õ',
Otilde: 'Õ',
ouml: 'ö',
Ouml: 'Ö',
para: '¶',
permil: '‰',
plusmn: '±',
pound: '£',
prime: '′',
Prime: '″',
quot: '"',
raquo: '»',
rdquo: '”',
reg: '®',
rsaquo: '›',
rsquo: '’',
sbquo: '‚',
scaron: 'š',
Scaron: 'Š',
sect: '§',
shy: '',
sup1: '¹',
sup2: '²',
sup3: '³',
szlig: 'ß',
thinsp: ' ',
thorn: 'þ',
THORN: 'Þ',
tilde: '˜',
times: '×',
trade: '™',
uacute: 'ú',
Uacute: 'Ú',
ucirc: 'û',
Ucirc: 'Û',
ugrave: 'ù',
Ugrave: 'Ù',
uml: '¨',
uuml: 'ü',
Uuml: 'Ü',
yacute: 'ý',
Yacute: 'Ý',
yen: '¥',
yuml: 'ÿ',
Yuml: 'Ÿ',
};
/** Windows-1252 replacements used by HTML numeric references. */
const WINDOWS_1252: Record<number, number> = {
0x80: 0x20ac,
0x82: 0x201a,
0x83: 0x0192,
0x84: 0x201e,
0x85: 0x2026,
0x86: 0x2020,
0x87: 0x2021,
0x88: 0x02c6,
0x89: 0x2030,
0x8a: 0x0160,
0x8b: 0x2039,
0x8c: 0x0152,
0x8e: 0x017d,
0x91: 0x2018,
0x92: 0x2019,
0x93: 0x201c,
0x94: 0x201d,
0x95: 0x2022,
0x96: 0x2013,
0x97: 0x2014,
0x98: 0x02dc,
0x99: 0x2122,
0x9a: 0x0161,
0x9b: 0x203a,
0x9c: 0x0153,
0x9e: 0x017e,
0x9f: 0x0178,
};
const ENTITY_PATTERN = /&(#[xX][\da-fA-F]+|#\d+|[a-zA-Z][\da-zA-Z]*);/g;
const decodeNumeric = (body: string): string => {
let code = body[1] === 'x' || body[1] === 'X' ? parseInt(body.slice(2), 16) : parseInt(body.slice(1), 10);
code = WINDOWS_1252[code] ?? code;
// replace invalid code points before calling String.fromCodePoint.
if (code === 0 || code > 0x10ffff || (code >= 0xd800 && code <= 0xdfff)) {
return '�';
}
return String.fromCodePoint(code);
};
/**
* decodes named and numeric HTML character references.
*
* @param input text that may contain HTML entities
* @returns text with recognized entities decoded
*/
export const decodeHtmlEntities = (input: string): string => {
if (!input.includes('&')) {
return input;
}
return input.replace(ENTITY_PATTERN, (match, body: string) => {
if (body[0] === '#') {
return decodeNumeric(body);
}
return NAMED_ENTITIES[body] ?? match;
});
};