@@ -2,18 +2,24 @@ import path from "path";
2
2
3
3
import HtmlSourceError from "./HtmlSourceError" ;
4
4
5
+ const HORIZONTAL_TAB = "\u0009" . charCodeAt ( 0 ) ;
6
+ const NEWLINE = "\u000A" . charCodeAt ( 0 ) ;
7
+ const FORM_FEED = "\u000C" . charCodeAt ( 0 ) ;
8
+ const CARRIAGE_RETURN = "\u000D" . charCodeAt ( 0 ) ;
9
+ const SPACE = "\u0020" . charCodeAt ( 0 ) ;
10
+
5
11
function isASCIIWhitespace ( character ) {
6
12
return (
7
13
// Horizontal tab
8
- character === "\u0009" ||
14
+ character === HORIZONTAL_TAB ||
9
15
// New line
10
- character === "\u000A" ||
16
+ character === NEWLINE ||
11
17
// Form feed
12
- character === "\u000C" ||
18
+ character === FORM_FEED ||
13
19
// Carriage return
14
- character === "\u000D" ||
20
+ character === CARRIAGE_RETURN ||
15
21
// Space
16
- character === "\u0020"
22
+ character === SPACE
17
23
) ;
18
24
}
19
25
@@ -26,6 +32,12 @@ const regexLeadingCommasOrSpaces = /^[, \t\n\r\u000c]+/;
26
32
const regexLeadingNotSpaces = / ^ [ ^ \t \n \r \u000c ] + / ;
27
33
const regexTrailingCommas = / [ , ] + $ / ;
28
34
const regexNonNegativeInteger = / ^ \d + $ / ;
35
+ const COMMA = "," . charCodeAt ( 0 ) ;
36
+ const LEFT_PARENTHESIS = "(" . charCodeAt ( 0 ) ;
37
+ const RIGHT_PARENTHESIS = ")" . charCodeAt ( 0 ) ;
38
+ const SMALL_LETTER_W = "w" . charCodeAt ( 0 ) ;
39
+ const SMALL_LETTER_X = "x" . charCodeAt ( 0 ) ;
40
+ const SMALL_LETTER_H = "h" . charCodeAt ( 0 ) ;
29
41
30
42
// ( Positive or negative or unsigned integers or decimals, without or without exponents.
31
43
// Must include at least one digit.
@@ -93,7 +105,7 @@ export function parseSrcset(input) {
93
105
// 8. If url ends with a U+002C COMMA character (,), follow these sub steps:
94
106
// (1). Remove all trailing U+002C COMMA characters from url. If this removed
95
107
// more than one character, that is a parse error.
96
- if ( url . slice ( - 1 ) === "," ) {
108
+ if ( url . charCodeAt ( url . length - 1 ) === COMMA ) {
97
109
url = url . replace ( regexTrailingCommas , "" ) ;
98
110
99
111
// (Jump ahead to step 9 to skip tokenization and just push the candidate).
@@ -124,7 +136,7 @@ export function parseSrcset(input) {
124
136
// eslint-disable-next-line no-constant-condition
125
137
while ( true ) {
126
138
// 8.4. Let c be the character at position.
127
- c = input . charAt ( position ) ;
139
+ c = input . charCodeAt ( position ) ;
128
140
129
141
// Do the following depending on the value of state.
130
142
// For the purpose of this step, "EOF" is a special character representing
@@ -149,7 +161,7 @@ export function parseSrcset(input) {
149
161
// Advance position to the next character in input. If current descriptor
150
162
// is not empty, append current descriptor to descriptors. Jump to the step
151
163
// labeled descriptor parser.
152
- else if ( c === "," ) {
164
+ else if ( c === COMMA ) {
153
165
position += 1 ;
154
166
155
167
if ( currentDescriptor ) {
@@ -162,14 +174,14 @@ export function parseSrcset(input) {
162
174
}
163
175
// U+0028 LEFT PARENTHESIS (()
164
176
// Append c to current descriptor. Set state to in parens.
165
- else if ( c === "\u0028" ) {
166
- currentDescriptor += c ;
177
+ else if ( c === LEFT_PARENTHESIS ) {
178
+ currentDescriptor += input . charAt ( position ) ;
167
179
state = "in parens" ;
168
180
}
169
181
// EOF
170
182
// If current descriptor is not empty, append current descriptor to
171
183
// descriptors. Jump to the step labeled descriptor parser.
172
- else if ( c === "" ) {
184
+ else if ( isNaN ( c ) ) {
173
185
if ( currentDescriptor ) {
174
186
descriptors . push ( currentDescriptor ) ;
175
187
}
@@ -181,29 +193,29 @@ export function parseSrcset(input) {
181
193
// Anything else
182
194
// Append c to current descriptor.
183
195
} else {
184
- currentDescriptor += c ;
196
+ currentDescriptor += input . charAt ( position ) ;
185
197
}
186
198
}
187
199
// In parens
188
200
else if ( state === "in parens" ) {
189
201
// U+0029 RIGHT PARENTHESIS ())
190
202
// Append c to current descriptor. Set state to in descriptor.
191
- if ( c === ")" ) {
192
- currentDescriptor += c ;
203
+ if ( c === RIGHT_PARENTHESIS ) {
204
+ currentDescriptor += input . charAt ( position ) ;
193
205
state = "in descriptor" ;
194
206
}
195
207
// EOF
196
208
// Append current descriptor to descriptors. Jump to the step labeled
197
209
// descriptor parser.
198
- else if ( c === "" ) {
210
+ else if ( isNaN ( c ) ) {
199
211
descriptors . push ( currentDescriptor ) ;
200
212
parseDescriptors ( ) ;
201
213
return ;
202
214
}
203
215
// Anything else
204
216
// Append c to current descriptor.
205
217
else {
206
- currentDescriptor += c ;
218
+ currentDescriptor += input . charAt ( position ) ;
207
219
}
208
220
}
209
221
// After descriptor
@@ -213,7 +225,7 @@ export function parseSrcset(input) {
213
225
// Space character: Stay in this state.
214
226
}
215
227
// EOF: Jump to the step labeled descriptor parser.
216
- else if ( c === "" ) {
228
+ else if ( isNaN ( c ) ) {
217
229
parseDescriptors ( ) ;
218
230
return ;
219
231
}
@@ -258,14 +270,14 @@ export function parseSrcset(input) {
258
270
for ( i = 0 ; i < descriptors . length ; i ++ ) {
259
271
desc = descriptors [ i ] ;
260
272
261
- lastChar = desc [ desc . length - 1 ] ;
273
+ lastChar = desc [ desc . length - 1 ] . charCodeAt ( 0 ) ;
262
274
value = desc . substring ( 0 , desc . length - 1 ) ;
263
275
intVal = parseInt ( value , 10 ) ;
264
276
floatVal = parseFloat ( value ) ;
265
277
266
278
// If the descriptor consists of a valid non-negative integer followed by
267
279
// a U+0077 LATIN SMALL LETTER W character
268
- if ( regexNonNegativeInteger . test ( value ) && lastChar === "w" ) {
280
+ if ( regexNonNegativeInteger . test ( value ) && lastChar === SMALL_LETTER_W ) {
269
281
// If width and density are not both absent, then let error be yes.
270
282
if ( w || d ) {
271
283
pError = true ;
@@ -282,7 +294,7 @@ export function parseSrcset(input) {
282
294
}
283
295
// If the descriptor consists of a valid floating-point number followed by
284
296
// a U+0078 LATIN SMALL LETTER X character
285
- else if ( regexFloatingPoint . test ( value ) && lastChar === "x" ) {
297
+ else if ( regexFloatingPoint . test ( value ) && lastChar === SMALL_LETTER_X ) {
286
298
// If width, density and future-compat-h are not all absent, then let error
287
299
// be yes.
288
300
if ( w || d || h ) {
@@ -300,7 +312,10 @@ export function parseSrcset(input) {
300
312
}
301
313
// If the descriptor consists of a valid non-negative integer followed by
302
314
// a U+0068 LATIN SMALL LETTER H character
303
- else if ( regexNonNegativeInteger . test ( value ) && lastChar === "h" ) {
315
+ else if (
316
+ regexNonNegativeInteger . test ( value ) &&
317
+ lastChar === SMALL_LETTER_H
318
+ ) {
304
319
// If height and density are not both absent, then let error be yes.
305
320
if ( h || d ) {
306
321
pError = true ;
@@ -354,14 +369,18 @@ export function parseSrc(input) {
354
369
}
355
370
356
371
let start = 0 ;
357
- for ( ; start < input . length && isASCIIWhitespace ( input [ start ] ) ; start ++ ) ;
372
+ for (
373
+ ;
374
+ start < input . length && isASCIIWhitespace ( input . charCodeAt ( start ) ) ;
375
+ start ++
376
+ ) ;
358
377
359
378
if ( start === input . length ) {
360
379
throw new Error ( "Must be non-empty" ) ;
361
380
}
362
381
363
382
let end = input . length - 1 ;
364
- for ( ; end > - 1 && isASCIIWhitespace ( input [ end ] ) ; end -- ) ;
383
+ for ( ; end > - 1 && isASCIIWhitespace ( input . charCodeAt ( end ) ) ; end -- ) ;
365
384
end += 1 ;
366
385
367
386
let value = input ;
@@ -430,12 +449,13 @@ export function isURLRequestable(url, options = {}) {
430
449
431
450
const WINDOWS_PATH_SEPARATOR_REGEXP = / \\ / g;
432
451
const RELATIVE_PATH_REGEXP = / ^ \. \. ? [ / \\ ] / ;
452
+ const SLASH = "/" . charCodeAt ( 0 ) ;
433
453
434
454
const absoluteToRequest = ( context , maybeAbsolutePath ) => {
435
- if ( maybeAbsolutePath [ 0 ] === "/" ) {
455
+ if ( maybeAbsolutePath . charCodeAt ( 0 ) === SLASH ) {
436
456
if (
437
457
maybeAbsolutePath . length > 1 &&
438
- maybeAbsolutePath [ maybeAbsolutePath . length - 1 ] === "/"
458
+ maybeAbsolutePath . charCodeAt ( maybeAbsolutePath . length - 1 ) === SLASH
439
459
) {
440
460
// this 'path' is actually a regexp generated by dynamic requires.
441
461
// Don't treat it as an absolute path.
@@ -505,7 +525,7 @@ export function requestify(context, request) {
505
525
. replace ( / [ \t \n \r ] / g, "" )
506
526
. replace ( / \\ / g, "/" ) ;
507
527
508
- if ( isWindowsAbsolutePath || newRequest [ 0 ] === "/" ) {
528
+ if ( isWindowsAbsolutePath || newRequest . charCodeAt ( 0 ) === SLASH ) {
509
529
return newRequest ;
510
530
}
511
531
@@ -1240,7 +1260,7 @@ export function getImportCode(html, loaderContext, imports, options) {
1240
1260
return `// Imports\n${ code } ` ;
1241
1261
}
1242
1262
1243
- const SLASH = "\\" . charCodeAt ( 0 ) ;
1263
+ const BACKSLASH = "\\" . charCodeAt ( 0 ) ;
1244
1264
const BACKTICK = "`" . charCodeAt ( 0 ) ;
1245
1265
const DOLLAR = "$" . charCodeAt ( 0 ) ;
1246
1266
@@ -1251,7 +1271,7 @@ export function convertToTemplateLiteral(str) {
1251
1271
const code = str . charCodeAt ( i ) ;
1252
1272
1253
1273
escapedString +=
1254
- code === SLASH || code === BACKTICK || code === DOLLAR
1274
+ code === BACKSLASH || code === BACKTICK || code === DOLLAR
1255
1275
? `\\${ str [ i ] } `
1256
1276
: str [ i ] ;
1257
1277
}
0 commit comments