Skip to content

Truly independent web browser

LibWeb: Move <animateMotion> targets between its from and to values

Jelle Raaijmakers37fe2da62ac1
diff --git a/Libraries/LibWeb/SVG/AttributeNames.h b/Libraries/LibWeb/SVG/AttributeNames.h
index 3beeeb3241a..eabb2bc6531 100644
--- a/Libraries/LibWeb/SVG/AttributeNames.h
+++ b/Libraries/LibWeb/SVG/AttributeNames.h
@@ -35,6 +35,7 @@ namespace Web::SVG::AttributeNames {
__ENUMERATE_SVG_ATTRIBUTE(exponent, "exponent") \
__ENUMERATE_SVG_ATTRIBUTE(filterUnits, "filterUnits") \
__ENUMERATE_SVG_ATTRIBUTE(fr, "fr") \
+ __ENUMERATE_SVG_ATTRIBUTE(from, "from") \
__ENUMERATE_SVG_ATTRIBUTE(fx, "fx") \
__ENUMERATE_SVG_ATTRIBUTE(fy, "fy") \
__ENUMERATE_SVG_ATTRIBUTE(glyphRef, "glyphRef") \
diff --git a/Libraries/LibWeb/SVG/SVGAnimateMotionElement.cpp b/Libraries/LibWeb/SVG/SVGAnimateMotionElement.cpp
index 17eb5fcdf14..565dda19d99 100644
--- a/Libraries/LibWeb/SVG/SVGAnimateMotionElement.cpp
+++ b/Libraries/LibWeb/SVG/SVGAnimateMotionElement.cpp
@@ -4,6 +4,9 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/CharacterTypes.h>
+#include <LibWeb/CSS/StyleValues/StyleValue.h>
+#include <LibWeb/SVG/AttributeNames.h>
#include <LibWeb/SVG/SVGAnimateMotionElement.h>
namespace Web::SVG {
@@ -15,11 +18,71 @@ SVGAnimateMotionElement::SVGAnimateMotionElement(DOM::Document& document, DOM::Q
{
}
+void SVGAnimateMotionElement::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::from, AttributeNames::to) && !namespace_.has_value())
+ update_animation();
+}
+
+// https://svgwg.org/specs/animations/#AnimateMotionElementPathAttribute
+Optional<CSS::PropertyID> SVGAnimateMotionElement::target_property() const
+{
+ // The effect of a motion path animation is to add a supplemental transformation matrix onto the CTM for the
+ // referenced object which causes a translation along the x- and y-axes of the current user coordinate system by the
+ // computed X and Y values computed over time.
+ // AD-HOC: Animate the translate property instead, which translates the target in the coordinate system of its
+ // parent, outside of its own transform, like the supplemental transformation matrix does. Unlike that
+ // matrix, it replaces any translate property of the target.
+ return CSS::PropertyID::Translate;
+}
+
// https://svgwg.org/specs/animations/#AnimateMotionElement
-Vector<Animations::BaseKeyframe> SVGAnimateMotionElement::keyframes(SVGElement const&, CSS::PropertyID) const
+static RefPtr<CSS::StyleValue const> parse_coordinate_pair_as_translation(SVGElement const& target, Utf16View coordinate_pair)
{
- // FIXME: The ‘animateMotion’ element causes a referenced element to move along a motion path.
- return {};
+ // For ‘animateMotion’, the specified values for ‘from’, ‘by’, ‘to’ and ‘values’ consists of x, y coordinate pairs,
+ // with a single comma and/or white space separating the x coordinate from the y coordinate.
+ auto is_separator = [](char16_t code_unit) { return code_unit == ',' || is_ascii_space(code_unit); };
+ coordinate_pair = coordinate_pair.trim_ascii_whitespace();
+ size_t x_length = 0;
+ while (x_length < coordinate_pair.length_in_code_units() && !is_separator(coordinate_pair.code_unit_at(x_length)))
+ ++x_length;
+ auto x = coordinate_pair.substring_view(0, x_length);
+ auto y = coordinate_pair.substring_view(x_length).trim_ascii_whitespace(TrimMode::Left);
+ if (!y.is_empty() && y.code_unit_at(0) == ',')
+ y = y.substring_view(1).trim_ascii_whitespace(TrimMode::Left);
+ if (x.is_empty() || y.is_empty())
+ return {};
+ for (size_t i = 0; i < y.length_in_code_units(); ++i) {
+ if (is_separator(y.code_unit_at(i)))
+ return {};
+ }
+
+ // Each coordinate represents a <length>.
+ // NB: Parse the coordinates as a translate value the way a presentation attribute is parsed, which takes a number
+ // as a length in user units.
+ return target.parse_presentation_attribute(CSS::PropertyID::Translate, Utf16String::formatted("{} {}", x, y));
+}
+
+// https://svgwg.org/specs/animations/#AnimateMotionElement
+Vector<Animations::BaseKeyframe> SVGAnimateMotionElement::keyframes(SVGElement const& target, CSS::PropertyID property_id) const
+{
+ // FIXME: Take the motion path from the by and values attributes, the path attribute or an mpath child element too.
+ // FIXME: Apply the keyPoints and rotate attributes.
+ auto to = get_attribute(AttributeNames::to);
+ auto to_translation = to.has_value() ? parse_coordinate_pair_as_translation(target, *to) : nullptr;
+ if (!to_translation)
+ return {};
+
+ // NB: Without a from value, this is a to animation, which animates from the underlying value. The keyframe effect
+ // takes its missing start keyframe from that value.
+ auto from = get_attribute(AttributeNames::from);
+ if (!from.has_value())
+ return { keyframe(1, property_id, *to_translation) };
+ auto from_translation = parse_coordinate_pair_as_translation(target, *from);
+ if (!from_translation)
+ return {};
+ return { keyframe(0, property_id, *from_translation), keyframe(1, property_id, *to_translation) };
}
}
diff --git a/Libraries/LibWeb/SVG/SVGAnimateMotionElement.h b/Libraries/LibWeb/SVG/SVGAnimateMotionElement.h
index a039639d058..39404b9cd0d 100644
--- a/Libraries/LibWeb/SVG/SVGAnimateMotionElement.h
+++ b/Libraries/LibWeb/SVG/SVGAnimateMotionElement.h
@@ -17,6 +17,9 @@ class SVGAnimateMotionElement final : public SVGAnimationElement {
private:
SVGAnimateMotionElement(DOM::Document&, DOM::QualifiedName);
+ virtual void attribute_changed(Utf16FlyString const& name, Optional<Utf16String> const& old_value, Optional<Utf16String> const& value, Optional<Utf16FlyString> const& namespace_) override;
+
+ virtual Optional<CSS::PropertyID> target_property() const override;
virtual Vector<Animations::BaseKeyframe> keyframes(SVGElement const& target, CSS::PropertyID) const override;
};
diff --git a/Tests/LibWeb/Text/expected/SVG/animate-motion-element-coordinate-pairs.txt b/Tests/LibWeb/Text/expected/SVG/animate-motion-element-coordinate-pairs.txt
new file mode 100644
index 00000000000..b087021a44a
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/SVG/animate-motion-element-coordinate-pairs.txt
@@ -0,0 +1,9 @@
+comma: 20, 30
+whitespace: 20, 30
+comma-and-whitespace: 20, 30
+units: 96, 0
+to-only: 20, 0
+two-commas: 0, 0
+three-coordinates: 0, 0
+one-coordinate: 0, 0
+invalid-from: 0, 0
diff --git a/Tests/LibWeb/Text/expected/wpt-import/svg/animations/animateMotion-fill-freeze.txt b/Tests/LibWeb/Text/expected/wpt-import/svg/animations/animateMotion-fill-freeze.txt
new file mode 100644
index 00000000000..de71e671413
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/svg/animations/animateMotion-fill-freeze.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Test for animation freeze when repeatDur is not a multiple of dur
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/svg/animations/animateMotion-fill-remove.txt b/Tests/LibWeb/Text/expected/wpt-import/svg/animations/animateMotion-fill-remove.txt
new file mode 100644
index 00000000000..de71e671413
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/svg/animations/animateMotion-fill-remove.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass Test for animation freeze when repeatDur is not a multiple of dur
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/SVG/animate-motion-element-coordinate-pairs.html b/Tests/LibWeb/Text/input/SVG/animate-motion-element-coordinate-pairs.html
new file mode 100644
index 00000000000..f067a2820d9
--- /dev/null
+++ b/Tests/LibWeb/Text/input/SVG/animate-motion-element-coordinate-pairs.html
@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<svg id="svg" width="400" height="200">
+ <rect id="comma" width="10" height="10"><animateMotion from="10,20" to="30,40" dur="2s"/></rect>
+ <rect id="whitespace" width="10" height="10"><animateMotion from=" 10 20 " to="30 40" dur="2s"/></rect>
+ <rect id="comma-and-whitespace" width="10" height="10"><animateMotion from="10 , 20" to="30,&#9;40" dur="2s"/></rect>
+ <rect id="units" width="10" height="10"><animateMotion from="0in,0" to="2in,0" dur="2s"/></rect>
+ <rect id="to-only" width="10" height="10"><animateMotion to="40,0" dur="2s"/></rect>
+ <rect id="two-commas" width="10" height="10"><animateMotion to="10,,20" dur="2s"/></rect>
+ <rect id="three-coordinates" width="10" height="10"><animateMotion to="10 20 30" dur="2s"/></rect>
+ <rect id="one-coordinate" width="10" height="10"><animateMotion to="10" dur="2s"/></rect>
+ <rect id="invalid-from" width="10" height="10"><animateMotion from="10;20" to="30,40" dur="2s"/></rect>
+</svg>
+<script>
+ asyncTest(async done => {
+ await new Promise(resolve => window.addEventListener("load", resolve));
+ await new Promise(requestAnimationFrame);
+ svg.pauseAnimations();
+ svg.setCurrentTime(1);
+ const origin = svg.getBoundingClientRect();
+ for (const rect of document.querySelectorAll("rect")) {
+ const box = rect.getBoundingClientRect();
+ println(`${rect.id}: ${box.left - origin.left}, ${box.top - origin.top}`);
+ }
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/svg/animations/animateMotion-fill-freeze.html b/Tests/LibWeb/Text/input/wpt-import/svg/animations/animateMotion-fill-freeze.html
new file mode 100644
index 00000000000..370080d77d8
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/svg/animations/animateMotion-fill-freeze.html
@@ -0,0 +1,53 @@
+<!doctype html>
+<html>
+<meta charset="utf-8">
+<title>Test for animation freeze when repeatDur is not a multiple of dur</title>
+<script src="../../resources/testharness.js"></script>
+<script src="../../resources/testharnessreport.js"></script>
+<script src="../../resources/SVGAnimationTestCase-testharness.js"></script>
+
+<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
+ <rect width="50" height="50" x="0" y="0" fill="green">
+ <animateMotion id="anim" to="100,0" begin="0s" dur="4s" fill="freeze" />
+ </rect>
+</svg>
+
+<script>
+var rootSVGElement = document.querySelector("svg");
+var epsilon = 1.0;
+
+// Setup animation test
+function sample1() {
+ assert_approx_equals(rootSVGElement.getBBox().x, 0, epsilon);
+}
+
+function sample2() {
+ assert_approx_equals(rootSVGElement.getBBox().x, 50, epsilon);
+}
+
+function sample3() {
+ assert_approx_equals(rootSVGElement.getBBox().x, 100, epsilon);
+}
+
+function sample4() {
+ assert_approx_equals(rootSVGElement.getBBox().x, 100, epsilon);
+}
+
+smil_async_test((t) => {
+ var rects = rootSVGElement.ownerDocument.getElementsByTagName("rect");
+ rect1 = rects[0];
+
+ const expectedValues = [
+ // [animationId, time, sampleCallback]
+ ["anim", 0.0, sample1],
+ ["anim", 2.0, sample2],
+ ["anim", 4.0, sample3],
+ ["anim", 6.0, sample4]
+ ];
+
+ runAnimationTest(t, expectedValues);
+});
+
+window.animationStartsImmediately = true;
+
+</script>
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/svg/animations/animateMotion-fill-remove.html b/Tests/LibWeb/Text/input/wpt-import/svg/animations/animateMotion-fill-remove.html
new file mode 100644
index 00000000000..4a4ee06f3ac
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/svg/animations/animateMotion-fill-remove.html
@@ -0,0 +1,53 @@
+<!doctype html>
+<html>
+<meta charset="utf-8">
+<title>Test for animation freeze when repeatDur is not a multiple of dur</title>
+<script src="../../resources/testharness.js"></script>
+<script src="../../resources/testharnessreport.js"></script>
+<script src="../../resources/SVGAnimationTestCase-testharness.js"></script>
+
+<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
+ <rect width="50" height="50" x="0" y="0" fill="green">
+ <animateMotion id="anim" to="100,0" begin="0s" dur="4s" fill="remove" />
+ </rect>
+</svg>
+
+<script>
+var rootSVGElement = document.querySelector("svg");
+var epsilon = 1.0;
+
+// Setup animation test
+function sample1() {
+ assert_approx_equals(rootSVGElement.getBBox().x, 0, epsilon);
+}
+
+function sample2() {
+ assert_approx_equals(rootSVGElement.getBBox().x, 50, epsilon);
+}
+
+function sample3() {
+ assert_approx_equals(rootSVGElement.getBBox().x, 0, epsilon);
+}
+
+function sample4() {
+ assert_approx_equals(rootSVGElement.getBBox().x, 0, epsilon);
+}
+
+smil_async_test((t) => {
+ var rects = rootSVGElement.ownerDocument.getElementsByTagName("rect");
+ rect1 = rects[0];
+
+ const expectedValues = [
+ // [animationId, time, sampleCallback]
+ ["anim", 0.0, sample1],
+ ["anim", 2.0, sample2],
+ ["anim", 4.0, sample3],
+ ["anim", 6.0, sample4]
+ ];
+
+ runAnimationTest(t, expectedValues);
+});
+
+window.animationStartsImmediately = true;
+
+</script>
\ No newline at end of file