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 "SVGAnimatedNumberList.h"
11 #include "DOMSVGAnimatedNumberList.h"
12 #include "SVGNumberListSMILType.h"
13 #include "mozilla/SMILValue.h"
14 #include "mozilla/dom/SVGElement.h"
16 using namespace mozilla::dom
;
20 nsresult
SVGAnimatedNumberList::SetBaseValueString(const nsAString
& aValue
) {
21 SVGNumberList newBaseValue
;
22 MOZ_TRY(newBaseValue
.SetValueFromString(aValue
));
24 DOMSVGAnimatedNumberList
* domWrapper
=
25 DOMSVGAnimatedNumberList::GetDOMWrapperIfExists(this);
27 // We must send this notification *before* changing mBaseVal! If the length
28 // of our baseVal is being reduced, our baseVal's DOM wrapper list may have
29 // to remove DOM items from itself, and any removed DOM items need to copy
30 // their internal counterpart values *before* we change them.
32 domWrapper
->InternalBaseValListWillChangeTo(newBaseValue
);
35 // We don't need to call DidChange* here - we're only called by
36 // SVGElement::ParseAttribute under Element::SetAttr,
37 // which takes care of notifying.
40 mBaseVal
.SwapWith(newBaseValue
);
44 void SVGAnimatedNumberList::ClearBaseValue(uint32_t aAttrEnum
) {
45 DOMSVGAnimatedNumberList
* domWrapper
=
46 DOMSVGAnimatedNumberList::GetDOMWrapperIfExists(this);
48 // We must send this notification *before* changing mBaseVal! (See above.)
49 domWrapper
->InternalBaseValListWillChangeTo(SVGNumberList());
56 nsresult
SVGAnimatedNumberList::SetAnimValue(const SVGNumberList
& aNewAnimValue
,
59 DOMSVGAnimatedNumberList
* domWrapper
=
60 DOMSVGAnimatedNumberList::GetDOMWrapperIfExists(this);
62 // A new animation may totally change the number of items in the animVal
63 // list, replacing what was essentially a mirror of the baseVal list, or
64 // else replacing and overriding an existing animation. When this happens
65 // we must try and keep our animVal's DOM wrapper in sync (see the comment
66 // in DOMSVGAnimatedNumberList::InternalBaseValListWillChangeTo).
68 // It's not possible for us to reliably distinguish between calls to this
69 // method that are setting a new sample for an existing animation, and
70 // calls that are setting the first sample of an animation that will
71 // override an existing animation. Happily it's cheap to just blindly
72 // notify our animVal's DOM wrapper of its internal counterpart's new value
73 // each time this method is called, so that's what we do.
75 // Note that we must send this notification *before* setting or changing
76 // mAnimVal! (See the comment in SetBaseValueString above.)
78 domWrapper
->InternalAnimValListWillChangeTo(aNewAnimValue
);
81 mAnimVal
= MakeUnique
<SVGNumberList
>();
83 nsresult rv
= mAnimVal
->CopyFrom(aNewAnimValue
);
85 // OOM. We clear the animation, and, importantly, ClearAnimValue() ensures
86 // that mAnimVal and its DOM wrapper (if any) will have the same length!
87 ClearAnimValue(aElement
, aAttrEnum
);
90 aElement
->DidAnimateNumberList(aAttrEnum
);
94 void SVGAnimatedNumberList::ClearAnimValue(SVGElement
* aElement
,
96 DOMSVGAnimatedNumberList
* domWrapper
=
97 DOMSVGAnimatedNumberList::GetDOMWrapperIfExists(this);
99 // When all animation ends, animVal simply mirrors baseVal, which may have
100 // a different number of items to the last active animated value. We must
101 // keep the length of our animVal's DOM wrapper list in sync, and again we
102 // must do that before touching mAnimVal. See comments above.
104 domWrapper
->InternalAnimValListWillChangeTo(mBaseVal
);
107 aElement
->DidAnimateNumberList(aAttrEnum
);
110 UniquePtr
<SMILAttr
> SVGAnimatedNumberList::ToSMILAttr(SVGElement
* aSVGElement
,
112 return MakeUnique
<SMILAnimatedNumberList
>(this, aSVGElement
, aAttrEnum
);
115 nsresult
SVGAnimatedNumberList::SMILAnimatedNumberList::ValueFromString(
116 const nsAString
& aStr
, const dom::SVGAnimationElement
* /*aSrcElement*/,
117 SMILValue
& aValue
, bool& aPreventCachingOfSandwich
) const {
118 SMILValue
val(&SVGNumberListSMILType::sSingleton
);
119 SVGNumberListAndInfo
* nlai
= static_cast<SVGNumberListAndInfo
*>(val
.mU
.mPtr
);
120 nsresult rv
= nlai
->SetValueFromString(aStr
);
121 if (NS_SUCCEEDED(rv
)) {
122 nlai
->SetInfo(mElement
);
123 aValue
= std::move(val
);
128 SMILValue
SVGAnimatedNumberList::SMILAnimatedNumberList::GetBaseValue() const {
129 // To benefit from Return Value Optimization and avoid copy constructor calls
130 // due to our use of return-by-value, we must return the exact same object
131 // from ALL return points. This function must only return THIS variable:
134 SMILValue
tmp(&SVGNumberListSMILType::sSingleton
);
135 SVGNumberListAndInfo
* nlai
= static_cast<SVGNumberListAndInfo
*>(tmp
.mU
.mPtr
);
136 nsresult rv
= nlai
->CopyFrom(mVal
->mBaseVal
);
137 if (NS_SUCCEEDED(rv
)) {
138 nlai
->SetInfo(mElement
);
144 nsresult
SVGAnimatedNumberList::SMILAnimatedNumberList::SetAnimValue(
145 const SMILValue
& aValue
) {
146 NS_ASSERTION(aValue
.mType
== &SVGNumberListSMILType::sSingleton
,
147 "Unexpected type to assign animated value");
148 if (aValue
.mType
== &SVGNumberListSMILType::sSingleton
) {
149 mVal
->SetAnimValue(*static_cast<SVGNumberListAndInfo
*>(aValue
.mU
.mPtr
),
150 mElement
, mAttrEnum
);
155 void SVGAnimatedNumberList::SMILAnimatedNumberList::ClearAnimValue() {
156 if (mVal
->mAnimVal
) {
157 mVal
->ClearAnimValue(mElement
, mAttrEnum
);
161 } // namespace mozilla