Bug 1882593 [wpt PR 44836] - Add test for unknown, invalid ancillary chunk which...
[gecko.git] / dom / svg / SVGAnimatedViewBox.h
blobad550b6462e612d559760821d8b56628813e0cb3
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 #ifndef DOM_SVG_SVGANIMATEDVIEWBOX_H_
8 #define DOM_SVG_SVGANIMATEDVIEWBOX_H_
10 #include "nsCycleCollectionParticipant.h"
11 #include "nsError.h"
12 #include "SVGAttrTearoffTable.h"
13 #include "mozilla/Attributes.h"
14 #include "mozilla/SMILAttr.h"
15 #include "mozilla/UniquePtr.h"
16 #include "mozilla/dom/SVGAnimatedRect.h"
18 namespace mozilla {
20 class SMILValue;
22 namespace dom {
23 class SVGRect;
24 class SVGAnimationElement;
25 class SVGElement;
26 } // namespace dom
28 struct SVGViewBox {
29 float x, y;
30 float width, height;
31 bool none;
33 SVGViewBox() : x(0.0), y(0.0), width(0.0), height(0.0), none(true) {}
34 SVGViewBox(float aX, float aY, float aWidth, float aHeight)
35 : x(aX), y(aY), width(aWidth), height(aHeight), none(false) {}
36 bool operator==(const SVGViewBox& aOther) const;
38 static nsresult FromString(const nsAString& aStr, SVGViewBox* aViewBox);
41 class SVGAnimatedViewBox {
42 public:
43 friend class AutoChangeViewBoxNotifier;
44 using SVGElement = dom::SVGElement;
46 SVGAnimatedViewBox& operator=(const SVGAnimatedViewBox& aOther) {
47 mBaseVal = aOther.mBaseVal;
48 if (aOther.mAnimVal) {
49 mAnimVal = MakeUnique<SVGViewBox>(*aOther.mAnimVal);
51 mHasBaseVal = aOther.mHasBaseVal;
52 return *this;
55 void Init();
57 /**
58 * Returns true if the corresponding "viewBox" attribute defined a rectangle
59 * with finite values and nonnegative width/height.
60 * Returns false if the viewBox was set to an invalid
61 * string, or if any of the four rect values were too big to store in a
62 * float, or the width/height are negative.
64 bool HasRect() const;
66 /**
67 * Returns true if the corresponding "viewBox" attribute either defined a
68 * rectangle with finite values or the special "none" value.
70 bool IsExplicitlySet() const {
71 if (mAnimVal || mHasBaseVal) {
72 const SVGViewBox& rect = GetAnimValue();
73 return rect.none || (rect.width >= 0 && rect.height >= 0);
75 return false;
78 const SVGViewBox& GetBaseValue() const { return mBaseVal; }
79 void SetBaseValue(const SVGViewBox& aRect, SVGElement* aSVGElement);
80 const SVGViewBox& GetAnimValue() const {
81 return mAnimVal ? *mAnimVal : mBaseVal;
83 void SetAnimValue(const SVGViewBox& aRect, SVGElement* aSVGElement);
85 nsresult SetBaseValueString(const nsAString& aValue, SVGElement* aSVGElement,
86 bool aDoSetAttr);
87 void GetBaseValueString(nsAString& aValue) const;
89 already_AddRefed<dom::SVGAnimatedRect> ToSVGAnimatedRect(
90 SVGElement* aSVGElement);
92 already_AddRefed<dom::SVGRect> ToDOMBaseVal(SVGElement* aSVGElement);
94 already_AddRefed<dom::SVGRect> ToDOMAnimVal(SVGElement* aSVGElement);
96 UniquePtr<SMILAttr> ToSMILAttr(SVGElement* aSVGElement);
98 private:
99 SVGViewBox mBaseVal;
100 UniquePtr<SVGViewBox> mAnimVal;
101 bool mHasBaseVal;
103 public:
104 struct SMILViewBox : public SMILAttr {
105 public:
106 SMILViewBox(SVGAnimatedViewBox* aVal, SVGElement* aSVGElement)
107 : mVal(aVal), mSVGElement(aSVGElement) {}
109 // These will stay alive because a SMILAttr only lives as long
110 // as the Compositing step, and DOM elements don't get a chance to
111 // die during that.
112 SVGAnimatedViewBox* mVal;
113 SVGElement* mSVGElement;
115 // SMILAttr methods
116 nsresult ValueFromString(const nsAString& aStr,
117 const dom::SVGAnimationElement* aSrcElement,
118 SMILValue& aValue,
119 bool& aPreventCachingOfSandwich) const override;
120 SMILValue GetBaseValue() const override;
121 void ClearAnimValue() override;
122 nsresult SetAnimValue(const SMILValue& aValue) override;
125 static SVGAttrTearoffTable<SVGAnimatedViewBox, dom::SVGAnimatedRect>
126 sSVGAnimatedRectTearoffTable;
129 } // namespace mozilla
131 #endif // DOM_SVG_SVGANIMATEDVIEWBOX_H_