api-surrogates-utf8.any.js (1461B)
1 // META: global=window,dedicatedworker,shadowrealm 2 // META: title=Encoding API: Invalid UTF-16 surrogates with UTF-8 encoding 3 4 var badStrings = [ 5 { 6 input: 'abc123', 7 expected: [0x61, 0x62, 0x63, 0x31, 0x32, 0x33], 8 decoded: 'abc123', 9 name: 'Sanity check' 10 }, 11 { 12 input: '\uD800', 13 expected: [0xef, 0xbf, 0xbd], 14 decoded: '\uFFFD', 15 name: 'Surrogate half (low)' 16 }, 17 { 18 input: '\uDC00', 19 expected: [0xef, 0xbf, 0xbd], 20 decoded: '\uFFFD', 21 name: 'Surrogate half (high)' 22 }, 23 { 24 input: 'abc\uD800123', 25 expected: [0x61, 0x62, 0x63, 0xef, 0xbf, 0xbd, 0x31, 0x32, 0x33], 26 decoded: 'abc\uFFFD123', 27 name: 'Surrogate half (low), in a string' 28 }, 29 { 30 input: 'abc\uDC00123', 31 expected: [0x61, 0x62, 0x63, 0xef, 0xbf, 0xbd, 0x31, 0x32, 0x33], 32 decoded: 'abc\uFFFD123', 33 name: 'Surrogate half (high), in a string' 34 }, 35 { 36 input: '\uDC00\uD800', 37 expected: [0xef, 0xbf, 0xbd, 0xef, 0xbf, 0xbd], 38 decoded: '\uFFFD\uFFFD', 39 name: 'Wrong order' 40 } 41 ]; 42 43 badStrings.forEach(function(t) { 44 test(function() { 45 var encoded = new TextEncoder().encode(t.input); 46 assert_array_equals([].slice.call(encoded), t.expected); 47 assert_equals(new TextDecoder('utf-8').decode(encoded), t.decoded); 48 }, 'Invalid surrogates encoded into UTF-8: ' + t.name); 49 });