Skip to content

Truly independent web browser

LibWeb: Key pseudo-element records under a display:none element

Andreas Klingd7bb68352c28
diff --git a/Libraries/LibWeb/Rust/src/css/style/publication.rs b/Libraries/LibWeb/Rust/src/css/style/publication.rs
index 53a780b0a50..07c1f4a35e3 100644
--- a/Libraries/LibWeb/Rust/src/css/style/publication.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/publication.rs
@@ -2756,7 +2756,9 @@ impl RetainedState {
let dependency_flags = view.dependency_flags;
let environment = self.inherited_custom_property_environment(parent)?;
let inherited_groups = self.computed_group_sets.node_inherited_groups_identity(parent)?;
- let parent_display = self.box_type_parent_display(parent)?;
+ // C++ styles the children of a display:none element on demand, past the engine's view of
+ // the parent, so no first record is computed under one.
+ let parent_display = self.box_type_parent_display(parent, UnderDisplayNone::NoKey)?;
let record = if self.state_explicitly_inherits_non_inherited_property(node, state) {
parent_record.raw()
} else {
@@ -2772,17 +2774,16 @@ impl RetainedState {
}
/// The display the box-type transformation reads as the parent's for a child of the parent:
- /// the parent's own, past any display:contents ancestor, packed into one word.
- fn box_type_parent_display(&self, parent: StyleNodeID) -> Option<u32> {
+ /// the parent's own, past any display:contents ancestor, packed into one word. Under a
+ /// display:none parent, `display_none` says whether there is one.
+ fn box_type_parent_display(&self, parent: StyleNodeID, display_none: UnderDisplayNone) -> Option<u32> {
let mut ancestor = Some(parent);
while let Some(current) = ancestor {
let record = self.computed_group_sets.assigned_style_record(current)?;
let view = self.computed_group_sets.style_record_view(record.raw())?;
let table = view.longhand_table?;
let display = crate::css::style_compute::effective_display(table, None);
- // C++ styles the children of a display:none element on demand, past the engine's
- // view of the parent, so no first record is computed under one.
- if display.is_none() {
+ if display.is_none() && display_none == UnderDisplayNone::NoKey {
return None;
}
if !display.is_contents() {
@@ -4434,6 +4435,13 @@ pub(super) struct SiblingPosition {
pub(super) index: u32,
}
+/// Whether a record derived under a display:none parent is keyed by the parent's display.
+#[derive(Clone, Copy, PartialEq, Eq)]
+pub(super) enum UnderDisplayNone {
+ NoKey,
+ Keyed,
+}
+
/// What a first record was derived from: the parent's side of the computation, the winner state
/// (with the generation its identity belongs to), the element facts, the pseudo-elements the
/// element has rules for, and the font environment.
diff --git a/Libraries/LibWeb/Rust/src/css/style/publication/pseudo.rs b/Libraries/LibWeb/Rust/src/css/style/publication/pseudo.rs
index bc5c3d303bb..2fe1e858b13 100644
--- a/Libraries/LibWeb/Rust/src/css/style/publication/pseudo.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/publication/pseudo.rs
@@ -441,7 +441,9 @@ impl RetainedState {
let key = self
.computed_group_sets
.node_inherited_groups_identity(node)
- .zip(self.box_type_parent_display(node))
+ // NB: The pseudo-elements of a display:none element are derived here like any others, so the
+ // element's display keys them as well.
+ .zip(self.box_type_parent_display(node, UnderDisplayNone::Keyed))
.filter(|_| !element_alone && !has_registered_declarations)
.map(|(inherited_groups, parent_display)| PseudoCohortKey {
monospace_recascaded_font_size: state.map_or(0, |state| self.monospace_cohort_key(target, state)),
diff --git a/Tests/LibWeb/Text/expected/css/pseudo-element-style-under-display-none.txt b/Tests/LibWeb/Text/expected/css/pseudo-element-style-under-display-none.txt
new file mode 100644
index 00000000000..f5d93f9f890
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/pseudo-element-style-under-display-none.txt
@@ -0,0 +1,24 @@
+hidden
+ 0: "x" rgb(255, 0, 0) rgb(255, 0, 0) 1px inline-block
+ 1: "x" rgb(0, 128, 0) rgb(0, 128, 0) 1px inline-block
+ 2: "x" rgb(255, 0, 0) rgb(255, 0, 0) 3px inline-block
+ 3: "x" rgb(255, 0, 0) rgb(255, 0, 0) 1px inline-block
+ 4: "x" rgb(0, 128, 0) rgb(0, 128, 0) 1px inline-block
+hidden, colors swapped
+ 0: "x" rgb(255, 0, 0) rgb(255, 0, 0) 1px inline-block
+ 1: "x" rgb(255, 0, 0) rgb(255, 0, 0) 1px inline-block
+ 2: "x" rgb(255, 0, 0) rgb(255, 0, 0) 3px inline-block
+ 3: "x" rgb(0, 128, 0) rgb(0, 128, 0) 1px inline-block
+ 4: "x" rgb(0, 128, 0) rgb(0, 128, 0) 1px inline-block
+first shown, last wide
+ 0: "x" rgb(255, 0, 0) rgb(255, 0, 0) 1px inline-block
+ 1: "x" rgb(255, 0, 0) rgb(255, 0, 0) 1px inline-block
+ 2: "x" rgb(255, 0, 0) rgb(255, 0, 0) 3px inline-block
+ 3: "x" rgb(0, 128, 0) rgb(0, 128, 0) 1px inline-block
+ 4: "x" rgb(0, 128, 0) rgb(0, 128, 0) 3px inline-block
+all shown
+ 0: "x" rgb(255, 0, 0) rgb(255, 0, 0) 1px inline-block
+ 1: "x" rgb(255, 0, 0) rgb(255, 0, 0) 1px inline-block
+ 2: "x" rgb(255, 0, 0) rgb(255, 0, 0) 3px inline-block
+ 3: "x" rgb(0, 128, 0) rgb(0, 128, 0) 1px inline-block
+ 4: "x" rgb(0, 128, 0) rgb(0, 128, 0) 3px inline-block
diff --git a/Tests/LibWeb/Text/input/css/pseudo-element-style-under-display-none.html b/Tests/LibWeb/Text/input/css/pseudo-element-style-under-display-none.html
new file mode 100644
index 00000000000..eac44ab874e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/pseudo-element-style-under-display-none.html
@@ -0,0 +1,42 @@
+<!DOCTYPE html>
+<style>
+ .item { display: none; }
+ .item.shown { display: block; }
+ .red { color: rgb(255, 0, 0); }
+ .green { color: rgb(0, 128, 0); }
+ .item::after { content: "x"; border-left: 1px solid currentcolor; display: inline-block; }
+ .item.wide::after { border-left-width: 3px; }
+</style>
+<div id="list">
+ <div class="item red"></div>
+ <div class="item green"></div>
+ <div class="item red wide"></div>
+ <div class="item red"></div>
+ <div class="item green"></div>
+</div>
+<script src="../include.js"></script>
+<script>
+test(() => {
+ const items = [...document.querySelectorAll(".item")];
+ const describe = label => {
+ println(label);
+ for (const [index, item] of items.entries()) {
+ const style = getComputedStyle(item, "::after");
+ println(` ${index}: ${style.content} ${style.color} ${style.borderLeftColor} ${style.borderLeftWidth} ${style.display}`);
+ }
+ };
+ describe("hidden");
+
+ items[1].classList.replace("green", "red");
+ items[3].classList.replace("red", "green");
+ describe("hidden, colors swapped");
+
+ items[0].classList.add("shown");
+ items[4].classList.add("wide");
+ describe("first shown, last wide");
+
+ for (const item of items)
+ item.classList.add("shown");
+ describe("all shown");
+});
+</script>