Skip to content

Truly independent web browser

LibWeb: Keep shadow hosts' pseudo-element matches across transactions

Andreas Klingf484ccb787ac
diff --git a/Libraries/LibWeb/Rust/src/css/style/flush.rs b/Libraries/LibWeb/Rust/src/css/style/flush.rs
index c4964f41dcd..dc8353bf273 100644
--- a/Libraries/LibWeb/Rust/src/css/style/flush.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/flush.rs
@@ -298,7 +298,21 @@ impl RetainedState {
None => self.batch_custom_property_answers.remove(&node),
};
}
- if let Some(matches) = self.batch_backing_pseudo_matches_of(node, answers, answer) {
+ let backing_pseudo_matches = self.batch_backing_pseudo_matches_of(node, answers, answer);
+ if self.tree.shadow_root_of(node).is_some() {
+ match &backing_pseudo_matches {
+ Some(matches) => {
+ self.shadow_host_backing_pseudo_matches
+ .insert(node, (self.program.version(), matches.clone()));
+ }
+ None => {
+ self.shadow_host_backing_pseudo_matches.remove(&node);
+ }
+ }
+ }
+ if let Some(matches) = backing_pseudo_matches
+ && !matches.is_empty()
+ {
self.batch_backing_pseudo_matches.insert(node, matches);
}
if let Some(complete) = self.answer_is_complete_but_for_custom_properties(node, answers, answer) {
@@ -534,6 +548,11 @@ impl StyleEngine {
| InputKey::CustomPropertyRegistration(_)
)
});
+ // A shadow host's matches may move with any input that reaches a selector, and only the transactions that
+ // publish the host's answer note them again.
+ if !transaction_reaches_no_selector {
+ self.retained.shadow_host_backing_pseudo_matches.clear();
+ }
let match_identity_is_complete_output = !transaction.has_coarsened_markers()
&& transaction.inputs.iter().all(|input| {
!matches!(
diff --git a/Libraries/LibWeb/Rust/src/css/style/inputs.rs b/Libraries/LibWeb/Rust/src/css/style/inputs.rs
index 9eda747bc22..29c9884560f 100644
--- a/Libraries/LibWeb/Rust/src/css/style/inputs.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/inputs.rs
@@ -2031,6 +2031,7 @@ impl StyleEngine {
batch_custom_property_matches: HashMap::default(),
batch_custom_property_answers: HashMap::default(),
batch_backing_pseudo_matches: HashMap::default(),
+ shadow_host_backing_pseudo_matches: HashMap::default(),
engine_cold_record_cache: Default::default(),
engine_cold_record_donors: Default::default(),
@@ -3410,6 +3411,8 @@ impl RetainedState {
batch_custom_property_matches: _,
batch_custom_property_answers: _,
batch_backing_pseudo_matches: _,
+ // Forgotten for departed elements in `forget_departed_elements`.
+ shadow_host_backing_pseudo_matches: _,
engine_cold_record_cache: _,
engine_cold_record_donors: _,
style_invalidation_cache: _,
diff --git a/Libraries/LibWeb/Rust/src/css/style/mod.rs b/Libraries/LibWeb/Rust/src/css/style/mod.rs
index 3afd16f2152..08757449c71 100644
--- a/Libraries/LibWeb/Rust/src/css/style/mod.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/mod.rs
@@ -963,6 +963,10 @@ pub struct RetainedState {
/// Beside them, each published host's matches for the element-backed pseudo-elements, which
/// the elements backing them cascade from while the host's answer is not installed.
batch_backing_pseudo_matches: HashMap<StyleNodeID, Vec<RuleMatch>>,
+ /// The same matches of each shadow host, as its last published answer held them under the program version beside
+ /// them. A host's answer is never retained, and the elements backing its pseudo-elements are styled in the
+ /// transactions after the host's, which cascade from these rather than matching the host again.
+ shadow_host_backing_pseudo_matches: HashMap<StyleNodeID, (ProgramVersion, Vec<RuleMatch>)>,
engine_cold_record_cache: crate::fork::ForkShared<HashMap<publication::ColdRecordKey, publication::ColdRecord>>,
engine_cold_record_donors:
crate::fork::ForkShared<HashMap<publication::ColdRecordDonorKey, Vec<publication::ColdRecordDonor>>>,
diff --git a/Libraries/LibWeb/Rust/src/css/style/ordering.rs b/Libraries/LibWeb/Rust/src/css/style/ordering.rs
index 1098f6853ba..2e8807a48cd 100644
--- a/Libraries/LibWeb/Rust/src/css/style/ordering.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/ordering.rs
@@ -2249,6 +2249,7 @@ impl StyleEngine {
}
for node in departed {
self.retained.facts.forget(node);
+ self.retained.shadow_host_backing_pseudo_matches.remove(&node);
self.retained
.retained_match_answers
.forget(&mut self.retained.match_answers, node);
diff --git a/Libraries/LibWeb/Rust/src/css/style/publication/pseudo.rs b/Libraries/LibWeb/Rust/src/css/style/publication/pseudo.rs
index 0954572d2fc..e92a6cd3a8c 100644
--- a/Libraries/LibWeb/Rust/src/css/style/publication/pseudo.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/publication/pseudo.rs
@@ -810,6 +810,11 @@ impl RetainedState {
if published.is_some() {
return published;
}
+ if let Some((version, matches)) = self.shadow_host_backing_pseudo_matches.get(&host)
+ && *version == self.program.version()
+ {
+ return Some(for_target(matches));
+ }
match self.retained_match_answer(host) {
Lookup::Known(answer) => answer
.iter()
@@ -826,7 +831,7 @@ impl RetainedState {
}
/// The matches for element-backed pseudo-elements in the answer a transaction publishes for a
- /// node, read before that answer is installed. `None` when it has none.
+ /// node, read before that answer is installed. `None` when the answer does not hold its matches.
pub(in crate::css::style) fn batch_backing_pseudo_matches_of(
&self,
node: StyleNodeID,
@@ -856,7 +861,7 @@ impl RetainedState {
.map(|entry| entry.materialize(node, &self.programs, 0))
.collect::<Option<_>>()?,
};
- (!matches.is_empty()).then_some(matches)
+ Some(matches)
}
/// The record of an element standing for its shadow host's pseudo-element, the way C++