OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <script src="../../../resources/js-test.js"></script> | 2 <title>Encoding API: End-of-file</title> |
| 3 <script src="../../../resources/testharness.js"></script> |
| 4 <script src="../../../resources/testharnessreport.js"></script> |
3 <script> | 5 <script> |
4 | 6 |
5 description("Edge cases around non-fatal errors at EOF"); | 7 test(function() { |
| 8 [ |
| 9 {encoding: 'utf-8', sequence: [0xC0]}, |
| 10 {encoding: 'utf-16le', sequence: [0x00]}, |
| 11 {encoding: 'utf-16be', sequence: [0x00]} |
| 12 ].forEach(function(testCase) { |
6 | 13 |
7 shouldThrow("new TextDecoder('utf-8', {fatal: true}).decode(new Uint8Array([0xff
]))"); | 14 assert_throws({name: 'EncodingError'}, function() { |
| 15 var decoder = new TextDecoder(testCase.encoding, {fatal: true}); |
| 16 decoder.decode(new Uint8Array(testCase.sequence)); |
| 17 }, 'Unterminated ' + testCase.encoding + ' sequence should throw if fata
l flag is set'); |
8 | 18 |
9 debug(""); | 19 assert_equals( |
10 shouldBe("new TextDecoder('utf-8').decode(new Uint8Array([0xff]))", "'\uFFFD'"); | 20 new TextDecoder(testCase.encoding).decode(new Uint8Array([testCase.s
equence])), |
| 21 '\uFFFD', |
| 22 'Unterminated UTF-8 sequence should emit replacement character if fa
tal flag is unset'); |
| 23 }); |
| 24 }, 'Fatal flag, non-streaming cases'); |
11 | 25 |
12 debug(""); | 26 test(function() { |
13 shouldThrow("new TextDecoder('utf-16le', {fatal: true}).decode(new Uint8Array([0
x00]))"); | |
14 | 27 |
15 debug(""); | 28 var decoder = new TextDecoder('utf-16le', {fatal: true}); |
16 shouldBe("new TextDecoder('utf-16le').decode(new Uint8Array([0x00]))", "'\uFFFD'
"); | 29 var odd = new Uint8Array([0x00]); |
| 30 var even = new Uint8Array([0x00, 0x00]); |
17 | 31 |
18 debug(""); | 32 assert_equals(decoder.decode(odd, {stream: true}), ''); |
19 shouldThrow("new TextDecoder('utf-16be', {fatal: true}).decode(new Uint8Array([0
x00]))"); | 33 assert_equals(decoder.decode(odd), '\u0000'); |
20 | 34 |
21 debug(""); | 35 assert_throws({name: 'EncodingError'}, function() { |
22 shouldBe("new TextDecoder('utf-16be').decode(new Uint8Array([0x00]))", "'\uFFFD'
"); | 36 decoder.decode(even, {stream: true}); |
| 37 decoder.decode(odd) |
| 38 }); |
23 | 39 |
24 debug(""); | 40 assert_throws({name: 'EncodingError'}, function() { |
25 debug("Streaming cases:"); | 41 decoder.decode(odd, {stream: true}); |
26 evalAndLog("decoder = new TextDecoder('utf-16le', {fatal: true})"); | 42 decoder.decode(even); |
27 evalAndLog("odd = new Uint8Array([0x00])"); | 43 }); |
28 evalAndLog("even = new Uint8Array([0x00, 0x00])"); | |
29 | 44 |
30 debug(""); | 45 assert_equals(decoder.decode(even, {stream: true}), '\u0000'); |
31 shouldNotThrow("decoder.decode(odd, {stream: true}); decoder.decode(odd)"); | 46 assert_equals(decoder.decode(even), '\u0000'); |
32 shouldThrow("decoder.decode(even, {stream: true}); decoder.decode(odd)"); | 47 |
33 shouldThrow("decoder.decode(odd, {stream: true}); decoder.decode(even)"); | 48 }, 'Fatal flag, streaming cases'); |
34 shouldNotThrow("decoder.decode(even, {stream: true}); decoder.decode(even)"); | |
35 | 49 |
36 </script> | 50 </script> |
OLD | NEW |