Skip to content

Truly independent web browser

LibWeb: Stop user action state propagation at top layer elements

Andreas Kling32c1c6e35147
diff --git a/Libraries/LibWeb/CSS/Invalidation/PseudoClassInvalidator.cpp b/Libraries/LibWeb/CSS/Invalidation/PseudoClassInvalidator.cpp
index 79b68cfa667..7bb4b1f6d3e 100644
--- a/Libraries/LibWeb/CSS/Invalidation/PseudoClassInvalidator.cpp
+++ b/Libraries/LibWeb/CSS/Invalidation/PseudoClassInvalidator.cpp
@@ -46,7 +46,7 @@ void invalidate_style_after_pseudo_class_state_change(CSS::PseudoClass pseudo_cl
if (propagates) {
for_each_inclusive_ancestor_element(*start, traversal, [&](DOM::Element& element) {
chain.set(&element);
- return TraversalDecision::Continue;
+ return element.rendered_in_top_layer() ? TraversalDecision::Break : TraversalDecision::Continue;
});
} else if (auto* element = as_if<DOM::Element>(*start)) {
chain.set(element);
@@ -79,7 +79,7 @@ void invalidate_style_after_pseudo_class_state_change(CSS::PseudoClass pseudo_cl
if (other_chain.contains(&element))
return TraversalDecision::Break;
visit(element);
- return TraversalDecision::Continue;
+ return element.rendered_in_top_layer() ? TraversalDecision::Break : TraversalDecision::Continue;
});
} else if (auto* element = as_if<DOM::Element>(*start)) {
if (!other_chain.contains(element))
@@ -112,6 +112,8 @@ static bool subtree_holds_propagating_state_source(DOM::Node const& subtree, CSS
return true;
if (is<DOM::ShadowRoot>(subtree) && node->parent() == &subtree)
return true;
+ if (auto const* element = as_if<DOM::Element>(*node); element && element->rendered_in_top_layer())
+ break;
}
return false;
}
@@ -139,7 +141,7 @@ void invalidate_style_after_subtree_place_changed(DOM::Node& subtree, GC::Ptr<DO
return;
for_each_inclusive_ancestor_element(*from, ancestor_traversal_for_pseudo_class(pseudo_class), [&](DOM::Element& element) {
record_element_state_changed(element, pseudo_class, element.matches_focus_within_pseudo_class());
- return TraversalDecision::Continue;
+ return element.rendered_in_top_layer() ? TraversalDecision::Break : TraversalDecision::Continue;
});
};
diff --git a/Libraries/LibWeb/CSS/SelectorMatching.cpp b/Libraries/LibWeb/CSS/SelectorMatching.cpp
index 20a9a8ffdaa..77edd6cbac6 100644
--- a/Libraries/LibWeb/CSS/SelectorMatching.cpp
+++ b/Libraries/LibWeb/CSS/SelectorMatching.cpp
@@ -31,9 +31,15 @@ static bool matches_hover_pseudo_class(DOM::Element const& element)
auto* hovered_node = element.document().hovered_node();
if (!hovered_node)
return false;
- if (&element == hovered_node)
- return true;
- return element.is_shadow_including_ancestor_of(*hovered_node);
+ // https://drafts.csswg.org/selectors-4/#useraction-pseudos
+ // NB: Match the top layer boundary used by the style engine's hover chain.
+ for (auto const* node = hovered_node; node; node = node->parent_or_shadow_host_node()) {
+ if (node == &element)
+ return true;
+ if (auto const* ancestor = as_if<DOM::Element>(*node); ancestor && ancestor->rendered_in_top_layer())
+ break;
+ }
+ return false;
}
// https://html.spec.whatwg.org/multipage/semantics-other.html#selector-indeterminate
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp
index 586a7862556..b727a8dbfd1 100644
--- a/Libraries/LibWeb/DOM/Element.cpp
+++ b/Libraries/LibWeb/DOM/Element.cpp
@@ -45,6 +45,7 @@
#include <LibWeb/CSS/Invalidation/ElementStateInvalidator.h>
#include <LibWeb/CSS/Invalidation/LanguageInvalidator.h>
#include <LibWeb/CSS/Invalidation/PartInvalidator.h>
+#include <LibWeb/CSS/Invalidation/PseudoClassInvalidator.h>
#include <LibWeb/CSS/Parser/Parser.h>
#include <LibWeb/CSS/PropertyID.h>
#include <LibWeb/CSS/SelectorMatching.h>
@@ -3487,6 +3488,17 @@ void Element::set_rendered_in_top_layer(bool rendered_in_top_layer)
return;
m_rendered_in_top_layer = rendered_in_top_layer;
CSS::record_element_adjustment_facts(*this);
+ // NB: Changing the top layer boundary moves ancestor state even when focus and the pointer stay put.
+ CSS::Invalidation::invalidate_style_after_subtree_place_changed(*this, flat_tree_parent());
+ if (is_being_activated()) {
+ for (auto* node = flat_tree_parent(); node; node = node->flat_tree_parent()) {
+ if (auto* element = as_if<Element>(*node)) {
+ element->set_being_activated(!rendered_in_top_layer);
+ if (element->rendered_in_top_layer())
+ break;
+ }
+ }
+ }
}
Layout::NodeWithStyle* Element::pseudo_element_layout_node(Layout::BegunRead const& read, CSS::PseudoElement pseudo_element) const
@@ -3650,9 +3662,14 @@ bool Element::matches_focus_within_pseudo_class() const
if (!focused_area)
return false;
+ // https://drafts.csswg.org/selectors-4/#useraction-pseudos
+ // Specifically, if these match on a given element, they also match on the element's flat tree ancestors, up to and
+ // including the first top layer element or the root element, whichever is encountered first.
for (auto const* node = focused_area.ptr(); node; node = node->flat_tree_parent()) {
if (node == this)
return true;
+ if (auto const* element = as_if<Element>(*node); element && element->rendered_in_top_layer())
+ break;
}
return false;
}
diff --git a/Libraries/LibWeb/HTML/Focus.cpp b/Libraries/LibWeb/HTML/Focus.cpp
index f303eb11ba8..615d17fc9ac 100644
--- a/Libraries/LibWeb/HTML/Focus.cpp
+++ b/Libraries/LibWeb/HTML/Focus.cpp
@@ -124,8 +124,11 @@ void run_focus_update_steps(Vector<GC::Root<DOM::Node>> old_chain, Vector<GC::Ro
auto build_focus_within_chain = [](GC::Ptr<DOM::Node> source) {
Vector<GC::Root<DOM::Element>> chain;
for (auto* node = source.ptr(); node; node = node->flat_tree_parent()) {
- if (auto* element = as_if<DOM::Element>(*node))
+ if (auto* element = as_if<DOM::Element>(*node)) {
chain.append(*element);
+ if (element->rendered_in_top_layer())
+ break;
+ }
}
return chain;
};
diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cpp
index 8fbe636739d..9feea54fb44 100644
--- a/Libraries/LibWeb/Page/EventHandler.cpp
+++ b/Libraries/LibWeb/Page/EventHandler.cpp
@@ -5175,9 +5175,13 @@ static void light_dismiss_activities(UIEvents::PointerEvent const& event, GC::Re
static void set_node_and_ancestors_being_activated(GC::Ptr<DOM::Node> node, bool activated)
{
- for (auto ancestor = node; ancestor; ancestor = ancestor->parent()) {
- if (auto* element = as_if<DOM::Element>(*ancestor))
+ // https://drafts.csswg.org/selectors-4/#useraction-pseudos
+ for (auto ancestor = node; ancestor; ancestor = ancestor->flat_tree_parent()) {
+ if (auto* element = as_if<DOM::Element>(*ancestor)) {
element->set_being_activated(activated);
+ if (element->rendered_in_top_layer())
+ break;
+ }
}
}
diff --git a/Libraries/LibWeb/Rust/src/css/style/boundary.rs b/Libraries/LibWeb/Rust/src/css/style/boundary.rs
index d40b1cf3728..8f79d7b45a2 100644
--- a/Libraries/LibWeb/Rust/src/css/style/boundary.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/boundary.rs
@@ -278,7 +278,13 @@ style_boundary! {
note_style_reaction_applied => NoteStyleReactionApplied {
node: StyleNodeID, reaction: u8, inherited_style_groups_changed: u8, changed_non_inherited_style_groups: u32, facts: u32
};
- set_element_adjustment_facts => SetElementAdjustmentFacts { node: StyleNodeID, facts: u32 };
+ set_element_adjustment_facts => SetElementAdjustmentFacts { node: StyleNodeID, facts: u32 } => {
+ let changed = engine.element_adjustment_facts(node) ^ facts;
+ engine.set_element_adjustment_facts(node, facts);
+ if changed & super::bridge::element_adjustment_fact::RENDERED_IN_TOP_LAYER != 0 {
+ engine.set_hover(engine.hover_target());
+ }
+ };
set_element_construction_facts => SetElementConstructionFacts { node: StyleNodeID, facts: u32 };
set_element_box_kind => SetElementBoxKind { node: StyleNodeID, box_kind: ElementBoxKind };
set_element_replaced_content_input => SetElementReplacedContentInput {
diff --git a/Libraries/LibWeb/Rust/src/css/style/hover_lane.rs b/Libraries/LibWeb/Rust/src/css/style/hover_lane.rs
index 6edf2f8da4d..403e58f8baf 100644
--- a/Libraries/LibWeb/Rust/src/css/style/hover_lane.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/hover_lane.rs
@@ -180,17 +180,24 @@ impl StyleEngine {
relations.parent.or_else(|| tree.host_of(node))
}
- /// The elements that match `:hover` while `hovered` is hovered: the element and its shadow-including ancestor
- /// elements, short of the document at the root of the tree, as the tree changes recorded so far leave it.
+ /// The elements that match `:hover` while `hovered` is hovered, up to the first top layer element or the root,
+ /// as the tree changes recorded so far leave it.
fn hover_chain(&self, hovered: Option<StyleNodeID>) -> HoverChain {
let tree = self.tree();
- std::iter::successors(hovered, |&node| self.recorded_parent_or_shadow_host(node))
- .filter(|&node| {
- node.element_index().is_some()
- && tree.host_of(node).is_none()
- && self.recorded_parent_or_shadow_host(node).is_some()
- })
- .collect()
+ // https://drafts.csswg.org/selectors-4/#useraction-pseudos
+ std::iter::successors(hovered, |&node| {
+ if self.computed_group_sets.adjustment_facts(node) & element_adjustment_fact::RENDERED_IN_TOP_LAYER != 0 {
+ None
+ } else {
+ self.recorded_parent_or_shadow_host(node)
+ }
+ })
+ .filter(|&node| {
+ node.element_index().is_some()
+ && tree.host_of(node).is_none()
+ && self.recorded_parent_or_shadow_host(node).is_some()
+ })
+ .collect()
}
/// How the move of the element `row` restyles builds the element's boxes again, where `boxed` says whether it has a
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/selectors/active-toplayer-001.txt b/Tests/LibWeb/Text/expected/wpt-import/css/selectors/active-toplayer-001.txt
index c4096a7bf54..e8d4db5b050 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/selectors/active-toplayer-001.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/selectors/active-toplayer-001.txt
@@ -2,7 +2,7 @@ Harness status: OK
Found 3 tests
-3 Fail
-Fail Clicking inside a modal dialog should not propagate :active beyond the dialog.
-Fail Clicking inside a popover should not propagate :active beyond the popover.
-Fail With nested top layer elements, :active should stop at the innermost top layer boundary.
\ No newline at end of file
+3 Pass
+Pass Clicking inside a modal dialog should not propagate :active beyond the dialog.
+Pass Clicking inside a popover should not propagate :active beyond the popover.
+Pass With nested top layer elements, :active should stop at the innermost top layer boundary.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/selectors/focus-within-toplayer-001.txt b/Tests/LibWeb/Text/expected/wpt-import/css/selectors/focus-within-toplayer-001.txt
index 6cc5820a0cc..8c306391215 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/selectors/focus-within-toplayer-001.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/selectors/focus-within-toplayer-001.txt
@@ -2,7 +2,7 @@ Harness status: OK
Found 3 tests
-3 Fail
-Fail Focusing inside a modal dialog should not propagate :focus-within beyond the dialog.
-Fail Focusing inside a popover should not propagate :focus-within beyond the popover.
-Fail With nested top layer elements, :focus-within should stop at the innermost top layer boundary.
\ No newline at end of file
+3 Pass
+Pass Focusing inside a modal dialog should not propagate :focus-within beyond the dialog.
+Pass Focusing inside a popover should not propagate :focus-within beyond the popover.
+Pass With nested top layer elements, :focus-within should stop at the innermost top layer boundary.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/selectors/hover-toplayer-001.txt b/Tests/LibWeb/Text/expected/wpt-import/css/selectors/hover-toplayer-001.txt
index 2c0801588f5..550ad892aad 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/selectors/hover-toplayer-001.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/selectors/hover-toplayer-001.txt
@@ -2,7 +2,7 @@ Harness status: OK
Found 3 tests
-3 Fail
-Fail Hovering inside a modal dialog should not propagate :hover beyond the dialog.
-Fail Hovering inside a popover should not propagate :hover beyond the popover.
-Fail With nested top layer elements, :hover should stop at the innermost top layer boundary.
\ No newline at end of file
+3 Pass
+Pass Hovering inside a modal dialog should not propagate :hover beyond the dialog.
+Pass Hovering inside a popover should not propagate :hover beyond the popover.
+Pass With nested top layer elements, :hover should stop at the innermost top layer boundary.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/selectors/toplayer-transition-001.txt b/Tests/LibWeb/Text/expected/wpt-import/css/selectors/toplayer-transition-001.txt
index 3a419ca6971..96b3e01d6d7 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/selectors/toplayer-transition-001.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/selectors/toplayer-transition-001.txt
@@ -2,6 +2,6 @@ Harness status: OK
Found 2 tests
-2 Fail
-Fail :focus-within should be adjusted on ancestors when popover enters/exits top layer.
-Fail :hover should be adjusted on ancestors when popover enters/exits top layer.
\ No newline at end of file
+2 Pass
+Pass :focus-within should be adjusted on ancestors when popover enters/exits top layer.
+Pass :hover should be adjusted on ancestors when popover enters/exits top layer.
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/selectors/user-action-pseudo-top-layer.txt b/Tests/LibWeb/Text/expected/wpt-import/css/selectors/user-action-pseudo-top-layer.txt
index 66b831ba387..4cb6c7985a4 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/css/selectors/user-action-pseudo-top-layer.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/selectors/user-action-pseudo-top-layer.txt
@@ -2,10 +2,9 @@ Harness status: OK
Found 4 tests
-1 Pass
-2 Fail
+3 Pass
1 Optional Feature Unsupported
-Fail User action pseudo-classes on dialog (modal)
+Pass User action pseudo-classes on dialog (modal)
Pass User action pseudo-classes on dialog (non-modal)
-Fail User action pseudo-classes on popover (popover)
+Pass User action pseudo-classes on popover (popover)
Optional Feature Unsupported User action pseudo-classes on customizable-select (open)
\ No newline at end of file