Bug 1635304 [wpt PR 23392] - Remove erroneous named properties object test, a=testonly
[gecko.git] / dom / svg / SVGAnimatedInteger.cpp
blob6e4e6db26e461591597a28ac592f8c883758958d
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"
9 #include "nsError.h"
10 #include "SMILIntegerType.h"
11 #include "SVGAttrTearoffTable.h"
12 #include "mozilla/SMILValue.h"
13 #include "mozilla/SVGContentUtils.h"
15 using namespace mozilla::dom;
17 namespace mozilla {
19 /* Implementation */
21 static SVGAttrTearoffTable<SVGAnimatedInteger,
22 SVGAnimatedInteger::DOMAnimatedInteger>
23 sSVGAnimatedIntegerTearoffTable;
25 nsresult SVGAnimatedInteger::SetBaseValueString(const nsAString& aValueAsString,
26 SVGElement* aSVGElement) {
27 bool success;
28 auto token = SVGContentUtils::GetAndEnsureOneToken(aValueAsString, success);
30 if (!success) {
31 return NS_ERROR_DOM_SYNTAX_ERR;
34 int32_t value;
36 if (!SVGContentUtils::ParseInteger(token, value)) {
37 return NS_ERROR_DOM_SYNTAX_ERR;
40 mIsBaseSet = true;
41 mBaseVal = value;
42 if (!mIsAnimated) {
43 mAnimVal = mBaseVal;
44 } else {
45 aSVGElement->AnimationNeedsResample();
47 return NS_OK;
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) {
61 return;
64 mBaseVal = aValue;
65 mIsBaseSet = true;
66 if (!mIsAnimated) {
67 mAnimVal = mBaseVal;
68 } else {
69 aSVGElement->AnimationNeedsResample();
71 aSVGElement->DidChangeInteger(mAttrEnum);
74 void SVGAnimatedInteger::SetAnimValue(int aValue, SVGElement* aSVGElement) {
75 if (mIsAnimated && aValue == mAnimVal) {
76 return;
78 mAnimVal = aValue;
79 mIsAnimated = true;
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 {
106 int32_t val;
108 if (!SVGContentUtils::ParseInteger(aStr, val)) {
109 return NS_ERROR_DOM_SYNTAX_ERR;
112 SMILValue smilVal(SMILIntegerType::Singleton());
113 smilVal.mU.mInt = val;
114 aValue = smilVal;
115 aPreventCachingOfSandwich = false;
116 return NS_OK;
119 SMILValue SVGAnimatedInteger::SMILInteger::GetBaseValue() const {
120 SMILValue val(SMILIntegerType::Singleton());
121 val.mU.mInt = mVal->mBaseVal;
122 return val;
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);
140 return NS_OK;
143 } // namespace mozilla