From a46c980629b51abb5a82520dcbe5c34c61ede931 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Tue, 19 Aug 2025 12:21:27 +0100 Subject: [PATCH] LibWeb/CSS: Implement CSSNumericArray --- Libraries/LibWeb/CMakeLists.txt | 1 + Libraries/LibWeb/CSS/CSSNumericArray.cpp | 60 +++++++++++++++++++ Libraries/LibWeb/CSS/CSSNumericArray.h | 37 ++++++++++++ Libraries/LibWeb/CSS/CSSNumericArray.idl | 11 ++++ Libraries/LibWeb/Forward.h | 1 + Libraries/LibWeb/idl_files.cmake | 1 + .../BindingsGenerator/IDLGenerators.cpp | 1 + .../Text/expected/all-window-properties.txt | 1 + 8 files changed, 113 insertions(+) create mode 100644 Libraries/LibWeb/CSS/CSSNumericArray.cpp create mode 100644 Libraries/LibWeb/CSS/CSSNumericArray.h create mode 100644 Libraries/LibWeb/CSS/CSSNumericArray.idl diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txt index 2e57ec81143..1066a0f34d8 100644 --- a/Libraries/LibWeb/CMakeLists.txt +++ b/Libraries/LibWeb/CMakeLists.txt @@ -120,6 +120,7 @@ set(SOURCES CSS/CSSMediaRule.cpp CSS/CSSNamespaceRule.cpp CSS/CSSNestedDeclarations.cpp + CSS/CSSNumericArray.cpp CSS/CSSNumericValue.cpp CSS/CSSPageRule.cpp CSS/CSSPageDescriptors.cpp diff --git a/Libraries/LibWeb/CSS/CSSNumericArray.cpp b/Libraries/LibWeb/CSS/CSSNumericArray.cpp new file mode 100644 index 00000000000..7dd5043e89e --- /dev/null +++ b/Libraries/LibWeb/CSS/CSSNumericArray.cpp @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2025, Sam Atkins + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include "CSSNumericArray.h" +#include +#include +#include + +namespace Web::CSS { + +GC_DEFINE_ALLOCATOR(CSSNumericArray); + +GC::Ref CSSNumericArray::create(JS::Realm& realm, Vector> values) +{ + return realm.create(realm, move(values)); +} + +CSSNumericArray::CSSNumericArray(JS::Realm& realm, Vector> values) + : Bindings::PlatformObject(realm) + , m_values(move(values)) +{ + m_legacy_platform_object_flags = LegacyPlatformObjectFlags { + .supports_indexed_properties = true, + }; +} + +CSSNumericArray::~CSSNumericArray() = default; + +void CSSNumericArray::initialize(JS::Realm& realm) +{ + WEB_SET_PROTOTYPE_FOR_INTERFACE(CSSNumericArray); + Base::initialize(realm); +} + +void CSSNumericArray::visit_edges(Visitor& visitor) +{ + Base::visit_edges(visitor); + visitor.visit(m_values); +} + +// https://drafts.css-houdini.org/css-typed-om-1/#dom-cssnumericarray-length +WebIDL::UnsignedLong CSSNumericArray::length() const +{ + // The length attribute of CSSNumericArray indicates how many CSSNumericValues are contained within the CSSNumericArray. + return m_values.size(); +} + +// https://drafts.css-houdini.org/css-typed-om-1/#cssnumericarray-indexed-property-getter +Optional CSSNumericArray::item_value(size_t index) const +{ + // The indexed property getter of CSSNumericArray retrieves the CSSNumericValue at the provided index. + if (auto item = m_values.get(index); item.has_value()) + return item.release_value(); + return {}; +} + +} diff --git a/Libraries/LibWeb/CSS/CSSNumericArray.h b/Libraries/LibWeb/CSS/CSSNumericArray.h new file mode 100644 index 00000000000..4d3396bd09b --- /dev/null +++ b/Libraries/LibWeb/CSS/CSSNumericArray.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2025, Sam Atkins + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include +#include + +namespace Web::CSS { + +// https://drafts.css-houdini.org/css-typed-om-1/#cssnumericarray +class CSSNumericArray : public Bindings::PlatformObject { + WEB_PLATFORM_OBJECT(CSSNumericArray, Bindings::PlatformObject); + GC_DECLARE_ALLOCATOR(CSSNumericArray); + +public: + [[nodiscard]] static GC::Ref create(JS::Realm&, Vector>); + + virtual ~CSSNumericArray() override; + + WebIDL::UnsignedLong length() const; + virtual Optional item_value(size_t index) const override; + Vector> values() { return m_values; } + + virtual void initialize(JS::Realm&) override; + virtual void visit_edges(Visitor&) override; + +private: + CSSNumericArray(JS::Realm&, Vector>); + + Vector> m_values; +}; + +} diff --git a/Libraries/LibWeb/CSS/CSSNumericArray.idl b/Libraries/LibWeb/CSS/CSSNumericArray.idl new file mode 100644 index 00000000000..5ceaff4d6e4 --- /dev/null +++ b/Libraries/LibWeb/CSS/CSSNumericArray.idl @@ -0,0 +1,11 @@ +#import + +// https://drafts.css-houdini.org/css-typed-om-1/#cssnumericarray +[Exposed=(Window, Worker, PaintWorklet, LayoutWorklet)] +interface CSSNumericArray { + readonly attribute unsigned long length; + getter CSSNumericValue (unsigned long index); + + // FIXME: Different order than the spec, because IDLParser requires that this comes after the indexed property getter. + iterable; +}; diff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.h index 7ae9032bdba..29656a81b79 100644 --- a/Libraries/LibWeb/Forward.h +++ b/Libraries/LibWeb/Forward.h @@ -246,6 +246,7 @@ class CSSMathValue; class CSSMediaRule; class CSSNamespaceRule; class CSSNestedDeclarations; +class CSSNumericArray; class CSSNumericValue; class CSSPageRule; class CSSPageDescriptors; diff --git a/Libraries/LibWeb/idl_files.cmake b/Libraries/LibWeb/idl_files.cmake index 3c3c817b8b5..64884926e17 100644 --- a/Libraries/LibWeb/idl_files.cmake +++ b/Libraries/LibWeb/idl_files.cmake @@ -41,6 +41,7 @@ libweb_js_bindings(CSS/CSSMediaRule) libweb_js_bindings(CSS/CSS NAMESPACE) libweb_js_bindings(CSS/CSSNamespaceRule) libweb_js_bindings(CSS/CSSNestedDeclarations) +libweb_js_bindings(CSS/CSSNumericArray) libweb_js_bindings(CSS/CSSNumericValue) libweb_js_bindings(CSS/CSSPageRule) libweb_js_bindings(CSS/CSSPageDescriptors) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp index 0cd994c0fba..894183bb9d5 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/BindingsGenerator/IDLGenerators.cpp @@ -55,6 +55,7 @@ static bool is_platform_object(Type const& type) "CredentialsContainer"sv, "CryptoKey"sv, "CSSKeywordValue"sv, + "CSSNumericArray"sv, "CSSNumericValue"sv, "CSSStyleValue"sv, "CSSUnitValue"sv, diff --git a/Tests/LibWeb/Text/expected/all-window-properties.txt b/Tests/LibWeb/Text/expected/all-window-properties.txt index 347062f2e92..822e53595f6 100644 --- a/Tests/LibWeb/Text/expected/all-window-properties.txt +++ b/Tests/LibWeb/Text/expected/all-window-properties.txt @@ -53,6 +53,7 @@ CSSMathValue CSSMediaRule CSSNamespaceRule CSSNestedDeclarations +CSSNumericArray CSSNumericValue CSSPageDescriptors CSSPageRule