Truly independent web browser
LibWeb: Cancel the animations of a pseudo-element that loses its style
Andreas Klingf16146f6e3f5
diff --git a/Libraries/LibWeb/Animations/Animatable.cpp b/Libraries/LibWeb/Animations/Animatable.cppindex 5a51574469f..6e343bbb366 100644--- a/Libraries/LibWeb/Animations/Animatable.cpp+++ b/Libraries/LibWeb/Animations/Animatable.cpp@@ -177,6 +177,17 @@ void Animatable::cancel_css_animations_and_transitions()m_animations, style_engine_host_of(*this), style_node_of(*this), keep_animation_alive_in, in_execution_context_of_animation, &animations, web_animation_perform_host_effect);}+void Animatable::cancel_css_animations_and_transitions_of(CSS::PseudoElement pseudo_element)+{+ auto slot = owner_slot(pseudo_element);+ if (!m_animations || !slot.has_value())+ return;++ GC::RootVector<AnimationRef> animations;+ RustFFI::animations_element_animations_cancel_css_animations_and_transitions_of_owner(+ m_animations, *slot, style_engine_host_of(*this), style_node_of(*this), keep_animation_alive_in, in_execution_context_of_animation, &animations, web_animation_perform_host_effect);+}+void Animatable::play_or_pause_css_animations_as_their_play_state_asks(){if (!m_animations)diff --git a/Libraries/LibWeb/Animations/Animatable.h b/Libraries/LibWeb/Animations/Animatable.hindex cfc3735cae3..5d8efa33d7b 100644--- a/Libraries/LibWeb/Animations/Animatable.h+++ b/Libraries/LibWeb/Animations/Animatable.h@@ -89,6 +89,8 @@ public:void disassociate_with_animation(AnimationRef const&, Optional<EffectRef> const& effect);void on_document_changed(DOM::Document& old_document, DOM::Document& new_document);void cancel_css_animations_and_transitions();+ // Cancels the CSS animations and transitions one of the element's pseudo-elements owns.+ void cancel_css_animations_and_transitions_of(CSS::PseudoElement);// Plays or pauses the CSS animations the element and its pseudo-elements own as their animation-play-state last// computed.void play_or_pause_css_animations_as_their_play_state_asks();diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cppindex 5a029f66281..4842dae997b 100644--- a/Libraries/LibWeb/DOM/Element.cpp+++ b/Libraries/LibWeb/DOM/Element.cpp@@ -1774,10 +1774,19 @@ void Element::swap_in_style_record(Layout::BegunRead const& read, Optional<CSS::// NB: The compositor animations of a pseudo-element keep the values they took from its style for as long as// the generation of its element stays the same.++m_animation_style_generation;- if (record)+ if (record) {ensure_synthetic_pseudo_element(*pseudo_element).swap_in_style_record(read, record);- else if (auto existing_pseudo_element = get_synthetic_pseudo_element(*pseudo_element); existing_pseudo_element.has_value())+ return;+ }+ if (auto existing_pseudo_element = get_synthetic_pseudo_element(*pseudo_element); existing_pseudo_element.has_value())existing_pseudo_element->clear_computed_style();+ // https://drafts.csswg.org/css-animations-2/#owning-element-section+ // If the display property of an element is set to none and its display value would compute to none when+ // ignoring the Transitions and Animations cascade origins, then terminate running animations with this owning+ // element.+ // NB: A pseudo-element that holds no style any more is no longer rendered, as an element under display: none+ // is not.+ cancel_css_animations_and_transitions_of(*pseudo_element);return;}++m_animation_style_generation;diff --git a/Libraries/LibWeb/Rust/src/animations/element_animations.rs b/Libraries/LibWeb/Rust/src/animations/element_animations.rsindex fefd020eade..aed42d28ed7 100644--- a/Libraries/LibWeb/Rust/src/animations/element_animations.rs+++ b/Libraries/LibWeb/Rust/src/animations/element_animations.rs@@ -302,14 +302,18 @@ impl ElementAnimations {transition.borrow_mut().disassociation_from_target_pending = true;}- /// Takes the CSS animations and transitions of every owner, answering them, and publishes to `engine`, if any, that- /// the owners that had CSS animations have none.+ /// Takes the CSS animations and transitions of the owner at `slot`, or of every owner where it names none, answering+ /// them, and publishes to `engine`, if any, that the owners that had CSS animations have none.pub(crate) fn take_css_animations_and_transitions(&mut self,+ slot: Option<AnimationOwnerSlot>,engine: Option<StyleEngineTarget<'_>>,) -> Vec<AnimationHandle> {let mut animations = Vec::new();for owner in &mut self.owners {+ if slot.is_some_and(|slot| owner.slot != slot) {+ continue;+ }if !owner.css_animations.is_empty()&& let Some(engine) = engine{@@ -323,7 +327,9 @@ impl ElementAnimations {animations.extend(std::mem::take(&mut owner.transitions));}self.remove_empty_owners();- self.has_css_defined_animations = false;+ if slot.is_none() {+ self.has_css_defined_animations = false;+ }animations}diff --git a/Libraries/LibWeb/Rust/src/animations/ffi.rs b/Libraries/LibWeb/Rust/src/animations/ffi.rsindex e78725977cc..26998e3de94 100644--- a/Libraries/LibWeb/Rust/src/animations/ffi.rs+++ b/Libraries/LibWeb/Rust/src/animations/ffi.rs@@ -2868,13 +2868,46 @@ pub unsafe extern "C" fn animations_element_animations_cancel_css_animations_andperform: HostEffectPerformer,) {// SAFETY: Guaranteed by the caller, and the borrow of the lists ends before the host performs anything.- let taken =- unsafe { &mut *animations }.take_css_animations_and_transitions(unsafe { style_engine_target(host, node) });- for animation in &taken {+ let taken = unsafe { &mut *animations }+ .take_css_animations_and_transitions(None, unsafe { style_engine_target(host, node) });+ cancel_taken_animations(&taken, keep_alive, in_execution_context, context, perform);+}++/// As [`animations_element_animations_cancel_css_animations_and_transitions`], of the owner at `slot` alone: a+/// pseudo-element that is no longer rendered, while its element is.+///+/// # Safety+/// As for [`animations_element_animations_cancel_css_animations_and_transitions`].+#[unsafe(no_mangle)]+pub unsafe extern "C" fn animations_element_animations_cancel_css_animations_and_transitions_of_owner(+ animations: *mut ElementAnimations,+ slot: AnimationOwnerSlot,+ host: *const DocumentHost,+ node: u32,+ keep_alive: FfiKeepAlive,+ in_execution_context: FfiInExecutionContext,+ context: *mut c_void,+ perform: HostEffectPerformer,+) {+ // SAFETY: Guaranteed by the caller, and the borrow of the lists ends before the host performs anything.+ let taken = unsafe { &mut *animations }+ .take_css_animations_and_transitions(Some(slot), unsafe { style_engine_target(host, node) });+ cancel_taken_animations(&taken, keep_alive, in_execution_context, context, perform);+}++/// Cancels the CSS animations and transitions taken from their owners, each kept alive by the host for the call.+fn cancel_taken_animations(+ taken: &[AnimationHandle],+ keep_alive: FfiKeepAlive,+ in_execution_context: FfiInExecutionContext,+ context: *mut c_void,+ perform: HostEffectPerformer,+) {+ for animation in taken {// SAFETY: The host keeps alive an animation it is handed for as long as this call lasts.unsafe { keep_alive(context, animation) };}- for animation in &taken {+ for animation in taken {cancel_animation(animation, Invalidate::No, in_execution_context, context, perform);}}diff --git a/Tests/LibWeb/Text/expected/css/pseudo-element-animations-canceled-when-no-longer-rendered.txt b/Tests/LibWeb/Text/expected/css/pseudo-element-animations-canceled-when-no-longer-rendered.txtnew file mode 100644index 00000000000..10a6dc3d84f--- /dev/null+++ b/Tests/LibWeb/Text/expected/css/pseudo-element-animations-canceled-when-no-longer-rendered.txt@@ -0,0 +1,5 @@+Animated ::before, rendered: rgb(255, 0, 0), animations: [CSSAnimation]+Animated ::before, no longer rendered: rgb(0, 128, 0), animations: []+Transitioned ::before, rendered: rgb(0, 128, 0), animations: []+Transitioned ::before, transition started: rgb(0, 0, 255), animations: [CSSTransition]+Transitioned ::before, no longer rendered: rgb(0, 128, 0), animations: []diff --git a/Tests/LibWeb/Text/input/css/pseudo-element-animations-canceled-when-no-longer-rendered.html b/Tests/LibWeb/Text/input/css/pseudo-element-animations-canceled-when-no-longer-rendered.htmlnew file mode 100644index 00000000000..417ec44885f--- /dev/null+++ b/Tests/LibWeb/Text/input/css/pseudo-element-animations-canceled-when-no-longer-rendered.html@@ -0,0 +1,42 @@+<!DOCTYPE html>+<style>+ @keyframes color {+ from { color: rgb(255, 0, 0); }+ to { color: rgb(255, 0, 0); }+ }+ .target::before { color: rgb(0, 128, 0); }+ #animated.rendered::before {+ content: "x";+ animation: color 100s;+ }+ #transitioned.rendered::before {+ content: "x";+ transition: color 100s steps(1, start);+ }+ #transitioned.rendered.moved::before { color: rgb(0, 0, 255); }+</style>+<div id="animated" class="target"></div>+<div id="transitioned" class="target"></div>+<script src="../include.js"></script>+<script>+ test(() => {+ const dump = (label, element) => {+ const animations = element.getAnimations({ subtree: true }).map(animation => animation.constructor.name);+ println(`${label}: ${getComputedStyle(element, "::before").color}, animations: [${animations}]`);+ };++ const animated = document.getElementById("animated");+ animated.classList.add("rendered");+ dump("Animated ::before, rendered", animated);+ animated.classList.remove("rendered");+ dump("Animated ::before, no longer rendered", animated);++ const transitioned = document.getElementById("transitioned");+ transitioned.classList.add("rendered");+ dump("Transitioned ::before, rendered", transitioned);+ transitioned.classList.add("moved");+ dump("Transitioned ::before, transition started", transitioned);+ transitioned.classList.remove("rendered");+ dump("Transitioned ::before, no longer rendered", transitioned);+ });+</script>