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