2023-11-04 13:57:57 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2024, Matthew Olsson <mattco@serenityos.org>.
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <LibWeb/Animations/KeyframeEffect.h>
|
2026-02-10 23:16:02 +13:00
|
|
|
#include <LibWeb/Animations/ScrollTimeline.h>
|
2024-04-27 12:09:58 +12:00
|
|
|
#include <LibWeb/Bindings/CSSAnimationPrototype.h>
|
2023-11-04 13:57:57 -07:00
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
|
|
|
|
#include <LibWeb/CSS/CSSAnimation.h>
|
2026-02-10 17:48:24 +13:00
|
|
|
#include <LibWeb/CSS/PropertyID.h>
|
2023-11-04 13:57:57 -07:00
|
|
|
#include <LibWeb/DOM/Element.h>
|
2026-02-10 17:38:11 +13:00
|
|
|
#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>
|
2023-11-04 13:57:57 -07:00
|
|
|
|
|
|
|
|
namespace Web::CSS {
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DEFINE_ALLOCATOR(CSSAnimation);
|
2024-04-06 10:16:04 -07:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<CSSAnimation> CSSAnimation::create(JS::Realm& realm)
|
2023-11-04 13:57:57 -07:00
|
|
|
{
|
2024-11-14 05:50:17 +13:00
|
|
|
return realm.create<CSSAnimation>(realm);
|
2023-11-04 13:57:57 -07:00
|
|
|
}
|
|
|
|
|
|
2024-02-02 16:38:09 -07:00
|
|
|
// https://www.w3.org/TR/css-animations-2/#animation-composite-order
|
2025-11-11 11:45:36 +01:00
|
|
|
int CSSAnimation::class_specific_composite_order(GC::Ref<Animations::Animation> other_animation) const
|
2024-02-02 16:38:09 -07:00
|
|
|
{
|
2025-01-21 09:12:05 -05:00
|
|
|
auto other = GC::Ref { as<CSSAnimation>(*other_animation) };
|
2024-02-02 16:38:09 -07:00
|
|
|
|
2025-04-07 10:46:22 +00:00
|
|
|
// The existence of an owning element determines the animation class, so both animations should have their owning
|
2024-02-02 16:38:09 -07:00
|
|
|
// element in the same state
|
2025-12-02 11:17:41 +00:00
|
|
|
VERIFY(owning_element().has_value() == other->owning_element().has_value());
|
2024-02-02 16:38:09 -07:00
|
|
|
|
|
|
|
|
// Within the set of CSS Animations with an owning element, two animations A and B are sorted in composite order
|
|
|
|
|
// (first to last) as follows:
|
2025-12-02 11:17:41 +00:00
|
|
|
if (owning_element().has_value()) {
|
2024-02-02 16:38:09 -07:00
|
|
|
// 1. If the owning element of A and B differs, sort A and B by tree order of their corresponding owning elements.
|
|
|
|
|
// With regard to pseudo-elements, the sort order is as follows:
|
|
|
|
|
// - element
|
|
|
|
|
// - ::marker
|
|
|
|
|
// - ::before
|
|
|
|
|
// - any other pseudo-elements not mentioned specifically in this list, sorted in ascending order by the Unicode
|
|
|
|
|
// codepoints that make up each selector
|
|
|
|
|
// - ::after
|
|
|
|
|
// - element children
|
2025-12-02 11:17:41 +00:00
|
|
|
if (owning_element() != other->owning_element()) {
|
2024-02-02 16:38:09 -07:00
|
|
|
// FIXME: Sort by tree order
|
2025-11-11 11:45:36 +01:00
|
|
|
return 0;
|
2024-02-02 16:38:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2. Otherwise, sort A and B based on their position in the computed value of the animation-name property of the
|
|
|
|
|
// (common) owning element.
|
|
|
|
|
// FIXME: Do this when animation-name supports multiple values
|
2025-11-11 11:45:36 +01:00
|
|
|
return 0;
|
2024-02-02 16:38:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The composite order of CSS Animations without an owning element is based on their position in the global animation list.
|
|
|
|
|
return global_animation_list_order() - other->global_animation_list_order();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Animations::AnimationClass CSSAnimation::animation_class() const
|
|
|
|
|
{
|
2025-12-02 11:17:41 +00:00
|
|
|
if (owning_element().has_value())
|
2024-02-02 16:38:09 -07:00
|
|
|
return Animations::AnimationClass::CSSAnimationWithOwningElement;
|
|
|
|
|
return Animations::AnimationClass::CSSAnimationWithoutOwningElement;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-10 23:16:02 +13:00
|
|
|
// NB: Unrelated style changes shouldn't cause us to recreate anonymous timelines, to achieve this we drop updates
|
|
|
|
|
// between two equivalent anonymous timelines.
|
|
|
|
|
static bool should_update_timeline(GC::Ptr<Animations::AnimationTimeline> old_timeline, GC::Ptr<Animations::AnimationTimeline> new_timeline)
|
|
|
|
|
{
|
|
|
|
|
if (!old_timeline || !new_timeline)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
if (is<Animations::ScrollTimeline>(*old_timeline) && is<Animations::ScrollTimeline>(*new_timeline)) {
|
|
|
|
|
auto const& old_scroll_timeline = as<Animations::ScrollTimeline>(*old_timeline);
|
|
|
|
|
auto const& new_scroll_timeline = as<Animations::ScrollTimeline>(*new_timeline);
|
|
|
|
|
|
|
|
|
|
if (!old_scroll_timeline.source_internal().has<Animations::ScrollTimeline::AnonymousSource>() || !new_scroll_timeline.source_internal().has<Animations::ScrollTimeline::AnonymousSource>())
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return old_scroll_timeline.source_internal().get<Animations::ScrollTimeline::AnonymousSource>() != new_scroll_timeline.source_internal().get<Animations::ScrollTimeline::AnonymousSource>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-10 17:38:11 +13:00
|
|
|
void CSSAnimation::apply_css_properties(ComputedProperties::AnimationProperties const& animation_properties)
|
|
|
|
|
{
|
2026-03-23 12:05:18 -04:00
|
|
|
// FIXME: Don't apply overridden properties as defined here: https://drafts.csswg.org/css-animations-2/#animations
|
2026-02-10 17:48:24 +13:00
|
|
|
|
2026-02-10 17:38:11 +13:00
|
|
|
VERIFY(effect());
|
|
|
|
|
|
|
|
|
|
auto& effect = as<Animations::KeyframeEffect>(*this->effect());
|
|
|
|
|
|
2026-02-10 23:16:02 +13:00
|
|
|
if (!m_ignored_css_properties.contains(PropertyID::AnimationTimeline) && should_update_timeline(timeline(), animation_properties.timeline)) {
|
2026-02-10 17:58:24 +13:00
|
|
|
HTML::TemporaryExecutionContext context(realm());
|
|
|
|
|
set_timeline(animation_properties.timeline);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-10 17:38:11 +13:00
|
|
|
effect.set_specified_iteration_duration(animation_properties.duration);
|
|
|
|
|
effect.set_specified_start_delay(animation_properties.delay);
|
2026-02-11 19:04:08 +13:00
|
|
|
effect.set_iteration_count(animation_properties.iteration_count);
|
2026-02-10 17:38:11 +13:00
|
|
|
// https://drafts.csswg.org/web-animations-2/#updating-animationeffect-timing
|
|
|
|
|
// Timing properties may also be updated due to a style change. Any change to a CSS animation property that affects
|
|
|
|
|
// timing requires rerunning the procedure to normalize specified timing.
|
|
|
|
|
effect.normalize_specified_timing();
|
2026-03-21 10:18:25 -05:00
|
|
|
// NB: animation-timing-function is applied per-keyframe, not as the effect-level timing function.
|
|
|
|
|
// The effect-level timing function remains linear.
|
|
|
|
|
m_default_easing = animation_properties.timing_function;
|
2026-02-10 17:38:11 +13:00
|
|
|
effect.set_fill_mode(Animations::css_fill_mode_to_bindings_fill_mode(animation_properties.fill_mode));
|
|
|
|
|
effect.set_playback_direction(Animations::css_animation_direction_to_bindings_playback_direction(animation_properties.direction));
|
|
|
|
|
effect.set_composite(Animations::css_animation_composition_to_bindings_composite_operation(animation_properties.composition));
|
|
|
|
|
|
|
|
|
|
if (animation_properties.play_state != last_css_animation_play_state()) {
|
|
|
|
|
if (animation_properties.play_state == CSS::AnimationPlayState::Running && play_state() != Bindings::AnimationPlayState::Running) {
|
|
|
|
|
HTML::TemporaryExecutionContext context(realm());
|
|
|
|
|
play().release_value_but_fixme_should_propagate_errors();
|
|
|
|
|
} else if (animation_properties.play_state == CSS::AnimationPlayState::Paused && play_state() != Bindings::AnimationPlayState::Paused) {
|
|
|
|
|
HTML::TemporaryExecutionContext context(realm());
|
|
|
|
|
pause().release_value_but_fixme_should_propagate_errors();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set_last_css_animation_play_state(animation_properties.play_state);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-10 17:48:24 +13:00
|
|
|
void CSSAnimation::set_timeline_for_bindings(GC::Ptr<Animations::AnimationTimeline> timeline)
|
|
|
|
|
{
|
|
|
|
|
// AD-HOC: When the timeline of a CSS animation is modified by the author from JS we should no longer apply changes
|
|
|
|
|
// to the `animation-timeline` property. See https://github.com/w3c/csswg-drafts/issues/13472
|
|
|
|
|
m_ignored_css_properties.set(PropertyID::AnimationTimeline);
|
|
|
|
|
set_timeline(timeline);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-04 13:57:57 -07:00
|
|
|
CSSAnimation::CSSAnimation(JS::Realm& realm)
|
|
|
|
|
: Animations::Animation(realm)
|
|
|
|
|
{
|
2024-02-19 13:53:01 +00:00
|
|
|
// FIXME:
|
|
|
|
|
// CSS Animations generated using the markup defined in this specification are not added to the global animation
|
|
|
|
|
// list when they are created. Instead, these animations are appended to the global animation list at the first
|
|
|
|
|
// moment when they transition out of the idle play state after being disassociated from their owning element. CSS
|
|
|
|
|
// Animations that have been disassociated from their owning element but are still idle do not have a defined
|
|
|
|
|
// composite order.
|
2023-11-04 13:57:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CSSAnimation::initialize(JS::Realm& realm)
|
|
|
|
|
{
|
2024-03-16 13:13:08 +01:00
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(CSSAnimation);
|
2025-04-20 16:22:57 +02:00
|
|
|
Base::initialize(realm);
|
2023-11-04 13:57:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|