Skip to content

Truly independent web browser

LibWeb: Restyle only readers below an animated custom property

Andreas Kling450db95d3e0a
diff --git a/Libraries/LibWeb/CSS/StyleComputer.cpp b/Libraries/LibWeb/CSS/StyleComputer.cpp
index 1fbdae3b9c4..38053160dca 100644
--- a/Libraries/LibWeb/CSS/StyleComputer.cpp
+++ b/Libraries/LibWeb/CSS/StyleComputer.cpp
@@ -575,7 +575,7 @@ void StyleComputer::collect_animations_into(Layout::BegunRead const& read, DOM::
{
if (refresh == AnimationRefresh::No) {
collect_animation_effects_into(read, abstract_element, effects, computed_properties, {});
- publish_animated_custom_properties(computed_properties, abstract_element);
+ publish_animated_custom_properties(read, computed_properties, abstract_element);
return;
}
m_keyframes_inherited_non_inherited_style_groups = 0;
@@ -610,7 +610,7 @@ void StyleComputer::refresh_animations_into_each(Layout::BegunRead const& read,
void StyleComputer::finish_animation_refresh(Layout::BegunRead const& read, DOM::AbstractElement abstract_element, ComputedStyleWorkingSet& computed_properties) const
{
- publish_animated_custom_properties(computed_properties, abstract_element);
+ publish_animated_custom_properties(read, computed_properties, abstract_element);
// An animation-only overlay update resolves keyframe values just like a full style computation does, so a
// keyframe-borne `inherit` on a non-inherited property discovered here must leave the same invalidation
// mark behind, or a later change to the parent's value never reaches this element's animated style.
@@ -852,7 +852,7 @@ void StyleComputer::finish_animation_sample(AnimationSample& sample, ComputedVal
computed_properties.finish_animated_overlay_rust_mutation(Badge<StyleComputer> {});
}
-void StyleComputer::publish_animated_custom_properties(ComputedStyleWorkingSet& computed_properties, DOM::AbstractElement abstract_element) const
+void StyleComputer::publish_animated_custom_properties(Layout::BegunRead const& read, ComputedStyleWorkingSet& computed_properties, DOM::AbstractElement abstract_element) const
{
auto data = abstract_element.custom_property_data();
RefPtr<CustomPropertyData const> base = data;
@@ -863,7 +863,7 @@ void StyleComputer::publish_animated_custom_properties(ComputedStyleWorkingSet&
if (animated_values.is_empty()) {
if (base.ptr() != data.ptr()) {
abstract_element.replace_custom_property_data(Badge<StyleComputer> {}, base);
- invalidate_animated_custom_property_readers(abstract_element, animated_values);
+ invalidate_animated_custom_property_readers(read, abstract_element, animated_values, move(data));
}
return;
}
@@ -891,10 +891,10 @@ void StyleComputer::publish_animated_custom_properties(ComputedStyleWorkingSet&
});
}
abstract_element.replace_custom_property_data(Badge<StyleComputer> {}, CustomPropertyData::create_animation_overlay(move(overlay_values), move(base), abstract_element));
- invalidate_animated_custom_property_readers(abstract_element, animated_values);
+ invalidate_animated_custom_property_readers(read, abstract_element, animated_values, move(data));
}
-void StyleComputer::invalidate_animated_custom_property_readers(DOM::AbstractElement abstract_element, OrderedHashMap<Utf16FlyString, NonnullRefPtr<StyleValue const>> const& animated_values) const
+void StyleComputer::invalidate_animated_custom_property_readers(Layout::BegunRead const& read, DOM::AbstractElement abstract_element, OrderedHashMap<Utf16FlyString, NonnullRefPtr<StyleValue const>> const& animated_values, RefPtr<CustomPropertyData const> old_data) const
{
auto& element = abstract_element.element();
// Which custom properties the element's own declarations read is not recorded: an element
@@ -912,12 +912,10 @@ void StyleComputer::invalidate_animated_custom_property_readers(DOM::AbstractEle
}
return false;
};
- if (!abstract_element.pseudo_element().has_value() && any_animated_custom_property_inherits()) {
- style_engine.record_flat_tree_descendant_style_input_changes(
- element.style_node_id(),
- StyleEngine::InheritedStyle,
- RequiredInvalidationAfterStyleChange::all_inherited_style_groups);
- }
+ // What the element hands down moved too. Its descendants take the moved environment as they take one a style
+ // reaction moved: only those that read a name whose value moved compute again.
+ if (!abstract_element.pseudo_element().has_value() && any_animated_custom_property_inherits())
+ move_custom_property_environment_below_animated_element(read, element, move(old_data));
}
// The host's form of one animation definition a style computation decided.
diff --git a/Libraries/LibWeb/CSS/StyleComputer.h b/Libraries/LibWeb/CSS/StyleComputer.h
index 64cec83f73f..a57b5cde769 100644
--- a/Libraries/LibWeb/CSS/StyleComputer.h
+++ b/Libraries/LibWeb/CSS/StyleComputer.h
@@ -191,8 +191,8 @@ private:
[[nodiscard]] NonnullOwnPtr<AnimationSample> begin_animation_sample(Layout::BegunRead const& read, DOM::AbstractElement, ReadonlySpan<GC::Ref<Animations::KeyframeEffect>>, ComputedStyleWorkingSet&, StyleRecordID sampled_style_record) const;
void finish_animation_sample(AnimationSample&, ComputedValuesFFI::FfiHostAnimationSampleResult const&) const;
void finish_animation_refresh(Layout::BegunRead const& read, DOM::AbstractElement, ComputedStyleWorkingSet&) const;
- void publish_animated_custom_properties(ComputedStyleWorkingSet&, DOM::AbstractElement) const;
- void invalidate_animated_custom_property_readers(DOM::AbstractElement, OrderedHashMap<Utf16FlyString, NonnullRefPtr<StyleValue const>> const& animated_values) const;
+ void publish_animated_custom_properties(Layout::BegunRead const&, ComputedStyleWorkingSet&, DOM::AbstractElement) const;
+ void invalidate_animated_custom_property_readers(Layout::BegunRead const&, DOM::AbstractElement, OrderedHashMap<Utf16FlyString, NonnullRefPtr<StyleValue const>> const& animated_values, RefPtr<CustomPropertyData const> old_data) const;
RefPtr<ComputedStyleWorkingSet> start_needed_transitions(Layout::BegunRead const& read, DOM::AbstractElement, StyleRecordID before_change_style_record) const;
[[nodiscard]] bool has_provisional_transition_states(DOM::AbstractElement) const;
void finalize_animated_box_type(Layout::BegunRead const& read, ComputedStyleWorkingSet&, DOM::AbstractElement) const;
@@ -336,4 +336,8 @@ private:
StyleRecordID m_style_record;
};
+// The custom properties an element's animations sample moved from `old_data`: its descendants take what it hands down
+// now, as they take an environment that a style reaction moved.
+void move_custom_property_environment_below_animated_element(Layout::BegunRead const&, DOM::Element&, RefPtr<CustomPropertyData const> old_data);
+
}
diff --git a/Libraries/LibWeb/CSS/UpdateStyle.cpp b/Libraries/LibWeb/CSS/UpdateStyle.cpp
index 095e848cdee..a9417cbb703 100644
--- a/Libraries/LibWeb/CSS/UpdateStyle.cpp
+++ b/Libraries/LibWeb/CSS/UpdateStyle.cpp
@@ -450,6 +450,12 @@ static void propagate_custom_property_environment_move(StyleReactionApplication&
move_custom_property_environment_below(application, origin, old_origin_data, origin.custom_property_data({}));
}
+void move_custom_property_environment_below_animated_element(Layout::BegunRead const& read, DOM::Element& element, RefPtr<CustomPropertyData const> old_data)
+{
+ StyleReactionApplication application { read, element.document() };
+ propagate_custom_property_environment_move(application, element, move(old_data));
+}
+
// Install the record the engine settled for a row, and the pseudo-element records beside it. A C++ computation applied
// the animation plan it decided beside the record it computed and collected the element's animations, those the plan
// starts among them, into that record; the host composes them over the record the engine settled once it is installed,
diff --git a/Libraries/LibWeb/Rust/src/css/custom_properties.rs b/Libraries/LibWeb/Rust/src/css/custom_properties.rs
index ec8a070a774..7f978b8eabf 100644
--- a/Libraries/LibWeb/Rust/src/css/custom_properties.rs
+++ b/Libraries/LibWeb/Rust/src/css/custom_properties.rs
@@ -606,6 +606,11 @@ impl CustomPropertyStore {
self.parent.as_ref()
}
+ /// The store this one was built over, whichever of its values it took in.
+ pub(crate) fn inheritance_parent(&self) -> Option<&Arc<CustomPropertyStore>> {
+ self.inheritance_parent.as_ref()
+ }
+
pub(crate) fn get(&self, name_raw: usize) -> Option<&CustomPropertyEntry> {
self.own_values
.get(&name_raw)
diff --git a/Libraries/LibWeb/Rust/src/css/style/environment_move.rs b/Libraries/LibWeb/Rust/src/css/style/environment_move.rs
index 8f9372b92c9..bb109e191e5 100644
--- a/Libraries/LibWeb/Rust/src/css/style/environment_move.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/environment_move.rs
@@ -240,8 +240,13 @@ impl StyleEngine {
let holds_inherited_environment = existing == old_parent_inheritable
|| (!declares && !self.retained.node_declares_custom_properties(element));
if !holds_inherited_environment {
- // Its declared values stand while it reads nothing that changed.
- if existing == 0 || !declares || self.environment_move_needs_recompute(element, moved, changed_names) {
+ // Its declared values stand while it reads nothing that changed, over what its parent handed it before
+ // the move. One built over anything else missed an earlier move, which only a computation catches up on.
+ if existing == 0
+ || !declares
+ || !self.environment_is_built_over(existing, old_parent_inheritable)
+ || self.environment_move_needs_recompute(element, moved, changed_names)
+ {
act(EnvironmentMoveAction::Recompute(element));
} else {
act(EnvironmentMoveAction::Rebuild(element));
@@ -315,6 +320,19 @@ impl StyleEngine {
Some(existing)
}
+ /// Whether the environment `identity` names was built over the one `parent` names.
+ fn environment_is_built_over(&self, identity: u64, parent: u64) -> bool {
+ let environments = &self.retained.custom_property_environments;
+ let Some(store) = environments.store(identity) else {
+ return false;
+ };
+ // SAFETY: The store is live while the identity naming it is.
+ let built_over = unsafe { &*store.cast::<CustomPropertyStore>() }
+ .inheritance_parent()
+ .map_or(std::ptr::null(), |parent| Arc::as_ptr(parent).cast());
+ built_over == environments.store(parent).unwrap_or(std::ptr::null())
+ }
+
/// Whether a moved environment computes the element again rather than handing it the moved
/// one: its style reads the environment other than through `var()`, or reads a changed custom property.
fn environment_move_needs_recompute(
@@ -478,12 +496,26 @@ mod tests {
}
}
+ /// Give `identity` a store of no values built over the one `parent` names, or over none.
+ fn note_store_built_over(engine: &mut StyleEngine, identity: u64, parent: u64) {
+ let environments = &mut engine.retained.custom_property_environments;
+ let parent = environments.store(parent).unwrap_or(std::ptr::null());
+ // SAFETY: The parent is null or a live store, and the environments retain the new one.
+ unsafe {
+ let store = CustomPropertyStore::cascaded_child(parent, Vec::new());
+ environments.retain(identity, store);
+ drop(Arc::from_raw(store.cast::<CustomPropertyStore>()));
+ }
+ }
+
#[test]
fn a_move_stops_at_declarations_readers_overlays_and_unstyled_elements() {
let (mut engine, nodes) = linear_document();
discard_transaction(&mut engine);
settle_cascades(&mut engine, &nodes);
// An element declaring its own over another environment is rebuilt over the moved one.
+ note_store_built_over(&mut engine, 1, 0);
+ note_store_built_over(&mut engine, 3, 1);
held_record(&mut engine, nodes[1]);
engine.set_element_custom_property_data(nodes[1], Some(data(0x1000)), 3, None, true);
// An element whose style reads the environment through if() computes again.
@@ -502,6 +534,22 @@ mod tests {
);
}
+ #[test]
+ fn a_move_recomputes_a_declaration_that_missed_an_earlier_move() {
+ let (mut engine, nodes) = linear_document();
+ discard_transaction(&mut engine);
+ settle_cascades(&mut engine, &nodes);
+ // The element's environment was built over one its parent no longer hands down, so its
+ // record may be older than the move: rebuilding it would publish that record as current.
+ note_store_built_over(&mut engine, 1, 0);
+ note_store_built_over(&mut engine, 4, 0);
+ note_store_built_over(&mut engine, 3, 4);
+ held_record(&mut engine, nodes[1]);
+ engine.set_element_custom_property_data(nodes[1], Some(data(0x1000)), 3, None, true);
+ let actions = walk(&mut engine, nodes[0], &moved(1, 2));
+ assert_eq!(actions, [EnvironmentMoveAction::Recompute(nodes[1])]);
+ }
+
#[test]
fn a_move_republishes_the_record_the_host_holds_beneath_a_newer_assignment() {
let (mut engine, nodes) = linear_document();
diff --git a/Tests/LibWeb/Text/expected/css/style-invalidation/animated-custom-property-stops-where-descendants-reset-it.txt b/Tests/LibWeb/Text/expected/css/style-invalidation/animated-custom-property-stops-where-descendants-reset-it.txt
index eb3d5db4299..0893031a615 100644
--- a/Tests/LibWeb/Text/expected/css/style-invalidation/animated-custom-property-stops-where-descendants-reset-it.txt
+++ b/Tests/LibWeb/Text/expected/css/style-invalidation/animated-custom-property-stops-where-descendants-reset-it.txt
@@ -2,6 +2,8 @@ reader in an element a slot takes: 90deg
deep reader below non-readers: 90deg
deep reader after a step: 180deg
reader below the reset: 0deg
-restyle stops at the reset: false
+restyle stops at the reset: true
reader declaring its own below a registered declaration: 90deg
that reader after a step: 180deg
+readers when the element's declarations move too: 90deg, 90deg
+those readers after a step: 180deg, 180deg
diff --git a/Tests/LibWeb/Text/input/css/style-invalidation/animated-custom-property-stops-where-descendants-reset-it.html b/Tests/LibWeb/Text/input/css/style-invalidation/animated-custom-property-stops-where-descendants-reset-it.html
index c59a6e5f46e..7cce1979195 100644
--- a/Tests/LibWeb/Text/input/css/style-invalidation/animated-custom-property-stops-where-descendants-reset-it.html
+++ b/Tests/LibWeb/Text/input/css/style-invalidation/animated-custom-property-stops-where-descendants-reset-it.html
@@ -27,14 +27,18 @@
#declaring {
--registered: 5deg;
}
- #declaring-reader {
+ #declaring-reader, #moving-declaring-reader {
--own: 1px;
}
+ #moving {
+ --declared: 1px;
+ }
</style>
<div id="reading" class="source"><div><div><div class="reader" id="deep-reader"></div></div></div></div>
<div id="resetting" class="source"><div id="reset-child"></div></div>
<div id="host"><div><span class="reader" id="slotted-reader"></span></div></div>
<div id="declaring" class="source"><div class="reader" id="declaring-reader"></div></div>
+<div id="moving" class="source"><div class="reader" id="moving-reader"></div><div class="reader" id="moving-declaring-reader"></div></div>
<script>
test(() => {
const shadow = document.getElementById("host").attachShadow({ mode: "open" });
@@ -86,5 +90,15 @@
println(`reader declaring its own below a registered declaration: ${getComputedStyle(declaringReader).rotate}`);
animationOf("declaring").currentTime = 500;
println(`that reader after a step: ${getComputedStyle(declaringReader).rotate}`);
+
+ // A change to the element's own declarations in the same update moves what it hands down once more.
+ const moving = document.getElementById("moving");
+ const rotateOf = id => getComputedStyle(document.getElementById(id)).rotate;
+ animationOf("moving").currentTime = 250;
+ moving.style.setProperty("--declared", "2px");
+ println(`readers when the element's declarations move too: ${rotateOf("moving-reader")}, ${rotateOf("moving-declaring-reader")}`);
+ animationOf("moving").currentTime = 500;
+ moving.style.setProperty("--declared", "3px");
+ println(`those readers after a step: ${rotateOf("moving-reader")}, ${rotateOf("moving-declaring-reader")}`);
});
</script>