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 "SVGAnimatedInteger.h"
10 #include "SMILIntegerType.h"
11 #include "SVGAttrTearoffTable.h"
12 #include "mozilla/SMILValue.h"
13 #include "mozilla/SVGContentUtils.h"
15 using namespace mozilla::dom
;
21 static SVGAttrTearoffTable
<SVGAnimatedInteger
,
22 SVGAnimatedInteger::DOMAnimatedInteger
>
23 sSVGAnimatedIntegerTearoffTable
;
25 nsresult
SVGAnimatedInteger::SetBaseValueString(const nsAString
& aValueAsString
,
26 SVGElement
* aSVGElement
) {
28 auto token
= SVGContentUtils::GetAndEnsureOneToken(aValueAsString
, success
);
31 return NS_ERROR_DOM_SYNTAX_ERR
;
36 if (!SVGContentUtils::ParseInteger(token
, value
)) {
37 return NS_ERROR_DOM_SYNTAX_ERR
;
45 aSVGElement
->AnimationNeedsResample();
50 void SVGAnimatedInteger::GetBaseValueString(nsAString
& aValueAsString
) {
51 aValueAsString
.Truncate();
52 aValueAsString
.AppendInt(mBaseVal
);
55 void SVGAnimatedInteger::SetBaseValue(int aValue
, SVGElement
* aSVGElement
) {
56 // We can't just rely on SetParsedAttrValue (as called by DidChangeInteger)
57 // detecting redundant changes since it will compare false if the existing
58 // attribute value has an associated serialized version (a string value) even
59 // if the integers match due to the way integers are stored in nsAttrValue.
60 if (aValue
== mBaseVal
&& mIsBaseSet
) {
69 aSVGElement
->AnimationNeedsResample();
71 aSVGElement
->DidChangeInteger(mAttrEnum
);
74 void SVGAnimatedInteger::SetAnimValue(int aValue
, SVGElement
* aSVGElement
) {
75 if (mIsAnimated
&& aValue
== mAnimVal
) {
80 aSVGElement
->DidAnimateInteger(mAttrEnum
);
83 already_AddRefed
<DOMSVGAnimatedInteger
>
84 SVGAnimatedInteger::ToDOMAnimatedInteger(SVGElement
* aSVGElement
) {
85 RefPtr
<DOMAnimatedInteger
> domAnimatedInteger
=
86 sSVGAnimatedIntegerTearoffTable
.GetTearoff(this);
87 if (!domAnimatedInteger
) {
88 domAnimatedInteger
= new DOMAnimatedInteger(this, aSVGElement
);
89 sSVGAnimatedIntegerTearoffTable
.AddTearoff(this, domAnimatedInteger
);
92 return domAnimatedInteger
.forget();
95 SVGAnimatedInteger::DOMAnimatedInteger::~DOMAnimatedInteger() {
96 sSVGAnimatedIntegerTearoffTable
.RemoveTearoff(mVal
);
99 UniquePtr
<SMILAttr
> SVGAnimatedInteger::ToSMILAttr(SVGElement
* aSVGElement
) {
100 return MakeUnique
<SMILInteger
>(this, aSVGElement
);
103 nsresult
SVGAnimatedInteger::SMILInteger::ValueFromString(
104 const nsAString
& aStr
, const dom::SVGAnimationElement
* /*aSrcElement*/,
105 SMILValue
& aValue
, bool& aPreventCachingOfSandwich
) const {
108 if (!SVGContentUtils::ParseInteger(aStr
, val
)) {
109 return NS_ERROR_DOM_SYNTAX_ERR
;
112 SMILValue
smilVal(SMILIntegerType::Singleton());
113 smilVal
.mU
.mInt
= val
;
115 aPreventCachingOfSandwich
= false;
119 SMILValue
SVGAnimatedInteger::SMILInteger::GetBaseValue() const {
120 SMILValue
val(SMILIntegerType::Singleton());
121 val
.mU
.mInt
= mVal
->mBaseVal
;
125 void SVGAnimatedInteger::SMILInteger::ClearAnimValue() {
126 if (mVal
->mIsAnimated
) {
127 mVal
->mIsAnimated
= false;
128 mVal
->mAnimVal
= mVal
->mBaseVal
;
129 mSVGElement
->DidAnimateInteger(mVal
->mAttrEnum
);
133 nsresult
SVGAnimatedInteger::SMILInteger::SetAnimValue(
134 const SMILValue
& aValue
) {
135 NS_ASSERTION(aValue
.mType
== SMILIntegerType::Singleton(),
136 "Unexpected type to assign animated value");
137 if (aValue
.mType
== SMILIntegerType::Singleton()) {
138 mVal
->SetAnimValue(int(aValue
.mU
.mInt
), mSVGElement
);
143 } // namespace mozilla