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 "SVGAnimatedPathSegList.h"
11 #include "DOMSVGPathSegList.h"
12 #include "SVGPathSegListSMILType.h"
13 #include "mozilla/SMILValue.h"
14 #include "mozilla/dom/SVGElement.h"
16 using namespace mozilla::dom
;
18 // See the comments in this file's header!
22 nsresult
SVGAnimatedPathSegList::SetBaseValueString(const nsAString
& aValue
) {
23 SVGPathData newBaseValue
;
25 // The spec says that the path data is parsed and accepted up to the first
26 // error encountered, so we don't return early if an error occurs. However,
27 // we do want to throw any error code from setAttribute if there's a problem.
28 nsresult rv
= newBaseValue
.SetValueFromString(aValue
);
30 // We must send these notifications *before* changing mBaseVal! Our baseVal's
31 // DOM wrapper list may have to remove DOM items from itself, and any removed
32 // DOM items need to copy their internal counterpart's values *before* we
33 // change them. See the comments in
34 // DOMSVGPathSegList::InternalListWillChangeTo().
35 DOMSVGPathSegList
* baseValWrapper
= nullptr;
36 DOMSVGPathSegList
* animValWrapper
= nullptr;
37 if (StaticPrefs::dom_svg_pathSeg_enabled()) {
38 baseValWrapper
= DOMSVGPathSegList::GetDOMWrapperIfExists(GetBaseValKey());
40 baseValWrapper
->InternalListWillChangeTo(newBaseValue
);
43 if (!IsAnimating()) { // DOM anim val wraps our base val too!
45 DOMSVGPathSegList::GetDOMWrapperIfExists(GetAnimValKey());
47 animValWrapper
->InternalListWillChangeTo(newBaseValue
);
52 // Only now may we modify mBaseVal!
54 // We don't need to call DidChange* here - we're only called by
55 // SVGElement::ParseAttribute under Element::SetAttr,
56 // which takes care of notifying.
58 mBaseVal
.SwapWith(newBaseValue
);
62 void SVGAnimatedPathSegList::ClearBaseValue() {
63 if (StaticPrefs::dom_svg_pathSeg_enabled()) {
64 // We must send these notifications *before* changing mBaseVal! (See above.)
66 DOMSVGPathSegList
* baseValWrapper
=
67 DOMSVGPathSegList::GetDOMWrapperIfExists(GetBaseValKey());
69 baseValWrapper
->InternalListWillChangeTo(SVGPathData());
72 if (!IsAnimating()) { // DOM anim val wraps our base val too!
73 DOMSVGPathSegList
* animValWrapper
=
74 DOMSVGPathSegList::GetDOMWrapperIfExists(GetAnimValKey());
76 animValWrapper
->InternalListWillChangeTo(SVGPathData());
85 nsresult
SVGAnimatedPathSegList::SetAnimValue(const SVGPathData
& aNewAnimValue
,
86 SVGElement
* aElement
) {
87 // Note that a new animation may totally change the number of items in the
88 // animVal list, either replacing what was essentially a mirror of the
89 // baseVal list, or else replacing and overriding an existing animation.
90 // Unfortunately it is not possible for us to reliably distinguish between
91 // calls to this method that are setting a new sample for an existing
92 // animation, and calls that are setting the first sample of an animation
93 // that will override an existing animation. In the case of DOMSVGPathSegList
94 // the InternalListWillChangeTo method is not virtually free as it is for the
95 // other DOM list classes, so this is a shame. We'd quite like to be able to
96 // skip the call if possible.
98 if (StaticPrefs::dom_svg_pathSeg_enabled()) {
99 // We must send these notifications *before* changing mAnimVal! (See above.)
101 DOMSVGPathSegList
* domWrapper
=
102 DOMSVGPathSegList::GetDOMWrapperIfExists(GetAnimValKey());
104 domWrapper
->InternalListWillChangeTo(aNewAnimValue
);
108 mAnimVal
= MakeUnique
<SVGPathData
>();
110 nsresult rv
= mAnimVal
->CopyFrom(aNewAnimValue
);
112 // OOM. We clear the animation and, importantly, ClearAnimValue() ensures
113 // that mAnimVal's DOM wrapper (if any) is kept in sync!
114 ClearAnimValue(aElement
);
116 aElement
->DidAnimatePathSegList();
120 void SVGAnimatedPathSegList::ClearAnimValue(SVGElement
* aElement
) {
121 if (StaticPrefs::dom_svg_pathSeg_enabled()) {
122 // We must send these notifications *before* changing mAnimVal! (See above.)
124 DOMSVGPathSegList
* domWrapper
=
125 DOMSVGPathSegList::GetDOMWrapperIfExists(GetAnimValKey());
127 // When all animation ends, animVal simply mirrors baseVal, which may have
128 // a different number of items to the last active animated value.
130 domWrapper
->InternalListWillChangeTo(mBaseVal
);
134 aElement
->DidAnimatePathSegList();
137 bool SVGAnimatedPathSegList::IsRendered() const {
138 return mAnimVal
? !mAnimVal
->IsEmpty() : !mBaseVal
.IsEmpty();
141 UniquePtr
<SMILAttr
> SVGAnimatedPathSegList::ToSMILAttr(SVGElement
* aElement
) {
142 return MakeUnique
<SMILAnimatedPathSegList
>(this, aElement
);
145 nsresult
SVGAnimatedPathSegList::SMILAnimatedPathSegList::ValueFromString(
146 const nsAString
& aStr
, const dom::SVGAnimationElement
* /*aSrcElement*/,
147 SMILValue
& aValue
, bool& aPreventCachingOfSandwich
) const {
148 SMILValue
val(SVGPathSegListSMILType::Singleton());
149 SVGPathDataAndInfo
* list
= static_cast<SVGPathDataAndInfo
*>(val
.mU
.mPtr
);
150 nsresult rv
= list
->SetValueFromString(aStr
);
151 if (NS_SUCCEEDED(rv
)) {
152 list
->SetElement(mElement
);
153 aValue
= std::move(val
);
158 SMILValue
SVGAnimatedPathSegList::SMILAnimatedPathSegList::GetBaseValue()
160 // To benefit from Return Value Optimization and avoid copy constructor calls
161 // due to our use of return-by-value, we must return the exact same object
162 // from ALL return points. This function must only return THIS variable:
165 SMILValue
tmp(SVGPathSegListSMILType::Singleton());
166 auto* list
= static_cast<SVGPathDataAndInfo
*>(tmp
.mU
.mPtr
);
167 nsresult rv
= list
->CopyFrom(mVal
->mBaseVal
);
168 if (NS_SUCCEEDED(rv
)) {
169 list
->SetElement(mElement
);
170 val
= std::move(tmp
);
175 nsresult
SVGAnimatedPathSegList::SMILAnimatedPathSegList::SetAnimValue(
176 const SMILValue
& aValue
) {
177 NS_ASSERTION(aValue
.mType
== SVGPathSegListSMILType::Singleton(),
178 "Unexpected type to assign animated value");
179 if (aValue
.mType
== SVGPathSegListSMILType::Singleton()) {
180 mVal
->SetAnimValue(*static_cast<SVGPathDataAndInfo
*>(aValue
.mU
.mPtr
),
186 void SVGAnimatedPathSegList::SMILAnimatedPathSegList::ClearAnimValue() {
187 if (mVal
->mAnimVal
) {
188 mVal
->ClearAnimValue(mElement
);
192 size_t SVGAnimatedPathSegList::SizeOfExcludingThis(
193 MallocSizeOf aMallocSizeOf
) const {
194 size_t total
= mBaseVal
.SizeOfExcludingThis(aMallocSizeOf
);
196 mAnimVal
->SizeOfIncludingThis(aMallocSizeOf
);
201 } // namespace mozilla