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 "DOMSVGNumber.h"
8 #include "DOMSVGNumberList.h"
9 #include "DOMSVGAnimatedNumberList.h"
10 #include "SVGAnimatedNumberList.h"
11 #include "nsSVGElement.h"
13 #include "nsContentUtils.h" // for NS_ENSURE_FINITE
14 #include "mozilla/dom/SVGNumberBinding.h"
16 // See the architecture comment in DOMSVGAnimatedNumberList.h.
20 // We could use NS_IMPL_CYCLE_COLLECTION(, except that in Unlink() we need to
21 // clear our list's weak ref to us to be safe. (The other option would be to
22 // not unlink and rely on the breaking of the other edges in the cycle, as
23 // NS_SVG_VAL_IMPL_CYCLE_COLLECTION does.)
24 NS_IMPL_CYCLE_COLLECTION_CLASS(DOMSVGNumber
)
26 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(DOMSVGNumber
)
27 // We may not belong to a list, so we must null check tmp->mList.
29 tmp
->mList
->mItems
[tmp
->mListIndex
] = nullptr;
31 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
32 NS_IMPL_CYCLE_COLLECTION_UNLINK(mList
)
33 NS_IMPL_CYCLE_COLLECTION_UNLINK(mParent
)
34 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
35 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(DOMSVGNumber
)
36 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mList
)
37 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParent
)
38 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
39 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(DOMSVGNumber
)
40 NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
41 NS_IMPL_CYCLE_COLLECTION_TRACE_END
43 NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMSVGNumber
)
44 NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMSVGNumber
)
46 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMSVGNumber
)
47 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
48 NS_INTERFACE_MAP_ENTRY(nsISupports
)
51 //----------------------------------------------------------------------
52 // Helper class: AutoChangeNumberNotifier
53 // Stack-based helper class to pair calls to WillChangeNumberList and
54 // DidChangeNumberList.
55 class MOZ_RAII AutoChangeNumberNotifier
58 explicit AutoChangeNumberNotifier(DOMSVGNumber
* aNumber MOZ_GUARD_OBJECT_NOTIFIER_PARAM
)
61 MOZ_GUARD_OBJECT_NOTIFIER_INIT
;
62 MOZ_ASSERT(mNumber
, "Expecting non-null number");
63 MOZ_ASSERT(mNumber
->HasOwner(),
64 "Expecting list to have an owner for notification");
66 mNumber
->Element()->WillChangeNumberList(mNumber
->mAttrEnum
);
69 ~AutoChangeNumberNotifier()
71 mNumber
->Element()->DidChangeNumberList(mNumber
->mAttrEnum
,
73 if (mNumber
->mList
->IsAnimating()) {
74 mNumber
->Element()->AnimationNeedsResample();
79 DOMSVGNumber
* const mNumber
;
80 nsAttrValue mEmptyOrOldValue
;
81 MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
84 DOMSVGNumber::DOMSVGNumber(DOMSVGNumberList
*aList
,
90 , mListIndex(aListIndex
)
91 , mAttrEnum(aAttrEnum
)
92 , mIsAnimValItem(aIsAnimValItem
)
95 // These shifts are in sync with the members in the header.
97 aAttrEnum
< (1 << 4) &&
98 aListIndex
<= MaxListIndex(),
101 MOZ_ASSERT(IndexIsValid(), "Bad index for DOMSVGNumber!");
104 DOMSVGNumber::DOMSVGNumber(nsISupports
* aParent
)
109 , mIsAnimValItem(false)
115 DOMSVGNumber::Value()
117 if (mIsAnimValItem
&& HasOwner()) {
118 Element()->FlushAnimations(); // May make HasOwner() == false
120 return HasOwner() ? InternalItem() : mValue
;
124 DOMSVGNumber::SetValue(float aValue
, ErrorResult
& aRv
)
126 if (mIsAnimValItem
) {
127 aRv
.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR
);
132 if (InternalItem() == aValue
) {
135 AutoChangeNumberNotifier
notifier(this);
136 InternalItem() = aValue
;
144 DOMSVGNumber::InsertingIntoList(DOMSVGNumberList
*aList
,
149 NS_ASSERTION(!HasOwner(), "Inserting item that is already in a list");
152 mAttrEnum
= aAttrEnum
;
153 mListIndex
= aListIndex
;
154 mIsAnimValItem
= aIsAnimValItem
;
156 MOZ_ASSERT(IndexIsValid(), "Bad index for DOMSVGNumber!");
160 DOMSVGNumber::RemovingFromList()
162 mValue
= InternalItem();
164 mIsAnimValItem
= false;
168 DOMSVGNumber::ToSVGNumber()
170 return HasOwner() ? InternalItem() : mValue
;
174 DOMSVGNumber::InternalItem()
176 SVGAnimatedNumberList
*alist
= Element()->GetAnimatedNumberList(mAttrEnum
);
177 return mIsAnimValItem
&& alist
->mAnimVal
?
178 (*alist
->mAnimVal
)[mListIndex
] :
179 alist
->mBaseVal
[mListIndex
];
184 DOMSVGNumber::IndexIsValid()
186 SVGAnimatedNumberList
*alist
= Element()->GetAnimatedNumberList(mAttrEnum
);
187 return (mIsAnimValItem
&&
188 mListIndex
< alist
->GetAnimValue().Length()) ||
190 mListIndex
< alist
->GetBaseValue().Length());
195 DOMSVGNumber::WrapObject(JSContext
* aCx
, JS::Handle
<JSObject
*> aGivenProto
)
197 return dom::SVGNumber_Binding::Wrap(aCx
, this, aGivenProto
);
200 } // namespace mozilla