Skip to content

Truly independent web browser

Tests: Import updated encoding WPT tests

Shannon Booth4e8ef07b00f6
diff --git a/Tests/LibWeb/Text/expected/wpt-import/encoding/json-document-utf8.txt b/Tests/LibWeb/Text/expected/wpt-import/encoding/json-document-utf8.txt
index 0a9e90b55a8..1b589515009 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/encoding/json-document-utf8.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/encoding/json-document-utf8.txt
@@ -1,9 +1,11 @@
Harness status: OK
-Found 4 tests
+Found 5 tests
4 Pass
+1 Fail
Pass application/json with no charset is decoded as UTF-8 under a windows-1251 parent frame
Pass application/json;charset=utf-8 is decoded as UTF-8
Pass text/json with no charset is decoded as UTF-8
-Pass application/json;charset=windows-1251 honors the explicit charset
\ No newline at end of file
+Pass APPLICATION/JSON with no charset is decoded as UTF-8
+Fail application/json;charset=windows-1251 is decoded as UTF-8
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/encoding/textdecoder-fatal-streaming.any.txt b/Tests/LibWeb/Text/expected/wpt-import/encoding/textdecoder-fatal-streaming.any.txt
index 12fd87afba6..cc52d710252 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/encoding/textdecoder-fatal-streaming.any.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/encoding/textdecoder-fatal-streaming.any.txt
@@ -1,7 +1,14 @@
Harness status: OK
-Found 2 tests
+Found 8 tests
-2 Pass
+5 Pass
+3 Fail
Pass Fatal flag, non-streaming cases
Pass Fatal flag, streaming cases
+Pass Fatal flag, streaming error discards the remaining input: utf-8
+Fail Fatal flag, streaming error resets BOM handling: utf-8
+Pass Fatal flag, streaming error discards the remaining input: utf-16le
+Fail Fatal flag, streaming error resets BOM handling: utf-16le
+Pass Fatal flag, streaming error discards the remaining input: utf-16be
+Fail Fatal flag, streaming error resets BOM handling: utf-16be
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/encoding/textdecoder-mistakes.any.txt b/Tests/LibWeb/Text/expected/wpt-import/encoding/textdecoder-mistakes.any.txt
index 1472f5d3cd3..23a3381637c 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/encoding/textdecoder-mistakes.any.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/encoding/textdecoder-mistakes.any.txt
@@ -1,8 +1,9 @@
Harness status: OK
-Found 87 tests
+Found 108 tests
-87 Pass
+107 Pass
+1 Fail
Pass Invalid Unicode input is replaced: utf-8
Pass Invalid Unicode input is replaced: utf-16le
Pass Invalid Unicode input is replaced: utf-16be
@@ -60,6 +61,27 @@ Pass Replacement, push back ASCII characters: euc-jp
Pass Replacement, push back ASCII characters: euc-kr
Pass Replacement, push back ASCII characters: shift_jis
Pass Replacement, push back ASCII characters: gbk
+Pass EUC-JP JIS0212 state: 143,162,32,161,161
+Pass EUC-JP JIS0212 state: 143,162,32,161,161, split at 1
+Pass EUC-JP JIS0212 state: 143,162,32,161,161, split at 2
+Pass EUC-JP JIS0212 state: 143,162,32,161,161, split at 3
+Pass EUC-JP JIS0212 state: 143,162,32,161,161, split at 4
+Pass EUC-JP JIS0212 state: 143,162,160,161,161
+Pass EUC-JP JIS0212 state: 143,162,160,161,161, split at 1
+Pass EUC-JP JIS0212 state: 143,162,160,161,161, split at 2
+Pass EUC-JP JIS0212 state: 143,162,160,161,161, split at 3
+Pass EUC-JP JIS0212 state: 143,162,160,161,161, split at 4
+Pass EUC-JP JIS0212 state: 143,162,255,176,161
+Pass EUC-JP JIS0212 state: 143,162,255,176,161, split at 1
+Pass EUC-JP JIS0212 state: 143,162,255,176,161, split at 2
+Pass EUC-JP JIS0212 state: 143,162,255,176,161, split at 3
+Pass EUC-JP JIS0212 state: 143,162,255,176,161, split at 4
+Pass EUC-JP JIS0212 state: 143,162,175,161,161
+Pass EUC-JP JIS0212 state: 143,162,175,161,161, split at 1
+Pass EUC-JP JIS0212 state: 143,162,175,161,161, split at 2
+Pass EUC-JP JIS0212 state: 143,162,175,161,161, split at 3
+Pass EUC-JP JIS0212 state: 143,162,175,161,161, split at 4
+Pass EUC-JP JIS0212 state after a fatal streaming error
Pass Sticky multibyte state: iso-2022-jp
Pass Sticky multibyte state: gb18030
Pass Sticky multibyte state: big5
@@ -84,7 +106,7 @@ Pass stream: iso-2022-jp
Pass fatal stream: utf-8
Pass fatal stream: utf-16le
Pass fatal stream: utf-16be
-Pass fatal stream: iso-2022-jp
+Fail fatal stream: iso-2022-jp
Pass WPT mislabels: euc-jp
Pass WPT mislabels: shift_jis
Pass WPT mislabels: iso-2022-jp
diff --git a/Tests/LibWeb/Text/input/wpt-import/encoding/json-document-utf8.html b/Tests/LibWeb/Text/input/wpt-import/encoding/json-document-utf8.html
index 18ce3e29d43..a69f5f3f631 100644
--- a/Tests/LibWeb/Text/input/wpt-import/encoding/json-document-utf8.html
+++ b/Tests/LibWeb/Text/input/wpt-import/encoding/json-document-utf8.html
@@ -1,7 +1,8 @@
<!DOCTYPE html>
<meta charset="windows-1251">
-<title>JSON documents are decoded as UTF-8 by default</title>
+<title>JSON documents are always decoded as UTF-8</title>
<link rel="help" href="https://datatracker.ietf.org/doc/html/rfc8259#section-8.1">
+<link rel="help" href="https://datatracker.ietf.org/doc/html/rfc8259#section-11">
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<body>
@@ -35,7 +36,11 @@ promise_test(async () => {
}, "text/json with no charset is decoded as UTF-8");
promise_test(async () => {
- assert_not_equals(await loadJSON("?charset=windows-1251"), utf8);
-}, "application/json;charset=windows-1251 honors the explicit charset");
+ assert_equals(await loadJSON("?type=APPLICATION/JSON"), utf8);
+}, "APPLICATION/JSON with no charset is decoded as UTF-8");
+
+promise_test(async () => {
+ assert_equals(await loadJSON("?charset=windows-1251"), utf8);
+}, "application/json;charset=windows-1251 is decoded as UTF-8");
</script>
</body>
diff --git a/Tests/LibWeb/Text/input/wpt-import/encoding/textdecoder-fatal-streaming.any.js b/Tests/LibWeb/Text/input/wpt-import/encoding/textdecoder-fatal-streaming.any.js
index 872962fbd01..78a7219d748 100644
--- a/Tests/LibWeb/Text/input/wpt-import/encoding/textdecoder-fatal-streaming.any.js
+++ b/Tests/LibWeb/Text/input/wpt-import/encoding/textdecoder-fatal-streaming.any.js
@@ -43,3 +43,27 @@ test(function() {
assert_equals(decoder.decode(even), '\u0000');
}, 'Fatal flag, streaming cases');
+
+[
+ {encoding: 'utf-8', bom: [0xEF, 0xBB, 0xBF], invalid: [0xFF], a: [0x41]},
+ {encoding: 'utf-16le', bom: [0xFF, 0xFE], invalid: [0x00, 0xDC], a: [0x41, 0x00]},
+ {encoding: 'utf-16be', bom: [0xFE, 0xFF], invalid: [0xDC, 0x00], a: [0x00, 0x41]}
+].forEach(function(testCase) {
+
+ test(function() {
+ var decoder = new TextDecoder(testCase.encoding, {fatal: true});
+ assert_throws_js(TypeError, function() {
+ decoder.decode(new Uint8Array(testCase.invalid.concat(testCase.a)), {stream: true});
+ });
+ assert_equals(decoder.decode(new Uint8Array(testCase.a)), 'A');
+ }, 'Fatal flag, streaming error discards the remaining input: ' + testCase.encoding);
+
+ test(function() {
+ var decoder = new TextDecoder(testCase.encoding, {fatal: true});
+ assert_equals(decoder.decode(new Uint8Array(testCase.bom), {stream: true}), '');
+ assert_throws_js(TypeError, function() {
+ decoder.decode(new Uint8Array(testCase.invalid), {stream: true});
+ });
+ assert_equals(decoder.decode(new Uint8Array(testCase.bom.concat(testCase.a))), 'A');
+ }, 'Fatal flag, streaming error resets BOM handling: ' + testCase.encoding);
+});
diff --git a/Tests/LibWeb/Text/input/wpt-import/encoding/textdecoder-mistakes.any.js b/Tests/LibWeb/Text/input/wpt-import/encoding/textdecoder-mistakes.any.js
index ea859dcccd7..a81c1f362f3 100644
--- a/Tests/LibWeb/Text/input/wpt-import/encoding/textdecoder-mistakes.any.js
+++ b/Tests/LibWeb/Text/input/wpt-import/encoding/textdecoder-mistakes.any.js
@@ -355,6 +355,39 @@ test(() => {
}
}
+// https://encoding.spec.whatwg.org/#euc-jp-decoder
+{
+ const vectors = [
+ [[0x8f, 0xa2, 0x20, 0xa1, 0xa1], '\uFFFD \u3000'],
+ [[0x8f, 0xa2, 0xa0, 0xa1, 0xa1], '\uFFFD\u3000'],
+ [[0x8f, 0xa2, 0xff, 0xb0, 0xa1], '\uFFFD\u4E9C'],
+ [[0x8f, 0xa2, 0xaf, 0xa1, 0xa1], '\u02D8\u3000'],
+ ];
+
+ for (const [bytes, expected] of vectors) {
+ const input = Uint8Array.from(bytes);
+ test(() => {
+ assert_equals(new TextDecoder('euc-jp').decode(input), expected);
+ }, `EUC-JP JIS0212 state: ${bytes}`);
+
+ for (let split = 1; split < input.length; split++) {
+ test(() => {
+ const d = new TextDecoder('euc-jp');
+ const first = d.decode(input.subarray(0, split), {stream: true});
+ assert_equals(first + d.decode(input.subarray(split)), expected);
+ }, `EUC-JP JIS0212 state: ${bytes}, split at ${split}`);
+ }
+ }
+}
+
+test(() => {
+ const d = new TextDecoder('euc-jp', {fatal: true});
+ assert_throws_js(TypeError,
+ () => d.decode(u(0x8f, 0xa2, 0x20), {stream: true}));
+ // The ASCII byte is restored to the input queue, but the decoder is reset when decode() throws.
+ assert_equals(d.decode(u(0xa1, 0xa1)), '\u3000');
+}, 'EUC-JP JIS0212 state after a fatal streaming error');
+
// Chrome, Firefox and Safari all fail on this
// https://issues.chromium.org/issues/467624168
{
@@ -668,22 +701,21 @@ for (const encoding of ['utf-16le', 'utf-16be']) {
}, `fatal stream: ${encoding}`)
}
-// Bun is incorrect
test(() => {
- // This is the only decoder which does not clear internal state before throwing in stream mode (non-EOF throws)
- // So the internal state of this decoder can legitimately persist after an error was thrown
+ // This is the only decoder which does not clear internal state when returning an error
+ // But decode() resets the decoder when it throws, so that state does not persist
{
const d = new TextDecoder('iso-2022-jp', { fatal: true })
assert_equals(d.decode(Uint8Array.of(0x7e)), '\x7E')
assert_throws_js(TypeError, () => d.decode(u(0x1b, 0x28, 0x4a, 0xff), { stream: true })) // Switch to Roman, error
- assert_equals(d.decode(Uint8Array.of(0x7e)), '\u203E')
+ assert_equals(d.decode(Uint8Array.of(0x7e)), '\x7E')
}
{
const d = new TextDecoder('iso-2022-jp', { fatal: true })
assert_equals(d.decode(Uint8Array.of(0x42)), 'B')
assert_throws_js(TypeError, () => d.decode(u(0x1b, 0x28, 0x49, 0xff), { stream: true })) // Switch to Katakana, error
- assert_equals(d.decode(Uint8Array.of(0x42)), '\uFF82')
+ assert_equals(d.decode(Uint8Array.of(0x42)), 'B')
}
}, 'fatal stream: iso-2022-jp')