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 "SVGElement.h"
13 #include "nsContentUtils.h" // for NS_ENSURE_FINITE
14 #include "mozilla/dom/SVGNumberBinding.h"
15 #include "mozilla/dom/SVGSVGElement.h"
17 // See the architecture comment in DOMSVGAnimatedNumberList.h.
19 namespace mozilla::dom
{
21 // We could use NS_IMPL_CYCLE_COLLECTION(, except that in Unlink() we need to
22 // clear our list's weak ref to us to be safe. (The other option would be to
23 // not unlink and rely on the breaking of the other edges in the cycle, as
24 // NS_SVG_VAL_IMPL_CYCLE_COLLECTION does.)
25 NS_IMPL_CYCLE_COLLECTION_CLASS(DOMSVGNumber
)
27 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(DOMSVGNumber
)
28 // We may not belong to a list, so we must null check tmp->mList.
30 tmp
->mList
->mItems
[tmp
->mListIndex
] = nullptr;
32 NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
33 NS_IMPL_CYCLE_COLLECTION_UNLINK(mList
)
34 NS_IMPL_CYCLE_COLLECTION_UNLINK(mParent
)
35 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
36 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(DOMSVGNumber
)
37 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mList
)
38 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mParent
)
39 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
40 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(DOMSVGNumber
)
41 NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
42 NS_IMPL_CYCLE_COLLECTION_TRACE_END
44 DOMSVGNumber::DOMSVGNumber(DOMSVGNumberList
* aList
, uint8_t aAttrEnum
,
45 uint32_t aListIndex
, bool aIsAnimValItem
)
48 mListIndex(aListIndex
),
50 mIsAnimValItem(aIsAnimValItem
),
52 // These shifts are in sync with the members in the header.
53 MOZ_ASSERT(aList
&& aAttrEnum
< (1 << 4) && aListIndex
<= MaxListIndex(),
56 MOZ_ASSERT(IndexIsValid(), "Bad index for DOMSVGNumber!");
59 DOMSVGNumber::DOMSVGNumber(nsISupports
* aParent
)
64 mIsAnimValItem(false),
67 DOMSVGNumber::DOMSVGNumber(SVGSVGElement
* aParent
)
69 mParent(ToSupports(aParent
)),
72 mIsAnimValItem(false),
75 float DOMSVGNumber::Value() {
76 if (mIsAnimValItem
&& HasOwner()) {
77 Element()->FlushAnimations(); // May make HasOwner() == false
79 return HasOwner() ? InternalItem() : mValue
;
82 void DOMSVGNumber::SetValue(float aValue
, ErrorResult
& aRv
) {
84 aRv
.Throw(NS_ERROR_DOM_NO_MODIFICATION_ALLOWED_ERR
);
89 if (InternalItem() == aValue
) {
92 AutoChangeNumberListNotifier
notifier(this);
93 InternalItem() = aValue
;
100 void DOMSVGNumber::InsertingIntoList(DOMSVGNumberList
* aList
, uint8_t aAttrEnum
,
101 uint32_t aListIndex
, bool aIsAnimValItem
) {
102 NS_ASSERTION(!HasOwner(), "Inserting item that is already in a list");
105 mAttrEnum
= aAttrEnum
;
106 mListIndex
= aListIndex
;
107 mIsAnimValItem
= aIsAnimValItem
;
109 MOZ_ASSERT(IndexIsValid(), "Bad index for DOMSVGNumber!");
112 void DOMSVGNumber::RemovingFromList() {
113 mValue
= InternalItem();
115 mIsAnimValItem
= false;
118 float DOMSVGNumber::ToSVGNumber() {
119 return HasOwner() ? InternalItem() : mValue
;
122 float& DOMSVGNumber::InternalItem() {
123 SVGAnimatedNumberList
* alist
= Element()->GetAnimatedNumberList(mAttrEnum
);
124 return mIsAnimValItem
&& alist
->mAnimVal
? (*alist
->mAnimVal
)[mListIndex
]
125 : alist
->mBaseVal
[mListIndex
];
129 bool DOMSVGNumber::IndexIsValid() {
130 SVGAnimatedNumberList
* alist
= Element()->GetAnimatedNumberList(mAttrEnum
);
131 return (mIsAnimValItem
&& mListIndex
< alist
->GetAnimValue().Length()) ||
132 (!mIsAnimValItem
&& mListIndex
< alist
->GetBaseValue().Length());
136 JSObject
* DOMSVGNumber::WrapObject(JSContext
* aCx
,
137 JS::Handle
<JSObject
*> aGivenProto
) {
138 return SVGNumber_Binding::Wrap(aCx
, this, aGivenProto
);
141 } // namespace mozilla::dom