Skip to content

Truly independent web browser

LibWeb: Update :focus-visible as a focused element takes keyboard input

Andreas Klingda9acfa0fdfb
diff --git a/Libraries/LibWeb/CSS/Invalidation/PseudoClassInvalidator.cpp b/Libraries/LibWeb/CSS/Invalidation/PseudoClassInvalidator.cpp
index 38fe8487dda..0fc9f7fec1a 100644
--- a/Libraries/LibWeb/CSS/Invalidation/PseudoClassInvalidator.cpp
+++ b/Libraries/LibWeb/CSS/Invalidation/PseudoClassInvalidator.cpp
@@ -175,6 +175,14 @@ void invalidate_style_after_slot_assignment_changed(DOM::Element& slot)
}
}
+// Whether the focused element takes keyboard input decides whether it indicates focus, which then moves without focus
+// moving, so the element is asked its own answer.
+void invalidate_style_after_focused_element_keyboard_input_change(DOM::Document& document)
+{
+ if (auto* element = as_if<DOM::Element>(document.focused_area().ptr()))
+ record_element_state_changed(*element, CSS::PseudoClass::FocusVisible, element->should_indicate_focus());
+}
+
// A slot leaving takes the flat tree chain of the elements assigned to it along, though they stay where they are, so
// that the source stands in no chain through the subtree by the time the subtree has left.
void invalidate_style_after_subtree_holding_a_state_source_left(DOM::Node& subtree, GC::Ptr<DOM::Node> old_parent)
diff --git a/Libraries/LibWeb/CSS/Invalidation/PseudoClassInvalidator.h b/Libraries/LibWeb/CSS/Invalidation/PseudoClassInvalidator.h
index 22f01202583..14aa44d33b8 100644
--- a/Libraries/LibWeb/CSS/Invalidation/PseudoClassInvalidator.h
+++ b/Libraries/LibWeb/CSS/Invalidation/PseudoClassInvalidator.h
@@ -18,5 +18,6 @@ bool descendants_hold_a_propagating_state_source(DOM::Node&);
bool holds_a_propagating_state_source(DOM::Node&);
void invalidate_style_after_slot_assignment_changed(DOM::Element& slot);
void invalidate_style_after_subtree_holding_a_state_source_left(DOM::Node& subtree, GC::Ptr<DOM::Node> old_parent);
+void invalidate_style_after_focused_element_keyboard_input_change(DOM::Document&);
}
diff --git a/Libraries/LibWeb/DOM/Node.cpp b/Libraries/LibWeb/DOM/Node.cpp
index bdd0debeb44..e1a8159f5b5 100644
--- a/Libraries/LibWeb/DOM/Node.cpp
+++ b/Libraries/LibWeb/DOM/Node.cpp
@@ -2180,6 +2180,8 @@ WebIDL::ExceptionOr<void> Node::move_node(Node& new_parent, Node* child)
for (auto const& state : previous_read_write_states)
CSS::Invalidation::invalidate_style_after_read_write_state_change(*state.element, state.value);
+ if (!previous_read_write_states.is_empty())
+ CSS::Invalidation::invalidate_style_after_focused_element_keyboard_input_change(document());
// 26. Queue a tree mutation record for oldParent with « », « node », oldPreviousSibling, and oldNextSibling.
GC::Ref<Node> moved_node = *this;
@@ -2456,6 +2458,7 @@ void Node::recompute_editable_subtree_flags_and_repaint()
return TraversalDecision::Continue;
});
document().page().keyboard_scroll_editability_changed(document());
+ CSS::Invalidation::invalidate_style_after_focused_element_keyboard_input_change(document());
}
// https://w3c.github.io/editing/docs/execCommand/#editable
diff --git a/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Libraries/LibWeb/HTML/HTMLInputElement.cpp
index 5d94a07c51f..5520e582798 100644
--- a/Libraries/LibWeb/HTML/HTMLInputElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLInputElement.cpp
@@ -25,6 +25,7 @@
#include <LibWeb/CSS/ElementBoxKind.h>
#include <LibWeb/CSS/Invalidation/ElementStateInvalidator.h>
#include <LibWeb/CSS/Invalidation/FormControlInvalidator.h>
+#include <LibWeb/CSS/Invalidation/PseudoClassInvalidator.h>
#include <LibWeb/CSS/Parser/Parser.h>
#include <LibWeb/CSS/PropertyID.h>
#include <LibWeb/CSS/SelectorMatching.h>
@@ -1964,6 +1965,7 @@ void HTMLInputElement::type_attribute_changed(TypeAttributeState old_state, Type
CSS::Invalidation::invalidate_style_after_default_state_change(*this, was_default);
}
CSS::Invalidation::invalidate_style_after_read_write_state_change(*this, was_read_write);
+ CSS::Invalidation::invalidate_style_after_focused_element_keyboard_input_change(document());
clear_element_reference_pseudo_elements();
auto should_materialize_shadow_tree = shadow_root() || has_style();
set_shadow_root(nullptr);
diff --git a/Tests/LibWeb/Text/expected/css/style-engine/focus-visible-as-editability-changes.txt b/Tests/LibWeb/Text/expected/css/style-engine/focus-visible-as-editability-changes.txt
index e54c47fe2b7..01e2fc6d1d4 100644
--- a/Tests/LibWeb/Text/expected/css/style-engine/focus-visible-as-editability-changes.txt
+++ b/Tests/LibWeb/Text/expected/css/style-engine/focus-visible-as-editability-changes.txt
@@ -1,12 +1,13 @@
Focused with a pointer: focused true, matches false, rgb(0, 0, 0)
-Ancestor made editable: focused true, matches true, rgb(0, 0, 0)
+Ancestor made editable: focused true, matches true, rgb(1, 2, 3)
Ancestor no longer editable: focused true, matches false, rgb(0, 0, 0)
-Made an editing host: focused true, matches true, rgb(0, 0, 0)
+Made an editing host: focused true, matches true, rgb(1, 2, 3)
No longer an editing host: focused true, matches false, rgb(0, 0, 0)
-designMode on: focused false, matches false, rgb(1, 2, 3)
-designMode off: focused false, matches false, rgb(1, 2, 3)
-Moved into an editing host: focused false, matches false, rgb(1, 2, 3)
-Moved out of it: focused false, matches false, rgb(1, 2, 3)
+designMode on moved focus to the root: focused true, matches true, rgb(1, 2, 3)
+designMode off: focused true, matches false, rgb(0, 0, 0)
+Focused with a pointer again: focused true, matches false, rgb(0, 0, 0)
+Moved into an editing host: focused true, matches true, rgb(1, 2, 3)
+Moved out of it: focused true, matches false, rgb(0, 0, 0)
Checkbox focused with a pointer: focused true, matches false, rgb(0, 0, 0)
-Became a text field: focused true, matches true, rgb(0, 0, 0)
+Became a text field: focused true, matches true, rgb(1, 2, 3)
Became a checkbox: focused true, matches false, rgb(0, 0, 0)
diff --git a/Tests/LibWeb/Text/input/css/style-engine/focus-visible-as-editability-changes.html b/Tests/LibWeb/Text/input/css/style-engine/focus-visible-as-editability-changes.html
index 005b2af9e46..5b880f4214c 100644
--- a/Tests/LibWeb/Text/input/css/style-engine/focus-visible-as-editability-changes.html
+++ b/Tests/LibWeb/Text/input/css/style-engine/focus-visible-as-editability-changes.html
@@ -40,9 +40,11 @@
focusable.removeAttribute("contenteditable");
report("No longer an editing host", focusable);
document.designMode = "on";
- report("designMode on", focusable);
+ report("designMode on moved focus to the root", document.documentElement);
document.designMode = "off";
- report("designMode off", focusable);
+ report("designMode off", document.documentElement);
+ focusWithPointer(focusable);
+ report("Focused with a pointer again", focusable);
element("editable").moveBefore(focusable, null);
report("Moved into an editing host", focusable);
element("ancestor").moveBefore(focusable, null);