Skip to content

Truly independent web browser

LibWeb: Track the styles a hidden slot clears from its slottables

sideshowbarker8dfa21ce963a
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/install.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/install.rs
index 2f25d45a594..5510f0a7758 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/install.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/install.rs
@@ -793,22 +793,22 @@ impl RetainedState {
}
}
- /// Whether a descendant of `node` in the shadow-including tree that holds a style is an SVG element content can
- /// reference, which keeps its style while hidden. An element without a style holds none below it.
+ /// Whether a descendant of `node` in the flat tree that holds a style is an SVG element content can reference,
+ /// which keeps its style while hidden. An element without a style holds none below it.
fn has_referenceable_svg_descendant(&self, node: StyleNodeID) -> bool {
let mut found = false;
- self.for_each_held_shadow_including_descendant(node, |state, element| {
+ self.for_each_held_flat_tree_descendant(node, |state, element| {
found = found || state.is_referenceable_in_display_none_subtree(element);
!found
});
found
}
- /// Notes that the host cleared the styles of `node`'s descendants that hold one, in the shadow-including tree, as
- /// it does for the descendants of a hidden element.
+ /// Notes that the host cleared the styles of `node`'s descendants that hold one, in the flat tree, as it does for
+ /// the descendants of a hidden element.
pub(crate) fn clear_hidden_descendants(&mut self, node: StyleNodeID) {
let mut cleared = Vec::new();
- self.for_each_held_shadow_including_descendant(node, |_, element| {
+ self.for_each_held_flat_tree_descendant(node, |_, element| {
cleared.push(element);
true
});
@@ -817,9 +817,10 @@ impl RetainedState {
}
}
- /// Calls `callback` with each descendant of `node` in the shadow-including tree that holds a style, in no
- /// particular order, skipping the descendants of those that hold none, until it answers false.
- fn for_each_held_shadow_including_descendant(
+ /// Calls `callback` with each descendant of `node` in the flat tree that holds a style, in no particular order,
+ /// skipping the descendants of those that hold none, until it answers false. As in the walk with which the host
+ /// clears the styles, a slottable is below the slot it is assigned to, and not reached from its host.
+ fn for_each_held_flat_tree_descendant(
&self,
node: StyleNodeID,
mut callback: impl FnMut(&Self, StyleNodeID) -> bool,
@@ -830,9 +831,18 @@ impl RetainedState {
pending.extend(projection.shadow_root_of(parent));
let mut child = projection.first_element_child(parent);
while let Some(current) = child {
- pending.push(current);
+ if projection.assigned_slot_of(current).is_none() {
+ pending.push(current);
+ }
child = projection.next_element_sibling(current);
}
+ pending.extend(
+ projection
+ .assigned_nodes_of(parent)
+ .iter()
+ .copied()
+ .filter(|assigned| assigned.text_index().is_none()),
+ );
};
push_children(&mut pending, node);
while let Some(current) = pending.pop() {
diff --git a/Tests/LibWeb/Text/expected/css/style-engine/slotted-subtree-revealed-with-its-slot-keeps-style.txt b/Tests/LibWeb/Text/expected/css/style-engine/slotted-subtree-revealed-with-its-slot-keeps-style.txt
new file mode 100644
index 00000000000..2fe289091eb
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/style-engine/slotted-subtree-revealed-with-its-slot-keeps-style.txt
@@ -0,0 +1,3 @@
+offsetParent: body
+offsetTop: true
+slotted display: block
diff --git a/Tests/LibWeb/Text/input/css/style-engine/slotted-subtree-revealed-with-its-slot-keeps-style.html b/Tests/LibWeb/Text/input/css/style-engine/slotted-subtree-revealed-with-its-slot-keeps-style.html
new file mode 100644
index 00000000000..8b03a353964
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/style-engine/slotted-subtree-revealed-with-its-slot-keeps-style.html
@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+<script src="../../include.js"></script>
+<x-host id="host"><div id="slotted"><div id="target">x</div></div></x-host>
+<script>
+ test(() => {
+ const root = host.attachShadow({ mode: "open" });
+ root.innerHTML = "<slot></slot>";
+ const slot = root.firstElementChild;
+ void host.offsetHeight;
+
+ // Hiding the slot withdraws the styles of the elements assigned to it, which then get them back as it's shown again.
+ slot.style.display = "none";
+ void host.offsetHeight;
+ slot.style.display = "";
+
+ println(`offsetParent: ${target.offsetParent.localName}`);
+ println(`offsetTop: ${target.offsetTop === slotted.offsetTop}`);
+ println(`slotted display: ${getComputedStyle(slotted).display}`);
+ });
+</script>