1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "DOMSVGNumberList.h"
9 #include "SVGElement.h"
10 #include "DOMSVGNumber.h"
12 #include "SVGAnimatedNumberList.h"
13 #include "mozilla/dom/SVGNumberListBinding.h"
14 #include "mozilla/RefPtr.h"
17 // See the comment in this file's header.
19 // local helper functions
22 using mozilla::dom::DOMSVGNumber
;
24 void UpdateListIndicesFromIndex(FallibleTArray
<DOMSVGNumber
*>& aItemsArray
,
25 uint32_t aStartingIndex
) {
26 uint32_t length
= aItemsArray
.Length();
28 for (uint32_t i
= aStartingIndex
; i
< length
; ++i
) {
30 aItemsArray
[i
]->UpdateListIndex(i
);
37 namespace mozilla::dom
{
39 // We could use NS_IMPL_CYCLE_COLLECTION(, except that in Unlink() we need to
40 // clear our DOMSVGAnimatedNumberList's weak ref to us to be safe. (The other
41 // option would be to not unlink and rely on the breaking of the other edges in
42 // the cycle, as NS_SVG_VAL_IMPL_CYCLE_COLLECTION does.)
43 NS_IMPL_CYCLE_COLLECTION_CLASS(DOMSVGNumberList
)
45 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(DOMSVGNumberList
)
47 if (tmp
->IsAnimValList()) {
48 tmp
->mAList
->mAnimVal
= nullptr;
50 tmp
->mAList
->mBaseVal
= nullptr;
52 NS_IMPL_CYCLE_COLLECTION_UNLINK(mAList
)
54 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
55 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
56 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(DOMSVGNumberList
)
57 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAList
)
58 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
59 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(DOMSVGNumberList
)
60 NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
61 NS_IMPL_CYCLE_COLLECTION_TRACE_END
63 NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMSVGNumberList
)
64 NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMSVGNumberList
)
66 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMSVGNumberList
)
67 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
68 NS_INTERFACE_MAP_ENTRY(nsISupports
)
71 JSObject
* DOMSVGNumberList::WrapObject(JSContext
* cx
,
72 JS::Handle
<JSObject
*> aGivenProto
) {
73 return mozilla::dom::SVGNumberList_Binding::Wrap(cx
, this, aGivenProto
);
76 void DOMSVGNumberList::InternalListLengthWillChange(uint32_t aNewLength
) {
77 uint32_t oldLength
= mItems
.Length();
79 if (aNewLength
> DOMSVGNumber::MaxListIndex()) {
80 // It's safe to get out of sync with our internal list as long as we have
81 // FEWER items than it does.
82 aNewLength
= DOMSVGNumber::MaxListIndex();
85 RefPtr
<DOMSVGNumberList
> kungFuDeathGrip
;
86 if (aNewLength
< oldLength
) {
87 // RemovingFromList() might clear last reference to |this|.
88 // Retain a temporary reference to keep from dying before returning.
89 kungFuDeathGrip
= this;
92 // If our length will decrease, notify the items that will be removed:
93 for (uint32_t i
= aNewLength
; i
< oldLength
; ++i
) {
95 mItems
[i
]->RemovingFromList();
99 if (!mItems
.SetLength(aNewLength
, fallible
)) {
100 // We silently ignore SetLength OOM failure since being out of sync is safe
101 // so long as we have *fewer* items than our internal list.
106 // If our length has increased, null out the new pointers:
107 for (uint32_t i
= oldLength
; i
< aNewLength
; ++i
) {
112 SVGNumberList
& DOMSVGNumberList::InternalList() const {
113 SVGAnimatedNumberList
* alist
= Element()->GetAnimatedNumberList(AttrEnum());
114 return IsAnimValList() && alist
->mAnimVal
? *alist
->mAnimVal
118 void DOMSVGNumberList::Clear(ErrorResult
& error
) {
119 if (IsAnimValList()) {
120 error
.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR
);
124 if (LengthNoFlush() > 0) {
125 AutoChangeNumberListNotifier
notifier(this);
126 // Notify any existing DOM items of removal *before* truncating the lists
127 // so that they can find their SVGNumber internal counterparts and copy
128 // their values. This also notifies the animVal list:
129 mAList
->InternalBaseValListWillChangeTo(SVGNumberList());
132 auto* alist
= Element()->GetAnimatedNumberList(AttrEnum());
133 alist
->mBaseVal
.Clear();
134 alist
->mIsBaseSet
= false;
138 already_AddRefed
<DOMSVGNumber
> DOMSVGNumberList::Initialize(
139 DOMSVGNumber
& aItem
, ErrorResult
& error
) {
140 if (IsAnimValList()) {
141 error
.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR
);
145 // If newItem is already in a list we should insert a clone of newItem, and
146 // for consistency, this should happen even if *this* is the list that
147 // newItem is currently in. Note that in the case of newItem being in this
148 // list, the Clear() call before the InsertItemBefore() call would remove it
149 // from this list, and so the InsertItemBefore() call would not insert a
150 // clone of newItem, it would actually insert newItem. To prevent that from
151 // happening we have to do the clone here, if necessary.
152 RefPtr
<DOMSVGNumber
> domItem
= aItem
.HasOwner() ? aItem
.Clone() : &aItem
;
155 MOZ_ASSERT(!error
.Failed());
156 return InsertItemBefore(*domItem
, 0, error
);
159 already_AddRefed
<DOMSVGNumber
> DOMSVGNumberList::GetItem(uint32_t index
,
160 ErrorResult
& error
) {
162 RefPtr
<DOMSVGNumber
> item
= IndexedGetter(index
, found
, error
);
164 error
.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR
);
166 return item
.forget();
169 already_AddRefed
<DOMSVGNumber
> DOMSVGNumberList::IndexedGetter(
170 uint32_t index
, bool& found
, ErrorResult
& error
) {
171 if (IsAnimValList()) {
172 Element()->FlushAnimations();
174 found
= index
< LengthNoFlush();
176 return GetItemAt(index
);
181 already_AddRefed
<DOMSVGNumber
> DOMSVGNumberList::InsertItemBefore(
182 DOMSVGNumber
& aItem
, uint32_t index
, ErrorResult
& error
) {
183 if (IsAnimValList()) {
184 error
.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR
);
188 index
= std::min(index
, LengthNoFlush());
189 if (index
>= DOMSVGNumber::MaxListIndex()) {
190 error
.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR
);
194 // must do this before changing anything!
195 RefPtr
<DOMSVGNumber
> domItem
= aItem
.HasOwner() ? aItem
.Clone() : &aItem
;
197 // Ensure we have enough memory so we can avoid complex error handling below:
198 if (!mItems
.SetCapacity(mItems
.Length() + 1, fallible
) ||
199 !InternalList().SetCapacity(InternalList().Length() + 1)) {
200 error
.Throw(NS_ERROR_OUT_OF_MEMORY
);
203 if (AnimListMirrorsBaseList()) {
204 if (!mAList
->mAnimVal
->mItems
.SetCapacity(
205 mAList
->mAnimVal
->mItems
.Length() + 1, fallible
)) {
206 error
.Throw(NS_ERROR_OUT_OF_MEMORY
);
211 AutoChangeNumberListNotifier
notifier(this);
212 // Now that we know we're inserting, keep animVal list in sync as necessary.
213 MaybeInsertNullInAnimValListAt(index
);
215 InternalList().InsertItem(index
, domItem
->ToSVGNumber());
216 MOZ_ALWAYS_TRUE(mItems
.InsertElementAt(index
, domItem
, fallible
));
218 // This MUST come after the insertion into InternalList(), or else under the
219 // insertion into InternalList() the values read from domItem would be bad
220 // data from InternalList() itself!:
221 domItem
->InsertingIntoList(this, AttrEnum(), index
, IsAnimValList());
223 UpdateListIndicesFromIndex(mItems
, index
+ 1);
225 return domItem
.forget();
228 already_AddRefed
<DOMSVGNumber
> DOMSVGNumberList::ReplaceItem(
229 DOMSVGNumber
& aItem
, uint32_t index
, ErrorResult
& error
) {
230 if (IsAnimValList()) {
231 error
.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR
);
235 if (index
>= LengthNoFlush()) {
236 error
.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR
);
240 // must do this before changing anything!
241 RefPtr
<DOMSVGNumber
> domItem
= aItem
.HasOwner() ? aItem
.Clone() : &aItem
;
243 AutoChangeNumberListNotifier
notifier(this);
245 // Notify any existing DOM item of removal *before* modifying the lists so
246 // that the DOM item can copy the *old* value at its index:
247 mItems
[index
]->RemovingFromList();
250 InternalList()[index
] = domItem
->ToSVGNumber();
251 mItems
[index
] = domItem
;
253 // This MUST come after the ToSVGPoint() call, otherwise that call
254 // would end up reading bad data from InternalList()!
255 domItem
->InsertingIntoList(this, AttrEnum(), index
, IsAnimValList());
257 return domItem
.forget();
260 already_AddRefed
<DOMSVGNumber
> DOMSVGNumberList::RemoveItem(
261 uint32_t index
, ErrorResult
& error
) {
262 if (IsAnimValList()) {
263 error
.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR
);
267 if (index
>= LengthNoFlush()) {
268 error
.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR
);
272 // Now that we know we're removing, keep animVal list in sync as necessary.
273 // Do this *before* touching InternalList() so the removed item can get its
275 MaybeRemoveItemFromAnimValListAt(index
);
277 // We have to return the removed item, so get it, creating it if necessary:
278 RefPtr
<DOMSVGNumber
> result
= GetItemAt(index
);
280 AutoChangeNumberListNotifier
notifier(this);
281 // Notify the DOM item of removal *before* modifying the lists so that the
282 // DOM item can copy its *old* value:
283 mItems
[index
]->RemovingFromList();
285 InternalList().RemoveItem(index
);
286 mItems
.RemoveElementAt(index
);
288 UpdateListIndicesFromIndex(mItems
, index
);
290 return result
.forget();
293 already_AddRefed
<DOMSVGNumber
> DOMSVGNumberList::GetItemAt(uint32_t aIndex
) {
294 MOZ_ASSERT(aIndex
< mItems
.Length());
296 if (!mItems
[aIndex
]) {
298 new DOMSVGNumber(this, AttrEnum(), aIndex
, IsAnimValList());
300 RefPtr
<DOMSVGNumber
> result
= mItems
[aIndex
];
301 return result
.forget();
304 void DOMSVGNumberList::MaybeInsertNullInAnimValListAt(uint32_t aIndex
) {
305 MOZ_ASSERT(!IsAnimValList(), "call from baseVal to animVal");
307 if (!AnimListMirrorsBaseList()) {
311 DOMSVGNumberList
* animVal
= mAList
->mAnimVal
;
313 MOZ_ASSERT(animVal
, "AnimListMirrorsBaseList() promised a non-null animVal");
314 MOZ_ASSERT(animVal
->mItems
.Length() == mItems
.Length(),
315 "animVal list not in sync!");
316 MOZ_ALWAYS_TRUE(animVal
->mItems
.InsertElementAt(aIndex
, nullptr, fallible
));
318 UpdateListIndicesFromIndex(animVal
->mItems
, aIndex
+ 1);
321 void DOMSVGNumberList::MaybeRemoveItemFromAnimValListAt(uint32_t aIndex
) {
322 MOZ_ASSERT(!IsAnimValList(), "call from baseVal to animVal");
324 if (!AnimListMirrorsBaseList()) {
328 // This needs to be a strong reference; otherwise, the RemovingFromList call
329 // below might drop the last reference to animVal before we're done with it.
330 RefPtr
<DOMSVGNumberList
> animVal
= mAList
->mAnimVal
;
332 MOZ_ASSERT(animVal
, "AnimListMirrorsBaseList() promised a non-null animVal");
333 MOZ_ASSERT(animVal
->mItems
.Length() == mItems
.Length(),
334 "animVal list not in sync!");
336 if (animVal
->mItems
[aIndex
]) {
337 animVal
->mItems
[aIndex
]->RemovingFromList();
339 animVal
->mItems
.RemoveElementAt(aIndex
);
341 UpdateListIndicesFromIndex(animVal
->mItems
, aIndex
);
344 } // namespace mozilla::dom