Skip to content

Truly independent web browser

LibWeb: Implement the autofocus delegate and dialog focus delegate

Andreas Kling642aafb4a046
diff --git a/Libraries/LibWeb/HTML/Focus.cpp b/Libraries/LibWeb/HTML/Focus.cpp
index d806f4311d0..fc18daf8220 100644
--- a/Libraries/LibWeb/HTML/Focus.cpp
+++ b/Libraries/LibWeb/HTML/Focus.cpp
@@ -19,6 +19,7 @@
#include <LibWeb/HTML/BrowsingContext.h>
#include <LibWeb/HTML/Focus.h>
#include <LibWeb/HTML/HTMLAreaElement.h>
+#include <LibWeb/HTML/HTMLDialogElement.h>
#include <LibWeb/HTML/HTMLInputElement.h>
#include <LibWeb/HTML/HTMLSlotElement.h>
#include <LibWeb/HTML/LocalNavigable.h>
@@ -400,8 +401,38 @@ static bool is_focusable_area(DOM::Node& node)
// https://html.spec.whatwg.org/multipage/interaction.html#get-the-focusable-area
static GC::Ptr<DOM::Node> get_focusable_area(DOM::Node& focus_target, FocusTrigger focus_trigger);
+static bool is_sequentially_focusable(DOM::Node const&);
+
+// https://html.spec.whatwg.org/multipage/interaction.html#autofocus-delegate
+static GC::Ptr<DOM::Node> autofocus_delegate(DOM::Node& focus_target, FocusTrigger focus_trigger)
+{
+ // 1. For each descendant descendant of focus target, in tree order:
+ for (auto* descendant = focus_target.first_child(); descendant; descendant = descendant->next_in_pre_order(&focus_target)) {
+ // 1. If descendant does not have an autofocus content attribute, then continue.
+ auto* element = as_if<DOM::Element>(*descendant);
+ if (!element || !element->has_attribute(AttributeNames::autofocus))
+ continue;
+
+ // 2. Let focusable area be descendant, if descendant is a focusable area; otherwise let focusable area be the
+ // result of getting the focusable area for descendant given focus trigger.
+ GC::Ptr<DOM::Node> focusable_area = is_focusable_area(*descendant) ? descendant : get_focusable_area(*descendant, focus_trigger);
+
+ // 3. If focusable area is null, then continue.
+ if (!focusable_area)
+ continue;
+
+ // FIXME: 4. If focusable area is not click focusable and focus trigger is "click", then continue.
+
+ // 5. Return focusable area.
+ return focusable_area;
+ }
+
+ // 2. Return null.
+ return nullptr;
+}
+
// https://html.spec.whatwg.org/multipage/interaction.html#focus-delegate
-static GC::Ptr<DOM::Node> focus_delegate(DOM::Node& focus_target, FocusTrigger focus_trigger)
+GC::Ptr<DOM::Node> focus_delegate(DOM::Node& focus_target, FocusTrigger focus_trigger)
{
GC::Ptr<DOM::Node> where_to_look = focus_target;
@@ -415,23 +446,37 @@ static GC::Ptr<DOM::Node> focus_delegate(DOM::Node& focus_target, FocusTrigger f
where_to_look = shadow_root;
}
- // FIXME: 4. Let autofocusDelegate be the autofocus delegate for whereToLook given focusTrigger.
- // FIXME: 5. If autofocusDelegate is not null, then return autofocusDelegate.
+ // 4. Let autofocusDelegate be the autofocus delegate for whereToLook given focusTrigger.
+ auto autofocus_delegate = HTML::autofocus_delegate(*where_to_look, focus_trigger);
+
+ // 5. If autofocusDelegate is not null, then return autofocusDelegate.
+ if (autofocus_delegate)
+ return autofocus_delegate;
// 6. For each descendant of whereToLook's descendants, in tree order:
for (auto* descendant = where_to_look->first_child(); descendant; descendant = descendant->next_in_pre_order(where_to_look.ptr())) {
- // FIXME: 1. Let focusableArea be null.
- // FIXME: 2. If focusTarget is a dialog element and descendant is sequentially focusable, then
- // set focusableArea to descendant.
-
- // 3. Otherwise, if focusTarget is not a dialog and descendant is a focusable area, set
- // focusableArea to descendant.
- if (is_focusable_area(*descendant))
- return descendant;
-
- // 4. Otherwise, set focusableArea to the result of getting the focusable area for descendant
- // given focusTrigger.
- if (auto focusable_area = get_focusable_area(*descendant, focus_trigger))
+ // 1. Let focusableArea be null.
+ GC::Ptr<DOM::Node> focusable_area;
+
+ // 2. If focusTarget is a dialog element and descendant is sequentially focusable, then set focusableArea to
+ // descendant.
+ if (is<HTMLDialogElement>(focus_target)) {
+ if (is_sequentially_focusable(*descendant))
+ focusable_area = descendant;
+ }
+ // 3. Otherwise, if focusTarget is not a dialog and descendant is a focusable area, set focusableArea to
+ // descendant.
+ else if (is_focusable_area(*descendant)) {
+ focusable_area = descendant;
+ }
+
+ // 4. Otherwise, set focusableArea to the result of getting the focusable area for descendant given
+ // focusTrigger.
+ if (!focusable_area)
+ focusable_area = get_focusable_area(*descendant, focus_trigger);
+
+ // 5. If focusableArea is not null, then return focusableArea.
+ if (focusable_area)
return focusable_area;
}
@@ -796,6 +841,16 @@ static Optional<i32> tabindex_value(DOM::Element const& element)
return parse_integer(*attribute);
}
+// https://html.spec.whatwg.org/multipage/interaction.html#sequentially-focusable
+static bool is_sequentially_focusable(DOM::Node const& node)
+{
+ // NB: A focusable area is sequentially focusable unless its tabindex value is a negative integer.
+ auto const* element = as_if<DOM::Element>(node);
+ if (!element || !is_focusable_area(const_cast<DOM::Element&>(*element)))
+ return false;
+ return tabindex_value(*element).value_or(0) >= 0;
+}
+
// https://html.spec.whatwg.org/multipage/interaction.html#tabindex-ordered-focus-navigation-scope
static GC::ConservativeVector<GC::Ref<DOM::Element>> tabindex_ordered_focus_navigation_scope(DOM::Node& owner)
{
diff --git a/Libraries/LibWeb/HTML/Focus.h b/Libraries/LibWeb/HTML/Focus.h
index 0b8f8d3fb7b..2f8effa9281 100644
--- a/Libraries/LibWeb/HTML/Focus.h
+++ b/Libraries/LibWeb/HTML/Focus.h
@@ -25,6 +25,7 @@ WEB_API Vector<GC::Root<DOM::Node>> focus_chain(GC::Ptr<DOM::Node> subject);
WEB_API void run_focus_update_steps(Vector<GC::Root<DOM::Node>> old_chain, Vector<GC::Root<DOM::Node>> new_chain, GC::Ptr<DOM::Node> new_focus_target);
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);
// 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 da5ccb95ef6..5db12797ad4 100644
--- a/Libraries/LibWeb/HTML/HTMLDialogElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLDialogElement.cpp
@@ -486,10 +486,15 @@ void HTMLDialogElement::run_dialog_focusing_steps()
return;
// 2. Let control be null
- GC::Ptr<Element> control = nullptr;
+ GC::Ptr<DOM::Node> control = nullptr;
- // FIXME: 3. If subject has the autofocus attribute, then set control to subject.
- // FIXME: 4. If control is null, then set control to the focus delegate of subject.
+ // 3. If subject has the autofocus attribute, then set control to subject.
+ if (has_attribute(AttributeNames::autofocus))
+ control = this;
+
+ // 4. If control is null, then set control to the focus delegate of subject.
+ if (!control)
+ control = focus_delegate(*this);
// 5. If control is null, then set control to subject.
if (!control)
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/interactive-elements/the-dialog-element/dialog-autofocus-multiple-times.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/interactive-elements/the-dialog-element/dialog-autofocus-multiple-times.txt
index 4533a73a119..83826422285 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/interactive-elements/the-dialog-element/dialog-autofocus-multiple-times.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/interactive-elements/the-dialog-element/dialog-autofocus-multiple-times.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail autofocus is run every time a dialog is opened
\ No newline at end of file
+1 Pass
+Pass autofocus is run every time a dialog is opened
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/interactive-elements/the-dialog-element/dialog-autofocus.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/interactive-elements/the-dialog-element/dialog-autofocus.txt
index 1614562071e..26391fb9a38 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/interactive-elements/the-dialog-element/dialog-autofocus.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/interactive-elements/the-dialog-element/dialog-autofocus.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail autofocus when a modal dialog is opened
\ No newline at end of file
+1 Pass
+Pass autofocus when a modal dialog is opened
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/interactive-elements/the-dialog-element/dialog-focus-shadow-double-nested.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/interactive-elements/the-dialog-element/dialog-focus-shadow-double-nested.txt
index 344698ac0f8..21de8237317 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/interactive-elements/the-dialog-element/dialog-focus-shadow-double-nested.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/interactive-elements/the-dialog-element/dialog-focus-shadow-double-nested.txt
@@ -2,6 +2,6 @@ Harness status: OK
Found 2 tests
-2 Fail
-Fail show()
-Fail showModal()
\ No newline at end of file
+2 Pass
+Pass show()
+Pass showModal()
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/interactive-elements/the-dialog-element/show-modal-focusing-steps.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/interactive-elements/the-dialog-element/show-modal-focusing-steps.txt
index 89d23b3b323..bb128d754ab 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/interactive-elements/the-dialog-element/show-modal-focusing-steps.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/interactive-elements/the-dialog-element/show-modal-focusing-steps.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail focus when a modal dialog is opened
\ No newline at end of file
+1 Pass
+Pass focus when a modal dialog is opened
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-autofocus.txt b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-autofocus.txt
index 756d07c38f0..6f7a484762e 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-autofocus.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/shadow-dom/focus/focus-autofocus.txt
@@ -2,10 +2,9 @@ Harness status: OK
Found 5 tests
-2 Pass
-3 Fail
-Fail The second input should be focused since it has autofocus
+5 Pass
+Pass The second input should be focused since it has autofocus
Pass Focus should not be delegated to the autofocus element because the inner host doesn't have delegates focus
-Fail Focus should be delegated to the autofocus element when the inner host has delegates focus
+Pass Focus should be delegated to the autofocus element when the inner host has delegates focus
Pass Focus should not be delegated to the slotted elements
-Fail Focus should be delegated to the nested div which has autofocus based on the tree order
\ No newline at end of file
+Pass Focus should be delegated to the nested div which has autofocus based on the tree order
\ No newline at end of file