tor-browser

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

textdecoder-utf16-surrogates.any.js (1285B)


      1 // META: global=window,dedicatedworker,shadowrealm
      2 // META: title=Encoding API: UTF-16 surrogate handling
      3 
      4 var bad = [
      5    {
      6        encoding: 'utf-16le',
      7        input: [0x00, 0xd8],
      8        expected: '\uFFFD',
      9        name: 'lone surrogate lead'
     10    },
     11    {
     12        encoding: 'utf-16le',
     13        input: [0x00, 0xdc],
     14        expected: '\uFFFD',
     15        name: 'lone surrogate trail'
     16    },
     17    {
     18        encoding: 'utf-16le',
     19        input: [0x00, 0xd8, 0x00, 0x00],
     20        expected: '\uFFFD\u0000',
     21        name: 'unmatched surrogate lead'
     22    },
     23    {
     24        encoding: 'utf-16le',
     25        input: [0x00, 0xdc, 0x00, 0x00],
     26        expected: '\uFFFD\u0000',
     27        name: 'unmatched surrogate trail'
     28    },
     29    {
     30        encoding: 'utf-16le',
     31        input: [0x00, 0xdc, 0x00, 0xd8],
     32        expected: '\uFFFD\uFFFD',
     33        name: 'swapped surrogate pair'
     34    }
     35 ];
     36 
     37 bad.forEach(function(t) {
     38    test(function() {
     39        assert_equals(new TextDecoder(t.encoding).decode(new Uint8Array(t.input)), t.expected);
     40    }, t.encoding + ' - ' + t.name);
     41    test(function() {
     42        assert_throws_js(TypeError, function() {
     43            new TextDecoder(t.encoding, {fatal: true}).decode(new Uint8Array(t.input))
     44        });
     45    }, t.encoding + ' - ' + t.name + ' (fatal flag set)');
     46 });