@@ -272,19 +272,26 @@ function removePercentEncoding(anchorText: string): string {
272
272
// First, convert a few of the known % encodings to the corresponding
273
273
// HTML entities that could accidentally be interpretted as special
274
274
// HTML characters
275
- const preProcessedEntityAnchorText = anchorText
276
- . replace ( / % 2 2 / gi, '"' ) // " char
277
- . replace ( / % 2 6 / gi, '&' ) // & char
278
- . replace ( / % 2 7 / gi, ''' ) // ' char
279
- . replace ( / % 3 C / gi, '<' ) // < char
280
- . replace ( / % 3 E / gi, '>' ) ; // > char
275
+ // NOTE: This used to be written as 5 separate .replace() calls, but that
276
+ // was 25% slower than the current form below according to jsperf
277
+ const preProcessedEntityAnchorText = anchorText . replace ( / % (?: 2 2 | 2 6 | 2 7 | 3 C | 3 E ) / gi, match => {
278
+ if ( match === '%22' ) return '"' ; // %22: '"' char
279
+ if ( match === '%26' ) return '&' ; // %26: '&' char
280
+ if ( match === '%27' ) return ''' ; // %27: "'" char
281
+ if ( match === '%3C' || match === '%3c' ) return '<' ; // %3C: '<' char
282
+ /*if (match === '%3E' || match === '%3e')*/ return '>' ; // %3E: '>' char
283
+ } ) ;
281
284
282
- try {
283
- // Now attempt to decode the rest of the anchor text
284
- return decodeURIComponent ( preProcessedEntityAnchorText ) ;
285
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
286
- } catch ( error : unknown ) {
287
- // Invalid % escape sequence in the anchor text
288
- return preProcessedEntityAnchorText ;
285
+ // Now attempt to decode the rest of the anchor text. However, decodeURIComponent()
286
+ // is a slow function. Only call if we have remaining %-encoded entities
287
+ if ( preProcessedEntityAnchorText . includes ( '%' ) ) {
288
+ try {
289
+ return decodeURIComponent ( preProcessedEntityAnchorText ) ;
290
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
291
+ } catch ( error : unknown ) {
292
+ // Invalid % escape sequence in the anchor text, we'll simply return
293
+ // the preProcessedEntityAnchorText below
294
+ }
289
295
}
296
+ return preProcessedEntityAnchorText ;
290
297
}
0 commit comments