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/. */
6 
7 #ifndef mozilla_dom_SVGImageElement_h
8 #define mozilla_dom_SVGImageElement_h
9 
10 #include "nsImageLoadingContent.h"
11 #include "SVGAnimatedLength.h"
12 #include "SVGAnimatedString.h"
13 #include "SVGGeometryElement.h"
14 #include "SVGAnimatedPreserveAspectRatio.h"
15 
16 nsresult NS_NewSVGImageElement(
17     nsIContent** aResult, already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo);
18 
19 class nsSVGImageFrame;
20 
21 namespace mozilla {
22 namespace dom {
23 class DOMSVGAnimatedPreserveAspectRatio;
24 
25 typedef SVGGeometryElement SVGImageElementBase;
26 
27 class SVGImageElement : public SVGImageElementBase,
28                         public nsImageLoadingContent {
29   friend class ::nsSVGImageFrame;
30 
31  protected:
32   explicit SVGImageElement(
33       already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo);
34   virtual ~SVGImageElement();
35   virtual JSObject* WrapNode(JSContext* aCx,
36                              JS::Handle<JSObject*> aGivenProto) override;
37   friend nsresult(::NS_NewSVGImageElement(
38       nsIContent** aResult,
39       already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo));
40 
41  public:
42   // interfaces:
43 
44   NS_DECL_ISUPPORTS_INHERITED
45 
46   // EventTarget
47   virtual void AsyncEventRunning(AsyncEventDispatcher* aEvent) override;
48 
49   // nsIContent interface
50   bool ParseAttribute(int32_t aNamespaceID, nsAtom* aAttribute,
51                       const nsAString& aValue,
52                       nsIPrincipal* aMaybeScriptedPrincipal,
53                       nsAttrValue& aResult) override;
54   virtual nsresult AfterSetAttr(int32_t aNamespaceID, nsAtom* aName,
55                                 const nsAttrValue* aValue,
56                                 const nsAttrValue* aOldValue,
57                                 nsIPrincipal* aSubjectPrincipal,
58                                 bool aNotify) override;
IsNodeOfType(uint32_t aFlags)59   bool IsNodeOfType(uint32_t aFlags) const override {
60     // <imag> is not really a SVGGeometryElement, we should
61     // ignore eSHAPE flag accepted by SVGGeometryElement.
62     return SVGGraphicsElement::IsNodeOfType(aFlags);
63   }
64 
65   virtual nsresult BindToTree(BindContext&, nsINode& aParent) override;
66   virtual void UnbindFromTree(bool aNullParent) override;
67 
68   virtual EventStates IntrinsicState() const override;
69 
70   NS_IMETHOD_(bool) IsAttributeMapped(const nsAtom* name) const override;
71 
72   // SVGGeometryElement methods:
73   virtual bool GetGeometryBounds(
74       Rect* aBounds, const StrokeOptions& aStrokeOptions,
75       const Matrix& aToBoundsSpace,
76       const Matrix* aToNonScalingStrokeSpace = nullptr) override;
77   virtual already_AddRefed<Path> BuildPath(PathBuilder* aBuilder) override;
78 
79   // nsSVGSVGElement methods:
80   virtual bool HasValidDimensions() const override;
81 
82   virtual nsresult Clone(dom::NodeInfo*, nsINode** aResult) const override;
83 
84   nsresult CopyInnerTo(mozilla::dom::Element* aDest);
85 
86   void MaybeLoadSVGImage();
87 
88   // WebIDL
89   already_AddRefed<DOMSVGAnimatedLength> X();
90   already_AddRefed<DOMSVGAnimatedLength> Y();
91   already_AddRefed<DOMSVGAnimatedLength> Width();
92   already_AddRefed<DOMSVGAnimatedLength> Height();
93   already_AddRefed<DOMSVGAnimatedPreserveAspectRatio> PreserveAspectRatio();
94   already_AddRefed<DOMSVGAnimatedString> Href();
95 
SetDecoding(const nsAString & aDecoding,ErrorResult & aError)96   void SetDecoding(const nsAString& aDecoding, ErrorResult& aError) {
97     SetAttr(nsGkAtoms::decoding, aDecoding, aError);
98   }
99   void GetDecoding(nsAString& aValue);
100 
101   already_AddRefed<Promise> Decode(ErrorResult& aRv);
102 
103   static nsCSSPropertyID GetCSSPropertyIdForAttrEnum(uint8_t aAttrEnum);
104 
105  protected:
106   nsresult LoadSVGImage(bool aForce, bool aNotify);
107 
108   virtual LengthAttributesInfo GetLengthInfo() override;
109   virtual SVGAnimatedPreserveAspectRatio* GetAnimatedPreserveAspectRatio()
110       override;
111   virtual StringAttributesInfo GetStringInfo() override;
112 
113   // Override for nsImageLoadingContent.
AsContent()114   nsIContent* AsContent() override { return this; }
115 
116   enum { ATTR_X, ATTR_Y, ATTR_WIDTH, ATTR_HEIGHT };
117   SVGAnimatedLength mLengthAttributes[4];
118   static LengthInfo sLengthInfo[4];
119 
120   SVGAnimatedPreserveAspectRatio mPreserveAspectRatio;
121 
122   enum { HREF, XLINK_HREF };
123   SVGAnimatedString mStringAttributes[2];
124   static StringInfo sStringInfo[2];
125 };
126 
127 }  // namespace dom
128 }  // namespace mozilla
129 
130 #endif  // mozilla_dom_SVGImageElement_h
131