Skip to content

Truly independent web browser

LibWeb: Hold a pseudo-element’s style record until it’s replaced

sideshowbarker06de9c59e47b
diff --git a/Libraries/LibWeb/CSS/StyleEngineBridge.h b/Libraries/LibWeb/CSS/StyleEngineBridge.h
index ca62abf22e1..b6b23bdc564 100644
--- a/Libraries/LibWeb/CSS/StyleEngineBridge.h
+++ b/Libraries/LibWeb/CSS/StyleEngineBridge.h
@@ -97,6 +97,7 @@ void style_engine_evaluate_size_containers_needing_evaluation_after_layout(Docum
void style_engine_set_element_associated_pseudo_kind(DocumentHost const*, StyleNodeID node, u8 pseudo_kind_plus_one);
void style_engine_set_counter_style_environment_identity(DocumentHost const*, TreeScopeID tree_scope, u64 identity);
void style_engine_set_held_style_record(DocumentHost const*, StyleNodeID node, u64 style_record);
+void style_engine_set_held_pseudo_style_record(DocumentHost const*, StyleNodeID node, u8 pseudo_kind, u64 style_record);
void style_engine_set_element_css_defined_animations(DocumentHost const*, StyleNodeID node, u8 slot, ReadonlySpan<FlatPtr> names, ReadonlySpan<FfiAppliedAnimationDefinition> definitions);
void style_engine_set_tree_scope_root(DocumentHost const*, TreeScopeID tree_scope, StyleNodeID root);
void style_engine_set_sheet_conditions_hold(DocumentHost const*, SheetID sheet, bool conditions_hold);
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp
index 8254ef67cfd..824adff30ef 100644
--- a/Libraries/LibWeb/DOM/Element.cpp
+++ b/Libraries/LibWeb/DOM/Element.cpp
@@ -3331,9 +3331,15 @@ void Element::set_style_node_id(CSS::StyleNodeID style_node_id)
pseudo_element.publish_scroll_offset();
});
}
- // A newly minted identity holds none of what the element held under its previous one.
- if (style_node_id != 0 && !!m_installed_style.record())
- CSS::StyleEngineFFI::style_engine_set_held_style_record(style_engine.host(), style_node_id, m_installed_style.record().value());
+ // A newly minted identity holds none of what the element and its pseudo-elements held under its previous one.
+ if (style_node_id != 0) {
+ if (!!m_installed_style.record())
+ CSS::StyleEngineFFI::style_engine_set_held_style_record(style_engine.host(), style_node_id, m_installed_style.record().value());
+ for_each_synthetic_pseudo_element([&](CSS::PseudoElement type, SyntheticPseudoElement const& pseudo_element) {
+ if (auto style_record = pseudo_element.style_record_identity(); !!style_record)
+ CSS::StyleEngineFFI::style_engine_set_held_pseudo_style_record(style_engine.host(), style_node_id, to_underlying(type), style_record.value());
+ });
+ }
if (style_node_id != 0)
publish_children_explicitly_inherit_mark();
}
diff --git a/Libraries/LibWeb/DOM/PseudoElement.cpp b/Libraries/LibWeb/DOM/PseudoElement.cpp
index 73278096d3c..3821666eb77 100644
--- a/Libraries/LibWeb/DOM/PseudoElement.cpp
+++ b/Libraries/LibWeb/DOM/PseudoElement.cpp
@@ -86,11 +86,14 @@ void SyntheticPseudoElement::replace_style_record(CSS::StyleRecordID style_recor
Layout::ForcedReadScope read { m_originating_element->document() };
if (m_installed_style.record() == style_record_identity)
return;
- m_installed_style = m_originating_element->document().style_computer().install_style(read, style_record_identity);
- // Only an element holds the record it installed before in the engine, so a pseudo-element's layout node reads the
- // one it moves from itself.
+ auto& style_computer = m_originating_element->document().style_computer();
+ auto held_before = exchange(m_installed_style, style_computer.install_style(read, style_record_identity));
+ // The pseudo-element holds the record it installed before until the engine takes the new one in, so the layout node
+ // reads what it moves from before the pseudo-element lets go of it.
if (auto* layout_node = unsafe_layout_node(read))
- layout_node->set_style_record_identity(m_installed_style, {});
+ layout_node->set_style_record_identity(m_installed_style, held_before);
+ if (auto style_node_id = m_originating_element->style_node_id(); style_node_id != 0)
+ CSS::StyleEngineFFI::style_engine_set_held_pseudo_style_record(style_computer.style_engine().host(), style_node_id, to_underlying(type()), style_record_identity.value());
}
void SyntheticPseudoElement::set_computed_style(CSS::StyleRecordID style_record_identity)
@@ -112,6 +115,8 @@ void SyntheticPseudoElement::clear_computed_style(RefPtr<CSS::ComputedValues con
else
layout_node->pin_style_record_for_detachment();
}
+ if (auto style_node_id = m_originating_element->style_node_id(); style_node_id != 0 && !!m_installed_style.record())
+ CSS::StyleEngineFFI::style_engine_set_held_pseudo_style_record(m_originating_element->document().style_computer().style_engine().host(), style_node_id, to_underlying(type()), 0);
}
m_installed_style = {};
}
diff --git a/Libraries/LibWeb/Rust/src/css/style/boundary.rs b/Libraries/LibWeb/Rust/src/css/style/boundary.rs
index 19c4a667424..1701d01f871 100644
--- a/Libraries/LibWeb/Rust/src/css/style/boundary.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/boundary.rs
@@ -320,6 +320,7 @@ style_boundary! {
tree_scope: TreeScopeID, identity: u64
};
set_held_style_record => SetHeldStyleRecord { node: StyleNodeID, style_record: u64 };
+ set_held_pseudo_style_record => SetHeldPseudoStyleRecord { node: StyleNodeID, pseudo_kind: u8, style_record: u64 };
set_element_css_defined_animations => SetElementCssDefinedAnimations {
node: StyleNodeID, slot: u8, names: Box<[CssString]>, definitions: Box<[FfiAppliedAnimationDefinition]>
} => engine.set_element_css_defined_animations(node, slot, names, &definitions);
@@ -362,6 +363,7 @@ impl StyleChange {
Self::BeginStyleRecordViewEpoch
| Self::EndStyleRecordViewEpoch
| Self::PinStyleRecord { .. }
+ | Self::SetHeldPseudoStyleRecord { .. }
| Self::UnpinStyleRecord { .. }
)
}
diff --git a/Libraries/LibWeb/Rust/src/css/style/inputs.rs b/Libraries/LibWeb/Rust/src/css/style/inputs.rs
index 6868d36f1de..1f395c76e9d 100644
--- a/Libraries/LibWeb/Rust/src/css/style/inputs.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/inputs.rs
@@ -1435,6 +1435,35 @@ impl RetainedState {
}
}
+ /// Record the style record the pseudo-element of `pseudo_kind` of an element holds; zero is
+ /// none. Keep it alive until the host replaces it, as an element's, even when the engine
+ /// publishes a newer assignment.
+ pub fn set_held_pseudo_style_record(&mut self, node: StyleNodeID, pseudo_kind: u8, style_record: u64) {
+ let held = self.held_pseudo_style_records.entry(node).or_default();
+ let slot = held.iter().position(|&(kind, _)| kind == pseudo_kind);
+ let previous = match slot {
+ Some(slot) if style_record == 0 => Some(held.swap_remove(slot).1),
+ Some(slot) => Some(std::mem::replace(&mut held[slot].1, style_record)),
+ None if style_record == 0 => None,
+ None => {
+ held.push((pseudo_kind, style_record));
+ None
+ }
+ };
+ if held.is_empty() {
+ self.held_pseudo_style_records.remove(&node);
+ }
+ if previous == Some(style_record) {
+ return;
+ }
+ if style_record != 0 {
+ self.computed_group_sets.pin_style_record(style_record);
+ }
+ if let Some(previous) = previous {
+ self.computed_group_sets.unpin_style_record(previous);
+ }
+ }
+
/// Record that a child of an element or shadow root explicitly inherits a non-inherited
/// property, as the host marks the node. The mark lasts as long as the node's identity.
pub fn note_children_explicitly_inherit(&mut self, node: StyleNodeID, groups: u32) {
@@ -1987,6 +2016,7 @@ impl StyleEngine {
size_container_queries: Default::default(),
counter_style_environment_identities: HashMap::default(),
held_style_records: Default::default(),
+ held_pseudo_style_records: Default::default(),
tick_shown: Default::default(),
backing_elements: HashMap::default(),
children_explicitly_inherit_marks: HashMap::default(),
@@ -3384,6 +3414,7 @@ impl RetainedState {
size_container_queries,
counter_style_environment_identities: _,
held_style_records,
+ held_pseudo_style_records,
// Empty but while a clock frame runs, which no retirement reaches.
tick_shown: _,
backing_elements,
@@ -3505,6 +3536,9 @@ impl RetainedState {
if let Some(style_record) = held_style_records.remove(&node) {
computed_group_sets.unpin_style_record(style_record);
}
+ for (_, style_record) in held_pseudo_style_records.remove(&node).into_iter().flatten() {
+ computed_group_sets.unpin_style_record(style_record);
+ }
children_explicitly_inherit_marks.remove(&node);
css_defined_animations.retire(node);
animation_effect_descriptions.retire(node);
diff --git a/Libraries/LibWeb/Rust/src/css/style/mod.rs b/Libraries/LibWeb/Rust/src/css/style/mod.rs
index c0cd6c3fa99..98fd7644f1e 100644
--- a/Libraries/LibWeb/Rust/src/css/style/mod.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/mod.rs
@@ -905,6 +905,9 @@ pub struct RetainedState {
/// The style record each element holds, for the elements that hold one, as the host reports
/// every record it installs or clears.
held_style_records: crate::fork::ForkShared<HashMap<StyleNodeID, u64>>,
+ /// The style record each pseudo-element holds, by its kind, for the elements whose pseudo-elements
+ /// hold one, as the host reports every record it installs or clears.
+ held_pseudo_style_records: crate::fork::ForkShared<HashMap<StyleNodeID, Vec<(u8, u64)>>>,
/// The records a clock frame shows in place of the ones the host installed, which it lends the published reads
/// for as long as it runs.
tick_shown: engine_sample::TickShownRecords,
diff --git a/Libraries/LibWeb/Rust/src/css/style/tests.rs b/Libraries/LibWeb/Rust/src/css/style/tests.rs
index e84c5107d16..58d40ed3cc1 100644
--- a/Libraries/LibWeb/Rust/src/css/style/tests.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/tests.rs
@@ -12030,6 +12030,63 @@ fn held_style_records_outlive_engine_assignments_and_release_on_retirement() {
assert!(!engine.computed_group_sets.final_style_record_is_live(held_record));
}
+#[test]
+fn held_pseudo_style_records_outlive_engine_assignments_and_release_on_retirement() {
+ use super::publication::pseudo_kind::{AFTER, BEFORE};
+ let mut engine = StyleEngine::new();
+ let mut raw_node = [0];
+ engine.allocate_style_nodes(&mut raw_node);
+ let node = StyleNodeID::from_raw(raw_node[0]).unwrap();
+ let publish = |engine: &mut StyleEngine, pseudo_kind, pseudo_element_styles| {
+ engine
+ .publish_computed_groups(
+ computed::ComputedStyleTarget::new(node, pseudo_kind),
+ &[],
+ 0,
+ 0,
+ computed::ComputedMetadataInput {
+ pseudo_element_styles,
+ dependency_flags: 0,
+ counter_style_environment_identity: 0,
+ animation_overlay_identity: 0,
+ animated_overlay: HostShared::null(),
+ animation_overlay_payloads: &[],
+ longhand_table: HostShared::null(),
+ },
+ )
+ .style_record_identity
+ .raw()
+ };
+ let held_record = publish(&mut engine, BEFORE, 1);
+ engine.set_held_pseudo_style_record(node, BEFORE, held_record);
+ engine.set_held_pseudo_style_record(node, BEFORE, held_record);
+
+ // The engine can publish a replacement before the host installs it.
+ let replacement = publish(&mut engine, BEFORE, 2);
+ engine.computed_group_sets.reclaim_unreachable();
+ assert!(engine.computed_group_sets.final_style_record_is_live(held_record));
+
+ // Installing the replacement lets go of the record it replaced, however often the host reported it.
+ engine.set_held_pseudo_style_record(node, BEFORE, replacement);
+ engine.computed_group_sets.reclaim_unreachable();
+ assert!(!engine.computed_group_sets.final_style_record_is_live(held_record));
+ assert!(engine.computed_group_sets.final_style_record_is_live(replacement));
+
+ // Clearing one pseudo-element's record must not release another's.
+ let after_record = publish(&mut engine, AFTER, 3);
+ engine.set_held_pseudo_style_record(node, AFTER, after_record);
+ engine.set_held_pseudo_style_record(node, BEFORE, 0);
+ engine.computed_group_sets.remove_pseudo(node, BEFORE);
+ engine.computed_group_sets.remove_pseudo(node, AFTER);
+ engine.computed_group_sets.reclaim_unreachable();
+ assert!(!engine.computed_group_sets.final_style_record_is_live(replacement));
+ assert!(engine.computed_group_sets.final_style_record_is_live(after_record));
+
+ engine.retire_node_state(node);
+ engine.computed_group_sets.reclaim_unreachable();
+ assert!(!engine.computed_group_sets.final_style_record_is_live(after_record));
+}
+
#[test]
fn records_left_unreachable_under_a_lease_are_reclaimed_once_it_drops() {
let mut engine = StyleEngine::new();