Bug 1882593 [wpt PR 44836] - Add test for unknown, invalid ancillary chunk which...
[gecko.git] / dom / svg / DOMSVGTransformList.cpp
blob251df5777160da99910397b3e92169ce0ac96cd4
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 "DOMSVGTransformList.h"
9 #include "mozilla/dom/SVGElement.h"
10 #include "mozilla/dom/SVGMatrix.h"
11 #include "mozilla/dom/SVGTransformListBinding.h"
12 #include "DOMSVGTransform.h"
13 #include "SVGAnimatedTransformList.h"
14 #include "nsError.h"
15 #include <algorithm>
17 // local helper functions
18 namespace {
20 void UpdateListIndicesFromIndex(
21 FallibleTArray<mozilla::dom::DOMSVGTransform*>& aItemsArray,
22 uint32_t aStartingIndex) {
23 uint32_t length = aItemsArray.Length();
25 for (uint32_t i = aStartingIndex; i < length; ++i) {
26 if (aItemsArray[i]) {
27 aItemsArray[i]->UpdateListIndex(i);
32 } // namespace
34 namespace mozilla::dom {
36 // We could use NS_IMPL_CYCLE_COLLECTION(, except that in Unlink() we need to
37 // clear our SVGAnimatedTransformList's weak ref to us to be safe. (The other
38 // option would be to not unlink and rely on the breaking of the other edges in
39 // the cycle, as NS_SVG_VAL_IMPL_CYCLE_COLLECTION does.)
40 NS_IMPL_CYCLE_COLLECTION_CLASS(DOMSVGTransformList)
42 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(DOMSVGTransformList)
43 if (tmp->mAList) {
44 if (tmp->IsAnimValList()) {
45 tmp->mAList->mAnimVal = nullptr;
46 } else {
47 tmp->mAList->mBaseVal = nullptr;
49 NS_IMPL_CYCLE_COLLECTION_UNLINK(mAList)
51 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
52 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
53 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(DOMSVGTransformList)
54 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAList)
55 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
56 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(DOMSVGTransformList)
57 NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
58 NS_IMPL_CYCLE_COLLECTION_TRACE_END
60 NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMSVGTransformList)
61 NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMSVGTransformList)
63 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMSVGTransformList)
64 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
65 NS_INTERFACE_MAP_ENTRY(nsISupports)
66 NS_INTERFACE_MAP_END
68 //----------------------------------------------------------------------
69 // DOMSVGTransformList methods:
71 JSObject* DOMSVGTransformList::WrapObject(JSContext* cx,
72 JS::Handle<JSObject*> aGivenProto) {
73 return mozilla::dom::SVGTransformList_Binding::Wrap(cx, this, aGivenProto);
76 void DOMSVGTransformList::InternalListLengthWillChange(uint32_t aNewLength) {
77 uint32_t oldLength = mItems.Length();
79 if (aNewLength > DOMSVGTransform::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 = DOMSVGTransform::MaxListIndex();
85 RefPtr<DOMSVGTransformList> 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) {
94 if (mItems[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.
102 mItems.Clear();
103 return;
106 // If our length has increased, null out the new pointers:
107 for (uint32_t i = oldLength; i < aNewLength; ++i) {
108 mItems[i] = nullptr;
112 SVGTransformList& DOMSVGTransformList::InternalList() const {
113 SVGAnimatedTransformList* alist = Element()->GetAnimatedTransformList();
114 return IsAnimValList() && alist->mAnimVal ? *alist->mAnimVal
115 : alist->mBaseVal;
118 //----------------------------------------------------------------------
119 void DOMSVGTransformList::Clear(ErrorResult& error) {
120 if (IsAnimValList()) {
121 error.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
122 return;
125 if (LengthNoFlush() > 0) {
126 AutoChangeTransformListNotifier notifier(this);
127 // Notify any existing DOM items of removal *before* truncating the lists
128 // so that they can find their DOMSVGTransform internal counterparts and
129 // copy their values. This also notifies the animVal list:
130 mAList->InternalBaseValListWillChangeLengthTo(0);
132 mItems.Clear();
133 auto* alist = Element()->GetAnimatedTransformList();
134 alist->mBaseVal.Clear();
135 alist->mIsBaseSet = false;
139 already_AddRefed<DOMSVGTransform> DOMSVGTransformList::Initialize(
140 DOMSVGTransform& newItem, ErrorResult& error) {
141 if (IsAnimValList()) {
142 error.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
143 return nullptr;
146 // If newItem is already in a list we should insert a clone of newItem, and
147 // for consistency, this should happen even if *this* is the list that
148 // newItem is currently in. Note that in the case of newItem being in this
149 // list, the Clear() call before the InsertItemBefore() call would remove it
150 // from this list, and so the InsertItemBefore() call would not insert a
151 // clone of newItem, it would actually insert newItem. To prevent that from
152 // happening we have to do the clone here, if necessary.
154 RefPtr<DOMSVGTransform> domItem = &newItem;
155 if (domItem->HasOwner()) {
156 domItem = newItem.Clone();
159 Clear(error);
160 MOZ_ASSERT(!error.Failed(), "How could this fail?");
161 return InsertItemBefore(*domItem, 0, error);
164 already_AddRefed<DOMSVGTransform> DOMSVGTransformList::GetItem(
165 uint32_t index, ErrorResult& error) {
166 bool found;
167 RefPtr<DOMSVGTransform> item = IndexedGetter(index, found, error);
168 if (!found) {
169 error.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
171 return item.forget();
174 already_AddRefed<DOMSVGTransform> DOMSVGTransformList::IndexedGetter(
175 uint32_t index, bool& found, ErrorResult& error) {
176 if (IsAnimValList()) {
177 Element()->FlushAnimations();
179 found = index < LengthNoFlush();
180 if (found) {
181 return GetItemAt(index);
183 return nullptr;
186 already_AddRefed<DOMSVGTransform> DOMSVGTransformList::InsertItemBefore(
187 DOMSVGTransform& newItem, uint32_t index, ErrorResult& error) {
188 if (IsAnimValList()) {
189 error.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
190 return nullptr;
193 index = std::min(index, LengthNoFlush());
194 if (index >= DOMSVGTransform::MaxListIndex()) {
195 error.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
196 return nullptr;
199 RefPtr<DOMSVGTransform> domItem = &newItem;
200 if (newItem.HasOwner()) {
201 domItem = newItem.Clone(); // must do this before changing anything!
204 // Ensure we have enough memory so we can avoid complex error handling below:
205 if (!mItems.SetCapacity(mItems.Length() + 1, fallible) ||
206 !InternalList().SetCapacity(InternalList().Length() + 1)) {
207 error.Throw(NS_ERROR_OUT_OF_MEMORY);
208 return nullptr;
210 if (AnimListMirrorsBaseList()) {
211 if (!mAList->mAnimVal->mItems.SetCapacity(
212 mAList->mAnimVal->mItems.Length() + 1, fallible)) {
213 error.Throw(NS_ERROR_OUT_OF_MEMORY);
214 return nullptr;
218 AutoChangeTransformListNotifier notifier(this);
219 // Now that we know we're inserting, keep animVal list in sync as necessary.
220 MaybeInsertNullInAnimValListAt(index);
222 InternalList().InsertItem(index, domItem->ToSVGTransform());
223 MOZ_ALWAYS_TRUE(mItems.InsertElementAt(index, domItem.get(), fallible));
225 // This MUST come after the insertion into InternalList(), or else under the
226 // insertion into InternalList() the values read from domItem would be bad
227 // data from InternalList() itself!:
228 domItem->InsertingIntoList(this, index, IsAnimValList());
230 UpdateListIndicesFromIndex(mItems, index + 1);
232 return domItem.forget();
235 already_AddRefed<DOMSVGTransform> DOMSVGTransformList::ReplaceItem(
236 DOMSVGTransform& newItem, uint32_t index, ErrorResult& error) {
237 if (IsAnimValList()) {
238 error.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
239 return nullptr;
242 if (index >= LengthNoFlush()) {
243 error.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
244 return nullptr;
247 RefPtr<DOMSVGTransform> domItem = &newItem;
248 if (newItem.HasOwner()) {
249 domItem = newItem.Clone(); // must do this before changing anything!
252 AutoChangeTransformListNotifier notifier(this);
253 if (mItems[index]) {
254 // Notify any existing DOM item of removal *before* modifying the lists so
255 // that the DOM item can copy the *old* value at its index:
256 mItems[index]->RemovingFromList();
259 InternalList()[index] = domItem->ToSVGTransform();
260 mItems[index] = domItem;
262 // This MUST come after the ToSVGPoint() call, otherwise that call
263 // would end up reading bad data from InternalList()!
264 domItem->InsertingIntoList(this, index, IsAnimValList());
266 return domItem.forget();
269 already_AddRefed<DOMSVGTransform> DOMSVGTransformList::RemoveItem(
270 uint32_t index, ErrorResult& error) {
271 if (IsAnimValList()) {
272 error.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
273 return nullptr;
276 if (index >= LengthNoFlush()) {
277 error.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
278 return nullptr;
281 AutoChangeTransformListNotifier notifier(this);
282 // Now that we know we're removing, keep animVal list in sync as necessary.
283 // Do this *before* touching InternalList() so the removed item can get its
284 // internal value.
285 MaybeRemoveItemFromAnimValListAt(index);
287 // We have to return the removed item, so get it, creating it if necessary:
288 RefPtr<DOMSVGTransform> result = GetItemAt(index);
290 // Notify the DOM item of removal *before* modifying the lists so that the
291 // DOM item can copy its *old* value:
292 result->RemovingFromList();
294 InternalList().RemoveItem(index);
295 mItems.RemoveElementAt(index);
297 UpdateListIndicesFromIndex(mItems, index);
299 return result.forget();
302 already_AddRefed<DOMSVGTransform>
303 DOMSVGTransformList::CreateSVGTransformFromMatrix(const DOMMatrix2DInit& matrix,
304 ErrorResult& rv) {
305 RefPtr<DOMSVGTransform> result = new DOMSVGTransform(matrix, rv);
306 return result.forget();
309 already_AddRefed<DOMSVGTransform> DOMSVGTransformList::Consolidate(
310 ErrorResult& error) {
311 if (IsAnimValList()) {
312 error.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR);
313 return nullptr;
316 if (LengthNoFlush() == 0) {
317 return nullptr;
320 // Note that SVG 1.1 says, "The consolidation operation creates new
321 // SVGTransform object as the first and only item in the list" hence, even if
322 // LengthNoFlush() == 1 we can't return that one item (after making it a
323 // matrix type). We must orphan the existing item and then make a new one.
325 // First calculate our matrix
326 gfxMatrix mx = InternalList().GetConsolidationMatrix();
328 // Then orphan the existing items
329 Clear(error);
330 MOZ_ASSERT(!error.Failed(), "How could this fail?");
332 // And append the new transform
333 RefPtr<DOMSVGTransform> transform = new DOMSVGTransform(mx);
334 return InsertItemBefore(*transform, LengthNoFlush(), error);
337 //----------------------------------------------------------------------
338 // Implementation helpers:
340 already_AddRefed<DOMSVGTransform> DOMSVGTransformList::GetItemAt(
341 uint32_t aIndex) {
342 MOZ_ASSERT(aIndex < mItems.Length());
344 if (!mItems[aIndex]) {
345 mItems[aIndex] = new DOMSVGTransform(this, aIndex, IsAnimValList());
347 RefPtr<DOMSVGTransform> result = mItems[aIndex];
348 return result.forget();
351 void DOMSVGTransformList::MaybeInsertNullInAnimValListAt(uint32_t aIndex) {
352 MOZ_ASSERT(!IsAnimValList(), "call from baseVal to animVal");
354 if (!AnimListMirrorsBaseList()) {
355 return;
358 DOMSVGTransformList* animVal = mAList->mAnimVal;
360 MOZ_ASSERT(animVal, "AnimListMirrorsBaseList() promised a non-null animVal");
361 MOZ_ASSERT(animVal->mItems.Length() == mItems.Length(),
362 "animVal list not in sync!");
363 MOZ_ALWAYS_TRUE(animVal->mItems.InsertElementAt(aIndex, nullptr, fallible));
365 UpdateListIndicesFromIndex(animVal->mItems, aIndex + 1);
368 void DOMSVGTransformList::MaybeRemoveItemFromAnimValListAt(uint32_t aIndex) {
369 MOZ_ASSERT(!IsAnimValList(), "call from baseVal to animVal");
371 if (!AnimListMirrorsBaseList()) {
372 return;
375 // This needs to be a strong reference; otherwise, the RemovingFromList call
376 // below might drop the last reference to animVal before we're done with it.
377 RefPtr<DOMSVGTransformList> animVal = mAList->mAnimVal;
379 MOZ_ASSERT(animVal, "AnimListMirrorsBaseList() promised a non-null animVal");
380 MOZ_ASSERT(animVal->mItems.Length() == mItems.Length(),
381 "animVal list not in sync!");
383 if (animVal->mItems[aIndex]) {
384 animVal->mItems[aIndex]->RemovingFromList();
386 animVal->mItems.RemoveElementAt(aIndex);
388 UpdateListIndicesFromIndex(animVal->mItems, aIndex);
391 } // namespace mozilla::dom