Skip to content

Truly independent web browser

LibWeb: Run microtasks between the scripts in a body chunk

sideshowbarkerfac9d864ba5d
diff --git a/Libraries/LibWeb/Fetch/Infrastructure/IncrementalReadLoopReadRequest.cpp b/Libraries/LibWeb/Fetch/Infrastructure/IncrementalReadLoopReadRequest.cpp
index f26172ce402..84e8f34d2d3 100644
--- a/Libraries/LibWeb/Fetch/Infrastructure/IncrementalReadLoopReadRequest.cpp
+++ b/Libraries/LibWeb/Fetch/Infrastructure/IncrementalReadLoopReadRequest.cpp
@@ -37,11 +37,13 @@ void IncrementalReadLoopReadRequest::on_chunk(JS::Value chunk)
: MUST(uint8_array->viewed_array_buffer()->copy_to_byte_buffer(uint8_array->byte_offset(), JS::typed_array_byte_length(record)));
// 2. Set continueAlgorithm to these steps:
continue_algorithm = GC::create_function<void()>(GC::Heap::the(), GC::captures(move(bytes), m_body, m_reader, m_task_destination, m_process_body_chunk, m_process_end_of_body, m_process_body_error), [](ByteBuffer& bytes, auto& body, auto& reader, auto& task_destination, auto& process_body_chunk, auto& process_end_of_body, auto& process_body_error) {
- HTML::TemporaryExecutionContext execution_context { reader->closed_promise_realm(), HTML::TemporaryExecutionContext::CallbacksEnabled::Yes };
// 1. Run processBodyChunk given bytes.
+ // NB: Like any task, this runs with an empty JavaScript execution context stack. So, the HTML parser, which
+ // processBodyChunk can run, performs its microtask checkpoints between the scripts it runs.
process_body_chunk->function()(move(bytes));
// 2. Perform the incrementally-read loop given reader, taskDestination, processBodyChunk, processEndOfBody, and processBodyError.
+ HTML::TemporaryExecutionContext execution_context { reader->closed_promise_realm(), HTML::TemporaryExecutionContext::CallbacksEnabled::Yes };
body->incrementally_read_loop(reader, task_destination, process_body_chunk, process_end_of_body, process_body_error);
});
}
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index db7f13ac58c..98ecfa5689a 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -140,6 +140,10 @@ Text/input/FileAPI/blob-url-revoked-after-request-is-made.html
; Loads a module script, which needs an HTTP origin.
Text/input/wpt-import/html/semantics/scripting-1/the-script-element/execution-timing/106-external-module-import.html
+Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/evaluation-order-1.html
+Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/evaluation-order-2.html
+Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/evaluation-order-3.html
+Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/evaluation-order-4.html
; Opens popups that must be same-origin with the page.
Text/input/HTML/window-open-finds-a-popup-by-target-name.html
diff --git a/Tests/LibWeb/Text/expected/HTML/microtask-checkpoint-between-parser-inserted-scripts.txt b/Tests/LibWeb/Text/expected/HTML/microtask-checkpoint-between-parser-inserted-scripts.txt
new file mode 100644
index 00000000000..50056f57199
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/microtask-checkpoint-between-parser-inserted-scripts.txt
@@ -0,0 +1,3 @@
+first script ran
+microtask queued by the first script
+observer changed the second script to text/plain
diff --git a/Tests/LibWeb/Text/expected/XHR/microtask-checkpoint-between-body-chunk-listeners.txt b/Tests/LibWeb/Text/expected/XHR/microtask-checkpoint-between-body-chunk-listeners.txt
new file mode 100644
index 00000000000..454e4289f0b
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/XHR/microtask-checkpoint-between-body-chunk-listeners.txt
@@ -0,0 +1,3 @@
+readystatechange
+microtask queued by readystatechange
+progress
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/evaluation-order-1.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/evaluation-order-1.txt
new file mode 100644
index 00000000000..4000de1fc40
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/evaluation-order-1.txt
@@ -0,0 +1,9 @@
+Harness status: OK
+
+Found 4 tests
+
+4 Pass
+Pass Classic script queueing a microtask then throwing an exception
+Pass Classic script queueing a microtask
+Pass Module script queueing a microtask then throwing an exception
+Pass Module script queueing a microtask
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/evaluation-order-2.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/evaluation-order-2.txt
new file mode 100644
index 00000000000..f278cf0ad8b
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/evaluation-order-2.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Module script queueing a microtask then throwing an exception
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/evaluation-order-3.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/evaluation-order-3.txt
new file mode 100644
index 00000000000..f278cf0ad8b
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/evaluation-order-3.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Module script queueing a microtask then throwing an exception
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/evaluation-order-4.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/evaluation-order-4.txt
new file mode 100644
index 00000000000..55793273d61
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/evaluation-order-4.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Module script queueing a microtask then top-level await
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/event-loops/microtask_after_script.txt b/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/event-loops/microtask_after_script.txt
new file mode 100644
index 00000000000..6fec1ab801f
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/event-loops/microtask_after_script.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Microtask immediately after script
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/event-loops/microtask_before_prepare_the_script_element-01.txt b/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/event-loops/microtask_before_prepare_the_script_element-01.txt
new file mode 100644
index 00000000000..262a2194744
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/event-loops/microtask_before_prepare_the_script_element-01.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Microtask before prepare-the-script-element steps
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/event-loops/task_microtask_ordering.txt b/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/event-loops/task_microtask_ordering.txt
new file mode 100644
index 00000000000..ce53a10b209
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/webappapis/scripting/event-loops/task_microtask_ordering.txt
@@ -0,0 +1,7 @@
+Harness status: OK
+
+Found 2 tests
+
+2 Pass
+Pass Basic task and microtask ordering
+Pass Level 1 bossfight (synthetic click)
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/HTML/microtask-checkpoint-between-parser-inserted-scripts.html b/Tests/LibWeb/Text/input/HTML/microtask-checkpoint-between-parser-inserted-scripts.html
new file mode 100644
index 00000000000..8b6741efd66
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/microtask-checkpoint-between-parser-inserted-scripts.html
@@ -0,0 +1,47 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ asyncTest(async done => {
+ // The scripts arrive in a later chunk of the body than the start of the page, so the parser runs them as it
+ // processes that chunk. A microtask that the first one queues runs before the parser prepares the second, and
+ // so does the callback of a mutation observer that keeps the second from running.
+ const server = httpTestServer();
+ const token = crypto.randomUUID();
+ const scriptStartTag = "<scr" + "ipt";
+ const scriptEndTag = "</scr" + "ipt>";
+ // NB: The parser waits for 1024 bytes before it starts, so the first chunk pads to more. And the response names
+ // its encoding, so that the parser doesn't look past the first chunk for an encoding declaration.
+ const firstChunk = `<!DOCTYPE html><!--${"x".repeat(1100)}-->`;
+ const secondChunk = `${scriptStartTag}>
+ const log = [];
+ Promise.resolve().then(() => log.push("microtask queued by the first script"));
+ new MutationObserver(records => {
+ for (const record of records) {
+ for (const node of record.addedNodes) {
+ if (node.id === "blocked") {
+ node.type = "text/plain";
+ log.push("observer changed the second script to text/plain");
+ }
+ }
+ }
+ }).observe(document.documentElement, { childList: true, subtree: true });
+ log.push("first script ran");
+ ${scriptEndTag}
+ ${scriptStartTag} id="blocked">log.push("second script ran");${scriptEndTag}
+ ${scriptStartTag}>addEventListener("load", () => parent.postMessage(log, "*"));${scriptEndTag}`;
+ const url = await server.createEcho("GET", `/microtask-checkpoint-between-parser-inserted-scripts-${token}`, {
+ status: 200,
+ headers: { "Content-Type": "text/html; charset=utf-8" },
+ body: firstChunk + secondChunk,
+ });
+
+ const frame = document.createElement("iframe");
+ window.addEventListener("message", event => {
+ for (const entry of event.data)
+ println(entry);
+ done();
+ });
+ frame.src = `${url}?chunks=${firstChunk.length}&chunk_delay_ms=100`;
+ document.body.appendChild(frame);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/XHR/microtask-checkpoint-between-body-chunk-listeners.html b/Tests/LibWeb/Text/input/XHR/microtask-checkpoint-between-body-chunk-listeners.html
new file mode 100644
index 00000000000..3a7bbd610c8
--- /dev/null
+++ b/Tests/LibWeb/Text/input/XHR/microtask-checkpoint-between-body-chunk-listeners.html
@@ -0,0 +1,28 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ asyncTest(done => {
+ // The readystatechange and progress events for a chunk of the response body fire in the same task. A microtask
+ // queued by the first listener runs as soon as that listener returns, before the second one.
+ const log = [];
+ const xhr = new XMLHttpRequest();
+ xhr.open("GET", "data:text/plain," + "x".repeat(1000));
+ xhr.onreadystatechange = () => {
+ if (xhr.readyState !== XMLHttpRequest.LOADING || log.length)
+ return;
+ log.push("readystatechange");
+ Promise.resolve().then(() => log.push("microtask queued by readystatechange"));
+ };
+ xhr.onprogress = () => {
+ if (log.includes("progress"))
+ return;
+ log.push("progress");
+ };
+ xhr.onload = () => {
+ for (const entry of log)
+ println(entry);
+ done();
+ };
+ xhr.send();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/evaluation-order-1.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/evaluation-order-1.html
new file mode 100644
index 00000000000..2f48d39b066
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/evaluation-order-1.html
@@ -0,0 +1,34 @@
+<!DOCTYPE html>
+<script src="../../../../../resources/testharness.js"></script>
+<script src="../../../../../resources/testharnessreport.js"></script>
+<script src="resources/evaluation-order-setup.js"></script>
+
+<script>
+// Spec: https://html.spec.whatwg.org/C/#run-a-classic-script
+setupTest("Classic script queueing a microtask then throwing an exception", [
+ "body", // Step 6.
+ "global-error", // "Report the exception" at Step 7.3.
+ "microtask", // "Clean up after running script" at Step 7.2.
+ ]);
+</script>
+<script src="resources/evaluation-order-1-throw.js"
+ onerror="unreachable()" onload="testDone()"></script>
+
+<script>
+// Spec: https://html.spec.whatwg.org/C/#run-a-classic-script
+setupTest("Classic script queueing a microtask", [
+ "body", // Step 6.
+ "microtask", // "Clean up after running script" at Step 7.2.
+ ]);
+</script>
+<script src="resources/evaluation-order-1-nothrow.js"
+ onerror="unreachable()" onload="testDone()"></script>
+
+
+<script type="module" src="resources/evaluation-order-1-throw-setup.js"></script>
+<script type="module" src="resources/evaluation-order-1-throw.js"
+ onerror="unreachable()" onload="testDone()"></script>
+
+<script type="module" src="resources/evaluation-order-1-nothrow-setup.js"></script>
+<script type="module" src="resources/evaluation-order-1-nothrow.js"
+ onerror="unreachable()" onload="testDone()"></script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/evaluation-order-2.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/evaluation-order-2.html
new file mode 100644
index 00000000000..44d3f66653d
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/evaluation-order-2.html
@@ -0,0 +1,8 @@
+<!DOCTYPE html>
+<script src="../../../../../resources/testharness.js"></script>
+<script src="../../../../../resources/testharnessreport.js"></script>
+<script src="resources/evaluation-order-setup.js"></script>
+
+<script type="module" src="resources/evaluation-order-2-setup.js"></script>
+<script type="module" src="resources/evaluation-order-2.1.mjs"
+ onerror="unreachable()" onload="testDone()"></script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/evaluation-order-3.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/evaluation-order-3.html
new file mode 100644
index 00000000000..fe726917d49
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/evaluation-order-3.html
@@ -0,0 +1,8 @@
+<!DOCTYPE html>
+<script src="../../../../../resources/testharness.js"></script>
+<script src="../../../../../resources/testharnessreport.js"></script>
+<script src="resources/evaluation-order-setup.js"></script>
+
+<script type="module" src="resources/evaluation-order-3-setup.js"></script>
+<script type="module" src="resources/evaluation-order-3.1.mjs"
+ onerror="unreachable()"></script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/evaluation-order-4.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/evaluation-order-4.html
new file mode 100644
index 00000000000..049ff83e206
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/evaluation-order-4.html
@@ -0,0 +1,18 @@
+<!DOCTYPE html>
+<script src="../../../../../resources/testharness.js"></script>
+<script src="../../../../../resources/testharnessreport.js"></script>
+<script src="resources/evaluation-order-setup.js"></script>
+
+<script>
+// Spec: https://html.spec.whatwg.org/C/#run-a-module-script
+setupTest("Module script queueing a microtask then top-level await", [
+ "step-4.1-1", "step-4.1-2",
+ "microtask-4.1",
+ "script-load",
+ "step-4.2-1", "step-4.2-2",
+ "microtask-4.2",
+]);
+window.onerror = testDone;
+</script>
+<script type="module" src="resources/evaluation-order-4.1.mjs"
+ onerror="unreachable()" onload="log.push('script-load')"></script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/resources/evaluation-order-1-nothrow-setup.js b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/resources/evaluation-order-1-nothrow-setup.js
new file mode 100644
index 00000000000..1b42e995937
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/resources/evaluation-order-1-nothrow-setup.js
@@ -0,0 +1,5 @@
+// Spec: https://html.spec.whatwg.org/C/#run-a-module-script
+setupTest("Module script queueing a microtask", [
+ "body", // Step 6.
+ "microtask", // "Clean up after running script" at Step 8.
+]);
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/resources/evaluation-order-1-nothrow.js b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/resources/evaluation-order-1-nothrow.js
new file mode 100644
index 00000000000..e19d9b1b137
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/resources/evaluation-order-1-nothrow.js
@@ -0,0 +1,2 @@
+queueMicrotask(() => globalThis.log.push("microtask"));
+globalThis.log.push("body");
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/resources/evaluation-order-1-throw-setup.js b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/resources/evaluation-order-1-throw-setup.js
new file mode 100644
index 00000000000..651a494e530
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/resources/evaluation-order-1-throw-setup.js
@@ -0,0 +1,10 @@
+// Spec: https://html.spec.whatwg.org/C/#run-a-module-script
+setupTest("Module script queueing a microtask then throwing an exception", [
+ "body", // Step 6.
+ "microtask", // "Clean up after running script" at Step 8.
+ "global-error", // "Clean up after running script" at Step 8, because
+ // `evaluationPromise` is synchronously rejected and the rejection is
+ // processed in the microtask checkpoint here (See also Step 7).
+ // As `evaluationPromise` is rejected after the microtask queued during
+ // evaluation, "global-error" occurs after "microtask".
+]);
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/resources/evaluation-order-1-throw.js b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/resources/evaluation-order-1-throw.js
new file mode 100644
index 00000000000..2451df1c155
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/resources/evaluation-order-1-throw.js
@@ -0,0 +1,4 @@
+queueMicrotask(() => globalThis.log.push("microtask"));
+globalThis.log.push("body");
+
+throw new Error("error");
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/resources/evaluation-order-3-setup.js b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/resources/evaluation-order-3-setup.js
new file mode 100644
index 00000000000..edc046910e4
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/resources/evaluation-order-3-setup.js
@@ -0,0 +1,7 @@
+setupTest("Module script queueing a microtask then throwing an exception", [
+ "step-3.1-1", "step-3.1-2", "step-3.1-3",
+ "microtask-3.1",
+ "step-3.2-1", "step-3.2-2",
+ "microtask-3.2",
+ "import-catch", "error",
+]);
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/resources/evaluation-order-3.1.mjs b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/resources/evaluation-order-3.1.mjs
new file mode 100644
index 00000000000..ef320632af7
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/resources/evaluation-order-3.1.mjs
@@ -0,0 +1,11 @@
+globalThis.log.push("step-3.1-1");
+queueMicrotask(() => globalThis.log.push("microtask-3.1"));
+globalThis.log.push("step-3.1-2");
+
+import("./evaluation-order-3.2.mjs").catch(
+ exception => {
+ globalThis.log.push("import-catch", exception.message);
+ globalThis.testDone();
+ });
+
+globalThis.log.push("step-3.1-3");
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/resources/evaluation-order-3.2.mjs b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/resources/evaluation-order-3.2.mjs
new file mode 100644
index 00000000000..8ccb5812066
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/resources/evaluation-order-3.2.mjs
@@ -0,0 +1,5 @@
+globalThis.log.push("step-3.2-1");
+queueMicrotask(() => globalThis.log.push("microtask-3.2"));
+globalThis.log.push("step-3.2-2");
+
+throw new Error("error");
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/resources/evaluation-order-4.1.mjs b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/resources/evaluation-order-4.1.mjs
new file mode 100644
index 00000000000..f3347c1d28e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/resources/evaluation-order-4.1.mjs
@@ -0,0 +1,8 @@
+log.push("step-4.1-1");
+queueMicrotask(() => log.push("microtask-4.1"));
+log.push("step-4.1-2");
+
+await import("./evaluation-order-4.2.mjs");
+
+// Not happening as we throw in the above module.
+log.push("step-4.1-3");
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/resources/evaluation-order-4.2.mjs b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/resources/evaluation-order-4.2.mjs
new file mode 100644
index 00000000000..96a5cca3a63
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/scripting-1/the-script-element/microtasks/resources/evaluation-order-4.2.mjs
@@ -0,0 +1,5 @@
+log.push("step-4.2-1");
+queueMicrotask(() => log.push("microtask-4.2"));
+log.push("step-4.2-2");
+
+throw new Error("error");
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/webappapis/scripting/event-loops/microtask_after_script.html b/Tests/LibWeb/Text/input/wpt-import/html/webappapis/scripting/event-loops/microtask_after_script.html
new file mode 100644
index 00000000000..2ca49c9e5ad
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/webappapis/scripting/event-loops/microtask_after_script.html
@@ -0,0 +1,55 @@
+<!DOCTYPE html>
+<head>
+<link rel=author title="Aleks Totic" href="mailto:[email protected]">
+<link rel=help href="https://html.spec.whatwg.org/#clean-up-after-running-script">
+<script src="../../../../resources/testharness.js"></script>
+<script src="../../../../resources/testharnessreport.js"></script>
+<script src="resources/common.js"></script>
+</head>
+<body style="height:2000px;">
+<script>
+/*
+promise 1, promise 2 execute immediately after script tag
+promise 1 child executes immediately after promise 2.
+
+Relevant specs:
+
+https://html.spec.whatwg.org/#clean-up-after-running-script
+If the JavaScript execution context stack is now empty, perform a microtask checkpoint.
+
+https://html.spec.whatwg.org/#perform-a-microtask-checkpoint
+"perform a microtask checkpoint" runs in a loop until all microtasks have been delivered.
+*/
+
+var test = async_test("Microtask immediately after script");
+
+var events = [];
+
+Promise.resolve()
+.then(function() {
+ events.push("promise 1");
+ return Promise.resolve();
+})
+.then(function() {
+ test.step(function() {
+ events.push("promise 1 child");
+ assert_array_equals(events, ["promise 1", "promise 2", "promise 1 child"]);
+ test.done();
+ });
+});
+Promise.resolve()
+.then(function() {
+ events.push("promise 2");
+});
+
+// Set up events that must be executed after Promise.
+window.setTimeout(function() {
+ events.push('timeout');
+}, 0);
+window.addEventListener('scroll', function() {
+ events.push('scroll');
+});
+window.scrollBy(0,10);
+
+</script>
+</body>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/webappapis/scripting/event-loops/microtask_before_prepare_the_script_element-01.html b/Tests/LibWeb/Text/input/wpt-import/html/webappapis/scripting/event-loops/microtask_before_prepare_the_script_element-01.html
new file mode 100644
index 00000000000..1ce447aac50
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/webappapis/scripting/event-loops/microtask_before_prepare_the_script_element-01.html
@@ -0,0 +1,39 @@
+<!DOCTYPE html>
+<head>
+<link rel=help href="https://html.spec.whatwg.org/#parsing-main-incdata">
+<link rel=help href="https://html.spec.whatwg.org/#perform-a-microtask-checkpoint">
+<script src="../../../../resources/testharness.js"></script>
+<script src="../../../../resources/testharnessreport.js"></script>
+</head>
+<body>
+<script>
+
+let classicScriptRan = false;
+let moduleScriptRan = false;
+let asyncScriptRan = false;
+let deferSCriptRan = false;
+
+new MutationObserver((mutations, observer) => {
+ mutations.forEach(({ addedNodes }) => {
+ addedNodes.forEach(node => {
+ if (node.nodeType == 1 && node.tagName == "SCRIPT") {
+ node.setAttribute("type", "text/plain");
+ }
+ });
+ });
+}).observe(document.body, { childList: true, subtree: true });
+
+let test = async_test("Microtask before prepare-the-script-element steps");
+window.onload = test.step_func_done(function(e) {
+ assert_false(classicScriptRan, "Classic script should not run");
+ assert_false(moduleScriptRan, "Module script should not run");
+ assert_false(asyncScriptRan, "Async script should not run");
+ assert_false(deferSCriptRan, "Defer script should not run");
+});
+
+</script>
+<script>classicScriptRan = true;</script>
+<script type="module">moduleScriptRan = true;</script>
+<script src="resources/async.js" async></script>
+<script src="resources/defer.js" defer></script>
+</body>
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/webappapis/scripting/event-loops/resources/async.js b/Tests/LibWeb/Text/input/wpt-import/html/webappapis/scripting/event-loops/resources/async.js
new file mode 100644
index 00000000000..5b6470d5a44
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/webappapis/scripting/event-loops/resources/async.js
@@ -0,0 +1 @@
+asyncScriptRan = true;
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/webappapis/scripting/event-loops/resources/common.js b/Tests/LibWeb/Text/input/wpt-import/html/webappapis/scripting/event-loops/resources/common.js
new file mode 100644
index 00000000000..e2279f93ddb
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/webappapis/scripting/event-loops/resources/common.js
@@ -0,0 +1,20 @@
+// Helper for tests that just want to verify the ordering of a series of events.
+// Usage:
+// log_test(function(t, log) {
+// log('first');
+// log('second');
+// }, ['first', 'second'], 'Ordinal numbers are ordinal');
+
+function log_test(func, expected, description) {
+ async_test(function(t) {
+ var actual = [];
+ function log(entry) {
+ actual.push(entry);
+ if (expected.length <= actual.length) {
+ assert_array_equals(actual, expected);
+ t.done();
+ }
+ }
+ func(t, t.step_func(log));
+ }, description);
+}
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/webappapis/scripting/event-loops/resources/defer.js b/Tests/LibWeb/Text/input/wpt-import/html/webappapis/scripting/event-loops/resources/defer.js
new file mode 100644
index 00000000000..d8498d274fb
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/webappapis/scripting/event-loops/resources/defer.js
@@ -0,0 +1 @@
+deferSCriptRan = true;
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/webappapis/scripting/event-loops/task_microtask_ordering.html b/Tests/LibWeb/Text/input/wpt-import/html/webappapis/scripting/event-loops/task_microtask_ordering.html
new file mode 100644
index 00000000000..85f050a93d0
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/webappapis/scripting/event-loops/task_microtask_ordering.html
@@ -0,0 +1,85 @@
+<!DOCTYPE html>
+<title>Task and Microtask Ordering </title>
+<link rel=author title="Joshua Bell" href="mailto:[email protected]">
+<link rel=help href="https://html.spec.whatwg.org/multipage/#event-loops">
+<script src="../../../../resources/testharness.js"></script>
+<script src="../../../../resources/testharnessreport.js"></script>
+<script src="resources/common.js"></script>
+
+<div class="outer">
+ <div class="inner"></div>
+</div>
+
+<script>
+
+// Based on: https://jakearchibald.com/2015/tasks-microtasks-queues-and-schedules/
+
+log_test(function(t, log) {
+ log('script start');
+
+ setTimeout(function() {
+ log('setTimeout');
+ }, 0);
+
+ Promise.resolve().then(function() {
+ log('promise1');
+ }).then(function() {
+ log('promise2');
+ });
+
+ log('script end');
+}, [
+ 'script start',
+ 'script end',
+ 'promise1',
+ 'promise2',
+ 'setTimeout'
+], 'Basic task and microtask ordering');
+
+log_test(function(t, log) {
+ // Let's get hold of those elements
+ var outer = document.querySelector('.outer');
+ var inner = document.querySelector('.inner');
+
+ // Let's listen for attribute changes on the
+ // outer element
+ new MutationObserver(function() {
+ log('mutate');
+ }).observe(outer, {
+ attributes: true
+ });
+
+ // Here's a click listener...
+ function onClick() {
+ log('click');
+
+ setTimeout(function() {
+ log('timeout');
+ }, 0);
+
+ Promise.resolve().then(function() {
+ log('promise');
+ });
+
+ outer.setAttribute('data-random', Math.random());
+ }
+
+ // ...which we'll attach to both elements
+ inner.addEventListener('click', onClick);
+ outer.addEventListener('click', onClick);
+
+ // Note that this will behave differently than a real click,
+ // since the dispatch is synchronous and microtasks will not
+ // run between event bubbling steps.
+ inner.click();
+}, [
+ 'click',
+ 'click',
+ 'promise',
+ 'mutate',
+ 'promise',
+ 'timeout',
+ 'timeout'
+], 'Level 1 bossfight (synthetic click)');
+
+</script>