Skip to content

Truly independent web browser

LibWeb: Apply popover autofocus and restore previous focus

Andreas Klingaf955d6a0f80
diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h
index 52c2c2278ed..15ee315fa03 100644
--- a/Libraries/LibWeb/DOM/Document.h
+++ b/Libraries/LibWeb/DOM/Document.h
@@ -789,6 +789,7 @@ public:
Vector<GC::Ref<Element>>& autofocus_candidates() { return m_autofocus_candidates; }
bool autofocus_processed_flag() const { return m_autofocus_processed_flag; }
+ void set_autofocus_processed_flag(bool value) { m_autofocus_processed_flag = value; }
void flush_autofocus_candidates();
Element const* accessibility_focus_target() const { return m_accessibility_focus_target.ptr(); }
diff --git a/Libraries/LibWeb/HTML/Focus.cpp b/Libraries/LibWeb/HTML/Focus.cpp
index 1f71fdf1c85..f303eb11ba8 100644
--- a/Libraries/LibWeb/HTML/Focus.cpp
+++ b/Libraries/LibWeb/HTML/Focus.cpp
@@ -486,6 +486,53 @@ GC::Ptr<DOM::Node> focus_delegate(DOM::Node& focus_target, FocusTrigger focus_tr
return nullptr;
}
+// https://html.spec.whatwg.org/multipage/popover.html#popover-focusing-steps
+void run_popover_focusing_steps(HTMLElement& subject)
+{
+ // 1. If the allow focus steps given subject's node document return false, then return.
+ if (!subject.document().allow_focus())
+ return;
+
+ // 2. If subject is a dialog element, then run the dialog focusing steps given subject and return.
+ if (auto* dialog = as_if<HTMLDialogElement>(subject)) {
+ dialog->run_dialog_focusing_steps();
+ return;
+ }
+
+ // 3. If subject has the autofocus attribute, then let control be subject.
+ // 4. Otherwise, let control be the autofocus delegate for subject given "other".
+ GC::Ptr<DOM::Node> control = subject.has_attribute(AttributeNames::autofocus)
+ ? GC::Ptr<DOM::Node> { subject }
+ : autofocus_delegate(subject, FocusTrigger::Other);
+
+ // 5. If control is null, then return.
+ if (!control)
+ return;
+
+ // 6. Run the focusing steps given control.
+ run_focusing_steps(control);
+
+ // 7. Let topDocument be control's node navigable's top-level traversable's active document.
+ // NB: Focus event handlers can detach the document from its navigable.
+ auto navigable = control->navigable();
+ if (!navigable)
+ return;
+ auto* top_level_traversable = as_if<LocalNavigable>(*navigable->top_level_traversable());
+ if (!top_level_traversable)
+ return;
+ auto top_document = top_level_traversable->active_document();
+
+ // 8. If control's node document's origin is not the same as the origin of topDocument, then return.
+ if (!top_document || !control->document().origin().is_same_origin(top_document->origin()))
+ return;
+
+ // 9. Empty topDocument's autofocus candidates.
+ top_document->autofocus_candidates().clear();
+
+ // 10. Set topDocument's autofocus processed flag to true.
+ top_document->set_autofocus_processed_flag(true);
+}
+
static GC::Ptr<DOM::Node> get_focusable_area(DOM::Node& focus_target, FocusTrigger focus_trigger)
{
// FIXME: Implement the rest of the get the focusable area algorithm.
diff --git a/Libraries/LibWeb/HTML/Focus.h b/Libraries/LibWeb/HTML/Focus.h
index a68e5f29323..08ae0f873ab 100644
--- a/Libraries/LibWeb/HTML/Focus.h
+++ b/Libraries/LibWeb/HTML/Focus.h
@@ -26,6 +26,7 @@ WEB_API void run_focus_update_steps(Vector<GC::Root<DOM::Node>> old_chain, Vecto
WEB_API void run_focusing_steps(GC::Ptr<DOM::Node> new_focus_target, GC::Ptr<DOM::Node> fallback_target = nullptr, FocusTrigger focus_trigger = FocusTrigger::Other, ScrollIntoView = ScrollIntoView::Yes);
WEB_API void run_unfocusing_steps(GC::Ptr<DOM::Node> old_focus_target);
GC::Ptr<DOM::Node> focus_delegate(DOM::Node& focus_target, FocusTrigger = FocusTrigger::Other);
+void run_popover_focusing_steps(HTMLElement& subject);
// https://html.spec.whatwg.org/multipage/interaction.html#sequential-focus-direction
enum class SequentialFocusDirection : u8 {
diff --git a/Libraries/LibWeb/HTML/HTMLDialogElement.cpp b/Libraries/LibWeb/HTML/HTMLDialogElement.cpp
index 72e8621a0f6..2ba543bd383 100644
--- a/Libraries/LibWeb/HTML/HTMLDialogElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLDialogElement.cpp
@@ -538,12 +538,24 @@ void HTMLDialogElement::run_dialog_focusing_steps()
run_focusing_steps(control);
// 7. Let topDocument be control's node navigable's top-level traversable's active document.
+ // NB: A focus event handler can move control into a document without a navigable.
+ auto navigable = control->navigable();
+ if (!navigable)
+ return;
+ auto* top_level_traversable = as_if<LocalNavigable>(*navigable->top_level_traversable());
+ if (!top_level_traversable)
+ return;
+ auto top_document = top_level_traversable->active_document();
+
// 8. If control's node document's origin is not the same as the origin of topDocument, then return.
- if (!control->document().origin().is_same_origin(*control->navigable()->top_level_traversable()->active_document_origin()))
+ if (!top_document || !control->document().origin().is_same_origin(top_document->origin()))
return;
- // FIXME: 9. Empty topDocument's autofocus candidates.
- // FIXME: 10. Set topDocument's autofocus processed flag to true.
+ // 9. Empty topDocument's autofocus candidates.
+ top_document->autofocus_candidates().clear();
+
+ // 10. Set topDocument's autofocus processed flag to true.
+ top_document->set_autofocus_processed_flag(true);
}
void HTMLDialogElement::set_is_modal(bool is_modal)
diff --git a/Libraries/LibWeb/HTML/HTMLDialogElement.h b/Libraries/LibWeb/HTML/HTMLDialogElement.h
index 11b0d34fc41..5233e0f8dfe 100644
--- a/Libraries/LibWeb/HTML/HTMLDialogElement.h
+++ b/Libraries/LibWeb/HTML/HTMLDialogElement.h
@@ -51,6 +51,7 @@ public:
bool is_modal() const { return m_is_modal; }
void set_is_modal(bool);
+ void run_dialog_focusing_steps();
bool is_valid_command(Utf16View) override;
void command_steps(DOM::Element&, Utf16View) override;
@@ -66,7 +67,6 @@ private:
void run_dialog_setup_steps();
void run_dialog_cleanup_steps();
- void run_dialog_focusing_steps();
void set_close_watcher();
diff --git a/Libraries/LibWeb/HTML/HTMLElement.cpp b/Libraries/LibWeb/HTML/HTMLElement.cpp
index 1f0007dc2f7..6f9fd46b56a 100644
--- a/Libraries/LibWeb/HTML/HTMLElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLElement.cpp
@@ -32,6 +32,7 @@
#include <LibWeb/HTML/CustomElements/CustomElementRegistry.h>
#include <LibWeb/HTML/ElementInternals.h>
#include <LibWeb/HTML/EventHandler.h>
+#include <LibWeb/HTML/Focus.h>
#include <LibWeb/HTML/HTMLAnchorElement.h>
#include <LibWeb/HTML/HTMLBRElement.h>
#include <LibWeb/HTML/HTMLBaseElement.h>
@@ -89,6 +90,9 @@ struct HTMLElement::RareData final
// https://html.spec.whatwg.org/multipage/popover.html#popover-close-watcher
GC::Ptr<CloseWatcher> popover_close_watcher;
+ // https://html.spec.whatwg.org/multipage/interactive-elements.html#previously-focused-element
+ GC::Ptr<DOM::Node> previously_focused_element;
+
Optional<Utf16FlyString> opened_in_popover_mode;
};
@@ -100,6 +104,7 @@ void HTMLElement::RareData::visit_edges(Cell::Visitor& visitor)
visitor.visit(attached_internals);
visitor.visit(popover_trigger);
visitor.visit(popover_close_watcher);
+ visitor.visit(previously_focused_element);
}
OwnPtr<DOM::Node::RareData> HTMLElement::create_rare_data() const
@@ -1840,7 +1845,9 @@ WebIDL::ExceptionOr<void> HTMLElement::show_popover(ThrowExceptions throw_except
return {};
}
- // FIXME: 4. If the result of running topmost auto or hint popover on document is null, then set shouldRestoreFocus to true.
+ // 4. If the result of running topmost auto or hint popover on document is null, then set shouldRestoreFocus to true.
+ if (!document.topmost_auto_or_hint_popover())
+ should_restore_focus = FocusPreviousElement::Yes;
// 5. If stackToAppendTo is "auto":
if (stack_to_append_to == StackToAppendTo::Auto) {
@@ -1888,8 +1895,13 @@ WebIDL::ExceptionOr<void> HTMLElement::show_popover(ThrowExceptions throw_except
rare_data.popover_close_watcher = CloseWatcher::establish(*document.window(), move(get_enabled_state));
rare_data.popover_close_watcher->add_event_listener_without_options(HTML::EventNames::close, DOM::IDLEventListener::create(close_callback));
}
- // FIXME: 19. Set element's previously focused element to null.
- // FIXME: 20. Let originallyFocusedElement be document's focused area of the document's DOM anchor.
+ // 19. Set element's previously focused element to null.
+ rare_data.previously_focused_element = nullptr;
+ // 20. Let originallyFocusedElement be document's focused area of the document's DOM anchor.
+ auto originally_focused_element = document.focused_area();
+ // NB: A null focused area represents viewport focus.
+ if (!originally_focused_element)
+ originally_focused_element = &document;
// 21. Add an element to the top layer given element.
document.add_an_element_to_the_top_layer(*this);
// 22. Set element's popover visibility state to showing.
@@ -1897,8 +1909,11 @@ WebIDL::ExceptionOr<void> HTMLElement::show_popover(ThrowExceptions throw_except
// 23. Set element's popover trigger to source.
rare_data.popover_trigger = source;
// FIXME: 24. Set element's implicit anchor element to source.
- // FIXME: 25. Run the popover focusing steps given element.
- // FIXME: 26. If shouldRestoreFocus is true and element's popover attribute is not in the No Popover state, then set element's previously focused element to originallyFocusedElement.
+ // 25. Run the popover focusing steps given element.
+ run_popover_focusing_steps(*this);
+ // 26. If shouldRestoreFocus is true and element's popover attribute is not in the No Popover state, then set element's previously focused element to originallyFocusedElement.
+ if (should_restore_focus == FocusPreviousElement::Yes && popover().has_value())
+ rare_data.previously_focused_element = originally_focused_element;
// 27. Queue a popover toggle event task given element, "closed", "open", and source.
queue_a_popover_toggle_event_task("closed"_utf16_fly_string, "open"_utf16_fly_string, source);
// 28. Run cleanupShowingFlag.
@@ -2034,11 +2049,21 @@ WebIDL::ExceptionOr<void> HTMLElement::hide_popover(FocusPreviousElement focus_p
if (fire_events == FireEvents::Yes)
queue_a_popover_toggle_event_task("open"_utf16_fly_string, "closed"_utf16_fly_string, source);
- // FIXME: 15. Let previouslyFocusedElement be element's previously focused element.
+ // 15. Let previouslyFocusedElement be element's previously focused element.
+ auto previously_focused_element = rare_data.previously_focused_element;
+
+ // 16. If previouslyFocusedElement is not null, then:
+ if (previously_focused_element) {
+ // 1. Set element's previously focused element to null.
+ rare_data.previously_focused_element = nullptr;
- // FIXME: 16. If previouslyFocusedElement is not null, then:
- // FIXME: 16.1. Set element's previously focused element to null.
- // FIXME: 16.2. If focusPreviousElement is true and document's focused area of the document's DOM anchor is a shadow-including inclusive descendant of element, then run the focusing steps for previouslyFocusedElement; the viewport should not be scrolled by doing this step.
+ // 2. If focusPreviousElement is true and document's focused area of the document's DOM anchor is a
+ // shadow-including inclusive descendant of element, then run the focusing steps for
+ // previouslyFocusedElement; the viewport should not be scrolled by doing this step.
+ auto focused_element = document.focused_area();
+ if (focus_previous_element == FocusPreviousElement::Yes && focused_element && focused_element->is_shadow_including_inclusive_descendant_of(*this))
+ run_focusing_steps(previously_focused_element, nullptr, FocusTrigger::Other, ScrollIntoView::No);
+ }
// 17. Run cleanupSteps.
cleanup_steps();
diff --git a/Tests/LibWeb/Text/expected/HTML/[email protected] b/Tests/LibWeb/Text/expected/HTML/[email protected]
new file mode 100644
index 00000000000..52a6b5a845a
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/[email protected]
@@ -0,0 +1,4 @@
+Viewport initially focused: true
+Popover focused when shown: true
+Viewport focused after hiding: true
+Late autofocus took focus: false
diff --git a/Tests/LibWeb/Text/expected/HTML/[email protected] b/Tests/LibWeb/Text/expected/HTML/[email protected]
new file mode 100644
index 00000000000..52a6b5a845a
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/[email protected]
@@ -0,0 +1,4 @@
+Viewport initially focused: true
+Popover focused when shown: true
+Viewport focused after hiding: true
+Late autofocus took focus: false
diff --git a/Tests/LibWeb/Text/expected/HTML/popover-focus-reentrant-adoption.txt b/Tests/LibWeb/Text/expected/HTML/popover-focus-reentrant-adoption.txt
new file mode 100644
index 00000000000..8b61b4d9e79
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/HTML/popover-focus-reentrant-adoption.txt
@@ -0,0 +1,4 @@
+dialog auto: focus handler ran=true, button adopted=true, popover open=true
+dialog manual: focus handler ran=true, button adopted=true, popover open=true
+div auto: focus handler ran=true, button adopted=true, popover open=true
+div manual: focus handler ran=true, button adopted=true, popover open=true
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/[email protected] b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/[email protected]
index 29cd31353c2..041386b16cb 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/[email protected]
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/[email protected]
@@ -2,10 +2,11 @@ Harness status: OK
Found 6 tests
-6 Fail
-Fail Focusing elements inside a popover with form controls inside, using commandfor
-Fail Focusing elements inside a popover with a details element inside, using commandfor
+4 Pass
+2 Fail
+Pass Focusing elements inside a popover with form controls inside, using commandfor
+Pass Focusing elements inside a popover with a details element inside, using commandfor
Fail Focusing elements inside a popover with a details element inside, and the after button adjacent to invoker, using commandfor
-Fail Focusing elements inside a popover with a custom-element with delegatesfocus inside, using commandfor
-Fail Focusing elements inside a popover with a custom-element with a slotted button inside, using commandfor
+Pass Focusing elements inside a popover with a custom-element with delegatesfocus inside, using commandfor
+Pass Focusing elements inside a popover with a custom-element with a slotted button inside, using commandfor
Fail Focusing elements inside a popover with custom-element with a slotted button, followed by a details element, where the after button is adjacent to the invoker, using commandfor
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/[email protected] b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/[email protected]
index d8fb2ad0eea..cd44a5cc305 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/[email protected]
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/[email protected]
@@ -2,10 +2,11 @@ Harness status: OK
Found 6 tests
-6 Fail
-Fail Focusing elements inside a popover with form controls inside, using commandforelement
-Fail Focusing elements inside a popover with a details element inside, using commandforelement
+4 Pass
+2 Fail
+Pass Focusing elements inside a popover with form controls inside, using commandforelement
+Pass Focusing elements inside a popover with a details element inside, using commandforelement
Fail Focusing elements inside a popover with a details element inside, and the after button adjacent to invoker, using commandforelement
-Fail Focusing elements inside a popover with a custom-element with delegatesfocus inside, using commandforelement
-Fail Focusing elements inside a popover with a custom-element with a slotted button inside, using commandforelement
+Pass Focusing elements inside a popover with a custom-element with delegatesfocus inside, using commandforelement
+Pass Focusing elements inside a popover with a custom-element with a slotted button inside, using commandforelement
Fail Focusing elements inside a popover with custom-element with a slotted button, followed by a details element, where the after button is adjacent to the invoker, using commandforelement
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/[email protected] b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/[email protected]
index f94bfb39a04..772b2dac425 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/[email protected]
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/[email protected]
@@ -2,10 +2,11 @@ Harness status: OK
Found 6 tests
-6 Fail
-Fail Focusing elements inside a popover with form controls inside, using imperative
-Fail Focusing elements inside a popover with a details element inside, using imperative
+4 Pass
+2 Fail
+Pass Focusing elements inside a popover with form controls inside, using imperative
+Pass Focusing elements inside a popover with a details element inside, using imperative
Fail Focusing elements inside a popover with a details element inside, and the after button adjacent to invoker, using imperative
-Fail Focusing elements inside a popover with a custom-element with delegatesfocus inside, using imperative
-Fail Focusing elements inside a popover with a custom-element with a slotted button inside, using imperative
+Pass Focusing elements inside a popover with a custom-element with delegatesfocus inside, using imperative
+Pass Focusing elements inside a popover with a custom-element with a slotted button inside, using imperative
Fail Focusing elements inside a popover with custom-element with a slotted button, followed by a details element, where the after button is adjacent to the invoker, using imperative
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/[email protected] b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/[email protected]
index a74481ca6c8..84a11e9fd64 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/[email protected]
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/[email protected]
@@ -2,10 +2,11 @@ Harness status: OK
Found 6 tests
-6 Fail
-Fail Focusing elements inside a popover with form controls inside, using popovertargetelement
-Fail Focusing elements inside a popover with a details element inside, using popovertargetelement
+4 Pass
+2 Fail
+Pass Focusing elements inside a popover with form controls inside, using popovertargetelement
+Pass Focusing elements inside a popover with a details element inside, using popovertargetelement
Fail Focusing elements inside a popover with a details element inside, and the after button adjacent to invoker, using popovertargetelement
-Fail Focusing elements inside a popover with a custom-element with delegatesfocus inside, using popovertargetelement
-Fail Focusing elements inside a popover with a custom-element with a slotted button inside, using popovertargetelement
+Pass Focusing elements inside a popover with a custom-element with delegatesfocus inside, using popovertargetelement
+Pass Focusing elements inside a popover with a custom-element with a slotted button inside, using popovertargetelement
Fail Focusing elements inside a popover with custom-element with a slotted button, followed by a details element, where the after button is adjacent to the invoker, using popovertargetelement
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/popover-focus.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/popover-focus.txt
index bdc24529f40..03d1b3481e6 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/popover-focus.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/popover-focus.txt
@@ -2,35 +2,34 @@ Harness status: OK
Found 30 tests
-1 Pass
-29 Fail
-Fail Popover focus test: default behavior - popover is not focused
-Fail Popover button click focus test: default behavior - popover is not focused
+30 Pass
+Pass Popover focus test: default behavior - popover is not focused
+Pass Popover button click focus test: default behavior - popover is not focused
Pass Popover corner cases test: default behavior - popover is not focused
-Fail Popover focus test: autofocus popover
-Fail Popover button click focus test: autofocus popover
-Fail Popover corner cases test: autofocus popover
-Fail Popover focus test: autofocus empty popover
-Fail Popover button click focus test: autofocus empty popover
-Fail Popover corner cases test: autofocus empty popover
-Fail Popover focus test: autofocus popover with button
-Fail Popover button click focus test: autofocus popover with button
-Fail Popover corner cases test: autofocus popover with button
-Fail Popover focus test: autofocus child
-Fail Popover button click focus test: autofocus child
-Fail Popover corner cases test: autofocus child
-Fail Popover focus test: autofocus on tabindex=0 element
-Fail Popover button click focus test: autofocus on tabindex=0 element
-Fail Popover corner cases test: autofocus on tabindex=0 element
-Fail Popover focus test: autofocus multiple children
-Fail Popover button click focus test: autofocus multiple children
-Fail Popover corner cases test: autofocus multiple children
-Fail Popover focus test: autofocus popover and multiple autofocus children
-Fail Popover button click focus test: autofocus popover and multiple autofocus children
-Fail Popover corner cases test: autofocus popover and multiple autofocus children
-Fail Popover focus test: Opening dialogs as popovers should use dialog initial focus algorithm.
-Fail Popover button click focus test: Opening dialogs as popovers should use dialog initial focus algorithm.
-Fail Popover corner cases test: Opening dialogs as popovers should use dialog initial focus algorithm.
-Fail Popover focus test: Opening dialogs as popovers which have autofocus should focus the dialog.
-Fail Popover button click focus test: Opening dialogs as popovers which have autofocus should focus the dialog.
-Fail Popover corner cases test: Opening dialogs as popovers which have autofocus should focus the dialog.
\ No newline at end of file
+Pass Popover focus test: autofocus popover
+Pass Popover button click focus test: autofocus popover
+Pass Popover corner cases test: autofocus popover
+Pass Popover focus test: autofocus empty popover
+Pass Popover button click focus test: autofocus empty popover
+Pass Popover corner cases test: autofocus empty popover
+Pass Popover focus test: autofocus popover with button
+Pass Popover button click focus test: autofocus popover with button
+Pass Popover corner cases test: autofocus popover with button
+Pass Popover focus test: autofocus child
+Pass Popover button click focus test: autofocus child
+Pass Popover corner cases test: autofocus child
+Pass Popover focus test: autofocus on tabindex=0 element
+Pass Popover button click focus test: autofocus on tabindex=0 element
+Pass Popover corner cases test: autofocus on tabindex=0 element
+Pass Popover focus test: autofocus multiple children
+Pass Popover button click focus test: autofocus multiple children
+Pass Popover corner cases test: autofocus multiple children
+Pass Popover focus test: autofocus popover and multiple autofocus children
+Pass Popover button click focus test: autofocus popover and multiple autofocus children
+Pass Popover corner cases test: autofocus popover and multiple autofocus children
+Pass Popover focus test: Opening dialogs as popovers should use dialog initial focus algorithm.
+Pass Popover button click focus test: Opening dialogs as popovers should use dialog initial focus algorithm.
+Pass Popover corner cases test: Opening dialogs as popovers should use dialog initial focus algorithm.
+Pass Popover focus test: Opening dialogs as popovers which have autofocus should focus the dialog.
+Pass Popover button click focus test: Opening dialogs as popovers which have autofocus should focus the dialog.
+Pass Popover corner cases test: Opening dialogs as popovers which have autofocus should focus the dialog.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/popover-shadowhost-focus.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/popover-shadowhost-focus.txt
index 57c692bf441..0565c2f9e86 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/popover-shadowhost-focus.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/popovers/popover-shadowhost-focus.txt
@@ -2,9 +2,8 @@ Harness status: OK
Found 4 tests
-2 Pass
-2 Fail
-Fail autofocus=true, delegatesfocus=false
-Fail autofocus=true, delegatesfocus=true
+4 Pass
+Pass autofocus=true, delegatesfocus=false
+Pass autofocus=true, delegatesfocus=true
Pass autofocus=false, delegatesfocus=false
Pass autofocus=false, delegatesfocus=true
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/HTML/popover-consumes-autofocus.html b/Tests/LibWeb/Text/input/HTML/popover-consumes-autofocus.html
new file mode 100644
index 00000000000..4f1c56847bf
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/popover-consumes-autofocus.html
@@ -0,0 +1,25 @@
+<!doctype html>
+<meta name="variant" content="?dialog" />
+<meta name="variant" content="?div" />
+<script src="../include.js"></script>
+<script>
+ asyncTest(async done => {
+ const tag = location.search.slice(1) || "dialog";
+ println(`Viewport initially focused: ${document.activeElement === document.body}`);
+ const popover = document.createElement(tag);
+ popover.setAttribute("popover", "auto");
+ popover.tabIndex = -1;
+ popover.autofocus = true;
+ document.body.append(popover);
+ popover.showPopover();
+ println(`Popover focused when shown: ${document.activeElement === popover}`);
+ popover.hidePopover();
+ println(`Viewport focused after hiding: ${document.activeElement === document.body}`);
+ const late = document.createElement("input");
+ late.autofocus = true;
+ document.body.append(late);
+ await animationFrame();
+ println(`Late autofocus took focus: ${document.activeElement === late}`);
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/HTML/popover-focus-reentrant-adoption.html b/Tests/LibWeb/Text/input/HTML/popover-focus-reentrant-adoption.html
new file mode 100644
index 00000000000..baa3bd90a9d
--- /dev/null
+++ b/Tests/LibWeb/Text/input/HTML/popover-focus-reentrant-adoption.html
@@ -0,0 +1,28 @@
+<!doctype html>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ for (const tag of ["dialog", "div"]) {
+ for (const mode of ["auto", "manual"]) {
+ const inert = document.implementation.createHTMLDocument();
+ const popover = document.createElement(tag);
+ popover.setAttribute("popover", mode);
+ const button = document.createElement("button");
+ button.autofocus = true;
+ let focusHandlerRan = false;
+ button.addEventListener("focus", () => {
+ focusHandlerRan = true;
+ inert.body.append(button);
+ });
+ popover.append(button);
+ document.body.append(popover);
+ popover.showPopover();
+ println(
+ `${tag} ${mode}: focus handler ran=${focusHandlerRan}, button adopted=${button.ownerDocument === inert}, popover open=${popover.matches(":popover-open")}`
+ );
+ popover.hidePopover();
+ popover.remove();
+ }
+ }
+ });
+</script>