• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// META: title=Encoding API: Byte-order marks
2
3var testCases = [
4    {
5        encoding: 'utf-8',
6        bom: [0xEF, 0xBB, 0xBF],
7        bytes: [0x7A, 0xC2, 0xA2, 0xE6, 0xB0, 0xB4, 0xF0, 0x9D, 0x84, 0x9E, 0xF4, 0x8F, 0xBF, 0xBD]
8    },
9    {
10        encoding: 'utf-16le',
11        bom: [0xff, 0xfe],
12        bytes: [0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C, 0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xDB, 0xFD, 0xDF]
13    },
14    {
15        encoding: 'utf-16be',
16        bom: [0xfe, 0xff],
17        bytes: [0x00, 0x7A, 0x00, 0xA2, 0x6C, 0x34, 0xD8, 0x34, 0xDD, 0x1E, 0xDB, 0xFF, 0xDF, 0xFD]
18    }
19];
20
21var string = 'z\xA2\u6C34\uD834\uDD1E\uDBFF\uDFFD'; // z, cent, CJK water, G-Clef, Private-use character
22
23testCases.forEach(function(t) {
24    test(function() {
25
26        var decoder = new TextDecoder(t.encoding);
27        assert_equals(decoder.decode(new Uint8Array(t.bytes)), string,
28                      'Sequence without BOM should decode successfully');
29
30        assert_equals(decoder.decode(new Uint8Array(t.bom.concat(t.bytes))), string,
31                      'Sequence with BOM should decode successfully (with no BOM present in output)');
32
33        testCases.forEach(function(o) {
34            if (o === t)
35                return;
36
37            assert_not_equals(decoder.decode(new Uint8Array(o.bom.concat(t.bytes))), string,
38                              'Mismatching BOM should not be ignored - treated as garbage bytes.');
39        });
40
41    }, 'Byte-order marks: ' + t.encoding);
42});
43