LibWeb: Add SVGList<T> and use it for SVGTransformList

The spec defines a generic list interfaces that we can reuse. Currently
we only have SVGTransformList, but we will need this to add
SVGNumberList as well.
This commit is contained in:
Jelle Raaijmakers 2025-11-05 15:20:53 +01:00 committed by Jelle Raaijmakers
parent 7e869c7816
commit 9991205403
Notes: github-actions[bot] 2025-11-09 00:24:22 +00:00
8 changed files with 307 additions and 70 deletions

View file

@ -932,6 +932,7 @@ set(SOURCES
SVG/SVGLength.cpp
SVG/SVGLinearGradientElement.cpp
SVG/SVGLineElement.cpp
SVG/SVGList.cpp
SVG/SVGMaskElement.cpp
SVG/SVGMetadataElement.cpp
SVG/SVGNumber.cpp

View file

@ -347,8 +347,8 @@ WebIDL::ExceptionOr<GC::Ref<Geometry::DOMRect>> SVGGraphicsElement::get_b_box(Op
GC::Ref<SVGAnimatedTransformList> SVGGraphicsElement::transform() const
{
dbgln("(STUBBED) SVGGraphicsElement::transform(). Called on: {}", debug_description());
auto base_val = SVGTransformList::create(realm());
auto anim_val = SVGTransformList::create(realm());
auto base_val = SVGTransformList::create(realm(), ReadOnlyList::Yes);
auto anim_val = SVGTransformList::create(realm(), ReadOnlyList::Yes);
return SVGAnimatedTransformList::create(realm(), base_val, anim_val);
}

View file

@ -0,0 +1,208 @@
/*
* Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/SVG/SVGList.h>
#include <LibWeb/SVG/SVGTransform.h>
namespace Web::SVG {
template<typename T>
SVGList<T>::SVGList(JS::Realm& realm, Vector<T> items, ReadOnlyList read_only)
: m_realm(realm)
, m_items(move(items))
, m_read_only(read_only)
{
}
template<typename T>
SVGList<T>::SVGList(JS::Realm& realm, ReadOnlyList read_only)
: m_realm(realm)
, m_read_only(read_only)
{
}
template<typename T>
void SVGList<T>::visit_edges(GC::Cell::Visitor& visitor)
{
visitor.visit(m_realm);
visitor.visit(m_items);
}
// https://www.w3.org/TR/SVG2/types.html#__svg__SVGNameList__length
template<typename T>
WebIDL::UnsignedLong SVGList<T>::length() const
{
// The length and numberOfItems IDL attributes represents the length of the list, and on getting simply return the
// length of the list.
return m_items.size();
}
// https://www.w3.org/TR/SVG2/types.html#__svg__SVGNameList__clear
template<typename T>
WebIDL::ExceptionOr<void> SVGList<T>::clear()
{
// 1. If the list is read only, then throw a NoModificationAllowedError.
if (m_read_only == ReadOnlyList::Yes)
return WebIDL::NoModificationAllowedError::create(m_realm, "Cannot modify a read-only list"_utf16);
// 2. Detach and then remove all elements in the list.
// FIXME: Detach items.
m_items.clear();
// FIXME: 3. If the list reflects an attribute, or represents the base value of an object that reflects an attribute, then
// reserialize the reflected attribute.
return {};
}
// https://www.w3.org/TR/SVG2/types.html#__svg__SVGNameList__initialize
template<typename T>
WebIDL::ExceptionOr<T> SVGList<T>::initialize_(T new_item)
{
// 1. If the list is read only, then throw a NoModificationAllowedError.
if (m_read_only == ReadOnlyList::Yes)
return WebIDL::NoModificationAllowedError::create(m_realm, "Cannot modify a read-only list"_utf16);
// 2. Detach and then remove all elements in the list.
// FIXME: Detach items.
m_items.clear();
// FIXME: 3. If newItem is an object type, and newItem is not a detached object, then set newItem to be a newly created
// object of the same type as newItem and which has the same (number or length) value.
// FIXME: 4. Attach newItem to the list interface object.
// 5. Append newItem to this list.
m_items.append(new_item);
// FIXME: 6. If the list reflects an attribute, or represents the base value of an object that reflects an attribute, then
// reserialize the reflected attribute.
// 7. Return newItem.
return new_item;
}
// https://www.w3.org/TR/SVG2/types.html#__svg__SVGNameList__getItem
template<typename T>
WebIDL::ExceptionOr<T> SVGList<T>::get_item(WebIDL::UnsignedLong index)
{
// 1. If index is greater than or equal to the length of the list, then throw an IndexSizeError.
if (index >= m_items.size())
return WebIDL::IndexSizeError::create(m_realm, "List index out of bounds"_utf16);
// 2. Return the element in the list at position index.
return m_items[index];
}
// https://www.w3.org/TR/SVG2/types.html#__svg__SVGNameList__insertItemBefore
template<typename T>
WebIDL::ExceptionOr<T> SVGList<T>::insert_item_before(T new_item, WebIDL::UnsignedLong index)
{
// 1. If the list is read only, then throw a NoModificationAllowedError.
if (m_read_only == ReadOnlyList::Yes)
return WebIDL::NoModificationAllowedError::create(m_realm, "Cannot modify a read-only list"_utf16);
// FIXME: 2. If newItem is an object type, and newItem is not a detached object, then set newItem to be a newly created
// object of the same type as newItem and which has the same (number or length) value.
// 3. If index is greater than the length of the list, then set index to be the list length.
if (index > m_items.size())
index = m_items.size();
// 4. Insert newItem into the list at index index.
m_items.insert(index, new_item);
// FIXME: 5. Attach newItem to the list interface object.
// FIXME: 6. If the list reflects an attribute, or represents the base value of an object that reflects an attribute, then
// reserialize the reflected attribute.
// 7. Return newItem.
return new_item;
}
// https://www.w3.org/TR/SVG2/types.html#__svg__SVGNameList__replaceItem
template<typename T>
WebIDL::ExceptionOr<T> SVGList<T>::replace_item(T new_item, WebIDL::UnsignedLong index)
{
// 1. If the list is read only, then throw a NoModificationAllowedError.
if (m_read_only == ReadOnlyList::Yes)
return WebIDL::NoModificationAllowedError::create(m_realm, "Cannot modify a read-only list"_utf16);
// 2. If index is greater than or equal to the length of the list, then throw an IndexSizeError.
if (index >= m_items.size())
return WebIDL::IndexSizeError::create(m_realm, "List index out of bounds"_utf16);
// FIXME: 3. If newItem is an object type, and newItem is not a detached object, then set newItem to be a newly created
// object of the same type as newItem and which has the same (number or length) value.
// FIXME: 4. Detach the element in the list at index index.
// 5. Replace the element in the list at index index with newItem.
m_items[index] = new_item;
// FIXME: 6. Attach newItem to the list interface object.
// FIXNE: 7. If the list reflects an attribute, or represents the base value of an object that reflects an attribute, then
// reserialize the reflected attribute.
// 8. Return newItem.
return new_item;
}
// https://www.w3.org/TR/SVG2/types.html#__svg__SVGNameList__removeItem
template<typename T>
WebIDL::ExceptionOr<T> SVGList<T>::remove_item(WebIDL::UnsignedLong index)
{
// 1. If the list is read only, then throw a NoModificationAllowedError.
if (m_read_only == ReadOnlyList::Yes)
return WebIDL::NoModificationAllowedError::create(m_realm, "Cannot modify a read-only list"_utf16);
// 2. If index is greater than or equal to the length of the list, then throw an IndexSizeError with code.
if (index >= m_items.size())
return WebIDL::IndexSizeError::create(m_realm, "List index out of bounds"_utf16);
// 3. Let item be the list element at index index.
auto item = m_items[index];
// FIXME: 4. Detach item.
// 5. Remove the list element at index index.
m_items.remove(index);
// 6. Return item.
return item;
}
// https://www.w3.org/TR/SVG2/types.html#__svg__SVGNameList__appendItem
template<typename T>
WebIDL::ExceptionOr<T> SVGList<T>::append_item(T new_item)
{
// 1. If the list is read only, then throw a NoModificationAllowedError.
if (m_read_only == ReadOnlyList::Yes)
return WebIDL::NoModificationAllowedError::create(m_realm, "Cannot modify a read-only list"_utf16);
// FIXME: 2. If newItem is an object type, and newItem is not a detached object, then set newItem to be a newly created
// object of the same type as newItem and which has the same (number or length) value.
// 3. Let index be the length of the list.
// AD-HOC: No, this is unused.
// 4. Append newItem to the end of the list.
m_items.append(new_item);
// FIXME: 5. Attach newItem to the list interface object.
// FIXME: 6. If the list reflects an attribute, or represents the base value of an object that reflects an attribute, then
// reserialize the reflected attribute.
// 7. Return newItem.
return new_item;
}
template class SVGList<GC::Ref<SVGTransform>>;
}

View file

@ -0,0 +1,56 @@
/*
* Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Vector.h>
#include <LibGC/Cell.h>
#include <LibWeb/WebIDL/ExceptionOr.h>
#include <LibWeb/WebIDL/Types.h>
namespace Web::SVG {
// https://www.w3.org/TR/SVG2/types.html#ReadOnlyList
enum class ReadOnlyList : u8 {
Yes,
No,
};
// https://www.w3.org/TR/SVG2/types.html#TermListInterface
template<typename T>
class SVGList {
public:
// https://www.w3.org/TR/SVG2/types.html#__svg__SVGNameList__length
WebIDL::UnsignedLong length() const;
WebIDL::UnsignedLong number_of_items() const { return length(); }
WebIDL::ExceptionOr<void> clear();
WebIDL::ExceptionOr<T> initialize_(T);
WebIDL::ExceptionOr<T> get_item(WebIDL::UnsignedLong);
WebIDL::ExceptionOr<T> insert_item_before(T, WebIDL::UnsignedLong);
WebIDL::ExceptionOr<T> replace_item(T, WebIDL::UnsignedLong);
WebIDL::ExceptionOr<T> remove_item(WebIDL::UnsignedLong);
WebIDL::ExceptionOr<T> append_item(T);
ReadonlySpan<T> items() { return m_items; }
protected:
SVGList(JS::Realm&, Vector<T>, ReadOnlyList);
SVGList(JS::Realm&, ReadOnlyList);
void visit_edges(GC::Cell::Visitor& visitor);
ReadOnlyList read_only() const { return m_read_only; }
private:
GC::Ref<JS::Realm> m_realm;
Vector<T> m_items;
// https://www.w3.org/TR/SVG2/types.html#ReadOnlyList
ReadOnlyList m_read_only;
};
}

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2024, MacDue <macdue@dueutil.tech>
* Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -13,48 +14,26 @@ namespace Web::SVG {
GC_DEFINE_ALLOCATOR(SVGTransformList);
GC::Ref<SVGTransformList> SVGTransformList::create(JS::Realm& realm)
GC::Ref<SVGTransformList> SVGTransformList::create(JS::Realm& realm, Vector<GC::Ref<SVGTransform>> items, ReadOnlyList read_only)
{
return realm.create<SVGTransformList>(realm);
return realm.create<SVGTransformList>(realm, move(items), read_only);
}
SVGTransformList::~SVGTransformList() = default;
GC::Ref<SVGTransformList> SVGTransformList::create(JS::Realm& realm, ReadOnlyList read_only)
{
return realm.create<SVGTransformList>(realm, read_only);
}
SVGTransformList::SVGTransformList(JS::Realm& realm)
: PlatformObject(realm)
SVGTransformList::SVGTransformList(JS::Realm& realm, Vector<GC::Ref<SVGTransform>> items, ReadOnlyList read_only)
: Bindings::PlatformObject(realm)
, SVGList(realm, move(items), read_only)
{
}
// https://svgwg.org/svg2-draft/single-page.html#types-__svg__SVGNameList__length
WebIDL::UnsignedLong SVGTransformList::length()
SVGTransformList::SVGTransformList(JS::Realm& realm, ReadOnlyList read_only)
: Bindings::PlatformObject(realm)
, SVGList(realm, read_only)
{
// The length and numberOfItems IDL attributes represents the length of the list, and on getting simply return the length of the list.
return m_transforms.size();
}
// https://svgwg.org/svg2-draft/single-page.html#types-__svg__SVGNameList__numberOfItems
WebIDL::UnsignedLong SVGTransformList::number_of_items()
{
// The length and numberOfItems IDL attributes represents the length of the list, and on getting simply return the length of the list.
return m_transforms.size();
}
// https://svgwg.org/svg2-draft/single-page.html#types-__svg__SVGNameList__getItem
WebIDL::ExceptionOr<GC::Ref<SVGTransform>> SVGTransformList::get_item(WebIDL::UnsignedLong index)
{
// 1. If index is greater than or equal to the length of the list, then throw an IndexSizeError.
if (index >= m_transforms.size())
return WebIDL::IndexSizeError::create(realm(), "SVGTransformList index out of bounds"_utf16);
// 2. Return the element in the list at position index.
return m_transforms.at(index);
}
// https://svgwg.org/svg2-draft/single-page.html#types-__svg__SVGNameList__appendItem
GC::Ref<SVGTransform> SVGTransformList::append_item(GC::Ref<SVGTransform> new_item)
{
// FIXME: This does not implement the steps from the specification.
m_transforms.append(new_item);
return new_item;
}
void SVGTransformList::initialize(JS::Realm& realm)
@ -63,11 +42,10 @@ void SVGTransformList::initialize(JS::Realm& realm)
Base::initialize(realm);
}
void SVGTransformList::visit_edges(Cell::Visitor& visitor)
void SVGTransformList::visit_edges(Visitor& visitor)
{
Base::visit_edges(visitor);
for (auto transform : m_transforms)
transform->visit_edges(visitor);
SVGList::visit_edges(visitor);
}
}

View file

@ -1,41 +1,35 @@
/*
* Copyright (c) 2024, MacDue <macdue@dueutil.tech>
* Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Vector.h>
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/SVG/SVGList.h>
#include <LibWeb/SVG/SVGTransform.h>
#include <LibWeb/WebIDL/Types.h>
namespace Web::SVG {
// https://svgwg.org/svg2-draft/single-page.html#coords-InterfaceSVGTransformList
class SVGTransformList final : public Bindings::PlatformObject {
class SVGTransformList final
: public Bindings::PlatformObject
, public SVGList<GC::Ref<SVGTransform>> {
WEB_PLATFORM_OBJECT(SVGTransformList, Bindings::PlatformObject);
GC_DECLARE_ALLOCATOR(SVGTransformList);
public:
[[nodiscard]] static GC::Ref<SVGTransformList> create(JS::Realm& realm);
virtual ~SVGTransformList() override;
WebIDL::UnsignedLong length();
WebIDL::UnsignedLong number_of_items();
WebIDL::ExceptionOr<GC::Ref<SVGTransform>> get_item(WebIDL::UnsignedLong index);
GC::Ref<SVGTransform> append_item(GC::Ref<SVGTransform> new_item);
[[nodiscard]] static GC::Ref<SVGTransformList> create(JS::Realm& realm, Vector<GC::Ref<SVGTransform>>, ReadOnlyList);
[[nodiscard]] static GC::Ref<SVGTransformList> create(JS::Realm& realm, ReadOnlyList);
virtual ~SVGTransformList() override = default;
private:
SVGTransformList(JS::Realm& realm);
SVGTransformList(JS::Realm&, Vector<GC::Ref<SVGTransform>>, ReadOnlyList);
SVGTransformList(JS::Realm&, ReadOnlyList);
virtual void initialize(JS::Realm& realm) override;
virtual void visit_edges(Cell::Visitor& visitor) override;
Vector<GC::Ref<SVGTransform>> m_transforms;
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Visitor&) override;
};
}

View file

@ -6,14 +6,14 @@ interface SVGTransformList {
readonly attribute unsigned long length;
readonly attribute unsigned long numberOfItems;
[FIXME] undefined clear();
// FIXME: SVGTransform initialize(SVGTransform newItem);
undefined clear();
SVGTransform initialize(SVGTransform newItem);
getter SVGTransform getItem(unsigned long index);
[FIXME] SVGTransform insertItemBefore(SVGTransform newItem, unsigned long index);
[FIXME] SVGTransform replaceItem(SVGTransform newItem, unsigned long index);
[FIXME] SVGTransform removeItem(unsigned long index);
SVGTransform insertItemBefore(SVGTransform newItem, unsigned long index);
SVGTransform replaceItem(SVGTransform newItem, unsigned long index);
SVGTransform removeItem(unsigned long index);
SVGTransform appendItem(SVGTransform newItem);
// FIXME: setter undefined (unsigned long index, SVGTransform newItem);
setter undefined (unsigned long index, SVGTransform newItem);
// Additional methods not common to other list interfaces.
[FIXME] SVGTransform createSVGTransformFromMatrix(optional DOMMatrix2DInit matrix = {});

View file

@ -2,8 +2,8 @@ Harness status: OK
Found 1781 tests
977 Pass
804 Fail
982 Pass
799 Fail
Pass idl_test setup
Pass idl_test validation
Pass Partial interface Document: original interface defined
@ -739,12 +739,12 @@ Pass SVGTransformList interface: existence and properties of interface prototype
Pass SVGTransformList interface: existence and properties of interface prototype object's @@unscopables property
Pass SVGTransformList interface: attribute length
Pass SVGTransformList interface: attribute numberOfItems
Fail SVGTransformList interface: operation clear()
Fail SVGTransformList interface: operation initialize(SVGTransform)
Pass SVGTransformList interface: operation clear()
Pass SVGTransformList interface: operation initialize(SVGTransform)
Pass SVGTransformList interface: operation getItem(unsigned long)
Fail SVGTransformList interface: operation insertItemBefore(SVGTransform, unsigned long)
Fail SVGTransformList interface: operation replaceItem(SVGTransform, unsigned long)
Fail SVGTransformList interface: operation removeItem(unsigned long)
Pass SVGTransformList interface: operation insertItemBefore(SVGTransform, unsigned long)
Pass SVGTransformList interface: operation replaceItem(SVGTransform, unsigned long)
Pass SVGTransformList interface: operation removeItem(unsigned long)
Pass SVGTransformList interface: operation appendItem(SVGTransform)
Fail SVGTransformList interface: operation createSVGTransformFromMatrix(optional DOMMatrix2DInit)
Fail SVGTransformList interface: operation consolidate()