Skip to content

Truly independent web browser

LibWeb: Repeat SVG animations as many times as their repeatCount says

Jelle Raaijmakers09cb46e923ba
diff --git a/Libraries/LibWeb/SVG/SVGAnimationElement.cpp b/Libraries/LibWeb/SVG/SVGAnimationElement.cpp
index 0b8c1fd4c86..04264764b52 100644
--- a/Libraries/LibWeb/SVG/SVGAnimationElement.cpp
+++ b/Libraries/LibWeb/SVG/SVGAnimationElement.cpp
@@ -55,7 +55,7 @@ void SVGAnimationElement::moved_from(IsSubtreeRoot is_subtree_root, GC::Ptr<Node
void SVGAnimationElement::attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_)
{
Base::attribute_changed(name, old_value, value, namespace_);
- if (first_is_one_of(name, AttributeNames::attributeName, AttributeNames::dur, AttributeNames::fill) && !namespace_.has_value())
+ if (first_is_one_of(name, AttributeNames::attributeName, AttributeNames::dur, AttributeNames::fill, AttributeNames::repeatCount) && !namespace_.has_value())
update_animation();
}
@@ -112,6 +112,32 @@ double SVGAnimationElement::simple_duration() const
return AK::Infinity<double>;
}
+// https://svgwg.org/specs/animations/#RepeatCountAttribute
+double SVGAnimationElement::repeat_count() const
+{
+ // Specifies the number of iterations of the animation function. It can have the following attribute values:
+ auto repeat_count = get_attribute(AttributeNames::repeatCount);
+ if (!repeat_count.has_value())
+ return 1;
+
+ // <number>
+ // This is a (base 10) "floating point" numeric value that specifies the number of iterations. It can include
+ // partial iterations expressed as fraction values. A fractional value describes a portion of the simple
+ // duration. Values must be greater than 0.
+ if (auto count = parse_number_percentage(*repeat_count); count.has_value() && !count->is_percentage()
+ && count->value() > 0 && isfinite(count->value())) {
+ return count->value();
+ }
+
+ // indefinite
+ // The animation is defined to repeat indefinitely (i.e. until the document ends).
+ if (*repeat_count == "indefinite"sv)
+ return AK::Infinity<double>;
+
+ // NB: SMIL Animation ignores a value with any error in its syntax, which leaves a single iteration.
+ return 1;
+}
+
// https://svgwg.org/specs/animations/#FillAttribute
Bindings::FillMode SVGAnimationElement::fill_mode() const
{
@@ -161,6 +187,7 @@ void SVGAnimationElement::update_animation()
// FIXME: A running animation requests a rendering update on every frame, even when its output cannot change, as
// with an indefinite one. This keeps the rendering loop running for as long as such an animation is active.
auto effect = MUST(Animations::KeyframeEffect::create_from_processed_keyframes(target, move(keyframes), simple_duration()));
+ effect->set_iteration_count(repeat_count());
effect->set_fill_mode(fill_mode());
auto animation = SMILAnimation::create(*this, *time_container);
diff --git a/Libraries/LibWeb/SVG/SVGAnimationElement.h b/Libraries/LibWeb/SVG/SVGAnimationElement.h
index 9f2de3f62ed..662afc295c6 100644
--- a/Libraries/LibWeb/SVG/SVGAnimationElement.h
+++ b/Libraries/LibWeb/SVG/SVGAnimationElement.h
@@ -51,6 +51,7 @@ protected:
private:
GC::Ptr<SMILTimeContainer> time_container();
double simple_duration() const;
+ double repeat_count() const;
Bindings::FillMode fill_mode() const;
GC::Ptr<SMILAnimation> m_animation;
diff --git a/Tests/LibWeb/Text/expected/SVG/animation-element-repeat-count.txt b/Tests/LibWeb/Text/expected/SVG/animation-element-repeat-count.txt
new file mode 100644
index 00000000000..96be13e41dc
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/SVG/animation-element-repeat-count.txt
@@ -0,0 +1,50 @@
+t=0.25
+ unspecified: 25.0
+ two: 25.0
+ fraction-frozen: 25.0
+ indefinite: 25.0
+ zero: 25.0
+ negative: 25.0
+ percentage: 25.0
+ too-large: 25.0
+ changed: 25.0
+t=1.25
+ unspecified: 0.0
+ two: 25.0
+ fraction-frozen: 25.0
+ indefinite: 25.0
+ zero: 0.0
+ negative: 0.0
+ percentage: 0.0
+ too-large: 0.0
+ changed: 25.0
+t=2.25
+ unspecified: 0.0
+ two: 0.0
+ fraction-frozen: 25.0
+ indefinite: 25.0
+ zero: 0.0
+ negative: 0.0
+ percentage: 0.0
+ too-large: 0.0
+ changed: 25.0
+t=2.75
+ unspecified: 0.0
+ two: 0.0
+ fraction-frozen: 50.0
+ indefinite: 75.0
+ zero: 0.0
+ negative: 0.0
+ percentage: 0.0
+ too-large: 0.0
+ changed: 75.0
+t=10.25
+ unspecified: 0.0
+ two: 0.0
+ fraction-frozen: 50.0
+ indefinite: 25.0
+ zero: 0.0
+ negative: 0.0
+ percentage: 0.0
+ too-large: 0.0
+ changed: 0.0
diff --git a/Tests/LibWeb/Text/input/SVG/animation-element-repeat-count.html b/Tests/LibWeb/Text/input/SVG/animation-element-repeat-count.html
new file mode 100644
index 00000000000..685f32b7244
--- /dev/null
+++ b/Tests/LibWeb/Text/input/SVG/animation-element-repeat-count.html
@@ -0,0 +1,31 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<svg id="svg" width="400" height="400">
+ <rect id="unspecified" width="10" height="10"><animateMotion from="0,0" to="100,0" dur="1s"/></rect>
+ <rect id="two" width="10" height="10"><animateMotion from="0,0" to="100,0" dur="1s" repeatCount="2"/></rect>
+ <rect id="fraction-frozen" width="10" height="10"><animateMotion from="0,0" to="100,0" dur="1s" repeatCount="2.5" fill="freeze"/></rect>
+ <rect id="indefinite" width="10" height="10"><animateMotion from="0,0" to="100,0" dur="1s" repeatCount="indefinite"/></rect>
+ <rect id="zero" width="10" height="10"><animateMotion from="0,0" to="100,0" dur="1s" repeatCount="0"/></rect>
+ <rect id="negative" width="10" height="10"><animateMotion from="0,0" to="100,0" dur="1s" repeatCount="-2"/></rect>
+ <rect id="percentage" width="10" height="10"><animateMotion from="0,0" to="100,0" dur="1s" repeatCount="200%"/></rect>
+ <rect id="too-large" width="10" height="10"><animateMotion from="0,0" to="100,0" dur="1s" repeatCount="1e+309"/></rect>
+ <rect id="changed" width="10" height="10"><animateMotion from="0,0" to="100,0" dur="1s" repeatCount="indefinite"/></rect>
+</svg>
+<script>
+ asyncTest(async done => {
+ await new Promise(resolve => window.addEventListener("load", resolve));
+ await new Promise(requestAnimationFrame);
+ svg.pauseAnimations();
+ document.querySelector("#changed animateMotion").setAttribute("repeatCount", "3");
+ const origin = svg.getBoundingClientRect();
+ for (const time of [0.25, 1.25, 2.25, 2.75, 10.25]) {
+ svg.setCurrentTime(time);
+ println(`t=${time}`);
+ for (const rect of document.querySelectorAll("rect")) {
+ const box = rect.getBoundingClientRect();
+ println(` ${rect.id}: ${(box.left - origin.left).toFixed(1)}`);
+ }
+ }
+ done();
+ });
+</script>