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 "SVGAnimatedBoolean.h"
9 #include "DOMSVGAnimatedBoolean.h"
11 #include "SMILBoolType.h"
12 #include "SVGAttrTearoffTable.h"
13 #include "mozilla/SMILValue.h"
15 using namespace mozilla::dom
;
21 //----------------------------------------------------------------------
22 // Helper class: AutoChangeBooleanNotifier
23 // Stack-based helper class to ensure DidChangeBoolean is called.
24 class MOZ_RAII AutoChangeBooleanNotifier
{
26 AutoChangeBooleanNotifier(SVGAnimatedBoolean
* aBoolean
,
27 SVGElement
* aSVGElement
, bool aDoSetAttr
= true)
28 : mBoolean(aBoolean
), mSVGElement(aSVGElement
), mDoSetAttr(aDoSetAttr
) {
29 MOZ_ASSERT(mBoolean
, "Expecting non-null boolean");
30 MOZ_ASSERT(mSVGElement
, "Expecting non-null element");
33 ~AutoChangeBooleanNotifier() {
35 mSVGElement
->DidChangeBoolean(mBoolean
->mAttrEnum
);
37 if (mBoolean
->mIsAnimated
) {
38 mSVGElement
->AnimationNeedsResample();
43 SVGAnimatedBoolean
* const mBoolean
;
44 SVGElement
* const mSVGElement
;
48 static inline SVGAttrTearoffTable
<SVGAnimatedBoolean
, DOMSVGAnimatedBoolean
>&
49 SVGAnimatedBooleanTearoffTable() {
50 static SVGAttrTearoffTable
<SVGAnimatedBoolean
, DOMSVGAnimatedBoolean
>
51 sSVGAnimatedBooleanTearoffTable
;
52 return sSVGAnimatedBooleanTearoffTable
;
55 static bool GetValueFromString(const nsAString
& aValueAsString
, bool& aValue
) {
56 if (aValueAsString
.EqualsLiteral("true")) {
60 if (aValueAsString
.EqualsLiteral("false")) {
67 static nsresult
GetValueFromAtom(const nsAtom
* aValueAsAtom
, bool* aValue
) {
68 if (aValueAsAtom
== nsGkAtoms::_true
) {
72 if (aValueAsAtom
== nsGkAtoms::_false
) {
76 return NS_ERROR_DOM_SYNTAX_ERR
;
79 nsresult
SVGAnimatedBoolean::SetBaseValueAtom(const nsAtom
* aValue
,
80 SVGElement
* aSVGElement
) {
83 nsresult rv
= GetValueFromAtom(aValue
, &val
);
88 // We don't need to call DidChange* here - we're only called by
89 // SVGElement::ParseAttribute under Element::SetAttr,
90 // which takes care of notifying.
91 AutoChangeBooleanNotifier
notifier(this, aSVGElement
, false);
101 nsAtom
* SVGAnimatedBoolean::GetBaseValueAtom() const {
102 return mBaseVal
? nsGkAtoms::_true
: nsGkAtoms::_false
;
105 void SVGAnimatedBoolean::SetBaseValue(bool aValue
, SVGElement
* aSVGElement
) {
106 if (aValue
== mBaseVal
) {
110 AutoChangeBooleanNotifier
notifier(this, aSVGElement
);
118 void SVGAnimatedBoolean::SetAnimValue(bool aValue
, SVGElement
* aSVGElement
) {
119 if (mIsAnimated
&& mAnimVal
== aValue
) {
124 aSVGElement
->DidAnimateBoolean(mAttrEnum
);
127 already_AddRefed
<DOMSVGAnimatedBoolean
>
128 SVGAnimatedBoolean::ToDOMAnimatedBoolean(SVGElement
* aSVGElement
) {
129 RefPtr
<DOMSVGAnimatedBoolean
> domAnimatedBoolean
=
130 SVGAnimatedBooleanTearoffTable().GetTearoff(this);
131 if (!domAnimatedBoolean
) {
132 domAnimatedBoolean
= new DOMSVGAnimatedBoolean(this, aSVGElement
);
133 SVGAnimatedBooleanTearoffTable().AddTearoff(this, domAnimatedBoolean
);
136 return domAnimatedBoolean
.forget();
139 DOMSVGAnimatedBoolean::~DOMSVGAnimatedBoolean() {
140 SVGAnimatedBooleanTearoffTable().RemoveTearoff(mVal
);
143 UniquePtr
<SMILAttr
> SVGAnimatedBoolean::ToSMILAttr(SVGElement
* aSVGElement
) {
144 return MakeUnique
<SMILBool
>(this, aSVGElement
);
147 nsresult
SVGAnimatedBoolean::SMILBool::ValueFromString(
148 const nsAString
& aStr
, const SVGAnimationElement
* /*aSrcElement*/,
149 SMILValue
& aValue
, bool& aPreventCachingOfSandwich
) const {
151 if (!GetValueFromString(aStr
, value
)) {
152 return NS_ERROR_DOM_SYNTAX_ERR
;
155 SMILValue
val(SMILBoolType::Singleton());
156 val
.mU
.mBool
= value
;
162 SMILValue
SVGAnimatedBoolean::SMILBool::GetBaseValue() const {
163 SMILValue
val(SMILBoolType::Singleton());
164 val
.mU
.mBool
= mVal
->mBaseVal
;
168 void SVGAnimatedBoolean::SMILBool::ClearAnimValue() {
169 if (mVal
->mIsAnimated
) {
170 mVal
->mIsAnimated
= false;
171 mVal
->mAnimVal
= mVal
->mBaseVal
;
172 mSVGElement
->DidAnimateBoolean(mVal
->mAttrEnum
);
176 nsresult
SVGAnimatedBoolean::SMILBool::SetAnimValue(const SMILValue
& aValue
) {
177 NS_ASSERTION(aValue
.mType
== SMILBoolType::Singleton(),
178 "Unexpected type to assign animated value");
179 if (aValue
.mType
== SMILBoolType::Singleton()) {
180 mVal
->SetAnimValue(uint16_t(aValue
.mU
.mBool
), mSVGElement
);
185 } // namespace mozilla