OLD | NEW |
| (Empty) |
1 <!DOCTYPE html> | |
2 <title>Encoding API: Basics</title> | |
3 <script src="../../../resources/testharness.js"></script> | |
4 <script src="../../../resources/testharnessreport.js"></script> | |
5 <script> | |
6 | |
7 test(function() { | |
8 assert_equals((new TextEncoder).encoding, 'utf-8', 'default encoding is utf-
8'); | |
9 assert_equals((new TextDecoder).encoding, 'utf-8', 'default encoding is utf-
8'); | |
10 }, 'Default encodings'); | |
11 | |
12 test(function() { | |
13 assert_array_equals(new TextEncoder().encode(), [], 'input default should be
empty string') | |
14 assert_array_equals(new TextEncoder().encode(undefined), [], 'input default
should be empty string') | |
15 }, 'Default inputs'); | |
16 | |
17 | |
18 function testDecodeSample(encoding, string, bytes) { | |
19 test(function() { | |
20 assert_equals(new TextDecoder(encoding).decode(new Uint8Array(bytes)), strin
g); | |
21 assert_equals(new TextDecoder(encoding).decode(new Uint8Array(bytes).buffer)
, string); | |
22 }, 'Decode sample: ' + encoding); | |
23 } | |
24 | |
25 // z (ASCII U+007A), cent (Latin-1 U+00A2), CJK water (BMP U+6C34), | |
26 // G-Clef (non-BMP U+1D11E), PUA (BMP U+F8FF), PUA (non-BMP U+10FFFD) | |
27 // byte-swapped BOM (non-character U+FFFE) | |
28 var sample = 'z\xA2\u6C34\uD834\uDD1E\uF8FF\uDBFF\uDFFD\uFFFE'; | |
29 | |
30 test(function() { | |
31 var encoding = 'utf-8'; | |
32 var string = sample; | |
33 var bytes = [0x7A, 0xC2, 0xA2, 0xE6, 0xB0, 0xB4, 0xF0, 0x9D, 0x84, 0x9E, 0xEF,
0xA3, 0xBF, 0xF4, 0x8F, 0xBF, 0xBD, 0xEF, 0xBF, 0xBE]; | |
34 var encoded = new TextEncoder().encode(string); | |
35 assert_array_equals([].slice.call(encoded), bytes); | |
36 assert_equals(new TextDecoder(encoding).decode(new Uint8Array(bytes)), string)
; | |
37 assert_equals(new TextDecoder(encoding).decode(new Uint8Array(bytes).buffer),
string); | |
38 }, 'Encode/decode round trip: utf-8'); | |
39 | |
40 testDecodeSample( | |
41 'utf-16le', | |
42 sample, | |
43 [0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C, 0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xF8, 0xFF,
0xDB, 0xFD, 0xDF, 0xFE, 0xFF] | |
44 ); | |
45 | |
46 testDecodeSample( | |
47 'utf-16be', | |
48 sample, | |
49 [0x00, 0x7A, 0x00, 0xA2, 0x6C, 0x34, 0xD8, 0x34, 0xDD, 0x1E, 0xF8, 0xFF, 0xDB,
0xFF, 0xDF, 0xFD, 0xFF, 0xFE] | |
50 ); | |
51 | |
52 testDecodeSample( | |
53 'utf-16', | |
54 sample, | |
55 [0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C, 0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xF8, 0xFF,
0xDB, 0xFD, 0xDF, 0xFE, 0xFF] | |
56 ); | |
57 | |
58 </script> | |
OLD | NEW |