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/StaticPrefs_dom.h"
15 #include "mozilla/dom/SVGElement.h"
17 using namespace mozilla::dom
;
19 // See the comments in this file's header!
23 nsresult
SVGAnimatedPathSegList::SetBaseValueString(const nsAString
& aValue
) {
24 SVGPathData newBaseValue
;
26 // The spec says that the path data is parsed and accepted up to the first
27 // error encountered, so we don't return early if an error occurs. However,
28 // we do want to throw any error code from setAttribute if there's a problem.
29 nsresult rv
= newBaseValue
.SetValueFromString(aValue
);
31 // We must send these notifications *before* changing mBaseVal! Our baseVal's
32 // DOM wrapper list may have to remove DOM items from itself, and any removed
33 // DOM items need to copy their internal counterpart's values *before* we
34 // change them. See the comments in
35 // DOMSVGPathSegList::InternalListWillChangeTo().
36 DOMSVGPathSegList
* baseValWrapper
= nullptr;
37 DOMSVGPathSegList
* animValWrapper
= nullptr;
38 if (StaticPrefs::dom_svg_pathSeg_enabled()) {
39 baseValWrapper
= DOMSVGPathSegList::GetDOMWrapperIfExists(GetBaseValKey());
41 baseValWrapper
->InternalListWillChangeTo(newBaseValue
);
44 if (!IsAnimating()) { // DOM anim val wraps our base val too!
46 DOMSVGPathSegList::GetDOMWrapperIfExists(GetAnimValKey());
48 animValWrapper
->InternalListWillChangeTo(newBaseValue
);
53 // Only now may we modify mBaseVal!
55 // We don't need to call DidChange* here - we're only called by
56 // SVGElement::ParseAttribute under Element::SetAttr,
57 // which takes care of notifying.
59 mBaseVal
.SwapWith(newBaseValue
);
63 void SVGAnimatedPathSegList::ClearBaseValue() {
64 if (StaticPrefs::dom_svg_pathSeg_enabled()) {
65 // We must send these notifications *before* changing mBaseVal! (See above.)
67 DOMSVGPathSegList
* baseValWrapper
=
68 DOMSVGPathSegList::GetDOMWrapperIfExists(GetBaseValKey());
70 baseValWrapper
->InternalListWillChangeTo(SVGPathData());
73 if (!IsAnimating()) { // DOM anim val wraps our base val too!
74 DOMSVGPathSegList
* animValWrapper
=
75 DOMSVGPathSegList::GetDOMWrapperIfExists(GetAnimValKey());
77 animValWrapper
->InternalListWillChangeTo(SVGPathData());
86 nsresult
SVGAnimatedPathSegList::SetAnimValue(const SVGPathData
& aNewAnimValue
,
87 SVGElement
* aElement
) {
88 // Note that a new animation may totally change the number of items in the
89 // animVal list, either replacing what was essentially a mirror of the
90 // baseVal list, or else replacing and overriding an existing animation.
91 // Unfortunately it is not possible for us to reliably distinguish between
92 // calls to this method that are setting a new sample for an existing
93 // animation, and calls that are setting the first sample of an animation
94 // that will override an existing animation. In the case of DOMSVGPathSegList
95 // the InternalListWillChangeTo method is not virtually free as it is for the
96 // other DOM list classes, so this is a shame. We'd quite like to be able to
97 // skip the call if possible.
99 if (StaticPrefs::dom_svg_pathSeg_enabled()) {
100 // We must send these notifications *before* changing mAnimVal! (See above.)
102 DOMSVGPathSegList
* domWrapper
=
103 DOMSVGPathSegList::GetDOMWrapperIfExists(GetAnimValKey());
105 domWrapper
->InternalListWillChangeTo(aNewAnimValue
);
109 mAnimVal
= MakeUnique
<SVGPathData
>();
111 nsresult rv
= mAnimVal
->CopyFrom(aNewAnimValue
);
113 // OOM. We clear the animation and, importantly, ClearAnimValue() ensures
114 // that mAnimVal's DOM wrapper (if any) is kept in sync!
115 ClearAnimValue(aElement
);
117 aElement
->DidAnimatePathSegList();
121 void SVGAnimatedPathSegList::ClearAnimValue(SVGElement
* aElement
) {
122 if (StaticPrefs::dom_svg_pathSeg_enabled()) {
123 // We must send these notifications *before* changing mAnimVal! (See above.)
125 DOMSVGPathSegList
* domWrapper
=
126 DOMSVGPathSegList::GetDOMWrapperIfExists(GetAnimValKey());
128 // When all animation ends, animVal simply mirrors baseVal, which may have
129 // a different number of items to the last active animated value.
131 domWrapper
->InternalListWillChangeTo(mBaseVal
);
135 aElement
->DidAnimatePathSegList();
138 bool SVGAnimatedPathSegList::IsRendered() const {
139 return mAnimVal
? !mAnimVal
->IsEmpty() : !mBaseVal
.IsEmpty();
142 UniquePtr
<SMILAttr
> SVGAnimatedPathSegList::ToSMILAttr(SVGElement
* aElement
) {
143 return MakeUnique
<SMILAnimatedPathSegList
>(this, aElement
);
146 nsresult
SVGAnimatedPathSegList::SMILAnimatedPathSegList::ValueFromString(
147 const nsAString
& aStr
, const dom::SVGAnimationElement
* /*aSrcElement*/,
148 SMILValue
& aValue
, bool& aPreventCachingOfSandwich
) const {
149 SMILValue
val(SVGPathSegListSMILType::Singleton());
150 SVGPathDataAndInfo
* list
= static_cast<SVGPathDataAndInfo
*>(val
.mU
.mPtr
);
151 nsresult rv
= list
->SetValueFromString(aStr
);
152 if (NS_SUCCEEDED(rv
)) {
153 list
->SetElement(mElement
);
154 aValue
= std::move(val
);
159 SMILValue
SVGAnimatedPathSegList::SMILAnimatedPathSegList::GetBaseValue()
161 // To benefit from Return Value Optimization and avoid copy constructor calls
162 // due to our use of return-by-value, we must return the exact same object
163 // from ALL return points. This function must only return THIS variable:
166 SMILValue
tmp(SVGPathSegListSMILType::Singleton());
167 auto* list
= static_cast<SVGPathDataAndInfo
*>(tmp
.mU
.mPtr
);
168 nsresult rv
= list
->CopyFrom(mVal
->mBaseVal
);
169 if (NS_SUCCEEDED(rv
)) {
170 list
->SetElement(mElement
);
171 val
= std::move(tmp
);
176 nsresult
SVGAnimatedPathSegList::SMILAnimatedPathSegList::SetAnimValue(
177 const SMILValue
& aValue
) {
178 NS_ASSERTION(aValue
.mType
== SVGPathSegListSMILType::Singleton(),
179 "Unexpected type to assign animated value");
180 if (aValue
.mType
== SVGPathSegListSMILType::Singleton()) {
181 mVal
->SetAnimValue(*static_cast<SVGPathDataAndInfo
*>(aValue
.mU
.mPtr
),
187 void SVGAnimatedPathSegList::SMILAnimatedPathSegList::ClearAnimValue() {
188 if (mVal
->mAnimVal
) {
189 mVal
->ClearAnimValue(mElement
);
193 size_t SVGAnimatedPathSegList::SizeOfExcludingThis(
194 MallocSizeOf aMallocSizeOf
) const {
195 size_t total
= mBaseVal
.SizeOfExcludingThis(aMallocSizeOf
);
197 mAnimVal
->SizeOfIncludingThis(aMallocSizeOf
);
202 } // namespace mozilla