Skip to content

Truly independent web browser

LibWeb: Keep a custom-property environment that resolves the same

Andreas Kling41e395d4aed3
diff --git a/Libraries/LibWeb/Rust/src/css/custom_properties.rs b/Libraries/LibWeb/Rust/src/css/custom_properties.rs
index 3e5634b96ea..ec8a070a774 100644
--- a/Libraries/LibWeb/Rust/src/css/custom_properties.rs
+++ b/Libraries/LibWeb/Rust/src/css/custom_properties.rs
@@ -629,10 +629,10 @@ impl CustomPropertyStore {
/// What a child inherits of this store: the store without the entries a registration keeps
/// from inheriting, over the same chain; `None` when every entry inherits.
pub(crate) fn inheritable(&self, registry: &CustomPropertyRegistry) -> Option<Arc<CustomPropertyStore>> {
- let inherits = |entry: &CustomPropertyEntry| registry.name_inherits(&entry.name);
- if self.own_values.values().all(inherits) {
+ if self.hands_down_every_value(registry) {
return None;
}
+ let inherits = |entry: &CustomPropertyEntry| registry.name_inherits(&entry.name);
let own_values: HashMap<usize, CustomPropertyEntry> = self
.own_values
.iter()
@@ -657,6 +657,33 @@ impl CustomPropertyStore {
}))
}
+ /// Whether the store holds the same values as `other`, built over the same store and chained to
+ /// the same parent.
+ pub(crate) fn holds_same_values_as(&self, other: &Self) -> bool {
+ let same = |first: &Option<Arc<Self>>, second: &Option<Arc<Self>>| match (first, second) {
+ (None, None) => true,
+ (Some(first), Some(second)) => Arc::ptr_eq(first, second),
+ _ => false,
+ };
+ same(&self.parent, &other.parent)
+ && same(&self.inheritance_parent, &other.inheritance_parent)
+ && self.declared_names == other.declared_names
+ && self.own_values.len() == other.own_values.len()
+ && self.own_values.iter().all(|(name_raw, entry)| {
+ other.own_values.get(name_raw).is_some_and(|other_entry| {
+ other_entry.important == entry.important
+ && crate::css::style_value::retained_style_values_equal(&other_entry.value, &entry.value)
+ })
+ })
+ }
+
+ /// Whether a child inherits every entry of this store.
+ pub(crate) fn hands_down_every_value(&self, registry: &CustomPropertyRegistry) -> bool {
+ self.own_values
+ .values()
+ .all(|entry| registry.name_inherits(&entry.name))
+ }
+
pub(crate) fn value_matches(&self, name_raw: usize, value: &StyleValueData) -> bool {
self.get(name_raw).is_some_and(|entry| entry.value.data() == value)
}
diff --git a/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs b/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs
index 479b921dbf0..544c517a619 100644
--- a/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/custom_property_cascade.rs
@@ -1847,6 +1847,12 @@ impl RetainedState {
}
let identity = if rust_store.is_null() {
inherited_environment
+ } else if let Some(previous) =
+ self.environment_resolving_to_the_same_values(node, pseudo, rust_store, registry_ref)
+ {
+ // SAFETY: The resolution handed over one strong reference to the store, which nothing else holds.
+ drop(unsafe { Arc::from_raw(rust_store.cast::<CustomPropertyStore>()) });
+ previous
} else {
unsafe {
self.custom_property_environments.adopt_engine_environment(
@@ -1870,6 +1876,40 @@ impl RetainedState {
Ok(identity)
}
+ /// The environment the element's declarations resolved to last, beneath any animation overlay, where they resolve to
+ /// its values again in `resolved`: an environment resolved anew, as one holding a registered value is, would move
+ /// what the element's descendants inherit without moving any value they see. What an environment hands down was
+ /// decided under the registrations it was resolved under, so only one handing down every value, then and now, is
+ /// kept.
+ fn environment_resolving_to_the_same_values(
+ &self,
+ node: StyleNodeID,
+ pseudo: Option<u8>,
+ resolved: *const c_void,
+ registry: &crate::css::custom_properties::CustomPropertyRegistry,
+ ) -> Option<u64> {
+ if pseudo.is_some() {
+ return None;
+ }
+ let held = self.element_custom_property_data.get(&node)?;
+ let previous = held.sampled_over.unwrap_or(held.identity);
+ if previous & custom_property_environments::ENGINE_ENVIRONMENT_IDENTITY_BIT == 0
+ || self.custom_property_environments.inheritable(previous) != previous
+ {
+ return None;
+ }
+ let previous_store = self.custom_property_environments.store(previous)?;
+ // SAFETY: Both stores are live: the previous one while the environment naming it is, the resolved one for
+ // the call.
+ let (previous_store, resolved) = unsafe {
+ (
+ &*previous_store.cast::<CustomPropertyStore>(),
+ &*resolved.cast::<CustomPropertyStore>(),
+ )
+ };
+ (resolved.hands_down_every_value(registry) && resolved.holds_same_values_as(previous_store)).then_some(previous)
+ }
+
/// What a node's records read beyond their cascade, as `FfiNodeRecordReads` bits, for the row
/// that installs them: an `attr()`, `inherit()`, `if()` or custom function call in the
/// element's winners, in its pseudo-elements' (which read the originating element's