Skip to content

Truly independent web browser

LibWeb: Test pseudo-classes after a pseudo-element on its element

Andreas Kling9839db77f40b
diff --git a/Libraries/LibWeb/Rust/src/css/style/compiler.rs b/Libraries/LibWeb/Rust/src/css/style/compiler.rs
index 9295da75395..97d32a64248 100644
--- a/Libraries/LibWeb/Rust/src/css/style/compiler.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/compiler.rs
@@ -518,6 +518,18 @@ impl<'a, A: AtomSpace> SelectorCompiler<'a, A> {
})
}
+ /// The kind of the element-backed pseudo-element this compound names, when it also writes
+ /// something after it.
+ fn backed_pseudo_element_with_pseudo_classes(compound: &CompoundSelector) -> Option<u16> {
+ let Some(SimpleSelector::PseudoElement(pseudo_element)) = compound.simple_selectors.first() else {
+ return None;
+ };
+ let kind = pseudo_element.pseudo_element as u16;
+ let backed_kinds = u16::from(super::bridge::FIRST_ELEMENT_REFERENCE_PSEUDO_ELEMENT_KIND)
+ ..=u16::from(super::bridge::LAST_ELEMENT_REFERENCE_PSEUDO_ELEMENT_KIND);
+ (backed_kinds.contains(&kind) && compound.simple_selectors.len() > 1).then_some(kind)
+ }
+
fn names_slotted(compound: &CompoundSelector) -> bool {
compound.simple_selectors.iter().any(|simple| {
matches!(simple, SimpleSelector::PseudoElement(pseudo_element)
@@ -584,6 +596,25 @@ impl<'a, A: AtomSpace> SelectorCompiler<'a, A> {
return self.builder.push_compound(&[compound, step]);
}
+ // `X::file-selector-button:hover` is the same shape again: the pseudo-classes written after
+ // an element-backed pseudo-element describe the element in `X`'s shadow tree that backs it,
+ // not `X`. Conjoining them onto `X` would match `:hover` wherever `X` is hovered, including
+ // over the rest of its shadow tree, so they become a step to the backing element.
+ if (index == 0 || compounds[index].combinator == Combinator::PseudoElement)
+ && let Some(kind) = Self::backed_pseudo_element_with_pseudo_classes(&compounds[index])
+ {
+ let inner = self.compile_compound(&compounds[index], marker);
+ let step = self.builder.push(SelectorOp::BackingElement { kind, inner });
+ let host = match index {
+ 0 => leftmost,
+ _ => Some(self.compile_chain_anchored(compounds, index - 1, marker, leftmost)),
+ };
+ return match host {
+ Some(host) => self.builder.push_compound(&[host, step]),
+ None => step,
+ };
+ }
+
let compound = self.compile_compound(&compounds[index], marker);
if index == 0 {
return match leftmost {
diff --git a/Libraries/LibWeb/Rust/src/css/style/impact.rs b/Libraries/LibWeb/Rust/src/css/style/impact.rs
index f3c07955453..a25bd046d88 100644
--- a/Libraries/LibWeb/Rust/src/css/style/impact.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/impact.rs
@@ -193,6 +193,12 @@ impl ImpactRegion {
None => Self::Empty,
},
+ // An element backing a pseudo-element reaches its shadow host, and only its host.
+ (Self::Node(node), InverseStep::ShadowHost) => match tree.shadow_host_of(node) {
+ Some(host) => Self::Node(host),
+ None => Self::Empty,
+ },
+
// A slotted node reaches its slot, and only its slot.
(Self::Node(node), InverseStep::SlotAssignment) => match tree.assigned_slot_of(node) {
Some(slot) => Self::Node(slot),
@@ -219,6 +225,7 @@ impl ImpactRegion {
_,
InverseStep::HostedTree
| InverseStep::HostedTrees
+ | InverseStep::ShadowHost
| InverseStep::SlotAssignment
| InverseStep::SlotAssignees,
) => Self::Document,
diff --git a/Libraries/LibWeb/Rust/src/css/style/inputs.rs b/Libraries/LibWeb/Rust/src/css/style/inputs.rs
index 698db6e0b91..a85756412d0 100644
--- a/Libraries/LibWeb/Rust/src/css/style/inputs.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/inputs.rs
@@ -1353,6 +1353,8 @@ impl RetainedState {
pub fn set_element_associated_pseudo_kind(&mut self, node: StyleNodeID, pseudo_kind_plus_one: u8) {
self.computed_group_sets
.set_associated_pseudo_kind(node, pseudo_kind_plus_one);
+ self.tree
+ .set_backing_pseudo_kind(node, pseudo_kind_plus_one.checked_sub(1).map(u16::from));
if pseudo_kind_plus_one != 0
&& let Some(host) = self.tree.shadow_host_of(node)
{
diff --git a/Libraries/LibWeb/Rust/src/css/style/selector.rs b/Libraries/LibWeb/Rust/src/css/style/selector.rs
index 1e28c1806ad..d3a3e47962a 100644
--- a/Libraries/LibWeb/Rust/src/css/style/selector.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/selector.rs
@@ -382,6 +382,14 @@ pub enum SelectorOp {
host: SelectorNodeID,
parts: SelectorNodeID,
},
+ /// `X::file-selector-button:hover` from the host: the element of the host's shadow tree that
+ /// stands for its element-backed pseudo-element of `kind` satisfies `inner`, the pseudo-classes
+ /// written after the pseudo-element. Those describe the pseudo-element rather than the host,
+ /// so they are a step to the element backing it, not conjuncts on the host.
+ BackingElement {
+ kind: u16,
+ inner: SelectorNodeID,
+ },
/// `:root`: the subject is the root of its tree.
Root,
/// `:empty`: the subject has no element or significant text children.
@@ -513,7 +521,7 @@ impl SelectorOp {
// Shadow operators are a lookup in a sparse relation: cheap, but only meaningful once
// the local compound has already accepted the candidate.
Self::Part(_) => 5,
- Self::ExposedToHost { .. } => 7,
+ Self::ExposedToHost { .. } | Self::BackingElement { .. } => 7,
Self::Root | Self::Empty | Self::Scope | Self::ScopeRootInstance => 5,
Self::IsNode(_) | Self::RelativeAnchorInstance => 0,
Self::AssignedSlot(_) => 7,
@@ -855,7 +863,8 @@ impl<A: AtomSpace> SelectorProgram<A> {
| SelectorOp::Parent(inner)
| SelectorOp::Ancestor(inner)
| SelectorOp::PreviousSibling(inner)
- | SelectorOp::PrecedingSibling(inner) => self.subtree_tests_position(inner),
+ | SelectorOp::PrecedingSibling(inner)
+ | SelectorOp::BackingElement { inner, .. } => self.subtree_tests_position(inner),
SelectorOp::RelativeExists(query) => self.subtree_tests_position(self.relative_query(query).compound),
_ => false,
}
@@ -1290,6 +1299,7 @@ impl<A: AtomSpace> SelectorProgram<A> {
| SelectorOp::Slotted(_)
| SelectorOp::Part(_)
| SelectorOp::ExposedToHost { .. }
+ | SelectorOp::BackingElement { .. }
| SelectorOp::Empty
| SelectorOp::Scope
| SelectorOp::IsNode(_)
@@ -2064,6 +2074,7 @@ impl<A: AtomSpace> SelectorProgram<A> {
| SelectorOp::PreviousSibling(_)
| SelectorOp::PrecedingSibling(_)
| SelectorOp::AssignedSlot(_)
+ | SelectorOp::BackingElement { .. }
| SelectorOp::RelativeExists(_) => false,
}
}
@@ -2113,7 +2124,8 @@ impl<A: AtomSpace> SelectorProgram<A> {
| SelectorOp::PrecedingSibling(inner)
| SelectorOp::Host(inner)
| SelectorOp::Slotted(inner)
- | SelectorOp::AssignedSlot(inner) => self.entry_is_monotone_under_arrivals(inner),
+ | SelectorOp::AssignedSlot(inner)
+ | SelectorOp::BackingElement { inner, .. } => self.entry_is_monotone_under_arrivals(inner),
SelectorOp::ExposedToHost { host, parts } => {
self.entry_is_monotone_under_arrivals(host) && self.entry_is_monotone_under_arrivals(parts)
}
@@ -2169,6 +2181,7 @@ impl<A: AtomSpace> SelectorProgram<A> {
| SelectorOp::Slotted(_)
| SelectorOp::Part(_)
| SelectorOp::ExposedToHost { .. }
+ | SelectorOp::BackingElement { .. }
| SelectorOp::Empty
| SelectorOp::Scope
| SelectorOp::AssignedSlot(_)
@@ -2194,7 +2207,8 @@ impl<A: AtomSpace> SelectorProgram<A> {
| SelectorOp::Ancestor(inner)
| SelectorOp::Host(inner)
| SelectorOp::Slotted(inner)
- | SelectorOp::AssignedSlot(inner) => self.entry_observes_sibling_relation(inner),
+ | SelectorOp::AssignedSlot(inner)
+ | SelectorOp::BackingElement { inner, .. } => self.entry_observes_sibling_relation(inner),
SelectorOp::ExposedToHost { host, parts } => {
self.entry_observes_sibling_relation(host) || self.entry_observes_sibling_relation(parts)
}
@@ -2238,6 +2252,9 @@ impl<A: AtomSpace> SelectorProgram<A> {
// The step lands on the slot, which is inside the scope; what stands outside it is the
// subject, and the `::slotted()` beside this says so.
SelectorOp::AssignedSlot(_) => false,
+ // The element backing a pseudo-element stands in the shadow tree its host hosts, which
+ // is a tree of its own.
+ SelectorOp::BackingElement { .. } => true,
// An implicit scoping root is the parent of its sheet's owner node, which for a sheet
// that is a direct child of a shadow root is the host - outside the tree. Which element
// it is is not a property of the program, so this is the widest of the two answers and
@@ -2304,6 +2321,7 @@ impl<A: AtomSpace> SelectorProgram<A> {
| SelectorOp::RelativeExists(_)
| SelectorOp::NthPosition(_)
| SelectorOp::AssignedSlot(_)
+ | SelectorOp::BackingElement { .. }
| SelectorOp::Language { .. }
| SelectorOp::ScopeRootInstance
| SelectorOp::RelativeAnchorInstance
@@ -2446,7 +2464,8 @@ impl<A: AtomSpace> SelectorProgram<A> {
| SelectorOp::Parent(inner)
| SelectorOp::Ancestor(inner)
| SelectorOp::Host(inner)
- | SelectorOp::Slotted(inner) => self.count_sibling_steps(inner),
+ | SelectorOp::Slotted(inner)
+ | SelectorOp::BackingElement { inner, .. } => self.count_sibling_steps(inner),
SelectorOp::ExposedToHost { host, parts } => {
self.count_sibling_steps(host).max(self.count_sibling_steps(parts))
}
@@ -3161,6 +3180,8 @@ pub enum InverseStep {
/// `::part()` rule names can sit several shadow roots below the host its outer compound
/// describes - past boundaries `HostedTree` stops at.
HostedTrees,
+ /// From an element backing a pseudo-element to the shadow host it stands for it on.
+ ShadowHost,
/// From a slotted node to the slot it is assigned to, and thence into the shadow tree.
SlotAssignment,
/// From a slot to the light-DOM elements it assigns.
@@ -3930,6 +3951,11 @@ impl<A: AtomSpace> SelectorProgram<A> {
self.walk_step(host, enclosing, InverseStep::HostedTrees, anchor, walk, visit);
self.walk_transpose(parts, enclosing, anchor, walk, visit);
}
+ // A change on the element backing a pseudo-element reaches the host it stands for it on,
+ // whose match carries the pseudo-element's rules.
+ SelectorOp::BackingElement { inner, .. } => {
+ self.walk_step(inner, enclosing, InverseStep::ShadowHost, anchor, walk, visit);
+ }
// Root, emptiness, scope membership and the place of a named element all change with
// the shape of the tree.
SelectorOp::Root
@@ -5924,6 +5950,11 @@ impl<'a> EngineTree<'a> {
impl SelectorTree for EngineTree<'_> {
type Node = StyleNodeID;
+ #[inline]
+ fn backs_pseudo_element(self, node: StyleNodeID, kind: u16) -> bool {
+ self.tree.backing_pseudo_kind_of(node) == Some(kind)
+ }
+
#[inline]
fn parent(self, node: StyleNodeID) -> Option<StyleNodeID> {
self.transaction_fact_view.map_or_else(
diff --git a/Libraries/LibWeb/Rust/src/css/style/selector_evaluation.rs b/Libraries/LibWeb/Rust/src/css/style/selector_evaluation.rs
index 7515aab8343..b68713504bd 100644
--- a/Libraries/LibWeb/Rust/src/css/style/selector_evaluation.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/selector_evaluation.rs
@@ -136,6 +136,24 @@ pub(crate) trait SelectorTree: Copy {
fn shadow_root_of(self, host: Self::Node) -> Option<Self::Node>;
fn host_of(self, shadow_root: Self::Node) -> Option<Self::Node>;
+ /// Whether the element stands for its shadow host's element-backed pseudo-element of `kind`.
+ fn backs_pseudo_element(self, _node: Self::Node, _kind: u16) -> bool {
+ false
+ }
+
+ /// The element of `host`'s shadow tree that stands for its element-backed pseudo-element of `kind`.
+ fn backing_element_of(self, host: Self::Node, kind: u16) -> Option<Self::Node> {
+ let shadow_root = self.shadow_root_of(host)?;
+ let mut node = self.next_in_subtree(shadow_root, shadow_root);
+ while let Some(current) = node {
+ if self.backs_pseudo_element(current, kind) {
+ return Some(current);
+ }
+ node = self.next_in_subtree(current, shadow_root);
+ }
+ None
+ }
+
/// The element after `node` in tree order that is a descendant of `root`, where `node` is `root` itself to start.
fn next_in_subtree(self, node: Self::Node, root: Self::Node) -> Option<Self::Node> {
if let Some(child) = self.first_child(node) {
@@ -1039,6 +1057,12 @@ impl<S: SelectorSubject> SelectorEvaluator<S> {
}
Ok(false)
}
+ // The pseudo-classes written after an element-backed pseudo-element describe the element
+ // backing it, which stands in the host's shadow tree.
+ SelectorOp::BackingElement { kind, inner } => match tree.backing_element_of(node, kind) {
+ Some(backing) => self.matches_node(program, inner, backing, counters),
+ None => Ok(false),
+ },
SelectorOp::Root => {
counters.bump(Counter::StructuralTests);
Ok(self.subject.is_root(node))
diff --git a/Libraries/LibWeb/Rust/src/css/style/tree.rs b/Libraries/LibWeb/Rust/src/css/style/tree.rs
index 807dd80b47c..1b87817d4fe 100644
--- a/Libraries/LibWeb/Rust/src/css/style/tree.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/tree.rs
@@ -478,6 +478,8 @@ struct ShadowRelations {
/// when one level exposes the name it writes and the host of that same level is the element its
/// outer compound describes.
part_hosts: HashMap<StyleNodeID, Vec<(StyleAtomID, StyleNodeID)>>,
+ /// The element-backed pseudo-element kind an element of a shadow tree stands for on its host.
+ backing_pseudo_kinds: HashMap<StyleNodeID, u16>,
}
impl ShadowRelations {
@@ -505,6 +507,7 @@ impl ShadowRelations {
self.shadow_root.remove(host);
}
self.part_hosts.remove(&node);
+ self.backing_pseudo_kinds.remove(&node);
}
fn retire_text(&mut self, node: StyleNodeID) {
@@ -1639,6 +1642,28 @@ impl StyleNodeTree {
}
}
+ pub fn set_backing_pseudo_kind(&mut self, node: StyleNodeID, kind: Option<u16>) {
+ match kind {
+ Some(kind) => {
+ self.shadow_mut().backing_pseudo_kinds.insert(node, kind);
+ }
+ None => {
+ if let Some(shadow) = self.shadow.as_mut() {
+ shadow.backing_pseudo_kinds.remove(&node);
+ }
+ }
+ }
+ }
+
+ /// The element-backed pseudo-element kind the element stands for on its shadow host.
+ #[must_use]
+ pub fn backing_pseudo_kind_of(&self, node: StyleNodeID) -> Option<u16> {
+ self.shadow
+ .as_ref()
+ .and_then(|shadow| shadow.backing_pseudo_kinds.get(&node))
+ .copied()
+ }
+
/// Every (name, host) pair the element answers a `::part()` rule under.
#[must_use]
pub fn part_hosts_of(&self, node: StyleNodeID) -> &[(StyleAtomID, StyleNodeID)] {
diff --git a/Tests/LibWeb/Text/expected/file-selector-button-states.txt b/Tests/LibWeb/Text/expected/file-selector-button-states.txt
index 87cf964be6f..0bb5dfece9b 100644
--- a/Tests/LibWeb/Text/expected/file-selector-button-states.txt
+++ b/Tests/LibWeb/Text/expected/file-selector-button-states.txt
@@ -1,6 +1,8 @@
idle: rgb(239, 239, 239)
label hovered: rgb(239, 239, 239)
+label pressed: rgb(239, 239, 239)
hovered: rgb(191, 191, 191)
+moved to label: rgb(239, 239, 239)
pressed: color(srgb 0.702941 0.702941 0.702941)
dragged outside: rgb(239, 239, 239)
released outside: rgb(239, 239, 239)
diff --git a/Tests/LibWeb/Text/input/file-selector-button-states.html b/Tests/LibWeb/Text/input/file-selector-button-states.html
index 0e71bd602f2..ff6a1247069 100644
--- a/Tests/LibWeb/Text/input/file-selector-button-states.html
+++ b/Tests/LibWeb/Text/input/file-selector-button-states.html
@@ -20,8 +20,14 @@
report("idle");
internals.mouseMove(labelX, buttonY);
report("label hovered");
+ internals.mouseDown(labelX, buttonY);
+ report("label pressed");
+ internals.mouseUp(labelX, buttonY);
internals.mouseMove(buttonX, buttonY);
report("hovered");
+ internals.mouseMove(labelX, buttonY);
+ report("moved to label");
+ internals.mouseMove(buttonX, buttonY);
internals.mouseDown(buttonX, buttonY);
report("pressed");
internals.mouseMove(outsideX, buttonY, 0, 1);