Skip to content

Truly independent web browser

LibWeb: Raise beginEvent, repeatEvent and endEvent for SVG animations

Andreas Kling1d2c5dd33217
diff --git a/Libraries/LibWeb/Animations/KeyframeEffect.cpp b/Libraries/LibWeb/Animations/KeyframeEffect.cpp
index b2c1abfd392..d3eb107ed3e 100644
--- a/Libraries/LibWeb/Animations/KeyframeEffect.cpp
+++ b/Libraries/LibWeb/Animations/KeyframeEffect.cpp
@@ -30,6 +30,8 @@
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/Layout/Node.h>
+#include <LibWeb/SVG/SMILAnimation.h>
+#include <LibWeb/SVG/SVGAnimationElement.h>
#include <LibWeb/WebIDL/ExceptionOr.h>
namespace Web::Animations {
@@ -1254,6 +1256,16 @@ static bool event_is_heard_at(DOM::Element& target, Hears const& hears)
// `only_iteration_events`.
bool KeyframeEffect::css_animation_events_are_heard(bool only_iteration_events) const
{
+ // NB: SMIL animations raise time events at their animation element instead, which do not bubble.
+ if (auto animation = associated_animation(); animation && animation->is_smil_animation()) {
+ auto& animation_element = as<SVG::SMILAnimation>(*animation).animation_element();
+ if (only_iteration_events)
+ return animation_element.has_event_listener(HTML::EventNames::repeatEvent);
+ return animation_element.has_event_listener(HTML::EventNames::beginEvent)
+ || animation_element.has_event_listener(HTML::EventNames::endEvent)
+ || animation_element.has_event_listener(HTML::EventNames::repeatEvent);
+ }
+
// Script animations do not dispatch CSS animation events, even when an ancestor listens for them.
if (auto animation = associated_animation(); animation && !animation->is_css_animation())
return false;
diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt
index 6fd9ef63f6e..e683bbbfc0c 100644
--- a/Libraries/LibWeb/CMakeLists.txt
+++ b/Libraries/LibWeb/CMakeLists.txt
@@ -941,6 +941,7 @@ set(SOURCES
SVG/SVGUseElement.cpp
SVG/SVGViewElement.cpp
SVG/TagNames.cpp
+ SVG/TimeEvent.cpp
TrustedTypes/InjectionSink.cpp
TrustedTypes/RequireTrustedTypesForDirective.cpp
TrustedTypes/TrustedHTML.cpp
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index 85c63371839..087631d86c9 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -247,6 +247,7 @@
#include <LibWeb/Platform/EventLoopPlugin.h>
#include <LibWeb/ResizeObserver/ResizeObserver.h>
#include <LibWeb/ResizeObserver/ResizeObserverEntry.h>
+#include <LibWeb/SVG/SMILAnimation.h>
#include <LibWeb/SVG/SMILTimeContainer.h>
#include <LibWeb/SVG/SVGAnimateMotionElement.h>
#include <LibWeb/SVG/SVGDecodedImageData.h>
@@ -4664,6 +4665,11 @@ void Document::dispatch_events_for_transition(GC::Ref<CSS::CSSTransition> transi
// https://www.w3.org/TR/css-animations-2/#event-dispatch
void Document::dispatch_events_for_animation_if_necessary(GC::Ref<Animations::Animation> animation)
{
+ if (animation->is_smil_animation()) {
+ as<SVG::SMILAnimation>(*animation).animation_element().queue_time_events_if_necessary();
+ return;
+ }
+
if (animation->is_css_transition()) {
dispatch_events_for_transition(as<CSS::CSSTransition>(*animation));
return;
diff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.h
index f72fd50391a..f7417e6c06e 100644
--- a/Libraries/LibWeb/Forward.h
+++ b/Libraries/LibWeb/Forward.h
@@ -1157,6 +1157,7 @@ class SVGSymbolElement;
class SVGTitleElement;
class SVGTransformList;
class SVGViewElement;
+class TimeEvent;
}
diff --git a/Libraries/LibWeb/SVG/SVGAnimationElement.cpp b/Libraries/LibWeb/SVG/SVGAnimationElement.cpp
index eb383d9744f..00482a6be68 100644
--- a/Libraries/LibWeb/SVG/SVGAnimationElement.cpp
+++ b/Libraries/LibWeb/SVG/SVGAnimationElement.cpp
@@ -13,12 +13,15 @@
#include <LibWeb/DOM/Document.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/Scripting/Environments.h>
+#include <LibWeb/HTML/Window.h>
+#include <LibWeb/HighResolutionTime/TimeOrigin.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/SVG/TimeEvent.h>
#include <LibWeb/WebIDL/DOMException.h>
namespace Web::SVG {
@@ -320,6 +323,10 @@ void SVGAnimationElement::update_animation()
animation->set_effect(effect);
time_container->add_animation(animation);
m_animation = animation;
+
+ // NB: Time events are raised for intervals that begin from now on.
+ if (!m_time_events_last_checked_time.has_value())
+ m_time_events_last_checked_time = current_presentation_time();
}
// https://svgwg.org/specs/animations/#__svg__SVGAnimationElement__onbegin
@@ -477,4 +484,79 @@ void SVGAnimationElement::end_element_at(float offset)
update_animation();
}
+void SVGAnimationElement::queue_time_event(Utf16FlyString const& type, i32 detail)
+{
+ // NB: Time events are dispatched along with the other animation events of the document.
+ auto& document = this->document();
+ auto event = TimeEvent::create(type, document.window() ? document.window()->window().ptr() : nullptr, detail,
+ HighResolutionTime::current_high_resolution_time(HTML::relevant_global_object(*this)));
+ event->set_is_trusted(true);
+
+ auto timeline = m_animation->timeline();
+ document.append_pending_animation_event({
+ .event = event,
+ .animation = *m_animation,
+ .target = *this,
+ .scheduled_event_time = timeline && timeline->can_convert_a_timeline_time_to_an_origin_relative_time()
+ ? timeline->convert_a_timeline_time_to_an_origin_relative_time(timeline->current_time())
+ : Optional<double> {},
+ });
+}
+
+// https://svgwg.org/specs/animations/#InterfaceTimeEvent
+void SVGAnimationElement::queue_time_events_if_necessary()
+{
+ // FIXME: Raise time events for an animation element without a target or animated property too.
+ auto time_container = is_connected() ? this->time_container() : nullptr;
+ if (!m_animation || !time_container || !time_container->has_begun())
+ return;
+
+ auto current_time = time_container->current_time_in_milliseconds();
+ auto last_checked_time = exchange(m_time_events_last_checked_time, current_time);
+ auto begin_time = interval_begin_time();
+ auto is_active = is_active_at(current_time);
+
+ // endEvent
+ // This event is raised at the active end of the element. Note that this event is not raised at the simple end
+ // of each repeat.
+ // Note that if an element is restarted while it is currently playing, the element will raise an end event and
+ // another begin event, as the element restarts.
+ if (m_time_events_reported_active && (!is_active || begin_time != m_time_events_begin_time)) {
+ m_time_events_reported_active = false;
+ queue_time_event(HTML::EventNames::endEvent);
+ }
+
+ // beginEvent
+ // This event is raised when the element local timeline begins to play. It will be raised each time the element
+ // begins the active duration (i.e. when it restarts, but not when it repeats).
+ if (is_active && !m_time_events_reported_active) {
+ m_time_events_reported_active = true;
+ m_time_events_begin_time = begin_time;
+ m_time_events_iteration = isinf(simple_duration()) ? 0 : floor((current_time - *begin_time) / simple_duration());
+ queue_time_event(HTML::EventNames::beginEvent);
+ return;
+ }
+
+ // NB: An interval that began and ended since the time events were last checked raises both events.
+ if (!is_active && begin_time.has_value() && begin_time != m_time_events_begin_time && last_checked_time.has_value()
+ && *begin_time >= *last_checked_time && *begin_time <= current_time) {
+ m_time_events_begin_time = begin_time;
+ queue_time_event(HTML::EventNames::beginEvent);
+ queue_time_event(HTML::EventNames::endEvent);
+ return;
+ }
+
+ // repeatEvent
+ // This event is raised when an element local timeline repeats. It will be raised each time the element
+ // repeats, after the first iteration. The event provides a numerical indication of which repeat iteration is
+ // beginning. The value is a 0-based integer, but the repeat event is not raised for the first iteration and
+ // so the observed values of the detail attribute will be >= 1.
+ if (is_active && !isinf(simple_duration())) {
+ auto iteration = floor((current_time - *begin_time) / simple_duration());
+ auto previous_iteration = exchange(m_time_events_iteration, iteration);
+ if (iteration > previous_iteration)
+ queue_time_event(HTML::EventNames::repeatEvent, static_cast<i32>(min(iteration, static_cast<double>(NumericLimits<i32>::max()))));
+ }
+}
+
}
diff --git a/Libraries/LibWeb/SVG/SVGAnimationElement.h b/Libraries/LibWeb/SVG/SVGAnimationElement.h
index 103bfc6927c..59a934ee743 100644
--- a/Libraries/LibWeb/SVG/SVGAnimationElement.h
+++ b/Libraries/LibWeb/SVG/SVGAnimationElement.h
@@ -38,6 +38,8 @@ public:
void end_element();
void end_element_at(float offset);
+ void queue_time_events_if_necessary();
+
protected:
SVGAnimationElement(DOM::Document&, DOM::QualifiedName);
@@ -73,6 +75,7 @@ private:
Optional<double> interval_begin_time() const;
Optional<double> interval_end_time() const;
bool is_active_at(double time) const;
+ void queue_time_event(Utf16FlyString const& type, i32 detail = 0);
GC::Ptr<SMILAnimation> m_animation;
@@ -80,6 +83,12 @@ private:
// fragment.
Optional<double> m_begin_instance_time_from_dom;
Optional<double> m_end_instance_time_from_dom;
+
+ // NB: What the time events that were last queued said about the interval, and when they were queued.
+ bool m_time_events_reported_active { false };
+ Optional<double> m_time_events_begin_time;
+ double m_time_events_iteration { 0 };
+ Optional<double> m_time_events_last_checked_time;
};
}
diff --git a/Libraries/LibWeb/SVG/TimeEvent.cpp b/Libraries/LibWeb/SVG/TimeEvent.cpp
new file mode 100644
index 00000000000..baeb6c59c29
--- /dev/null
+++ b/Libraries/LibWeb/SVG/TimeEvent.cpp
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibGC/Heap.h>
+#include <LibWeb/SVG/TimeEvent.h>
+
+namespace Web::SVG {
+
+GC_DEFINE_ALLOCATOR(TimeEvent);
+
+GC::Ref<TimeEvent> TimeEvent::create(Utf16FlyString const& type, GC::Ptr<HTML::WindowProxy> view, i32 detail, HighResolutionTime::DOMHighResTimeStamp time_stamp)
+{
+ return GC::Heap::the().allocate<TimeEvent>(type, view, detail, time_stamp);
+}
+
+TimeEvent::TimeEvent(Utf16FlyString const& type, GC::Ptr<HTML::WindowProxy> view, i32 detail, HighResolutionTime::DOMHighResTimeStamp time_stamp)
+ : Event(type, time_stamp)
+ , m_view(view)
+ , m_detail(detail)
+{
+}
+
+void TimeEvent::visit_edges(GC::Cell::Visitor& visitor)
+{
+ Base::visit_edges(visitor);
+ visitor.visit(m_view);
+}
+
+// https://svgwg.org/specs/animations/#__svg__TimeEvent__initTimeEvent
+void TimeEvent::init_time_event(Utf16FlyString const& type, GC::Ptr<HTML::WindowProxy> view, i32 detail)
+{
+ // The initTimeEvent method is used to initialize the value of a TimeEvent created with document.createEvent(). This
+ // method may only be called before the TimeEvent has been dispatched via the dispatchEvent method, though it may be
+ // called multiple times during that phase if necessary. If called multiple times, the final invocation takes
+ // precedence.
+ if (dispatched())
+ return;
+
+ // NB: Time events neither bubble nor are cancelable.
+ initialize_event(type, false, false);
+ m_view = view;
+ m_detail = detail;
+}
+
+}
diff --git a/Libraries/LibWeb/SVG/TimeEvent.h b/Libraries/LibWeb/SVG/TimeEvent.h
new file mode 100644
index 00000000000..e5084365e4a
--- /dev/null
+++ b/Libraries/LibWeb/SVG/TimeEvent.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <LibWeb/DOM/Event.h>
+#include <LibWeb/HTML/WindowProxy.h>
+
+namespace Web::SVG {
+
+// https://svgwg.org/specs/animations/#InterfaceTimeEvent
+class TimeEvent final : public DOM::Event {
+ WEB_WRAPPABLE(TimeEvent, DOM::Event);
+ GC_DECLARE_ALLOCATOR(TimeEvent);
+
+public:
+ [[nodiscard]] static GC::Ref<TimeEvent> create(Utf16FlyString const& type, GC::Ptr<HTML::WindowProxy> view, i32 detail, HighResolutionTime::DOMHighResTimeStamp);
+
+ GC::Ptr<HTML::WindowProxy> view() const { return m_view; }
+ i32 detail() const { return m_detail; }
+
+ void init_time_event(Utf16FlyString const& type, GC::Ptr<HTML::WindowProxy> view, i32 detail);
+
+private:
+ TimeEvent(Utf16FlyString const& type, GC::Ptr<HTML::WindowProxy> view, i32 detail, HighResolutionTime::DOMHighResTimeStamp);
+
+ virtual void visit_edges(GC::Cell::Visitor&) override;
+
+ GC::Ptr<HTML::WindowProxy> m_view;
+ i32 m_detail { 0 };
+};
+
+}
diff --git a/Libraries/LibWeb/SVG/TimeEvent.idl b/Libraries/LibWeb/SVG/TimeEvent.idl
new file mode 100644
index 00000000000..99be9ffc02d
--- /dev/null
+++ b/Libraries/LibWeb/SVG/TimeEvent.idl
@@ -0,0 +1,8 @@
+// https://svgwg.org/specs/animations/#InterfaceTimeEvent
+[Exposed=Window]
+interface TimeEvent : Event {
+ readonly attribute WindowProxy? view;
+ readonly attribute long detail;
+
+ [ImplementedAs=init_time_event] undefined initTimeEvent([FlyString] DOMString typeArg, optional WindowProxy? viewArg = null, optional long detailArg = 0);
+};
diff --git a/Libraries/LibWeb/idl_files.cmake b/Libraries/LibWeb/idl_files.cmake
index f2b77d382c7..3829acc0e26 100644
--- a/Libraries/LibWeb/idl_files.cmake
+++ b/Libraries/LibWeb/idl_files.cmake
@@ -597,6 +597,7 @@ libweb_js_bindings(SVG/SVGUnitTypes)
libweb_js_bindings(SVG/SVGURIReference)
libweb_js_bindings(SVG/SVGUseElement)
libweb_js_bindings(SVG/SVGViewElement)
+libweb_js_bindings(SVG/TimeEvent)
libweb_js_bindings(TrustedTypes/TrustedHTML)
libweb_js_bindings(TrustedTypes/TrustedHTMLParserOptions)
libweb_js_bindings(TrustedTypes/TrustedScript)
diff --git a/Tests/LibWeb/Text/expected/SVG/animation-element-time-events.txt b/Tests/LibWeb/Text/expected/SVG/animation-element-time-events.txt
new file mode 100644
index 00000000000..fb817bcf1cf
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/SVG/animation-element-time-events.txt
@@ -0,0 +1,24 @@
+beginElement()
+ repeating: beginEvent (TimeEvent, detail 0, bubbles false, cancelable false, trusted true, view is window true)
+seek into the second iteration
+ repeating: repeatEvent (TimeEvent, detail 1, bubbles false, cancelable false, trusted true, view is window true)
+ repeating: onrepeat
+seek into the third iteration
+ repeating: repeatEvent (TimeEvent, detail 2, bubbles false, cancelable false, trusted true, view is window true)
+ repeating: onrepeat
+seek back into the first iteration
+seek into the second iteration again
+ repeating: repeatEvent (TimeEvent, detail 1, bubbles false, cancelable false, trusted true, view is window true)
+ repeating: onrepeat
+restart while active
+ repeating: endEvent (TimeEvent, detail 0, bubbles false, cancelable false, trusted true, view is window true)
+ repeating: beginEvent (TimeEvent, detail 0, bubbles false, cancelable false, trusted true, view is window true)
+endElement()
+ repeating: endEvent (TimeEvent, detail 0, bubbles false, cancelable false, trusted true, view is window true)
+begin again
+ repeating: beginEvent (TimeEvent, detail 0, bubbles false, cancelable false, trusted true, view is window true)
+seek past the active end
+ repeating: endEvent (TimeEvent, detail 0, bubbles false, cancelable false, trusted true, view is window true)
+interval that begins and ends between frames
+ short: beginEvent (TimeEvent, detail 0, bubbles false, cancelable false, trusted true, view is window true)
+ short: endEvent (TimeEvent, detail 0, bubbles false, cancelable false, trusted true, view is window true)
diff --git a/Tests/LibWeb/Text/expected/all-window-properties.txt b/Tests/LibWeb/Text/expected/all-window-properties.txt
index 24f10f0bfef..dfce470327e 100644
--- a/Tests/LibWeb/Text/expected/all-window-properties.txt
+++ b/Tests/LibWeb/Text/expected/all-window-properties.txt
@@ -557,6 +557,7 @@ TextTrack
TextTrackCue
TextTrackCueList
TextTrackList
+TimeEvent
TimeRanges
ToggleEvent
Touch
diff --git a/Tests/LibWeb/Text/expected/wpt-import/svg/idlharness.window.txt b/Tests/LibWeb/Text/expected/wpt-import/svg/idlharness.window.txt
index 0d4b55c3ce0..6a20e69a028 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: OK
Found 1780 tests
-1632 Pass
-148 Fail
+1641 Pass
+139 Fail
Pass idl_test setup
Pass idl_test validation
Pass Partial interface Document: original interface defined
@@ -1628,15 +1628,15 @@ Pass SVGElement interface: objects.view must inherit property "ownerSVGElement"
Pass SVGElement interface: objects.view must inherit property "viewportElement" with the proper type
Fail SVGElement interface: objects.view must inherit property "correspondingElement" with the proper type
Fail SVGElement interface: objects.view must inherit property "correspondingUseElement" with the proper type
-Fail TimeEvent interface: existence and properties of interface object
-Fail TimeEvent interface object length
-Fail TimeEvent interface object name
-Fail TimeEvent interface: existence and properties of interface prototype object
-Fail TimeEvent interface: existence and properties of interface prototype object's "constructor" property
-Fail TimeEvent interface: existence and properties of interface prototype object's @@unscopables property
-Fail TimeEvent interface: attribute view
-Fail TimeEvent interface: attribute detail
-Fail TimeEvent interface: operation initTimeEvent(DOMString, optional Window?, optional long)
+Pass TimeEvent interface: existence and properties of interface object
+Pass TimeEvent interface object length
+Pass TimeEvent interface object name
+Pass TimeEvent interface: existence and properties of interface prototype object
+Pass TimeEvent interface: existence and properties of interface prototype object's "constructor" property
+Pass TimeEvent interface: existence and properties of interface prototype object's @@unscopables property
+Pass TimeEvent interface: attribute view
+Pass TimeEvent interface: attribute detail
+Pass TimeEvent interface: operation initTimeEvent(DOMString, optional Window?, optional long)
Pass SVGAnimationElement interface: existence and properties of interface object
Pass SVGAnimationElement interface object length
Pass SVGAnimationElement interface object name
diff --git a/Tests/LibWeb/Text/input/SVG/animation-element-time-events.html b/Tests/LibWeb/Text/input/SVG/animation-element-time-events.html
new file mode 100644
index 00000000000..ece610ade53
--- /dev/null
+++ b/Tests/LibWeb/Text/input/SVG/animation-element-time-events.html
@@ -0,0 +1,49 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<svg id="svg" width="100" height="100">
+ <rect width="10" height="10">
+ <animate id="repeating" attributeName="opacity" from="0" to="1" begin="indefinite" dur="1s" repeatCount="3"/>
+ </rect>
+ <rect width="10" height="10">
+ <animate id="short" attributeName="opacity" from="0" to="1" begin="indefinite" dur="1ms"/>
+ </rect>
+</svg>
+<script>
+ asyncTest(async done => {
+ const nextFrame = () => new Promise(requestAnimationFrame);
+ for (const animation of document.querySelectorAll("animate")) {
+ for (const type of ["beginEvent", "repeatEvent", "endEvent"]) {
+ animation.addEventListener(type, event => {
+ println(` ${animation.id}: ${event.type} (${event.constructor.name}, detail ${event.detail}, bubbles ${event.bubbles}, cancelable ${event.cancelable}, trusted ${event.isTrusted}, view is window ${event.view === window})`);
+ });
+ }
+ }
+ repeating.onrepeat = () => println(` repeating: onrepeat`);
+ const step = async (label, action) => {
+ println(label);
+ action();
+ await nextFrame();
+ await nextFrame();
+ };
+
+ await new Promise(resolve => window.addEventListener("load", resolve));
+ await nextFrame();
+ svg.pauseAnimations();
+ svg.setCurrentTime(1);
+
+ await step("beginElement()", () => repeating.beginElement());
+ await step("seek into the second iteration", () => svg.setCurrentTime(2.5));
+ await step("seek into the third iteration", () => svg.setCurrentTime(3.5));
+ await step("seek back into the first iteration", () => svg.setCurrentTime(1.5));
+ await step("seek into the second iteration again", () => svg.setCurrentTime(2.5));
+ await step("restart while active", () => repeating.beginElement());
+ await step("endElement()", () => repeating.endElement());
+ await step("begin again", () => repeating.beginElement());
+ await step("seek past the active end", () => svg.setCurrentTime(10));
+ await step("interval that begins and ends between frames", () => {
+ short.beginElement();
+ svg.setCurrentTime(11);
+ });
+ done();
+ });
+</script>