tor-browser

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

textdecoder-byte-order-marks.any.js (1522B)


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