diff --git a/Libraries/LibWeb/HTML/CloseWatcher.cpp b/Libraries/LibWeb/HTML/CloseWatcher.cpp
index f162b8be3eb..9e42ce5ea41 100644
--- a/Libraries/LibWeb/HTML/CloseWatcher.cpp
+++ b/Libraries/LibWeb/HTML/CloseWatcher.cpp
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2024, the Ladybird developers.
+ * Copyright (c) 2024, Felipe Muñoz Mazur
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@@ -57,9 +58,17 @@ WebIDL::ExceptionOr> CloseWatcher::construct_impl(JS::Real
auto close_watcher = establish(window);
// 3. If options["signal"] exists, then:
- if (options.signal) {
- // FIXME: 3.1 If options["signal"]'s aborted, then destroy closeWatcher.
- // FIXME: 3.2 Add the following steps to options["signal"]:
+ if (auto signal = options.signal) {
+ // 3.1 If options["signal"]'s aborted, then destroy closeWatcher.
+ if (signal->aborted()) {
+ close_watcher->destroy();
+ }
+
+ // 3.2 Add the following steps to options["signal"]:
+ signal->add_abort_algorithm([close_watcher] {
+ // 3.2.1 Destroy closeWatcher.
+ close_watcher->destroy();
+ });
}
return close_watcher;
diff --git a/Tests/LibWeb/Text/expected/CloseWatcher-abort-requestClose.txt b/Tests/LibWeb/Text/expected/CloseWatcher-abort-requestClose.txt
new file mode 100644
index 00000000000..4b095fd0ff9
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/CloseWatcher-abort-requestClose.txt
@@ -0,0 +1,2 @@
+false
+false
diff --git a/Tests/LibWeb/Text/expected/CloseWatcher-already-aborted.txt b/Tests/LibWeb/Text/expected/CloseWatcher-already-aborted.txt
new file mode 100644
index 00000000000..4b095fd0ff9
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/CloseWatcher-already-aborted.txt
@@ -0,0 +1,2 @@
+false
+false
diff --git a/Tests/LibWeb/Text/expected/CloseWatcher-fire-once.txt b/Tests/LibWeb/Text/expected/CloseWatcher-fire-once.txt
new file mode 100644
index 00000000000..6ed281c757a
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/CloseWatcher-fire-once.txt
@@ -0,0 +1,2 @@
+1
+1
diff --git a/Tests/LibWeb/Text/input/CloseWatcher-abort-requestClose.html b/Tests/LibWeb/Text/input/CloseWatcher-abort-requestClose.html
new file mode 100644
index 00000000000..36bb3747e7a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/CloseWatcher-abort-requestClose.html
@@ -0,0 +1,18 @@
+
+
+
diff --git a/Tests/LibWeb/Text/input/CloseWatcher-already-aborted.html b/Tests/LibWeb/Text/input/CloseWatcher-already-aborted.html
new file mode 100644
index 00000000000..722b3ed59cb
--- /dev/null
+++ b/Tests/LibWeb/Text/input/CloseWatcher-already-aborted.html
@@ -0,0 +1,17 @@
+
+
+
diff --git a/Tests/LibWeb/Text/input/CloseWatcher-fire-once.html b/Tests/LibWeb/Text/input/CloseWatcher-fire-once.html
new file mode 100644
index 00000000000..a2f08d41c4e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/CloseWatcher-fire-once.html
@@ -0,0 +1,18 @@
+
+
+