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