OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <script src="../../../resources/js-test.js"></script> | 2 <title>Encoding API: Byte-order marks</title> |
| 3 <script src="../../../resources/testharness.js"></script> |
| 4 <script src="../../../resources/testharnessreport.js"></script> |
3 <script> | 5 <script> |
4 | 6 |
5 description("Test the Encoding API's handling of byte-order marks (BOMs)."); | 7 var testCases = [ |
| 8 { |
| 9 encoding: 'utf-8', |
| 10 bom: [0xEF, 0xBB, 0xBF], |
| 11 bytes: [0x7A, 0xC2, 0xA2, 0xE6, 0xB0, 0xB4, 0xF0, 0x9D, 0x84, 0x9E, 0xF4
, 0x8F, 0xBF, 0xBD] |
| 12 }, |
| 13 { |
| 14 encoding: 'utf-16le', |
| 15 bom: [0xff, 0xfe], |
| 16 bytes: [0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C, 0x34, 0xD8, 0x1E, 0xDD, 0xFF
, 0xDB, 0xFD, 0xDF] |
| 17 }, |
| 18 { |
| 19 encoding: 'utf-16be', |
| 20 bom: [0xfe, 0xff], |
| 21 bytes: [0x00, 0x7A, 0x00, 0xA2, 0x6C, 0x34, 0xD8, 0x34, 0xDD, 0x1E, 0xDB
, 0xFF, 0xDF, 0xFD] |
| 22 } |
| 23 ]; |
6 | 24 |
7 var utf8_bom = [0xEF, 0xBB, 0xBF]; | 25 var string = 'z\xA2\u6C34\uD834\uDD1E\uDBFF\uDFFD'; // z, cent, CJK water, G-Cle
f, Private-use character |
8 var utf8 = [0x7A, 0xC2, 0xA2, 0xE6, 0xB0, 0xB4, 0xF0, 0x9D, 0x84, 0x9E, 0xF4, 0x
8F, 0xBF, 0xBD]; | |
9 | 26 |
10 var utf16le_bom = [0xff, 0xfe]; | 27 testCases.forEach(function(t) { |
11 var utf16le = [0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C, 0x34, 0xD8, 0x1E, 0xDD, 0xFF,
0xDB, 0xFD, 0xDF]; | 28 test(function() { |
12 | 29 |
13 var utf16be_bom = [0xfe, 0xff]; | 30 var decoder = new TextDecoder(t.encoding); |
14 var utf16be = [0x00, 0x7A, 0x00, 0xA2, 0x6C, 0x34, 0xD8, 0x34, 0xDD, 0x1E, 0xDB,
0xFF, 0xDF, 0xFD]; | 31 assert_equals(decoder.decode(new Uint8Array(t.bytes)), string, |
| 32 'Sequence without BOM should decode successfully'); |
15 | 33 |
16 var string = "z\xA2\u6C34\uD834\uDD1E\uDBFF\uDFFD"; // z, cent, CJK water, G-Cle
f, Private-use character | 34 assert_equals(decoder.decode(new Uint8Array(t.bom.concat(t.bytes))), str
ing, |
| 35 'Sequence with BOM should decode successfully (with no BOM
present in output)'); |
17 | 36 |
18 // missing BOMs | 37 testCases.forEach(function(o) { |
19 shouldBeEqualToString("new TextDecoder('utf-8').decode(new Uint8Array(utf8))", s
tring); | 38 if (o === t) |
20 shouldBeEqualToString("new TextDecoder('utf-16le').decode(new Uint8Array(utf16le
))", string); | 39 return; |
21 shouldBeEqualToString("new TextDecoder('utf-16be').decode(new Uint8Array(utf16be
))", string); | |
22 | 40 |
23 // matching BOMs | 41 assert_not_equals(decoder.decode(new Uint8Array(o.bom.concat(t.bytes
))), string, |
24 shouldBeEqualToString("new TextDecoder('utf-8').decode(new Uint8Array(utf8_bom.c
oncat(utf8)))", string); | 42 'Mismatching BOM should not be ignored - treated a
s garbage bytes.'); |
25 shouldBeEqualToString("new TextDecoder('utf-16le').decode(new Uint8Array(utf16le
_bom.concat(utf16le)))", string); | 43 }); |
26 shouldBeEqualToString("new TextDecoder('utf-16be').decode(new Uint8Array(utf16be
_bom.concat(utf16be)))", string); | |
27 | 44 |
28 // mismatching BOMs | 45 }, 'Byte-order marks: ' + t.encoding); |
29 shouldNotBe("new TextDecoder('utf-8').decode(new Uint8Array(utf16le_bom.concat(u
tf8)))", JSON.stringify(string)); | 46 }); |
30 shouldNotBe("new TextDecoder('utf-8').decode(new Uint8Array(utf16be_bom.concat(u
tf8)))", JSON.stringify(string)); | |
31 shouldNotBe("new TextDecoder('utf-16le').decode(new Uint8Array(utf8_bom.concat(u
tf16le)))", JSON.stringify(string)); | |
32 shouldNotBe("new TextDecoder('utf-16le').decode(new Uint8Array(utf16be_bom.conca
t(utf16le)))", JSON.stringify(string)); | |
33 shouldNotBe("new TextDecoder('utf-16be').decode(new Uint8Array(utf8_bom.concat(u
tf16be)))", JSON.stringify(string)); | |
34 shouldNotBe("new TextDecoder('utf-16be').decode(new Uint8Array(utf16le_bom.conca
t(utf16be)))", JSON.stringify(string)); | |
35 | |
36 // FIXME: Add tests where the BOM is split across buffers. | |
37 | 47 |
38 </script> | 48 </script> |
OLD | NEW |