11cb0ef41Sopenharmony_ci// META: title=Encoding API: TextDecoder ignoreBOM option 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_civar cases = [ 41cb0ef41Sopenharmony_ci {encoding: 'utf-8', bytes: [0xEF, 0xBB, 0xBF, 0x61, 0x62, 0x63]}, 51cb0ef41Sopenharmony_ci {encoding: 'utf-16le', bytes: [0xFF, 0xFE, 0x61, 0x00, 0x62, 0x00, 0x63, 0x00]}, 61cb0ef41Sopenharmony_ci {encoding: 'utf-16be', bytes: [0xFE, 0xFF, 0x00, 0x61, 0x00, 0x62, 0x00, 0x63]} 71cb0ef41Sopenharmony_ci]; 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_cicases.forEach(function(testCase) { 101cb0ef41Sopenharmony_ci test(function() { 111cb0ef41Sopenharmony_ci var BOM = '\uFEFF'; 121cb0ef41Sopenharmony_ci var decoder = new TextDecoder(testCase.encoding, {ignoreBOM: true}); 131cb0ef41Sopenharmony_ci var bytes = new Uint8Array(testCase.bytes); 141cb0ef41Sopenharmony_ci assert_equals( 151cb0ef41Sopenharmony_ci decoder.decode(bytes), 161cb0ef41Sopenharmony_ci BOM + 'abc', 171cb0ef41Sopenharmony_ci testCase.encoding + ': BOM should be present in decoded string if ignored'); 181cb0ef41Sopenharmony_ci assert_equals( 191cb0ef41Sopenharmony_ci decoder.decode(bytes), 201cb0ef41Sopenharmony_ci BOM + 'abc', 211cb0ef41Sopenharmony_ci testCase.encoding + ': BOM should be present in decoded string if ignored by a reused decoder'); 221cb0ef41Sopenharmony_ci 231cb0ef41Sopenharmony_ci decoder = new TextDecoder(testCase.encoding, {ignoreBOM: false}); 241cb0ef41Sopenharmony_ci assert_equals( 251cb0ef41Sopenharmony_ci decoder.decode(bytes), 261cb0ef41Sopenharmony_ci 'abc', 271cb0ef41Sopenharmony_ci testCase.encoding + ': BOM should be absent from decoded string if not ignored'); 281cb0ef41Sopenharmony_ci assert_equals( 291cb0ef41Sopenharmony_ci decoder.decode(bytes), 301cb0ef41Sopenharmony_ci 'abc', 311cb0ef41Sopenharmony_ci testCase.encoding + ': BOM should be absent from decoded string if not ignored by a reused decoder'); 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci decoder = new TextDecoder(testCase.encoding); 341cb0ef41Sopenharmony_ci assert_equals( 351cb0ef41Sopenharmony_ci decoder.decode(bytes), 361cb0ef41Sopenharmony_ci 'abc', 371cb0ef41Sopenharmony_ci testCase.encoding + ': BOM should be absent from decoded string by default'); 381cb0ef41Sopenharmony_ci assert_equals( 391cb0ef41Sopenharmony_ci decoder.decode(bytes), 401cb0ef41Sopenharmony_ci 'abc', 411cb0ef41Sopenharmony_ci testCase.encoding + ': BOM should be absent from decoded string by default with a reused decoder'); 421cb0ef41Sopenharmony_ci }, 'BOM is ignored if ignoreBOM option is specified: ' + testCase.encoding); 431cb0ef41Sopenharmony_ci}); 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_citest(function() { 461cb0ef41Sopenharmony_ci assert_true('ignoreBOM' in new TextDecoder(), 'The ignoreBOM attribute should exist on TextDecoder.'); 471cb0ef41Sopenharmony_ci assert_equals(typeof new TextDecoder().ignoreBOM, 'boolean', 'The type of the ignoreBOM attribute should be boolean.'); 481cb0ef41Sopenharmony_ci assert_false(new TextDecoder().ignoreBOM, 'The ignoreBOM attribute should default to false.'); 491cb0ef41Sopenharmony_ci assert_true(new TextDecoder('utf-8', {ignoreBOM: true}).ignoreBOM, 'The ignoreBOM attribute can be set using an option.'); 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci}, 'The ignoreBOM attribute of TextDecoder'); 52