forked from whatwg/encoding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Overview.html
2873 lines (2183 loc) · 124 KB
/
Overview.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>Encoding Standard</title>
<link href="//www.whatwg.org/style/specification" rel="stylesheet">
<link href="//resources.whatwg.org/logo-encoding.svg" rel="icon">
<div class="head">
<p><a class="logo" href="//www.whatwg.org/"><img alt="WHATWG" height="100" src="//resources.whatwg.org/logo-encoding.svg" width="100"></a></p>
<h1>Encoding</h1>
<h2 class="no-num no-toc" id="living-standard-—-last-updated-22-february-2013">Living Standard — Last Updated 22 February 2013</h2>
<dl>
<dt>This Version:
<dd><a href="http://encoding.spec.whatwg.org/">http://encoding.spec.whatwg.org/</a>
<dt>Participate:
<dd>Send feedback to <a href="mailto:[email protected]?subject=Encoding:%20">[email protected]</a>
(<a href="http://lists.w3.org/Archives/Public/www-international/">archives</a>) or
<a href="https://www.w3.org/Bugs/Public/enter_bug.cgi?product=WHATWG&component=Encoding">file a bug</a>
(<a href="https://www.w3.org/Bugs/Public/buglist.cgi?product=WHATWG&component=Encoding&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/encoding/commits">https://github.com/whatwg/encoding/commits</a>
<dd><a href="https://twitter.com/encodings">@encodings</a>
<dt>Editors:
<dd><a href="http://annevankesteren.nl/">Anne van Kesteren</a>
<<a href="mailto:[email protected]">[email protected]</a>>
<dd>Joshua Bell (Google)
</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">CC0 1.0 Universal</a>.
To the extent possible under law, the editor has waived all copyright and
related or neighboring rights to this work. In addition, as of
22 February 2013, the editor has 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 href="#preface"><span class="secno">1 </span>Preface</a></li>
<li><a href="#conformance"><span class="secno">2 </span>Conformance</a></li>
<li><a href="#terminology"><span class="secno">3 </span>Terminology</a></li>
<li><a href="#encodings"><span class="secno">4 </span>Encodings</a></li>
<li><a href="#indexes"><span class="secno">5 </span>Indexes</a></li>
<li><a href="#decode-and-encode"><span class="secno">6 </span>Decode and encode</a></li>
<li><a href="#api"><span class="secno">7 </span>API</a>
<ol>
<li><a href="#interface-textdecoder"><span class="secno">7.1 </span>Interface <code title="">TextDecoder</code></a></li>
<li><a href="#interface-textencoder"><span class="secno">7.2 </span>Interface <code title="">TextEncoder</code></a></ol></li>
<li><a href="#the-encoding"><span class="secno">8 </span>The encoding</a>
<ol>
<li><a href="#utf-8"><span class="secno">8.1 </span>utf-8</a></ol></li>
<li><a href="#legacy-single-byte-encodings"><span class="secno">9 </span>Legacy single-byte encodings</a></li>
<li><a href="#legacy-multi-byte-chinese-(simplified)-encodings"><span class="secno">10 </span>Legacy multi-byte Chinese (simplified) encodings</a>
<ol>
<li><a href="#gbk"><span class="secno">10.1 </span>gbk</a></li>
<li><a href="#gb18030"><span class="secno">10.2 </span>gb18030</a></li>
<li><a href="#hz-gb-2312"><span class="secno">10.3 </span>hz-gb-2312</a></ol></li>
<li><a href="#legacy-multi-byte-chinese-(traditional)-encodings"><span class="secno">11 </span>Legacy multi-byte Chinese (traditional) encodings</a>
<ol>
<li><a href="#big5"><span class="secno">11.1 </span>big5</a></ol></li>
<li><a href="#legacy-multi-byte-japanese-encodings"><span class="secno">12 </span>Legacy multi-byte Japanese encodings</a>
<ol>
<li><a href="#euc-jp"><span class="secno">12.1 </span>euc-jp</a></li>
<li><a href="#iso-2022-jp"><span class="secno">12.2 </span>iso-2022-jp</a></li>
<li><a href="#shift_jis"><span class="secno">12.3 </span>shift_jis</a></ol></li>
<li><a href="#legacy-multi-byte-korean-encodings"><span class="secno">13 </span>Legacy multi-byte Korean encodings</a>
<ol>
<li><a href="#euc-kr"><span class="secno">13.1 </span>euc-kr</a></li>
<li><a href="#iso-2022-kr"><span class="secno">13.2 </span>iso-2022-kr</a></ol></li>
<li><a href="#legacy-miscellaneous-encodings"><span class="secno">14 </span>Legacy miscellaneous encodings</a>
<ol>
<li><a href="#replacement"><span class="secno">14.1 </span>replacement</a></li>
<li><a href="#common-infrastructure-for-utf-16be-and-utf-16le"><span class="secno">14.2 </span>Common infrastructure for <span>utf-16be</span> and <span>utf-16le</span></a></li>
<li><a href="#utf-16be"><span class="secno">14.3 </span>utf-16be</a></li>
<li><a href="#utf-16le"><span class="secno">14.4 </span>utf-16le</a></li>
<li><a href="#x-user-defined"><span class="secno">14.5 </span>x-user-defined</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 id="preface"><span class="secno">1 </span>Preface</h2>
<p>While encodings have been defined to some extent, implementations have
not always implemented them in the same way, have not always used the same
labels, and often differ in dealing with undefined and former proprietary
areas of encodings. This specification attempts to fill those gaps so that
new implementations do not have to reverse engineer encoding implementations
of the market leaders and existing implementations can converge.
<p class="note">This specification is primarily intended for dealing with
legacy content, it requires new content and formats to use the
<a href="#utf-8">utf-8</a> <a href="#encoding">encoding</a> exclusively.
<h2 id="conformance"><span class="secno">2 </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 document 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>
<p>Conformance requirements phrased as algorithms or specific steps
may be implemented in any manner, so long as the end result is
equivalent. (In particular, the algorithms defined in this
specification are intended to be easy to follow, and not intended to
be performant.)
<p id="hardwareLimitations">User agents may impose
implementation-specific limits on otherwise unconstrained inputs,
e.g. to prevent denial of service attacks, to guard against running
out of memory, or to work around platform-specific limitations.
<h2 id="terminology"><span class="secno">3 </span>Terminology</h2>
<p>Hexadecimal numbers are prefixed with "0x".
<p>In equations, all numbers are integers, addition is represented by "+",
subtraction by "−", multiplication by "×", division by "/",
calculating the remainder of a division (also known as modulo) by "%",
exponentiation by "<var title="">b</var><sup><var title="">n</var></sup>",
arithmetic left shifts by "<<", arithmetic right shifts by ">>",
bitwise AND by "&", and bitwise OR by "|".
<p>A <dfn id="byte">byte</dfn> is a sequence of eight bits, represented as a
double-digit hexadecimal number in the range 0x00 to 0xFF.
<p>A <dfn id="code-point">code point</dfn> is a Unicode code point and is represented as a
four-to-six digit hexadecimal number, typically prefixed with "U+".
In equations and <a href="#index" title="index">indexes</a> code points are prefixed
with "0x". <a href="#refsUNICODE">[UNICODE]</a>
<p>The <dfn id="ascii-whitespace">ASCII whitespace</dfn> are code points U+0009, U+000A, U+000C,
U+000D, and U+0020.
<p>A <dfn id="string">string</dfn> is a sequence of code points.
<p>Comparing two strings in an <dfn id="ascii-case-insensitive">ASCII case-insensitive</dfn> manner
means comparing them exactly, code point for code point, except that the
characters in the range U+0041 to U+005A (i.e. LATIN CAPITAL LETTER A to
LATIN CAPITAL LETTER Z) and the corresponding characters in the range
U+0061 to U+007A (i.e. LATIN SMALL LETTER A to LATIN SMALL LETTER Z) are
considered to also match.
<h2 id="encodings"><span class="secno">4 </span>Encodings</h2>
<p>An <dfn id="encoding">encoding</dfn> defines a mapping from a code point sequence to a
byte sequence (and vice versa). Each <a href="#encoding">encoding</a> has a
<dfn id="name">name</dfn>, and one or more <dfn id="label" title="label">labels</dfn>.
<p>Each <a href="#encoding">encoding</a> also has a <dfn id="decoder">decoder</dfn> and
<dfn id="encoder">encoder</dfn> algorithm.
<p>A <a href="#decoder">decoder</a> algorithm takes a byte stream and emits a
code point stream. The <dfn id="byte-pointer">byte pointer</dfn> is initially zero,
pointing to the first byte in the stream. It cannot be negative. It can be
increased and decreased to point to other bytes in the stream. The
<dfn id="eof-byte">EOF byte</dfn> is a conceptual byte representing the end of the
stream. The <a href="#byte-pointer">byte pointer</a> cannot point beyond the
<a href="#eof-byte">EOF byte</a>. The <dfn id="eof-code-point">EOF code point</dfn> is a conceptual code
point that is emitted once the byte stream is handled in its entirety.
A <dfn id="decoder-error">decoder error</dfn> indicates an error in the byte stream.
Unless stated otherwise, emitting a <a href="#decoder-error">decoder error</a> must emit
code point U+FFFD.
A <a href="#decoder">decoder</a> must be invoked again when the word continue is used,
when one or more code points are emitted of which none is the
<a href="#eof-code-point">EOF code point</a>, or when a <a href="#decoder-error">decoder error</a> is
emitted.
<p class="note">An XML processor would halt upon the first
<a href="#decoder-error">decoder error</a> emitted.
<p>An <a href="#encoder">encoder</a> algorithm takes a code point stream and emits a
byte stream. It fails when a code point is passed for which it does not have
a corresponding byte (sequence). Analogously to a <a href="#decoder">decoder</a>, it
has a <dfn id="code-point-pointer">code point pointer</dfn> and <dfn id="encoder-error">encoder error</dfn>.
An <a href="#encoder">encoder</a> must be invoked again when the word continue is
used, or when one or more bytes are emitted of which none is the
<a href="#eof-byte">EOF byte</a>. Unless stated otherwise, when an
<a href="#encoder-error">encoder error</a> is emitted the <a href="#encoder">encoder</a> terminates.
<p class="note">HTML forms and URLs require non-terminating
<a href="#encoder" title="encoder">encoders</a> and have therefore special handling
whenever an <a href="#encoder-error">encoder error</a> is reached. Instead of terminating
the <a href="#encoder">encoder</a> one or more code points in the range
U+0000 to U+007F are inserted into the code point stream at
<a href="#code-point-pointer">code point pointer</a> after <a href="#encoder-error">encoder error</a> is emitted.
<hr>
<p>The table below lists all <a href="#encoding" title="encoding">encodings</a>
and their <a href="#label" title="label">labels</a> user agents must support.
User agents must not support any other <a href="#encoding" title="encoding">encodings</a>
or <a href="#label" title="label">labels</a>.
<p>Authors must use the <a href="#utf-8">utf-8</a> <a href="#encoding">encoding</a> and must use the
"<code title="">utf-8</code>" <a href="#label">label</a> to identify it.
<p>New protocols and formats must use the <a href="#utf-8">utf-8</a> <a href="#encoding">encoding</a>
exclusively. If these protocols and formats need to expose the <a href="#encoding">encoding</a>'s
<a href="#label">label</a>, they must expose it as "<code title="">utf-8</code>".
<p>To <dfn id="concept-encoding-get" title="concept-encoding-get">get an encoding</dfn> from a string
<var title="">label</var>, run these steps:
<ol>
<li><p>Remove any leading and trailing <a href="#ascii-whitespace">ASCII whitespace</a> from
<var title="">label</var>.
<li><p>If <var title="">label</var> is an <a href="#ascii-case-insensitive">ASCII case-insensitive</a>
match for any of the <a href="#label" title="label">labels</a> listed in the table
below, return the corresponding <a href="#encoding">encoding</a>, and return failure
otherwise.
</ol>
<p class="note">In violation of
<a href="http://www.unicode.org/reports/tr22/tr22-7.html#Charset_Alias_Matching">section 1.4 of Unicode Technical Standard #22</a>
this is a much simpler and more restrictive matching algorithm, as that is
found to be necessary to be compatible with deployed content.
<table>
<thead>
<tr>
<th><a href="#name">Name</a>
<th><a href="#label" title="label">Labels</a>
<tbody>
<tr><th colspan="2"><a href="#the-encoding">The Encoding</a>
<tr>
<td rowspan="3"><a href="#utf-8">utf-8</a>
<td>"<code title="">unicode-1-1-utf-8</code>"
<tr><td>"<code title="">utf-8</code>"
<tr><td>"<code title="">utf8</code>"
<tbody>
<tr><th colspan="2"><a href="#legacy-single-byte-encodings">Legacy single-byte encodings</a>
<tr>
<td rowspan="4"><a href="#ibm866">ibm866</a>
<td>"<code title="">866</code>"
<tr><td>"<code title="">cp866</code>"
<tr><td>"<code title="">csibm866</code>"
<tr><td>"<code title="">ibm866</code>"
<tr>
<td rowspan="9"><a href="#iso-8859-2">iso-8859-2</a>
<td>"<code title="">csisolatin2</code>"
<tr><td>"<code title="">iso-8859-2</code>"
<tr><td>"<code title="">iso-ir-101</code>"
<tr><td>"<code title="">iso8859-2</code>"
<tr><td>"<code title="">iso88592</code>"
<tr><td>"<code title="">iso_8859-2</code>"
<tr><td>"<code title="">iso_8859-2:1987</code>"
<tr><td>"<code title="">l2</code>"
<tr><td>"<code title="">latin2</code>"
<tr>
<td rowspan="9"><a href="#iso-8859-3">iso-8859-3</a>
<td>"<code title="">csisolatin3</code>"
<tr><td>"<code title="">iso-8859-3</code>"
<tr><td>"<code title="">iso-ir-109</code>"
<tr><td>"<code title="">iso8859-3</code>"
<tr><td>"<code title="">iso88593</code>"
<tr><td>"<code title="">iso_8859-3</code>"
<tr><td>"<code title="">iso_8859-3:1988</code>"
<tr><td>"<code title="">l3</code>"
<tr><td>"<code title="">latin3</code>"
<tr>
<td rowspan="9"><a href="#iso-8859-4">iso-8859-4</a>
<td>"<code title="">csisolatin4</code>"
<tr><td>"<code title="">iso-8859-4</code>"
<tr><td>"<code title="">iso-ir-110</code>"
<tr><td>"<code title="">iso8859-4</code>"
<tr><td>"<code title="">iso88594</code>"
<tr><td>"<code title="">iso_8859-4</code>"
<tr><td>"<code title="">iso_8859-4:1988</code>"
<tr><td>"<code title="">l4</code>"
<tr><td>"<code title="">latin4</code>"
<tr>
<td rowspan="8"><a href="#iso-8859-5">iso-8859-5</a>
<td>"<code title="">csisolatincyrillic</code>"
<tr><td>"<code title="">cyrillic</code>"
<tr><td>"<code title="">iso-8859-5</code>"
<tr><td>"<code title="">iso-ir-144</code>"
<tr><td>"<code title="">iso8859-5</code>"
<tr><td>"<code title="">iso88595</code>"
<tr><td>"<code title="">iso_8859-5</code>"
<tr><td>"<code title="">iso_8859-5:1988</code>"
<tr>
<td rowspan="14"><a href="#iso-8859-6">iso-8859-6</a>
<td>"<code title="">arabic</code>"
<tr><td>"<code title="">asmo-708</code>"
<tr><td>"<code title="">csiso88596e</code>"
<tr><td>"<code title="">csiso88596i</code>"
<tr><td>"<code title="">csisolatinarabic</code>"
<tr><td>"<code title="">ecma-114</code>"
<tr><td>"<code title="">iso-8859-6</code>"
<tr><td>"<code title="">iso-8859-6-e</code>"
<tr><td>"<code title="">iso-8859-6-i</code>"
<tr><td>"<code title="">iso-ir-127</code>"
<tr><td>"<code title="">iso8859-6</code>"
<tr><td>"<code title="">iso88596</code>"
<tr><td>"<code title="">iso_8859-6</code>"
<tr><td>"<code title="">iso_8859-6:1987</code>"
<tr>
<td rowspan="12"><a href="#iso-8859-7">iso-8859-7</a>
<td>"<code title="">csisolatingreek</code>"
<tr><td>"<code title="">ecma-118</code>"
<tr><td>"<code title="">elot_928</code>"
<tr><td>"<code title="">greek</code>"
<tr><td>"<code title="">greek8</code>"
<tr><td>"<code title="">iso-8859-7</code>"
<tr><td>"<code title="">iso-ir-126</code>"
<tr><td>"<code title="">iso8859-7</code>"
<tr><td>"<code title="">iso88597</code>"
<tr><td>"<code title="">iso_8859-7</code>"
<tr><td>"<code title="">iso_8859-7:1987</code>"
<tr><td>"<code title="">sun_eu_greek</code>"
<tr>
<td rowspan="11"><a href="#iso-8859-8">iso-8859-8</a>
<td>"<code title="">csiso88598e</code>"
<tr><td>"<code title="">csisolatinhebrew</code>"
<tr><td>"<code title="">hebrew</code>"
<tr><td>"<code title="">iso-8859-8</code>"
<tr><td>"<code title="">iso-8859-8-e</code>"
<tr><td>"<code title="">iso-ir-138</code>"
<tr><td>"<code title="">iso8859-8</code>"
<tr><td>"<code title="">iso88598</code>"
<tr><td>"<code title="">iso_8859-8</code>"
<tr><td>"<code title="">iso_8859-8:1988</code>"
<tr><td>"<code title="">visual</code>"
<tr>
<td rowspan="3"><a href="#iso-8859-8-i">iso-8859-8-i</a>
<td>"<code title="">csiso88598i</code>"
<tr><td>"<code title="">iso-8859-8-i</code>"
<tr><td>"<code title="">logical</code>"
<tr>
<td rowspan="7"><a href="#iso-8859-10">iso-8859-10</a>
<td>"<code title="">csisolatin6</code>"
<tr><td>"<code title="">iso-8859-10</code>"
<tr><td>"<code title="">iso-ir-157</code>"
<tr><td>"<code title="">iso8859-10</code>"
<tr><td>"<code title="">iso885910</code>"
<tr><td>"<code title="">l6</code>"
<tr><td>"<code title="">latin6</code>"
<tr>
<td rowspan="3"><a href="#iso-8859-13">iso-8859-13</a>
<td>"<code title="">iso-8859-13</code>"
<tr><td>"<code title="">iso8859-13</code>"
<tr><td>"<code title="">iso885913</code>"
<tr>
<td rowspan="3"><a href="#iso-8859-14">iso-8859-14</a>
<td>"<code title="">iso-8859-14</code>"
<tr><td>"<code title="">iso8859-14</code>"
<tr><td>"<code title="">iso885914</code>"
<tr>
<td rowspan="6"><a href="#iso-8859-15">iso-8859-15</a>
<td>"<code title="">csisolatin9</code>"
<tr><td>"<code title="">iso-8859-15</code>"
<tr><td>"<code title="">iso8859-15</code>"
<tr><td>"<code title="">iso885915</code>"
<tr><td>"<code title="">iso_8859-15</code>"
<tr><td>"<code title="">l9</code>"
<tr>
<td><a href="#iso-8859-16">iso-8859-16</a>
<td>"<code title="">iso-8859-16</code>"
<tr>
<td rowspan="5"><a href="#koi8-r">koi8-r</a>
<td>"<code title="">cskoi8r</code>"
<tr><td>"<code title="">koi</code>"
<tr><td>"<code title="">koi8</code>"
<tr><td>"<code title="">koi8-r</code>"
<tr><td>"<code title="">koi8_r</code>"
<tr>
<td><a href="#koi8-u">koi8-u</a>
<td>"<code title="">koi8-u</code>"
<tr>
<td rowspan="4"><a href="#macintosh">macintosh</a>
<td>"<code title="">csmacintosh</code>"
<tr><td>"<code title="">mac</code>"
<tr><td>"<code title="">macintosh</code>"
<tr><td>"<code title="">x-mac-roman</code>"
<tr>
<td rowspan="6"><a href="#windows-874">windows-874</a>
<td>"<code title="">dos-874</code>"
<tr><td>"<code title="">iso-8859-11</code>"
<tr><td>"<code title="">iso8859-11</code>"
<tr><td>"<code title="">iso885911</code>"
<tr><td>"<code title="">tis-620</code>"
<tr><td>"<code title="">windows-874</code>"
<tr>
<td rowspan="3"><a href="#windows-1250">windows-1250</a>
<td>"<code title="">cp1250</code>"
<tr><td>"<code title="">windows-1250</code>"
<tr><td>"<code title="">x-cp1250</code>"
<tr>
<td rowspan="3"><a href="#windows-1251">windows-1251</a>
<td>"<code title="">cp1251</code>"
<tr><td>"<code title="">windows-1251</code>"
<tr><td>"<code title="">x-cp1251</code>"
<tr>
<td rowspan="17"><a href="#windows-1252">windows-1252</a>
<td>"<code title="">ansi_x3.4-1968</code>"
<tr><td>"<code title="">ascii</code>"
<tr><td>"<code title="">cp1252</code>"
<tr><td>"<code title="">cp819</code>"
<tr><td>"<code title="">csisolatin1</code>"
<tr><td>"<code title="">ibm819</code>"
<tr><td>"<code title="">iso-8859-1</code>"
<tr><td>"<code title="">iso-ir-100</code>"
<tr><td>"<code title="">iso8859-1</code>"
<tr><td>"<code title="">iso88591</code>"
<tr><td>"<code title="">iso_8859-1</code>"
<tr><td>"<code title="">iso_8859-1:1987</code>"
<tr><td>"<code title="">l1</code>"
<tr><td>"<code title="">latin1</code>"
<tr><td>"<code title="">us-ascii</code>"
<tr><td>"<code title="">windows-1252</code>"
<tr><td>"<code title="">x-cp1252</code>"
<tr>
<td rowspan="3"><a href="#windows-1253">windows-1253</a>
<td>"<code title="">cp1253</code>"
<tr><td>"<code title="">windows-1253</code>"
<tr><td>"<code title="">x-cp1253</code>"
<tr>
<td rowspan="12"><a href="#windows-1254">windows-1254</a>
<td>"<code title="">cp1254</code>"
<tr><td>"<code title="">csisolatin5</code>"
<tr><td>"<code title="">iso-8859-9</code>"
<tr><td>"<code title="">iso-ir-148</code>"
<tr><td>"<code title="">iso8859-9</code>"
<tr><td>"<code title="">iso88599</code>"
<tr><td>"<code title="">iso_8859-9</code>"
<tr><td>"<code title="">iso_8859-9:1989</code>"
<tr><td>"<code title="">l5</code>"
<tr><td>"<code title="">latin5</code>"
<tr><td>"<code title="">windows-1254</code>"
<tr><td>"<code title="">x-cp1254</code>"
<tr>
<td rowspan="3"><a href="#windows-1255">windows-1255</a>
<td>"<code title="">cp1255</code>"
<tr><td>"<code title="">windows-1255</code>"
<tr><td>"<code title="">x-cp1255</code>"
<tr>
<td rowspan="3"><a href="#windows-1256">windows-1256</a>
<td>"<code title="">cp1256</code>"
<tr><td>"<code title="">windows-1256</code>"
<tr><td>"<code title="">x-cp1256</code>"
<tr>
<td rowspan="3"><a href="#windows-1257">windows-1257</a>
<td>"<code title="">cp1257</code>"
<tr><td>"<code title="">windows-1257</code>"
<tr><td>"<code title="">x-cp1257</code>"
<tr>
<td rowspan="3"><a href="#windows-1258">windows-1258</a>
<td>"<code title="">cp1258</code>"
<tr><td>"<code title="">windows-1258</code>"
<tr><td>"<code title="">x-cp1258</code>"
<tr>
<td rowspan="2"><a href="#x-mac-cyrillic">x-mac-cyrillic</a>
<td>"<code title="">x-mac-cyrillic</code>"
<tr><td>"<code title="">x-mac-ukrainian</code>"
<tbody>
<tr><th colspan="2"><a href="#legacy-multi-byte-chinese-(simplified)-encodings">Legacy multi-byte Chinese (simplified) encodings</a>
<tr>
<td rowspan="9"><a href="#gbk">gbk</a>
<td>"<code title="">chinese</code>"
<tr><td>"<code title="">csgb2312</code>"
<tr><td>"<code title="">csiso58gb231280</code>"
<tr><td>"<code title="">gb2312</code>"
<tr><td>"<code title="">gb_2312</code>"
<tr><td>"<code title="">gb_2312-80</code>"
<tr><td>"<code title="">gbk</code>"
<tr><td>"<code title="">iso-ir-58</code>"
<tr><td>"<code title="">x-gbk</code>"
<tr>
<td><a href="#gb18030">gb18030</a>
<td>"<code title="">gb18030</code>"
<tr>
<td><a href="#hz-gb-2312">hz-gb-2312</a>
<td>"<code title="">hz-gb-2312</code>"
<tbody>
<tr><th colspan="2"><a href="#legacy-multi-byte-chinese-(traditional)-encodings">Legacy multi-byte Chinese (traditional) encodings</a>
<tr>
<td rowspan="5"><a href="#big5">big5</a>
<td>"<code title="">big5</code>"
<tr><td>"<code title="">big5-hkscs</code>"
<tr><td>"<code title="">cn-big5</code>"
<tr><td>"<code title="">csbig5</code>"
<tr><td>"<code title="">x-x-big5</code>"
<tbody>
<tr><th colspan="2"><a href="#legacy-multi-byte-japanese-encodings">Legacy multi-byte Japanese encodings</a>
<tr>
<td rowspan="3"><a href="#euc-jp">euc-jp</a>
<td>"<code title="">cseucpkdfmtjapanese</code>"
<tr><td>"<code title="">euc-jp</code>"
<tr><td>"<code title="">x-euc-jp</code>"
<tr>
<td rowspan="2"><a href="#iso-2022-jp">iso-2022-jp</a>
<td>"<code title="">csiso2022jp</code>"
<tr><td>"<code title="">iso-2022-jp</code>"
<tr>
<td rowspan="7"><a href="#shift_jis">shift_jis</a>
<td>"<code title="">csshiftjis</code>"
<tr><td>"<code title="">ms_kanji</code>"
<tr><td>"<code title="">shift-jis</code>"
<tr><td>"<code title="">shift_jis</code>"
<tr><td>"<code title="">sjis</code>"
<tr><td>"<code title="">windows-31j</code>"
<tr><td>"<code title="">x-sjis</code>"
<tbody>
<tr><th colspan="2"><a href="#legacy-multi-byte-korean-encodings">Legacy multi-byte Korean encodings</a>
<tr>
<td rowspan="10"><a href="#euc-kr">euc-kr</a>
<td>"<code title="">cseuckr</code>"
<tr><td>"<code title="">csksc56011987</code>"
<tr><td>"<code title="">euc-kr</code>"
<tr><td>"<code title="">iso-ir-149</code>"
<tr><td>"<code title="">korean</code>"
<tr><td>"<code title="">ks_c_5601-1987</code>"
<tr><td>"<code title="">ks_c_5601-1989</code>"
<tr><td>"<code title="">ksc5601</code>"
<tr><td>"<code title="">ksc_5601</code>"
<tr><td>"<code title="">windows-949</code>"
<tr>
<td rowspan="2"><a href="#iso-2022-kr">iso-2022-kr</a>
<td>"<code title="">csiso2022kr</code>"
<tr><td>"<code title="">iso-2022-kr</code>"
<tbody>
<tr><th colspan="2"><a href="#legacy-miscellaneous-encodings">Legacy miscellaneous encodings</a>
<tr>
<td rowspan="2"><a href="#replacement">replacement</a>
<td>"<code title="">iso-2022-cn</code>"
<tr><td>"<code title="">iso-2022-cn-ext</code>"
<tr>
<td><a href="#utf-16be">utf-16be</a>
<td>"<code title="">utf-16be</code>"
<tr>
<td rowspan="2"><a href="#utf-16le">utf-16le</a>
<td>"<code title="">utf-16</code>"
<tr><td>"<code title="">utf-16le</code>"
<tr>
<td><a href="#x-user-defined">x-user-defined</a>
<td>"<code title="">x-user-defined</code>"
</table>
<p class="note">All <a href="#encoding" title="encoding">encodings</a> and their
<a href="#label" title="label">labels</a> are also available as non-normative
<a href="encodings.json">encodings.json</a> resource.
<h2 id="indexes"><span class="secno">5 </span>Indexes</h2>
<p>Most legacy <a href="#encoding" title="encoding">encodings</a> make use of
an <dfn id="index">index</dfn>. An <a href="#index">index</a> is an ordered list of
pointers and corresponding code points. Within an <a href="#index">index</a>
pointers are unique and code points can be duplicated.
<p>To find the pointers and their corresponding code points in an <a href="#index">index</a>,
let <var title="">lines</var> be the result of splitting the resource's contents on U+000A.
Then remove each item in <var title="">lines</var> that is the empty string or starts with U+0023.
Then the pointers and their corresponding code points are found by splitting each item in <var title="">lines</var> on U+0009.
The first subitem is the pointer (as a decimal number) and the second is the corresponding code point (as a hexadecimal number).
Other subitems are not relevant.
<p>The <dfn id="index-code-point">index code point</dfn> for <var title="">pointer</var> in
<var title="">index</var> is the code point corresponding to
<var title="">pointer</var> in <var title="">index</var>, or null if
<var title="">pointer</var> is not in <var title="">index</var>.
<p>The <dfn id="index-pointer">index pointer</dfn> for <var title="">code point</var> in
<var title="">index</var> is the <em>first</em> pointer corresponding to
<var title="">code point</var> in <var title="">index</var>, or null if
<var title="">code point</var> is not in <var title="">index</var>.
<p>These are the <a href="#index" title="index">indexes</a> defined by this
specification, excluding <a href="#index-single-byte">index single-byte</a>:
<table>
<tr><th colspan="2"><a href="#index">Index</a><th>Notes
<tr>
<td><dfn id="index-big5">index big5</dfn>
<td><a href="index-big5.txt">index-big5.txt</a>
<td>This matches the Big5 standard in combination with the
Hong Kong Supplementary Character Set and other common extensions.
<tr>
<td><dfn id="index-euc-kr">index euc-kr</dfn>
<td><a href="index-euc-kr.txt">index-euc-kr.txt</a>
<td>This matches the KS X 1001 standard and the Unified Hangul Code, more
commonly known together as Windows Codepage 949.
<tr>
<td><dfn id="index-gbk">index gbk</dfn>
<td><a href="index-gbk.txt">index-gbk.txt</a>
<td>This matches the GB18030 standard for code points encoded as two
bytes.
<tr>
<td><dfn id="index-gb18030">index gb18030</dfn>
<td><a href="index-gb18030.txt">index-gb18030.txt</a>
<td>This <a href="#index">index</a> works different from all others. Listing all
code points would result in over a million items whereas they can be
represented neatly in 207 ranges combined with trivial limit checks. It
therefore only superficially matches the GB18030 standard for code points
encoded as four bytes. See also <a href="#index-gb18030-code-point">index gb18030 code point</a> and
<a href="#index-gb18030-pointer">index gb18030 pointer</a> below.
<tr>
<td><dfn id="index-jis0208">index jis0208</dfn>
<td><a href="index-jis0208.txt">index-jis0208.txt</a>
<td>This is the JIS X 0208 standard including formerly proprietary
extensions from IBM and NEC.
<!-- NEC = Nippon Electronics Corporation -->
<tr>
<td><dfn id="index-jis0212">index jis0212</dfn>
<td><a href="index-jis0212.txt">index-jis0212.txt</a>
<td>This is the JIS X 0212 standard.
</table>
<p>The <dfn id="index-gb18030-code-point">index gb18030 code point</dfn> for <var title="">pointer</var> is
the return value of these steps:
<ol>
<li><p>If <var title="">pointer</var> is greater than 39419 and less than
189000, or <var title="">pointer</var> is greater than 1237575, return null.
<li><p>Let <var title="">offset</var> be the last pointer in
<a href="#index-gb18030">index gb18030</a> that is equal to or less than
<var title="">pointer</var> and let <var title="">code point offset</var> be its
corresponding code point.
<li><p>Return a code point whose value is
<var title="">code point offset</var> + <var title="">pointer</var> − <var title="">offset</var>.
</ol>
<p>The <dfn id="index-gb18030-pointer">index gb18030 pointer</dfn> for <var title="">code point</var> is
the return value of these steps:
<ol>
<li><p>Let <var title="">offset</var> be the last code point in
<a href="#index-gb18030">index gb18030</a> that is equal to or less than
<var title="">code point</var> and let <var title="">pointer offset</var> be its
corresponding pointer.
<li><p>Return a pointer whose value is
<var title="">pointer offset</var> + <var title="">code point</var> − <var title="">offset</var>.
</ol>
<p class="note">All <a href="#index" title="index">indexes</a> are also available as
non-normative <a href="indexes.json">indexes.json</a> resource.
(<a href="#index-gb18030">index gb18030</a> has a slightly different format here, to be able
to represent ranges.)
<h2 id="decode-and-encode"><span class="secno">6 </span>Decode and encode</h2>
<p class="note">The algorithms <a href="#decode">decode</a>, <a href="#utf-8-decode">utf-8 decode</a>,
and <a href="#encode">encode</a> are intended for usage by other specifications.
<a href="#utf-8-decode">utf-8 decode</a> is to be used by new formats. The
<a href="#concept-encoding-get" title="concept-encoding-get">get an encoding</a> algorithm can be
used first to turn a <a href="#label">label</a> into an <a href="#encoding">encoding</a>.
<p>To <dfn id="decode">decode</dfn> a byte stream <var title="">stream</var> using
fallback encoding <var title="">encoding</var>, run these steps:
<ol>
<li><p>Let <var title="">offset</var> be 0.
<li>
<p>For each of the rows in the following table, starting with the first
one and going down, if the first bytes of <var title="">stream</var> match
all the bytes given in the first column (ergo <var title="">stream</var>
contains at least two or three bytes), then set <var title="">encoding</var>
to the <a href="#encoding">encoding</a> given in the cell in the second column of
that row, and set <var title="">offset</var> to the offset given in the cell
in the third column of that row.
<table>
<tr><th>Byte order mark<th>Encoding<th>Offset
<tr><td>0xFF 0xFE<td><a href="#utf-16le">utf-16le</a><td>2
<tr><td>0xFE 0xFF<td><a href="#utf-16be">utf-16be</a><td>2
<tr><td>0xEF 0xBB 0xBF<td><a href="#utf-8">utf-8</a><td>3
</table>
<p class="note">For compatibility with deployed content, the byte order mark
(also known as BOM) is considered more authoritative than anything else.
<li><p>Return the result of running <var title="">encoding</var>'s
<a href="#decoder">decoder</a> with <a href="#byte-pointer">byte pointer</a> set to
<var title="">offset</var>, on <var title="">stream</var>.
</ol>
<p>To <dfn id="utf-8-decode">utf-8 decode</dfn> a byte stream <var title="">stream</var>, run
these steps:
<ol>
<li><p>Let <var title="">offset</var> be 0.
<li><p>If <var title="">stream</var> contains at least three bytes and its
first three bytes match 0xEF 0xBB 0xBF, set <var title="">offset</var> to 3.
<li><p>Return the result of running the <a href="#utf-8-decoder">utf-8 decoder</a> with
<a href="#byte-pointer">byte pointer</a> set to <var title="">offset</var>, on
<var title="">stream</var>.
</ol>
<hr>
<p>To <dfn id="encode">encode</dfn> a code point stream <var title="">stream</var> using
encoding <var title="">encoding</var>, return the result of running
<var title="">encoding</var>'s <a href="#encoder">encoder</a> on <var title="">stream</var>.
<p>To <dfn id="utf-8-encode">utf-8 encode</dfn> a code point stream <var title="">stream</var>,
return the result of <a href="#encode" title="encode">encoding</a>
<var title="">stream</var> using encoding <a href="#utf-8">utf-8</a>.
<p class="note">If the input to this algorithm stems from a DOMString, the
<a class="external" data-anolis-spec="webidl" href="http://dev.w3.org/2006/webapi/WebIDL/#dfn-obtain-unicode">convert a DOMString to a sequence of Unicode characters</a>
from Web IDL is to be used first.
<h2 id="api"><span class="secno">7 </span>API</h2>
<!-- http://wiki.whatwg.org/wiki/StringEncoding -->
<p>This section uses terminology from the DOM, Typed Arrays, and Web IDL.
Non-browser implementations are not required to implement this API.
<a href="#refsDOM">[DOM]</a>
<a href="#refsTYPEDARRAY">[TYPEDARRAY]</a>
<a href="#refsWEBIDL">[WEBIDL]</a>
<div class="example">
<p>The following example uses the <a href="#textencoder"><code>TextEncoder</code></a> object to encode
an array of strings into an
<a href="http://www.khronos.org/registry/typedarray/specs/latest/#5"><code class="external" data-anolis-spec="typedarray">ArrayBuffer</code></a>. The result is a
<a href="http://www.khronos.org/registry/typedarray/specs/latest/#7"><code class="external" data-anolis-spec="typedarray">Uint8Array</code></a> containing the number
of strings (as a <a href="http://www.khronos.org/registry/typedarray/specs/latest/#7"><code class="external" data-anolis-spec="typedarray">Uint32Array</code></a>),
followed by the length of the first string (as a
<a href="http://www.khronos.org/registry/typedarray/specs/latest/#7"><code class="external" data-anolis-spec="typedarray">Uint32Array</code></a>), the
<a href="#utf-8">utf-8</a> encoded string data, the length of the second string (as
a <a href="http://www.khronos.org/registry/typedarray/specs/latest/#7"><code class="external" data-anolis-spec="typedarray">Uint32Array</code></a>), the string data,
and so on.
<pre><code>function encodeArrayOfStrings(strings, encoding) {
var encoder, encoded, len, i, bytes, view, offset;
encoder = TextEncoder(encoding);
encoded = [];
len = Uint32Array.BYTES_PER_ELEMENT;
for (i = 0; i < strings.length; i += 1) {
len += Uint32Array.BYTES_PER_ELEMENT;
encoded[i] = TextEncoder(encoding).encode(strings[i]);
len += encoded[i].byteLength;
}
bytes = new Uint8Array(len);
view = new DataView(bytes.buffer);
offset = 0;
view.setUint32(offset, strings.length);
offset += Uint32Array.BYTES_PER_ELEMENT;
for (i = 0; i < encoded.length; i += 1) {
len = encoded[i].byteLength;
view.setUint32(offset, len);
offset += Uint32Array.BYTES_PER_ELEMENT;
bytes.set(encoded[i], offset);
offset += len;
}
return bytes.buffer;
}</code></pre>
<p>The following example decodes an
<a href="http://www.khronos.org/registry/typedarray/specs/latest/#5"><code class="external" data-anolis-spec="typedarray">ArrayBuffer</code></a> containing data
encoded in the format produced by the previous example back into an array
of strings.
<pre><code>function decodeArrayOfStrings(buffer, encoding) {
var decoder, view, offset, num_strings, strings, i, len;
decoder = TextDecoder(encoding);
view = new DataView(buffer);
offset = 0;
strings = [];
num_strings = view.getUint32(offset);
offset += Uint32Array.BYTES_PER_ELEMENT;
for (i = 0; i < num_strings; i += 1) {
len = view.getUint32(offset);
offset += Uint32Array.BYTES_PER_ELEMENT;
strings[i] = decoder.decode(
new DataView(view.buffer, offset, len));
offset += len;
}
return strings;
}</code></pre>
</div>
<h3 id="interface-textdecoder"><span class="secno">7.1 </span>Interface <code title="">TextDecoder</code></h3>
<pre class="idl">dictionary <dfn id="textdecoderoptions">TextDecoderOptions</dfn> {
boolean fatal = false;
};
dictionary <dfn id="textdecodeoptions">TextDecodeOptions</dfn> {
boolean stream = false;
};
[<a href="#dom-textdecoder" title="dom-TextDecoder">Constructor</a>(optional DOMString <var title="">label</var> = "utf-8", optional <a href="#textdecoderoptions">TextDecoderOptions</a> <var title="">options</var>)]
interface <dfn id="textdecoder">TextDecoder</dfn> {
readonly attribute DOMString <a href="#dom-textdecoder-encoding" title="dom-TextDecoder-encoding">encoding</a>;
DOMString <a href="#dom-textdecoder-decode" title="dom-TextDecoder-decode">decode</a>();
DOMString <a href="#dom-textdecoder-decode" title="dom-TextDecoder-decode">decode</a>(<a class="external" data-anolis-spec="typedarray" href="http://www.khronos.org/registry/typedarray/specs/latest/#6">ArrayBufferView</a> <var title="">input</var>, optional <a href="#textdecodeoptions">TextDecodeOptions</a> <var title="">options</var>);
};</pre>
<p>A <a href="#textdecoder"><code>TextDecoder</code></a> object has an associated
<b>encoding</b>, <b>encoding state</b>, <b>stream</b>,
<b>BOM seen flag</b> (initially unset), <b>fatal flag</b> (initially unset)
and <b>streaming flag</b> (initially unset).
<dl class="domintro">
<dt><code><var title="">decoder</var> = new <a href="#dom-textdecoder" title="dom-TextDecoder">TextDecoder</a>([<var title="">label</var> = "utf-8" [, <var title="">options</var>]])</code>
<dd>
<p>Returns a new <a href="#textdecoder"><code>TextDecoder</code></a> object.
<p>If <var title="">label</var> is either not a <a href="#label">label</a> or is a
<a href="#label">label</a> for <a href="#replacement">replacement</a>,
<a class="external" data-anolis-spec="webidl" href="http://dev.w3.org/2006/webapi/WebIDL/#dfn-throw" title="throw">throws</a> a
<code>TypeError</code>.
<dt><code><var title="">decoder</var> . <a href="#dom-textdecoder-encoding" title="dom-TextDecoder-encoding">encoding</a></code>
<dd><p>Returns <b>encoding</b>'s <a href="#name">name</a>.
<dt><code><var title="">decoder</var> . <a href="#dom-textdecoder-decode" title="dom-TextDecoder-decode">decode</a>([<var title="">input</var> [, <var title="">options</var>]])</code>
<dd>
<p>Returns the result of running <b>encoding</b>'s
<a href="#decoder">decoder</a>. If <var title="">options</var>'s
<code title="">stream</code> is set to true the method can be invoked
multiple times to process a fragmented stream.
<p>If the <b>fatal flag</b> is set and <b>encoding</b>'s
<a href="#decoder">decoder</a> emits a <a href="#decoder-error">decoder error</a>,
<a class="external" data-anolis-spec="dom" href="http://dom.spec.whatwg.org/#concept-throw" title="concept-throw">throws</a> an
"<a href="http://dom.spec.whatwg.org/#encodingerror"><code class="external" data-anolis-spec="dom">EncodingError</code></a>".
</dl>
<p>The
<dfn id="dom-textdecoder" title="dom-TextDecoder"><code>TextDecoder(<var title="">label</var>, <var title="">options</var>)</code></dfn>
constructor must run these steps:
<ol>
<li><p>Let <var title="">encoding</var> be the result of
<a href="#concept-encoding-get" title="concept-encoding-get">getting an encoding</a> from
<var title="">label</var>.
<li><p>If <var title="">encoding</var> is failure or <a href="#replacement">replacement</a>,
<a class="external" data-anolis-spec="webidl" href="http://dev.w3.org/2006/webapi/WebIDL/#dfn-throw">throw</a> a <code>TypeError</code>.
<li><p>Let <var title="">dec</var> be a new <a href="#textdecoder"><code>TextDecoder</code></a> object.
<li><p>Set <var title="">dec</var>'s <b>encoding</b> to
<var title="">encoding</var>.
<li><p>Set <var title="">dec</var>'s <b>encoding state</b> to the default
values of <var title="">dec</var>'s <b>encoding</b>'s <a href="#decoder">decoder</a>'s
associated variables.
<li><p>If <var title="">options</var>'s <code title="">fatal</code> member is
true, set <var title="">dec</var>'s <b>fatal flag</b>.
<li><p>Return <var title="">dec</var>.
</ol>
<p>The <dfn id="dom-textdecoder-encoding" title="dom-TextDecoder-encoding"><code>encoding</code></dfn>
attribute must return <b>encoding</b>'s <a href="#name">name</a>.
<p>The
<dfn id="dom-textdecoder-decode" title="dom-TextDecoder-decode"><code>decode(<var title="">input</var>, <var title="">options</var>)</code></dfn>
method must run these steps:
<ol>
<li><p>If the <b>streaming flag</b> is unset, set the <b>encoding state</b>
to the default values of the <b>encoding</b>'s <a href="#decoder">decoder</a>'s
associated variables and empty the <b>stream</b>.
<li><p>If <var title="">options</var>'s <code title="">stream</code> is true,
set the <b>streaming flag</b>, and unset the <b>streaming flag</b>
otherwise.
<li><p>If <var title="">input</var> is given, then given
<var title="">input</var>'s <code title="">buffer</code>,
<code title="">byteOffset</code>, and <code title="">byteLength</code>, append
<code title="">byteLength</code> bytes from <code title="">buffer</code>,
starting at <code title="">byteOffset</code>, to the <b>stream</b>.
<!-- TYPEDARRAY really needs to make this simpler -->
<li>
<p>If the <b>BOM seen flag</b> is unset, and the <b>stream</b> either
holds at least two bytes, or at least three bytes if the <b>encoding</b>
is <a href="#utf-8">utf-8</a>, then set the <b>BOM seen flag</b>, and for each of
the rows in the following table, starting with the first one and going
down, if the first bytes of the <b>stream</b> match all the bytes given in
the first column, and the <b>encoding</b> matches the
<a href="#encoding">encoding</a> given in the cell in the second column of that row,
then remove those bytes at the start of the <b>stream</b>.
<table>
<tr><th>Byte order mark<th>Encoding
<tr><td>0xFF 0xFE<td><a href="#utf-16le">utf-16le</a>
<tr><td>0xFE 0xFF<td><a href="#utf-16be">utf-16be</a>
<tr><td>0xEF 0xBB 0xBF<td><a href="#utf-8">utf-8</a>
</table>
<p class="note">This algorithm is intentionally different from the
<a href="#decode">decode</a> algorithm used by the rest of the platform to give API
users more control.
<li><p>If the <b>streaming flag</b> is unset, append the
<a href="#eof-byte">EOF byte</a> to the <b>stream</b>.
<li>
<p>Return the output of running <b>encoding</b>'s <a href="#decoder">decoder</a> on
the <b>stream</b>. If the <b>fatal flag</b> is set and
<b>encoding</b>'s <a href="#decoder">decoder</a> emits a <a href="#decoder-error">decoder error</a>,
<a class="external" data-anolis-spec="dom" href="http://dom.spec.whatwg.org/#concept-throw" title="concept-throw">throw</a> a
"<code title="">EncodingError</code>".
<p class="note">In addition to the reason given above with respect to the
byte order mark, this also does not use the <a href="#encode">encode</a> algorithm
as it assumes a continuous stream rather than one delivered in fragments.
</ol>
<h3 id="interface-textencoder"><span class="secno">7.2 </span>Interface <code title="">TextEncoder</code></h3>
<pre class="idl">dictionary <dfn id="textencodeoptions">TextEncodeOptions</dfn> {
boolean stream = false;
};
[<a href="#dom-textencoder" title="dom-TextEncoder">Constructor</a>(optional DOMString <var title="">utfLabel</var> = "utf-8")]
interface <dfn id="textencoder">TextEncoder</dfn> {
readonly attribute DOMString <a href="#dom-textencoder-encoding" title="dom-TextEncoder-encoding">encoding</a>;
<a class="external" data-anolis-spec="typedarray" href="http://www.khronos.org/registry/typedarray/specs/latest/#7">Uint8Array</a> <a href="#dom-textencoder-encode" title="dom-TextEncoder-encode">encode</a>(optional DOMString <var title="">input</var> = "", optional <a href="#textencodeoptions">TextEncodeOptions</a> <var title="">options</var>);
};</pre>
<p>A <a href="#textencoder"><code>TextEncoder</code></a> object has an associated
<b>encoding</b>, <b>encoding state</b>, <b>stream</b>, and
<b>streaming flag</b> (initially unset).
<dl class="domintro">
<dt><code><var title="">encoder</var> = new <a href="#dom-textencoder" title="dom-TextEncoder">TextEncoder</a>([<var title="">utfLabel</var> = "utf-8"])</code>
<dd>
<p>Returns a new <a href="#textencoder"><code>TextEncoder</code></a> object.
<p>If <var title="">utfLabel</var> is not a <a href="#label">label</a> for
<a href="#utf-8">utf-8</a>, <a href="#utf-16le">utf-16le</a>, or <a href="#utf-16be">utf-16be</a>,
<a class="external" data-anolis-spec="webidl" href="http://dev.w3.org/2006/webapi/WebIDL/#dfn-throw" title="throw">throws</a> a
<code>TypeError</code>.
<dt><code><var title="">encoder</var> . <a href="#dom-textencoder-encoding" title="dom-TextEncoder-encoding">encoding</a></code>
<dd><p>Returns <b>encoding</b>'s <a href="#name">name</a>.
<dt><code><var title="">encoder</var> . <a href="#dom-textencoder-encode" title="dom-TextEncoder-encode">encode</a>([<var title="">input</var> [, <var title="">options</var>]])</code>
<dd><p>Returns the result of running <b>encoding</b>'s
<a href="#encoder">encoder</a>. If <var title="">options</var>'s
<code title="">stream</code> is set to true, the method can be invoked
multiple times to process a fragmented stream.
</dl>
<p>The
<dfn id="dom-textencoder" title="dom-TextEncoder"><code>TextEncoder(<var title="">utfLabel</var>)</code></dfn>
constructor must run these steps:
<ol>
<li><p>Let <var title="">encoding</var> be the result of
<a href="#concept-encoding-get" title="concept-encoding-get">getting an encoding</a> from
<var title="">utfLabel</var>.
<li><p>If <var title="">encoding</var> is failure, or is none of