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