@@ -106,11 +106,8 @@ export class Parser {
106
106
this . #changeState( State . HASH , /*skip=*/ 1 ) ;
107
107
} else if ( this . #isSearchPrefix( ) ) {
108
108
this . #changeState( State . SEARCH , /*skip=*/ 1 ) ;
109
- this . #internalResult. hash = '' ;
110
109
} else {
111
110
this . #changeState( State . PATHNAME , /*skip=*/ 0 ) ;
112
- this . #internalResult. search = '' ;
113
- this . #internalResult. hash = '' ;
114
111
}
115
112
continue ;
116
113
}
@@ -147,17 +144,6 @@ export class Parser {
147
144
switch ( this . #state) {
148
145
case State . INIT :
149
146
if ( this . #isProtocolSuffix( ) ) {
150
- // We are in absolute mode and we know values will not be inherited
151
- // from a base URL. Therefore initialize the rest of the components
152
- // to the empty string.
153
- this . #internalResult. username = '' ;
154
- this . #internalResult. password = '' ;
155
- this . #internalResult. hostname = '' ;
156
- this . #internalResult. port = '' ;
157
- this . #internalResult. pathname = '' ;
158
- this . #internalResult. search = '' ;
159
- this . #internalResult. hash = '' ;
160
-
161
147
// Update the state to expect the start of an absolute URL.
162
148
this . #rewindAndSetState( State . PROTOCOL ) ;
163
149
}
@@ -179,10 +165,6 @@ export class Parser {
179
165
let nextState : State = State . PATHNAME ;
180
166
let skip : number = 1 ;
181
167
182
- if ( this . #shouldTreatAsStandardURL) {
183
- this . #internalResult. pathname = '/' ;
184
- }
185
-
186
168
// If there are authority slashes, like `https://`, then
187
169
// we must transition to the authority section of the URLPattern.
188
170
if ( this . #nextIsAuthoritySlashes( ) ) {
@@ -318,6 +300,13 @@ export class Parser {
318
300
// This should not be reached.
319
301
break ;
320
302
}
303
+
304
+ if ( this . #internalResult. hostname !== undefined &&
305
+ this . #internalResult. port === undefined ) {
306
+ // If the hostname is specified in a constructor string but the port is
307
+ // not, the default port is assumed to be meant.
308
+ this . #internalResult. port = '' ;
309
+ }
321
310
}
322
311
}
323
312
@@ -358,6 +347,31 @@ export class Parser {
358
347
break ;
359
348
}
360
349
350
+ if ( this . #state !== State . INIT && newState !== State . DONE ) {
351
+ // If hostname, pathname or search is skipped but something appears after
352
+ // it, then it takes its default value (usually the empty string).
353
+ if ( [ State . PROTOCOL , State . AUTHORITY , State . USERNAME , State . PASSWORD ]
354
+ . includes ( this . #state) &&
355
+ [ State . PORT , State . PATHNAME , State . SEARCH , State . HASH ]
356
+ . includes ( newState ) ) {
357
+ this . #internalResult. hostname ??= '' ;
358
+ }
359
+ if ( [ State . PROTOCOL , State . AUTHORITY , State . USERNAME , State . PASSWORD ,
360
+ State . HOSTNAME , State . PORT ]
361
+ . includes ( this . #state) &&
362
+ [ State . SEARCH , State . HASH ]
363
+ . includes ( newState ) ) {
364
+ this . #internalResult. pathname ??=
365
+ ( this . #shouldTreatAsStandardURL ? '/' : '' ) ;
366
+ }
367
+ if ( [ State . PROTOCOL , State . AUTHORITY , State . USERNAME , State . PASSWORD ,
368
+ State . HOSTNAME , State . PORT , State . PATHNAME ]
369
+ . includes ( this . #state) &&
370
+ newState === State . HASH ) {
371
+ this . #internalResult. search ??= '' ;
372
+ }
373
+ }
374
+
361
375
this . #changeStateWithoutSettingComponent( newState , skip ) ;
362
376
}
363
377
0 commit comments