/
url.src.html
2440 lines (1893 loc) · 91.9 KB
/
url.src.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 rel=stylesheet href=http://www.whatwg.org/style/specification>
<link rel=icon href=//resources.whatwg.org/logo-url.svg>
<div class=head>
<p><a class=logo href=//www.whatwg.org/><img alt=WHATWG src=//resources.whatwg.org/logo-url.svg width=100 height=100></a></p>
<h1>URL</h1>
<h2 class="no-num no-toc">Living Standard — Last Updated [DATE: 01 Jan 1901]</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" lang="nl" href="http://annevankesteren.nl/">Anne van Kesteren</a>
<<a class="u-email email" href="mailto:annevk@annevk.nl">annevk@annevk.nl</a>>
</dl>
<script src=//resources.whatwg.org/file-bug.js async></script>
<p class=copyright><a rel="license" href="http://creativecommons.org/publicdomain/zero/1.0/"><img src="http://i.creativecommons.org/p/zero/1.0/80x15.png" alt="CC0"></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
[DATE: 01 Jan 1901], the editors have made this specification available
under the
<a rel="license"
href="http://www.openwebfoundation.org/legal/the-owf-1-0-agreements/owfa-1-0">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">Table of Contents</h2>
<!--toc-->
<h2 class=no-num>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.
<span data-anolis-ref class=informative>URI</span>
<span data-anolis-ref class=informative>IRI</span>
<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 <code>URL</code>
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>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.
<span data-anolis-ref>RFC2119</span>
<h2>Terminology</h2>
<p>Some terms used in this specification are defined in the
Encoding Standard. <span data-anolis-ref>ENCODING</span>
<p>The <dfn>ASCII digits</dfn> are code points in the range U+0030 to U+0039.
<p>The <dfn>ASCII hex digits</dfn> are <span>ASCII digits</span> or are
code points in the range U+0041 to U+0046 or in the range U+0061 to U+0066.
<p>The <dfn>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>ASCII alphanumeric</dfn> are <span>ASCII digits</span> or
<span>ASCII alpha</span>.
<p>The <dfn>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>Parsers</h3>
<p>The <dfn>EOF code point</dfn> signifies the end of a string or
code point stream.
<p>A <dfn>parse error</dfn> indicates a non-fatal mismatch between input and requirements.
User agents are encouraged to expose <span title="parse error">parse errors</span>
somehow.
<p>Within a parser algorithm that uses a <var title>pointer</var> variable, <dfn>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>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>",
<span>c</span> is "<code title>@</code>" and <span>remaining</span> is
"<code title>example</code>".
<h2>Percent-encoded bytes</h2>
<p>A <dfn>percent-encoded byte</dfn> is "<code title>%</code>", followed by
two <span>ASCII hex digits</span>. Sequences of
<span title="percent-encoded byte">percent-encoded bytes</span>, after
conversion to bytes, should not cause
<span data-anolis-spec=encoding>utf-8 decode</span> to emit any
<span data-anolis-spec=encoding title="decoder error">decoder errors</span>.
<p>To <dfn>percent encode</dfn> a <var title>byte</var> into a
<span>percent-encoded byte</span>, return a string consisting of
"<code title>%</code>", followed by a double-digit, uppercase, hexadecimal
representation of <var title>byte</var>.
<p>To <dfn>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 <span>c</span> is not the <span>EOF code point</span>, run
these substeps:
<ol>
<li><p>While <span>c</span> is not "<code title>%</code>" or the
<span>EOF code point</span>, append to <var title>bytes</var> a byte
whose value is <span>c</span>'s code point and increase
<var title>pointer</var> by one.
<li><p>If <span>c</span> is "<code title>%</code>" and
<span>remaining</span> does not start with two
<span>ASCII hex digits</span>, append to <var title>bytes</var> a byte
whose value is <span>c</span>'s code point, increase
<var title>pointer</var> by one.
<li><p>Otherwise, while <span>c</span> is "<code title>%</code>" and
<span>remaining</span> starts with two <span>ASCII hex digits</span>,
append to <var title>bytes</var> a byte whose value is
<span>remaining</span>'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>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>default encode set</dfn> is the
<span>simple encode set</span> 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>password encode set</dfn> is the
<span>default encode set</span> and code points
"<code title>/</code>", <!-- 0x2F -->
"<code title>@</code>", <!-- 0x40 -->
and
"<code title>\</code>". <!-- 0x5C -->
<p>The <dfn>username encode set</dfn> is the
<span>password encode set</span> and code point
"<code title>:</code>". <!-- 0x3A -->
<p>To <dfn>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
<span data-anolis-spec=encoding>utf-8 encode</span> on
<var title>code point</var>.
<li><p><span>Percent encode</span> each byte in <var title>bytes</var>, and
then return them concatenated, in the same order.
</ol>
<h2>Hosts and IP addresses</h2>
<!-- Punycode:
http://tools.ietf.org/html/rfc3492
http://mothereff.in/punycode -->
<p>A <dfn title=concept-host>host</dfn> is a string that represents a
network address, either in the form of a
<span title=concept-domain>domain</span> or an
<span title=concept-ipv6>IPv6 address</span>.
<p class=note>This is a slightly more generic definition of
<span title=concept-host>host</span> than its traditional meaning for the
sake of convenience.
<!-- "convenience's sake" as The Economist would write it is ugly -->
<p>A <dfn title=concept-domain>domain</dfn> is an ordered list of one or
more <dfn title=concept-domain-label>domain labels</dfn>.
<p>An <dfn 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 title=concept-ipv6-piece>16-bit pieces</dfn>.
<span data-anolis-ref class=informative>IPV6</span>
<h3 id=host-writing>Writing</h3>
<p>A <span title=concept-host>host</span> must be either a
<span title=concept-domain>domain</span> or "<code title>[</code>", followed
by an <span title=concept-ipv6>IPv6 address</span>, followed by
"<code title>]</code>".
<p>A <span title=concept-domain>domain</span> is one or more
<span title=concept-domain-label>domain labels</span> separated from each
other by a
<span title="domain label separators">domain label separator</span>,
optionally followed by a
<span title="domain label separators">domain label separator</span>.
<p class=note>A trailing
<span title="domain label separators">domain label separator</span>
signifies an empty <span title=concept-domain-label>domain label</span>.
<!-- 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 <span title=concept-domain-label>domain label</span> is ...
<p>An <span title=concept-ipv6>IPv6 address</span> 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>.
<span data-anolis-ref>IPV6</span>
<!-- 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>Parsing</h3>
<p>The <dfn 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>", <span>parse error</span>, return failure.
<li><p>Return the result of
<span title=concept-ipv6-parser>parsing</span> <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
<span data-anolis-spec=encoding>utf-8</span>'s
<span data-anolis-spec=encoding>decoder</span> on the
<span title="percent decode">percent decoding</span> 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 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
<span title=concept-ipv6>IPv6 address</span> with its
<span title=concept-ipv6-piece>16-bit pieces</span> initialized to 0.
<li><p>Let <var title>piece pointer</var> be a pointer into
<var title>address</var>'s
<span title=concept-ipv6-piece>16-bit pieces</span>, initially zero
(pointing to the first <span title=concept-ipv6-piece>16-bit piece</span>),
and let <var title>piece</var> be the
<span title=concept-ipv6-piece>16-bit piece</span> 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 <span>c</span> is "<code title>:</code>", run these substeps:
<ol>
<li><p>If <span>remaining</span> does not start with
"<code title>:</code>", <span>parse error</span>, 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 title=concept-ipv6-parser-main>Main</dfn>:
While <span>c</span> is not the <span>EOF code point</span>, run these
substeps:
<ol>
<li><p>If <var title>piece pointer</var> is eight,
<span>parse error</span>, return failure.
<li>
<p>If <span>c</span> is "<code title>:</code>", run these inner
substeps:
<ol>
<li><p>If <var title>compress pointer</var> is not null,
<span>parse error</span>, 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 <span title=concept-ipv6-parser-main>Main</span>.
</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
<span>c</span> is an
<span title="ASCII hex digits">ASCII hex digit</span>, set
<var title>value</var> to
<var title>value</var> × 0x10 + <span>c</span> interpreted as hexadecimal number,
and increase <var title>pointer</var> and <var title>length</var> by one.
<li>
<p>Based on <span>c</span>:
<dl class=switch>
<dt>"<code title>.</code>"
<dd>
<p>If <var title>length</var> is 0, <span>parse error</span>,
return failure.
<p>Decrease <var title>pointer</var> by <var title>length</var>.
<p>Jump to <span title=concept-ipv6-parser-ipv4>IPv4</span>.
<dt>"<code title>:</code>"
<dd>
<p>Increase <var title>pointer</var> by one.
<p>If <span>c</span> is the <span>EOF code point</span>,
<span>parse error</span>, return failure.
<dt>Anything but the <span>EOF code point</span>
<dd><p><span>Parse error</span>, 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 <span>c</span> is the <span>EOF code point</span>, jump to
<span title=concept-ipv6-parser-finale>Finale</span>.
<li><p><dfn title=concept-ipv6-parser-ipv4>IPv4</dfn>:
If <var title>piece pointer</var> is greater than six,
<span>parse error</span>, return failure.
<li><p>Let <var title>dots seen</var> be 0.
<li>
<p>While <span>c</span> is not the <span>EOF code point</span>, run
these substeps:
<ol>
<li><p>Let <var title>value</var> be 0.
<li><p>While <span>c</span> is an
<span title="ASCII digits">ASCII digit</span>, set <var title>value</var> to
<var title>value</var> × 10 + <span>c</span> interpreted as decimal number
and increase <var title>pointer</var> by one.
<li><p>If <var title>value</var> is greater than 255,
<span>parse error</span>, return failure.
<li><p>If <var title>dots seen</var> is less than 3 and
<span>c</span> is not a "<code title>.</code>",
<span>parse error</span>, return failure.
<li><p>If <var title>dots seen</var> is 3 and <span>c</span> is not
the <span>EOF code point</span>,
<span>parse error</span>, 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 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
<span title=concept-ipv6-piece>piece</span> 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, <span>parse error</span>,
return failure.
<li><p>Return <var title>address</var>.
</ol>
<h3 id=host-serializing>Serializing</h3>
<p>The <dfn title=concept-host-serializer>host serializer</dfn> takes a
<span title=concept-host>host</span> <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
<span title=concept-ipv6>IPv6 address</span>, return
"<code title>[</code>", followed by the result of running the
<span title=concept-ipv6-serializer>IPv6 serializer</span> on <var title>host</var>,
followed by "<code title>]</code>".
<li class=XXX><p>If <var title>host</var> is a
<span title=concept-domain>domain</span> ...
</ol>
<p>The <dfn title=concept-ipv6-serializer>IPv6 serializer</dfn> takes an
<span title=concept-ipv6>IPv6 address</span> <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
<span title=concept-ipv6-piece>16-bit piece</span> in the first longest
sequences of <var title>address</var>'s
<span title=concept-ipv6-piece>16-bit pieces</span> 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
<span title=concept-ipv6-piece>16-bit pieces</span> 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
<span title=concept-ipv6-piece>pieces</span>, 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 <span title=concept-ipv6-piece>piece</span> and append
"<code title>:</code>" otherwise, and then run these substeps again with all
subsequent <span title=concept-ipv6-piece>pieces</span> in
<var title>address</var>'s <span title=concept-ipv6-piece>pieces</span>
that are 0 skipped or go the next step in the overall set of steps if
that leaves no <span title=concept-ipv6-piece>pieces</span>.
<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 <span title=concept-ipv6-piece>piece</span>,
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.
<span data-anolis-ref class=informative>IPV6TEXT</span>
<!-- Safari/Gecko/Opera do not normalize IPv6. Chrome does. This algorithm
follows Chrome because we normalize domains too. -->
<h2>URLs</h2>
<!-- History behind URL as term:
http://lists.w3.org/Archives/Public/uri/2012Oct/0080.html -->
<p>A <dfn title=concept-url>URL</dfn> is a string that represents an
identifier.
<p>A <span title=concept-url>URL</span> is either a
<span title=concept-relative-url>relative URL</span> or an
<span title=concept-absolute-url>absolute URL</span>. Either form can be
followed by a <span title=concept-url-fragment>fragment</span>.
<p>A <dfn title=concept-relative-url>relative URL</dfn> is a
<span title=concept-url>URL</span> without a <span title=concept-url-scheme>scheme</span>.
A <span title=concept-relative-url>relative URL</span> must be relative
to a <span title=concept-base-url>base URL</span>.
<p>An <dfn title=concept-absolute-url>absolute URL</dfn> is a
<span title=concept-url>URL</span> with a <span title=concept-url-scheme>scheme</span>.
<p>A <dfn title=concept-base-url>base URL</dfn> is an
<span title=concept-absolute-url>absolute URL</span> with a <span>relative scheme</span>.
<p><span title=concept-url-parser>Parsing</span> (provided it does not
return failure) and <span title=concept-url-serializer>serializing</span> a
<span title=concept-url>URL</span> will turn it into an
<span title=concept-absolute-url>absolute URL</span>. The intermediate form
is named a <dfn title=concept-parsed-url>parsed URL</dfn>. The components a
<span title=concept-url>URL</span> can consist of and
<span title=concept-parsed-url>parsed URL</span> consists of are
<dfn title=concept-url-scheme>scheme</dfn>,
<dfn title=concept-url-scheme-data>scheme data</dfn> (not used if
<span title=concept-url-scheme>scheme</span> is a <span>relative scheme</span>),
<dfn title=concept-url-username>username</dfn>,
<dfn title=concept-url-password>password</dfn>,
<dfn title=concept-url-host>host</dfn>,
<dfn title=concept-url-port>port</dfn>,
<dfn title=concept-url-path>path</dfn>,
<dfn title=concept-url-query>query</dfn>, and
<dfn title=concept-url-fragment>fragment</dfn>.
<p>A <dfn>relative scheme</dfn> is a
<span title=concept-url-scheme>scheme</span> listed in the first column of
the following table. A <dfn>default port</dfn> is a
<span>relative scheme</span>'s optional corresponding
<span title=concept-url-port>port</span> and is listed in the second column
on the same row.
<table>
<tr><th><span title=concept-url-scheme>scheme</span>
<th><span title=concept-url-port>port</span>
<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>Writing</h3>
<!-- http://tantek.com/2011/238/b1/many-ways-slice-url-name-pieces -->
<p>A <span title=concept-url>URL</span> must be either a
<span title=concept-relative-url>relative URL</span> or an
<span title=concept-absolute-url>absolute URL</span>, optionally followed by
"<code title>#</code>" and a
<span title=concept-url-fragment>fragment</span>.
<p>An <span title=concept-absolute-url>absolute URL</span> must be a
<span title=concept-url-scheme>scheme</span>, followed by
"<code title>:</code>", followed by
<span title=concept-url-scheme-data>scheme data</span>, optionally followed
by "<code title>?</code>" and a <span title=concept-url-query>query</span>.
<p>A <span title=concept-url-scheme>scheme</span> must be one
<span>ASCII alpha</span>, followed by zero or more of
<span>ASCII alphanumeric</span>, "<code title>+</code>",
"<code title>-</code>", and "<code title>.</code>". A
<span title=concept-url-scheme>scheme</span> must be registered
<span class=XXX>...</span>.
<p>The syntax of <span title=concept-url-scheme-data>scheme data</span>
depends on the <span title=concept-url-scheme>scheme</span> and is typically
defined alongside it. For a <span>relative scheme</span>,
<span title=concept-url-scheme-data>scheme data</span> must be a
<span title=concept-scheme-relative-url>scheme-relative URL</span>. For
other <span title=concept-url-scheme>schemes</span>, specifications or
standards must define <span title=concept-url-scheme-data>scheme data</span>
within the constraints of zero or more <span>URL units</span>.
<p>A <span title=concept-relative-url>relative URL</span> must be either a
<span title=concept-scheme-relative-url>scheme-relative URL</span>, an
<span title=concept-absolute-path-relative-url>absolute-path-relative URL</span>,
or a <span title=concept-path-relative-url>path-relative URL</span> that
does not start with a <span title=concept-url-scheme>scheme</span> and
"<code title>:</code>", optionally followed by a "<code title>?</code>" and
a <span title=concept-url-query>query</span>.
<p>A <dfn title=concept-scheme-relative-url>scheme-relative URL</dfn> must be
"<code title>//</code>", optionally followed by
<span title=concept-url-userinfo>userinfo</span> and "<code title>@</code>",
followed by a <span title=concept-host>host</span>, optionally followed
by "<code title>:</code>" and a <span title=concept-url-port>port</span>,
optionally followed by an
<span title=concept-absolute-path-relative-url>absolute-path-relative URL</span>.
<p><dfn title=concept-url-userinfo>Userinfo</dfn> must be a
<span title=concept-url-username>username</span>, optionally followed by a
"<code title>:</code>" and a
<span title=concept-url-password>password</span>.
<p>A <span title=concept-url-username>username</span> must be zero or more
<span>URL units</span>, excluding "<code title>/</code>",
"<code title>:</code>, "<code title>?</code>", and "<code title>@</code>".
<!-- password without ":" (sorted on ASCII position) -->
<p>A <span title=concept-url-password>password</span> must be zero or more
<span>URL units</span>, excluding "<code title>/</code>",
"<code title>?</code>", and "<code title>@</code>".
<p>A <span title=concept-url-port>port</span> must be zero or more
<span>ASCII digits</span>.
<p>An
<dfn title=concept-absolute-path-relative-url>absolute-path-relative URL</dfn>
must be "<code title>/</code>", followed by a
<span title=concept-path-relative-url>path-relative URL</span> that does not
start with "<code title>/</code>".
<p>A <dfn title=concept-path-relative-url>path-relative URL</dfn> must be zero or
more <span title="path segment">path segments</span> separated from each
other by a "<code title>/</code>".
<p>A <dfn>path segment</dfn> must be zero or more <span>URL units</span>,
excluding "<code title>/</code>" and "<code title>?</code>".
<p>A <span title=concept-url-query>query</span> must be zero or more
<span>URL units</span>.
<p>A <span title=concept-url-fragment>fragment</span> must be zero or more
<span>URL units</span>.
<p>The <dfn>URL code points</dfn> are <span>ASCII alphanumeric</span>,
"<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
<span title="percent-encoded byte">percent-encoded bytes</span> by the
<span title=concept-url-parser>URL parser</span>.
<p>The <dfn>URL units</dfn> are <span>URL code points</span> and
<span title="percent-encoded byte">percent-encoded bytes</span>.
<h3>Parsing</h3>
<p>Aside from the components mentioned earlier, a
<span title=concept-parsed-url>parsed URL</span> also has an associated
<dfn>relative flag</dfn>.
<p class=note>The <span>relative flag</span> exists as checking if a
<span title=concept-parsed-url>parsed URL</span>'s
<span title=concept-url-scheme>scheme</span> is a <span>relative scheme</span> can give
incorrect results due to the <code title=dom-URL-protocol>protocol</code> 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 title=concept-url-parser>URL parser</dfn> takes a string
<var title>input</var>, optionally with a
<span title=concept-base-url>base URL</span> <var title>base</var>,
optionally with an <span data-anolis-spec=encoding>encoding</span>
<var title>encoding override</var>, optionally with an
<span title=concept-parsed-url>parsed URL</span> <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 <span title=concept-parsed-url>parsed URL</span>.
<span class=informative data-anolis-ref>HTML</span>
<ol>
<li>
<p>If <var title>url</var> is not given:
<ol>
<li><p>Set <var title>url</var> to a new
<span title=concept-parsed-url>parsed URL</span>.
<li><p>Set <var title>url</var>'s <span title=concept-url-scheme>scheme</span>,
<span title=concept-url-scheme-data>scheme data</span>,
<span title=concept-url-username>username</span>, and
<span title=concept-url-port>port</span> to the empty string,
<span title=concept-url-password>password</span>,
<span title=concept-url-host>host</span>,
<span title=concept-url-query>query</span>, and
<span title=concept-url-fragment>fragment</span> to null,
<span title=concept-url-path>path</span> to the empty list, and unset its
<span>relative flag</span>.
<li><p>Remove any leading and trailing
<span data-anolis-spec=encoding>ASCII whitespace</span> from
<var title>input</var>.
</ol>
<li><p>Let <var title>state</var> be <var title>state override</var>
if given, or <span>scheme start state</span> 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
<span data-anolis-spec=encoding>utf-8</span>.
<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>scheme start state</dfn>
<dd>
<ol>
<li><p>If <span>c</span> is an <span>ASCII alpha</span>,
append <span>c</span>, lowercased, to <var title>buffer</var>, and
set <var title>state</var> to <span>scheme state</span>.
<li><p>Otherwise, if <var title>state override</var> is not given, set
<var title>state</var> to <span>no scheme state</span>, and decrease
<var title>pointer</var> by one.
<li><p>Otherwise, <span>parse error</span>, terminate this algorithm.
</ol>
<dt><dfn>scheme state</dfn>
<dd>
<ol>
<li><p>If <span>c</span> is an <span>ASCII alphanumeric</span>,
"<code title>+</code>", "<code title>-</code>", or
"<code title>.</code>", append <span>c</span>, lowercased, to
<var title>buffer</var>.
<li>
<p>Otherwise, if <span>c</span> is "<code title>:</code>", set
<var title>url</var>'s <span title=concept-url-scheme>scheme</span> 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
<span title=concept-url-scheme>scheme</span> is
a <span>relative scheme</span>, set <var title>url</var>'s
<span>relative flag</span>.
<li><p>If <var title>url</var>'s
<span title=concept-url-scheme>scheme</span> is
"<code title>file</code>", set <var title>state</var> to
<span>relative state</span>.
<li><p>Otherwise, if <var title>url</var>'s
<span>relative flag</span> is set, <var title>base</var> is not null
and <var title>base</var>'s
<span title=concept-url-scheme>scheme</span> is equal to
<var title>url</var>'s <span title=concept-url-scheme>scheme</span>,
set <var title>state</var> to
<span>relative or authority state</span>.
<li><p>Otherwise, if <var title>url</var>'s
<span>relative flag</span> is set, set <var title>state</var> to
<span>authority first slash state</span>.
<li><p>Otherwise, set <var title>state</var> to
<span>scheme data state</span>.
</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
<span>no scheme state</span>, and start over (from the first code point
in <var title>input</var>).
<li><p>Otherwise, if <span>c</span> is the
<span>EOF code point</span>, terminate this algorithm.
<!-- account for URL.protocol = "test" -->
<li><p>Otherwise, <span>parse error</span>, terminate this algorithm.
</ol>
<dt><dfn>scheme data state</dfn>
<dd>
<ol>
<li><p>If <span>c</span> is "<code title>?</code>", set
<var title>url</var>'s <span title=concept-url-query>query</span>
to the empty string and <var title>state</var> to
<span>query state</span>.
<li><p>Otherwise, if <span>c</span> is "<code title>#</code>", set
<var title>url</var>'s <span title=concept-url-fragment>fragment</span>
to the empty string and <var title>state</var> to
<span>fragment state</span>.
<li>
<p>Otherwise, run these substeps:
<ol>
<li><p>If <span>c</span> is not the <span>EOF code point</span>
and not a <span title="URL code points">URL code point</span>, or
<span>c</span> is "<code title>%</code>" and
<span>remaining</span> does not start with two
<span>ASCII hex digits</span>, <span>parse error</span>.
<li><p>If <span>c</span> is none of
<span>EOF code point</span>, U+0009, U+000A, and U+000D,
<span>utf-8 percent encode</span> <span>c</span> using the
<span>simple encode set</span>, and append the result to
<var title>url</var>'s
<span title=concept-url-scheme-data>scheme data</span>.
</ol>
</ol>
<dt><dfn>no scheme state</dfn>
<dd>
<p>If <var title>base</var> is null, or <var title>base</var>'s
<span title=concept-url-scheme>scheme</span> is not a
<span>relative scheme</span>, <span>parse error</span>, return failure.
<p class=note>Due to the <code title=dom-URL-protocol>protocol</code> attribute's
ability to change <var title>base</var>'s
<span title=concept-url-scheme>scheme</span>, <var title>base</var>'s
<span>relative flag</span> is not used here.
<p>Otherwise, set <var title>state</var> to <span>relative state</span>,
and decrease <var title>pointer</var> by one.
<dt><dfn>relative or authority state</dfn>
<dd>
<p>If <span>c</span> is "<code>/</code>" and
<span>remaining</span> starts with "<code>/</code>", set
<var title>state</var> to <span>authority ignore slashes state</span>
and increase <var title>pointer</var> by one.
<p>Otherwise, <span>parse error</span>, set <var title>state</var> to
<span>relative state</span> and decrease <var title>pointer</var> by
one.
<dt><dfn>relative state</dfn>
<dd>
<p>Set <var title>url</var>'s <span>relative flag</span>, set
<var title>url</var>'s <span title=concept-url-scheme>scheme</span> to
<var title>base</var>'s <span title=concept-url-scheme>scheme</span> if
<var title>url</var>'s <span title=concept-url-scheme>scheme</span> is not
"<code title>file</code>", and then, based on <span>c</span>:
<dl class=switch>
<dt><span>EOF code point</span>
<dd>
<p>Set <var title>url</var>'s <span title=concept-url-host>host</span>
to <var title>base</var>'s <span title=concept-url-host>host</span>,
<var title>url</var>'s <span title=concept-url-port>port</span> to
<var title>base</var>'s <span title=concept-url-port>port</span>,
<var title>url</var>'s <span title=concept-url-path>path</span> to
<var title>base</var>'s <span title=concept-url-path>path</span>, and
<var title>url</var>'s <span title=concept-url-query>query</span> to
<var title>base</var>'s <span title=concept-url-query>query</span>.
<dt>"<code title>/</code>"
<dt>"<code title>\</code>"
<dd>
<ol>
<li><p>If <span>c</span> is "<code title>\</code>",
<span>parse error</span>.
<li><p>Set <var title>state</var> to
<span>relative slash state</span>.
</ol>
<dt>"<code title>?</code>"
<dd><p>Set
<var title>url</var>'s <span title=concept-url-host>host</span> to
<var title>base</var>'s <span title=concept-url-host>host</span>,
<var title>url</var>'s <span title=concept-url-port>port</span> to
<var title>base</var>'s <span title=concept-url-port>port</span>,