-
Notifications
You must be signed in to change notification settings - Fork 22
/
spec.bs
2137 lines (1762 loc) · 137 KB
/
spec.bs
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<pre class="metadata">
Group: WHATWG
H1: URL Pattern
Shortname: urlpattern
Text Macro: TWITTER urlpatterns
Text Macro: LATESTRD 2024-03
Abstract: The URL Pattern Standard provides a web platform primitive for matching URLs based on a convenient pattern syntax.
Indent: 2
Markup Shorthands: markdown yes
Translation: zh-Hans https://htmlspecs.com/urlpattern/
Required IDs: url-pattern,url-pattern-create,url-pattern-match,url-pattern-has-regexp-groups
</pre>
<pre class="link-defaults">
spec:infra; type:dfn; text:list
spec:webidl; type:dfn; text:record
</pre>
<pre class="anchors">
spec: ECMASCRIPT; urlPrefix: https://tc39.es/ecma262/
type: dfn
text: IdentifierPart; url: #prod-IdentifierPart
text: IdentifierStart; url: #prod-IdentifierStart
spec: URL; urlPrefix: https://url.spec.whatwg.org/
type: dfn
text: serialize an integer; url: #serialize-an-integer
text: host serializer; url: #concept-host-serializer
spec: RFC8941; urlPrefix: https://httpwg.org/specs/rfc8941.html
type: dfn
text: structured header; url: top
for: structured header
text: string; url: string
</pre>
<h2 id=urlpatterns>URL patterns</h2>
<h3 id=introduction>Introduction</h3>
A [=URL pattern=] consists of several [=components=], each of which represents a [=/pattern string|pattern=] which could be matched against the corresponding component of a [=/URL=].
It can be constructed using a string for each component, or from a [[#constructor-string-parsing|shorthand string]]. It can optionally be resolved relative to a base URL.
<div class="example" id="example-intro">
<p>The shorthand "`https://example.com/:category/*`" corresponds to the following components:
<dl class="props">
<dt>[=URL pattern/protocol component|protocol=]
<dd>"`https`"
<dt>[=URL pattern/username component|username=]
<dd>"`*`"
<dt>[=URL pattern/password component|password=]
<dd>"`*`"
<dt>[=URL pattern/hostname component|hostname=]
<dd>"`example.com`"
<dt>[=URL pattern/port component|port=]
<dd>""
<dt>[=URL pattern/pathname component|pathname=]
<dd>"`/:category/*`"
<dt>[=URL pattern/search component|search=]
<dd>"`*`"
<dt>[=URL pattern/hash component|hash=]
<dd>"`*`"
</dl>
It matches the following URLs:
<ul class="brief">
* `https://example.com/products/`
* `https://example.com/blog/our-greatest-product-ever`
</ul>
It does not match the following URLs:
<ul class="brief">
* `https://example.com/`
* `http://example.com/products/`
* `https://example.com:8443/blog/our-greatest-product-ever`
</ul>
This is a fairly simple pattern which requires most components to either match an exact string, or allows any string ("`*`"). The [=URL pattern/pathname component=] matches any path with at least two `/`-separated path components, the first of which is captured as "`category`".
</div>
<div class="example" id="example-intro-2">
<p>The shorthand "`http{s}?://{:subdomain.}?shop.example/products/:id([0-9]+)#reviews`" corresponds to the following components:
<dl class="props">
<dt>[=URL pattern/protocol component|protocol=]
<dd>"`http{s}?`"
<dt>[=URL pattern/username component|username=]
<dd>"`*`"
<dt>[=URL pattern/password component|password=]
<dd>"`*`"
<dt>[=URL pattern/hostname component|hostname=]
<dd>"`{:subdomain.}?shop.example`"
<dt>[=URL pattern/port component|port=]
<dd>""
<dt>[=URL pattern/pathname component|pathname=]
<dd>"`/products/:id([0-9]+)`"
<dt>[=URL pattern/search component|search=]
<dd>""
<dt>[=URL pattern/hash component|hash=]
<dd>"`reviews`"
</dl>
It matches the following URLs:
<ul class="brief">
* `https://shop.example/products/74205#reviews`
* `https://[email protected]/products/74656#reviews`
* `http://insecure.shop.example/products/1701#reviews`
</ul>
It does not match the following URLs:
<ul class="brief">
* `https://shop.example/products/2000`
* `http://shop.example:8080/products/0#reviews`
* `https://nx.shop.example/products/01?speed=5#reviews`
* `https://shop.example/products/chair#reviews`
</ul>
This is a more complicated pattern which includes:
* [=part/modifier/optional=] parts marked with `?` (braces are needed to make it unambiguous exactly what is optional), and
* a [=part/type/regexp=] part named "`id`" which uses a regular expression to define what sorts of substrings match (the parentheses are required to mark it as a regular expression, and are not part of the regexp itself).
</div>
<div class="example" id="example-intro-3">
<p>The shorthand "`../admin/*`" with the base URL "`https://discussion.example/forum/?page=2`" corresponds to the following components:
<dl class="props">
<dt>[=URL pattern/protocol component|protocol=]
<dd>"`https`"
<dt>[=URL pattern/username component|username=]
<dd>"`*`"
<dt>[=URL pattern/password component|password=]
<dd>"`*`"
<dt>[=URL pattern/hostname component|hostname=]
<dd>"`discussion.example`"
<dt>[=URL pattern/port component|port=]
<dd>""
<dt>[=URL pattern/pathname component|pathname=]
<dd>"`/admin/*`"
<dt>[=URL pattern/search component|search=]
<dd>"`*`"
<dt>[=URL pattern/hash component|hash=]
<dd>"`*`"
</dl>
It matches the following URLs:
<ul class="brief">
* `https://discussion.example/admin/`
* `https://edd:[email protected]/admin/update?id=1`
</ul>
It does not match the following URLs:
<ul class="brief">
* `https://discussion.example/forum/admin/`
* `http://discussion.example:8080/admin/update?id=1`
</ul>
This pattern demonstrates how pathnames are resolved relative to a base URL, in a similar way to relative URLs.
</div>
<h3 id=urlpattern-class>The {{URLPattern}} class</h3>
<xmp class="idl">
typedef (USVString or URLPatternInit) URLPatternInput;
[Exposed=(Window,Worker)]
interface URLPattern {
constructor(URLPatternInput input, USVString baseURL, optional URLPatternOptions options = {});
constructor(optional URLPatternInput input = {}, optional URLPatternOptions options = {});
boolean test(optional URLPatternInput input = {}, optional USVString baseURL);
URLPatternResult? exec(optional URLPatternInput input = {}, optional USVString baseURL);
readonly attribute USVString protocol;
readonly attribute USVString username;
readonly attribute USVString password;
readonly attribute USVString hostname;
readonly attribute USVString port;
readonly attribute USVString pathname;
readonly attribute USVString search;
readonly attribute USVString hash;
readonly attribute boolean hasRegExpGroups;
};
dictionary URLPatternInit {
USVString protocol;
USVString username;
USVString password;
USVString hostname;
USVString port;
USVString pathname;
USVString search;
USVString hash;
USVString baseURL;
};
dictionary URLPatternOptions {
boolean ignoreCase = false;
};
dictionary URLPatternResult {
sequence<URLPatternInput> inputs;
URLPatternComponentResult protocol;
URLPatternComponentResult username;
URLPatternComponentResult password;
URLPatternComponentResult hostname;
URLPatternComponentResult port;
URLPatternComponentResult pathname;
URLPatternComponentResult search;
URLPatternComponentResult hash;
};
dictionary URLPatternComponentResult {
USVString input;
record<USVString, (USVString or undefined)> groups;
};
</xmp>
Each {{URLPattern}} has an <dfn for="URLPattern">associated URL pattern</dfn>, a [=URL pattern=].
<dl class="domintro non-normative">
<dt><code>|urlPattern| = new {{URLPattern/constructor(input, baseURL, options)|URLPattern}}(|input|)</code></dt>
<dd>
Constructs a new {{URLPattern}} object. The |input| is an object containing separate patterns for each URL component; e.g. hostname, pathname, etc. Missing components will default to a wildcard pattern. In addition, |input| can contain a {{URLPatternInit/baseURL}} property that provides static text patterns for any missing components.
</dd>
<dt><code>|urlPattern| = new {{URLPattern/constructor(input, baseURL, options)|URLPattern}}(|patternString|, |baseURL|)</code></dt>
<dd>
Constructs a new {{URLPattern}} object. |patternString| is a URL string containing pattern syntax for one or more components. If |baseURL| is provided, then |patternString| can be relative. This constructor will always set at least an empty string value and does not default any components to wildcard patterns.
</dd>
<dt><code>|urlPattern| = new {{URLPattern/constructor(input, baseURL, options)|URLPattern}}(|input|, |options|)</code></dt>
<dd>
Constructs a new {{URLPattern}} object. The |options| is an object containing the additional configuration options that can affect how the components are matched. Currently it has only one property {{URLPatternOptions/ignoreCase}} which can be set to true to enable case-insensitive matching.
Note that by default, that is in the absence of the |options| argument, matching is always case-sensitive.
</dd>
<dt><code>|urlPattern| = new {{URLPattern/constructor(input, baseURL, options)|URLPattern}}(|patternString|, |baseURL|, |options|)</code></dt>
<dd>
Constructs a new {{URLPattern}} object. This overrides supports a {{URLPatternOptions}} object when constructing a pattern from a |patternString| object, describing the patterns for individual components, and base URL.
</dd>
<dt><code>|matches| = |urlPattern|.{{URLPattern/test(input, baseURL)|test}}(|input|)</code></dt>
<dd>
Tests if |urlPattern| matches the given arguments. The |input| is an object containing strings representing each URL component; e.g. hostname, pathname, etc. Missing components are treated as empty strings. In addition, |input| can contain a {{URLPatternInit/baseURL}} property that provides values for any missing components. If |urlPattern| matches the |input| on a component-by-component basis then true is returned. Otherwise, false is returned.
</dd>
<dt><code>|matches| = |urlPattern|.{{URLPattern/test(input, baseURL)|test}}(|url|, |baseURL|)</code></dt>
<dd>
Tests if |urlPattern| matches the given arguments. |url| is a URL string. If |baseURL| is provided, then |url| can be relative.
If |urlPattern| matches the |input| on a component-by-component basis then true is returned. Otherwise, false is returned.
</dd>
<dt><code>|result| = |urlPattern|.{{URLPattern/exec(input, baseURL)|exec}}(|input|)</code></dt>
<dd>
Executes the |urlPattern| against the given arguments. The |input| is an object containing strings representing each URL component; e.g. hostname, pathname, etc. Missing components are treated as empty strings. In addition, |input| can contain a baseURL property that provides values for any missing components.
If |urlPattern| matches the |input| on a component-by-component basis then an object is returned containing the results. Matched group values are contained in per-component group objects within the |result| object; e.g. `matches.pathname.groups.id`. If |urlPattern| does not match the |input|, then |result| is null.
</dd>
<dt><code>|result| = |urlPattern|.{{URLPattern/exec(input, baseURL)|exec}}(|url|, |baseURL|)</code></dt>
<dd>
Executes the |urlPattern| against the given arguments. |url| is a URL string. If |baseURL| is provided, then |input| can be relative.
If |urlPattern| matches the |input| on a component-by-component basis then an object is returned containing the results. Matched group values are contained in per-component group objects within the |result| object; e.g. `matches.pathname.groups.id`. If |urlPattern| does not match the |input|, then |result| is null.
</dd>
<dt><code>|urlPattern|.{{URLPattern/protocol}}</code></dt>
<dd>
<p>Returns |urlPattern|'s normalized protocol pattern string.
</dd>
<dt><code>|urlPattern|.{{URLPattern/username}}</code></dt>
<dd>
<p>Returns |urlPattern|'s normalized username pattern string.
</dd>
<dt><code>|urlPattern|.{{URLPattern/password}}</code></dt>
<dd>
<p>Returns |urlPattern|'s normalized password pattern string.
</dd>
<dt><code>|urlPattern|.{{URLPattern/hostname}}</code></dt>
<dd>
<p>Returns |urlPattern|'s normalized hostname pattern string.
</dd>
<dt><code>|urlPattern|.{{URLPattern/port}}</code></dt>
<dd>
<p>Returns |urlPattern|'s normalized port pattern string.
</dd>
<dt><code>|urlPattern|.{{URLPattern/pathname}}</code></dt>
<dd>
<p>Returns |urlPattern|'s normalized pathname pattern string.
</dd>
<dt><code>|urlPattern|.{{URLPattern/search}}</code></dt>
<dd>
<p>Returns |urlPattern|'s normalized search pattern string.
</dd>
<dt><code>|urlPattern|.{{URLPattern/hash}}</code></dt>
<dd>
<p>Returns |urlPattern|'s normalized hash pattern string.
</dd>
<dt><code>|urlPattern|.{{URLPattern/hasRegExpGroups}}</code></dt>
<dd>
<p>Returns whether |urlPattern| contains one or more groups which uses regular expression matching.
</dd>
</dl>
<div algorithm>
The <dfn constructor for=URLPattern lt="URLPattern(input, baseURL, options)">new URLPattern(|input|, |baseURL|, |options|)</dfn> constructor steps are:
1. Run [=initialize=] given [=this=], |input|, |baseURL|, and |options|.
</div>
<div algorithm>
The <dfn constructor for=URLPattern lt="URLPattern(input, options)">new URLPattern(|input|, |options|)</dfn> constructor steps are:
1. Run [=initialize=] given [=this=], |input|, null, and |options|.
</div>
<div algorithm>
To <dfn for=URLPattern>initialize</dfn> a {{URLPattern}} given a {{URLPattern}} |this|, {{URLPatternInput}} |input|, string or null |baseURL|, and {{URLPatternOptions}} |options|:
1. Set |this|'s [=URLPattern/associated URL pattern=] to the result of [=create=] given |input|, |baseURL|, and |options|.
</div>
<div algorithm>
The <dfn attribute for="URLPattern">protocol</dfn> getter steps are:
1. Return [=this=]'s [=URLPattern/associated URL pattern=]'s [=URL pattern/protocol component=]'s [=component/pattern string=].
</div>
<div algorithm>
The <dfn attribute for="URLPattern">username</dfn> getter steps are:
1. Return [=this=]'s [=URLPattern/associated URL pattern=]'s [=URL pattern/username component=]'s [=component/pattern string=].
</div>
<div algorithm>
The <dfn attribute for="URLPattern">password</dfn> getter steps are:
1. Return [=this=]'s [=URLPattern/associated URL pattern=]'s [=URL pattern/password component=]'s [=component/pattern string=].
</div>
<div algorithm>
The <dfn attribute for="URLPattern">hostname</dfn> getter steps are:
1. Return [=this=]'s [=URLPattern/associated URL pattern=]'s [=URL pattern/hostname component=]'s [=component/pattern string=].
</div>
<div algorithm>
The <dfn attribute for="URLPattern">port</dfn> getter steps are:
1. Return [=this=]'s [=URLPattern/associated URL pattern=]'s [=URL pattern/port component=]'s [=component/pattern string=].
</div>
<div algorithm>
The <dfn attribute for="URLPattern">pathname</dfn> getter steps are:
1. Return [=this=]'s [=URLPattern/associated URL pattern=]'s [=URL pattern/pathname component=]'s [=component/pattern string=].
</div>
<div algorithm>
The <dfn attribute for="URLPattern">search</dfn> getter steps are:
1. Return [=this=]'s [=URLPattern/associated URL pattern=]'s [=URL pattern/search component=]'s [=component/pattern string=].
</div>
<div algorithm>
The <dfn attribute for="URLPattern">hash</dfn> getter steps are:
1. Return [=this=]'s [=URLPattern/associated URL pattern=]'s [=URL pattern/hash component=]'s [=component/pattern string=].
</div>
<div algorithm>
The <dfn attribute for="URLPattern">hasRegExpGroups</dfn> getter steps are:
1. If [=this=]'s [=URLPattern/associated URL pattern=]'s [=URL pattern/has regexp groups=], then return true.
1. Return false.
</div>
<div algorithm>
The <dfn method for="URLPattern">test(|input|, |baseURL|)</dfn> method steps are:
1. Let |result| be the result of [=URL pattern/match=] given [=this=]'s [=URLPattern/associated URL pattern=], |input|, and |baseURL| if given.
1. If |result| is null, return false.
1. Return true.
</div>
<div algorithm>
The <dfn method for="URLPattern">exec(|input|, |baseURL|)</dfn> method steps are:
1. Return the result of [=URL pattern/match=] given [=this=]'s [=URLPattern/associated URL pattern=], |input|, and |baseURL| if given.
</div>
<h3 id=url-pattern-struct>The URL pattern struct</h3>
A <dfn export>URL pattern</dfn> is a [=struct=] with the following [=struct/items=]:
* <dfn for="URL pattern">protocol component</dfn>, a [=component=]
* <dfn for="URL pattern">username component</dfn>, a [=component=]
* <dfn for="URL pattern">password component</dfn>, a [=component=]
* <dfn for="URL pattern">hostname component</dfn>, a [=component=]
* <dfn for="URL pattern">port component</dfn>, a [=component=]
* <dfn for="URL pattern">pathname component</dfn>, a [=component=]
* <dfn for="URL pattern">search component</dfn>, a [=component=]
* <dfn for="URL pattern">hash component</dfn>, a [=component=]
A <dfn>component</dfn> is a [=struct=] with the following [=struct/items=]:
* <dfn for=component>pattern string</dfn>, a [=pattern string/well formed=] [=/pattern string=]
* <dfn for=component>regular expression</dfn>, a {{RegExp}}
* <dfn for=component>group name list</dfn>, a [=list=] of strings
* <dfn for=component>has regexp groups</dfn>, a [=boolean=]
<h3 id=high-level-operations>High-level operations</h3>
<div algorithm>
To <dfn export for="URL pattern">create</dfn> a [=URL pattern=] given a {{URLPatternInput}} |input|, string or null |baseURL|, and {{URLPatternOptions}} |options|:
1. Let |init| be null.
1. If |input| is a [=scalar value string=] then:
1. Set |init| to the result of running [=parse a constructor string=] given |input|.
1. If |baseURL| is null and |init|["{{URLPatternInit/protocol}}"] does not [=map/exist=], then throw a {{TypeError}}.
1. If |baseURL| is not null, [=map/set=] |init|["{{URLPatternInit/baseURL}}"] to |baseURL|.
1. Otherwise:
1. [=Assert=]: |input| is a {{URLPatternInit}}.
1. If |baseURL| is not null, then throw a {{TypeError}}.
1. Set |init| to |input|.
1. Let |processedInit| be the result of [=process a URLPatternInit=] given |init|, "`pattern`", null, null, null, null, null, null, null, and null.
1. [=list/For each=] |componentName| of « "{{URLPatternInit/protocol}}", "{{URLPatternInit/username}}", "{{URLPatternInit/password}}", "{{URLPatternInit/hostname}}", "{{URLPatternInit/port}}", "{{URLPatternInit/pathname}}", "{{URLPatternInit/search}}", "{{URLPatternInit/hash}}" »:
1. If |processedInit|[|componentName|] does not [=map/exist=], then [=map/set=] |processedInit|[|componentName|] to "`*`".
1. If |processedInit|["{{URLPatternInit/protocol}}"] is a [=special scheme=] and |processedInit|["{{URLPatternInit/port}}"] is a string which represents its corresponding [=default port=] in radix-10 using [=ASCII digits=] then set |processedInit|["{{URLPatternInit/port}}"] to the empty string.
1. Let |urlPattern| be a new [=URL pattern=].
1. Set |urlPattern|'s [=URL pattern/protocol component=] to the result of [=compiling a component=] given |processedInit|["{{URLPatternInit/protocol}}"], [=canonicalize a protocol=], and [=default options=].
1. Set |urlPattern|'s [=URL pattern/username component=] to the result of [=compiling a component=] given |processedInit|["{{URLPatternInit/username}}"], [=canonicalize a username=], and [=default options=].
1. Set |urlPattern|'s [=URL pattern/password component=] to the result of [=compiling a component=] given |processedInit|["{{URLPatternInit/password}}"], [=canonicalize a password=], and [=default options=].
1. If the result running [=hostname pattern is an IPv6 address=] given |processedInit|["{{URLPatternInit/hostname}}"] is true, then set |urlPattern|'s [=URL pattern/hostname component=] to the result of [=compiling a component=] given |processedInit|["{{URLPatternInit/hostname}}"], [=canonicalize an IPv6 hostname=], and [=hostname options=].
1. Otherwise, set |urlPattern|'s [=URL pattern/hostname component=] to the result of [=compiling a component=] given |processedInit|["{{URLPatternInit/hostname}}"], [=canonicalize a hostname=], and [=hostname options=].
1. Set |urlPattern|'s [=URL pattern/port component=] to the result of [=compiling a component=] given |processedInit|["{{URLPatternInit/port}}"], [=canonicalize a port=], and [=default options=].
1. Let |compileOptions| be a copy of the [=default options=] with the [=options/ignore case=] property set to |options|["{{URLPatternOptions/ignoreCase}}"].
1. If the result of running [=protocol component matches a special scheme=] given |urlPattern|'s [=URL pattern/protocol component=] is true, then:
1. Let |pathCompileOptions| be copy of the [=pathname options=] with the [=options/ignore case=] property set to |options|["{{URLPatternOptions/ignoreCase}}"].
1. Set |urlPattern|'s [=URL pattern/pathname component=] to the result of [=compiling a component=] given |processedInit|["{{URLPatternInit/pathname}}"], [=canonicalize a pathname=], and |pathCompileOptions|.
1. Otherwise set |urlPattern|'s [=URL pattern/pathname component=] to the result of [=compiling a component=] given |processedInit|["{{URLPatternInit/pathname}}"], [=canonicalize an opaque pathname=], and |compileOptions|.
1. Set |urlPattern|'s [=URL pattern/search component=] to the result of [=compiling a component=] given |processedInit|["{{URLPatternInit/search}}"], [=canonicalize a search=], and |compileOptions|.
1. Set |urlPattern|'s [=URL pattern/hash component=] to the result of [=compiling a component=] given |processedInit|["{{URLPatternInit/hash}}"], [=canonicalize a hash=], and |compileOptions|.
1. Return |urlPattern|.
</div>
<div algorithm>
To perform a <dfn export for="URL pattern">match</dfn> given a [=URL pattern=] |urlPattern|, a {{URLPatternInput}} or [=/URL=] |input|, and an optional string |baseURLString|:
1. Let |protocol| be the empty string.
1. Let |username| be the empty string.
1. Let |password| be the empty string.
1. Let |hostname| be the empty string.
1. Let |port| be the empty string.
1. Let |pathname| be the empty string.
1. Let |search| be the empty string.
1. Let |hash| be the empty string.
1. Let |inputs| be an empty [=list=].
1. [=list/Append=] |input| to |inputs|.
1. If |input| is a {{URLPatternInit}} then:
1. If |baseURLString| was given, throw a {{TypeError}}.
1. Let |applyResult| be the result of [=process a URLPatternInit=] given |input|, "url", |protocol|, |username|, |password|, |hostname|, |port|, |pathname|, |search|, and |hash|. If this throws an exception, catch it, and return null.
1. Set |protocol| to |applyResult|["{{URLPatternInit/protocol}}"].
1. Set |username| to |applyResult|["{{URLPatternInit/username}}"].
1. Set |password| to |applyResult|["{{URLPatternInit/password}}"].
1. Set |hostname| to |applyResult|["{{URLPatternInit/hostname}}"].
1. Set |port| to |applyResult|["{{URLPatternInit/port}}"].
1. Set |pathname| to |applyResult|["{{URLPatternInit/pathname}}"].
1. Set |search| to |applyResult|["{{URLPatternInit/search}}"].
1. Set |hash| to |applyResult|["{{URLPatternInit/hash}}"].
1. Otherwise:
1. Let |url| be |input|.
1. If |input| is a {{USVString}}:
1. Let |baseURL| be null.
1. If |baseURLString| was given, then:
1. Set |baseURL| to the result of [=URL parser|parsing=] |baseURLString|.
1. If |baseURL| is failure, return null.
1. [=list/Append=] |baseURLString| to |inputs|.
1. Set |url| to the result of [=URL parser|parsing=] |input| given |baseURL|.
1. If |url| is failure, return null.
1. [=Assert=]: |url| is a [=/URL=].
1. Set |protocol| to |url|'s [=url/scheme=].
1. Set |username| to |url|'s [=url/username=].
1. Set |password| to |url|'s [=url/password=].
1. Set |hostname| to |url|'s [=url/host=], [=host serializer|serialized=], or the empty string if the value is null.
1. Set |port| to |url|'s [=url/port=], [=serialize an integer|serialized=], or the empty string if the value is null.
1. Set |pathname| to the result of [=URL path serializing=] |url|.
1. Set |search| to |url|'s [=url/query=] or the empty string if the value is null.
1. Set |hash| to |url|'s [=url/fragment=] or the empty string if the value is null.
1. Let |protocolExecResult| be [$RegExpBuiltinExec$](|urlPattern|'s [=URL pattern/protocol component=]'s [=component/regular expression=], |protocol|).
1. Let |usernameExecResult| be [$RegExpBuiltinExec$](|urlPattern|'s [=URL pattern/username component=]'s [=component/regular expression=], |username|).
1. Let |passwordExecResult| be [$RegExpBuiltinExec$](|urlPattern|'s [=URL pattern/password component=]'s [=component/regular expression=], |password|).
1. Let |hostnameExecResult| be [$RegExpBuiltinExec$](|urlPattern|'s [=URL pattern/hostname component=]'s [=component/regular expression=], |hostname|).
1. Let |portExecResult| be [$RegExpBuiltinExec$](|urlPattern|'s [=URL pattern/port component=]'s [=component/regular expression=], |port|).
1. Let |pathnameExecResult| be [$RegExpBuiltinExec$](|urlPattern|'s [=URL pattern/pathname component=]'s [=component/regular expression=], |pathname|).
1. Let |searchExecResult| be [$RegExpBuiltinExec$](|urlPattern|'s [=URL pattern/search component=]'s [=component/regular expression=], |search|).
1. Let |hashExecResult| be [$RegExpBuiltinExec$](|urlPattern|'s [=URL pattern/hash component=]'s [=component/regular expression=], |hash|).
1. If |protocolExecResult|, |usernameExecResult|, |passwordExecResult|, |hostnameExecResult|, |portExecResult|, |pathnameExecResult|, |searchExecResult|, or |hashExecResult| are null then return null.
1. Let |result| be a new {{URLPatternResult}}.
1. Set |result|["{{URLPatternResult/inputs}}"] to |inputs|.
1. Set |result|["{{URLPatternResult/protocol}}"] to the result of [=creating a component match result=] given |urlPattern|'s [=URL pattern/protocol component=], |protocol|, and |protocolExecResult|.
1. Set |result|["{{URLPatternResult/username}}"] to the result of [=creating a component match result=] given |urlPattern|'s [=URL pattern/username component=], |username|, and |usernameExecResult|.
1. Set |result|["{{URLPatternResult/password}}"] to the result of [=creating a component match result=] given |urlPattern|'s [=URL pattern/password component=], |password|, and |passwordExecResult|.
1. Set |result|["{{URLPatternResult/hostname}}"] to the result of [=creating a component match result=] given |urlPattern|'s [=URL pattern/hostname component=], |hostname|, and |hostnameExecResult|.
1. Set |result|["{{URLPatternResult/port}}"] to the result of [=creating a component match result=] given |urlPattern|'s [=URL pattern/port component=], |port|, and |portExecResult|.
1. Set |result|["{{URLPatternResult/pathname}}"] to the result of [=creating a component match result=] given |urlPattern|'s [=URL pattern/pathname component=], |pathname|, and |pathnameExecResult|.
1. Set |result|["{{URLPatternResult/search}}"] to the result of [=creating a component match result=] given |urlPattern|'s [=URL pattern/search component=], |search|, and |searchExecResult|.
1. Set |result|["{{URLPatternResult/hash}}"] to the result of [=creating a component match result=] given |urlPattern|'s [=URL pattern/hash component=], |hash|, and |hashExecResult|.
1. Return |result|.
</div>
<div algorithm>
A [=URL pattern=] |urlPattern| <dfn export for="URL pattern">has regexp groups</dfn> if the following steps return true:
1. If |urlPattern|'s [=URL pattern/protocol component=] [=component/has regexp groups=] is true, then return true.
1. If |urlPattern|'s [=URL pattern/username component=] [=component/has regexp groups=] is true, then return true.
1. If |urlPattern|'s [=URL pattern/password component=] [=component/has regexp groups=] is true, then return true.
1. If |urlPattern|'s [=URL pattern/hostname component=] [=component/has regexp groups=] is true, then return true.
1. If |urlPattern|'s [=URL pattern/port component=] [=component/has regexp groups=] is true, then return true.
1. If |urlPattern|'s [=URL pattern/pathname component=] [=component/has regexp groups=] is true, then return true.
1. If |urlPattern|'s [=URL pattern/search component=] [=component/has regexp groups=] is true, then return true.
1. If |urlPattern|'s [=URL pattern/hash component=] [=component/has regexp groups=] is true, then return true.
1. Return false.
</div>
<h3 id=urlpattern-internals>Internals</h3>
<div algorithm>
To <dfn>compile a component</dfn> given a string |input|, [=/encoding callback=] |encoding callback|, and [=/options=] |options|:
1. Let |part list| be the result of running [=parse a pattern string=] given |input|, |options|, and |encoding callback|.
1. Let (|regular expression string|, |name list|) be the result of running [=generate a regular expression and name list=] given |part list| and |options|.
1. Let |flags| be an empty string.
1. If |options|'s [=options/ignore case=] is true then set |flags| to "`vi`".
1. Otherwise set |flags| to "`v`"
1. Let |regular expression| be [$RegExpCreate$](|regular expression string|, |flags|). If this throws an exception, catch it, and throw a {{TypeError}}.
<p class="note">The specification uses regular expressions to perform all matching, but this is not mandated. Implementations are free to perform matching directly against the [=/part list=] when possible; e.g. when there are no custom regexp matching groups. If there are custom regular expressions, however, its important that they be immediately evaluated in the [=compile a component=] algorithm so an error can be thrown if they are invalid.
1. Let |pattern string| be the result of running [=generate a pattern string=] given |part list| and |options|.
1. Let |has regexp groups| be false.
1. [=list/For each=] |part| of |part list|:
1. If |part|'s [=part/type=] is "<a for=part/type>`regexp`</a>", then set |has regexp groups| to true.
1. Return a new [=component=] whose [=component/pattern string=] is |pattern string|, [=component/regular expression=] is |regular expression|, [=component/group name list=] is |name list|, and [=component/has regexp groups=] is |has regexp groups|.
</div>
<div algorithm>
To <dfn>create a component match result</dfn> given a [=component=] |component|, a string |input|, and an array representing the output of [$RegExpBuiltinExec$] |execResult|:
1. Let |result| be a new {{URLPatternComponentResult}}.
1. Set |result|["{{URLPatternComponentResult/input}}"] to |input|.
1. Let |groups| be a <code>[=record=]<{{USVString}}, ({{USVString}} or {{undefined}})></code>.
1. Let |index| be 1.
1. While |index| is less than [$Get$](|execResult|, "`length`"):
1. Let |name| be |component|'s [=component/group name list=][|index| − 1].
1. Let |value| be [$Get$](|execResult|, [$ToString$](|index|)).
1. Set |groups|[|name|] to |value|.
1. Increment |index| by 1.
1. Set |result|["{{URLPatternComponentResult/groups}}"] to |groups|.
1. Return |result|.
</div>
The <dfn>default options</dfn> is an [=options=] [=struct=] with [=options/delimiter code point=] set to the empty string and [=options/prefix code point=] set to the empty string.
The <dfn>hostname options</dfn> is an [=options=] [=struct=] with [=options/delimiter code point=] set "`.`" and [=options/prefix code point=] set to the empty string.
The <dfn>pathname options</dfn> is an [=options=] [=struct=] with [=options/delimiter code point=] set "`/`" and [=options/prefix code point=] set to "`/`".
<div algorithm>
To determine if a <dfn>protocol component matches a special scheme</dfn> given a [=component=] |protocol component|:
1. Let |special scheme list| be a [=list=] populated with all of the [=special schemes=].
1. [=list/For each=] |scheme| of |special scheme list|:
1. Let |test result| be [$RegExpBuiltinExec$](|protocol component|'s [=component/regular expression=], |scheme|).
1. If |test result| is not null, then return true.
1. Return false.
</div>
<div algorithm>
To determine if a <dfn>hostname pattern is an IPv6 address</dfn> given a [=/pattern string=] |input|:
1. If |input|'s [=string/code point length=] is less than 2, then return false.
1. Let |input code points| be |input| interpreted as a [=list=] of [=/code points=].
1. If |input code points|[0] is U+005B (`[`), then return true.
1. If |input code points|[0] is U+007B (`{`) and |input code points|[1] is U+005B (`[`), then return true.
1. If |input code points|[0] is U+005C (<code>\</code>) and |input code points|[1] is U+005B (`[`), then return true.
1. Return false.
</div>
<h3 id=constructor-string-parsing>Constructor string parsing</h3>
A <dfn export>constructor string parser</dfn> is a [=struct=].
A [=constructor string parser=] has an associated <dfn export for="constructor string parser">input</dfn>, a string, which must be set upon creation.
A [=constructor string parser=] has an associated <dfn export for="constructor string parser">token list</dfn>, a [=/token list=], which must be set upon creation.
A [=constructor string parser=] has an associated <dfn export for="constructor string parser">result</dfn>, a {{URLPatternInit}}, initially set to a new {{URLPatternInit}}.
A [=constructor string parser=] has an associated <dfn export for="constructor string parser">component start</dfn>, a number, initially set to 0.
A [=constructor string parser=] has an associated <dfn export for="constructor string parser">token index</dfn>, a number, initially set to 0.
A [=constructor string parser=] has an associated <dfn export for="constructor string parser">token increment</dfn>, a number, initially set to 1.
A [=constructor string parser=] has an associated <dfn export for="constructor string parser">group depth</dfn>, a number, initially set to 0.
A [=constructor string parser=] has an associated <dfn export for="constructor string parser">hostname IPv6 bracket depth</dfn>, a number, initially set to 0.
A [=constructor string parser=] has an associated <dfn export for="constructor string parser">protocol matches a special scheme flag</dfn>, a boolean, initially set to false.
A [=constructor string parser=] has an associated <dfn export for="constructor string parser">state</dfn>, a string, initially set to "<a for="constructor string parser/state">`init`</a>". It must be one of the following:
<ul>
<li>"<dfn for="constructor string parser/state">`init`</dfn>"</li>
<li>"<dfn export for="constructor string parser/state">`protocol`</dfn>"</li>
<li>"<dfn export for="constructor string parser/state">`authority`</dfn>"</li>
<li>"<dfn export for="constructor string parser/state">`username`</dfn>"</li>
<li>"<dfn export for="constructor string parser/state">`password`</dfn>"</li>
<li>"<dfn export for="constructor string parser/state">`hostname`</dfn>"</li>
<li>"<dfn export for="constructor string parser/state">`port`</dfn>"</li>
<li>"<dfn export for="constructor string parser/state">`pathname`</dfn>"</li>
<li>"<dfn export for="constructor string parser/state">`search`</dfn>"</li>
<li>"<dfn export for="constructor string parser/state">`hash`</dfn>"</li>
<li>"<dfn export for="constructor string parser/state">`done`</dfn>"</li>
</ul>
<div class=note>
<p>The URLPattern constructor string algorithm is very similar to the [=basic URL parser=] algorithm, but some differences prevent us from using that algorithm directly.
<p>First, the URLPattern constructor string parser operates on [=tokens=] generated using the "`lenient`" [=tokenize policy=]. In constrast, [=basic URL parser=] operates on code points. Operating on [=tokens=] allows the URLPattern constructor string parser to more easily distinguish between code points that are significant pattern syntax and code points that might be a URL component separator. For example, it makes it trivial to handle named groups like "`:hmm`" in "`https://a.c:hmm.example.com:8080`" without getting confused with the port number.
<p>Second, the URLPattern constructor string parser needs to avoid applying URL canonicalization to all code points like [=basic URL parser=] does. Instead we perform canonicalization on only parts of the pattern string we know are safe later when compiling each component pattern string.
<p>Finally, the URLPattern constructor string parser does not handle some parts of the [=basic URL parser=] state machine. For example, it does not treat backslashes specially as they would all be treated as pattern characters and would require excessive escaping. In addition, this parser might not handle some more esoteric parts of the URL parsing algorithm like file URLs with a hostname. The goal with this parser was to handle the most common URLs while allowing any niche case to be handled instead via the {{URLPatternInit}} constructor.
</div>
<div class=note>
<p>In the constructor string algorithm, the pathname, search, and hash are wildcarded if earlier components are specified but later ones are not. For example, "`https://example.com/foo`" matches any search and any hash. Similarly, "`https://example.com`" matches any URL on that origin. This is analogous to the notion of a more specific component in the notes about [=process a URLPatternInit=] (e.g., a search is more specific than a pathname), but the constructor syntax only has a few cases where it is possible to specify a more specific component without also specifying the less specific components.
<p>The username and password components are always wildcard unless they are explicitly specified.
<p>If a hostname is specified and the port is not, the port is assumed to be the default port. If authors want to match any port, they have to write `:*` explicitly. For example, "`https://*`" is any HTTPS origin on port 443, and "`https://*:*`" is any HTTPS origin on any port.
</div>
<div algorithm>
To <dfn>parse a constructor string</dfn> given a string |input|:
1. Let |parser| be a new [=constructor string parser=] whose [=constructor string parser/input=] is |input| and [=constructor string parser/token list=] is the result of running [=tokenize=] given |input| and "<a for="tokenize policy">`lenient`</a>".
1. [=While=] |parser|'s [=constructor string parser/token index=] is less than |parser|'s [=constructor string parser/token list=] [=list/size=]:
1. Set |parser|'s [=constructor string parser/token increment=] to 1.
<p class="note">On every iteration of the parse loop the |parser|'s [=constructor string parser/token index=] will be incremented by its [=constructor string parser/token increment=] value. Typically this means incrementing by 1, but at certain times it is set to zero. The [=constructor string parser/token increment=] is then always reset back to 1 at the top of the loop.
1. If |parser|'s [=constructor string parser/token list=][|parser|'s [=constructor string parser/token index=]]'s [=token/type=] is "<a for=token/type>`end`</a>" then:
1. If |parser|'s [=constructor string parser/state=] is "<a for="constructor string parser/state">`init`</a>":
<p class="note">If we reached the end of the string in the "<a for="constructor string parser/state">`init`</a>" [=constructor string parser/state=], then we failed to find a protocol terminator and this has to be a relative URLPattern constructor string.
1. Run [=rewind=] given |parser|.
<p class=note>We next determine at which component the relative pattern begins. Relative pathnames are most common, but URLs and URLPattern constructor strings can begin with the search or hash components as well.
1. If the result of running [=is a hash prefix=] given |parser| is true, then run [=change state=] given |parser|, "<a for="constructor string parser/state">`hash`</a>" and 1.
1. Otherwise if the result of running [=is a search prefix=] given |parser| is true:
1. Run [=change state=] given |parser|, "<a for="constructor string parser/state">`search`</a>" and 1.
1. Otherwise:
1. Run [=change state=] given |parser|, "<a for="constructor string parser/state">`pathname`</a>" and 0.
1. Increment |parser|'s [=constructor string parser/token index=] by |parser|'s [=constructor string parser/token increment=].
1. [=Continue=].
1. If |parser|'s [=constructor string parser/state=] is "<a for="constructor string parser/state">`authority`</a>":
<p class=note>If we reached the end of the string in the "<a for="constructor string parser/state">`authority`</a>" [=constructor string parser/state=], then we failed to find an "`@`". Therefore there is no username or password.
1. Run [=rewind and set state=] given |parser|, and "<a for="constructor string parser/state">`hostname`</a>".
1. Increment |parser|'s [=constructor string parser/token index=] by |parser|'s [=constructor string parser/token increment=].
1. [=Continue=].
1. Run [=change state=] given |parser|, "<a for="constructor string parser/state">`done`</a>" and 0.
1. [=Break=].
1. If the result of running [=is a group open=] given |parser| is true:
<div class=note>
<p>We ignore all code points within "`{ ... }`" pattern groupings. It would not make sense to allow a URL component boundary to lie within a grouping; e.g. "`https://example.c{om/fo}o`". While not supported within [=well formed=] [=/pattern strings=], we handle nested groupings here to avoid parser confusion.
<p>It is not necessary to perform this logic for regexp or named groups since those values are collapsed into individual [=tokens=] by the [=tokenize=] algorithm.
</div>
1. Increment |parser|'s [=constructor string parser/group depth=] by 1.
1. Increment |parser|'s [=constructor string parser/token index=] by |parser|'s [=constructor string parser/token increment=].
1. [=Continue=].
1. If |parser|'s [=constructor string parser/group depth=] is greater than 0:
1. If the result of running [=is a group close=] given |parser| is true, then decrement |parser|'s [=constructor string parser/group depth=] by 1.
1. Otherwise:
1. Increment |parser|'s [=constructor string parser/token index=] by |parser|'s [=constructor string parser/token increment=].
1. [=Continue=].
1. Switch on |parser|'s [=constructor string parser/state=] and run the associated steps:
<dl class="switch">
<dt>"<a for="constructor string parser/state">`init`</a>"</dt>
<dd>
1. If the result of running [=is a protocol suffix=] given |parser| is true:
1. Run [=rewind and set state=] given |parser| and "<a for="constructor string parser/state">`protocol`</a>".
</dd>
<dt>"<a for="constructor string parser/state">`protocol`</a>"</dt>
<dd>
1. If the result of running [=is a protocol suffix=] given |parser| is true:
1. Run [=compute protocol matches a special scheme flag=] given |parser|.
<p class="note">We need to eagerly compile the protocol component to determine if it matches any [=special schemes=]. If it does then certain special rules apply. It determines if the pathname defaults to a "`/`" and also whether we will look for the username, password, hostname, and port components. Authority slashes can also cause us to look for these components as well. Otherwise we treat this as an "opaque path URL" and go straight to the pathname component.
1. Let |next state| be "<a for="constructor string parser/state">`pathname`</a>".
1. Let |skip| be 1.
1. If the result of running [=next is authority slashes=] given |parser| is true:
1. Set |next state| to "<a for="constructor string parser/state">`authority`</a>".
1. Set |skip| to 3.
1. Otherwise if |parser|'s [=constructor string parser/protocol matches a special scheme flag=] is true, then set |next state| to "<a for="constructor string parser/state">`authority`</a>".
1. Run [=change state=] given |parser|, |next state|, and |skip|.
</dd>
<dt>"<a for="constructor string parser/state">`authority`</a>"</dt>
<dd>
1. If the result of running [=is an identity terminator=] given |parser| is true, then run [=rewind and set state=] given |parser| and "<a for="constructor string parser/state">`username`</a>".
1. Otherwise if any of the following are true:
<ul>
<li>the result of running [=is a pathname start=] given |parser|;</li>
<li>the result of running [=is a search prefix=] given |parser|; or</li>
<li>the result of running [=is a hash prefix=] given |parser|,</li>
</ul>
<p>then run [=rewind and set state=] given |parser| and "<a for="constructor string parser/state">`hostname`</a>".
</dd>
<dt>"<a for="constructor string parser/state">`username`</a>"</dt>
<dd>
1. If the result of running [=is a password prefix=] given |parser| is true, then run [=change state=] given |parser|, "<a for="constructor string parser/state">`password`</a>", and 1.
1. Otherwise if the result of running [=is an identity terminator=] given |parser| is true, then run [=change state=] given |parser|, "<a for="constructor string parser/state">`hostname`</a>", and 1.
</dd>
<dt>"<a for="constructor string parser/state">`password`</a>"</dt>
<dd>
1. If the result of running [=is an identity terminator=] given |parser| is true, then run [=change state=] given |parser|, "<a for="constructor string parser/state">`hostname`</a>", and 1.
</dd>
<dt>"<a for="constructor string parser/state">`hostname`</a>"</dt>
<dd>
1. If the result of running [=is an IPv6 open=] given |parser| is true, then increment |parser|'s [=constructor string parser/hostname IPv6 bracket depth=] by 1.
1. Otherwise if the result of running [=is an IPv6 close=] given |parser| is true, then decrement |parser|'s [=constructor string parser/hostname IPv6 bracket depth=] by 1.
1. Otherwise if the result of running [=is a port prefix=] given |parser| is true and |parser|'s [=constructor string parser/hostname IPv6 bracket depth=] is zero, then run [=change state=] given |parser|, "<a for="constructor string parser/state">`port`</a>", and 1.
1. Otherwise if the result of running [=is a pathname start=] given |parser| is true, then run [=change state=] given |parser|, "<a for="constructor string parser/state">`pathname`</a>", and 0.
1. Otherwise if the result of running [=is a search prefix=] given |parser| is true, then run [=change state=] given |parser|, "<a for="constructor string parser/state">`search`</a>", and 1.
1. Otherwise if the result of running [=is a hash prefix=] given |parser| is true, then run [=change state=] given |parser|, "<a for="constructor string parser/state">`hash`</a>", and 1.
</dd>
<dt>"<a for="constructor string parser/state">`port`</a>"</dt>
<dd>
1. If the result of running [=is a pathname start=] given |parser| is true, then run [=change state=] given |parser|, "<a for="constructor string parser/state">`pathname`</a>", and 0.
1. Otherwise if the result of running [=is a search prefix=] given |parser| is true, then run [=change state=] given |parser|, "<a for="constructor string parser/state">`search`</a>", and 1.
1. Otherwise if the result of running [=is a hash prefix=] given |parser| is true, then run [=change state=] given |parser|, "<a for="constructor string parser/state">`hash`</a>", and 1.
</dd>
<dt>"<a for="constructor string parser/state">`pathname`</a>"</dt>
<dd>
1. If the result of running [=is a search prefix=] given |parser| is true, then run [=change state=] given |parser|, "<a for="constructor string parser/state">`search`</a>", and 1.
1. Otherwise if the result of running [=is a hash prefix=] given |parser| is true, then run [=change state=] given |parser|, "<a for="constructor string parser/state">`hash`</a>", and 1.
</dd>
<dt>"<a for="constructor string parser/state">`search`</a>"</dt>
<dd>
1. If the result of running [=is a hash prefix=] given |parser| is true, then run [=change state=] given |parser|, "<a for="constructor string parser/state">`hash`</a>", and 1.
</dd>
<dt>"<a for="constructor string parser/state">`hash`</a>"</dt>
<dd>
1. Do nothing.
</dd>
<dt>"<a for="constructor string parser/state">`done`</a>"</dt>
<dd>
1. [=Assert=]: This step is never reached.
</dd>
</dl>
1. Increment |parser|'s [=constructor string parser/token index=] by |parser|'s [=constructor string parser/token increment=].
1. If |parser|'s [=constructor string parser/result=] [=map/contains=] "{{URLPatternInit/hostname}}" and not "{{URLPatternInit/port}}", then set |parser|'s [=constructor string parser/result=]["{{URLPatternInit/port}}"] to the empty string.
<div class="note">This is special-cased because when an author does not specify a port, they usually intend the default port. If any port is acceptable, the author can specify it as a wildcard explicitly. For example, "`https://example.com/*`" does not match URLs beginning with "`https://example.com:8443/`", which is a different origin.</div>
1. Return |parser|'s [=constructor string parser/result=].
</div>
<div algorithm>
To <dfn>change state</dfn> given a [=constructor string parser=] |parser|, a [=constructor string parser/state=] |new state|, and a number |skip|:
1. If |parser|'s [=constructor string parser/state=] is not "<a for="constructor string parser/state">`init`</a>", not "<a for="constructor string parser/state">`authority`</a>", and not "<a for="constructor string parser/state">`done`</a>", then set |parser|'s [=constructor string parser/result=][|parser|'s [=constructor string parser/state=]] to the result of running [=make a component string=] given |parser|.
1. If |parser|'s [=constructor string parser/state=] is not "<a for="constructor string parser/state">`init`</a>" and |new state| is not "<a for="constructor string parser/state">`done`</a>", then:
1. If |parser|'s [=constructor string parser/state=] is "<a for="constructor string parser/state">`protocol`</a>", "<a for="constructor string parser/state">`authority`</a>", "<a for="constructor string parser/state">`username`</a>", or "<a for="constructor string parser/state">`password`</a>"; |new state| is "<a for="constructor string parser/state">`port`</a>", "<a for="constructor string parser/state">`pathname`</a>", "<a for="constructor string parser/state">`search`</a>", or "<a for="constructor string parser/state">`hash`</a>"; and |parser|'s [=constructor string parser/result=]["{{URLPatternInit/hostname}}"] does not [=map/exist=], then set |parser|'s [=constructor string parser/result=]["{{URLPatternInit/hostname}}"] to the empty string.
1. If |parser|'s [=constructor string parser/state=] is "<a for="constructor string parser/state">`protocol`</a>", "<a for="constructor string parser/state">`authority`</a>", "<a for="constructor string parser/state">`username`</a>", "<a for="constructor string parser/state">`password`</a>", "<a for="constructor string parser/state">`hostname`</a>", or "<a for="constructor string parser/state">`port`</a>"; |new state| is "<a for="constructor string parser/state">`search`</a>" or "<a for="constructor string parser/state">`hash`</a>"; and |parser|'s [=constructor string parser/result=]["{{URLPatternInit/pathname}}"] does not [=map/exist=], then:
1. If |parser|'s [=constructor string parser/protocol matches a special scheme flag=] is true, then set |parser|'s [=constructor string parser/result=]["{{URLPatternInit/pathname}}"] to "`/`".
1. Otherwise, set |parser|'s [=constructor string parser/result=]["{{URLPatternInit/pathname}}"] to the empty string.
1. If |parser|'s [=constructor string parser/state=] is "<a for="constructor string parser/state">`protocol`</a>", "<a for="constructor string parser/state">`authority`</a>", "<a for="constructor string parser/state">`username`</a>", "<a for="constructor string parser/state">`password`</a>", "<a for="constructor string parser/state">`hostname`</a>", "<a for="constructor string parser/state">`port`</a>", or "<a for="constructor string parser/state">`pathname`</a>"; |new state| is "<a for="constructor string parser/state">`hash`</a>"; and |parser|'s [=constructor string parser/result=]["{{URLPatternInit/search}}"] does not [=map/exist=], then set |parser|'s [=constructor string parser/result=]["{{URLPatternInit/search}}"] to the empty string.
1. Set |parser|'s [=constructor string parser/state=] to |new state|.
1. Increment |parser|'s [=constructor string parser/token index=] by |skip|.
1. Set |parser|'s [=constructor string parser/component start=] to |parser|'s [=constructor string parser/token index=].
1. Set |parser|'s [=constructor string parser/token increment=] to 0.
</div>
<div algorithm>
To <dfn>rewind</dfn> given a [=constructor string parser=] |parser|:
1. Set |parser|'s [=constructor string parser/token index=] to |parser|'s [=constructor string parser/component start=].
1. Set |parser|'s [=constructor string parser/token increment=] to 0.
</div>
<div algorithm>
To <dfn>rewind and set state</dfn> given a [=constructor string parser=] |parser| and a [=constructor string parser/state=] |state|:
1. Run [=rewind=] given |parser|.
1. Set |parser|'s [=constructor string parser/state=] to |state|.
</div>
<div algorithm>
To <dfn>get a safe token</dfn> given a [=constructor string parser=] |parser| and a number |index|:
1. If |index| is less than |parser|'s [=constructor string parser/token list=]'s [=list/size=], then return |parser|'s [=constructor string parser/token list=][|index|].
1. [=Assert=]: |parser|'s [=constructor string parser/token list=]'s [=list/size=] is greater than or equal to 1.
1. Let |last index| be |parser|'s [=constructor string parser/token list=]'s [=list/size=] − 1.
1. Let |token| be |parser|'s [=constructor string parser/token list=][|last index|].
1. [=Assert=]: |token|'s [=token/type=] is "<a for=token/type>`end`</a>".
1. Return |token|.
</div>
<div algorithm>
To run <dfn>is a non-special pattern char</dfn> given a [=constructor string parser=] |parser|, a number |index|, and a string |value|:
1. Let |token| be the result of running [=get a safe token=] given |parser| and |index|.
1. If |token|'s [=token/value=] is not |value|, then return false.
1. If any of the following are true:
<ul>
<li>|token|'s [=token/type=] is "<a for=token/type>`char`</a>";
<li>|token|'s [=token/type=] is "<a for=token/type>`escaped-char`</a>"; or
<li>|token|'s [=token/type=] is "<a for=token/type>`invalid-char`</a>",
</ul>
<p>then return true.
1. Return false.
</div>
<div algorithm>
To run <dfn>is a protocol suffix</dfn> given a [=constructor string parser=] |parser|:
1. Return the result of running [=is a non-special pattern char=] given |parser|, |parser|'s [=constructor string parser/token index=], and "`:`".
</div>
<div algorithm>
To run <dfn>next is authority slashes</dfn> given a [=constructor string parser=] |parser|:
1. If the result of running [=is a non-special pattern char=] given |parser|, |parser|'s [=constructor string parser/token index=] + 1, and "`/`" is false, then return false.
1. If the result of running [=is a non-special pattern char=] given |parser|, |parser|'s [=constructor string parser/token index=] + 2, and "`/`" is false, then return false.
1. Return true.
</div>
<div algorithm>
To run <dfn>is an identity terminator</dfn> given a [=constructor string parser=] |parser|:
1. Return the result of running [=is a non-special pattern char=] given |parser|, |parser|'s [=constructor string parser/token index=], and "`@`".
</div>
<div algorithm>
To run <dfn>is a password prefix</dfn> given a [=constructor string parser=] |parser|:
1. Return the result of running [=is a non-special pattern char=] given |parser|, |parser|'s [=constructor string parser/token index=], and "`:`".
</div>
<div algorithm>
To run <dfn>is a port prefix</dfn> given a [=constructor string parser=] |parser|:
1. Return the result of running [=is a non-special pattern char=] given |parser|, |parser|'s [=constructor string parser/token index=], and "`:`".
</div>
<div algorithm>
To run <dfn>is a pathname start</dfn> given a [=constructor string parser=] |parser|:
1. Return the result of running [=is a non-special pattern char=] given |parser|, |parser|'s [=constructor string parser/token index=], and "`/`".
</div>
<div algorithm>
To run <dfn>is a search prefix</dfn> given a [=constructor string parser=] |parser|:
1. If result of running [=is a non-special pattern char=] given |parser|, |parser|'s [=constructor string parser/token index=] and "`?`" is true, then return true.
1. If |parser|'s [=constructor string parser/token list=][|parser|'s [=constructor string parser/token index=]]'s [=token/value=] is not "`?`", then return false.
1. Let |previous index| be |parser|'s [=constructor string parser/token index=] − 1.
1. If |previous index| is less than 0, then return true.
1. Let |previous token| be the result of running [=get a safe token=] given |parser| and |previous index|.
1. If any of the following are true, then return false:
<ul>
<li>|previous token|'s [=token/type=] is "<a for=token/type>`name`</a>".</li>
<li>|previous token|'s [=token/type=] is "<a for=token/type>`regexp`</a>".</li>
<li>|previous token|'s [=token/type=] is "<a for=token/type>`close`</a>".</li>
<li>|previous token|'s [=token/type=] is "<a for=token/type>`asterisk`</a>".</li>
</ul>
1. Return true.
</div>
<div algorithm>
To run <dfn>is a hash prefix</dfn> given a [=constructor string parser=] |parser|:
1. Return the result of running [=is a non-special pattern char=] given |parser|, |parser|'s [=constructor string parser/token index=] and "`#`".
</div>
<div algorithm>
To run <dfn>is a group open</dfn> given a [=constructor string parser=] |parser|:
1. If |parser|'s [=constructor string parser/token list=][|parser|'s [=constructor string parser/token index=]]'s [=token/type=] is "<a for=token/type>`open`</a>", then return true.
1. Otherwise return false.
</div>
<div algorithm>
To run <dfn>is a group close</dfn> given a [=constructor string parser=] |parser|:
1. If |parser|'s [=constructor string parser/token list=][|parser|'s [=constructor string parser/token index=]]'s [=token/type=] is "<a for=token/type>`close`</a>", then return true.
1. Otherwise return false.
</div>
<div algorithm>
To run <dfn>is an IPv6 open</dfn> given a [=constructor string parser=] |parser|:
1. Return the result of running [=is a non-special pattern char=] given |parser|, |parser|'s [=constructor string parser/token index=], and "`[`".
</div>
<div algorithm>
To run <dfn>is an IPv6 close</dfn> given a [=constructor string parser=] |parser|:
1. Return the result of running [=is a non-special pattern char=] given |parser|, |parser|'s [=constructor string parser/token index=], and "`]`".
</div>
<div algorithm>
To run <dfn>make a component string</dfn> given a [=constructor string parser=] |parser|:
1. [=Assert=]: |parser|'s [=constructor string parser/token index=] is less than |parser|'s [=constructor string parser/token list=]'s [=list/size=].
1. Let |token| be |parser|'s [=constructor string parser/token list=][|parser|'s [=constructor string parser/token index=]].
1. Let |component start token| be the result of running [=get a safe token=] given |parser| and |parser|'s [=constructor string parser/component start=].
1. Let |component start input index| be |component start token|'s [=token/index=].
1. Let |end index| be |token|'s [=token/index=].
1. Return the [=code point substring by positions|code point substring=] from |component start input index| to |end index| within |parser|'s [=constructor string parser/input=].
</div>
<div algorithm>
To <dfn>compute protocol matches a special scheme flag</dfn> given a [=constructor string parser=] |parser|:
1. Let |protocol string| be the result of running [=make a component string=] given |parser|.
1. Let |protocol component| be the result of [=compiling a component=] given |protocol string|, [=canonicalize a protocol=], and [=default options=].
1. If the result of running [=protocol component matches a special scheme=] given |protocol component| is true, then set |parser|'s [=constructor string parser/protocol matches a special scheme flag=] to true.
</div>
<h2 id=pattern-strings>Pattern strings</h2>
A <dfn>pattern string</dfn> is a string that is written to match a set of target strings. A <dfn for="pattern string">well formed</dfn> pattern string conforms to a particular pattern syntax. This pattern syntax is directly based on the syntax used by the popular [path-to-regexp](https://github.com/pillarjs/path-to-regexp) JavaScript library.
It can be [=parse a pattern string|parsed=] to produce a [=/part list=] which describes, in order, what must appear in a component string for the pattern string to match.
<div class="example" id="example-pattern-strings">
Pattern strings can contain capture groups, which by default match the shortest possible string, up to a component-specific separator (`/` in the pathname, `.` in the hostname). For example, the pathname pattern "`/blog/:title`" will match "`/blog/hello-world`" but not "`/blog/2012/02`".
A regular expression enclosed in parentheses can also be used instead, so the pathname pattern "`/blog/:year(\\d+)/:month(\\d+)`" will match "`/blog/2012/02`".
A group can also be made <span class="allow-2119">optional</span>, or repeated, by using a modifier. For example, the pathname pattern "`/products/:id?"` will match both "`/products`" and "`/products/2`" (but not "`/products/`"). In the pathname specifically, groups automatically require a leading `/`; to avoid this, the group can be explicitly deliminated, as in the pathname pattern "`/products/{:id}?`".
A full wildcard `*` can also be used to match as much as possible, as in the pathname pattern "`/products/*`".
</div>
<h3 id=parsing-pattern-strings>Parsing pattern strings</h3>
<h4 id=tokens>Tokens</h4>
A <dfn>token list</dfn> is a [=list=] containing zero or more [=token=] [=structs=].
A <dfn>token</dfn> is a [=struct=] representing a single lexical token within a [=/pattern string=].
A [=token=] has an associated <dfn for=token>type</dfn>, a string, initially "<a for=token/type>`invalid-char`</a>". It must be one of the following:
<dl>
<dt>"<dfn for=token/type>`open`</dfn>"</dt>
<dd>The [=token=] represents a U+007B (`{`) code point.
<dt>"<dfn for=token/type>`close`</dfn>"</dt>
<dd>The [=token=] represents a U+007D (`}`) code point.
<dt>"<dfn for=token/type>`regexp`</dfn>"</dt>
<dd>The [=token=] represents a string of the form "`(<regular expression>)`". The regular expression is required to consist of only ASCII code points.
<dt>"<dfn for=token/type>`name`</dfn>"</dt>
<dd>The [=token=] represents a string of the form "`:<name>`". The name value is restricted to code points that are consistent with JavaScript identifiers.
<dt>"<dfn for=token/type>`char`</dfn>"</dt>
<dd>The [=token=] represents a valid pattern code point without any special syntactical meaning.
<dt>"<dfn for=token/type>`escaped-char`</dfn>"</dt>
<dd>The [=token=] represents a code point escaped using a backslash like "`\<char>`".
<dt>"<dfn for=token/type>`other-modifier`</dfn>"</dt>
<dd>The [=token=] represents a matching group modifier that is either the U+003F (`?`) or U+002B (`+`) code points.
<dt>"<dfn for=token/type>`asterisk`</dfn>"</dt>