Skip to content

Truly independent web browser

Tests/LibWeb: Add TextEncoder coverage

Aliaksandr Kalenik52ea6c43bd66
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index 462253507a9..1990940f5bc 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -303,6 +303,8 @@ Text/input/wpt-import/workers/nested_worker_importScripts.worker.html
Text/input/wpt-import/workers/nested_worker_sync_xhr.worker.html
Text/input/wpt-import/workers/nested_worker_terminate_from_document.html
Text/input/wpt-import/encoding/legacy-mb-schinese/gb18030/gb18030-decoder.any.worker.html
+Text/input/wpt-import/encoding/api-basics.any.worker.html
+Text/input/wpt-import/encoding/encodeInto.any.worker.html
Text/input/wpt-import/WebCryptoAPI/historical.any.worker.html
Text/input/Worker/Worker-close-after-postMessage.html
Text/input/Worker/Worker-crypto.html
diff --git a/Tests/LibWeb/Text/expected/Encoding/TextEncoder-wrapper.txt b/Tests/LibWeb/Text/expected/Encoding/TextEncoder-wrapper.txt
new file mode 100644
index 00000000000..d58e3edec10
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Encoding/TextEncoder-wrapper.txt
@@ -0,0 +1,34 @@
+TextEncoder.prototype:
+ encoding { get: function "get encoding" length 0, enumerable: true, configurable: true }
+ encode { value: function "encode" length 0, writable: true, enumerable: true, configurable: true }
+ encodeInto { value: function "encodeInto" length 2, writable: true, enumerable: true, configurable: true }
+ constructor { value: function "TextEncoder" length 0, writable: true, enumerable: false, configurable: true }
+ Symbol(Symbol.toStringTag) { value: TextEncoder, writable: false, enumerable: false, configurable: true }
+TextEncoder:
+ length { value: 0, writable: false, enumerable: false, configurable: true }
+ name { value: TextEncoder, writable: false, enumerable: false, configurable: true }
+prototype of TextEncoder.prototype is Object.prototype: true
+Object.prototype.toString of an instance: [object TextEncoder]
+own keys of an instance: 0
+encode(): []
+encode(undefined): []
+encode(null): [110,117,108,108]
+encode of a lone surrogate: [97,239,191,189,98]
+encode of a symbol: TypeError: Cannot convert symbol to string
+calling without new: TypeError: TextEncoder constructor must be called with 'new'
+encode on a plain object: TypeError: Not an object of type TextEncoder
+encode on undefined: TypeError: Not an object of type TextEncoder
+encoding getter on a TextDecoder: TypeError: Not an object of type TextEncoder
+encodeInto with one argument: TypeError: encodeInto() needs 2 arguments
+encodeInto into an ArrayBuffer: TypeError: Not an object of type Uint8Array
+encodeInto into an Int8Array: TypeError: Not an object of type Uint8Array
+encodeInto into a resizable buffer: TypeError: Not an object of type fixed-length Uint8Array
+encodeInto into a detached buffer: {"read":0,"written":0}
+encodeInto stops before a code point that does not fit: [{"read":2,"written":2},[97,98,0,0,0]]
+encodeInto at an offset: [{"read":1,"written":2},[0,0,195,169,0,0]]
+structuredClone: DataCloneError: Cannot serialize platform objects
+passed as a Node: TypeError: Not an object of type Node
+subclass: true true [2,"utf-8"]
+prototype from the realm of newTarget: true
+encode result realm: true false
+encoders work after a garbage collection: true
diff --git a/Tests/LibWeb/Text/expected/wpt-import/encoding/api-basics.any.txt b/Tests/LibWeb/Text/expected/wpt-import/encoding/api-basics.any.txt
new file mode 100644
index 00000000000..47ec326ff5a
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/encoding/api-basics.any.txt
@@ -0,0 +1,11 @@
+Harness status: OK
+
+Found 6 tests
+
+6 Pass
+Pass Default encodings
+Pass Default inputs
+Pass Encode/decode round trip: utf-8
+Pass Decode sample: utf-16le
+Pass Decode sample: utf-16be
+Pass Decode sample: utf-16
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/encoding/api-basics.any.worker.txt b/Tests/LibWeb/Text/expected/wpt-import/encoding/api-basics.any.worker.txt
new file mode 100644
index 00000000000..47ec326ff5a
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/encoding/api-basics.any.worker.txt
@@ -0,0 +1,11 @@
+Harness status: OK
+
+Found 6 tests
+
+6 Pass
+Pass Default encodings
+Pass Default inputs
+Pass Encode/decode round trip: utf-8
+Pass Decode sample: utf-16le
+Pass Decode sample: utf-16be
+Pass Decode sample: utf-16
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/encoding/api-surrogates-utf8.any.txt b/Tests/LibWeb/Text/expected/wpt-import/encoding/api-surrogates-utf8.any.txt
new file mode 100644
index 00000000000..8ab0b59cbe4
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/encoding/api-surrogates-utf8.any.txt
@@ -0,0 +1,11 @@
+Harness status: OK
+
+Found 6 tests
+
+6 Pass
+Pass Invalid surrogates encoded into UTF-8: Sanity check
+Pass Invalid surrogates encoded into UTF-8: Surrogate half (low)
+Pass Invalid surrogates encoded into UTF-8: Surrogate half (high)
+Pass Invalid surrogates encoded into UTF-8: Surrogate half (low), in a string
+Pass Invalid surrogates encoded into UTF-8: Surrogate half (high), in a string
+Pass Invalid surrogates encoded into UTF-8: Wrong order
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/encoding/encodeInto.any.worker.txt b/Tests/LibWeb/Text/expected/wpt-import/encoding/encodeInto.any.worker.txt
new file mode 100644
index 00000000000..865fbc1e028
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/encoding/encodeInto.any.worker.txt
@@ -0,0 +1,116 @@
+Harness status: OK
+
+Found 111 tests
+
+111 Pass
+Pass encodeInto() into ArrayBuffer with Hi and destination length 0, offset 0, filler 0
+Pass encodeInto() into SharedArrayBuffer with Hi and destination length 0, offset 0, filler 0
+Pass encodeInto() into ArrayBuffer with Hi and destination length 0, offset 4, filler 0
+Pass encodeInto() into SharedArrayBuffer with Hi and destination length 0, offset 4, filler 0
+Pass encodeInto() into ArrayBuffer with Hi and destination length 0, offset 0, filler 128
+Pass encodeInto() into SharedArrayBuffer with Hi and destination length 0, offset 0, filler 128
+Pass encodeInto() into ArrayBuffer with Hi and destination length 0, offset 4, filler 128
+Pass encodeInto() into SharedArrayBuffer with Hi and destination length 0, offset 4, filler 128
+Pass encodeInto() into ArrayBuffer with Hi and destination length 0, offset 0, filler random
+Pass encodeInto() into SharedArrayBuffer with Hi and destination length 0, offset 0, filler random
+Pass encodeInto() into ArrayBuffer with Hi and destination length 0, offset 4, filler random
+Pass encodeInto() into SharedArrayBuffer with Hi and destination length 0, offset 4, filler random
+Pass encodeInto() into ArrayBuffer with A and destination length 10, offset 0, filler 0
+Pass encodeInto() into SharedArrayBuffer with A and destination length 10, offset 0, filler 0
+Pass encodeInto() into ArrayBuffer with A and destination length 10, offset 4, filler 0
+Pass encodeInto() into SharedArrayBuffer with A and destination length 10, offset 4, filler 0
+Pass encodeInto() into ArrayBuffer with A and destination length 10, offset 0, filler 128
+Pass encodeInto() into SharedArrayBuffer with A and destination length 10, offset 0, filler 128
+Pass encodeInto() into ArrayBuffer with A and destination length 10, offset 4, filler 128
+Pass encodeInto() into SharedArrayBuffer with A and destination length 10, offset 4, filler 128
+Pass encodeInto() into ArrayBuffer with A and destination length 10, offset 0, filler random
+Pass encodeInto() into SharedArrayBuffer with A and destination length 10, offset 0, filler random
+Pass encodeInto() into ArrayBuffer with A and destination length 10, offset 4, filler random
+Pass encodeInto() into SharedArrayBuffer with A and destination length 10, offset 4, filler random
+Pass encodeInto() into ArrayBuffer with 𝌆 and destination length 4, offset 0, filler 0
+Pass encodeInto() into SharedArrayBuffer with 𝌆 and destination length 4, offset 0, filler 0
+Pass encodeInto() into ArrayBuffer with 𝌆 and destination length 4, offset 4, filler 0
+Pass encodeInto() into SharedArrayBuffer with 𝌆 and destination length 4, offset 4, filler 0
+Pass encodeInto() into ArrayBuffer with 𝌆 and destination length 4, offset 0, filler 128
+Pass encodeInto() into SharedArrayBuffer with 𝌆 and destination length 4, offset 0, filler 128
+Pass encodeInto() into ArrayBuffer with 𝌆 and destination length 4, offset 4, filler 128
+Pass encodeInto() into SharedArrayBuffer with 𝌆 and destination length 4, offset 4, filler 128
+Pass encodeInto() into ArrayBuffer with 𝌆 and destination length 4, offset 0, filler random
+Pass encodeInto() into SharedArrayBuffer with 𝌆 and destination length 4, offset 0, filler random
+Pass encodeInto() into ArrayBuffer with 𝌆 and destination length 4, offset 4, filler random
+Pass encodeInto() into SharedArrayBuffer with 𝌆 and destination length 4, offset 4, filler random
+Pass encodeInto() into ArrayBuffer with 𝌆A and destination length 3, offset 0, filler 0
+Pass encodeInto() into SharedArrayBuffer with 𝌆A and destination length 3, offset 0, filler 0
+Pass encodeInto() into ArrayBuffer with 𝌆A and destination length 3, offset 4, filler 0
+Pass encodeInto() into SharedArrayBuffer with 𝌆A and destination length 3, offset 4, filler 0
+Pass encodeInto() into ArrayBuffer with 𝌆A and destination length 3, offset 0, filler 128
+Pass encodeInto() into SharedArrayBuffer with 𝌆A and destination length 3, offset 0, filler 128
+Pass encodeInto() into ArrayBuffer with 𝌆A and destination length 3, offset 4, filler 128
+Pass encodeInto() into SharedArrayBuffer with 𝌆A and destination length 3, offset 4, filler 128
+Pass encodeInto() into ArrayBuffer with 𝌆A and destination length 3, offset 0, filler random
+Pass encodeInto() into SharedArrayBuffer with 𝌆A and destination length 3, offset 0, filler random
+Pass encodeInto() into ArrayBuffer with 𝌆A and destination length 3, offset 4, filler random
+Pass encodeInto() into SharedArrayBuffer with 𝌆A and destination length 3, offset 4, filler random
+Pass encodeInto() into ArrayBuffer with U+d834AU+df06A¥Hi and destination length 10, offset 0, filler 0
+Pass encodeInto() into SharedArrayBuffer with U+d834AU+df06A¥Hi and destination length 10, offset 0, filler 0
+Pass encodeInto() into ArrayBuffer with U+d834AU+df06A¥Hi and destination length 10, offset 4, filler 0
+Pass encodeInto() into SharedArrayBuffer with U+d834AU+df06A¥Hi and destination length 10, offset 4, filler 0
+Pass encodeInto() into ArrayBuffer with U+d834AU+df06A¥Hi and destination length 10, offset 0, filler 128
+Pass encodeInto() into SharedArrayBuffer with U+d834AU+df06A¥Hi and destination length 10, offset 0, filler 128
+Pass encodeInto() into ArrayBuffer with U+d834AU+df06A¥Hi and destination length 10, offset 4, filler 128
+Pass encodeInto() into SharedArrayBuffer with U+d834AU+df06A¥Hi and destination length 10, offset 4, filler 128
+Pass encodeInto() into ArrayBuffer with U+d834AU+df06A¥Hi and destination length 10, offset 0, filler random
+Pass encodeInto() into SharedArrayBuffer with U+d834AU+df06A¥Hi and destination length 10, offset 0, filler random
+Pass encodeInto() into ArrayBuffer with U+d834AU+df06A¥Hi and destination length 10, offset 4, filler random
+Pass encodeInto() into SharedArrayBuffer with U+d834AU+df06A¥Hi and destination length 10, offset 4, filler random
+Pass encodeInto() into ArrayBuffer with AU+df06 and destination length 4, offset 0, filler 0
+Pass encodeInto() into SharedArrayBuffer with AU+df06 and destination length 4, offset 0, filler 0
+Pass encodeInto() into ArrayBuffer with AU+df06 and destination length 4, offset 4, filler 0
+Pass encodeInto() into SharedArrayBuffer with AU+df06 and destination length 4, offset 4, filler 0
+Pass encodeInto() into ArrayBuffer with AU+df06 and destination length 4, offset 0, filler 128
+Pass encodeInto() into SharedArrayBuffer with AU+df06 and destination length 4, offset 0, filler 128
+Pass encodeInto() into ArrayBuffer with AU+df06 and destination length 4, offset 4, filler 128
+Pass encodeInto() into SharedArrayBuffer with AU+df06 and destination length 4, offset 4, filler 128
+Pass encodeInto() into ArrayBuffer with AU+df06 and destination length 4, offset 0, filler random
+Pass encodeInto() into SharedArrayBuffer with AU+df06 and destination length 4, offset 0, filler random
+Pass encodeInto() into ArrayBuffer with AU+df06 and destination length 4, offset 4, filler random
+Pass encodeInto() into SharedArrayBuffer with AU+df06 and destination length 4, offset 4, filler random
+Pass encodeInto() into ArrayBuffer with ¥¥ and destination length 4, offset 0, filler 0
+Pass encodeInto() into SharedArrayBuffer with ¥¥ and destination length 4, offset 0, filler 0
+Pass encodeInto() into ArrayBuffer with ¥¥ and destination length 4, offset 4, filler 0
+Pass encodeInto() into SharedArrayBuffer with ¥¥ and destination length 4, offset 4, filler 0
+Pass encodeInto() into ArrayBuffer with ¥¥ and destination length 4, offset 0, filler 128
+Pass encodeInto() into SharedArrayBuffer with ¥¥ and destination length 4, offset 0, filler 128
+Pass encodeInto() into ArrayBuffer with ¥¥ and destination length 4, offset 4, filler 128
+Pass encodeInto() into SharedArrayBuffer with ¥¥ and destination length 4, offset 4, filler 128
+Pass encodeInto() into ArrayBuffer with ¥¥ and destination length 4, offset 0, filler random
+Pass encodeInto() into SharedArrayBuffer with ¥¥ and destination length 4, offset 0, filler random
+Pass encodeInto() into ArrayBuffer with ¥¥ and destination length 4, offset 4, filler random
+Pass encodeInto() into SharedArrayBuffer with ¥¥ and destination length 4, offset 4, filler random
+Pass Invalid encodeInto() destination: DataView, backed by: ArrayBuffer
+Pass Invalid encodeInto() destination: DataView, backed by: SharedArrayBuffer
+Pass Invalid encodeInto() destination: Int8Array, backed by: ArrayBuffer
+Pass Invalid encodeInto() destination: Int8Array, backed by: SharedArrayBuffer
+Pass Invalid encodeInto() destination: Int16Array, backed by: ArrayBuffer
+Pass Invalid encodeInto() destination: Int16Array, backed by: SharedArrayBuffer
+Pass Invalid encodeInto() destination: Int32Array, backed by: ArrayBuffer
+Pass Invalid encodeInto() destination: Int32Array, backed by: SharedArrayBuffer
+Pass Invalid encodeInto() destination: Uint16Array, backed by: ArrayBuffer
+Pass Invalid encodeInto() destination: Uint16Array, backed by: SharedArrayBuffer
+Pass Invalid encodeInto() destination: Uint32Array, backed by: ArrayBuffer
+Pass Invalid encodeInto() destination: Uint32Array, backed by: SharedArrayBuffer
+Pass Invalid encodeInto() destination: Uint8ClampedArray, backed by: ArrayBuffer
+Pass Invalid encodeInto() destination: Uint8ClampedArray, backed by: SharedArrayBuffer
+Pass Invalid encodeInto() destination: BigInt64Array, backed by: ArrayBuffer
+Pass Invalid encodeInto() destination: BigInt64Array, backed by: SharedArrayBuffer
+Pass Invalid encodeInto() destination: BigUint64Array, backed by: ArrayBuffer
+Pass Invalid encodeInto() destination: BigUint64Array, backed by: SharedArrayBuffer
+Pass Invalid encodeInto() destination: Float16Array, backed by: ArrayBuffer
+Pass Invalid encodeInto() destination: Float16Array, backed by: SharedArrayBuffer
+Pass Invalid encodeInto() destination: Float32Array, backed by: ArrayBuffer
+Pass Invalid encodeInto() destination: Float32Array, backed by: SharedArrayBuffer
+Pass Invalid encodeInto() destination: Float64Array, backed by: ArrayBuffer
+Pass Invalid encodeInto() destination: Float64Array, backed by: SharedArrayBuffer
+Pass Invalid encodeInto() destination: ArrayBuffer
+Pass Invalid encodeInto() destination: SharedArrayBuffer
+Pass encodeInto() and a detached output buffer
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/encoding/textencoder-constructor-non-utf.any.txt b/Tests/LibWeb/Text/expected/wpt-import/encoding/textencoder-constructor-non-utf.any.txt
new file mode 100644
index 00000000000..f975e4d1a4a
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/encoding/textencoder-constructor-non-utf.any.txt
@@ -0,0 +1,84 @@
+Harness status: OK
+
+Found 79 tests
+
+79 Pass
+Pass Encoding argument supported for decode: UTF-8
+Pass Encoding argument not considered for encode: UTF-8
+Pass Encoding argument supported for decode: IBM866
+Pass Encoding argument not considered for encode: IBM866
+Pass Encoding argument supported for decode: ISO-8859-2
+Pass Encoding argument not considered for encode: ISO-8859-2
+Pass Encoding argument supported for decode: ISO-8859-3
+Pass Encoding argument not considered for encode: ISO-8859-3
+Pass Encoding argument supported for decode: ISO-8859-4
+Pass Encoding argument not considered for encode: ISO-8859-4
+Pass Encoding argument supported for decode: ISO-8859-5
+Pass Encoding argument not considered for encode: ISO-8859-5
+Pass Encoding argument supported for decode: ISO-8859-6
+Pass Encoding argument not considered for encode: ISO-8859-6
+Pass Encoding argument supported for decode: ISO-8859-7
+Pass Encoding argument not considered for encode: ISO-8859-7
+Pass Encoding argument supported for decode: ISO-8859-8
+Pass Encoding argument not considered for encode: ISO-8859-8
+Pass Encoding argument supported for decode: ISO-8859-8-I
+Pass Encoding argument not considered for encode: ISO-8859-8-I
+Pass Encoding argument supported for decode: ISO-8859-10
+Pass Encoding argument not considered for encode: ISO-8859-10
+Pass Encoding argument supported for decode: ISO-8859-13
+Pass Encoding argument not considered for encode: ISO-8859-13
+Pass Encoding argument supported for decode: ISO-8859-14
+Pass Encoding argument not considered for encode: ISO-8859-14
+Pass Encoding argument supported for decode: ISO-8859-15
+Pass Encoding argument not considered for encode: ISO-8859-15
+Pass Encoding argument supported for decode: ISO-8859-16
+Pass Encoding argument not considered for encode: ISO-8859-16
+Pass Encoding argument supported for decode: KOI8-R
+Pass Encoding argument not considered for encode: KOI8-R
+Pass Encoding argument supported for decode: KOI8-U
+Pass Encoding argument not considered for encode: KOI8-U
+Pass Encoding argument supported for decode: macintosh
+Pass Encoding argument not considered for encode: macintosh
+Pass Encoding argument supported for decode: windows-874
+Pass Encoding argument not considered for encode: windows-874
+Pass Encoding argument supported for decode: windows-1250
+Pass Encoding argument not considered for encode: windows-1250
+Pass Encoding argument supported for decode: windows-1251
+Pass Encoding argument not considered for encode: windows-1251
+Pass Encoding argument supported for decode: windows-1252
+Pass Encoding argument not considered for encode: windows-1252
+Pass Encoding argument supported for decode: windows-1253
+Pass Encoding argument not considered for encode: windows-1253
+Pass Encoding argument supported for decode: windows-1254
+Pass Encoding argument not considered for encode: windows-1254
+Pass Encoding argument supported for decode: windows-1255
+Pass Encoding argument not considered for encode: windows-1255
+Pass Encoding argument supported for decode: windows-1256
+Pass Encoding argument not considered for encode: windows-1256
+Pass Encoding argument supported for decode: windows-1257
+Pass Encoding argument not considered for encode: windows-1257
+Pass Encoding argument supported for decode: windows-1258
+Pass Encoding argument not considered for encode: windows-1258
+Pass Encoding argument supported for decode: x-mac-cyrillic
+Pass Encoding argument not considered for encode: x-mac-cyrillic
+Pass Encoding argument supported for decode: GBK
+Pass Encoding argument not considered for encode: GBK
+Pass Encoding argument supported for decode: gb18030
+Pass Encoding argument not considered for encode: gb18030
+Pass Encoding argument supported for decode: Big5
+Pass Encoding argument not considered for encode: Big5
+Pass Encoding argument supported for decode: EUC-JP
+Pass Encoding argument not considered for encode: EUC-JP
+Pass Encoding argument supported for decode: ISO-2022-JP
+Pass Encoding argument not considered for encode: ISO-2022-JP
+Pass Encoding argument supported for decode: Shift_JIS
+Pass Encoding argument not considered for encode: Shift_JIS
+Pass Encoding argument supported for decode: EUC-KR
+Pass Encoding argument not considered for encode: EUC-KR
+Pass Encoding argument not considered for encode: replacement
+Pass Encoding argument supported for decode: UTF-16BE
+Pass Encoding argument not considered for encode: UTF-16BE
+Pass Encoding argument supported for decode: UTF-16LE
+Pass Encoding argument not considered for encode: UTF-16LE
+Pass Encoding argument supported for decode: x-user-defined
+Pass Encoding argument not considered for encode: x-user-defined
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/encoding/textencoder-utf16-surrogates.any.txt b/Tests/LibWeb/Text/expected/wpt-import/encoding/textencoder-utf16-surrogates.any.txt
new file mode 100644
index 00000000000..eb185770bec
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/encoding/textencoder-utf16-surrogates.any.txt
@@ -0,0 +1,12 @@
+Harness status: OK
+
+Found 7 tests
+
+7 Pass
+Pass USVString handling: lone surrogate lead
+Pass USVString handling: lone surrogate trail
+Pass USVString handling: unmatched surrogate lead
+Pass USVString handling: unmatched surrogate trail
+Pass USVString handling: swapped surrogate pair
+Pass USVString handling: properly encoded MUSICAL SYMBOL G CLEF (U+1D11E)
+Pass USVString default
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/Encoding/TextEncoder-wrapper.html b/Tests/LibWeb/Text/input/Encoding/TextEncoder-wrapper.html
new file mode 100644
index 00000000000..153c637252b
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Encoding/TextEncoder-wrapper.html
@@ -0,0 +1,103 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<script src="../include.js"></script>
+<script>
+ function describe(f) {
+ try {
+ return JSON.stringify(f());
+ } catch (e) {
+ return `${e.name}: ${e.message}`;
+ }
+ }
+
+ function describeProperty(object, key) {
+ const descriptor = Object.getOwnPropertyDescriptor(object, key);
+ const parts = [];
+ for (const [name, value] of Object.entries(descriptor)) {
+ if (typeof value === "function")
+ parts.push(`${name}: function "${value.name}" length ${value.length}`);
+ else if (value !== undefined)
+ parts.push(`${name}: ${String(value)}`);
+ }
+ return `${String(key)} { ${parts.join(", ")} }`;
+ }
+
+ test(() => {
+ println("TextEncoder.prototype:");
+ for (const key of Reflect.ownKeys(TextEncoder.prototype))
+ println(` ${describeProperty(TextEncoder.prototype, key)}`);
+ println("TextEncoder:");
+ for (const key of Reflect.ownKeys(TextEncoder)) {
+ if (key !== "prototype")
+ println(` ${describeProperty(TextEncoder, key)}`);
+ }
+ println(`prototype of TextEncoder.prototype is Object.prototype: ${Object.getPrototypeOf(TextEncoder.prototype) === Object.prototype}`);
+ println(`Object.prototype.toString of an instance: ${Object.prototype.toString.call(new TextEncoder())}`);
+ println(`own keys of an instance: ${Reflect.ownKeys(new TextEncoder()).length}`);
+
+ println(`encode(): ${describe(() => Array.from(new TextEncoder().encode()))}`);
+ println(`encode(undefined): ${describe(() => Array.from(new TextEncoder().encode(undefined)))}`);
+ println(`encode(null): ${describe(() => Array.from(new TextEncoder().encode(null)))}`);
+ println(`encode of a lone surrogate: ${describe(() => Array.from(new TextEncoder().encode("a\uD800b")))}`);
+ println(`encode of a symbol: ${describe(() => new TextEncoder().encode(Symbol("s")))}`);
+
+ println(`calling without new: ${describe(() => TextEncoder())}`);
+ println(`encode on a plain object: ${describe(() => TextEncoder.prototype.encode.call({}, "x"))}`);
+ println(`encode on undefined: ${describe(() => TextEncoder.prototype.encode.call(undefined, "x"))}`);
+ println(`encoding getter on a TextDecoder: ${describe(() => Object.getOwnPropertyDescriptor(TextEncoder.prototype, "encoding").get.call(new TextDecoder()))}`);
+ println(`encodeInto with one argument: ${describe(() => new TextEncoder().encodeInto("x"))}`);
+ println(`encodeInto into an ArrayBuffer: ${describe(() => new TextEncoder().encodeInto("x", new ArrayBuffer(4)))}`);
+ println(`encodeInto into an Int8Array: ${describe(() => new TextEncoder().encodeInto("x", new Int8Array(4)))}`);
+ println(`encodeInto into a resizable buffer: ${describe(() => new TextEncoder().encodeInto("x", new Uint8Array(new ArrayBuffer(4, { maxByteLength: 8 }))))}`);
+ println(`encodeInto into a detached buffer: ${describe(() => {
+ const buffer = new ArrayBuffer(4);
+ const view = new Uint8Array(buffer);
+ structuredClone(buffer, { transfer: [buffer] });
+ return new TextEncoder().encodeInto("x", view);
+ })}`);
+ println(`encodeInto stops before a code point that does not fit: ${describe(() => {
+ const view = new Uint8Array(5);
+ const result = new TextEncoder().encodeInto("ab\u{1F600}c", view);
+ return [result, Array.from(view)];
+ })}`);
+ println(`encodeInto at an offset: ${describe(() => {
+ const view = new Uint8Array(new ArrayBuffer(6), 2, 3);
+ const result = new TextEncoder().encodeInto("éé", view);
+ return [result, Array.from(new Uint8Array(view.buffer))];
+ })}`);
+
+ println(`structuredClone: ${describe(() => structuredClone(new TextEncoder()))}`);
+ println(`passed as a Node: ${describe(() => document.body.appendChild(new TextEncoder()))}`);
+
+ class SubEncoder extends TextEncoder {
+ lengths(string) {
+ return [this.encode(string).length, this.encoding];
+ }
+ }
+ const sub = new SubEncoder();
+ println(`subclass: ${sub instanceof SubEncoder} ${sub instanceof TextEncoder} ${JSON.stringify(sub.lengths("ab"))}`);
+
+ const iframe = document.createElement("iframe");
+ document.body.appendChild(iframe);
+ const otherWindow = iframe.contentWindow;
+ const newTarget = new otherWindow.Function();
+ newTarget.prototype = 1;
+ const constructed = Reflect.construct(TextEncoder, [], newTarget);
+ println(`prototype from the realm of newTarget: ${Object.getPrototypeOf(constructed) === otherWindow.TextEncoder.prototype}`);
+ const encoded = TextEncoder.prototype.encode.call(constructed, "x");
+ println(`encode result realm: ${encoded instanceof Uint8Array} ${encoded instanceof otherWindow.Uint8Array}`);
+ iframe.remove();
+
+ const encoders = [];
+ for (let i = 0; i < 100; ++i) {
+ const encoder = new TextEncoder();
+ encoder.expando = i;
+ encoders.push(encoder);
+ }
+ Object.setPrototypeOf(encoders[1], null);
+ Object.preventExtensions(encoders[2]);
+ internals.gc();
+ const allWork = encoders.every((encoder, i) => encoder.expando === i && TextEncoder.prototype.encode.call(encoder, "ab").length === 2);
+ println(`encoders work after a garbage collection: ${allWork}`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/encoding/api-basics.any.html b/Tests/LibWeb/Text/input/wpt-import/encoding/api-basics.any.html
new file mode 100644
index 00000000000..547050405e0
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/encoding/api-basics.any.html
@@ -0,0 +1,15 @@
+<!doctype html>
+<meta charset=utf-8>
+<title>Encoding API: Basics</title>
+<script>
+self.GLOBAL = {
+ isWindow: function() { return true; },
+ isWorker: function() { return false; },
+ isShadowRealm: function() { return false; },
+};
+</script>
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+
+<div id=log></div>
+<script src="../encoding/api-basics.any.js"></script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/encoding/api-basics.any.js b/Tests/LibWeb/Text/input/wpt-import/encoding/api-basics.any.js
new file mode 100644
index 00000000000..9c7fc879b77
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/encoding/api-basics.any.js
@@ -0,0 +1,53 @@
+// META: global=window,dedicatedworker
+// META: title=Encoding API: Basics
+
+test(function() {
+ assert_equals((new TextEncoder).encoding, 'utf-8', 'default encoding is utf-8');
+ assert_equals((new TextDecoder).encoding, 'utf-8', 'default encoding is utf-8');
+}, 'Default encodings');
+
+test(function() {
+ assert_array_equals(new TextEncoder().encode(), [], 'input default should be empty string')
+ assert_array_equals(new TextEncoder().encode(undefined), [], 'input default should be empty string')
+}, 'Default inputs');
+
+
+function testDecodeSample(encoding, string, bytes) {
+ test(function() {
+ assert_equals(new TextDecoder(encoding).decode(new Uint8Array(bytes)), string);
+ assert_equals(new TextDecoder(encoding).decode(new Uint8Array(bytes).buffer), string);
+ }, 'Decode sample: ' + encoding);
+}
+
+// z (ASCII U+007A), cent (Latin-1 U+00A2), CJK water (BMP U+6C34),
+// G-Clef (non-BMP U+1D11E), PUA (BMP U+F8FF), PUA (non-BMP U+10FFFD)
+// byte-swapped BOM (non-character U+FFFE)
+var sample = 'z\xA2\u6C34\uD834\uDD1E\uF8FF\uDBFF\uDFFD\uFFFE';
+
+test(function() {
+ var encoding = 'utf-8';
+ var string = sample;
+ var bytes = [0x7A, 0xC2, 0xA2, 0xE6, 0xB0, 0xB4, 0xF0, 0x9D, 0x84, 0x9E, 0xEF, 0xA3, 0xBF, 0xF4, 0x8F, 0xBF, 0xBD, 0xEF, 0xBF, 0xBE];
+ var encoded = new TextEncoder().encode(string);
+ assert_array_equals([].slice.call(encoded), bytes);
+ assert_equals(new TextDecoder(encoding).decode(new Uint8Array(bytes)), string);
+ assert_equals(new TextDecoder(encoding).decode(new Uint8Array(bytes).buffer), string);
+}, 'Encode/decode round trip: utf-8');
+
+testDecodeSample(
+ 'utf-16le',
+ sample,
+ [0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C, 0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xF8, 0xFF, 0xDB, 0xFD, 0xDF, 0xFE, 0xFF]
+);
+
+testDecodeSample(
+ 'utf-16be',
+ sample,
+ [0x00, 0x7A, 0x00, 0xA2, 0x6C, 0x34, 0xD8, 0x34, 0xDD, 0x1E, 0xF8, 0xFF, 0xDB, 0xFF, 0xDF, 0xFD, 0xFF, 0xFE]
+);
+
+testDecodeSample(
+ 'utf-16',
+ sample,
+ [0x7A, 0x00, 0xA2, 0x00, 0x34, 0x6C, 0x34, 0xD8, 0x1E, 0xDD, 0xFF, 0xF8, 0xFF, 0xDB, 0xFD, 0xDF, 0xFE, 0xFF]
+);
diff --git a/Tests/LibWeb/Text/input/wpt-import/encoding/api-basics.any.worker.html b/Tests/LibWeb/Text/input/wpt-import/encoding/api-basics.any.worker.html
new file mode 100644
index 00000000000..64fec701d07
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/encoding/api-basics.any.worker.html
@@ -0,0 +1,9 @@
+<!doctype html>
+<meta charset=utf-8>
+<title>Encoding API: Basics</title>
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<div id=log></div>
+<script>
+fetch_tests_from_worker(new Worker("../encoding/api-basics.any.worker.js"));
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/encoding/api-basics.any.worker.js b/Tests/LibWeb/Text/input/wpt-import/encoding/api-basics.any.worker.js
new file mode 100644
index 00000000000..e976c257de6
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/encoding/api-basics.any.worker.js
@@ -0,0 +1,10 @@
+
+self.GLOBAL = {
+ isWindow: function() { return false; },
+ isWorker: function() { return true; },
+ isShadowRealm: function() { return false; },
+};
+importScripts("/resources/testharness.js");
+self.META_TITLE = "Encoding API: Basics";
+importScripts("/encoding/api-basics.any.js");
+done();
diff --git a/Tests/LibWeb/Text/input/wpt-import/encoding/api-surrogates-utf8.any.html b/Tests/LibWeb/Text/input/wpt-import/encoding/api-surrogates-utf8.any.html
new file mode 100644
index 00000000000..6904e10d5dd
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/encoding/api-surrogates-utf8.any.html
@@ -0,0 +1,15 @@
+<!doctype html>
+<meta charset=utf-8>
+<title>Encoding API: Invalid UTF-16 surrogates with UTF-8 encoding</title>
+<script>
+self.GLOBAL = {
+ isWindow: function() { return true; },
+ isWorker: function() { return false; },
+ isShadowRealm: function() { return false; },
+};
+</script>
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+
+<div id=log></div>
+<script src="../encoding/api-surrogates-utf8.any.js"></script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/encoding/api-surrogates-utf8.any.js b/Tests/LibWeb/Text/input/wpt-import/encoding/api-surrogates-utf8.any.js
new file mode 100644
index 00000000000..24e3a8a0c63
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/encoding/api-surrogates-utf8.any.js
@@ -0,0 +1,49 @@
+// META: global=window,dedicatedworker
+// META: title=Encoding API: Invalid UTF-16 surrogates with UTF-8 encoding
+
+var badStrings = [
+ {
+ input: 'abc123',
+ expected: [0x61, 0x62, 0x63, 0x31, 0x32, 0x33],
+ decoded: 'abc123',
+ name: 'Sanity check'
+ },
+ {
+ input: '\uD800',
+ expected: [0xef, 0xbf, 0xbd],
+ decoded: '\uFFFD',
+ name: 'Surrogate half (low)'
+ },
+ {
+ input: '\uDC00',
+ expected: [0xef, 0xbf, 0xbd],
+ decoded: '\uFFFD',
+ name: 'Surrogate half (high)'
+ },
+ {
+ input: 'abc\uD800123',
+ expected: [0x61, 0x62, 0x63, 0xef, 0xbf, 0xbd, 0x31, 0x32, 0x33],
+ decoded: 'abc\uFFFD123',
+ name: 'Surrogate half (low), in a string'
+ },
+ {
+ input: 'abc\uDC00123',
+ expected: [0x61, 0x62, 0x63, 0xef, 0xbf, 0xbd, 0x31, 0x32, 0x33],
+ decoded: 'abc\uFFFD123',
+ name: 'Surrogate half (high), in a string'
+ },
+ {
+ input: '\uDC00\uD800',
+ expected: [0xef, 0xbf, 0xbd, 0xef, 0xbf, 0xbd],
+ decoded: '\uFFFD\uFFFD',
+ name: 'Wrong order'
+ }
+];
+
+badStrings.forEach(function(t) {
+ test(function() {
+ var encoded = new TextEncoder().encode(t.input);
+ assert_array_equals([].slice.call(encoded), t.expected);
+ assert_equals(new TextDecoder('utf-8').decode(encoded), t.decoded);
+ }, 'Invalid surrogates encoded into UTF-8: ' + t.name);
+});
diff --git a/Tests/LibWeb/Text/input/wpt-import/encoding/encodeInto.any.worker.html b/Tests/LibWeb/Text/input/wpt-import/encoding/encodeInto.any.worker.html
new file mode 100644
index 00000000000..f77c87b0279
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/encoding/encodeInto.any.worker.html
@@ -0,0 +1,9 @@
+<!doctype html>
+<meta charset=utf-8>
+
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<div id=log></div>
+<script>
+fetch_tests_from_worker(new Worker("../encoding/encodeInto.any.worker.js"));
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/encoding/encodeInto.any.worker.js b/Tests/LibWeb/Text/input/wpt-import/encoding/encodeInto.any.worker.js
new file mode 100644
index 00000000000..ffb444883a1
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/encoding/encodeInto.any.worker.js
@@ -0,0 +1,10 @@
+
+self.GLOBAL = {
+ isWindow: function() { return false; },
+ isWorker: function() { return true; },
+ isShadowRealm: function() { return false; },
+};
+importScripts("/resources/testharness.js");
+importScripts("/common/sab.js")
+importScripts("/encoding/encodeInto.any.js");
+done();
diff --git a/Tests/LibWeb/Text/input/wpt-import/encoding/textencoder-constructor-non-utf.any.html b/Tests/LibWeb/Text/input/wpt-import/encoding/textencoder-constructor-non-utf.any.html
new file mode 100644
index 00000000000..19575470546
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/encoding/textencoder-constructor-non-utf.any.html
@@ -0,0 +1,15 @@
+<!doctype html>
+<meta charset=utf-8>
+<title>Encoding API: Legacy encodings</title>
+<script>
+self.GLOBAL = {
+ isWindow: function() { return true; },
+ isWorker: function() { return false; },
+ isShadowRealm: function() { return false; },
+};
+</script>
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<script src="resources/encodings.js"></script>
+<div id=log></div>
+<script src="../encoding/textencoder-constructor-non-utf.any.js"></script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/encoding/textencoder-constructor-non-utf.any.js b/Tests/LibWeb/Text/input/wpt-import/encoding/textencoder-constructor-non-utf.any.js
new file mode 100644
index 00000000000..c7136da4dce
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/encoding/textencoder-constructor-non-utf.any.js
@@ -0,0 +1,16 @@
+// META: title=Encoding API: Legacy encodings
+// META: script=resources/encodings.js
+
+encodings_table.forEach(function(section) {
+ section.encodings.forEach(function(encoding) {
+ if (encoding.name !== 'replacement') {
+ test(function() {
+ assert_equals(new TextDecoder(encoding.name).encoding, encoding.name.toLowerCase()); // ASCII names only, so safe
+ }, 'Encoding argument supported for decode: ' + encoding.name);
+ }
+
+ test(function() {
+ assert_equals(new TextEncoder(encoding.name).encoding, 'utf-8');
+ }, 'Encoding argument not considered for encode: ' + encoding.name);
+ });
+});
diff --git a/Tests/LibWeb/Text/input/wpt-import/encoding/textencoder-utf16-surrogates.any.html b/Tests/LibWeb/Text/input/wpt-import/encoding/textencoder-utf16-surrogates.any.html
new file mode 100644
index 00000000000..26847d51277
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/encoding/textencoder-utf16-surrogates.any.html
@@ -0,0 +1,15 @@
+<!doctype html>
+<meta charset=utf-8>
+<title>Encoding API: USVString surrogate handling when encoding</title>
+<script>
+self.GLOBAL = {
+ isWindow: function() { return true; },
+ isWorker: function() { return false; },
+ isShadowRealm: function() { return false; },
+};
+</script>
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+
+<div id=log></div>
+<script src="../encoding/textencoder-utf16-surrogates.any.js"></script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/encoding/textencoder-utf16-surrogates.any.js b/Tests/LibWeb/Text/input/wpt-import/encoding/textencoder-utf16-surrogates.any.js
new file mode 100644
index 00000000000..b7294f31474
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/encoding/textencoder-utf16-surrogates.any.js
@@ -0,0 +1,47 @@
+// META: global=window,dedicatedworker
+// META: title=Encoding API: USVString surrogate handling when encoding
+
+var bad = [
+ {
+ input: '\uD800',
+ expected: '\uFFFD',
+ name: 'lone surrogate lead'
+ },
+ {
+ input: '\uDC00',
+ expected: '\uFFFD',
+ name: 'lone surrogate trail'
+ },
+ {
+ input: '\uD800\u0000',
+ expected: '\uFFFD\u0000',
+ name: 'unmatched surrogate lead'
+ },
+ {
+ input: '\uDC00\u0000',
+ expected: '\uFFFD\u0000',
+ name: 'unmatched surrogate trail'
+ },
+ {
+ input: '\uDC00\uD800',
+ expected: '\uFFFD\uFFFD',
+ name: 'swapped surrogate pair'
+ },
+ {
+ input: '\uD834\uDD1E',
+ expected: '\uD834\uDD1E',
+ name: 'properly encoded MUSICAL SYMBOL G CLEF (U+1D11E)'
+ }
+];
+
+bad.forEach(function(t) {
+ test(function() {
+ var encoded = new TextEncoder().encode(t.input);
+ var decoded = new TextDecoder().decode(encoded);
+ assert_equals(decoded, t.expected);
+ }, 'USVString handling: ' + t.name);
+});
+
+test(function() {
+ assert_equals(new TextEncoder().encode().length, 0, 'Should default to empty string');
+}, 'USVString default');