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 "DOMSVGLengthList.h"
9 #include "SVGElement.h"
10 #include "DOMSVGLength.h"
12 #include "SVGAnimatedLengthList.h"
13 #include "mozilla/dom/SVGLengthListBinding.h"
16 // See the comment in this file's header.
18 // local helper functions
21 using mozilla::dom::DOMSVGLength
;
23 void UpdateListIndicesFromIndex(FallibleTArray
<DOMSVGLength
*>& aItemsArray
,
24 uint32_t aStartingIndex
) {
25 uint32_t length
= aItemsArray
.Length();
27 for (uint32_t i
= aStartingIndex
; i
< length
; ++i
) {
29 aItemsArray
[i
]->UpdateListIndex(i
);
36 namespace mozilla::dom
{
38 // We could use NS_IMPL_CYCLE_COLLECTION(, except that in Unlink() we need to
39 // clear our DOMSVGAnimatedLengthList's weak ref to us to be safe. (The other
40 // option would be to not unlink and rely on the breaking of the other edges in
41 // the cycle, as NS_SVG_VAL_IMPL_CYCLE_COLLECTION does.)
42 NS_IMPL_CYCLE_COLLECTION_CLASS(DOMSVGLengthList
)
44 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(DOMSVGLengthList
)
46 if (tmp
->IsAnimValList()) {
47 tmp
->mAList
->mAnimVal
= nullptr;
49 tmp
->mAList
->mBaseVal
= nullptr;
51 NS_IMPL_CYCLE_COLLECTION_UNLINK(mAList
)
53 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
54 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
55 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(DOMSVGLengthList
)
56 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAList
)
57 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
58 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(DOMSVGLengthList
)
59 NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
60 NS_IMPL_CYCLE_COLLECTION_TRACE_END
62 NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMSVGLengthList
)
63 NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMSVGLengthList
)
65 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMSVGLengthList
)
66 NS_INTERFACE_MAP_ENTRY(DOMSVGLengthList
)
67 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
68 NS_INTERFACE_MAP_ENTRY(nsISupports
)
71 void DOMSVGLengthList::IndexedSetter(uint32_t index
, DOMSVGLength
& newValue
,
73 // Need to take a ref to the return value so it does not leak.
74 RefPtr
<DOMSVGLength
> ignored
= ReplaceItem(newValue
, index
, error
);
78 JSObject
* DOMSVGLengthList::WrapObject(JSContext
* cx
,
79 JS::Handle
<JSObject
*> aGivenProto
) {
80 return mozilla::dom::SVGLengthList_Binding::Wrap(cx
, this, aGivenProto
);
83 void DOMSVGLengthList::InternalListLengthWillChange(uint32_t aNewLength
) {
84 uint32_t oldLength
= mItems
.Length();
86 if (aNewLength
> DOMSVGLength::MaxListIndex()) {
87 // It's safe to get out of sync with our internal list as long as we have
88 // FEWER items than it does.
89 aNewLength
= DOMSVGLength::MaxListIndex();
92 RefPtr
<DOMSVGLengthList
> kungFuDeathGrip
;
93 if (aNewLength
< oldLength
) {
94 // RemovingFromList() might clear last reference to |this|.
95 // Retain a temporary reference to keep from dying before returning.
96 kungFuDeathGrip
= this;
99 // If our length will decrease, notify the items that will be removed:
100 for (uint32_t i
= aNewLength
; i
< oldLength
; ++i
) {
102 mItems
[i
]->RemovingFromList();
106 if (!mItems
.SetLength(aNewLength
, fallible
)) {
107 // We silently ignore SetLength OOM failure since being out of sync is safe
108 // so long as we have *fewer* items than our internal list.
113 // If our length has increased, null out the new pointers:
114 for (uint32_t i
= oldLength
; i
< aNewLength
; ++i
) {
119 SVGLengthList
& DOMSVGLengthList::InternalList() const {
120 SVGAnimatedLengthList
* alist
= Element()->GetAnimatedLengthList(AttrEnum());
121 return IsAnimValList() && alist
->mAnimVal
? *alist
->mAnimVal
125 // ----------------------------------------------------------------------------
127 void DOMSVGLengthList::Clear(ErrorResult
& aError
) {
128 if (IsAnimValList()) {
129 aError
.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR
);
133 if (LengthNoFlush() > 0) {
134 AutoChangeLengthListNotifier
notifier(this);
135 // Notify any existing DOM items of removal *before* truncating the lists
136 // so that they can find their SVGLength internal counterparts and copy
137 // their values. This also notifies the animVal list:
138 mAList
->InternalBaseValListWillChangeTo(SVGLengthList());
141 InternalList().Clear();
145 already_AddRefed
<DOMSVGLength
> DOMSVGLengthList::Initialize(
146 DOMSVGLength
& newItem
, ErrorResult
& error
) {
147 if (IsAnimValList()) {
148 error
.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR
);
152 // If newItem already has an owner or is reflecting an attribute, we should
153 // insert a clone of newItem, and for consistency, this should happen even if
154 // *this* is the list that newItem is currently in. Note that in the case of
155 // newItem being in this list, the Clear() call before the InsertItemBefore()
156 // call would remove it from this list, and so the InsertItemBefore() call
157 // would not insert a clone of newItem, it would actually insert newItem. To
158 // prevent that from happening we have to do the clone here, if necessary.
160 RefPtr
<DOMSVGLength
> domItem
= &newItem
;
161 if (domItem
->HasOwner()) {
162 domItem
= domItem
->Copy();
167 MOZ_ASSERT(!rv
.Failed());
168 return InsertItemBefore(*domItem
, 0, error
);
171 already_AddRefed
<DOMSVGLength
> DOMSVGLengthList::GetItem(uint32_t index
,
172 ErrorResult
& error
) {
174 RefPtr
<DOMSVGLength
> item
= IndexedGetter(index
, found
, error
);
176 error
.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR
);
178 return item
.forget();
181 already_AddRefed
<DOMSVGLength
> DOMSVGLengthList::IndexedGetter(
182 uint32_t index
, bool& found
, ErrorResult
& error
) {
183 if (IsAnimValList()) {
184 Element()->FlushAnimations();
186 found
= index
< LengthNoFlush();
188 return GetItemAt(index
);
193 already_AddRefed
<DOMSVGLength
> DOMSVGLengthList::InsertItemBefore(
194 DOMSVGLength
& newItem
, uint32_t index
, ErrorResult
& error
) {
195 if (IsAnimValList()) {
196 error
.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR
);
200 index
= std::min(index
, LengthNoFlush());
201 if (index
>= DOMSVGLength::MaxListIndex()) {
202 error
.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR
);
206 RefPtr
<DOMSVGLength
> domItem
= &newItem
;
207 if (domItem
->HasOwner()) {
208 domItem
= domItem
->Copy(); // must do this before changing anything!
211 // Ensure we have enough memory so we can avoid complex error handling below:
212 if (!mItems
.SetCapacity(mItems
.Length() + 1, fallible
) ||
213 !InternalList().SetCapacity(InternalList().Length() + 1)) {
214 error
.Throw(NS_ERROR_OUT_OF_MEMORY
);
217 if (AnimListMirrorsBaseList()) {
218 if (!mAList
->mAnimVal
->mItems
.SetCapacity(
219 mAList
->mAnimVal
->mItems
.Length() + 1, fallible
)) {
220 error
.Throw(NS_ERROR_OUT_OF_MEMORY
);
225 AutoChangeLengthListNotifier
notifier(this);
226 // Now that we know we're inserting, keep animVal list in sync as necessary.
227 MaybeInsertNullInAnimValListAt(index
);
229 InternalList().InsertItem(index
, domItem
->ToSVGLength());
230 MOZ_ALWAYS_TRUE(mItems
.InsertElementAt(index
, domItem
.get(), fallible
));
232 // This MUST come after the insertion into InternalList(), or else under the
233 // insertion into InternalList() the values read from domItem would be bad
234 // data from InternalList() itself!:
235 domItem
->InsertingIntoList(this, AttrEnum(), index
, IsAnimValList());
237 UpdateListIndicesFromIndex(mItems
, index
+ 1);
239 return domItem
.forget();
242 already_AddRefed
<DOMSVGLength
> DOMSVGLengthList::ReplaceItem(
243 DOMSVGLength
& newItem
, uint32_t index
, ErrorResult
& error
) {
244 if (IsAnimValList()) {
245 error
.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR
);
249 if (index
>= LengthNoFlush()) {
250 error
.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR
);
254 RefPtr
<DOMSVGLength
> domItem
= &newItem
;
255 if (domItem
->HasOwner()) {
256 domItem
= domItem
->Copy(); // must do this before changing anything!
259 AutoChangeLengthListNotifier
notifier(this);
261 // Notify any existing DOM item of removal *before* modifying the lists so
262 // that the DOM item can copy the *old* value at its index:
263 mItems
[index
]->RemovingFromList();
266 InternalList()[index
] = domItem
->ToSVGLength();
267 mItems
[index
] = domItem
;
269 // This MUST come after the ToSVGPoint() call, otherwise that call
270 // would end up reading bad data from InternalList()!
271 domItem
->InsertingIntoList(this, AttrEnum(), index
, IsAnimValList());
273 return domItem
.forget();
276 already_AddRefed
<DOMSVGLength
> DOMSVGLengthList::RemoveItem(
277 uint32_t index
, ErrorResult
& error
) {
278 if (IsAnimValList()) {
279 error
.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR
);
283 if (index
>= LengthNoFlush()) {
284 error
.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR
);
288 AutoChangeLengthListNotifier
notifier(this);
289 // Now that we know we're removing, keep animVal list in sync as necessary.
290 // Do this *before* touching InternalList() so the removed item can get its
292 MaybeRemoveItemFromAnimValListAt(index
);
294 // We have to return the removed item, so get it, creating it if necessary:
295 RefPtr
<DOMSVGLength
> result
= GetItemAt(index
);
297 // Notify the DOM item of removal *before* modifying the lists so that the
298 // DOM item can copy its *old* value:
299 mItems
[index
]->RemovingFromList();
301 InternalList().RemoveItem(index
);
302 mItems
.RemoveElementAt(index
);
304 UpdateListIndicesFromIndex(mItems
, index
);
306 return result
.forget();
309 already_AddRefed
<DOMSVGLength
> DOMSVGLengthList::GetItemAt(uint32_t aIndex
) {
310 MOZ_ASSERT(aIndex
< mItems
.Length());
312 if (!mItems
[aIndex
]) {
314 new DOMSVGLength(this, AttrEnum(), aIndex
, IsAnimValList());
316 RefPtr
<DOMSVGLength
> result
= mItems
[aIndex
];
317 return result
.forget();
320 void DOMSVGLengthList::MaybeInsertNullInAnimValListAt(uint32_t aIndex
) {
321 MOZ_ASSERT(!IsAnimValList(), "call from baseVal to animVal");
323 if (!AnimListMirrorsBaseList()) {
327 DOMSVGLengthList
* animVal
= mAList
->mAnimVal
;
329 MOZ_ASSERT(animVal
, "AnimListMirrorsBaseList() promised a non-null animVal");
330 MOZ_ASSERT(animVal
->mItems
.Length() == mItems
.Length(),
331 "animVal list not in sync!");
332 MOZ_ALWAYS_TRUE(animVal
->mItems
.InsertElementAt(aIndex
, nullptr, fallible
));
334 UpdateListIndicesFromIndex(animVal
->mItems
, aIndex
+ 1);
337 void DOMSVGLengthList::MaybeRemoveItemFromAnimValListAt(uint32_t aIndex
) {
338 MOZ_ASSERT(!IsAnimValList(), "call from baseVal to animVal");
340 if (!AnimListMirrorsBaseList()) {
344 // This needs to be a strong reference; otherwise, the RemovingFromList call
345 // below might drop the last reference to animVal before we're done with it.
346 RefPtr
<DOMSVGLengthList
> animVal
= mAList
->mAnimVal
;
348 MOZ_ASSERT(animVal
, "AnimListMirrorsBaseList() promised a non-null animVal");
349 MOZ_ASSERT(animVal
->mItems
.Length() == mItems
.Length(),
350 "animVal list not in sync!");
352 if (animVal
->mItems
[aIndex
]) {
353 animVal
->mItems
[aIndex
]->RemovingFromList();
355 animVal
->mItems
.RemoveElementAt(aIndex
);
357 UpdateListIndicesFromIndex(animVal
->mItems
, aIndex
);
360 } // namespace mozilla::dom