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

No behaviour change.
This commit is contained in:
MacDue 2024-04-01 00:40:26 +01:00 committed by Alexander Kalenik
commit d2918b8204
Notes: sideshowbarker 2024-07-17 09:48:50 +09:00
3 changed files with 19 additions and 27 deletions

View file

@ -83,43 +83,19 @@ Gfx::Path SVGCircleElement::get_path(CSSPixelSize viewport_size)
// https://www.w3.org/TR/SVG11/shapes.html#CircleElementCXAttribute
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
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
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);
}
}

View file

@ -7,6 +7,7 @@
#include <LibWeb/Bindings/ExceptionOrUtils.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/CSS/StyleProperties.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/ShadowRoot.h>
#include <LibWeb/HTML/DOMStringMap.h>
@ -107,4 +108,17 @@ void 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());
}
}

View file

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