فهرست منبع

LibWeb: Add (and use) CSS property to `SVGAnimatedLength` helper

No behaviour change.
MacDue 1 سال پیش
والد
کامیت
d2918b8204

+ 3 - 27
Userland/Libraries/LibWeb/SVG/SVGCircleElement.cpp

@@ -83,43 +83,19 @@ Gfx::Path SVGCircleElement::get_path(CSSPixelSize viewport_size)
 // https://www.w3.org/TR/SVG11/shapes.html#CircleElementCXAttribute
 // https://www.w3.org/TR/SVG11/shapes.html#CircleElementCXAttribute
 JS::NonnullGCPtr<SVGAnimatedLength> SVGCircleElement::cx() const
 JS::NonnullGCPtr<SVGAnimatedLength> SVGCircleElement::cx() const
 {
 {
-    // FIXME: Create a proper animated value when animations are supported.
-    auto make_length = [&] {
-        if (auto const* style = computed_css_values(); style) {
-            if (auto cx = style->length_percentage(CSS::PropertyID::Cx); cx.has_value())
-                return SVGLength::from_length_percentage(realm(), *cx);
-        }
-        return SVGLength::create(realm(), 0, 0.0f);
-    };
-    return SVGAnimatedLength::create(realm(), make_length(), make_length());
+    return svg_animated_length_for_property(CSS::PropertyID::Cx);
 }
 }
 
 
 // https://www.w3.org/TR/SVG11/shapes.html#CircleElementCYAttribute
 // https://www.w3.org/TR/SVG11/shapes.html#CircleElementCYAttribute
 JS::NonnullGCPtr<SVGAnimatedLength> SVGCircleElement::cy() const
 JS::NonnullGCPtr<SVGAnimatedLength> SVGCircleElement::cy() const
 {
 {
-    // FIXME: Create a proper animated value when animations are supported.
-    auto make_length = [&] {
-        if (auto const* style = computed_css_values(); style) {
-            if (auto cy = style->length_percentage(CSS::PropertyID::Cy); cy.has_value())
-                return SVGLength::from_length_percentage(realm(), *cy);
-        }
-        return SVGLength::create(realm(), 0, 0.0f);
-    };
-    return SVGAnimatedLength::create(realm(), make_length(), make_length());
+    return svg_animated_length_for_property(CSS::PropertyID::Cy);
 }
 }
 
 
 // https://www.w3.org/TR/SVG11/shapes.html#CircleElementRAttribute
 // https://www.w3.org/TR/SVG11/shapes.html#CircleElementRAttribute
 JS::NonnullGCPtr<SVGAnimatedLength> SVGCircleElement::r() const
 JS::NonnullGCPtr<SVGAnimatedLength> SVGCircleElement::r() const
 {
 {
-    // FIXME: Create a proper animated value when animations are supported.
-    auto make_length = [&] {
-        if (auto const* style = computed_css_values(); style) {
-            if (auto r = computed_css_values()->length_percentage(CSS::PropertyID::R); r.has_value())
-                return SVGLength::from_length_percentage(realm(), *r);
-        }
-        return SVGLength::create(realm(), 0, 0.0f);
-    };
-    return SVGAnimatedLength::create(realm(), make_length(), make_length());
+    return svg_animated_length_for_property(CSS::PropertyID::R);
 }
 }
 
 
 }
 }

+ 14 - 0
Userland/Libraries/LibWeb/SVG/SVGElement.cpp

@@ -7,6 +7,7 @@
 
 
 #include <LibWeb/Bindings/ExceptionOrUtils.h>
 #include <LibWeb/Bindings/ExceptionOrUtils.h>
 #include <LibWeb/Bindings/Intrinsics.h>
 #include <LibWeb/Bindings/Intrinsics.h>
+#include <LibWeb/CSS/StyleProperties.h>
 #include <LibWeb/DOM/Document.h>
 #include <LibWeb/DOM/Document.h>
 #include <LibWeb/DOM/ShadowRoot.h>
 #include <LibWeb/DOM/ShadowRoot.h>
 #include <LibWeb/HTML/DOMStringMap.h>
 #include <LibWeb/HTML/DOMStringMap.h>
@@ -107,4 +108,17 @@ void SVGElement::blur()
     dbgln("(STUBBED) SVGElement::blur()");
     dbgln("(STUBBED) SVGElement::blur()");
 }
 }
 
 
+JS::NonnullGCPtr<SVGAnimatedLength> SVGElement::svg_animated_length_for_property(CSS::PropertyID property) const
+{
+    // FIXME: Create a proper animated value when animations are supported.
+    auto make_length = [&] {
+        if (auto const* style = computed_css_values(); style) {
+            if (auto length = style->length_percentage(property); length.has_value())
+                return SVGLength::from_length_percentage(realm(), *length);
+        }
+        return SVGLength::create(realm(), 0, 0.0f);
+    };
+    return SVGAnimatedLength::create(realm(), make_length(), make_length());
+}
+
 }
 }

+ 2 - 0
Userland/Libraries/LibWeb/SVG/SVGElement.h

@@ -40,6 +40,8 @@ protected:
 
 
     JS::GCPtr<HTML::DOMStringMap> m_dataset;
     JS::GCPtr<HTML::DOMStringMap> m_dataset;
 
 
+    JS::NonnullGCPtr<SVGAnimatedLength> svg_animated_length_for_property(CSS::PropertyID) const;
+
 private:
 private:
     virtual bool is_svg_element() const final { return true; }
     virtual bool is_svg_element() const final { return true; }
 };
 };