Skip to content

Truly independent web browser

LibWeb: Don't sniff a UTF-16 BOM in worker and module scripts

Shannon Booth077dbb375bd8
diff --git a/Libraries/LibWeb/HTML/Scripting/Fetching.cpp b/Libraries/LibWeb/HTML/Scripting/Fetching.cpp
index 86a9bc4e9c4..d56e7da3b27 100644
--- a/Libraries/LibWeb/HTML/Scripting/Fetching.cpp
+++ b/Libraries/LibWeb/HTML/Scripting/Fetching.cpp
@@ -858,9 +858,7 @@ WebIDL::ExceptionOr<void> fetch_classic_worker_script(URL::URL const& url, Envir
// We might be able to tighten this in the future; see https://github.com/whatwg/html/issues/3255.
// 4. Let sourceText be the result of UTF-8 decoding bodyBytes.
- auto decoder = TextCodec::decoder_for("UTF-8"sv);
- VERIFY(decoder.has_value());
- auto source_text = decode_source_text_to_utf16(*decoder, body_bytes_view(body_bytes)).release_value_but_fixme_should_propagate_errors();
+ auto source_text = Utf16String::from_utf8_with_replacement_character(body_bytes_view(body_bytes));
// 5. Let script be the result of creating a classic script using sourceText, settingsObject,
// response's URL, and the default classic script fetch options.
@@ -947,9 +945,7 @@ WebIDL::ExceptionOr<GC::Ref<ClassicScript>> fetch_a_classic_worker_imported_scri
}
// 8. Let sourceText be the result of UTF-8 decoding bodyBytes.
- auto decoder = TextCodec::decoder_for("UTF-8"sv);
- VERIFY(decoder.has_value());
- auto source_text = decode_source_text_to_utf16(*decoder, body_bytes_view(body_bytes)).release_value_but_fixme_should_propagate_errors();
+ auto source_text = Utf16String::from_utf8_with_replacement_character(body_bytes_view(body_bytes));
// 9. Let mutedErrors be true if response was CORS-cross-origin, and false otherwise.
auto muted_errors = response->is_cors_cross_origin() ? ClassicScript::MutedErrors::Yes : ClassicScript::MutedErrors::No;
@@ -1132,13 +1128,12 @@ void fetch_single_module_script(JS::Realm& realm,
// 7. Otherwise
else {
// 1. Let sourceText be the result of UTF-8 decoding bodyBytes.
- auto decoder = TextCodec::decoder_for("UTF-8"sv);
- VERIFY(decoder.has_value());
+ // NB: Done at each use, so a JavaScript module with a bytecode cache isn't decoded up front.
// 2. If moduleType is "text", then set moduleScript to the result of creating a text module script given
// sourceText and settingsObject.
if (module_type == "text"sv) {
- auto source_text = decode_source_text_to_utf16(*decoder, body_bytes_view(body_bytes)).release_value_but_fixme_should_propagate_errors();
+ auto source_text = Utf16String::from_utf8_with_replacement_character(body_bytes_view(body_bytes));
module_script = ModuleScript::create_a_text_module_script(url.to_byte_string(), source_text.utf16_view(), settings_object).release_value_but_fixme_should_propagate_errors();
}
@@ -1160,7 +1155,9 @@ void fetch_single_module_script(JS::Realm& realm,
if (bytecode.has_value() || bytecode_cache_context.has_value())
source_hash = bytecode_cache_source_hash(source_bytes, "UTF-8"sv);
if (bytecode.has_value()) {
- auto source_length = TextCodec::convert_input_to_utf16_length_using_given_decoder_unless_there_is_a_byte_order_mark(*decoder, StringView { source_bytes }).release_value_but_fixme_should_propagate_errors();
+ auto decoder = TextCodec::decoder_for("UTF-8"sv);
+ VERIFY(decoder.has_value());
+ auto source_length = decoder->length_in_utf16_code_units(StringView { source_bytes }).release_value_but_fixme_should_propagate_errors();
prepare_bytecode_cache_off_thread(settings_object, *bytecode, JS::ProgramType::Module, source_length, *source_hash,
[url = move(url), url_string = move(url_string), response_url = move(response_url),
module_type_string = move(module_type_string),
@@ -1185,11 +1182,9 @@ void fetch_single_module_script(JS::Realm& realm,
}
if (!source_code.has_value()) {
- auto fallback_decoder = TextCodec::decoder_for("UTF-8"sv);
- VERIFY(fallback_decoder.has_value());
source_code = JS::SourceCode::create(
utf16_string_from_url_ascii(url_string.view()),
- decode_source_text_to_utf16(*fallback_decoder, source_byte_storage.bytes()).release_value_but_fixme_should_propagate_errors());
+ Utf16String::from_utf8_with_replacement_character(source_byte_storage.bytes()));
}
auto retain_parse_for_cache = bytecode_cache_context.has_value();
@@ -1229,7 +1224,7 @@ void fetch_single_module_script(JS::Realm& realm,
if (!source_code.has_value()) {
source_code = JS::SourceCode::create(
utf16_string_from_url_ascii(url_string.view()),
- decode_source_text_to_utf16(*decoder, source_bytes).release_value_but_fixme_should_propagate_errors());
+ Utf16String::from_utf8_with_replacement_character(source_bytes));
}
auto retain_parse_for_cache = bytecode_cache_context.has_value();
@@ -1267,14 +1262,14 @@ void fetch_single_module_script(JS::Realm& realm,
// 4. If the MIME type essence of mimeType is "text/css" and moduleType is "css", then set moduleScript to
// the result of creating a CSS module script given sourceText and settingsObject.
if (mime_type.has_value() && mime_type->essence() == "text/css"sv && module_type == "css"sv) {
- auto source_text = decode_source_text_to_utf16(*decoder, body_bytes_view(body_bytes)).release_value_but_fixme_should_propagate_errors();
+ auto source_text = Utf16String::from_utf8_with_replacement_character(body_bytes_view(body_bytes));
module_script = ModuleScript::create_a_css_module_script(url.to_byte_string(), source_text.utf16_view(), settings_object).release_value_but_fixme_should_propagate_errors();
}
// 5. If mimeType is a JSON MIME type and moduleType is "json", then set moduleScript to the result of
// creating a JSON module script given sourceText and settingsObject.
if (mime_type.has_value() && mime_type->is_json() && module_type == "json"sv) {
- auto source_text = decode_source_text_to_utf16(*decoder, body_bytes_view(body_bytes)).release_value_but_fixme_should_propagate_errors();
+ auto source_text = Utf16String::from_utf8_with_replacement_character(body_bytes_view(body_bytes));
module_script = ModuleScript::create_a_json_module_script(url.to_byte_string(), source_text.utf16_view(), settings_object).release_value_but_fixme_should_propagate_errors();
}
}
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index 9f653cfdb5c..5edbddf3ce2 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -259,6 +259,12 @@ Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/i
Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/import-css-module-basic.html
Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/css-module-worker-test.html
Text/input/wpt-import/html/semantics/scripting-1/the-script-element/json-module/module.html
+Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/charset-bom.html
+Text/input/wpt-import/html/semantics/scripting-1/the-script-element/json-module/charset-bom.any.html
+Text/input/wpt-import/html/semantics/scripting-1/the-script-element/json-module/charset-bom.any.worker.html
+Text/input/wpt-import/html/semantics/scripting-1/the-script-element/module/charset-02.html
+Text/input/wpt-import/html/semantics/scripting-1/the-script-element/text-module/charset-bom.any.html
+Text/input/wpt-import/html/semantics/scripting-1/the-script-element/text-module/charset-bom.any.worker.html
Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/evaluation-order-2.any.worker-module.html
Text/input/wpt-import/wasm/webapi/esm-integration/wasm-import.tentative.html
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/css-module/charset-bom.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/css-module/charset-bom.txt
new file mode 100644
index 00000000000..66aeb826b7d
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/css-module/charset-bom.txt
@@ -0,0 +1,8 @@
+Harness status: OK
+
+Found 3 tests
+
+3 Pass
+Pass UTF-8 BOM should be stripped when decoding CSS module script
+Pass UTF-16BE BOM should be ignored, so CSS module should be UTF-8 decoded
+Pass UTF-16LE BOM should be ignored, so CSS module should be UTF-8 decoded
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/json-module/charset-bom.any.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/json-module/charset-bom.any.txt
new file mode 100644
index 00000000000..c1599dfacd6
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/json-module/charset-bom.any.txt
@@ -0,0 +1,8 @@
+Harness status: OK
+
+Found 3 tests
+
+3 Pass
+Pass UTF-8 BOM should be stripped when decoding JSON module script
+Pass UTF-16BE BOM should result in parse error in JSON module script
+Pass UTF-16LE BOM should result in parse error in JSON module script
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/json-module/charset-bom.any.worker.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/json-module/charset-bom.any.worker.txt
new file mode 100644
index 00000000000..c1599dfacd6
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/json-module/charset-bom.any.worker.txt
@@ -0,0 +1,8 @@
+Harness status: OK
+
+Found 3 tests
+
+3 Pass
+Pass UTF-8 BOM should be stripped when decoding JSON module script
+Pass UTF-16BE BOM should result in parse error in JSON module script
+Pass UTF-16LE BOM should result in parse error in JSON module script
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/module/charset-02.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/module/charset-02.txt
new file mode 100644
index 00000000000..543f1311e9c
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/module/charset-02.txt
@@ -0,0 +1,8 @@
+Harness status: OK
+
+Found 3 tests
+
+3 Pass
+Pass UTF-8 module script with UTF-8 BOM
+Pass UTF-16 module script with UTF-16BE BOM
+Pass UTF-16 module script with UTF-16LE BOM
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/text-module/charset-bom.any.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/text-module/charset-bom.any.txt
new file mode 100644
index 00000000000..8cf2dbf3a92
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/text-module/charset-bom.any.txt
@@ -0,0 +1,8 @@
+Harness status: OK
+
+Found 3 tests
+
+3 Pass
+Pass UTF-8 BOM should be stripped when decoding text module script
+Pass UTF-16BE with BOM should be parsed as UTF-8
+Pass UTF-16LE with BOM should be parsed as UTF-8
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/text-module/charset-bom.any.worker.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/text-module/charset-bom.any.worker.txt
new file mode 100644
index 00000000000..8cf2dbf3a92
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/text-module/charset-bom.any.worker.txt
@@ -0,0 +1,8 @@
+Harness status: OK
+
+Found 3 tests
+
+3 Pass
+Pass UTF-8 BOM should be stripped when decoding text module script
+Pass UTF-16BE with BOM should be parsed as UTF-8
+Pass UTF-16LE with BOM should be parsed as UTF-8
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/workers/constructors/Worker/constructor-utf16-bom.txt b/Tests/LibWeb/Text/expected/wpt-import/workers/constructors/Worker/constructor-utf16-bom.txt
new file mode 100644
index 00000000000..19c951e68ea
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/workers/constructors/Worker/constructor-utf16-bom.txt
@@ -0,0 +1,7 @@
+Harness status: OK
+
+Found 2 tests
+
+2 Pass
+Pass Worker with UTF-16BE BOM
+Pass Worker with UTF-16LE BOM
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/charset-bom.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/charset-bom.html
new file mode 100644
index 00000000000..56256ba64b2
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/charset-bom.html
@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+<title>CSS Module scripts should ignore BOMs and always use UTF-8</title>
+<script src="../../../../../resources/testharness.js"></script>
+<script src="../../../../../resources/testharnessreport.js"></script>
+<script type="module">
+ import utf8BOMSheet from './resources/bom-utf-8.css' with { type: 'css' };
+ test(function() {
+ assert_equals(utf8BOMSheet.rules[0].selectorText, 'div', 'No UTF-8 BOM expected in selector');
+ }, 'UTF-8 BOM should be stripped when decoding CSS module script');
+
+ import utf16BEBOMSheet from './resources/bom-utf-16be.css' with { type: 'css' };
+ test(function() {
+ assert_equals(utf16BEBOMSheet.rules[0].selectorText, '\ufffd\ufffd\ufffdd\ufffdi\ufffdv\ufffd \ufffd', 'Expected UTF-8 decoded selectorText with 0xfffd replacement characters');
+ }, 'UTF-16BE BOM should be ignored, so CSS module should be UTF-8 decoded');
+
+ import utf16LEBOMSheet from './resources/bom-utf-16le.css' with { type: 'css' };
+ test(function() {
+ assert_equals(utf16LEBOMSheet.rules[0].selectorText, '\ufffd\ufffdd\ufffdi\ufffdv\ufffd \ufffd', 'Expected UTF-8 decoded selectorText with 0xfffd replacement characters');
+ }, 'UTF-16LE BOM should be ignored, so CSS module should be UTF-8 decoded');
+</script>
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/resources/bom-utf-16be.css b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/resources/bom-utf-16be.css
new file mode 100644
index 00000000000..9e17902a1d3
Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/resources/bom-utf-16be.css differ
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/resources/bom-utf-16le.css b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/resources/bom-utf-16le.css
new file mode 100644
index 00000000000..ef90843d8ed
Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/resources/bom-utf-16le.css differ
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/resources/bom-utf-8.css b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/resources/bom-utf-8.css
new file mode 100644
index 00000000000..5cf81232b7a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/css-module/resources/bom-utf-8.css
@@ -0,0 +1 @@
+div { background-color: blue; }
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/json-module/bom-utf-16be.json b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/json-module/bom-utf-16be.json
new file mode 100644
index 00000000000..d22a45a5910
Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/json-module/bom-utf-16be.json differ
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/json-module/bom-utf-16le.json b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/json-module/bom-utf-16le.json
new file mode 100644
index 00000000000..4d1aa264a69
Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/json-module/bom-utf-16le.json differ
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/json-module/bom-utf-8.json b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/json-module/bom-utf-8.json
new file mode 100644
index 00000000000..07ba933e860
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/json-module/bom-utf-8.json
@@ -0,0 +1 @@
+{ "data": "hello" }
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/json-module/charset-bom.any.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/json-module/charset-bom.any.html
new file mode 100644
index 00000000000..4b63f6989ca
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/json-module/charset-bom.any.html
@@ -0,0 +1,15 @@
+<!doctype html>
+<meta charset=utf-8>
+
+<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="../../../../../common/utils.js"></script>
+<div id=log></div>
+<script src="../../../../../html/semantics/scripting-1/the-script-element/json-module/charset-bom.any.js"></script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/json-module/charset-bom.any.js b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/json-module/charset-bom.any.js
new file mode 100644
index 00000000000..483936a4f70
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/json-module/charset-bom.any.js
@@ -0,0 +1,17 @@
+// META: global=window,dedicatedworker,sharedworker
+// META: script=/common/utils.js
+
+promise_test(async () => {
+ const jsonModule = await import('./bom-utf-8.json', { with: { type: 'json' } });
+ assert_equals(jsonModule.default.data, 'hello');
+}, 'UTF-8 BOM should be stripped when decoding JSON module script');
+
+promise_test(async test => {
+ await promise_rejects_js(test, SyntaxError,
+ import('./bom-utf-16be.json', { with: { type: 'json' } }), 'Expected parse error from UTF-16BE BOM');
+}, 'UTF-16BE BOM should result in parse error in JSON module script');
+
+promise_test(async test => {
+ await promise_rejects_js(test, SyntaxError,
+ import('./bom-utf-16le.json', { with: { type: 'json' } }), 'Expected parse error from UTF-16LE BOM');
+}, 'UTF-16LE BOM should result in parse error in JSON module script');
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/json-module/charset-bom.any.worker.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/json-module/charset-bom.any.worker.html
new file mode 100644
index 00000000000..97c4b66f6e0
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/json-module/charset-bom.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("../../../../../html/semantics/scripting-1/the-script-element/json-module/charset-bom.any.worker.js"));
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/json-module/charset-bom.any.worker.js b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/json-module/charset-bom.any.worker.js
new file mode 100644
index 00000000000..a96b737bb50
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/json-module/charset-bom.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/utils.js")
+importScripts("/html/semantics/scripting-1/the-script-element/json-module/charset-bom.any.js");
+done();
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/module/charset-02.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/module/charset-02.html
new file mode 100644
index 00000000000..a34e60a8c55
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/module/charset-02.html
@@ -0,0 +1,26 @@
+<!DOCTYPE html>
+<title>Module scripts should ignore BOMs and always use UTF-8</title>
+<script src="../../../../../resources/testharness.js"></script>
+<script src="../../../../../resources/testharnessreport.js"></script>
+<script>
+setup({allow_uncaught_exception: true});
+</script>
+<script type="module" src="../serve-with-content-type.py?fn=resources/bom-utf-8.js&ct=text/javascript"></script>
+<script type="module" src="../serve-with-content-type.py?fn=resources/bom-utf-16be.js&ct=text/javascript"></script>
+<script type="module" src="../serve-with-content-type.py?fn=resources/bom-utf-16le.js&ct=text/javascript"></script>
+<script type="module">
+test(function() {
+ assert_equals(window.executed_utf8_bom, '\u4e09\u6751\u304b\u306a\u5b50',
+ 'Should be decoded as UTF-8');
+}, 'UTF-8 module script with UTF-8 BOM');
+
+test(function() {
+ assert_equals(window.executed_utf16be_bom, undefined,
+ 'Should result in compile error because of UTF-16BE BOM');
+}, 'UTF-16 module script with UTF-16BE BOM');
+
+test(function() {
+ assert_equals(window.executed_utf16le_bom, undefined,
+ 'Should result in compile error because of UTF-16LE BOM');
+}, 'UTF-16 module script with UTF-16LE BOM');
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/resources/bom-utf-16be.js b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/resources/bom-utf-16be.js
new file mode 100644
index 00000000000..8a529e10b13
Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/resources/bom-utf-16be.js differ
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/resources/bom-utf-16le.js b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/resources/bom-utf-16le.js
new file mode 100644
index 00000000000..578f7f19519
Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/resources/bom-utf-16le.js differ
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/resources/bom-utf-8.js b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/resources/bom-utf-8.js
new file mode 100644
index 00000000000..fb88bdda2af
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/resources/bom-utf-8.js
@@ -0,0 +1,2 @@
+// JavaScript file with UTF-8 BOM.
+window.executed_utf8_bom = '三村かな子';
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/serve-with-content-type.py b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/serve-with-content-type.py
new file mode 100644
index 00000000000..675b3fc3eb0
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/serve-with-content-type.py
@@ -0,0 +1,17 @@
+import os
+
+from wptserve.utils import isomorphic_decode
+
+def main(request, response):
+ directory = os.path.dirname(isomorphic_decode(__file__))
+
+ try:
+ file_name = request.GET.first(b"fn")
+ content_type = request.GET.first(b"ct")
+ with open(os.path.join(directory, isomorphic_decode(file_name)), u"rb") as fh:
+ content = fh.read()
+
+ response.headers.set(b"Content-Type", content_type)
+ response.content = content
+ except:
+ response.set_error(400, u"Not enough parameters or file not found")
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/text-module/bom-utf-16be.txt b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/text-module/bom-utf-16be.txt
new file mode 100644
index 00000000000..94f8257ce6d
Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/text-module/bom-utf-16be.txt differ
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/text-module/bom-utf-16le.txt b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/text-module/bom-utf-16le.txt
new file mode 100644
index 00000000000..a1790e092e3
Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/text-module/bom-utf-16le.txt differ
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/text-module/bom-utf-8.txt b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/text-module/bom-utf-8.txt
new file mode 100644
index 00000000000..a0da97f57b4
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/text-module/bom-utf-8.txt
@@ -0,0 +1 @@
+text file
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/text-module/charset-bom.any.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/text-module/charset-bom.any.html
new file mode 100644
index 00000000000..c471e322739
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/text-module/charset-bom.any.html
@@ -0,0 +1,15 @@
+<!doctype html>
+<meta charset=utf-8>
+
+<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="../../../../../common/utils.js"></script>
+<div id=log></div>
+<script src="../../../../../html/semantics/scripting-1/the-script-element/text-module/charset-bom.any.js"></script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/text-module/charset-bom.any.js b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/text-module/charset-bom.any.js
new file mode 100644
index 00000000000..6d1c342e6d0
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/text-module/charset-bom.any.js
@@ -0,0 +1,17 @@
+// META: global=window,dedicatedworker,sharedworker
+// META: script=/common/utils.js
+
+promise_test(async () => {
+ const jsonModule = await import('./bom-utf-8.txt', { with: { type: 'text' } });
+ assert_equals(jsonModule.default, 'text file\n');
+}, 'UTF-8 BOM should be stripped when decoding text module script');
+
+promise_test(async () => {
+ const jsonModule = await import('./bom-utf-16be.txt', { with: { type: 'text' } });
+ assert_equals(jsonModule.default, '��\x00t\x00e\x00x\x00t\x00 \x00f\x00i\x00l\x00e\x00\n');
+}, 'UTF-16BE with BOM should be parsed as UTF-8');
+
+promise_test(async () => {
+ const jsonModule = await import('./bom-utf-16le.txt', { with: { type: 'text' } });
+ assert_equals(jsonModule.default, '��t\x00e\x00x\x00t\x00 \x00f\x00i\x00l\x00e\x00\n\x00');
+}, 'UTF-16LE with BOM should be parsed as UTF-8');
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/text-module/charset-bom.any.worker.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/text-module/charset-bom.any.worker.html
new file mode 100644
index 00000000000..27dad9e68f7
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/text-module/charset-bom.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("../../../../../html/semantics/scripting-1/the-script-element/text-module/charset-bom.any.worker.js"));
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/text-module/charset-bom.any.worker.js b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/text-module/charset-bom.any.worker.js
new file mode 100644
index 00000000000..3e4b20dc78a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/text-module/charset-bom.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/utils.js")
+importScripts("/html/semantics/scripting-1/the-script-element/text-module/charset-bom.any.js");
+done();
diff --git a/Tests/LibWeb/Text/input/wpt-import/workers/constructors/Worker/constructor-utf16-bom.html b/Tests/LibWeb/Text/input/wpt-import/workers/constructors/Worker/constructor-utf16-bom.html
new file mode 100644
index 00000000000..153a6f694bd
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/workers/constructors/Worker/constructor-utf16-bom.html
@@ -0,0 +1,21 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<title>Worker constructor: UTF-16 BOM should trigger error</title>
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+<script>
+function test_utf16_bom(url, title) {
+ async_test(t => {
+ const worker = new Worker(url);
+
+ worker.onmessage = t.unreached_func(`${title} should result to a compile error`);
+
+ worker.onerror = t.step_func_done(e => {
+ assert_true(true);
+ });
+ }, title);
+}
+
+test_utf16_bom("script-utf16be.js", "Worker with UTF-16BE BOM");
+test_utf16_bom("script-utf16le.js", "Worker with UTF-16LE BOM");
+</script>
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/workers/constructors/Worker/script-utf16be.js b/Tests/LibWeb/Text/input/wpt-import/workers/constructors/Worker/script-utf16be.js
new file mode 100644
index 00000000000..fa15f44bd0a
Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/workers/constructors/Worker/script-utf16be.js differ
diff --git a/Tests/LibWeb/Text/input/wpt-import/workers/constructors/Worker/script-utf16le.js b/Tests/LibWeb/Text/input/wpt-import/workers/constructors/Worker/script-utf16le.js
new file mode 100644
index 00000000000..6253e4ceac2
Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/workers/constructors/Worker/script-utf16le.js differ