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