Skip to content

Truly independent web browser

LibWeb: Read the custom properties of an element from its record

Andreas Klingb62f8bf2b247
diff --git a/Libraries/LibWeb/CSS/UpdateStyle.cpp b/Libraries/LibWeb/CSS/UpdateStyle.cpp
index 6c9329cf46d..c8957fa392a 100644
--- a/Libraries/LibWeb/CSS/UpdateStyle.cpp
+++ b/Libraries/LibWeb/CSS/UpdateStyle.cpp
@@ -371,7 +371,7 @@ void StyleEngineFFI::web_css_install_styles(StyleReactionApplication* applicatio
InstalledStyle const old_style { element_effects.old_record };
swap_in_styles_until(element_effects.swaps_end);
- if (!element || !element->has_style() || main_effects == CSS::MainEffects::None)
+ if (!element || !element->has_style())
continue;
element->note_subtree_style_moved();
auto invalidation = decode_style_invalidation(element_effects.damage);
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp
index 84e1c9bfb46..7e38d580046 100644
--- a/Libraries/LibWeb/DOM/Element.cpp
+++ b/Libraries/LibWeb/DOM/Element.cpp
@@ -5509,8 +5509,8 @@ void Element::replace_custom_property_data(Optional<CSS::PseudoElement> pseudo_e
install_custom_property_data(read, pseudo_element, move(data), is_animation_overlay);
}
-// The style engine keeps the custom-property environments an element and its synthetic pseudo-elements hold; the
-// element keeps no copy of its own. This is the only place they move.
+// An element and its synthetic pseudo-elements hold the custom-property environment of their records. The style engine
+// keeps the ones lying over that; the element keeps no copy of its own. This is the only place they move.
void Element::install_custom_property_data(Layout::BegunRead const& read, Optional<CSS::PseudoElement> pseudo_element, RefPtr<CSS::CustomPropertyData const> data, CSS::IsAnimationOverlay is_animation_overlay)
{
VERIFY(data || is_animation_overlay == CSS::IsAnimationOverlay::No);
@@ -5522,6 +5522,9 @@ void Element::install_custom_property_data(Layout::BegunRead const& read, Option
VERIFY(style_node != 0 || !data);
if (style_node == 0)
return;
+ // NB: What the record holds already lies over nothing.
+ if (is_animation_overlay == CSS::IsAnimationOverlay::No && data && data->identity() == installed_style(pseudo_element).custom_property_environment())
+ data = nullptr;
auto& style_engine = document().style_computer().style_engine();
if (!pseudo_element.has_value()) {
style_engine.set_element_custom_property_data(read, *this, data.ptr(), is_animation_overlay);
@@ -5570,9 +5573,13 @@ RefPtr<CSS::CustomPropertyData const> Element::custom_property_data(Optional<CSS
if (style_node == 0)
return nullptr;
auto const& style_engine = document().style_computer().style_engine();
- if (!pseudo_element.has_value())
- return static_cast<CSS::CustomPropertyData const*>(CSS::StyleEngineFFI::style_engine_element_custom_property_data(style_engine.host(), style_node.value()));
- return static_cast<CSS::CustomPropertyData const*>(CSS::StyleEngineFFI::style_engine_pseudo_element_custom_property_data(style_engine.host(), style_node.value(), to_underlying(pseudo_element.value())));
+ auto const* lying_over = static_cast<CSS::CustomPropertyData const*>(pseudo_element.has_value()
+ ? CSS::StyleEngineFFI::style_engine_pseudo_element_custom_property_data(style_engine.host(), style_node.value(), to_underlying(pseudo_element.value()))
+ : CSS::StyleEngineFFI::style_engine_element_custom_property_data(style_engine.host(), style_node.value()));
+ if (lying_over)
+ return lying_over;
+ // NB: The identity of an environment is the address of its store, which the record holds a reference to.
+ return reinterpret_cast<CSS::CustomPropertyData const*>(static_cast<FlatPtr>(installed_style(pseudo_element).custom_property_environment()));
}
if (auto existing_pseudo_element = get_pseudo_element(pseudo_element.value()); existing_pseudo_element.has_value())
diff --git a/Libraries/LibWeb/Rust/src/css/style/computed.rs b/Libraries/LibWeb/Rust/src/css/style/computed.rs
index cd3e5fdd28d..f77d9d3fa2d 100644
--- a/Libraries/LibWeb/Rust/src/css/style/computed.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/computed.rs
@@ -1626,6 +1626,15 @@ impl ComputedGroupSets {
Some(self.installed.get(installed)?.custom_property_environment())
}
+ /// Each element holding an installed record, with the custom-property environment that names.
+ pub(super) fn installed_custom_property_environments(&self) -> impl Iterator<Item = (StyleNodeID, u64)> + '_ {
+ (0..self.installed_column.len()).filter_map(|index| {
+ let installed = self.installed_column.get(index)?.as_ref()?;
+ let node = StyleNodeID::element(u32::try_from(index).ok()?);
+ Some((node, installed.record.custom_property_environment()))
+ })
+ }
+
/// Every raw custom-property environment identity a live record was installed with.
pub fn live_custom_property_environments(&self) -> impl Iterator<Item = u64> + '_ {
self.installed.iter().map(InstalledRecord::custom_property_environment)
diff --git a/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs b/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs
index 6a910ff2a11..a16f0f3fc80 100644
--- a/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/engine_calls.rs
@@ -1257,11 +1257,20 @@ pub extern "C" fn style_engine_for_each_element_custom_property_environment(
// The engine keeps each environment alive while an element holds it, and nothing moves one on before the
// callbacks have run.
let held: Vec<(u32, *const c_void)> = with_engine(read, host, |engine| {
- engine
- .retained
- .element_custom_property_data
+ let retained = &engine.retained;
+ let lying_over = &retained.element_custom_property_data;
+ let mut named = super::fast_hash::FastSet::default();
+ lying_over
.iter()
.map(|(node, held)| (node.raw(), held.data.data()))
+ .chain(
+ retained
+ .computed_group_sets
+ .installed_custom_property_environments()
+ .filter(|&(node, environment)| environment != 0 && !lying_over.contains_key(&node))
+ .filter(|&(_, environment)| named.insert(environment))
+ .map(|(node, environment)| (node.raw(), std::ptr::with_exposed_provenance(environment as usize))),
+ )
.collect()
});
for (node, data) in held {
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/main_effects.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/main_effects.rs
index feff681bf17..a5bf35eb15d 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/main_effects.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/main_effects.rs
@@ -338,9 +338,14 @@ impl RetainedState {
MainEffects::VISIBILITY_FLIP,
old_values.is_some_and(|old| old.visibility() != new_values.visibility()),
)
+ // NB: An element's environment is the store its record holds. The host has something to do only where
+ // another lies over that: what animations sample, on the element or a parent it inherits them from.
.with(
MainEffects::CUSTOM_PROPERTIES,
- custom_properties || facts.pseudo_elements.environment_moved,
+ (custom_properties || facts.pseudo_elements.environment_moved)
+ && (facts.inherits_sampled_custom_properties
+ || self.element_custom_property_data.contains_key(&node)
+ || self.pseudo_element_custom_property_data.contains_key(&node)),
)
.with(
MainEffects::ANCHOR_NAMES,