diff --git a/Libraries/LibWeb/HTML/HTMLDialogElement.cpp b/Libraries/LibWeb/HTML/HTMLDialogElement.cpp
index ea838cd0dee..33352026d13 100644
--- a/Libraries/LibWeb/HTML/HTMLDialogElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLDialogElement.cpp
@@ -43,6 +43,7 @@ void HTMLDialogElement::visit_edges(JS::Cell::Visitor& visitor)
visitor.visit(m_close_watcher);
visitor.visit(m_request_close_source_element);
+ visitor.visit(m_previously_focused_element);
}
void HTMLDialogElement::removed_from(Node* old_parent, Node& old_root)
@@ -142,7 +143,9 @@ WebIDL::ExceptionOr HTMLDialogElement::show()
// 9. Set the dialog close watcher with this.
set_close_watcher();
- // FIXME: 10. Set this's previously focused element to the focused element.
+
+ // 10. Set this's previously focused element to the focused element.
+ m_previously_focused_element = document().focused_area();
// 11. Let document be this's node document.
auto document = m_document;
@@ -358,7 +361,8 @@ void HTMLDialogElement::close_the_dialog(Optional result, GC::Ptr result, GC::Ptris_shadow_including_inclusive_descendant_of(*this);
+ if (is_focus_within_dialog || was_modal)
+ run_focusing_steps(element);
+ }
// 14. Queue an element task on the user interaction task source given the subject element to fire an event named close at subject.
queue_an_element_task(HTML::Task::Source::UserInteraction, [this] {
diff --git a/Libraries/LibWeb/HTML/HTMLDialogElement.h b/Libraries/LibWeb/HTML/HTMLDialogElement.h
index a7e794fc2af..e49b04e3fb0 100644
--- a/Libraries/LibWeb/HTML/HTMLDialogElement.h
+++ b/Libraries/LibWeb/HTML/HTMLDialogElement.h
@@ -72,6 +72,9 @@ private:
// https://html.spec.whatwg.org/multipage/interactive-elements.html#dialog-toggle-task-tracker
Optional m_dialog_toggle_task_tracker;
+
+ // https://html.spec.whatwg.org/multipage/interactive-elements.html#previously-focused-element
+ GC::Ptr m_previously_focused_element;
};
}
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/interactive-elements/the-dialog-element/dialog-focus-previous-outside.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/interactive-elements/the-dialog-element/dialog-focus-previous-outside.txt
new file mode 100644
index 00000000000..9fd4d26fc98
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/interactive-elements/the-dialog-element/dialog-focus-previous-outside.txt
@@ -0,0 +1,11 @@
+Harness status: OK
+
+Found 5 tests
+
+3 Pass
+2 Fail
+Pass Focus should not be restored if the currently focused element is not inside the dialog.
+Pass Focus restore should not occur when the focused element is in a shadowroot outside of the dialog.
+Pass Focus restore should occur when the focused element is in a shadowroot inside the dialog.
+Fail Focus restore should occur when the focused element is slotted into a dialog.
+Fail Focus restore should always occur for modal dialogs.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/html/semantics/interactive-elements/the-dialog-element/dialog-focus-previous-outside.html b/Tests/LibWeb/Text/input/wpt-import/html/semantics/interactive-elements/the-dialog-element/dialog-focus-previous-outside.html
new file mode 100644
index 00000000000..a35df88e31d
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/html/semantics/interactive-elements/the-dialog-element/dialog-focus-previous-outside.html
@@ -0,0 +1,82 @@
+
+
+
+
+
+
+
+
+