| OLD | NEW |
| 1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
| 2 <script src="../../../resources/js-test.js"></script> | 2 <title>Encoding API: Basics</title> |
| 3 <script src="../../../resources/testharness.js"></script> |
| 4 <script src="../../../resources/testharnessreport.js"></script> |
| 3 <script> | 5 <script> |
| 4 | 6 |
| 5 description("This tests the basics of the Encoding API."); | 7 test(function() { |
| 6 | 8 |
| 7 shouldBeTrue("'TextEncoder' in window"); | 9 assert_true('TextEncoder' in window); |
| 8 shouldBeTrue("'TextDecoder' in window"); | 10 assert_true('TextDecoder' in window); |
| 9 | 11 |
| 10 shouldBeTrue("'encoding' in new TextEncoder"); | 12 assert_true('encoding' in new TextEncoder); |
| 11 shouldBeTrue("'encoding' in new TextDecoder"); | 13 assert_true('encoding' in new TextDecoder); |
| 14 assert_equals(typeof (new TextEncoder).encoding, 'string'); |
| 15 assert_equals(typeof (new TextDecoder).encoding, 'string'); |
| 12 | 16 |
| 13 shouldBeEqualToString("typeof (new TextEncoder).encoding", "string"); | 17 assert_true('encode' in new TextEncoder); |
| 14 shouldBeEqualToString("typeof (new TextDecoder).encoding", "string"); | 18 assert_true('decode' in new TextDecoder); |
| 15 | 19 |
| 16 shouldBeTrue("'encode' in new TextEncoder"); | 20 assert_equals(typeof (new TextEncoder).encode, 'function'); |
| 17 shouldBeTrue("'decode' in new TextDecoder"); | 21 assert_equals(typeof (new TextDecoder).decode, 'function'); |
| 18 | 22 |
| 19 shouldBeEqualToString("typeof (new TextEncoder).encode", "function"); | 23 assert_equals((new TextEncoder).encoding, 'utf-8', 'default encoding is utf-
8'); |
| 20 shouldBeEqualToString("typeof (new TextDecoder).decode", "function"); | 24 assert_equals((new TextDecoder).encoding, 'utf-8', 'default encoding is utf-
8'); |
| 21 | 25 |
| 26 function testEncodeDecodeSample(encoding, string, bytes) { |
| 27 var encoded = new TextEncoder(encoding).encode(string); |
| 28 assert_array_equals([].slice.call(encoded), bytes); |
| 29 assert_equals(new TextDecoder(encoding).decode(new Uint8Array(bytes)), s
tring); |
| 30 } |
| 22 | 31 |
| 23 shouldBeEqualToString("(new TextEncoder).encoding", "utf-8"); | 32 // z (ASCII U+007A), cent (Latin-1 U+00A2), CJK water (BMP U+6C34), |
| 24 shouldBeEqualToString("(new TextDecoder).encoding", "utf-8"); | 33 // G-Clef (non-BMP U+1D11E), PUA (BMP U+F8FF), PUA (non-BMP U+10FFFD) |
| 34 // byte-swapped BOM (non-character U+FFFE) |
| 35 var sample = 'z\xA2\u6C34\uD834\uDD1E\uF8FF\uDBFF\uDFFD\uFFFE'; |
| 25 | 36 |
| 26 function toArray(arrayLike) { | 37 testEncodeDecodeSample( |
| 27 return [].map.call(arrayLike, function(x) { return x; }); | 38 'utf-8', |
| 28 } | 39 sample, |
| 29 | 40 [0x7A, 0xC2, 0xA2, 0xE6, 0xB0, 0xB4, 0xF0, 0x9D, 0x84, 0x9E, 0xEF, 0xA3,
0xBF, 0xF4, 0x8F, 0xBF, 0xBD, 0xEF, 0xBF, 0xBE] |
| 30 function testEncodeDecodeSample(encoding, string, bytes) { | 41 ); |
| 31 debug(""); | 42 testEncodeDecodeSample( |
| 32 debug("test encode/decode sample - " + encoding); | 43 'utf-16le', |
| 33 | 44 sample, |
| 34 evalAndLog("encoded = new TextEncoder('" + encoding + "').encode(" + JSON.st
ringify(string) + ")"); | 45 [0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C, 0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xF8,
0xFF, 0xDB, 0xFD, 0xDF, 0xFE, 0xFF] |
| 35 shouldBeEqualToString("JSON.stringify(toArray(encoded))", JSON.stringify(byt
es)); | 46 ); |
| 36 shouldBeEqualToString("new TextDecoder('" + encoding + "').decode(new Uint8A
rray(" + JSON.stringify(bytes) + "))", string); | 47 testEncodeDecodeSample( |
| 37 } | 48 'utf-16be', |
| 38 | 49 sample, |
| 39 testEncodeDecodeSample( | 50 [0x00, 0x7A, 0x00, 0xA2, 0x6C, 0x34, 0xD8, 0x34, 0xDD, 0x1E, 0xF8, 0xFF,
0xDB, 0xFF, 0xDF, 0xFD, 0xFF, 0xFE] |
| 40 "utf-8", | 51 ); |
| 41 "z\xA2\u6C34\uD834\uDD1E\uDBFF\uDFFD", // z, cent, CJK water, G-Clef, Privat
e-use character | 52 testEncodeDecodeSample( |
| 42 [0x7A, 0xC2, 0xA2, 0xE6, 0xB0, 0xB4, 0xF0, 0x9D, 0x84, 0x9E, 0xF4, 0x8F, 0xB
F, 0xBD] | 53 'utf-16', |
| 43 ); | 54 sample, |
| 44 testEncodeDecodeSample( | 55 [0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C, 0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xF8,
0xFF, 0xDB, 0xFD, 0xDF, 0xFE, 0xFF] |
| 45 "utf-16le", | 56 ); |
| 46 "z\xA2\u6C34\uD834\uDD1E\uDBFF\uDFFD", // z, cent, CJK water, G-Clef, Privat
e-use character | 57 }, 'Encoding API basics'); |
| 47 [0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C, 0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xDB, 0xF
D, 0xDF] | |
| 48 ); | |
| 49 testEncodeDecodeSample( | |
| 50 "utf-16be", | |
| 51 "z\xA2\u6C34\uD834\uDD1E\uDBFF\uDFFD", // z, cent, CJK water, G-Clef, Privat
e-use character | |
| 52 [0x00, 0x7A, 0x00, 0xA2, 0x6C, 0x34, 0xD8, 0x34, 0xDD, 0x1E, 0xDB, 0xFF, 0xD
F, 0xFD] | |
| 53 ); | |
| 54 testEncodeDecodeSample( | |
| 55 "utf-16", | |
| 56 "z\xA2\u6C34\uD834\uDD1E\uDBFF\uDFFD", // z, cent, CJK water, G-Clef, Privat
e-use character | |
| 57 [0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C, 0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xDB, 0xF
D, 0xDF] | |
| 58 ); | |
| 59 | 58 |
| 60 </script> | 59 </script> |
| OLD | NEW |