forked from whatwg/urlpattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspec.bs
1739 lines (1463 loc) · 108 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">
Title: URLPattern API
Shortname: urlpattern
Repository: WICG/urlpattern
Inline Github Issues: true
Group: WICG
Status: CG-DRAFT
Level: 1
URL: https://wicg.github.io/urlpattern/
Boilerplate: omit conformance, omit feedback-header
Editor: Ben Kelly, Google https://www.google.com/, [email protected]
Abstract: The URLPattern API provides a web platform primitive for matching URLs based on a convenient pattern syntax.
Status Text: This proposal will hopefully move into the WHATWG URL workstream in the future.
!Participate: <a href="https://github.com/WICG/urlpattern">GitHub WICG/urlpattern</a> (<a href="https://github.com/WICG/urlpattern/issues/new">new issue</a>, <a href="https://github.com/WICG/urlpattern/issues?state=open">open issues</a>)
!Commits: <a href="https://github.com/WICG/urlpattern/commits/main/spec.bs">GitHub spec.bs commits</a>
Complain About: accidental-2119 yes, missing-example-ids yes
Indent: 2
Default Biblio Status: current
Markup Shorthands: markdown yes
</pre>
<pre class="link-defaults">
spec:infra; type:dfn; text:list
spec:url; type:dfn; for:/; text:url
spec:webidl; type:dfn; text:record
</pre>
<pre class="anchors">
spec: ECMASCRIPT; urlPrefix: https://tc39.es/ecma262/
type: abstract-op
text: Get; url: #sec-get-o-p
text: RegExpBuiltinExec; url: #sec-regexpbuiltinexec
text: RegExpCreate; url: #sec-regexpcreate
text: ToString; url: #sec-tostring
type: dfn
text: IdentifierPart; url: #prod-IdentifierPart
text: IdentifierStart; url: #prod-IdentifierStart
type: interface
text: RegExp; url: #sec-regexp-regular-expression-objects
spec: url; urlPrefix: https://url.spec.whatwg.org/
type: dfn
text: cannot be a base URL flag; url: #url-cannot-be-a-base-url-flag
text: cannot be a base URL path state; url: #cannot-be-a-base-url-path-state
text: default port; url: #default-port
text: fragment state; url: #fragment-state
text: hostname state; url: #hostname-state
text: path start state; url: #path-start-state
text: port state; url: #port-state
text: query state; url: #query-state
text: special scheme; url: #special-scheme
text: scheme start state; url: #scheme-start-state
</pre>
<style>
.selected-text-file-an-issue {
position: fixed;
bottom: 0;
right: 0;
background: rgba(255, 255, 255, 0.8);
font-size: smaller;
padding: 4px 10px;
z-index: 4;
}
dfn var {
font-style: italic;
}
table {
margin: 1em 0;
}
/* WHATWG-style <hr>s, instead of WICG-style. Specific selector is necessary to override WICG styles. */
:not(.head) > :not(.head) + hr {
display: block;
background: none;
border: none;
padding: 0;
margin: 3em 0;
height: auto;
}
:not(.head) > :not(.head) + hr::before {
content: none;
}
/* domintro from https://resources.whatwg.org/standard.css */
.domintro {
position: relative;
color: green;
background: #DDFFDD;
margin: 2.5em 0 2em 0;
padding: 1.5em 1em 0.5em 2em;
}
.domintro dt, .domintro dt * {
color: black;
font-size: inherit;
}
.domintro dd {
margin: 0.5em 0 1em 2em; padding: 0;
}
.domintro dd p {
margin: 0.5em 0;
}
.domintro::before {
content: 'For web developers (non-normative)';
background: green;
color: white;
padding: 0.15em 0.25em;
font-style: normal;
position: absolute;
top: -0.8em;
left: -0.8em;
}
</style>
<script src="https://resources.whatwg.org/file-issue.js" async></script>
<h2 id=urlpattern-class>The {{URLPattern}} class </h2>
<xmp class="idl">
typedef (USVString or URLPatternInit) URLPatternInput;
[Exposed=(Window,Worker)]
interface URLPattern {
constructor(URLPatternInput input, optional USVString baseURL);
boolean test(URLPatternInput input, optional USVString baseURL);
URLPatternResult? exec(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;
};
dictionary URLPatternInit {
USVString protocol;
USVString username;
USVString password;
USVString hostname;
USVString port;
USVString pathname;
USVString search;
USVString hash;
USVString baseURL;
};
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> groups;
};
</xmp>
Each {{URLPattern}} object has an associated <dfn for=URLPattern>protocol component</dfn>, a [=component=], which must be set upon creation.
Each {{URLPattern}} object has an associated <dfn for=URLPattern>username component</dfn>, a [=component=], which must be set upon creation.
Each {{URLPattern}} object has an associated <dfn for=URLPattern>password component</dfn>, a [=component=], which must be set upon creation.
Each {{URLPattern}} object has an associated <dfn for=URLPattern>hostname component</dfn>, a [=component=], which must be set upon creation.
Each {{URLPattern}} object has an associated <dfn for=URLPattern>port component</dfn>, a [=component=], which must be set upon creation.
Each {{URLPattern}} object has an associated <dfn for=URLPattern>pathname component</dfn>, a [=component=], which must be set upon creation.
Each {{URLPattern}} object has an associated <dfn for=URLPattern>search component</dfn>, a [=component=], which must be set upon creation.
Each {{URLPattern}} object has an associated <dfn for=URLPattern>hash component</dfn>, a [=component=], which must be set upon creation.
<dl class="domintro non-normative">
<dt><code>|urlPattern| = new {{URLPattern/constructor(input, baseURL)|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)|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>|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>
</dl>
<div algorithm>
The <dfn constructor for=URLPattern lt="URLPattern(input, baseURL)">new URLPattern(|input|, |baseURL|)</dfn> constructor steps are:
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. Set |init|["{{URLPatternInit/baseURL}}"] to |baseURL|.
1. Else:
1. [=Assert=]: |input| is a {{URLPatternInit}}.
1. If |baseURL| is given, 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. If |processedInit|["{{URLPatternInit/protocol}}"] is a [=special scheme=] and |processedInit|["{{URLPatternInit/port}}"] is its corresponding [=default port=], then set |processedInit|["{{URLPatternInit/port}}"] to the empty string.
1. Set [=this=]'s [=URLPattern/protocol component=] to the result of [=compiling a component=] given |processedInit|["{{URLPatternInit/protocol}}"], [=canonicalize a protocol=], and [=default options=].
1. Set [=this=]'s [=URLPattern/username component=] to the result of [=compiling a component=] given |processedInit|["{{URLPatternInit/username}}"], [=canonicalize a username=], and [=default options=].
1. Set [=this=]'s [=URLPattern/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 [=this=]'s [=URLPattern/hostname component=] to the result of [=compiling a component=] given |processedInit|["{{URLPatternInit/hostname}}"], [=canonicalize an IPv6 hostname=], and [=hostname options=].
1. Else, set [=this=]'s [=URLPattern/hostname component=] to the result of [=compiling a component=] given |processedInit|["{{URLPatternInit/hostname}}"], [=canonicalize a hostname=], and [=hostname options=].
1. Set [=this=]'s [=URLPattern/port component=] to the result of [=compiling a component=] given |processedInit|["{{URLPatternInit/port}}"], [=canonicalize a port=], and [=default options=].
1. If the result of running [=protocol component matches a special scheme=] given [=this=]'s [=URLPattern/protocol component=] is true, then set [=this=]'s [=URLPattern/pathname component=] to the result of [=compiling a component=] given |processedInit|["{{URLPatternInit/pathname}}"], [=canonicalize a pathname=], and [=pathname options=].
1. Else set [=this=]'s [=URLPattern/pathname component=] to the result of [=compiling a component=] given |processedInit|["{{URLPatternInit/pathname}}"], [=canonicalize a cannot-be-a-base-URL pathname=], and [=default options=]
1. Set [=this=]'s [=URLPattern/search component=] to the result of [=compiling a component=] given |processedInit|["{{URLPatternInit/search}}"], [=canonicalize a search=], and [=default options=].
1. Set [=this=]'s [=URLPattern/hash component=] to the result of [=compiling a component=] given |processedInit|["{{URLPatternInit/hash}}"], [=canonicalize a hash=], and [=default options=].
</div>
<div algorithm>
The <dfn attribute for="URLPattern">protocol</dfn> getter steps are:
1. Return [=this=]'s [=URLPattern/protocol component=]'s [=component/pattern string=].
</div>
<div algorithm>
The <dfn attribute for="URLPattern">username</dfn> getter steps are:
1. Return [=this=]'s [=URLPattern/username component=]'s [=component/pattern string=].
</div>
<div algorithm>
The <dfn attribute for="URLPattern">password</dfn> getter steps are:
1. Return [=this=]'s [=URLPattern/password component=]'s [=component/pattern string=].
</div>
<div algorithm>
The <dfn attribute for="URLPattern">hostname</dfn> getter steps are:
1. Return [=this=]'s [=URLPattern/hostname component=]'s [=component/pattern string=].
</div>
<div algorithm>
The <dfn attribute for="URLPattern">port</dfn> getter steps are:
1. Return [=this=]'s [=URLPattern/port component=]'s [=component/pattern string=].
</div>
<div algorithm>
The <dfn attribute for="URLPattern">pathname</dfn> getter steps are:
1. Return [=this=]'s [=URLPattern/pathname component=]'s [=component/pattern string=].
</div>
<div algorithm>
The <dfn attribute for="URLPattern">search</dfn> getter steps are:
1. Return [=this=]'s [=URLPattern/search component=]'s [=component/pattern string=].
</div>
<div algorithm>
The <dfn attribute for="URLPattern">hash</dfn> getter steps are:
1. Return [=this=]'s [=URLPattern/hash component=]'s [=component/pattern string=].
</div>
<div algorithm>
The <dfn method for="URLPattern">test(|input|, |baseURL|)</dfn> method steps are:
1. Let |result| be the result of [=match=] given [=this=], |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 [=match=] given [=this=], |input|, and |baseURL| if given.
</div>
<h3 id=urlpattern-internals>Internals</h3>
A {{URLPattern}} is associated with multiple <dfn>component</dfn> [=structs=].
A [=component=] has an associated <dfn for=component>pattern string</dfn>, a [=pattern string/well formed=] [=/pattern string=], which must be set upon creation.
A [=component=] has an associated <dfn for=component>regular expression</dfn>, a {{RegExp}}, which must be set upon creation.
A [=component=] has an associated <dfn for=component>group name list</dfn>, a [=list=] of strings, which must be set upon creation.
<div algorithm>
To <dfn>compile a component</dfn> given a string |input|, [=/encoding callback=] |encoding callback|, and [=/options=] |options|:
1. If |input| is null, then set |input| to "`*`".
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 |regular expression| be [$RegExpCreate$](|regular expression string|, "`u`"). If this throws an exception, catch it, and throw a {{TypeError}}.
<p class="note allow-2119">The specification uses regular expressions to perform all matching, but this is not required. 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 should 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. Return a new [=component=] whose [=component/pattern string=] is |pattern string|, [=component/regular expression=] is |regular expression|, and [=component/group name list=] is |name list|.
</div>
<div algorithm>
To perform a <dfn>match</dfn> given a {{URLPattern}} |urlpattern|, a {{URLPatternInput}} |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. Else:
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=] |baseURL| to |inputs|.
1. Let |url| be the result of [=URL parser|parsing=] |input| given |baseURL|.
1. If |url| is failure, return null.
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=] or the empty string if the value is null.
1. Set |port| to |url|'s [=url/port=] or the empty string if the value is null.
1. Set |pathname| to |url|'s [=url/API pathname string=].
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 [=URLPattern/protocol component=]'s [=component/regular expression=], |protocol|).
1. Let |usernameExecResult| be [$RegExpBuiltinExec$](|urlpattern|'s [=URLPattern/username component=]'s [=component/regular expression=], |username|).
1. Let |passwordExecResult| be [$RegExpBuiltinExec$](|urlpattern|'s [=URLPattern/password component=]'s [=component/regular expression=], |password|).
1. Let |hostnameExecResult| be [$RegExpBuiltinExec$](|urlpattern|'s [=URLPattern/hostname component=]'s [=component/regular expression=], |hostname|).
1. Let |portExecResult| be [$RegExpBuiltinExec$](|urlpattern|'s [=URLPattern/port component=]'s [=component/regular expression=], |port|).
1. Let |pathnameExecResult| be [$RegExpBuiltinExec$](|urlpattern|'s [=URLPattern/pathname component=]'s [=component/regular expression=], |pathname|).
1. Let |searchExecResult| be [$RegExpBuiltinExec$](|urlpattern|'s [=URLPattern/search component=]'s [=component/regular expression=], |search|).
1. Let |hashExecResult| be [$RegExpBuiltinExec$](|urlpattern|'s [=URLPattern/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 [=URLPattern/protocol component=], |protocol|, and |protocolExecResult|.
1. Set |result|["{{URLPatternResult/username}}"] to the result of [=creating a component match result=] given |urlpattern|'s [=URLPattern/username component=], |username|, and |usernameExecResult|.
1. Set |result|["{{URLPatternResult/password}}"] to the result of [=creating a component match result=] given |urlpattern|'s [=URLPattern/password component=], |password|, and |passwordExecResult|.
1. Set |result|["{{URLPatternResult/hostname}}"] to the result of [=creating a component match result=] given |urlpattern|'s [=URLPattern/hostname component=], |hostname|, and |hostnameExecResult|.
1. Set |result|["{{URLPatternResult/port}}"] to the result of [=creating a component match result=] given |urlpattern|'s [=URLPattern/port component=], |port|, and |portExecResult|.
1. Set |result|["{{URLPatternResult/pathname}}"] to the result of [=creating a component match result=] given |urlpattern|'s [=URLPattern/pathname component=], |pathname|, and |pathnameExecResult|.
1. Set |result|["{{URLPatternResult/search}}"] to the result of [=creating a component match result=] given |urlpattern|'s [=URLPattern/search component=], |search|, and |searchExecResult|.
1. Set |result|["{{URLPatternResult/hash}}"] to the result of [=creating a component match result=] given |urlpattern|'s [=URLPattern/hash component=], |hash|, and |hashExecResult|.
1. Return |result|.
</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}}></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. 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 class=allow-2119>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 may 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 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>".
<div class=note>
<p>When constructing a pattern using a {{URLPatternInit}} like `new URLPattern({ pathname: 'foo' })` any missing components will be defaulted to wildcards. In the constructor string case, however, all components are precisely defined as either empty string or a longer value. This is due to there being no way to simply "leave out" a component when writing a URL.
<p>To implement this we initialize components in |parser|'s [=constructor string parser/result=] with empty string in advance.
<p>We can't, however, do this immediately. We want to allow the `baseURL` to provide information for relative URLs, so we only want to set the default empty string values for components following the first component in the relative URL. We therefore wait to set the default component values until after we exit the "<a for="constructor string parser/state">`init`</a>" [=constructor string parser/state=].
</div>
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 allow-2119">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>":
<div class=note>
<p class=allow-2119>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 must be a relative URLPattern constructor string.
</div>
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. Else 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. Set |parser|'s [=constructor string parser/result=]["{{URLPattern/hash}}"] to the empty string.
1. Else:
1. Run [=change state=] given |parser|, "<a for="constructor string parser/state">`pathname`</a>" and 0.
1. Set |parser|'s [=constructor string parser/result=]["{{URLPattern/search}}"] to the empty string.
1. Set |parser|'s [=constructor string parser/result=]["{{URLPattern/hash}}"] to the empty string.
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. Else:
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:
<p class="note allow-2119">We found a protocol suffix, so this must be an absolute URLPattern constructor string. Therefore initialize all component to the empty string.
1. Set |parser|'s [=constructor string parser/result=]["{{URLPatternInit/username}}"] to the empty string.
1. Set |parser|'s [=constructor string parser/result=]["{{URLPatternInit/password}}"] to the empty string.
1. Set |parser|'s [=constructor string parser/result=]["{{URLPatternInit/hostname}}"] to the empty string.
1. Set |parser|'s [=constructor string parser/result=]["{{URLPatternInit/port}}"] to the empty string.
1. Set |parser|'s [=constructor string parser/result=]["{{URLPatternInit/pathname}}"] to the empty string.
1. Set |parser|'s [=constructor string parser/result=]["{{URLPatternInit/search}}"] to the empty string.
1. Set |parser|'s [=constructor string parser/result=]["{{URLPatternInit/hash}}"] to the empty string.
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 a "cannot be a base URL" and go straight to the pathname component.
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. 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. Else 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. Else 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. Else 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 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. Else if the 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. Else 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. Else 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. Else 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. Else 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. Else 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. Else 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. Else 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. 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=] |state|, and a number |skip|:
1. If |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=][|state|] to the result of running [=make a component string=] given |parser|.
1. Set |parser|'s [=constructor string parser/state=] to |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. Else 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. Else 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. Return the [=code point substring by indices|code point substring=] from indices |component start input index| to |token|'s [=token/index=] inclusive 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=patterns>Patterns</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.
<h3 id=parsing-patterns>Parsing Patterns</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>
<dd>The [=token=] represents a U+002A (`*`) code point that can be either a wildcard matching group or a matching group modifier.
<dt>"<dfn for=token/type>`end`</dfn>"</dt>
<dd>The [=token=] represents the end of the [=/pattern string=].
<dt>"<dfn for=token/type>`invalid-char`</dfn>"</dt>
<dd>The [=token=] represents a code point that is invalid in the pattern. This could be because of the code point value itself or due to its location within the pattern relative to other syntactic elements.
</dl>
A [=token=] has an associated <dfn for=token>index</dfn>, a number, initially 0. It is the position of the first code point in the [=/pattern string=] represented by the [=token=].
A [=token=] has an associated <dfn for=token>value</dfn>, a string, initially the empty string. It contains the code points from the [=/pattern string=] represented by the [=token=].
<h4 id=tokenizing>Tokenizing</h4>
A <dfn>tokenize policy</dfn> is a string that must be either "<dfn for="tokenize policy">`strict`</dfn>" or "<dfn for="tokenize policy">`lenient`</dfn>".
A <dfn>tokenizer</dfn> is a [=struct=].
A [=tokenizer=] has an associated <dfn for=tokenizer>input</dfn>, a [=/pattern string=], initially the empty string.
A [=tokenizer=] has an associated <dfn for=tokenizer>policy</dfn>, a [=tokenize policy=], initially "<a for="tokenize policy">`strict`</a>".
A [=tokenizer=] has an associated <dfn for=tokenizer>token list</dfn>, a [=/token list=], initially an empty [=list=].
A [=tokenizer=] has an associated <dfn for=tokenizer>index</dfn>, a number, initially 0.
A [=tokenizer=] has an associated <dfn for=tokenizer>next index</dfn>, a number, initially 0.
A [=tokenizer=] has an associated <dfn for=tokenizer>code point</dfn>, a Unicode code point, initially null.
<div algorithm>
To <dfn>tokenize</dfn> a given string |input| and [=tokenize policy=] |policy|:
1. Let |tokenizer| be a new [=tokenizer=].
1. Set |tokenizer|'s [=tokenizer/input=] to |input|.
1. Set |tokenizer|'s [=tokenizer/policy=] to |policy|.
1. While |tokenizer|'s [=tokenizer/index=] is less than |tokenizer|'s [=tokenizer/input=]'s [=string/code point length=]:
1. Run [=get the next code point=] given |tokenizer|.
1. If |tokenizer|'s [=tokenizer/code point=] is U+002A (`*`):
1. Run [=add a token with default position and length=] given |tokenizer| and "<a for=token/type>`asterisk`</a>".
1. [=Continue=].
1. If |tokenizer|'s [=tokenizer/code point=] is U+002B (`+`) or U+003F (`?`):
1. Run [=add a token with default position and length=] given |tokenizer| and "<a for=token/type>`other-modifier`</a>".
1. [=Continue=].
1. If |tokenizer|'s [=tokenizer/code point=] is U+005C (<code>\</code>):
1. If |tokenizer|'s [=tokenizer/index=] is equal to |tokenizer|'s [=tokenizer/input=]'s [=string/code point length=] − 1:
1. Run [=process a tokenizing error=] given |tokenizer|, |tokenizer|'s [=tokenizer/next index=], and |tokenizer|'s [=tokenizer/index=].
1. [=Continue=].
1. Let |escaped index| be |tokenizer|'s [=tokenizer/next index=].
1. Run [=get the next code point=] given |tokenizer|.
1. Run [=add a token with default length=] given |tokenizer|, "<a for=token/type>`escaped-char`</a>", |tokenizer|'s [=tokenizer/next index=], and |escaped index|.
1. [=Continue=].
1. If |tokenizer|'s [=tokenizer/code point=] is U+007B (`{`):
1. Run [=add a token with default position and length=] given |tokenizer| and "<a for=token/type>`open`</a>".
1. [=Continue=].
1. If |tokenizer|'s [=tokenizer/code point=] is U+007D (`}`):
1. Run [=add a token with default position and length=] given |tokenizer| and "<a for=token/type>`close`</a>".
1. [=Continue=].
1. If |tokenizer|'s [=tokenizer/code point=] is U+003A (`:`):
1. Let |name position| be |tokenizer|'s [=tokenizer/next index=].
1. Let |name start| be |name position|.
1. While |name position| is less than |tokenizer|'s [=tokenizer/input=]'s [=string/code point length=]:
1. Run [=seek and get the next code point=] given |tokenizer| and |name position|.
1. Let |first code point| be true if |name position| equals |name start| and false otherwise.
1. Let |valid code point| be the result of running [=is a valid name code point=] given |tokenizer|'s [=tokenizer/code point=] and |first code point|.
1. If |valid code point| is false [=break=].
1. Set |name position| to |tokenizer|'s [=tokenizer/next index=].
1. If |name position| is less than or equal to |name start|:
1. Run [=process a tokenizing error=] given |tokenizer|, |name start|, and |tokenizer|'s [=tokenizer/index=].
1. [=Continue=].
1. Run [=add a token with default length=] given |tokenizer|, "<a for=token/type>`name`</a>", |name position|, and |name start|.
1. If |tokenizer|'s [=tokenizer/code point=] is U+0028 (`(`):
1. Let |depth| be 1.
1. Let |regexp position| be |tokenizer|'s [=tokenizer/next index=].
1. Let |regexp start| be |regexp position|.
1. Let |error| be false.
1. While |regexp position| is less than |tokenizer|'s [=tokenizer/input=]'s [=string/code point length=]:
1. Run [=seek and get the next code point=] given |tokenizer| and |regexp position|.
1. If the result of running [=is ASCII=] given |tokenizer|'s [=tokenizer/code point=] is false:
1. Run [=process a tokenizing error=] given |tokenizer|, |regexp start|, and |tokenizer|'s [=tokenizer/index=].
1. Set |error| to true.
1. [=Break=].
1. If |regexp position| equals |regexp start| and |tokenizer|'s [=tokenizer/code point=] is U+003F (`?`):
1. Run [=process a tokenizing error=] given |tokenizer|, |regexp start|, and |tokenizer|'s [=tokenizer/index=].
1. Set |error| to true.
1. [=Break=].
1. If |tokenizer|'s [=tokenizer/code point=] is U+005C (<code>\</code>):
1. If |regexp position| equals |tokenizer|'s [=tokenizer/input=]'s [=string/code point length=] − 1:
1. Run [=process a tokenizing error=] given |tokenizer|, |regexp start|, and |tokenizer|'s [=tokenizer/index=].
1. Set |error| to true.
1. [=Break=]
1. Run [=get the next code point=] given |tokenizer|.
1. If the result of running [=is ASCII=] given |tokenizer|'s [=tokenizer/code point=] is false:
1. Run [=process a tokenizing error=] given |tokenizer|, |regexp start|, and |tokenizer|'s [=tokenizer/index=].
1. Set |error| to true.
1. [=Break=].
1. Set |regexp position| to |tokenizer|'s [=tokenizer/next index=].
1. [=Continue=].
1. If |tokenizer|'s [=tokenizer/code point=] is U+0029 (`)`):
1. Decrement |depth| by 1.
1. If |depth| is 0:
1. Set |regexp position| to |tokenizer|'s [=tokenizer/next index=].
1. [=Break=].
1. Else if |tokenizer|'s [=tokenizer/code point=] is U+0028 (`(`):
1. Increment |depth| by 1.
1. If |regexp position| equals |tokenizer|'s [=tokenizer/input=]'s [=string/code point length=] − 1:
1. Run [=process a tokenizing error=] given |tokenizer|, |regexp start|, and |tokenizer|'s [=tokenizer/index=].
1. Set |error| to true.
1. [=Break=]
1. Let |temporary position| be |tokenizer|'s [=tokenizer/next index=].
1. Run [=get the next code point=] given |tokenizer|.
1. If |tokenizer|'s [=tokenizer/code point=] is not U+003F (`?`):
1. Run [=process a tokenizing error=] given |tokenizer|, |regexp start|, and |tokenizer|'s [=tokenizer/index=].
1. Set |error| to true.
1. [=Break=].
1. Set |tokenizer|'s [=tokenizer/next index=] to |temporary position|.
1. Set |regexp position| to |tokenizer|'s [=tokenizer/next index=].
1. If |error| is true [=continue=].
1. If |depth| is not zero:
1. Run [=process a tokenizing error=] given |tokenizer|, |regexp start|, and |tokenizer|'s [=tokenizer/index=].
1. [=Continue=].
1. Let |regexp length| be |regexp position| − |regexp start| − 1.
1. If |regexp length| is zero:
1. Run [=process a tokenizing error=] given |tokenizer|, |regexp start|, and |tokenizer|'s [=tokenizer/index=].
1. [=Continue=].
1. Run [=add a token=] given |tokenizer|, "<a for=token/type>`regexp`</a>", |regexp position|, |regexp start|, and |regexp length|.
1. Run [=add a token with default position and length=] given |tokenizer| and "<a for=token/type>`char`</a>".
1. Run [=add a token with default length=] given |tokenizer|, "<a for=token/type>`end`</a>", |tokenizer|'s [=tokenizer/index=], and |tokenizer|'s [=tokenizer/index=].
1. Return |tokenizer|'s [=tokenizer/token list=].
</div>
<div algorithm>
To <dfn>get the next code point</dfn> for a given [=tokenizer=] |tokenizer|:
1. Set |tokenizer|'s [=tokenizer/code point=] to the Unicode code point in |tokenizer|'s [=tokenizer/input=] at the position indicated by |tokenizer|'s [=tokenizer/next index=].
1. Increment |tokenizer|'s [=tokenizer/next index=] by 1.
</div>
<div algorithm>
To <dfn>seek and get the next code point</dfn> for a given [=tokenizer=] |tokenizer| and number |index|:
1. Set |tokenizer|'s [=tokenizer/next index=] to |index|.
1. Run [=get the next code point=] given |tokenizer|.
</div>
<div algorithm>
To <dfn>add a token</dfn> for a given [=tokenizer=] |tokenizer|, [=token/type=] |type|, number |next position|, number |value position|, and number |value length|:
1. Let |token| be a new [=token=].
1. Set |token|'s [=token/type=] to |type|.
1. Set |token|'s [=token/index=] to |tokenizer|'s [=tokenizer/index=].
1. Set |token|'s [=token/value=] to the [=code point substring=] from index |value position| with length |value length| within |tokenizer|'s [=tokenizer/input=].
1. [=list/Append=] |token| to the back of |tokenizer|'s [=tokenizer/token list=].
1. Set |tokenizer|'s [=token/index=] to |next position|.
</div>
<div algorthm>
To <dfn>add a token with default length</dfn> for a given [=tokenizer=] |tokenizer|, [=token/type=] |type|, number |next position|, and number |value position|: