Truly independent web browser
LibWeb: Give each SVG document fragment a timeline for its animations
Jelle Raaijmakersdc0a75413663
diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txtindex b951d13a98e..c0c748a5069 100644--- a/Libraries/LibWeb/CMakeLists.txt+++ b/Libraries/LibWeb/CMakeLists.txt@@ -882,6 +882,7 @@ set(SOURCESSVG/AttributeParsing.cppSVG/Path.cppSVG/SMILAnimation.cpp+ SVG/SMILTimeContainer.cppSVG/SVGAElement.cppSVG/SVGAnimatedEnumeration.cppSVG/SVGAnimatedInteger.cppdiff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cppindex 62ce0708f5c..2ee1fcd2127 100644--- a/Libraries/LibWeb/DOM/Document.cpp+++ b/Libraries/LibWeb/DOM/Document.cpp@@ -236,6 +236,7 @@#include <LibWeb/Platform/EventLoopPlugin.h>#include <LibWeb/ResizeObserver/ResizeObserver.h>#include <LibWeb/ResizeObserver/ResizeObserverEntry.h>+#include <LibWeb/SVG/SMILTimeContainer.h>#include <LibWeb/SVG/SVGDecodedImageData.h>#include <LibWeb/SVG/SVGElement.h>#include <LibWeb/SVG/SVGPatternElement.h>@@ -994,6 +995,7 @@ void Document::visit_edges(Cell::Visitor& visitor)resource.value->visit_edges(visitor);visitor.visit(m_associated_animation_timelines);+ visitor.visit(m_smil_time_containers_to_begin);visitor.visit(m_list_of_available_images);for (auto& it : m_map_of_preloaded_resources)visitor.visit(it.value);@@ -5148,6 +5150,18 @@ void Document::queue_navigation_timing_entry()HTML::relevant_window_or_worker_global_scope(*this).queue_performance_entry(*m_navigation_timing_entry);}+void Document::begin_smil_time_container_once_loaded(SVG::SMILTimeContainer& time_container)+{+ // The timeline of an SVG document fragment should begin when the load event for its outermost svg element is+ // triggered, but that event is not fired. Like Blink, begin the timeline once the document has completely loaded+ // instead, or right away for a fragment that is inserted after that.+ if (is_completely_loaded()) {+ time_container.begin();+ return;+ }+ m_smil_time_containers_to_begin.append(time_container);+}+// https://html.spec.whatwg.org/multipage/browsing-the-web.html#completely-finish-loadingvoid Document::completely_finish_loading(){@@ -5162,6 +5176,9 @@ void Document::completely_finish_loading()if (!m_completely_loaded_time.has_value())m_completely_loaded_time = AK::UnixDateTime::now();+ for (auto& time_container : exchange(m_smil_time_containers_to_begin, {}))+ time_container->begin();+auto navigable = this->navigable();if (!navigable) {m_completely_loaded_deferred = true;diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.hindex db96fe3ecef..f59c0b09fc9 100644--- a/Libraries/LibWeb/DOM/Document.h+++ b/Libraries/LibWeb/DOM/Document.h@@ -1207,6 +1207,7 @@ public:WebIDL::ExceptionOr<Vector<GC::Ref<Animations::Animation>>> get_animations();HashTable<GC::Ref<Animations::AnimationTimeline>> const& associated_animation_timelines() const { return m_associated_animation_timelines; }+ void begin_smil_time_container_once_loaded(SVG::SMILTimeContainer&);bool ready_to_run_scripts() const { return m_ready_to_run_scripts; }void set_ready_to_run_scripts();@@ -2165,6 +2166,7 @@ private:// https://www.w3.org/TR/web-animations-1/#timeline-associated-with-a-documentHashTable<GC::Ref<Animations::AnimationTimeline>> m_associated_animation_timelines;+ Vector<GC::Ref<SVG::SMILTimeContainer>> m_smil_time_containers_to_begin;// NB: Weak so the document does not unnecessarily keep animations alive. Note that this also includes animations// associated with elements in shadow trees so differs from getAnimations()diff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.hindex 389334adae4..92fdc276640 100644--- a/Libraries/LibWeb/Forward.h+++ b/Libraries/LibWeb/Forward.h@@ -1133,6 +1133,7 @@ namespace Web::SVG {class Path;class SMILAnimation;+class SMILTimeContainer;class SVGAnimatedEnumeration;class SVGAnimatedInteger;class SVGAnimatedLength;diff --git a/Libraries/LibWeb/SVG/SMILAnimation.cpp b/Libraries/LibWeb/SVG/SMILAnimation.cppindex e739cf51c1c..e9dff957588 100644--- a/Libraries/LibWeb/SVG/SMILAnimation.cpp+++ b/Libraries/LibWeb/SVG/SMILAnimation.cpp@@ -6,24 +6,32 @@#include <LibWeb/DOM/Document.h>#include <LibWeb/SVG/SMILAnimation.h>+#include <LibWeb/SVG/SMILTimeContainer.h>#include <LibWeb/SVG/SVGAnimationElement.h>namespace Web::SVG {GC_DEFINE_ALLOCATOR(SMILAnimation);-GC::Ref<SMILAnimation> SMILAnimation::create(SVGAnimationElement& animation_element)+GC::Ref<SMILAnimation> SMILAnimation::create(SVGAnimationElement& animation_element, SMILTimeContainer& time_container){auto& environment = animation_element.document().relevant_settings_object();- return GC::Heap::the().allocate<SMILAnimation>(environment, animation_element);+ return GC::Heap::the().allocate<SMILAnimation>(environment, animation_element, time_container);}-SMILAnimation::SMILAnimation(HTML::EnvironmentSettingsObject& environment, SVGAnimationElement& animation_element)+SMILAnimation::SMILAnimation(HTML::EnvironmentSettingsObject& environment, SVGAnimationElement& animation_element, SMILTimeContainer& time_container): Animations::Animation(environment), m_animation_element(animation_element)+ , m_time_container(time_container){}+void SMILAnimation::cancel(ShouldInvalidate should_invalidate)+{+ m_time_container->remove_animation(*this);+ Base::cancel(should_invalidate);+}+// https://www.w3.org/TR/smil-animation/#AnimationSandwichModelint SMILAnimation::class_specific_composite_order(GC::Ref<Animations::Animation> other) const{@@ -44,6 +52,7 @@ void SMILAnimation::visit_edges(Cell::Visitor& visitor){Base::visit_edges(visitor);visitor.visit(m_animation_element);+ visitor.visit(m_time_container);}}diff --git a/Libraries/LibWeb/SVG/SMILAnimation.h b/Libraries/LibWeb/SVG/SMILAnimation.hindex 5e5fbac1cbc..d4753be5a1b 100644--- a/Libraries/LibWeb/SVG/SMILAnimation.h+++ b/Libraries/LibWeb/SVG/SMILAnimation.h@@ -16,20 +16,23 @@ class SMILAnimation final : public Animations::Animation {GC_DECLARE_ALLOCATOR(SMILAnimation);public:- static GC::Ref<SMILAnimation> create(SVGAnimationElement&);+ static GC::Ref<SMILAnimation> create(SVGAnimationElement&, SMILTimeContainer&);SVGAnimationElement& animation_element() const { return m_animation_element; }+ virtual void cancel(ShouldInvalidate = ShouldInvalidate::Yes) override;+virtual bool is_smil_animation() const override { return true; }virtual Animations::AnimationClass animation_class() const override { return Animations::AnimationClass::SMILAnimation; }virtual int class_specific_composite_order(GC::Ref<Animations::Animation> other) const override;private:- SMILAnimation(HTML::EnvironmentSettingsObject&, SVGAnimationElement&);+ SMILAnimation(HTML::EnvironmentSettingsObject&, SVGAnimationElement&, SMILTimeContainer&);virtual void visit_edges(Cell::Visitor&) override;GC::Ref<SVGAnimationElement> m_animation_element;+ GC::Ref<SMILTimeContainer> m_time_container;};}diff --git a/Libraries/LibWeb/SVG/SMILTimeContainer.cpp b/Libraries/LibWeb/SVG/SMILTimeContainer.cppnew file mode 100644index 00000000000..b787d48b8ef--- /dev/null+++ b/Libraries/LibWeb/SVG/SMILTimeContainer.cpp@@ -0,0 +1,166 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <LibWeb/Animations/DocumentTimeline.h>+#include <LibWeb/DOM/Document.h>+#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>+#include <LibWeb/SVG/SMILAnimation.h>+#include <LibWeb/SVG/SMILTimeContainer.h>++namespace Web::SVG {++GC_DEFINE_ALLOCATOR(SMILTimeContainer);++GC::Ref<SMILTimeContainer> SMILTimeContainer::create(DOM::Document& document)+{+ return GC::Heap::the().allocate<SMILTimeContainer>(document);+}++// https://svgwg.org/specs/animations/#SVGElementTimelinebeginAttribute+// The outermost svg element of an SVG document fragment defines a timeline. Absolute times used by animation elements+// in an SVG document fragment are relative to the document fragment's timeline.+// NB: The animations of the timeline play on the document timeline, with their current time kept equal to the+// timeline's: while the timeline runs, they start at the time on the document timeline at which its current time+// is zero, and while it is paused, they are held at the time it was paused at. Until the timeline begins, they+// stay idle.+SMILTimeContainer::SMILTimeContainer(DOM::Document& document)+ : m_document(document)+{+}++void SMILTimeContainer::visit_edges(GC::Cell::Visitor& visitor)+{+ Base::visit_edges(visitor);+ visitor.visit(m_document);+ visitor.visit(m_animations);+}++double SMILTimeContainer::document_timeline_time() const+{+ auto time = m_document->timeline()->current_time();+ return time.has_value() ? time->value : 0.;+}++// https://svgwg.org/specs/animations/#SVGElementTimelinebeginAttribute+void SMILTimeContainer::begin()+{+ // 'loadend'+ // The document fragment's timeline starts the moment the load event for the outermost svg element is+ // triggered.+ // NB: The caller decides when that moment is.+ VERIFY(!m_has_begun);+ m_has_begun = true;++ // NB: Seek to the time that setCurrentTime() recorded before the timeline began, if any.+ auto time = m_time_to_seek_to_once_begun.value_or(0.);+ m_time_to_seek_to_once_begun.clear();+ if (m_is_paused)+ m_paused_time = time;+ else+ m_zero_time = document_timeline_time() - time;+ synchronize_animations();+}++// https://svgwg.org/specs/animations/#__svg__SVGSVGElement__pauseAnimations+void SMILTimeContainer::pause()+{+ // The pauseAnimations and unpauseAnimations methods are used to pause and unpause all of the animations within+ // the SVG document fragment.+ if (m_is_paused)+ return;+ if (m_has_begun)+ m_paused_time = current_time_in_milliseconds();+ m_is_paused = true;+ synchronize_animations();+}++// https://svgwg.org/specs/animations/#__svg__SVGSVGElement__unpauseAnimations+void SMILTimeContainer::unpause()+{+ // The pauseAnimations and unpauseAnimations methods are used to pause and unpause all of the animations within+ // the SVG document fragment.+ if (!m_is_paused)+ return;+ m_is_paused = false;+ if (m_has_begun)+ m_zero_time = document_timeline_time() - m_paused_time;+ synchronize_animations();+}++// https://svgwg.org/specs/animations/#__svg__SVGSVGElement__getCurrentTime+double SMILTimeContainer::current_time_in_milliseconds() const+{+ // Return the current time in seconds relative to the start time of the timeline.+ // NB: This returns it in milliseconds, the unit of the document timeline.+ VERIFY(m_has_begun);+ if (m_is_paused)+ return m_paused_time;+ return document_timeline_time() - m_zero_time;+}++// https://svgwg.org/specs/animations/#__svg__SVGSVGElement__setCurrentTime+void SMILTimeContainer::seek_to_time_in_milliseconds(double time)+{+ // If the timeline for the current SVG document fragment has begun, then the timeline's current time (in seconds,+ // relative to the start time of the timeline) is set to seconds.+ VERIFY(m_has_begun);++ // NB: Like other engines, clamp a negative time to the start of the timeline, as SVG Tiny 1.2 does.+ time = max(time, 0.);+ if (m_is_paused)+ m_paused_time = time;+ else+ m_zero_time = document_timeline_time() - time;+ synchronize_animations();+}++// https://svgwg.org/specs/animations/#__svg__SVGSVGElement__setCurrentTime+void SMILTimeContainer::record_time_to_seek_to_once_begun_in_milliseconds(double time)+{+ // Otherwise, the timeline has not begun. seconds is recorded on the current SVG document fragment as the time to be+ // seeked to once the timeline has begun.+ VERIFY(!m_has_begun);+ m_time_to_seek_to_once_begun = max(time, 0.);+}++void SMILTimeContainer::add_animation(SMILAnimation& animation)+{+ m_animations.append(animation);+ synchronize_animation(animation);+}++void SMILTimeContainer::remove_animation(SMILAnimation& animation)+{+ m_animations.remove_first_matching([&](auto const& entry) { return entry.ptr() == &animation; });+}++void SMILTimeContainer::synchronize_animations()+{+ for (auto& animation : m_animations)+ synchronize_animation(animation);+}++// https://drafts.csswg.org/web-animations-1/#setting-the-start-time-of-an-animation+// https://drafts.csswg.org/web-animations-1/#setting-the-current-time-of-an-animation+void SMILTimeContainer::synchronize_animation(SMILAnimation& animation)+{+ // NB: Both procedures take effect synchronously, and update the finished state of the animation as a seek does. A+ // backward seek therefore applies to an animation that already finished, too.+ if (!m_has_begun)+ return;++ HTML::TemporaryExecutionContext execution_context { m_document->relevant_settings_object() };+ if (m_is_paused) {+ // NB: Making the start time unresolved holds the animation at its current time, which setting the current time+ // then moves to the time the timeline is paused at.+ MUST(animation.set_start_time_for_bindings(Animations::NullableCSSNumberish { Empty {} }));+ MUST(animation.set_current_time_for_bindings(Animations::NullableCSSNumberish { m_paused_time }));+ return;+ }+ MUST(animation.set_start_time_for_bindings(Animations::NullableCSSNumberish { m_zero_time }));+}++}diff --git a/Libraries/LibWeb/SVG/SMILTimeContainer.h b/Libraries/LibWeb/SVG/SMILTimeContainer.hnew file mode 100644index 00000000000..8ff336a9ef6--- /dev/null+++ b/Libraries/LibWeb/SVG/SMILTimeContainer.h@@ -0,0 +1,59 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/Optional.h>+#include <AK/Vector.h>+#include <LibGC/Cell.h>+#include <LibGC/CellAllocator.h>+#include <LibGC/Ptr.h>+#include <LibWeb/Forward.h>++namespace Web::SVG {++// https://svgwg.org/specs/animations/#SVGElementTimelinebeginAttribute+class SMILTimeContainer final : public GC::Cell {+ GC_CELL(SMILTimeContainer, GC::Cell);+ GC_DECLARE_ALLOCATOR(SMILTimeContainer);++public:+ [[nodiscard]] static GC::Ref<SMILTimeContainer> create(DOM::Document&);++ void begin();+ bool has_begun() const { return m_has_begun; }++ void pause();+ void unpause();+ bool is_paused() const { return m_is_paused; }++ double current_time_in_milliseconds() const;+ void seek_to_time_in_milliseconds(double);+ void record_time_to_seek_to_once_begun_in_milliseconds(double);++ void add_animation(SMILAnimation&);+ void remove_animation(SMILAnimation&);++private:+ explicit SMILTimeContainer(DOM::Document&);++ virtual void visit_edges(GC::Cell::Visitor&) override;++ double document_timeline_time() const;+ void synchronize_animations();+ void synchronize_animation(SMILAnimation&);++ GC::Ref<DOM::Document> m_document;+ Vector<GC::Ref<SMILAnimation>> m_animations;++ bool m_has_begun { false };+ bool m_is_paused { false };+ Optional<double> m_time_to_seek_to_once_begun;+ double m_paused_time { 0 };+ double m_zero_time { 0 };+};++}diff --git a/Libraries/LibWeb/SVG/SVGAnimationElement.cpp b/Libraries/LibWeb/SVG/SVGAnimationElement.cppindex 51cca082f11..f5fb966e3c5 100644--- a/Libraries/LibWeb/SVG/SVGAnimationElement.cpp+++ b/Libraries/LibWeb/SVG/SVGAnimationElement.cpp@@ -11,11 +11,12 @@#include <LibWeb/DOM/Document.h>#include <LibWeb/HTML/EventNames.h>#include <LibWeb/HTML/Scripting/Environments.h>-#include <LibWeb/HTML/Scripting/TemporaryExecutionContext.h>#include <LibWeb/Layout/Node.h>#include <LibWeb/SVG/AttributeNames.h>#include <LibWeb/SVG/AttributeParsing.h>#include <LibWeb/SVG/SMILAnimation.h>+#include <LibWeb/SVG/SMILTimeContainer.h>+#include <LibWeb/SVG/SVGSVGElement.h>#include <LibWeb/WebIDL/DOMException.h>namespace Web::SVG {@@ -56,6 +57,12 @@ void SVGAnimationElement::attribute_changed(Utf16FlyString const& name, Optionalupdate_animation();}+GC::Ptr<SMILTimeContainer> SVGAnimationElement::time_container()+{+ auto outermost_svg_element = this->outermost_svg_element();+ return outermost_svg_element ? outermost_svg_element->time_container() : nullptr;+}+// https://svgwg.org/specs/animations/#HrefAttributeGC::Ptr<SVGElement> SVGAnimationElement::target_element(){@@ -125,6 +132,9 @@ void SVGAnimationElement::update_animation()if (!is_connected())return;+ auto time_container = this->time_container();+ if (!time_container)+ return;auto target = target_element();if (!target)return;@@ -141,14 +151,10 @@ void SVGAnimationElement::update_animation()auto effect = MUST(Animations::KeyframeEffect::create_from_processed_keyframes(target, move(keyframes), simple_duration()));effect->set_fill_mode(fill_mode());- // AD-HOC: Animations begin once their element is connected, and begin again whenever an attribute they depend on- // changes, on the document's default timeline. They should begin at the document begin of their SVG- // document fragment, which is the time of its load event.- auto animation = SMILAnimation::create(*this);+ auto animation = SMILAnimation::create(*this, *time_container);animation->set_timeline(document().timeline());animation->set_effect(effect);- HTML::TemporaryExecutionContext execution_context { document().relevant_settings_object() };- MUST(animation->play());+ time_container->add_animation(animation);m_animation = animation;}@@ -188,6 +194,32 @@ GC::Ptr<WebIDL::CallbackType> SVGAnimationElement::onrepeat()return event_handler_attribute(HTML::EventNames::repeatEvent);}+// https://svgwg.org/specs/animations/#__svg__SVGAnimationElement__getStartTime+WebIDL::ExceptionOr<float> SVGAnimationElement::get_start_time()+{+ // Returns the begin time, in seconds, for this animation element's current interval, if it exists, regardless of+ // whether the interval has begun yet. If there is no current interval, then a DOMException with code+ // INVALID_STATE_ERR is thrown.+ // NB: An animation element in an SVG document fragment has one interval, which begins when the timeline of the+ // fragment begins.+ // FIXME: Resolve the begin times of intervals from the begin attribute.+ if (!is_connected() || !time_container())+ return WebIDL::InvalidStateError::create(HTML::relevant_realm(*this), "The animation element has no current interval"_utf16);+ return 0;+}++// https://svgwg.org/specs/animations/#__svg__SVGAnimationElement__getCurrentTime+float SVGAnimationElement::get_current_time()+{+ // Returns the current time in seconds relative to time zero for the given time container.+ // NB: Like SVGSVGElement.getCurrentTime(), return 0 until the timeline has begun, and for an animation element+ // outside of an SVG document fragment.+ auto time_container = is_connected() ? this->time_container() : nullptr;+ if (!time_container || !time_container->has_begun())+ return 0;+ return static_cast<float>(time_container->current_time_in_milliseconds() / 1000.);+}+// https://svgwg.org/specs/animations/#__svg__SVGAnimationElement__getSimpleDurationWebIDL::ExceptionOr<float> SVGAnimationElement::get_simple_duration(){diff --git a/Libraries/LibWeb/SVG/SVGAnimationElement.h b/Libraries/LibWeb/SVG/SVGAnimationElement.hindex 6e1624ddbab..ec602076124 100644--- a/Libraries/LibWeb/SVG/SVGAnimationElement.h+++ b/Libraries/LibWeb/SVG/SVGAnimationElement.h@@ -29,6 +29,8 @@ public:void set_onrepeat(GC::Ptr<WebIDL::CallbackType>);GC::Ptr<WebIDL::CallbackType> onrepeat();+ WebIDL::ExceptionOr<float> get_start_time();+ float get_current_time();WebIDL::ExceptionOr<float> get_simple_duration();protected:@@ -46,6 +48,7 @@ protected:private:Optional<CSS::PropertyID> target_property() const;+ GC::Ptr<SMILTimeContainer> time_container();double simple_duration() const;Bindings::FillMode fill_mode() const;diff --git a/Libraries/LibWeb/SVG/SVGAnimationElement.idl b/Libraries/LibWeb/SVG/SVGAnimationElement.idlindex b3eac84a5b7..d5377ab8e23 100644--- a/Libraries/LibWeb/SVG/SVGAnimationElement.idl+++ b/Libraries/LibWeb/SVG/SVGAnimationElement.idl@@ -8,8 +8,8 @@ interface SVGAnimationElement : SVGElement {attribute EventHandler onend;attribute EventHandler onrepeat;- [FIXME] float getStartTime();- [FIXME] float getCurrentTime();+ float getStartTime();+ float getCurrentTime();float getSimpleDuration();[FIXME] undefined beginElement();diff --git a/Libraries/LibWeb/SVG/SVGElement.cpp b/Libraries/LibWeb/SVG/SVGElement.cppindex b7b62945895..3e27920d056 100644--- a/Libraries/LibWeb/SVG/SVGElement.cpp+++ b/Libraries/LibWeb/SVG/SVGElement.cpp@@ -612,6 +612,16 @@ GC::Ptr<SVGSVGElement> SVGElement::owner_svg_element()return nullptr;}+// https://svgwg.org/svg2-draft/struct.html#TermOutermostSVGElement+GC::Ptr<SVGSVGElement> SVGElement::outermost_svg_element()+{+ // The furthest ‘svg’ ancestor element that remains in the current SVG document fragment.+ GC::Ptr<SVGSVGElement> outermost_svg_element = as_if<SVGSVGElement>(*this);+ for (auto ancestor = owner_svg_element(); ancestor; ancestor = ancestor->owner_svg_element())+ outermost_svg_element = ancestor;+ return outermost_svg_element;+}+// https://svgwg.org/svg2-draft/types.html#__svg__SVGElement__viewportElementGC::Ptr<SVGElement> SVGElement::viewport_element(){diff --git a/Libraries/LibWeb/SVG/SVGElement.h b/Libraries/LibWeb/SVG/SVGElement.hindex 0538c8a9f8e..24f92aa6d25 100644--- a/Libraries/LibWeb/SVG/SVGElement.h+++ b/Libraries/LibWeb/SVG/SVGElement.h@@ -37,6 +37,7 @@ public:GC::Ref<SVGAnimatedString> class_name();GC::Ptr<SVGSVGElement> owner_svg_element();+ GC::Ptr<SVGSVGElement> outermost_svg_element();GC::Ptr<SVGElement> viewport_element();bool should_include_in_accessibility_tree() const;diff --git a/Libraries/LibWeb/SVG/SVGSVGElement.cpp b/Libraries/LibWeb/SVG/SVGSVGElement.cppindex 309c891d024..b305d7b3c02 100644--- a/Libraries/LibWeb/SVG/SVGSVGElement.cpp+++ b/Libraries/LibWeb/SVG/SVGSVGElement.cpp@@ -18,6 +18,7 @@#include <LibWeb/HTML/Parser/HTMLParser.h>#include <LibWeb/SVG/AttributeNames.h>#include <LibWeb/SVG/FragmentIdentifier.h>+#include <LibWeb/SVG/SMILTimeContainer.h>#include <LibWeb/SVG/SVGAnimatedRect.h>#include <LibWeb/SVG/SVGNumber.h>#include <LibWeb/SVG/SVGSVGElement.h>@@ -43,6 +44,38 @@ void SVGSVGElement::visit_edges(Visitor& visitor)Base::visit_edges(visitor);SVGFitToViewBox::visit_edges(visitor);visitor.visit(m_active_view_element);+ visitor.visit(m_time_container);+}++void SVGSVGElement::inserted()+{+ Base::inserted();+ create_time_container_if_outermost();+}++void SVGSVGElement::adopted_from(DOM::Document& old_document)+{+ Base::adopted_from(old_document);++ // A time container follows the document timeline of the document it was created for, so an adopted svg element+ // creates a new one once it is inserted.+ m_time_container = nullptr;+}++void SVGSVGElement::moved_from(IsSubtreeRoot is_subtree_root, GC::Ptr<Node> old_ancestor)+{+ Base::moved_from(is_subtree_root, old_ancestor);+ create_time_container_if_outermost();+}++void SVGSVGElement::create_time_container_if_outermost()+{+ // The outermost svg element of an SVG document fragment defines its timeline, which it keeps when it is removed or+ // moved and inserted again. The animations inside it update after it, as it comes first in tree order.+ if (m_time_container || !is_connected() || owner_svg_element())+ return;+ m_time_container = SMILTimeContainer::create(document());+ document().begin_smil_time_container_once_loaded(*m_time_container);}CSS::ElementBoxKind SVGSVGElement::box_kind() const@@ -217,6 +250,78 @@ void SVGSVGElement::deselect_all() constselection->remove_all_ranges();}+// https://svgwg.org/specs/animations/#__svg__SVGSVGElement__pauseAnimations+void SVGSVGElement::pause_animations()+{+ // When pauseAnimations() is called, the following steps are run:+ // 1. If the current ‘svg’ element is not the outermost svg element, then return.+ if (owner_svg_element())+ return;++ // 2. Pause the timeline that controls SVG animations for the SVG document fragment that the ‘svg’ element is in.+ // NB: An svg element that has never been connected has no timeline.+ if (m_time_container)+ m_time_container->pause();+}++// https://svgwg.org/specs/animations/#__svg__SVGSVGElement__unpauseAnimations+void SVGSVGElement::unpause_animations()+{+ // When unpauseAnimations() is called, the following steps are run:+ // 1. If the current ‘svg’ element is not the outermost svg element, then return.+ if (owner_svg_element())+ return;++ // 2. Unpause the timeline that controls SVG animations for the SVG document fragment that the ‘svg’ element is in.+ if (m_time_container)+ m_time_container->unpause();+}++// https://svgwg.org/specs/animations/#__svg__SVGSVGElement__animationsPaused+bool SVGSVGElement::animations_paused()+{+ // When animationsPaused() is called, true is returned if the timeline that controls SVG animations for the SVG+ // document fragment that the ‘svg’ element in has been paused by pauseAnimations; otherwise, false is returned.+ auto time_container = outermost_svg_element()->time_container();+ return time_container && time_container->is_paused();+}++// https://svgwg.org/specs/animations/#__svg__SVGSVGElement__getCurrentTime+float SVGSVGElement::get_current_time()+{+ // When getCurrentTime() is called, the following steps are run:+ // 1. If the timeline for the current SVG document fragment has not yet begun, then return 0.+ auto time_container = outermost_svg_element()->time_container();+ if (!time_container || !time_container->has_begun())+ return 0;++ // 2. Otherwise, the timeline for the current SVG document fragment has begun. Return the current time in seconds+ // relative to the start time of the timeline.+ return static_cast<float>(time_container->current_time_in_milliseconds() / 1000.);+}++// https://svgwg.org/specs/animations/#__svg__SVGSVGElement__setCurrentTime+void SVGSVGElement::set_current_time(float seconds)+{+ // When setCurrentTime(seconds) is called, the following steps are run:+ // 1. If the current ‘svg’ element is not the outermost svg element, then return.+ if (owner_svg_element() || !m_time_container)+ return;++ // 2. If the timeline for the current SVG document fragment has begun, then the timeline's current time (in seconds,+ // relative to the start time of the timeline) is set to seconds.+ auto milliseconds = static_cast<double>(seconds) * 1000.;+ if (m_time_container->has_begun()) {+ m_time_container->seek_to_time_in_milliseconds(milliseconds);+ return;+ }++ // 3. Otherwise, the timeline has not begun. seconds is recorded on the current SVG document fragment as the time to+ // be seeked to once the timeline has begun. If multiple calls to setCurrentTime are made before the timeline has+ // begun, the most recent call's time will be used to seek the timeline once it begins.+ m_time_container->record_time_to_seek_to_once_begun_in_milliseconds(milliseconds);+}+// https://w3c.github.io/svgwg/svg2-draft/struct.html#__svg__SVGSVGElement__createSVGNumberGC::Ref<SVGNumber> SVGSVGElement::create_svg_number() const{diff --git a/Libraries/LibWeb/SVG/SVGSVGElement.h b/Libraries/LibWeb/SVG/SVGSVGElement.hindex c1d7e7ba049..1f435082087 100644--- a/Libraries/LibWeb/SVG/SVGSVGElement.h+++ b/Libraries/LibWeb/SVG/SVGSVGElement.h@@ -84,6 +84,14 @@ public:void unsuspend_redraw_all() const { }void force_redraw() const { }+ void pause_animations();+ void unpause_animations();+ bool animations_paused();+ float get_current_time();+ void set_current_time(float seconds);++ GC::Ptr<SMILTimeContainer> time_container() const { return m_time_container; }+// The width/height attributes parsed as the CSS width/height properties, when that yields a// plain <length> - the only case the natural size negotiation can use.[[nodiscard]] Optional<CSS::Length> width_attribute_length() const;@@ -98,6 +106,9 @@ private:virtual void initialize_element() override;virtual void visit_edges(Visitor&) override;+ virtual void inserted() override;+ virtual void adopted_from(DOM::Document&) override;+ virtual void moved_from(IsSubtreeRoot, GC::Ptr<Node> old_ancestor) override;virtual bool is_svg_svg_element() const override { return true; }@@ -107,6 +118,7 @@ private:virtual void children_changed(ChildrenChangedMetadata const&) override;void update_fallback_view_box_for_svg_as_image();+ void create_time_container_if_outermost();Optional<ViewBox> m_fallback_view_box_for_svg_as_image;@@ -114,6 +126,8 @@ private:mutable Optional<Optional<CSS::Length>> m_cached_height_attribute_length;GC::Ptr<SVGViewElement> m_active_view_element;++ GC::Ptr<SMILTimeContainer> m_time_container;};}diff --git a/Libraries/LibWeb/SVG/SVGSVGElement.idl b/Libraries/LibWeb/SVG/SVGSVGElement.idlindex 528b1f16cc0..29ea7d7a972 100644--- a/Libraries/LibWeb/SVG/SVGSVGElement.idl+++ b/Libraries/LibWeb/SVG/SVGSVGElement.idl@@ -36,3 +36,12 @@ interface SVGSVGElement : SVGGraphicsElement {};SVGSVGElement includes SVGFitToViewBox;++// https://svgwg.org/specs/animations/#InterfaceSVGSVGElement+partial interface SVGSVGElement {+ undefined pauseAnimations();+ undefined unpauseAnimations();+ boolean animationsPaused();+ float getCurrentTime();+ undefined setCurrentTime(float seconds);+};diff --git a/Tests/LibWeb/Text/expected/SVG/svg-element-animation-time-control.txt b/Tests/LibWeb/Text/expected/SVG/svg-element-animation-time-control.txtnew file mode 100644index 00000000000..e53d6a942fb--- /dev/null+++ b/Tests/LibWeb/Text/expected/SVG/svg-element-animation-time-control.txt@@ -0,0 +1,13 @@+time before the timeline begins: 0, animation 0+animation start time before the timeline begins: 0+paused before the timeline begins: true+time once the timeline begins: 0.5, animation 0.5+fills once the timeline begins: removed rgb(0, 128, 0), frozen rgb(0, 128, 0)+fills after seeking past the end: removed rgb(255, 0, 0), frozen rgb(0, 128, 0)+fills after seeking back: removed rgb(0, 128, 0), frozen rgb(0, 128, 0)+nested svg reports the timeline: paused true, time 0.25+nested svg leaves the timeline alone: paused true, time 0.25+time after seeking to a negative time: 0+detached animation: time 0, start time InvalidStateError+paused after unpausing: false+time advances after unpausing: truediff --git a/Tests/LibWeb/Text/expected/SVG/svg-element-animation-timeline-after-adoption.txt b/Tests/LibWeb/Text/expected/SVG/svg-element-animation-timeline-after-adoption.txtnew file mode 100644index 00000000000..3dece9565ed--- /dev/null+++ b/Tests/LibWeb/Text/expected/SVG/svg-element-animation-timeline-after-adoption.txt@@ -0,0 +1,2 @@+fill: rgb(0, 128, 0)+time advances: truediff --git a/Tests/LibWeb/Text/expected/SVG/svg-element-animation-timeline-after-move-before.txt b/Tests/LibWeb/Text/expected/SVG/svg-element-animation-timeline-after-move-before.txtnew file mode 100644index 00000000000..ef451993335--- /dev/null+++ b/Tests/LibWeb/Text/expected/SVG/svg-element-animation-timeline-after-move-before.txt@@ -0,0 +1,3 @@+fill: rgb(0, 128, 0)+time advances: true+paused: truediff --git a/Tests/LibWeb/Text/expected/wpt-import/svg/idlharness.window.txt b/Tests/LibWeb/Text/expected/wpt-import/svg/idlharness.window.txtindex 3669f2e4a03..78edfe8f4ab 100644--- a/Tests/LibWeb/Text/expected/wpt-import/svg/idlharness.window.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/svg/idlharness.window.txt@@ -2,8 +2,8 @@ Harness status: OKFound 1780 tests-1141 Pass-639 Fail+1158 Pass+622 FailPass idl_test setupPass idl_test validationPass Partial interface Document: original interface defined@@ -430,11 +430,11 @@ Pass SVGSVGElement interface: operation suspendRedraw(unsigned long)Pass SVGSVGElement interface: operation unsuspendRedraw(unsigned long)Pass SVGSVGElement interface: operation unsuspendRedrawAll()Pass SVGSVGElement interface: operation forceRedraw()-Fail SVGSVGElement interface: operation pauseAnimations()-Fail SVGSVGElement interface: operation unpauseAnimations()-Fail SVGSVGElement interface: operation animationsPaused()-Fail SVGSVGElement interface: operation getCurrentTime()-Fail SVGSVGElement interface: operation setCurrentTime(float)+Pass SVGSVGElement interface: operation pauseAnimations()+Pass SVGSVGElement interface: operation unpauseAnimations()+Pass SVGSVGElement interface: operation animationsPaused()+Pass SVGSVGElement interface: operation getCurrentTime()+Pass SVGSVGElement interface: operation setCurrentTime(float)Pass SVGSVGElement interface: attribute viewBoxFail SVGSVGElement interface: attribute preserveAspectRatioPass SVGSVGElement must be primary interface of objects.svg@@ -471,12 +471,12 @@ Pass SVGSVGElement interface: objects.svg must inherit property "unsuspendRedrawPass SVGSVGElement interface: calling unsuspendRedraw(unsigned long) on objects.svg with too few arguments must throw TypeErrorPass SVGSVGElement interface: objects.svg must inherit property "unsuspendRedrawAll()" with the proper typePass SVGSVGElement interface: objects.svg must inherit property "forceRedraw()" with the proper type-Fail SVGSVGElement interface: objects.svg must inherit property "pauseAnimations()" with the proper type-Fail SVGSVGElement interface: objects.svg must inherit property "unpauseAnimations()" with the proper type-Fail SVGSVGElement interface: objects.svg must inherit property "animationsPaused()" with the proper type-Fail SVGSVGElement interface: objects.svg must inherit property "getCurrentTime()" with the proper type-Fail SVGSVGElement interface: objects.svg must inherit property "setCurrentTime(float)" with the proper type-Fail SVGSVGElement interface: calling setCurrentTime(float) on objects.svg with too few arguments must throw TypeError+Pass SVGSVGElement interface: objects.svg must inherit property "pauseAnimations()" with the proper type+Pass SVGSVGElement interface: objects.svg must inherit property "unpauseAnimations()" with the proper type+Pass SVGSVGElement interface: objects.svg must inherit property "animationsPaused()" with the proper type+Pass SVGSVGElement interface: objects.svg must inherit property "getCurrentTime()" with the proper type+Pass SVGSVGElement interface: objects.svg must inherit property "setCurrentTime(float)" with the proper type+Pass SVGSVGElement interface: calling setCurrentTime(float) on objects.svg with too few arguments must throw TypeErrorPass SVGSVGElement interface: objects.svg must inherit property "viewBox" with the proper typeFail SVGSVGElement interface: objects.svg must inherit property "preserveAspectRatio" with the proper typePass SVGGraphicsElement interface: objects.svg must inherit property "transform" with the proper type@@ -1647,8 +1647,8 @@ Pass SVGAnimationElement interface: attribute targetElementPass SVGAnimationElement interface: attribute onbeginPass SVGAnimationElement interface: attribute onendPass SVGAnimationElement interface: attribute onrepeat-Fail SVGAnimationElement interface: operation getStartTime()-Fail SVGAnimationElement interface: operation getCurrentTime()+Pass SVGAnimationElement interface: operation getStartTime()+Pass SVGAnimationElement interface: operation getCurrentTime()Pass SVGAnimationElement interface: operation getSimpleDuration()Fail SVGAnimationElement interface: operation beginElement()Fail SVGAnimationElement interface: operation beginElementAt(float)@@ -1696,8 +1696,8 @@ Pass SVGAnimationElement interface: objects.set must inherit property "targetElePass SVGAnimationElement interface: objects.set must inherit property "onbegin" with the proper typePass SVGAnimationElement interface: objects.set must inherit property "onend" with the proper typePass SVGAnimationElement interface: objects.set must inherit property "onrepeat" with the proper type-Fail SVGAnimationElement interface: objects.set must inherit property "getStartTime()" with the proper type-Fail SVGAnimationElement interface: objects.set must inherit property "getCurrentTime()" with the proper type+Pass SVGAnimationElement interface: objects.set must inherit property "getStartTime()" with the proper type+Pass SVGAnimationElement interface: objects.set must inherit property "getCurrentTime()" with the proper typePass SVGAnimationElement interface: objects.set must inherit property "getSimpleDuration()" with the proper typeFail SVGAnimationElement interface: objects.set must inherit property "beginElement()" with the proper typeFail SVGAnimationElement interface: objects.set must inherit property "beginElementAt(float)" with the proper type@@ -1724,8 +1724,8 @@ Pass SVGAnimationElement interface: objects.animateMotion must inherit propertyPass SVGAnimationElement interface: objects.animateMotion must inherit property "onbegin" with the proper typePass SVGAnimationElement interface: objects.animateMotion must inherit property "onend" with the proper typePass SVGAnimationElement interface: objects.animateMotion must inherit property "onrepeat" with the proper type-Fail SVGAnimationElement interface: objects.animateMotion must inherit property "getStartTime()" with the proper type-Fail SVGAnimationElement interface: objects.animateMotion must inherit property "getCurrentTime()" with the proper type+Pass SVGAnimationElement interface: objects.animateMotion must inherit property "getStartTime()" with the proper type+Pass SVGAnimationElement interface: objects.animateMotion must inherit property "getCurrentTime()" with the proper typePass SVGAnimationElement interface: objects.animateMotion must inherit property "getSimpleDuration()" with the proper typeFail SVGAnimationElement interface: objects.animateMotion must inherit property "beginElement()" with the proper typeFail SVGAnimationElement interface: objects.animateMotion must inherit property "beginElementAt(float)" with the proper typediff --git a/Tests/LibWeb/Text/input/SVG/set-element-composite-order.html b/Tests/LibWeb/Text/input/SVG/set-element-composite-order.htmlindex 78b4f8a94dd..745469f1d51 100644--- a/Tests/LibWeb/Text/input/SVG/set-element-composite-order.html+++ b/Tests/LibWeb/Text/input/SVG/set-element-composite-order.html@@ -7,6 +7,7 @@<script>asyncTest(async done => {const nextFrame = () => new Promise(requestAnimationFrame);+ await new Promise(resolve => window.addEventListener("load", resolve));const createSet = to => {const set = document.createElementNS("http://www.w3.org/2000/svg", "set");set.setAttribute("attributeName", "fill");diff --git a/Tests/LibWeb/Text/input/SVG/set-element-dur-and-fill.html b/Tests/LibWeb/Text/input/SVG/set-element-dur-and-fill.htmlindex a4d1b8616f2..911da0805dc 100644--- a/Tests/LibWeb/Text/input/SVG/set-element-dur-and-fill.html+++ b/Tests/LibWeb/Text/input/SVG/set-element-dur-and-fill.html@@ -23,6 +23,7 @@<script>asyncTest(async done => {const nextFrame = () => new Promise(requestAnimationFrame);+ await new Promise(resolve => window.addEventListener("load", resolve));await nextFrame();await new Promise(resolve => setTimeout(resolve, 20));await nextFrame();diff --git a/Tests/LibWeb/Text/input/SVG/set-element-frozen-not-replaced.html b/Tests/LibWeb/Text/input/SVG/set-element-frozen-not-replaced.htmlindex 54f085d9933..f15b8cfe64a 100644--- a/Tests/LibWeb/Text/input/SVG/set-element-frozen-not-replaced.html+++ b/Tests/LibWeb/Text/input/SVG/set-element-frozen-not-replaced.html@@ -8,6 +8,7 @@<script>asyncTest(async done => {const nextFrame = () => new Promise(requestAnimationFrame);+ await new Promise(resolve => window.addEventListener("load", resolve));await nextFrame();await new Promise(resolve => setTimeout(resolve, 20));await nextFrame();diff --git a/Tests/LibWeb/Text/input/SVG/set-element-not-in-get-animations.html b/Tests/LibWeb/Text/input/SVG/set-element-not-in-get-animations.htmlindex a40df3cfa59..a05c37ef5e6 100644--- a/Tests/LibWeb/Text/input/SVG/set-element-not-in-get-animations.html+++ b/Tests/LibWeb/Text/input/SVG/set-element-not-in-get-animations.html@@ -8,6 +8,7 @@<script>asyncTest(async done => {const nextFrame = () => new Promise(requestAnimationFrame);+ await new Promise(resolve => window.addEventListener("load", resolve));await nextFrame();await nextFrame();println(`fill: ${getComputedStyle(rect).fill}`);diff --git a/Tests/LibWeb/Text/input/SVG/svg-element-animation-time-control.html b/Tests/LibWeb/Text/input/SVG/svg-element-animation-time-control.htmlnew file mode 100644index 00000000000..5e736bf110c--- /dev/null+++ b/Tests/LibWeb/Text/input/SVG/svg-element-animation-time-control.html@@ -0,0 +1,57 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<svg id="outer">+ <rect id="removed" width="10" height="10" fill="red">+ <set attributeName="fill" to="green" dur="1s"/>+ </rect>+ <rect id="frozen" width="10" height="10" fill="red">+ <set attributeName="fill" to="green" dur="1s" fill="freeze"/>+ </rect>+ <svg id="inner"></svg>+</svg>+<script>+ asyncTest(async done => {+ const nextFrame = () => new Promise(requestAnimationFrame);+ const fills = () => `removed ${getComputedStyle(removed).fill}, frozen ${getComputedStyle(frozen).fill}`;+ const animation = removed.firstElementChild;+ const detached = document.createElementNS("http://www.w3.org/2000/svg", "set");+ const startTime = element => {+ try {+ return element.getStartTime();+ } catch (error) {+ return error.name;+ }+ };++ println(`time before the timeline begins: ${outer.getCurrentTime()}, animation ${animation.getCurrentTime()}`);+ println(`animation start time before the timeline begins: ${startTime(animation)}`);+ outer.pauseAnimations();+ outer.setCurrentTime(0.5);+ println(`paused before the timeline begins: ${outer.animationsPaused()}`);+ await new Promise(resolve => window.addEventListener("load", resolve));+ await nextFrame();+ println(`time once the timeline begins: ${outer.getCurrentTime()}, animation ${animation.getCurrentTime()}`);+ println(`fills once the timeline begins: ${fills()}`);++ outer.setCurrentTime(1.5);+ println(`fills after seeking past the end: ${fills()}`);+ outer.setCurrentTime(0.25);+ println(`fills after seeking back: ${fills()}`);++ println(`nested svg reports the timeline: paused ${inner.animationsPaused()}, time ${inner.getCurrentTime()}`);+ inner.unpauseAnimations();+ inner.setCurrentTime(5);+ println(`nested svg leaves the timeline alone: paused ${outer.animationsPaused()}, time ${outer.getCurrentTime()}`);++ outer.setCurrentTime(-1);+ println(`time after seeking to a negative time: ${outer.getCurrentTime()}`);+ println(`detached animation: time ${detached.getCurrentTime()}, start time ${startTime(detached)}`);++ outer.unpauseAnimations();+ println(`paused after unpausing: ${outer.animationsPaused()}`);+ await nextFrame();+ await nextFrame();+ println(`time advances after unpausing: ${outer.getCurrentTime() > 0}`);+ done();+ });+</script>diff --git a/Tests/LibWeb/Text/input/SVG/svg-element-animation-timeline-after-adoption.html b/Tests/LibWeb/Text/input/SVG/svg-element-animation-timeline-after-adoption.htmlnew file mode 100644index 00000000000..5dbc9c94072--- /dev/null+++ b/Tests/LibWeb/Text/input/SVG/svg-element-animation-timeline-after-adoption.html@@ -0,0 +1,16 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ asyncTest(async done => {+ await new Promise(resolve => window.addEventListener("load", resolve));+ const otherDocument = document.implementation.createHTMLDocument();+ otherDocument.body.innerHTML = `<svg><rect width="10" height="10" fill="red"><set attributeName="fill" to="green" dur="10s"/></rect></svg>`;+ const svg = otherDocument.querySelector("svg");+ document.body.append(svg);+ await new Promise(requestAnimationFrame);+ await new Promise(requestAnimationFrame);+ println(`fill: ${getComputedStyle(svg.querySelector("rect")).fill}`);+ println(`time advances: ${svg.getCurrentTime() > 0}`);+ done();+ });+</script>diff --git a/Tests/LibWeb/Text/input/SVG/svg-element-animation-timeline-after-move-before.html b/Tests/LibWeb/Text/input/SVG/svg-element-animation-timeline-after-move-before.htmlnew file mode 100644index 00000000000..85ef98e0fcb--- /dev/null+++ b/Tests/LibWeb/Text/input/SVG/svg-element-animation-timeline-after-move-before.html@@ -0,0 +1,23 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<div id="container"></div>+<svg>+ <svg id="nested">+ <rect id="rect" width="10" height="10" fill="red"><set attributeName="fill" to="green" dur="10s"/></rect>+ </svg>+</svg>+<script>+ asyncTest(async done => {+ await new Promise(resolve => window.addEventListener("load", resolve));+ await new Promise(requestAnimationFrame);+ const nested = document.getElementById("nested");+ document.getElementById("container").moveBefore(nested, null);+ await new Promise(requestAnimationFrame);+ await new Promise(requestAnimationFrame);+ println(`fill: ${getComputedStyle(document.getElementById("rect")).fill}`);+ println(`time advances: ${nested.getCurrentTime() > 0}`);+ nested.pauseAnimations();+ println(`paused: ${nested.animationsPaused()}`);+ done();+ });+</script>