Skip to content

Truly independent web browser

LibWeb: Forget a freed element as the target of its inert animations

sideshowbarkerdb0de4451515
diff --git a/Libraries/LibWeb/Animations/Animatable.cpp b/Libraries/LibWeb/Animations/Animatable.cpp
index 28f54c960be..5a51574469f 100644
--- a/Libraries/LibWeb/Animations/Animatable.cpp
+++ b/Libraries/LibWeb/Animations/Animatable.cpp
@@ -80,7 +80,7 @@ Animatable::~Animatable()
void Animatable::finalize_animations()
{
if (auto* animations = exchange(m_animations, nullptr))
- RustFFI::animations_element_animations_destroy(animations);
+ RustFFI::animations_element_animations_destroy(animations, as_if<DOM::Element>(*this));
}
RustFFI::ElementAnimations& Animatable::ensure_animations()
diff --git a/Libraries/LibWeb/Rust/src/animations/element_animations.rs b/Libraries/LibWeb/Rust/src/animations/element_animations.rs
index 2f2e24bdad7..fefd020eade 100644
--- a/Libraries/LibWeb/Rust/src/animations/element_animations.rs
+++ b/Libraries/LibWeb/Rust/src/animations/element_animations.rs
@@ -9,7 +9,9 @@
//! publish what the style engine reads of them to the engine the host names as it changes them.
//! https://www.w3.org/TR/web-animations-1/#the-animatable-interface-mixin
-use super::animation::{AnimationCell, AnimationHandle, AnimationRecord, HostCellKind, SmilHolder};
+use super::animation::{
+ AnimationCell, AnimationHandle, AnimationRecord, HostCellKind, SmilHolder, WeakAnimationHandle,
+};
use super::composite_order::{TreeOrder, composite_order};
use crate::css::retained_fly_string::RetainedUtf16FlyString;
use crate::css::style::animations::CssDefinedAnimation;
@@ -82,6 +84,9 @@ pub struct ElementAnimations {
associated: SmallVec<[AnimationHandle; 1]>,
/// The lists of the owners that own any CSS animation or transition.
owners: SmallVec<[OwnedAnimations; 1]>,
+ /// The animations disassociated from the element, which may still name it as their effect target: An animation
+ /// that turns inert leaves `associated`, but its effect keeps targeting the element until it is revived.
+ disassociated: SmallVec<[WeakAnimationHandle; 1]>,
/// Whether `associated` is in composite order.
is_sorted_by_composite_order: bool,
/// Whether any owner has had a CSS animation since the element last canceled them all.
@@ -138,6 +143,8 @@ impl ElementAnimations {
if self.associated.iter().any(|associated| associated.ptr_eq(&animation)) {
return false;
}
+ self.disassociated
+ .retain(|disassociated| disassociated.upgrade().is_some_and(|other| !other.ptr_eq(&animation)));
animation.note_smil_holder(SmilHolder::TargetList, true);
self.associated.push(animation);
self.is_sorted_by_composite_order = false;
@@ -150,12 +157,31 @@ impl ElementAnimations {
let Some(index) = self.associated.iter().position(|associated| associated.is(animation)) else {
return false;
};
- self.associated
- .remove(index)
- .note_smil_holder(SmilHolder::TargetList, false);
+ let animation = self.associated.remove(index);
+ animation.note_smil_holder(SmilHolder::TargetList, false);
+ self.disassociated
+ .retain(|disassociated| disassociated.upgrade().is_some());
+ self.disassociated.push(animation.downgrade());
true
}
+ /// Makes the effects of the animations disassociated from `element`, whose lists these are, forget it as their
+ /// target, as the element is finalized: An animation that outlives the element, for an event still pending on it,
+ /// would otherwise name an element that is gone.
+ pub(crate) fn forget_element_as_target_of_disassociated(&mut self, element: NonNull<c_void>) {
+ for disassociated in self.disassociated.drain(..) {
+ let Some(animation) = disassociated.upgrade() else {
+ continue;
+ };
+ let effect = animation.borrow().effect().cloned();
+ if let Some(effect) = effect
+ && effect.borrow().target_element() == Some(element)
+ {
+ effect.borrow_mut().set_target_element(None);
+ }
+ }
+ }
+
pub(crate) fn associated(&self) -> &[AnimationHandle] {
&self.associated
}
@@ -348,6 +374,7 @@ impl ElementAnimations {
size_of::<Self>()
+ small_vec_bytes(&self.associated)
+ small_vec_bytes(&self.owners)
+ + small_vec_bytes(&self.disassociated)
+ self
.owners
.iter()
@@ -379,3 +406,67 @@ fn publish_css_defined_animations(
.collect(),
}));
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::animations::animation::AnimationKind;
+ use crate::animations::effect::EffectHandle;
+
+ fn element_at(address: &mut u8) -> NonNull<c_void> {
+ NonNull::from(address).cast()
+ }
+
+ fn animation_targeting(element: NonNull<c_void>) -> AnimationHandle {
+ let effect = EffectHandle::new();
+ effect.borrow_mut().set_target_element(Some(element));
+ let animation = AnimationHandle::new(AnimationKind::Script);
+ animation.borrow_mut().set_effect(Some(effect));
+ animation
+ }
+
+ fn target_of(animation: &AnimationHandle) -> Option<NonNull<c_void>> {
+ animation
+ .borrow()
+ .effect()
+ .and_then(|effect| effect.borrow().target_element())
+ }
+
+ #[test]
+ fn finalizing_an_element_clears_the_target_of_an_animation_it_let_go_of() {
+ let mut element = 0;
+ let element = element_at(&mut element);
+ let animation = animation_targeting(element);
+ let mut animations = ElementAnimations::default();
+ animations.associate(animation.clone());
+ animations.disassociate(&animation);
+ animations.forget_element_as_target_of_disassociated(element);
+ assert_eq!(target_of(&animation), None);
+ }
+
+ #[test]
+ fn finalizing_an_element_keeps_the_target_an_animation_it_let_go_of_moved_to() {
+ let (mut first, mut second) = (0, 0);
+ let (first, second) = (element_at(&mut first), element_at(&mut second));
+ let animation = animation_targeting(first);
+ let mut animations = ElementAnimations::default();
+ animations.associate(animation.clone());
+ animations.disassociate(&animation);
+ let effect = animation.borrow().effect().cloned().unwrap();
+ effect.borrow_mut().set_target_element(Some(second));
+ animations.forget_element_as_target_of_disassociated(first);
+ assert_eq!(target_of(&animation), Some(second));
+ }
+
+ #[test]
+ fn an_animation_associated_again_is_no_longer_held_as_let_go_of() {
+ let mut element = 0;
+ let element = element_at(&mut element);
+ let animation = animation_targeting(element);
+ let mut animations = ElementAnimations::default();
+ animations.associate(animation.clone());
+ animations.disassociate(&animation);
+ animations.associate(animation.clone());
+ assert!(animations.disassociated.is_empty());
+ }
+}
diff --git a/Libraries/LibWeb/Rust/src/animations/ffi.rs b/Libraries/LibWeb/Rust/src/animations/ffi.rs
index 234500f64fa..e78725977cc 100644
--- a/Libraries/LibWeb/Rust/src/animations/ffi.rs
+++ b/Libraries/LibWeb/Rust/src/animations/ffi.rs
@@ -2599,11 +2599,19 @@ pub extern "C" fn animations_element_animations_create() -> *mut ElementAnimatio
/// Destroys the lists of the animations of an element, giving up the references they hold.
///
/// # Safety
-/// `animations` must be lists `animations_element_animations_create` handed out, which the host gives up.
+/// `animations` must be lists `animations_element_animations_create` handed out, which the host gives up, and
+/// `element` null or the element whose lists they are.
#[unsafe(no_mangle)]
-pub unsafe extern "C" fn animations_element_animations_destroy(animations: *mut ElementAnimations) {
+pub unsafe extern "C" fn animations_element_animations_destroy(
+ animations: *mut ElementAnimations,
+ element: *mut c_void,
+) {
// SAFETY: Guaranteed by the caller.
- drop(unsafe { Box::from_raw(animations) });
+ let mut animations = unsafe { Box::from_raw(animations) };
+ if let Some(element) = NonNull::new(element) {
+ animations.forget_element_as_target_of_disassociated(element);
+ }
+ drop(animations);
}
/// Calls `visit` with `context` and each GC cell of the host that keeps alive what an animation the lists hold names.
diff --git a/Tests/LibWeb/Text/expected/css/style-memory/bytes-per-animated-element.txt b/Tests/LibWeb/Text/expected/css/style-memory/bytes-per-animated-element.txt
index e57a758e26f..0c68d3bf0e7 100644
--- a/Tests/LibWeb/Text/expected/css/style-memory/bytes-per-animated-element.txt
+++ b/Tests/LibWeb/Text/expected/css/style-memory/bytes-per-animated-element.txt
@@ -27,7 +27,7 @@ animation: 500 elements
cppInstalledStyles: within 8
cppHintProperties: within 0
animationObjects: within 0
- animationRecords: within 856
+ animationRecords: within 880
transition: 500 elements
initial: within 0
ruleStore: within 0
@@ -57,4 +57,4 @@ transition: 500 elements
cppInstalledStyles: within 8
cppHintProperties: within 0
animationObjects: within 0
- animationRecords: within 1058
+ animationRecords: within 1066
diff --git a/Tests/LibWeb/Text/input/css/style-memory/bytes-per-animated-element.html b/Tests/LibWeb/Text/input/css/style-memory/bytes-per-animated-element.html
index 4ec4271a2c8..80828b276f5 100644
--- a/Tests/LibWeb/Text/input/css/style-memory/bytes-per-animated-element.html
+++ b/Tests/LibWeb/Text/input/css/style-memory/bytes-per-animated-element.html
@@ -48,7 +48,8 @@
// - cssAnimations: each CSS animation's name and applied definition.
// - styleValues: the animated value of the longhand the payload of its group holds.
// - animationRecords: the records the animation and its effect keep their state in, a transition's keyframes, and
- // the element's lists of its animations and of the effect descriptions it published to the engine.
+ // the element's lists of its animations, of those it let go of while inert, and of the effect descriptions it
+ // published to the engine.
const animationBudgets = {
records: 464,
payloads: 656,
@@ -63,7 +64,7 @@
animationEffects: 416,
cssAnimations: 240,
cppInstalledStyles: 8,
- animationRecords: 856,
+ animationRecords: 880,
};
const transitionBudgets = {
records: 464,
@@ -77,7 +78,7 @@
layoutStyleColumns: 28,
animationEffects: 348,
cppInstalledStyles: 8,
- animationRecords: 1058,
+ animationRecords: 1066,
};
// NB: What a transaction in flight holds of earlier generations, and what the style threads keep for the next