• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// META: title=Encoding API: End-of-file
2
3test(function() {
4    [
5        {encoding: 'utf-8', sequence: [0xC0]},
6        {encoding: 'utf-16le', sequence: [0x00]},
7        {encoding: 'utf-16be', sequence: [0x00]}
8    ].forEach(function(testCase) {
9
10        assert_throws(new TypeError(), function() {
11            var decoder = new TextDecoder(testCase.encoding, {fatal: true});
12            decoder.decode(new Uint8Array(testCase.sequence));
13        }, 'Unterminated ' + testCase.encoding + ' sequence should throw if fatal flag is set');
14
15        assert_equals(
16            new TextDecoder(testCase.encoding).decode(new Uint8Array([testCase.sequence])),
17            '\uFFFD',
18            'Unterminated UTF-8 sequence should emit replacement character if fatal flag is unset');
19    });
20}, 'Fatal flag, non-streaming cases');
21
22test(function() {
23
24    var decoder = new TextDecoder('utf-16le', {fatal: true});
25    var odd = new Uint8Array([0x00]);
26    var even = new Uint8Array([0x00, 0x00]);
27
28    assert_equals(decoder.decode(odd, {stream: true}), '');
29    assert_equals(decoder.decode(odd), '\u0000');
30
31    assert_throws(new TypeError(), function() {
32        decoder.decode(even, {stream: true});
33        decoder.decode(odd)
34    });
35
36    assert_throws(new TypeError(), function() {
37        decoder.decode(odd, {stream: true});
38        decoder.decode(even);
39    });
40
41    assert_equals(decoder.decode(even, {stream: true}), '\u0000');
42    assert_equals(decoder.decode(even), '\u0000');
43
44}, 'Fatal flag, streaming cases');
45