tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

textdecoder-ignorebom.any.js (2402B)


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