diff --git a/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
index 4d12e58bfcc..13b39e96aa8 100644
--- a/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
+++ b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.cpp
@@ -369,6 +369,31 @@ void EventLoop::process()
});
}
+// https://html.spec.whatwg.org/multipage/webappapis.html#queue-a-task
+int queue_a_task(HTML::Task::Source source, JS::GCPtr event_loop, JS::GCPtr document, JS::NonnullGCPtr> steps)
+{
+ // 1. If event loop was not given, set event loop to the implied event loop.
+ if (!event_loop)
+ event_loop = main_thread_event_loop();
+
+ // FIXME: 2. If document was not given, set document to the implied document.
+
+ // 3. Let task be a new task.
+ // 4. Set task's steps to steps.
+ // 5. Set task's source to source.
+ // 6. Set task's document to the document.
+ // 7. Set task's script evaluation environment settings object set to an empty set.
+ auto task = HTML::Task::create(event_loop->vm(), source, document, steps);
+
+ // 8. Let queue be the task queue to which source is associated on event loop.
+ auto& queue = event_loop->task_queue();
+
+ // 9. Append task to queue.
+ queue.add(task);
+
+ return queue.last_added_task()->id();
+}
+
// https://html.spec.whatwg.org/multipage/webappapis.html#queue-a-global-task
int queue_global_task(HTML::Task::Source source, JS::Object& global_object, JS::NonnullGCPtr> steps)
{
@@ -384,10 +409,7 @@ int queue_global_task(HTML::Task::Source source, JS::Object& global_object, JS::
}
// 3. Queue a task given source, event loop, document, and steps.
- auto& vm = global_object.vm();
- event_loop->task_queue().add(HTML::Task::create(vm, source, document, steps));
-
- return event_loop->task_queue().last_added_task()->id();
+ return queue_a_task(source, *event_loop, document, steps);
}
// https://html.spec.whatwg.org/#queue-a-microtask
diff --git a/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.h b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.h
index ea5f003f340..9503af86d7c 100644
--- a/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.h
+++ b/Userland/Libraries/LibWeb/HTML/EventLoop/EventLoop.h
@@ -116,6 +116,7 @@ private:
};
EventLoop& main_thread_event_loop();
+int queue_a_task(HTML::Task::Source, JS::GCPtr, JS::GCPtr, JS::NonnullGCPtr> steps);
int queue_global_task(HTML::Task::Source, JS::Object&, JS::NonnullGCPtr> steps);
void queue_a_microtask(DOM::Document const*, JS::NonnullGCPtr> steps);
void perform_a_microtask_checkpoint();