/
url.html
2488 lines (1932 loc) · 116 KB
/
url.html
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
<!DOCTYPE html><html lang="en-US"><meta charset="utf-8">
<title>URL Standard</title>
<link href="http://www.whatwg.org/style/specification" rel="stylesheet">
<link href="//resources.whatwg.org/logo-url.svg" rel="icon">
<div class="head">
<p><a class="logo" href="//www.whatwg.org/"><img alt="WHATWG" height="100" src="//resources.whatwg.org/logo-url.svg" width="100"></a></p>
<h1>URL</h1>
<h2 class="no-num no-toc" id="living-standard-—-last-updated-24-april-2013">Living Standard — Last Updated 24 April 2013</h2>
<dl>
<dt>This Version:
<dd><a href="//url.spec.whatwg.org/">http://url.spec.whatwg.org/</a>
<dt>Participate:</dt>
<dd>Send feedback to
<a href="http://www.whatwg.org/mailing-list">whatwg@whatwg.org</a>
(<a href="http://www.whatwg.org/mailing-list#specs">archives</a>) or
<a href="https://www.w3.org/Bugs/Public/enter_bug.cgi?product=WHATWG&component=URL">file a bug</a>
(<a href="https://www.w3.org/Bugs/Public/buglist.cgi?product=WHATWG&component=URL&resolution=---">open bugs</a>)
<dd><a href="http://wiki.whatwg.org/wiki/IRC">IRC: #whatwg on Freenode</a>
<dt>Version History:
<dd><a href="https://github.com/whatwg/url/commits">https://github.com/whatwg/url/commits</a>
<dd><a href="https://twitter.com/urlstandard">@urlstandard</a>
<dt>Editor:
<dd class="h-card vcard"><a class="u-url url p-name fn" href="http://annevankesteren.nl/" lang="nl">Anne van Kesteren</a>
<<a class="u-email email" href="mailto:annevk@annevk.nl">annevk@annevk.nl</a>>
</dl>
<script async="" src="//resources.whatwg.org/file-bug.js"></script>
<p class="copyright"><a href="http://creativecommons.org/publicdomain/zero/1.0/" rel="license"><img alt="CC0" src="http://i.creativecommons.org/p/zero/1.0/80x15.png"></a>
To the extent possible under law, the editors have waived all copyright and
related or neighboring rights to this work. In addition, as of
24 April 2013, the editors have made this specification available
under the
<a href="http://www.openwebfoundation.org/legal/the-owf-1-0-agreements/owfa-1-0" rel="license">Open Web Foundation Agreement Version 1.0</a>,
which is available at
http://www.openwebfoundation.org/legal/the-owf-1-0-agreements/owfa-1-0.
</div>
<h2 class="no-num no-toc" id="table-of-contents">Table of Contents</h2>
<!--begin-toc-->
<ol class="toc">
<li><a class="no-num" href="#goals">Goals</a></li>
<li><a href="#conformance"><span class="secno">1 </span>Conformance</a></li>
<li><a href="#terminology"><span class="secno">2 </span>Terminology</a>
<ol>
<li><a href="#parsers"><span class="secno">2.1 </span>Parsers</a></ol></li>
<li><a href="#percent-encoded-bytes"><span class="secno">3 </span>Percent-encoded bytes</a></li>
<li><a href="#hosts-and-ip-addresses"><span class="secno">4 </span>Hosts and IP addresses</a>
<ol>
<li><a href="#host-writing"><span class="secno">4.1 </span>Writing</a></li>
<li><a href="#host-parsing"><span class="secno">4.2 </span>Parsing</a></li>
<li><a href="#host-serializing"><span class="secno">4.3 </span>Serializing</a></ol></li>
<li><a href="#urls"><span class="secno">5 </span>URLs</a>
<ol>
<li><a href="#writing"><span class="secno">5.1 </span>Writing</a></li>
<li><a href="#parsing"><span class="secno">5.2 </span>Parsing</a></li>
<li><a href="#serializing"><span class="secno">5.3 </span>Serializing</a></ol></li>
<li><a href="#application/x-www-form-urlencoded"><span class="secno">6 </span><code>application/x-www-form-urlencoded</code></a></li>
<li><a href="#api"><span class="secno">7 </span>API</a>
<ol>
<li><a href="#constructors"><span class="secno">7.1 </span>Constructors</a></li>
<li><a href="#urlutils-and-urlutilsreadonly-members"><span class="secno">7.2 </span><code>URLUtils</code> and <code>URLUtilsReadOnly</code> members</a></li>
<li><a href="#interface-urlquery"><span class="secno">7.3 </span>Interface <code>URLQuery</code></a></ol></li>
<li><a class="no-num" href="#references">References</a></li>
<li><a class="no-num" href="#acknowledgments">Acknowledgments</a></ol>
<!--end-toc-->
<h2 class="no-num" id="goals">Goals</h2>
<p>The URL standard sets out to make URLs fully predictable and
interoperable. This is the plan:
<ul>
<li><p>Align RFC 3986 and RFC 3987 with contemporary implementations and
obsolete them in the process. (E.g. spaces, other "illegal" code points,
query encoding, equality, canonicalization, are all concepts not entirely
shared, or defined.) URL parsing needs to become as solid as HTML parsing.
<a class="informative" href="#refsURI">[URI]</a>
<a class="informative" href="#refsIRI">[IRI]</a>
<li><p>Standardize on the term URL. URI and IRI are just confusing. In
practice a single algorithm is used for both so keeping them distinct is
not helping anyone. URL also easily wins the
<a href="http://www.googlefight.com/index.php?word1=url&word2=uri">search result popularity contest</a>.
<li><p>Define URL's existing JavaScript API in full detail and add
enhancements to make it easier to work with. Add a new <a href="#url"><code>URL</code></a>
object as well for URL manipulation without usage of HTML elements. (Useful
for Web Workers.)
</ul>
<p class="note">As the editor learns more about the subject matter the goals
might increase in scope somewhat.
<h2 id="conformance"><span class="secno">1 </span>Conformance</h2>
<p>All diagrams, examples, and notes in this specification are
non-normative, as are all sections explicitly marked non-normative.
Everything else in this specification is normative.
<p>The key words "MUST", "MUST NOT", "REQUIRED", <!--"SHALL", "SHALL
NOT",--> "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and
"OPTIONAL" in the normative parts of this specification are to be
interpreted as described in RFC2119. For readability, these words do
not appear in all uppercase letters in this specification.
<a href="#refsRFC2119">[RFC2119]</a>
<h2 id="terminology"><span class="secno">2 </span>Terminology</h2>
<p>Some terms used in this specification are defined in the
Encoding Standard. <a href="#refsENCODING">[ENCODING]</a>
<p>The <dfn id="ascii-digits">ASCII digits</dfn> are code points in the range U+0030 to U+0039.
<p>The <dfn id="ascii-hex-digits">ASCII hex digits</dfn> are <a href="#ascii-digits">ASCII digits</a> or are
code points in the range U+0041 to U+0046 or in the range U+0061 to U+0066.
<p>The <dfn id="ascii-alpha">ASCII alpha</dfn> are code points in the range U+0041 to U+005A
or in the range U+0061 to U+007A.
<p>The <dfn id="ascii-alphanumeric">ASCII alphanumeric</dfn> are <a href="#ascii-digits">ASCII digits</a> or
<a href="#ascii-alpha">ASCII alpha</a>.
<p>The <dfn id="domain-label-separators">domain label separators</dfn> are the code points
U+002E, U+3002, U+FF0E, and U+FF61.
<!-- IDNA2003/UTS #46; IDNA2008 does not define these -->
<h3 id="parsers"><span class="secno">2.1 </span>Parsers</h3>
<p>The <dfn id="eof-code-point">EOF code point</dfn> signifies the end of a string or
code point stream.
<p>A <dfn id="parse-error">parse error</dfn> indicates a non-fatal mismatch between input and requirements.
User agents are encouraged to expose <a href="#parse-error" title="parse error">parse errors</a>
somehow.
<p>Within a parser algorithm that uses a <var title="">pointer</var> variable, <dfn id="c">c</dfn>
references the code point the <var title="">pointer</var> variable points to.
<p>Within a string-based parser algorithm that uses a <var title="">pointer</var> variable,
<dfn id="remaining">remaining</dfn> references the substring after <var title="">pointer</var> in the string
being processed.
<p class="example">If "<code title="">mailto:example@example</code>" is a string being
processed and <var title="">pointer</var> points to "<code title="">@</code>",
<a href="#c">c</a> is "<code title="">@</code>" and <a href="#remaining">remaining</a> is
"<code title="">example</code>".
<h2 id="percent-encoded-bytes"><span class="secno">3 </span>Percent-encoded bytes</h2>
<p>A <dfn id="percent-encoded-byte">percent-encoded byte</dfn> is "<code title="">%</code>", followed by
two <a href="#ascii-hex-digits">ASCII hex digits</a>. Sequences of
<a href="#percent-encoded-byte" title="percent-encoded byte">percent-encoded bytes</a>, after
conversion to bytes, should not cause
<a class="external" data-anolis-spec="encoding" href="http://encoding.spec.whatwg.org/#utf-8-decode">utf-8 decode</a> to emit any
<a class="external" data-anolis-spec="encoding" href="http://encoding.spec.whatwg.org/#decoder-error" title="decoder error">decoder errors</a>.
<p>To <dfn id="percent-encode">percent encode</dfn> a <var title="">byte</var> into a
<a href="#percent-encoded-byte">percent-encoded byte</a>, return a string consisting of
"<code title="">%</code>", followed by a double-digit, uppercase, hexadecimal
representation of <var title="">byte</var>.
<p>To <dfn id="percent-decode">percent decode</dfn> a <var title="">string</var> using code points in the range
U+0000 to U+007F into a byte sequence, run these steps:
<ol>
<li><p>Let <var title="">pointer</var> be a pointer into
<var title="">string</var>, initially zero (pointing to the first code point).
<li><p>Let <var title="">bytes</var> be an empty byte sequence.
<li>
<p>While <a href="#c">c</a> is not the <a href="#eof-code-point">EOF code point</a>, run
these substeps:
<ol>
<li><p>While <a href="#c">c</a> is not "<code title="">%</code>" or the
<a href="#eof-code-point">EOF code point</a>, append to <var title="">bytes</var> a byte
whose value is <a href="#c">c</a>'s code point and increase
<var title="">pointer</var> by one.
<li><p>If <a href="#c">c</a> is "<code title="">%</code>" and
<a href="#remaining">remaining</a> does not start with two
<a href="#ascii-hex-digits">ASCII hex digits</a>, append to <var title="">bytes</var> a byte
whose value is <a href="#c">c</a>'s code point, increase
<var title="">pointer</var> by one.
<li><p>Otherwise, while <a href="#c">c</a> is "<code title="">%</code>" and
<a href="#remaining">remaining</a> starts with two <a href="#ascii-hex-digits">ASCII hex digits</a>,
append to <var title="">bytes</var> a byte whose value is
<a href="#remaining">remaining</a>'s two leading code points, interpreted as
hexadecimal number, and increase <var title="">pointer</var> by three.
</ol>
<li>
<p>Return <var title="">bytes</var>.
</ol>
<!-- the escape sets are minimal as escaping can lead to problems; we might
be able to escape more here but only if implementors are willing and
there's an upside
note that query and application/x-www-form-urlencoded use their own
local sets -->
<p>The <dfn id="simple-encode-set">simple encode set</dfn> are all code points less than
U+0020 (i.e. excluding U+0020) and all code points greater than U+007E.
<p>The <dfn id="default-encode-set">default encode set</dfn> is the
<a href="#simple-encode-set">simple encode set</a> and code points U+0020,
'<code title="">"</code>', <!-- 0x22 -->
"<code title="">#</code>", <!-- 0x23 -->
"<code title=""><</code>", <!-- 0x3C -->
"<code title="">></code>", <!-- 0x3E -->
"<code title="">?</code>", <!-- 0x3F -->
and
"<code title="">`</code>". <!-- 0x60 -->
<p>The <dfn id="password-encode-set">password encode set</dfn> is the
<a href="#default-encode-set">default encode set</a> and code points
"<code title="">/</code>", <!-- 0x2F -->
"<code title="">@</code>", <!-- 0x40 -->
and
"<code title="">\</code>". <!-- 0x5C -->
<p>The <dfn id="username-encode-set">username encode set</dfn> is the
<a href="#password-encode-set">password encode set</a> and code point
"<code title="">:</code>". <!-- 0x3A -->
<p>To <dfn id="utf-8-percent-encode">utf-8 percent encode</dfn> a <var title="">code point</var>, using
an <var title="">encode set</var>, run these steps:
<ol>
<li><p>If <var title="">code point</var> is not in
<var title="">encode set</var>, return <var title="">code point</var>.
<li><p>Let <var title="">bytes</var> be the result of running
<a class="external" data-anolis-spec="encoding" href="http://encoding.spec.whatwg.org/#utf-8-encode">utf-8 encode</a> on
<var title="">code point</var>.
<li><p><a href="#percent-encode">Percent encode</a> each byte in <var title="">bytes</var>, and
then return them concatenated, in the same order.
</ol>
<h2 id="hosts-and-ip-addresses"><span class="secno">4 </span>Hosts and IP addresses</h2>
<!-- Punycode:
http://tools.ietf.org/html/rfc3492
http://mothereff.in/punycode -->
<p>A <dfn id="concept-host" title="concept-host">host</dfn> is a string that represents a
network address, either in the form of a
<a href="#concept-domain" title="concept-domain">domain</a> or an
<a href="#concept-ipv6" title="concept-ipv6">IPv6 address</a>.
<p class="note">This is a slightly more generic definition of
<a href="#concept-host" title="concept-host">host</a> than its traditional meaning for the
sake of convenience.
<!-- "convenience's sake" as The Economist would write it is ugly -->
<p>A <dfn id="concept-domain" title="concept-domain">domain</dfn> is an ordered list of one or
more <dfn id="concept-domain-label" title="concept-domain-label">domain labels</dfn>.
<p>An <dfn id="concept-ipv6" title="concept-ipv6">IPv6 address</dfn> is a 128-bit identifier and
for the purposes of this specification represented as an ordered list of
eight <dfn id="concept-ipv6-piece" title="concept-ipv6-piece">16-bit pieces</dfn>.
<a class="informative" href="#refsIPV6">[IPV6]</a>
<h3 id="host-writing"><span class="secno">4.1 </span>Writing</h3>
<p>A <a href="#concept-host" title="concept-host">host</a> must be either a
<a href="#concept-domain" title="concept-domain">domain</a> or "<code title="">[</code>", followed
by an <a href="#concept-ipv6" title="concept-ipv6">IPv6 address</a>, followed by
"<code title="">]</code>".
<p>A <a href="#concept-domain" title="concept-domain">domain</a> is one or more
<a href="#concept-domain-label" title="concept-domain-label">domain labels</a> separated from each
other by a
<a href="#domain-label-separators" title="domain label separators">domain label separator</a>,
optionally followed by a
<a href="#domain-label-separators" title="domain label separators">domain label separator</a>.
<p class="note">A trailing
<a href="#domain-label-separators" title="domain label separators">domain label separator</a>
signifies an empty <a href="#concept-domain-label" title="concept-domain-label">domain label</a>.
<!-- if a domain has 4 labels, no trailing dot, and each label consists only
of ASCII digits it's IPv4 because the last label of a domain cannot
start with an ASCII digit -->
<p class="XXX">A <a href="#concept-domain-label" title="concept-domain-label">domain label</a> is ...
<p>An <a href="#concept-ipv6" title="concept-ipv6">IPv6 address</a> is defined in the
<a href="http://tools.ietf.org/html/rfc4291#section-2.2">"Text Representation of Addresses" chapter of IP Version 6 Addressing Architecture</a>.
<a href="#refsIPV6">[IPV6]</a>
<!-- http://tools.ietf.org/html/rfc5952 updates that RFC, but it seems as
far as what developers can do we should be liberal
XXX should we define the format inline instead just like STD 66? -->
<h3 id="host-parsing"><span class="secno">4.2 </span>Parsing</h3>
<p>The <dfn id="concept-host-parser" title="concept-host-parser">host parser</dfn> takes a string
<var title="">input</var> and then runs these steps:
<ol>
<li>
<p>If <var title="">input</var> starts with "<code title="">[</code>", run these
substeps:
<ol>
<li><p>If <var title="">input</var> does not end with
"<code title="">]</code>", <a href="#parse-error">parse error</a>, return failure.
<li><p>Return the result of
<a href="#concept-ipv6-parser" title="concept-ipv6-parser">parsing</a> <var title="">input</var>
with its leading "<code title="">[</code>" and trailing
"<code title="">]</code>" removed.
</ol>
<li><p>Let <var title="">host</var> be the result of running
<a class="external" data-anolis-spec="encoding" href="http://encoding.spec.whatwg.org/#utf-8">utf-8</a>'s
<a class="external" data-anolis-spec="encoding" href="http://encoding.spec.whatwg.org/#decoder">decoder</a> on the
<a href="#percent-decode" title="percent decode">percent decoding</a> of
<var title="">input</var>.
<li><p class="XXX"><a href="http://wiki.whatwg.org/wiki/URL#IDNA">IDNA hell</a>
</ol>
<p>The <dfn id="concept-ipv6-parser" title="concept-ipv6-parser">IPv6 parser</dfn> takes a string
<var title="">input</var> and then runs these steps:
<ol>
<li><p>Let <var title="">address</var> be a new
<a href="#concept-ipv6" title="concept-ipv6">IPv6 address</a> with its
<a href="#concept-ipv6-piece" title="concept-ipv6-piece">16-bit pieces</a> initialized to 0.
<li><p>Let <var title="">piece pointer</var> be a pointer into
<var title="">address</var>'s
<a href="#concept-ipv6-piece" title="concept-ipv6-piece">16-bit pieces</a>, initially zero
(pointing to the first <a href="#concept-ipv6-piece" title="concept-ipv6-piece">16-bit piece</a>),
and let <var title="">piece</var> be the
<a href="#concept-ipv6-piece" title="concept-ipv6-piece">16-bit piece</a> it points to.
<li><p>Let <var title="">compress pointer</var> be another pointer into
<var title="">pieces</var>, initially null and pointing to nothing.
<li><p>Let <var title="">pointer</var> be a pointer into
<var title="">input</var>, initially zero (pointing to the first code point).
<li>
<p>If <a href="#c">c</a> is "<code title="">:</code>", run these substeps:
<ol>
<li><p>If <a href="#remaining">remaining</a> does not start with
"<code title="">:</code>", <a href="#parse-error">parse error</a>, return failure.
<li><p>Increase <var title="">pointer</var> by two.
<li><p>Increase <var title="">piece pointer</var> by one and then set
<var title="">compress pointer</var> to <var title="">piece pointer</var>.
</ol>
<li>
<p><dfn id="concept-ipv6-parser-main" title="concept-ipv6-parser-main">Main</dfn>:
While <a href="#c">c</a> is not the <a href="#eof-code-point">EOF code point</a>, run these
substeps:
<ol>
<li><p>If <var title="">piece pointer</var> is eight,
<a href="#parse-error">parse error</a>, return failure.
<li>
<p>If <a href="#c">c</a> is "<code title="">:</code>", run these inner
substeps:
<ol>
<li><p>If <var title="">compress pointer</var> is not null,
<a href="#parse-error">parse error</a>, return failure.
<li>Increase <var title="">pointer</var> and <var title="">piece pointer</var> by one, set
<var title="">compress pointer</var> to <var title="">piece pointer</var>,
and then jump to <a href="#concept-ipv6-parser-main" title="concept-ipv6-parser-main">Main</a>.
</ol>
<li><p>Let <var title="">value</var> and <var title="">length</var> be 0.
<li><p>While <var title="">length</var> is less than 4 and
<a href="#c">c</a> is an
<a href="#ascii-hex-digits" title="ASCII hex digits">ASCII hex digit</a>, set
<var title="">value</var> to
<var title="">value</var> × 0x10 + <a href="#c">c</a> interpreted as hexadecimal number,
and increase <var title="">pointer</var> and <var title="">length</var> by one.
<li>
<p>Based on <a href="#c">c</a>:
<dl class="switch">
<dt>"<code title="">.</code>"
<dd>
<p>If <var title="">length</var> is 0, <a href="#parse-error">parse error</a>,
return failure.
<p>Decrease <var title="">pointer</var> by <var title="">length</var>.
<p>Jump to <a href="#concept-ipv6-parser-ipv4" title="concept-ipv6-parser-ipv4">IPv4</a>.
<dt>"<code title="">:</code>"
<dd>
<p>Increase <var title="">pointer</var> by one.
<p>If <a href="#c">c</a> is the <a href="#eof-code-point">EOF code point</a>,
<a href="#parse-error">parse error</a>, return failure.
<dt>Anything but the <a href="#eof-code-point">EOF code point</a>
<dd><p><a href="#parse-error">Parse error</a>, return failure.
</dl>
<li><p>Set <var title="">piece</var> to <var title="">value</var>.
<li><p>Increase <var title="">piece pointer</var> by one.
</ol>
<li><p>If <a href="#c">c</a> is the <a href="#eof-code-point">EOF code point</a>, jump to
<a href="#concept-ipv6-parser-finale" title="concept-ipv6-parser-finale">Finale</a>.
<li><p><dfn id="concept-ipv6-parser-ipv4" title="concept-ipv6-parser-ipv4">IPv4</dfn>:
If <var title="">piece pointer</var> is greater than six,
<a href="#parse-error">parse error</a>, return failure.
<li><p>Let <var title="">dots seen</var> be 0.
<li>
<p>While <a href="#c">c</a> is not the <a href="#eof-code-point">EOF code point</a>, run
these substeps:
<ol>
<li><p>Let <var title="">value</var> be 0.
<li><p>While <a href="#c">c</a> is an
<a href="#ascii-digits" title="ASCII digits">ASCII digit</a>, set <var title="">value</var> to
<var title="">value</var> × 10 + <a href="#c">c</a> interpreted as decimal number
and increase <var title="">pointer</var> by one.
<li><p>If <var title="">value</var> is greater than 255,
<a href="#parse-error">parse error</a>, return failure.
<li><p>If <var title="">dots seen</var> is less than 3 and
<a href="#c">c</a> is not a "<code title="">.</code>",
<a href="#parse-error">parse error</a>, return failure.
<li><p>If <var title="">dots seen</var> is 3 and <a href="#c">c</a> is not
the <a href="#eof-code-point">EOF code point</a>,
<a href="#parse-error">parse error</a>, return failure.
<li><p>Set <var title="">piece</var> to
<var title="">piece</var> × 0x100 + <var title="">value</var>.
<li><p>If <var title="">dots seen</var> is 0 or 2, increase
<var title="">piece pointer</var> by one.
<li><p>Increase <var title="">dots seen</var> by one.
</ol>
<li>
<p><dfn id="concept-ipv6-parser-finale" title="concept-ipv6-parser-finale">Finale</dfn>:
If <var title="">compress pointer</var> is not null, run these substeps:
<ol>
<li><p>Let <var title="">swaps</var> be
<var title="">piece pointer</var> − <var title="">compress pointer</var>.
<li><p>Set <var title="">piece pointer</var> to seven.
<li><p>While neither <var title="">piece pointer</var> nor
<var title="">swaps</var> is zero, swap <var title="">piece</var> with the
<a href="#concept-ipv6-piece" title="concept-ipv6-piece">piece</a> at pointer
<var title="">compress pointer</var> + <var title="">swaps</var> − 1, and then
decrease <var title="">piece pointer</var> and <var title="">swaps</var> by
one.
</ol>
<li><p>Otherwise, if <var title="">compress pointer</var> is null and
<var title="">piece pointer</var> is not eight, <a href="#parse-error">parse error</a>,
return failure.
<li><p>Return <var title="">address</var>.
</ol>
<h3 id="host-serializing"><span class="secno">4.3 </span>Serializing</h3>
<p>The <dfn id="concept-host-serializer" title="concept-host-serializer">host serializer</dfn> takes a
<a href="#concept-host" title="concept-host">host</a> <var title="">host</var> and then runs
these steps:
<ol>
<li><p>If <var title="">host</var> is null, return the empty string.
<!-- XXX Are we okay with this? -->
<li><p>If <var title="">host</var> is an
<a href="#concept-ipv6" title="concept-ipv6">IPv6 address</a>, return
"<code title="">[</code>", followed by the result of running the
<a href="#concept-ipv6-serializer" title="concept-ipv6-serializer">IPv6 serializer</a> on <var title="">host</var>,
followed by "<code title="">]</code>".
<li class="XXX"><p>If <var title="">host</var> is a
<a href="#concept-domain" title="concept-domain">domain</a> ...
</ol>
<p>The <dfn id="concept-ipv6-serializer" title="concept-ipv6-serializer">IPv6 serializer</dfn> takes an
<a href="#concept-ipv6" title="concept-ipv6">IPv6 address</a> <var title="">address</var> and
then runs these steps:
<ol>
<li><p>Let <var title="">output</var> be the empty string.
<li>
<p>Let <var title="">compress pointer</var> be a pointer to the first
<a href="#concept-ipv6-piece" title="concept-ipv6-piece">16-bit piece</a> in the first longest
sequences of <var title="">address</var>'s
<a href="#concept-ipv6-piece" title="concept-ipv6-piece">16-bit pieces</a> that are 0.
<p class="example">In <code title="">0:f:0:0:f:f:0:0</code> it would point to
the second 0.
<li><p>If there is no sequence of <var title="">address</var>'s
<a href="#concept-ipv6-piece" title="concept-ipv6-piece">16-bit pieces</a> that are 0 longer than
one, set <var title="">compress pointer</var> to null.
<li>
<p>For each <var title="">piece</var> in <var title="">address</var>'s
<a href="#concept-ipv6-piece" title="concept-ipv6-piece">pieces</a>, run these substeps:
<ol>
<li><p>If <var title="">compress pointer</var> points to
<var title="">piece</var>, append "<code title="">::</code>" to
<var title="">output</var> if <var title="">piece</var> is
<var title="">address</var>'s first <a href="#concept-ipv6-piece" title="concept-ipv6-piece">piece</a> and append
"<code title="">:</code>" otherwise, and then run these substeps again with all
subsequent <a href="#concept-ipv6-piece" title="concept-ipv6-piece">pieces</a> in
<var title="">address</var>'s <a href="#concept-ipv6-piece" title="concept-ipv6-piece">pieces</a>
that are 0 skipped or go the next step in the overall set of steps if
that leaves no <a href="#concept-ipv6-piece" title="concept-ipv6-piece">pieces</a>.
<li><p>Append <var title="">piece</var>, represented as the shortest
possible lowercase hexadecimal number, to <var title="">output</var>.
<li><p>If <var title="">piece</var> is not
<var title="">address</var>'s last <a href="#concept-ipv6-piece" title="concept-ipv6-piece">piece</a>,
append "<code title="">:</code>" to <var title="">output</var>.
</ol>
<li><p>Return <var title="">output</var>.
</ol>
<p class="note">This algorithm requires the recommendation from
A Recommendation for IPv6 Address Text Representation.
<a class="informative" href="#refsIPV6TEXT">[IPV6TEXT]</a>
<!-- Safari/Gecko/Opera do not normalize IPv6. Chrome does. This algorithm
follows Chrome because we normalize domains too. -->
<h2 id="urls"><span class="secno">5 </span>URLs</h2>
<!-- History behind URL as term:
http://lists.w3.org/Archives/Public/uri/2012Oct/0080.html -->
<p>A <dfn id="concept-url" title="concept-url">URL</dfn> is a string that represents an
identifier.
<p>A <a href="#concept-url" title="concept-url">URL</a> is either a
<a href="#concept-relative-url" title="concept-relative-url">relative URL</a> or an
<a href="#concept-absolute-url" title="concept-absolute-url">absolute URL</a>. Either form can be
followed by a <a href="#concept-url-fragment" title="concept-url-fragment">fragment</a>.
<p>A <dfn id="concept-relative-url" title="concept-relative-url">relative URL</dfn> is a
<a href="#concept-url" title="concept-url">URL</a> without a <a href="#concept-url-scheme" title="concept-url-scheme">scheme</a>.
A <a href="#concept-relative-url" title="concept-relative-url">relative URL</a> must be relative
to a <a href="#concept-base-url" title="concept-base-url">base URL</a>.
<p>An <dfn id="concept-absolute-url" title="concept-absolute-url">absolute URL</dfn> is a
<a href="#concept-url" title="concept-url">URL</a> with a <a href="#concept-url-scheme" title="concept-url-scheme">scheme</a>.
<p>A <dfn id="concept-base-url" title="concept-base-url">base URL</dfn> is an
<a href="#concept-absolute-url" title="concept-absolute-url">absolute URL</a> with a <a href="#relative-scheme">relative scheme</a>.
<p><a href="#concept-url-parser" title="concept-url-parser">Parsing</a> (provided it does not
return failure) and <a href="#concept-url-serializer" title="concept-url-serializer">serializing</a> a
<a href="#concept-url" title="concept-url">URL</a> will turn it into an
<a href="#concept-absolute-url" title="concept-absolute-url">absolute URL</a>. The intermediate form
is named a <dfn id="concept-parsed-url" title="concept-parsed-url">parsed URL</dfn>. The components a
<a href="#concept-url" title="concept-url">URL</a> can consist of and
<a href="#concept-parsed-url" title="concept-parsed-url">parsed URL</a> consists of are
<dfn id="concept-url-scheme" title="concept-url-scheme">scheme</dfn>,
<dfn id="concept-url-scheme-data" title="concept-url-scheme-data">scheme data</dfn> (not used if
<a href="#concept-url-scheme" title="concept-url-scheme">scheme</a> is a <a href="#relative-scheme">relative scheme</a>),
<dfn id="concept-url-username" title="concept-url-username">username</dfn>,
<dfn id="concept-url-password" title="concept-url-password">password</dfn>,
<dfn id="concept-url-host" title="concept-url-host">host</dfn>,
<dfn id="concept-url-port" title="concept-url-port">port</dfn>,
<dfn id="concept-url-path" title="concept-url-path">path</dfn>,
<dfn id="concept-url-query" title="concept-url-query">query</dfn>, and
<dfn id="concept-url-fragment" title="concept-url-fragment">fragment</dfn>.
<p>A <dfn id="relative-scheme">relative scheme</dfn> is a
<a href="#concept-url-scheme" title="concept-url-scheme">scheme</a> listed in the first column of
the following table. A <dfn id="default-port">default port</dfn> is a
<a href="#relative-scheme">relative scheme</a>'s optional corresponding
<a href="#concept-url-port" title="concept-url-port">port</a> and is listed in the second column
on the same row.
<table>
<tr><th><a href="#concept-url-scheme" title="concept-url-scheme">scheme</a>
<th><a href="#concept-url-port" title="concept-url-port">port</a>
<tr><td>"<code title="">ftp</code>"<td>"<code title="">21</code>"
<tr><td>"<code title="">file</code>"<td>
<tr><td>"<code title="">gopher</code>"<td>"<code title="">70</code>"
<tr><td>"<code title="">http</code>"<td>"<code title="">80</code>"
<tr><td>"<code title="">https</code>"<td>"<code title="">443</code>"
<tr><td>"<code title="">ws</code>"<td>"<code title="">80</code>"
<tr><td>"<code title="">wss</code>"<td>"<code title="">443</code>"
</table>
<!-- The best reason I have for listing "gopher" is Google does it too:
https://code.google.com/p/google-url/source/browse/trunk/src/url_canon_stdurl.cc#120
It seems fine to remain compatible on that front, no need to support it
elsewhere though. -->
<h3 id="writing"><span class="secno">5.1 </span>Writing</h3>
<!-- http://tantek.com/2011/238/b1/many-ways-slice-url-name-pieces -->
<p>A <a href="#concept-url" title="concept-url">URL</a> must be either a
<a href="#concept-relative-url" title="concept-relative-url">relative URL</a> or an
<a href="#concept-absolute-url" title="concept-absolute-url">absolute URL</a>, optionally followed by
"<code title="">#</code>" and a
<a href="#concept-url-fragment" title="concept-url-fragment">fragment</a>.
<p>An <a href="#concept-absolute-url" title="concept-absolute-url">absolute URL</a> must be a
<a href="#concept-url-scheme" title="concept-url-scheme">scheme</a>, followed by
"<code title="">:</code>", followed by
<a href="#concept-url-scheme-data" title="concept-url-scheme-data">scheme data</a>, optionally followed
by "<code title="">?</code>" and a <a href="#concept-url-query" title="concept-url-query">query</a>.
<p>A <a href="#concept-url-scheme" title="concept-url-scheme">scheme</a> must be one
<a href="#ascii-alpha">ASCII alpha</a>, followed by zero or more of
<a href="#ascii-alphanumeric">ASCII alphanumeric</a>, "<code title="">+</code>",
"<code title="">-</code>", and "<code title="">.</code>". A
<a href="#concept-url-scheme" title="concept-url-scheme">scheme</a> must be registered
<span class="XXX">...</span>.
<p>The syntax of <a href="#concept-url-scheme-data" title="concept-url-scheme-data">scheme data</a>
depends on the <a href="#concept-url-scheme" title="concept-url-scheme">scheme</a> and is typically
defined alongside it. For a <a href="#relative-scheme">relative scheme</a>,
<a href="#concept-url-scheme-data" title="concept-url-scheme-data">scheme data</a> must be a
<a href="#concept-scheme-relative-url" title="concept-scheme-relative-url">scheme-relative URL</a>. For
other <a href="#concept-url-scheme" title="concept-url-scheme">schemes</a>, specifications or
standards must define <a href="#concept-url-scheme-data" title="concept-url-scheme-data">scheme data</a>
within the constraints of zero or more <a href="#url-units">URL units</a>.
<p>A <a href="#concept-relative-url" title="concept-relative-url">relative URL</a> must be either a
<a href="#concept-scheme-relative-url" title="concept-scheme-relative-url">scheme-relative URL</a>, an
<a href="#concept-absolute-path-relative-url" title="concept-absolute-path-relative-url">absolute-path-relative URL</a>,
or a <a href="#concept-path-relative-url" title="concept-path-relative-url">path-relative URL</a> that
does not start with a <a href="#concept-url-scheme" title="concept-url-scheme">scheme</a> and
"<code title="">:</code>", optionally followed by a "<code title="">?</code>" and
a <a href="#concept-url-query" title="concept-url-query">query</a>.
<p>A <dfn id="concept-scheme-relative-url" title="concept-scheme-relative-url">scheme-relative URL</dfn> must be
"<code title="">//</code>", optionally followed by
<a href="#concept-url-userinfo" title="concept-url-userinfo">userinfo</a> and "<code title="">@</code>",
followed by a <a href="#concept-host" title="concept-host">host</a>, optionally followed
by "<code title="">:</code>" and a <a href="#concept-url-port" title="concept-url-port">port</a>,
optionally followed by an
<a href="#concept-absolute-path-relative-url" title="concept-absolute-path-relative-url">absolute-path-relative URL</a>.
<p><dfn id="concept-url-userinfo" title="concept-url-userinfo">Userinfo</dfn> must be a
<a href="#concept-url-username" title="concept-url-username">username</a>, optionally followed by a
"<code title="">:</code>" and a
<a href="#concept-url-password" title="concept-url-password">password</a>.
<p>A <a href="#concept-url-username" title="concept-url-username">username</a> must be zero or more
<a href="#url-units">URL units</a>, excluding "<code title="">/</code>",
"<code title="">:</code>, "<code title="">?</code>", and "<code title="">@</code>".
<!-- password without ":" (sorted on ASCII position) -->
<p>A <a href="#concept-url-password" title="concept-url-password">password</a> must be zero or more
<a href="#url-units">URL units</a>, excluding "<code title="">/</code>",
"<code title="">?</code>", and "<code title="">@</code>".
<p>A <a href="#concept-url-port" title="concept-url-port">port</a> must be zero or more
<a href="#ascii-digits">ASCII digits</a>.
<p>An
<dfn id="concept-absolute-path-relative-url" title="concept-absolute-path-relative-url">absolute-path-relative URL</dfn>
must be "<code title="">/</code>", followed by a
<a href="#concept-path-relative-url" title="concept-path-relative-url">path-relative URL</a> that does not
start with "<code title="">/</code>".
<p>A <dfn id="concept-path-relative-url" title="concept-path-relative-url">path-relative URL</dfn> must be zero or
more <a href="#path-segment" title="path segment">path segments</a> separated from each
other by a "<code title="">/</code>".
<p>A <dfn id="path-segment">path segment</dfn> must be zero or more <a href="#url-units">URL units</a>,
excluding "<code title="">/</code>" and "<code title="">?</code>".
<p>A <a href="#concept-url-query" title="concept-url-query">query</a> must be zero or more
<a href="#url-units">URL units</a>.
<p>A <a href="#concept-url-fragment" title="concept-url-fragment">fragment</a> must be zero or more
<a href="#url-units">URL units</a>.
<p>The <dfn id="url-code-points">URL code points</dfn> are <a href="#ascii-alphanumeric">ASCII alphanumeric</a>,
"<code title="">!</code>",<!-- 0x21, sub-delims -->
"<code title="">$</code>",<!-- 0x24, sub-delims -->
"<code title="">&</code>",<!-- 0x26, sub-delims -->
"<code title="">'</code>",<!-- 0x27, sub-delims -->
"<code title="">(</code>",<!-- 0x28, sub-delims -->
"<code title="">)</code>",<!-- 0x29, sub-delims -->
"<code title="">*</code>",<!-- 0x2A, sub-delims -->
"<code title="">+</code>",<!-- 0x2B, sub-delims -->
"<code title="">,</code>",<!-- 0x2C, sub-delims -->
"<code title="">-</code>",<!-- 0x2D, iunreserved -->
"<code title="">.</code>",<!-- 0x2E, iunreserved -->
"<code title="">/</code>",<!-- 0x2F, iquery/ifragment -->
"<code title="">:</code>",<!-- 0x3A, ipchar -->
"<code title="">;</code>",<!-- 0x3B, sub-delims -->
"<code title="">=</code>",<!-- 0x3D, sub-delims -->
"<code title="">?</code>",<!-- 0x3F, iquery/ifragment -->
"<code title="">@</code>",<!-- 0x40, ipchar -->
"<code title="">_</code>",<!-- 0x5F, iunreserved -->
"<code title="">~</code>",<!-- 0x7E, iunreserved -->
and code points in the ranges
U+00A0 to U+D7FF,
U+E000 to <!--U+F8FF,
U+F900 to -->U+FDCF,
U+FDF0 to U+FFEF,
U+10000 to U+1FFFD,
U+20000 to U+2FFFD,
U+30000 to U+3FFFD,
U+40000 to U+4FFFD,
U+50000 to U+5FFFD,
U+60000 to U+6FFFD,
U+70000 to U+7FFFD,
U+80000 to U+8FFFD,
U+90000 to U+9FFFD,
U+A0000 to U+AFFFD,
U+B0000 to U+BFFFD,
U+C0000 to U+CFFFD,
U+D0000 to U+DFFFD,
U+E1000 to U+EFFFD,
U+F0000 to U+FFFFD,
U+100000 to U+10FFFD.
<!-- XXX https://www.w3.org/Bugs/Public/show_bug.cgi?id=19743 -->
<p class="note">Code points higher than U+009F will be converted to
<a href="#percent-encoded-byte" title="percent-encoded byte">percent-encoded bytes</a> by the
<a href="#concept-url-parser" title="concept-url-parser">URL parser</a>.
<p>The <dfn id="url-units">URL units</dfn> are <a href="#url-code-points">URL code points</a> and
<a href="#percent-encoded-byte" title="percent-encoded byte">percent-encoded bytes</a>.
<h3 id="parsing"><span class="secno">5.2 </span>Parsing</h3>
<p>Aside from the components mentioned earlier, a
<a href="#concept-parsed-url" title="concept-parsed-url">parsed URL</a> also has an associated
<dfn id="relative-flag">relative flag</dfn>.
<p class="note">The <a href="#relative-flag">relative flag</a> exists as checking if a
<a href="#concept-parsed-url" title="concept-parsed-url">parsed URL</a>'s
<a href="#concept-url-scheme" title="concept-url-scheme">scheme</a> is a <a href="#relative-scheme">relative scheme</a> can give
incorrect results due to the <a href="#dom-url-protocol"><code title="dom-URL-protocol">protocol</code></a> attribute.
<!-- XXX maybe disallow setting protocol to non-sensical values -->
<p class="XXX">Add the ability to halt on the first conformance error.
<p>The <dfn id="concept-url-parser" title="concept-url-parser">URL parser</dfn> takes a string
<var title="">input</var>, optionally with a
<a href="#concept-base-url" title="concept-base-url">base URL</a> <var title="">base</var>,
optionally with an <a class="external" data-anolis-spec="encoding" href="http://encoding.spec.whatwg.org/#encoding">encoding</a>
<var title="">encoding override</var>, optionally with an
<a href="#concept-parsed-url" title="concept-parsed-url">parsed URL</a> <var title="">url</var>, and
if <var title="">url</var> is given, optionally with a state override
<var title="">state override</var>, and then runs these steps:
<p class="note">The <var title="">encoding override</var> argument is a legacy concept only
relevant for HTML. The <var title="">url</var> and <var title="">state override</var> arguments
can be used for API manipulation of a <a href="#concept-parsed-url" title="concept-parsed-url">parsed URL</a>.
<a class="informative" href="#refsHTML">[HTML]</a>
<ol>
<li>
<p>If <var title="">url</var> is not given:
<ol>
<li><p>Set <var title="">url</var> to a new
<a href="#concept-parsed-url" title="concept-parsed-url">parsed URL</a>.
<li><p>Set <var title="">url</var>'s <a href="#concept-url-scheme" title="concept-url-scheme">scheme</a>,
<a href="#concept-url-scheme-data" title="concept-url-scheme-data">scheme data</a>,
<a href="#concept-url-username" title="concept-url-username">username</a>, and
<a href="#concept-url-port" title="concept-url-port">port</a> to the empty string,
<a href="#concept-url-password" title="concept-url-password">password</a>,
<a href="#concept-url-host" title="concept-url-host">host</a>,
<a href="#concept-url-query" title="concept-url-query">query</a>, and
<a href="#concept-url-fragment" title="concept-url-fragment">fragment</a> to null,
<a href="#concept-url-path" title="concept-url-path">path</a> to the empty list, and unset its
<a href="#relative-flag">relative flag</a>.
<li><p>Remove any leading and trailing
<a class="external" data-anolis-spec="encoding" href="http://encoding.spec.whatwg.org/#ascii-whitespace">ASCII whitespace</a> from
<var title="">input</var>.
</ol>
<li><p>Let <var title="">state</var> be <var title="">state override</var>
if given, or <a href="#scheme-start-state">scheme start state</a> otherwise.
<li><p>If <var title="">base</var> is not given, set it to null.
<li><p>If <var title="">encoding override</var> is not given, set it to
<a class="external" data-anolis-spec="encoding" href="http://encoding.spec.whatwg.org/#utf-8">utf-8</a>.
<li><p>Let <var title="">buffer</var> be the empty string.
<li><p>Let the <var title="">@ flag</var> and the <var title="">[] flag</var> be
unset.
<li><p>Let <var title="">pointer</var> be a pointer to first code point in
<var title="">input</var>.
<li>
<p>Keep running the following state machine by switching on
<var title="">state</var>, increasing <var title="">pointer</var> by one after
each time it is run, as long as <var title="">pointer</var> does not point
past the end of <var title="">input</var>.
<dl class="switch">
<dt><dfn id="scheme-start-state">scheme start state</dfn>
<dd>
<ol>
<li><p>If <a href="#c">c</a> is an <a href="#ascii-alpha">ASCII alpha</a>,
append <a href="#c">c</a>, lowercased, to <var title="">buffer</var>, and
set <var title="">state</var> to <a href="#scheme-state">scheme state</a>.
<li><p>Otherwise, if <var title="">state override</var> is not given, set
<var title="">state</var> to <a href="#no-scheme-state">no scheme state</a>, and decrease
<var title="">pointer</var> by one.
<li><p>Otherwise, <a href="#parse-error">parse error</a>, terminate this algorithm.
</ol>
<dt><dfn id="scheme-state">scheme state</dfn>
<dd>
<ol>
<li><p>If <a href="#c">c</a> is an <a href="#ascii-alphanumeric">ASCII alphanumeric</a>,
"<code title="">+</code>", "<code title="">-</code>", or
"<code title="">.</code>", append <a href="#c">c</a>, lowercased, to
<var title="">buffer</var>.
<li>
<p>Otherwise, if <a href="#c">c</a> is "<code title="">:</code>", set
<var title="">url</var>'s <a href="#concept-url-scheme" title="concept-url-scheme">scheme</a> to
<var title="">buffer</var>, <var title="">buffer</var> to the empty string,
and then run these substeps:
<ol>
<li><p>If <var title="">state override</var> is given,
terminate this algorithm.
<li><p>If <var title="">url</var>'s
<a href="#concept-url-scheme" title="concept-url-scheme">scheme</a> is
a <a href="#relative-scheme">relative scheme</a>, set <var title="">url</var>'s
<a href="#relative-flag">relative flag</a>.
<li><p>If <var title="">url</var>'s
<a href="#concept-url-scheme" title="concept-url-scheme">scheme</a> is
"<code title="">file</code>", set <var title="">state</var> to
<a href="#relative-state">relative state</a>.
<li><p>Otherwise, if <var title="">url</var>'s
<a href="#relative-flag">relative flag</a> is set, <var title="">base</var> is not null
and <var title="">base</var>'s
<a href="#concept-url-scheme" title="concept-url-scheme">scheme</a> is equal to
<var title="">url</var>'s <a href="#concept-url-scheme" title="concept-url-scheme">scheme</a>,
set <var title="">state</var> to
<a href="#relative-or-authority-state">relative or authority state</a>.
<li><p>Otherwise, if <var title="">url</var>'s
<a href="#relative-flag">relative flag</a> is set, set <var title="">state</var> to
<a href="#authority-first-slash-state">authority first slash state</a>.
<li><p>Otherwise, set <var title="">state</var> to
<a href="#scheme-data-state">scheme data state</a>.
</ol>
<li><p>Otherwise, if <var title="">state override</var> is not given, set
<var title="">buffer</var> to the empty string, <var title="">state</var> to
<a href="#no-scheme-state">no scheme state</a>, and start over (from the first code point
in <var title="">input</var>).
<li><p>Otherwise, if <a href="#c">c</a> is the
<a href="#eof-code-point">EOF code point</a>, terminate this algorithm.
<!-- account for URL.protocol = "test" -->
<li><p>Otherwise, <a href="#parse-error">parse error</a>, terminate this algorithm.
</ol>
<dt><dfn id="scheme-data-state">scheme data state</dfn>
<dd>
<ol>
<li><p>If <a href="#c">c</a> is "<code title="">?</code>", set
<var title="">url</var>'s <a href="#concept-url-query" title="concept-url-query">query</a>
to the empty string and <var title="">state</var> to
<a href="#query-state">query state</a>.
<li><p>Otherwise, if <a href="#c">c</a> is "<code title="">#</code>", set
<var title="">url</var>'s <a href="#concept-url-fragment" title="concept-url-fragment">fragment</a>
to the empty string and <var title="">state</var> to
<a href="#fragment-state">fragment state</a>.
<li>
<p>Otherwise, run these substeps:
<ol>
<li><p>If <a href="#c">c</a> is not the <a href="#eof-code-point">EOF code point</a>
and not a <a href="#url-code-points" title="URL code points">URL code point</a>, or
<a href="#c">c</a> is "<code title="">%</code>" and
<a href="#remaining">remaining</a> does not start with two
<a href="#ascii-hex-digits">ASCII hex digits</a>, <a href="#parse-error">parse error</a>.
<li><p>If <a href="#c">c</a> is none of
<a href="#eof-code-point">EOF code point</a>, U+0009, U+000A, and U+000D,
<a href="#utf-8-percent-encode">utf-8 percent encode</a> <a href="#c">c</a> using the
<a href="#simple-encode-set">simple encode set</a>, and append the result to
<var title="">url</var>'s
<a href="#concept-url-scheme-data" title="concept-url-scheme-data">scheme data</a>.
</ol>
</ol>
<dt><dfn id="no-scheme-state">no scheme state</dfn>
<dd>
<p>If <var title="">base</var> is null, or <var title="">base</var>'s
<a href="#concept-url-scheme" title="concept-url-scheme">scheme</a> is not a
<a href="#relative-scheme">relative scheme</a>, <a href="#parse-error">parse error</a>, return failure.
<p class="note">Due to the <a href="#dom-url-protocol"><code title="dom-URL-protocol">protocol</code></a> attribute's
ability to change <var title="">base</var>'s
<a href="#concept-url-scheme" title="concept-url-scheme">scheme</a>, <var title="">base</var>'s
<a href="#relative-flag">relative flag</a> is not used here.
<p>Otherwise, set <var title="">state</var> to <a href="#relative-state">relative state</a>,
and decrease <var title="">pointer</var> by one.
<dt><dfn id="relative-or-authority-state">relative or authority state</dfn>
<dd>
<p>If <a href="#c">c</a> is "<code>/</code>" and
<a href="#remaining">remaining</a> starts with "<code>/</code>", set
<var title="">state</var> to <a href="#authority-ignore-slashes-state">authority ignore slashes state</a>
and increase <var title="">pointer</var> by one.
<p>Otherwise, <a href="#parse-error">parse error</a>, set <var title="">state</var> to
<a href="#relative-state">relative state</a> and decrease <var title="">pointer</var> by
one.
<dt><dfn id="relative-state">relative state</dfn>
<dd>
<p>Set <var title="">url</var>'s <a href="#relative-flag">relative flag</a>, set
<var title="">url</var>'s <a href="#concept-url-scheme" title="concept-url-scheme">scheme</a> to
<var title="">base</var>'s <a href="#concept-url-scheme" title="concept-url-scheme">scheme</a> if
<var title="">url</var>'s <a href="#concept-url-scheme" title="concept-url-scheme">scheme</a> is not
"<code title="">file</code>", and then, based on <a href="#c">c</a>:
<dl class="switch">
<dt><a href="#eof-code-point">EOF code point</a>
<dd>
<p>Set <var title="">url</var>'s <a href="#concept-url-host" title="concept-url-host">host</a>