• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// META: title=Encoding API: TextDecoder ignoreBOM option
2
3var cases = [
4    {encoding: 'utf-8', bytes: [0xEF, 0xBB, 0xBF, 0x61, 0x62, 0x63]},
5    {encoding: 'utf-16le', bytes: [0xFF, 0xFE, 0x61, 0x00, 0x62, 0x00, 0x63, 0x00]},
6    {encoding: 'utf-16be', bytes: [0xFE, 0xFF, 0x00, 0x61, 0x00, 0x62, 0x00, 0x63]}
7];
8
9cases.forEach(function(testCase) {
10    test(function() {
11        var BOM = '\uFEFF';
12        var decoder = new TextDecoder(testCase.encoding, {ignoreBOM: true});
13        var bytes = new Uint8Array(testCase.bytes);
14        assert_equals(
15            decoder.decode(bytes),
16            BOM + 'abc',
17            testCase.encoding + ': BOM should be present in decoded string if ignored');
18
19        decoder = new TextDecoder(testCase.encoding, {ignoreBOM: false});
20        assert_equals(
21            decoder.decode(bytes),
22            'abc',
23            testCase.encoding + ': BOM should be absent from decoded string if not ignored');
24
25        decoder = new TextDecoder(testCase.encoding);
26        assert_equals(
27            decoder.decode(bytes),
28            'abc',
29            testCase.encoding + ': BOM should be absent from decoded string by default');
30    }, 'BOM is ignored if ignoreBOM option is specified: ' + testCase.encoding);
31});
32
33test(function() {
34    assert_true('ignoreBOM' in new TextDecoder(), 'The ignoreBOM attribute should exist on TextDecoder.');
35    assert_equals(typeof  new TextDecoder().ignoreBOM, 'boolean', 'The type of the ignoreBOM attribute should be boolean.');
36    assert_false(new TextDecoder().ignoreBOM, 'The ignoreBOM attribute should default to false.');
37    assert_true(new TextDecoder('utf-8', {ignoreBOM: true}).ignoreBOM, 'The ignoreBOM attribute can be set using an option.');
38
39}, 'The ignoreBOM attribute of TextDecoder');
40