Skip to content

Commit 9991205

Browse files
committed
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.
1 parent 7e869c7 commit 9991205

File tree

8 files changed

+305
-68
lines changed

8 files changed

+305
-68
lines changed

Libraries/LibWeb/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,7 @@ set(SOURCES
932932
SVG/SVGLength.cpp
933933
SVG/SVGLinearGradientElement.cpp
934934
SVG/SVGLineElement.cpp
935+
SVG/SVGList.cpp
935936
SVG/SVGMaskElement.cpp
936937
SVG/SVGMetadataElement.cpp
937938
SVG/SVGNumber.cpp

Libraries/LibWeb/SVG/SVGGraphicsElement.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,8 @@ WebIDL::ExceptionOr<GC::Ref<Geometry::DOMRect>> SVGGraphicsElement::get_b_box(Op
347347
GC::Ref<SVGAnimatedTransformList> SVGGraphicsElement::transform() const
348348
{
349349
dbgln("(STUBBED) SVGGraphicsElement::transform(). Called on: {}", debug_description());
350-
auto base_val = SVGTransformList::create(realm());
351-
auto anim_val = SVGTransformList::create(realm());
350+
auto base_val = SVGTransformList::create(realm(), ReadOnlyList::Yes);
351+
auto anim_val = SVGTransformList::create(realm(), ReadOnlyList::Yes);
352352
return SVGAnimatedTransformList::create(realm(), base_val, anim_val);
353353
}
354354

Libraries/LibWeb/SVG/SVGList.cpp

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
/*
2+
* Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#include <LibWeb/SVG/SVGList.h>
8+
#include <LibWeb/SVG/SVGTransform.h>
9+
10+
namespace Web::SVG {
11+
12+
template<typename T>
13+
SVGList<T>::SVGList(JS::Realm& realm, Vector<T> items, ReadOnlyList read_only)
14+
: m_realm(realm)
15+
, m_items(move(items))
16+
, m_read_only(read_only)
17+
{
18+
}
19+
20+
template<typename T>
21+
SVGList<T>::SVGList(JS::Realm& realm, ReadOnlyList read_only)
22+
: m_realm(realm)
23+
, m_read_only(read_only)
24+
{
25+
}
26+
27+
template<typename T>
28+
void SVGList<T>::visit_edges(GC::Cell::Visitor& visitor)
29+
{
30+
visitor.visit(m_realm);
31+
visitor.visit(m_items);
32+
}
33+
34+
// https://www.w3.org/TR/SVG2/types.html#__svg__SVGNameList__length
35+
template<typename T>
36+
WebIDL::UnsignedLong SVGList<T>::length() const
37+
{
38+
// The length and numberOfItems IDL attributes represents the length of the list, and on getting simply return the
39+
// length of the list.
40+
return m_items.size();
41+
}
42+
43+
// https://www.w3.org/TR/SVG2/types.html#__svg__SVGNameList__clear
44+
template<typename T>
45+
WebIDL::ExceptionOr<void> SVGList<T>::clear()
46+
{
47+
// 1. If the list is read only, then throw a NoModificationAllowedError.
48+
if (m_read_only == ReadOnlyList::Yes)
49+
return WebIDL::NoModificationAllowedError::create(m_realm, "Cannot modify a read-only list"_utf16);
50+
51+
// 2. Detach and then remove all elements in the list.
52+
// FIXME: Detach items.
53+
m_items.clear();
54+
55+
// FIXME: 3. If the list reflects an attribute, or represents the base value of an object that reflects an attribute, then
56+
// reserialize the reflected attribute.
57+
58+
return {};
59+
}
60+
61+
// https://www.w3.org/TR/SVG2/types.html#__svg__SVGNameList__initialize
62+
template<typename T>
63+
WebIDL::ExceptionOr<T> SVGList<T>::initialize_(T new_item)
64+
{
65+
// 1. If the list is read only, then throw a NoModificationAllowedError.
66+
if (m_read_only == ReadOnlyList::Yes)
67+
return WebIDL::NoModificationAllowedError::create(m_realm, "Cannot modify a read-only list"_utf16);
68+
69+
// 2. Detach and then remove all elements in the list.
70+
// FIXME: Detach items.
71+
m_items.clear();
72+
73+
// FIXME: 3. If newItem is an object type, and newItem is not a detached object, then set newItem to be a newly created
74+
// object of the same type as newItem and which has the same (number or length) value.
75+
76+
// FIXME: 4. Attach newItem to the list interface object.
77+
78+
// 5. Append newItem to this list.
79+
m_items.append(new_item);
80+
81+
// FIXME: 6. If the list reflects an attribute, or represents the base value of an object that reflects an attribute, then
82+
// reserialize the reflected attribute.
83+
84+
// 7. Return newItem.
85+
return new_item;
86+
}
87+
88+
// https://www.w3.org/TR/SVG2/types.html#__svg__SVGNameList__getItem
89+
template<typename T>
90+
WebIDL::ExceptionOr<T> SVGList<T>::get_item(WebIDL::UnsignedLong index)
91+
{
92+
// 1. If index is greater than or equal to the length of the list, then throw an IndexSizeError.
93+
if (index >= m_items.size())
94+
return WebIDL::IndexSizeError::create(m_realm, "List index out of bounds"_utf16);
95+
96+
// 2. Return the element in the list at position index.
97+
return m_items[index];
98+
}
99+
100+
// https://www.w3.org/TR/SVG2/types.html#__svg__SVGNameList__insertItemBefore
101+
template<typename T>
102+
WebIDL::ExceptionOr<T> SVGList<T>::insert_item_before(T new_item, WebIDL::UnsignedLong index)
103+
{
104+
// 1. If the list is read only, then throw a NoModificationAllowedError.
105+
if (m_read_only == ReadOnlyList::Yes)
106+
return WebIDL::NoModificationAllowedError::create(m_realm, "Cannot modify a read-only list"_utf16);
107+
108+
// FIXME: 2. If newItem is an object type, and newItem is not a detached object, then set newItem to be a newly created
109+
// object of the same type as newItem and which has the same (number or length) value.
110+
111+
// 3. If index is greater than the length of the list, then set index to be the list length.
112+
if (index > m_items.size())
113+
index = m_items.size();
114+
115+
// 4. Insert newItem into the list at index index.
116+
m_items.insert(index, new_item);
117+
118+
// FIXME: 5. Attach newItem to the list interface object.
119+
120+
// FIXME: 6. If the list reflects an attribute, or represents the base value of an object that reflects an attribute, then
121+
// reserialize the reflected attribute.
122+
123+
// 7. Return newItem.
124+
return new_item;
125+
}
126+
127+
// https://www.w3.org/TR/SVG2/types.html#__svg__SVGNameList__replaceItem
128+
template<typename T>
129+
WebIDL::ExceptionOr<T> SVGList<T>::replace_item(T new_item, WebIDL::UnsignedLong index)
130+
{
131+
// 1. If the list is read only, then throw a NoModificationAllowedError.
132+
if (m_read_only == ReadOnlyList::Yes)
133+
return WebIDL::NoModificationAllowedError::create(m_realm, "Cannot modify a read-only list"_utf16);
134+
135+
// 2. If index is greater than or equal to the length of the list, then throw an IndexSizeError.
136+
if (index >= m_items.size())
137+
return WebIDL::IndexSizeError::create(m_realm, "List index out of bounds"_utf16);
138+
139+
// FIXME: 3. If newItem is an object type, and newItem is not a detached object, then set newItem to be a newly created
140+
// object of the same type as newItem and which has the same (number or length) value.
141+
142+
// FIXME: 4. Detach the element in the list at index index.
143+
144+
// 5. Replace the element in the list at index index with newItem.
145+
m_items[index] = new_item;
146+
147+
// FIXME: 6. Attach newItem to the list interface object.
148+
149+
// FIXNE: 7. If the list reflects an attribute, or represents the base value of an object that reflects an attribute, then
150+
// reserialize the reflected attribute.
151+
152+
// 8. Return newItem.
153+
return new_item;
154+
}
155+
156+
// https://www.w3.org/TR/SVG2/types.html#__svg__SVGNameList__removeItem
157+
template<typename T>
158+
WebIDL::ExceptionOr<T> SVGList<T>::remove_item(WebIDL::UnsignedLong index)
159+
{
160+
// 1. If the list is read only, then throw a NoModificationAllowedError.
161+
if (m_read_only == ReadOnlyList::Yes)
162+
return WebIDL::NoModificationAllowedError::create(m_realm, "Cannot modify a read-only list"_utf16);
163+
164+
// 2. If index is greater than or equal to the length of the list, then throw an IndexSizeError with code.
165+
if (index >= m_items.size())
166+
return WebIDL::IndexSizeError::create(m_realm, "List index out of bounds"_utf16);
167+
168+
// 3. Let item be the list element at index index.
169+
auto item = m_items[index];
170+
171+
// FIXME: 4. Detach item.
172+
173+
// 5. Remove the list element at index index.
174+
m_items.remove(index);
175+
176+
// 6. Return item.
177+
return item;
178+
}
179+
180+
// https://www.w3.org/TR/SVG2/types.html#__svg__SVGNameList__appendItem
181+
template<typename T>
182+
WebIDL::ExceptionOr<T> SVGList<T>::append_item(T new_item)
183+
{
184+
// 1. If the list is read only, then throw a NoModificationAllowedError.
185+
if (m_read_only == ReadOnlyList::Yes)
186+
return WebIDL::NoModificationAllowedError::create(m_realm, "Cannot modify a read-only list"_utf16);
187+
188+
// FIXME: 2. If newItem is an object type, and newItem is not a detached object, then set newItem to be a newly created
189+
// object of the same type as newItem and which has the same (number or length) value.
190+
191+
// 3. Let index be the length of the list.
192+
// AD-HOC: No, this is unused.
193+
194+
// 4. Append newItem to the end of the list.
195+
m_items.append(new_item);
196+
197+
// FIXME: 5. Attach newItem to the list interface object.
198+
199+
// FIXME: 6. If the list reflects an attribute, or represents the base value of an object that reflects an attribute, then
200+
// reserialize the reflected attribute.
201+
202+
// 7. Return newItem.
203+
return new_item;
204+
}
205+
206+
template class SVGList<GC::Ref<SVGTransform>>;
207+
208+
}

Libraries/LibWeb/SVG/SVGList.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#pragma once
8+
9+
#include <AK/Vector.h>
10+
#include <LibGC/Cell.h>
11+
#include <LibWeb/WebIDL/ExceptionOr.h>
12+
#include <LibWeb/WebIDL/Types.h>
13+
14+
namespace Web::SVG {
15+
16+
// https://www.w3.org/TR/SVG2/types.html#ReadOnlyList
17+
enum class ReadOnlyList : u8 {
18+
Yes,
19+
No,
20+
};
21+
22+
// https://www.w3.org/TR/SVG2/types.html#TermListInterface
23+
template<typename T>
24+
class SVGList {
25+
public:
26+
// https://www.w3.org/TR/SVG2/types.html#__svg__SVGNameList__length
27+
WebIDL::UnsignedLong length() const;
28+
WebIDL::UnsignedLong number_of_items() const { return length(); }
29+
30+
WebIDL::ExceptionOr<void> clear();
31+
WebIDL::ExceptionOr<T> initialize_(T);
32+
WebIDL::ExceptionOr<T> get_item(WebIDL::UnsignedLong);
33+
WebIDL::ExceptionOr<T> insert_item_before(T, WebIDL::UnsignedLong);
34+
WebIDL::ExceptionOr<T> replace_item(T, WebIDL::UnsignedLong);
35+
WebIDL::ExceptionOr<T> remove_item(WebIDL::UnsignedLong);
36+
WebIDL::ExceptionOr<T> append_item(T);
37+
38+
ReadonlySpan<T> items() { return m_items; }
39+
40+
protected:
41+
SVGList(JS::Realm&, Vector<T>, ReadOnlyList);
42+
SVGList(JS::Realm&, ReadOnlyList);
43+
44+
void visit_edges(GC::Cell::Visitor& visitor);
45+
46+
ReadOnlyList read_only() const { return m_read_only; }
47+
48+
private:
49+
GC::Ref<JS::Realm> m_realm;
50+
Vector<T> m_items;
51+
52+
// https://www.w3.org/TR/SVG2/types.html#ReadOnlyList
53+
ReadOnlyList m_read_only;
54+
};
55+
56+
}
Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 2024, MacDue <macdue@dueutil.tech>
3+
* Copyright (c) 2025, Jelle Raaijmakers <jelle@ladybird.org>
34
*
45
* SPDX-License-Identifier: BSD-2-Clause
56
*/
@@ -13,48 +14,26 @@ namespace Web::SVG {
1314

1415
GC_DEFINE_ALLOCATOR(SVGTransformList);
1516

16-
GC::Ref<SVGTransformList> SVGTransformList::create(JS::Realm& realm)
17+
GC::Ref<SVGTransformList> SVGTransformList::create(JS::Realm& realm, Vector<GC::Ref<SVGTransform>> items, ReadOnlyList read_only)
1718
{
18-
return realm.create<SVGTransformList>(realm);
19+
return realm.create<SVGTransformList>(realm, move(items), read_only);
1920
}
2021

21-
SVGTransformList::~SVGTransformList() = default;
22-
23-
SVGTransformList::SVGTransformList(JS::Realm& realm)
24-
: PlatformObject(realm)
25-
{
26-
}
27-
28-
// https://svgwg.org/svg2-draft/single-page.html#types-__svg__SVGNameList__length
29-
WebIDL::UnsignedLong SVGTransformList::length()
30-
{
31-
// The length and numberOfItems IDL attributes represents the length of the list, and on getting simply return the length of the list.
32-
return m_transforms.size();
33-
}
34-
35-
// https://svgwg.org/svg2-draft/single-page.html#types-__svg__SVGNameList__numberOfItems
36-
WebIDL::UnsignedLong SVGTransformList::number_of_items()
22+
GC::Ref<SVGTransformList> SVGTransformList::create(JS::Realm& realm, ReadOnlyList read_only)
3723
{
38-
// The length and numberOfItems IDL attributes represents the length of the list, and on getting simply return the length of the list.
39-
return m_transforms.size();
24+
return realm.create<SVGTransformList>(realm, read_only);
4025
}
4126

42-
// https://svgwg.org/svg2-draft/single-page.html#types-__svg__SVGNameList__getItem
43-
WebIDL::ExceptionOr<GC::Ref<SVGTransform>> SVGTransformList::get_item(WebIDL::UnsignedLong index)
27+
SVGTransformList::SVGTransformList(JS::Realm& realm, Vector<GC::Ref<SVGTransform>> items, ReadOnlyList read_only)
28+
: Bindings::PlatformObject(realm)
29+
, SVGList(realm, move(items), read_only)
4430
{
45-
// 1. If index is greater than or equal to the length of the list, then throw an IndexSizeError.
46-
if (index >= m_transforms.size())
47-
return WebIDL::IndexSizeError::create(realm(), "SVGTransformList index out of bounds"_utf16);
48-
// 2. Return the element in the list at position index.
49-
return m_transforms.at(index);
5031
}
5132

52-
// https://svgwg.org/svg2-draft/single-page.html#types-__svg__SVGNameList__appendItem
53-
GC::Ref<SVGTransform> SVGTransformList::append_item(GC::Ref<SVGTransform> new_item)
33+
SVGTransformList::SVGTransformList(JS::Realm& realm, ReadOnlyList read_only)
34+
: Bindings::PlatformObject(realm)
35+
, SVGList(realm, read_only)
5436
{
55-
// FIXME: This does not implement the steps from the specification.
56-
m_transforms.append(new_item);
57-
return new_item;
5837
}
5938

6039
void SVGTransformList::initialize(JS::Realm& realm)
@@ -63,11 +42,10 @@ void SVGTransformList::initialize(JS::Realm& realm)
6342
Base::initialize(realm);
6443
}
6544

66-
void SVGTransformList::visit_edges(Cell::Visitor& visitor)
45+
void SVGTransformList::visit_edges(Visitor& visitor)
6746
{
6847
Base::visit_edges(visitor);
69-
for (auto transform : m_transforms)
70-
transform->visit_edges(visitor);
48+
SVGList::visit_edges(visitor);
7149
}
7250

7351
}

0 commit comments

Comments
 (0)