Skip to content

Commit 747e38c

Browse files
committed
Bug 1999989 Part 2 - refactor SVGAnimatedViewBox interface r=emilio
Differential Revision: https://phabricator.services.mozilla.com/D272472
1 parent 1a8092d commit 747e38c

File tree

3 files changed

+46
-40
lines changed

3 files changed

+46
-40
lines changed

dom/svg/SVGAnimatedViewBox.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,28 @@ void SVGAnimatedViewBox::SetAnimValue(const SVGViewBox& aRect,
148148
aSVGElement->DidAnimateViewBox();
149149
}
150150

151+
void SVGAnimatedViewBox::SetBaseField(float aValue, SVGElement* aSVGElement,
152+
float& aField) {
153+
if (!mHasBaseVal) {
154+
aField = aValue;
155+
return;
156+
}
157+
if (aField == aValue) {
158+
return;
159+
}
160+
AutoChangeViewBoxNotifier notifier(this, aSVGElement);
161+
aField = aValue;
162+
}
163+
151164
void SVGAnimatedViewBox::SetBaseValue(const SVGViewBox& aRect,
152-
SVGElement* aSVGElement) {
153-
if (!mHasBaseVal || mBaseVal == aRect) {
154-
// This method is used to set a single x, y, width
155-
// or height value. It can't create a base value
156-
// as the other components may be undefined. We record
157-
// the new value though, so as not to lose data.
158-
mBaseVal = aRect;
165+
SVGElement* aSVGElement,
166+
bool aDoSetAttr) {
167+
// Comparison against mBaseVal is only valid if we currently have a base val.
168+
if (mHasBaseVal && mBaseVal == aRect) {
159169
return;
160170
}
161171

162-
AutoChangeViewBoxNotifier notifier(this, aSVGElement);
172+
AutoChangeViewBoxNotifier notifier(this, aSVGElement, aDoSetAttr);
163173

164174
mBaseVal = aRect;
165175
mHasBaseVal = true;
@@ -174,15 +184,7 @@ nsresult SVGAnimatedViewBox::SetBaseValueString(const nsAString& aValue,
174184
if (NS_FAILED(rv)) {
175185
return rv;
176186
}
177-
// Comparison against mBaseVal is only valid if we currently have a base val.
178-
if (mHasBaseVal && viewBox == mBaseVal) {
179-
return NS_OK;
180-
}
181-
182-
AutoChangeViewBoxNotifier notifier(this, aSVGElement, aDoSetAttr);
183-
mHasBaseVal = true;
184-
mBaseVal = viewBox;
185-
187+
SetBaseValue(viewBox, aSVGElement, aDoSetAttr);
186188
return NS_OK;
187189
}
188190

dom/svg/SVGAnimatedViewBox.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,20 @@ class SVGAnimatedViewBox {
9999
}
100100

101101
const SVGViewBox& GetBaseValue() const { return mBaseVal; }
102-
void SetBaseValue(const SVGViewBox& aRect, SVGElement* aSVGElement);
102+
void SetBaseX(float aX, SVGElement* aSVGElement) {
103+
SetBaseField(aX, aSVGElement, mBaseVal.x);
104+
}
105+
void SetBaseY(float aY, SVGElement* aSVGElement) {
106+
SetBaseField(aY, aSVGElement, mBaseVal.y);
107+
}
108+
void SetBaseWidth(float aWidth, SVGElement* aSVGElement) {
109+
SetBaseField(aWidth, aSVGElement, mBaseVal.width);
110+
}
111+
void SetBaseHeight(float aHeight, SVGElement* aSVGElement) {
112+
SetBaseField(aHeight, aSVGElement, mBaseVal.height);
113+
}
114+
void SetBaseValue(const SVGViewBox& aRect, SVGElement* aSVGElement,
115+
bool aDoSetAttr);
103116
const SVGViewBox& GetAnimValue() const {
104117
return mAnimVal ? *mAnimVal : mBaseVal;
105118
}
@@ -119,6 +132,8 @@ class SVGAnimatedViewBox {
119132
UniquePtr<SMILAttr> ToSMILAttr(SVGElement* aSVGElement);
120133

121134
private:
135+
void SetBaseField(float aHeight, SVGElement* aSVGElement, float& aElement);
136+
122137
SVGViewBox mBaseVal;
123138
UniquePtr<SVGViewBox> mAnimVal;
124139
bool mHasBaseVal;

dom/svg/SVGRect.cpp

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ JSObject* SVGRect::WrapObject(JSContext* aCx,
3838
float SVGRect::X() {
3939
switch (mType) {
4040
case RectType::AnimValue:
41-
static_cast<SVGElement*>(mParent->AsElement())->FlushAnimations();
41+
static_cast<SVGElement*>(mParent.get())->FlushAnimations();
4242
return mVal->GetAnimValue().x;
4343
case RectType::BaseValue:
4444
return mVal->GetBaseValue().x;
@@ -50,7 +50,7 @@ float SVGRect::X() {
5050
float SVGRect::Y() {
5151
switch (mType) {
5252
case RectType::AnimValue:
53-
static_cast<SVGElement*>(mParent->AsElement())->FlushAnimations();
53+
static_cast<SVGElement*>(mParent.get())->FlushAnimations();
5454
return mVal->GetAnimValue().y;
5555
case RectType::BaseValue:
5656
return mVal->GetBaseValue().y;
@@ -62,7 +62,7 @@ float SVGRect::Y() {
6262
float SVGRect::Width() {
6363
switch (mType) {
6464
case RectType::AnimValue:
65-
static_cast<SVGElement*>(mParent->AsElement())->FlushAnimations();
65+
static_cast<SVGElement*>(mParent.get())->FlushAnimations();
6666
return mVal->GetAnimValue().width;
6767
case RectType::BaseValue:
6868
return mVal->GetBaseValue().width;
@@ -74,7 +74,7 @@ float SVGRect::Width() {
7474
float SVGRect::Height() {
7575
switch (mType) {
7676
case RectType::AnimValue:
77-
static_cast<SVGElement*>(mParent->AsElement())->FlushAnimations();
77+
static_cast<SVGElement*>(mParent.get())->FlushAnimations();
7878
return mVal->GetAnimValue().height;
7979
case RectType::BaseValue:
8080
return mVal->GetBaseValue().height;
@@ -88,12 +88,9 @@ void SVGRect::SetX(float aX, ErrorResult& aRv) {
8888
case RectType::AnimValue:
8989
aRv.ThrowNoModificationAllowedError("Animated values cannot be set");
9090
return;
91-
case RectType::BaseValue: {
92-
SVGViewBox rect = mVal->GetBaseValue();
93-
rect.x = aX;
94-
mVal->SetBaseValue(rect, static_cast<SVGElement*>(mParent->AsElement()));
91+
case RectType::BaseValue:
92+
mVal->SetBaseX(aX, static_cast<SVGElement*>(mParent.get()));
9593
return;
96-
}
9794
default:
9895
mRect.x = aX;
9996
}
@@ -104,12 +101,9 @@ void SVGRect::SetY(float aY, ErrorResult& aRv) {
104101
case RectType::AnimValue:
105102
aRv.ThrowNoModificationAllowedError("Animated values cannot be set");
106103
return;
107-
case RectType::BaseValue: {
108-
SVGViewBox rect = mVal->GetBaseValue();
109-
rect.y = aY;
110-
mVal->SetBaseValue(rect, static_cast<SVGElement*>(mParent->AsElement()));
104+
case RectType::BaseValue:
105+
mVal->SetBaseY(aY, static_cast<SVGElement*>(mParent.get()));
111106
return;
112-
}
113107
default:
114108
mRect.y = aY;
115109
}
@@ -121,9 +115,7 @@ void SVGRect::SetWidth(float aWidth, ErrorResult& aRv) {
121115
aRv.ThrowNoModificationAllowedError("Animated values cannot be set");
122116
return;
123117
case RectType::BaseValue: {
124-
SVGViewBox rect = mVal->GetBaseValue();
125-
rect.width = aWidth;
126-
mVal->SetBaseValue(rect, static_cast<SVGElement*>(mParent->AsElement()));
118+
mVal->SetBaseWidth(aWidth, static_cast<SVGElement*>(mParent.get()));
127119
return;
128120
}
129121
default:
@@ -136,12 +128,9 @@ void SVGRect::SetHeight(float aHeight, ErrorResult& aRv) {
136128
case RectType::AnimValue:
137129
aRv.ThrowNoModificationAllowedError("Animated values cannot be set");
138130
return;
139-
case RectType::BaseValue: {
140-
SVGViewBox rect = mVal->GetBaseValue();
141-
rect.height = aHeight;
142-
mVal->SetBaseValue(rect, static_cast<SVGElement*>(mParent->AsElement()));
131+
case RectType::BaseValue:
132+
mVal->SetBaseHeight(aHeight, static_cast<SVGElement*>(mParent.get()));
143133
return;
144-
}
145134
default:
146135
mRect.height = aHeight;
147136
}

0 commit comments

Comments
 (0)