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 assert_equals( 19 decoder.decode(bytes), 20 BOM + 'abc', 21 testCase.encoding + ': BOM should be present in decoded string if ignored by a reused decoder'); 22 23 decoder = new TextDecoder(testCase.encoding, {ignoreBOM: false}); 24 assert_equals( 25 decoder.decode(bytes), 26 'abc', 27 testCase.encoding + ': BOM should be absent from decoded string if not ignored'); 28 assert_equals( 29 decoder.decode(bytes), 30 'abc', 31 testCase.encoding + ': BOM should be absent from decoded string if not ignored by a reused decoder'); 32 33 decoder = new TextDecoder(testCase.encoding); 34 assert_equals( 35 decoder.decode(bytes), 36 'abc', 37 testCase.encoding + ': BOM should be absent from decoded string by default'); 38 assert_equals( 39 decoder.decode(bytes), 40 'abc', 41 testCase.encoding + ': BOM should be absent from decoded string by default with a reused decoder'); 42 }, 'BOM is ignored if ignoreBOM option is specified: ' + testCase.encoding); 43}); 44 45test(function() { 46 assert_true('ignoreBOM' in new TextDecoder(), 'The ignoreBOM attribute should exist on TextDecoder.'); 47 assert_equals(typeof new TextDecoder().ignoreBOM, 'boolean', 'The type of the ignoreBOM attribute should be boolean.'); 48 assert_false(new TextDecoder().ignoreBOM, 'The ignoreBOM attribute should default to false.'); 49 assert_true(new TextDecoder('utf-8', {ignoreBOM: true}).ignoreBOM, 'The ignoreBOM attribute can be set using an option.'); 50 51}, 'The ignoreBOM attribute of TextDecoder'); 52