From aeef17966877c3d2d0156ee17e85e2a73b96c81e Mon Sep 17 00:00:00 2001 From: Jelle Raaijmakers Date: Fri, 21 Feb 2025 13:22:15 +0100 Subject: [PATCH] LibWeb: Add SVGAnimatedEnumeration --- Libraries/LibWeb/CMakeLists.txt | 1 + Libraries/LibWeb/Forward.h | 1 + .../LibWeb/SVG/SVGAnimatedEnumeration.cpp | 67 +++++++++++++++++++ Libraries/LibWeb/SVG/SVGAnimatedEnumeration.h | 37 ++++++++++ .../LibWeb/SVG/SVGAnimatedEnumeration.idl | 6 ++ Libraries/LibWeb/idl_files.cmake | 1 + .../Userland/Libraries/LibWeb/idl_files.gni | 1 + .../Text/expected/all-window-properties.txt | 1 + 8 files changed, 115 insertions(+) create mode 100644 Libraries/LibWeb/SVG/SVGAnimatedEnumeration.cpp create mode 100644 Libraries/LibWeb/SVG/SVGAnimatedEnumeration.h create mode 100644 Libraries/LibWeb/SVG/SVGAnimatedEnumeration.idl diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt index 2c1e7a8e035..a17ee91fbe7 100644 --- a/Libraries/LibWeb/CMakeLists.txt +++ b/Libraries/LibWeb/CMakeLists.txt @@ -729,6 +729,7 @@ set(SOURCES SVG/AttributeNames.cpp SVG/AttributeParser.cpp SVG/SVGAElement.cpp + SVG/SVGAnimatedEnumeration.cpp SVG/SVGAnimatedLength.cpp SVG/SVGAnimatedNumber.cpp SVG/SVGAnimatedRect.cpp diff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.h index 03d6b393ba5..f4a29992385 100644 --- a/Libraries/LibWeb/Forward.h +++ b/Libraries/LibWeb/Forward.h @@ -795,6 +795,7 @@ struct StorageShelf; } namespace Web::SVG { +class SVGAnimatedEnumeration; class SVGAnimatedLength; class SVGAnimatedRect; class SVGCircleElement; diff --git a/Libraries/LibWeb/SVG/SVGAnimatedEnumeration.cpp b/Libraries/LibWeb/SVG/SVGAnimatedEnumeration.cpp new file mode 100644 index 00000000000..141198436a8 --- /dev/null +++ b/Libraries/LibWeb/SVG/SVGAnimatedEnumeration.cpp @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2025, Jelle Raaijmakers + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include + +namespace Web::SVG { + +GC_DEFINE_ALLOCATOR(SVGAnimatedEnumeration); + +GC::Ref SVGAnimatedEnumeration::create(JS::Realm& realm, u16 value) +{ + return realm.create(realm, value); +} + +SVGAnimatedEnumeration::SVGAnimatedEnumeration(JS::Realm& realm, u16 value) + : PlatformObject(realm) + , m_value(value) +{ +} + +SVGAnimatedEnumeration::~SVGAnimatedEnumeration() = default; + +// https://svgwg.org/svg2-draft/types.html#__svg__SVGAnimatedEnumeration__baseVal +WebIDL::ExceptionOr SVGAnimatedEnumeration::set_base_val(u16 base_val) +{ + // 1. Let value be the value being assigned to baseVal. + auto value = base_val; + + // FIXME: 2. If value is 0 or is not the numeric type value for any value of the reflected attribute, then throw a + // TypeError. + if (value == 0) + return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "invalid value for baseVal"sv }; + + // FIXME: 3. Otherwise, if the reflecting IDL attribute is orientType and value is SVG_MARKER_ORIENT_ANGLE, then set the + // reflected attribute to the string "0". + + // FIXME: 4. Otherwise, value is the numeric type value for a specific, single keyword value for the reflected attribute. + // Set the reflected attribute to that value. + m_value = value; + + return {}; +} + +void SVGAnimatedEnumeration::initialize(JS::Realm& realm) +{ + Base::initialize(realm); + WEB_SET_PROTOTYPE_FOR_INTERFACE(SVGAnimatedEnumeration); +} + +// https://svgwg.org/svg2-draft/types.html#__svg__SVGAnimatedEnumeration__baseVal +u16 SVGAnimatedEnumeration::base_or_anim_value() const +{ + // FIXME: 1. Let value be the value of the reflected attribute (using the attribute's initial value if it is not present or + // invalid). + + // FIXME: 2. Return the numeric type value for value, according to the reflecting IDL attribute's definition. + + return m_value; +} + +} diff --git a/Libraries/LibWeb/SVG/SVGAnimatedEnumeration.h b/Libraries/LibWeb/SVG/SVGAnimatedEnumeration.h new file mode 100644 index 00000000000..a60916fd14c --- /dev/null +++ b/Libraries/LibWeb/SVG/SVGAnimatedEnumeration.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025, Jelle Raaijmakers + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include + +namespace Web::SVG { + +// https://svgwg.org/svg2-draft/types.html#InterfaceSVGAnimatedEnumeration +class SVGAnimatedEnumeration final : public Bindings::PlatformObject { + WEB_PLATFORM_OBJECT(SVGAnimatedEnumeration, Bindings::PlatformObject); + GC_DECLARE_ALLOCATOR(SVGAnimatedEnumeration); + +public: + [[nodiscard]] static GC::Ref create(JS::Realm&, u16 value); + virtual ~SVGAnimatedEnumeration() override; + + u16 base_val() const { return base_or_anim_value(); } + WebIDL::ExceptionOr set_base_val(u16); + + u16 anim_val() const { return base_or_anim_value(); } + +private: + SVGAnimatedEnumeration(JS::Realm&, u16 value); + + virtual void initialize(JS::Realm&) override; + + u16 base_or_anim_value() const; + + u16 m_value; +}; + +} diff --git a/Libraries/LibWeb/SVG/SVGAnimatedEnumeration.idl b/Libraries/LibWeb/SVG/SVGAnimatedEnumeration.idl new file mode 100644 index 00000000000..d9e1472bbf2 --- /dev/null +++ b/Libraries/LibWeb/SVG/SVGAnimatedEnumeration.idl @@ -0,0 +1,6 @@ +// https://svgwg.org/svg2-draft/types.html#InterfaceSVGAnimatedEnumeration +[Exposed=Window] +interface SVGAnimatedEnumeration { + attribute unsigned short baseVal; + readonly attribute unsigned short animVal; +}; diff --git a/Libraries/LibWeb/idl_files.cmake b/Libraries/LibWeb/idl_files.cmake index 1187bac6011..f6230c1610e 100644 --- a/Libraries/LibWeb/idl_files.cmake +++ b/Libraries/LibWeb/idl_files.cmake @@ -302,6 +302,7 @@ libweb_js_bindings(Streams/WritableStream) libweb_js_bindings(Streams/WritableStreamDefaultController) libweb_js_bindings(Streams/WritableStreamDefaultWriter) libweb_js_bindings(SVG/SVGAElement) +libweb_js_bindings(SVG/SVGAnimatedEnumeration) libweb_js_bindings(SVG/SVGAnimatedLength) libweb_js_bindings(SVG/SVGAnimatedNumber) libweb_js_bindings(SVG/SVGAnimatedRect) diff --git a/Meta/gn/secondary/Userland/Libraries/LibWeb/idl_files.gni b/Meta/gn/secondary/Userland/Libraries/LibWeb/idl_files.gni index d2c6461acb4..02077dead10 100644 --- a/Meta/gn/secondary/Userland/Libraries/LibWeb/idl_files.gni +++ b/Meta/gn/secondary/Userland/Libraries/LibWeb/idl_files.gni @@ -300,6 +300,7 @@ standard_idl_files = [ "//Userland/Libraries/LibWeb/Streams/WritableStreamDefaultController.idl", "//Userland/Libraries/LibWeb/Streams/WritableStreamDefaultWriter.idl", "//Userland/Libraries/LibWeb/SVG/SVGAElement.idl", + "//Userland/Libraries/LibWeb/SVG/SVGAnimatedEnumeration.idl", "//Userland/Libraries/LibWeb/SVG/SVGAnimatedLength.idl", "//Userland/Libraries/LibWeb/SVG/SVGAnimatedNumber.idl", "//Userland/Libraries/LibWeb/SVG/SVGAnimatedRect.idl", diff --git a/Tests/LibWeb/Text/expected/all-window-properties.txt b/Tests/LibWeb/Text/expected/all-window-properties.txt index 9eec691f26b..83cca30c30a 100644 --- a/Tests/LibWeb/Text/expected/all-window-properties.txt +++ b/Tests/LibWeb/Text/expected/all-window-properties.txt @@ -309,6 +309,7 @@ ResizeObserverEntry ResizeObserverSize Response SVGAElement +SVGAnimatedEnumeration SVGAnimatedLength SVGAnimatedNumber SVGAnimatedRect