@@ -2,18 +2,24 @@ import path from "path";
22
33import HtmlSourceError from "./HtmlSourceError" ;
44
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+
511function isASCIIWhitespace ( character ) {
612 return (
713 // Horizontal tab
8- character === "\u0009" ||
14+ character === HORIZONTAL_TAB ||
915 // New line
10- character === "\u000A" ||
16+ character === NEWLINE ||
1117 // Form feed
12- character === "\u000C" ||
18+ character === FORM_FEED ||
1319 // Carriage return
14- character === "\u000D" ||
20+ character === CARRIAGE_RETURN ||
1521 // Space
16- character === "\u0020"
22+ character === SPACE
1723 ) ;
1824}
1925
@@ -26,6 +32,12 @@ const regexLeadingCommasOrSpaces = /^[, \t\n\r\u000c]+/;
2632const regexLeadingNotSpaces = / ^ [ ^ \t \n \r \u000c ] + / ;
2733const regexTrailingCommas = / [ , ] + $ / ;
2834const 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 ) ;
2941
3042// ( Positive or negative or unsigned integers or decimals, without or without exponents.
3143// Must include at least one digit.
@@ -93,7 +105,7 @@ export function parseSrcset(input) {
93105 // 8. If url ends with a U+002C COMMA character (,), follow these sub steps:
94106 // (1). Remove all trailing U+002C COMMA characters from url. If this removed
95107 // more than one character, that is a parse error.
96- if ( url . slice ( - 1 ) === "," ) {
108+ if ( url . charCodeAt ( url . length - 1 ) === COMMA ) {
97109 url = url . replace ( regexTrailingCommas , "" ) ;
98110
99111 // (Jump ahead to step 9 to skip tokenization and just push the candidate).
@@ -124,7 +136,7 @@ export function parseSrcset(input) {
124136 // eslint-disable-next-line no-constant-condition
125137 while ( true ) {
126138 // 8.4. Let c be the character at position.
127- c = input . charAt ( position ) ;
139+ c = input . charCodeAt ( position ) ;
128140
129141 // Do the following depending on the value of state.
130142 // For the purpose of this step, "EOF" is a special character representing
@@ -149,7 +161,7 @@ export function parseSrcset(input) {
149161 // Advance position to the next character in input. If current descriptor
150162 // is not empty, append current descriptor to descriptors. Jump to the step
151163 // labeled descriptor parser.
152- else if ( c === "," ) {
164+ else if ( c === COMMA ) {
153165 position += 1 ;
154166
155167 if ( currentDescriptor ) {
@@ -162,14 +174,14 @@ export function parseSrcset(input) {
162174 }
163175 // U+0028 LEFT PARENTHESIS (()
164176 // 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 ) ;
167179 state = "in parens" ;
168180 }
169181 // EOF
170182 // If current descriptor is not empty, append current descriptor to
171183 // descriptors. Jump to the step labeled descriptor parser.
172- else if ( c === "" ) {
184+ else if ( isNaN ( c ) ) {
173185 if ( currentDescriptor ) {
174186 descriptors . push ( currentDescriptor ) ;
175187 }
@@ -181,29 +193,29 @@ export function parseSrcset(input) {
181193 // Anything else
182194 // Append c to current descriptor.
183195 } else {
184- currentDescriptor += c ;
196+ currentDescriptor += input . charAt ( position ) ;
185197 }
186198 }
187199 // In parens
188200 else if ( state === "in parens" ) {
189201 // U+0029 RIGHT PARENTHESIS ())
190202 // 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 ) ;
193205 state = "in descriptor" ;
194206 }
195207 // EOF
196208 // Append current descriptor to descriptors. Jump to the step labeled
197209 // descriptor parser.
198- else if ( c === "" ) {
210+ else if ( isNaN ( c ) ) {
199211 descriptors . push ( currentDescriptor ) ;
200212 parseDescriptors ( ) ;
201213 return ;
202214 }
203215 // Anything else
204216 // Append c to current descriptor.
205217 else {
206- currentDescriptor += c ;
218+ currentDescriptor += input . charAt ( position ) ;
207219 }
208220 }
209221 // After descriptor
@@ -213,7 +225,7 @@ export function parseSrcset(input) {
213225 // Space character: Stay in this state.
214226 }
215227 // EOF: Jump to the step labeled descriptor parser.
216- else if ( c === "" ) {
228+ else if ( isNaN ( c ) ) {
217229 parseDescriptors ( ) ;
218230 return ;
219231 }
@@ -258,14 +270,14 @@ export function parseSrcset(input) {
258270 for ( i = 0 ; i < descriptors . length ; i ++ ) {
259271 desc = descriptors [ i ] ;
260272
261- lastChar = desc [ desc . length - 1 ] ;
273+ lastChar = desc [ desc . length - 1 ] . charCodeAt ( 0 ) ;
262274 value = desc . substring ( 0 , desc . length - 1 ) ;
263275 intVal = parseInt ( value , 10 ) ;
264276 floatVal = parseFloat ( value ) ;
265277
266278 // If the descriptor consists of a valid non-negative integer followed by
267279 // a U+0077 LATIN SMALL LETTER W character
268- if ( regexNonNegativeInteger . test ( value ) && lastChar === "w" ) {
280+ if ( regexNonNegativeInteger . test ( value ) && lastChar === SMALL_LETTER_W ) {
269281 // If width and density are not both absent, then let error be yes.
270282 if ( w || d ) {
271283 pError = true ;
@@ -282,7 +294,7 @@ export function parseSrcset(input) {
282294 }
283295 // If the descriptor consists of a valid floating-point number followed by
284296 // 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 ) {
286298 // If width, density and future-compat-h are not all absent, then let error
287299 // be yes.
288300 if ( w || d || h ) {
@@ -300,7 +312,10 @@ export function parseSrcset(input) {
300312 }
301313 // If the descriptor consists of a valid non-negative integer followed by
302314 // 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+ ) {
304319 // If height and density are not both absent, then let error be yes.
305320 if ( h || d ) {
306321 pError = true ;
@@ -354,14 +369,18 @@ export function parseSrc(input) {
354369 }
355370
356371 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+ ) ;
358377
359378 if ( start === input . length ) {
360379 throw new Error ( "Must be non-empty" ) ;
361380 }
362381
363382 let end = input . length - 1 ;
364- for ( ; end > - 1 && isASCIIWhitespace ( input [ end ] ) ; end -- ) ;
383+ for ( ; end > - 1 && isASCIIWhitespace ( input . charCodeAt ( end ) ) ; end -- ) ;
365384 end += 1 ;
366385
367386 let value = input ;
@@ -430,12 +449,13 @@ export function isURLRequestable(url, options = {}) {
430449
431450const WINDOWS_PATH_SEPARATOR_REGEXP = / \\ / g;
432451const RELATIVE_PATH_REGEXP = / ^ \. \. ? [ / \\ ] / ;
452+ const SLASH = "/" . charCodeAt ( 0 ) ;
433453
434454const absoluteToRequest = ( context , maybeAbsolutePath ) => {
435- if ( maybeAbsolutePath [ 0 ] === "/" ) {
455+ if ( maybeAbsolutePath . charCodeAt ( 0 ) === SLASH ) {
436456 if (
437457 maybeAbsolutePath . length > 1 &&
438- maybeAbsolutePath [ maybeAbsolutePath . length - 1 ] === "/"
458+ maybeAbsolutePath . charCodeAt ( maybeAbsolutePath . length - 1 ) === SLASH
439459 ) {
440460 // this 'path' is actually a regexp generated by dynamic requires.
441461 // Don't treat it as an absolute path.
@@ -505,7 +525,7 @@ export function requestify(context, request) {
505525 . replace ( / [ \t \n \r ] / g, "" )
506526 . replace ( / \\ / g, "/" ) ;
507527
508- if ( isWindowsAbsolutePath || newRequest [ 0 ] === "/" ) {
528+ if ( isWindowsAbsolutePath || newRequest . charCodeAt ( 0 ) === SLASH ) {
509529 return newRequest ;
510530 }
511531
@@ -1240,7 +1260,7 @@ export function getImportCode(html, loaderContext, imports, options) {
12401260 return `// Imports\n${ code } ` ;
12411261}
12421262
1243- const SLASH = "\\" . charCodeAt ( 0 ) ;
1263+ const BACKSLASH = "\\" . charCodeAt ( 0 ) ;
12441264const BACKTICK = "`" . charCodeAt ( 0 ) ;
12451265const DOLLAR = "$" . charCodeAt ( 0 ) ;
12461266
@@ -1251,7 +1271,7 @@ export function convertToTemplateLiteral(str) {
12511271 const code = str . charCodeAt ( i ) ;
12521272
12531273 escapedString +=
1254- code === SLASH || code === BACKTICK || code === DOLLAR
1274+ code === BACKSLASH || code === BACKTICK || code === DOLLAR
12551275 ? `\\${ str [ i ] } `
12561276 : str [ i ] ;
12571277 }
0 commit comments