SVGUseElement.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_dom_SVGUseElement_h
  6. #define mozilla_dom_SVGUseElement_h
  7. #include "mozilla/dom/FromParser.h"
  8. #include "nsReferencedElement.h"
  9. #include "nsStubMutationObserver.h"
  10. #include "mozilla/dom/SVGGraphicsElement.h"
  11. #include "nsSVGLength2.h"
  12. #include "nsSVGString.h"
  13. #include "nsTArray.h"
  14. class nsIContent;
  15. class nsSVGUseFrame;
  16. nsresult
  17. NS_NewSVGSVGElement(nsIContent **aResult,
  18. already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo,
  19. mozilla::dom::FromParser aFromParser);
  20. nsresult NS_NewSVGUseElement(nsIContent **aResult,
  21. already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo);
  22. namespace mozilla {
  23. namespace dom {
  24. typedef SVGGraphicsElement SVGUseElementBase;
  25. class SVGUseElement final : public SVGUseElementBase,
  26. public nsStubMutationObserver
  27. {
  28. friend class ::nsSVGUseFrame;
  29. protected:
  30. friend nsresult (::NS_NewSVGUseElement(nsIContent **aResult,
  31. already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo));
  32. explicit SVGUseElement(already_AddRefed<mozilla::dom::NodeInfo>& aNodeInfo);
  33. virtual ~SVGUseElement();
  34. virtual JSObject* WrapNode(JSContext *cx, JS::Handle<JSObject*> aGivenProto) override;
  35. public:
  36. // interfaces:
  37. NS_DECL_ISUPPORTS_INHERITED
  38. NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(SVGUseElement, SVGUseElementBase)
  39. NS_DECL_NSIMUTATIONOBSERVER_CHARACTERDATACHANGED
  40. NS_DECL_NSIMUTATIONOBSERVER_ATTRIBUTECHANGED
  41. NS_DECL_NSIMUTATIONOBSERVER_CONTENTAPPENDED
  42. NS_DECL_NSIMUTATIONOBSERVER_CONTENTINSERTED
  43. NS_DECL_NSIMUTATIONOBSERVER_CONTENTREMOVED
  44. NS_DECL_NSIMUTATIONOBSERVER_NODEWILLBEDESTROYED
  45. // for nsSVGUseFrame's nsIAnonymousContentCreator implementation.
  46. nsIContent* CreateAnonymousContent();
  47. nsIContent* GetAnonymousContent() const { return mClone; }
  48. void DestroyAnonymousContent();
  49. // nsSVGElement specializations:
  50. virtual gfxMatrix PrependLocalTransformsTo(
  51. const gfxMatrix &aMatrix,
  52. SVGTransformTypes aWhich = eAllTransforms) const override;
  53. virtual bool HasValidDimensions() const override;
  54. // nsIContent interface
  55. virtual nsresult Clone(mozilla::dom::NodeInfo *aNodeInfo, nsINode **aResult) const override;
  56. NS_IMETHOD_(bool) IsAttributeMapped(const nsIAtom* aAttribute) const override;
  57. // WebIDL
  58. already_AddRefed<SVGAnimatedString> Href();
  59. already_AddRefed<SVGAnimatedLength> X();
  60. already_AddRefed<SVGAnimatedLength> Y();
  61. already_AddRefed<SVGAnimatedLength> Width();
  62. already_AddRefed<SVGAnimatedLength> Height();
  63. nsIURI* GetSourceDocURI();
  64. protected:
  65. class SourceReference : public nsReferencedElement {
  66. public:
  67. explicit SourceReference(SVGUseElement* aContainer) : mContainer(aContainer) {}
  68. protected:
  69. virtual void ElementChanged(Element* aFrom, Element* aTo) override {
  70. nsReferencedElement::ElementChanged(aFrom, aTo);
  71. if (aFrom) {
  72. aFrom->RemoveMutationObserver(mContainer);
  73. }
  74. mContainer->TriggerReclone();
  75. }
  76. private:
  77. SVGUseElement* mContainer;
  78. };
  79. virtual LengthAttributesInfo GetLengthInfo() override;
  80. virtual StringAttributesInfo GetStringInfo() override;
  81. /**
  82. * Returns true if our width and height should be used, or false if they
  83. * should be ignored. As per the spec, this depends on the type of the
  84. * element that we're referencing.
  85. */
  86. bool OurWidthAndHeightAreUsed() const;
  87. void SyncWidthOrHeight(nsIAtom *aName);
  88. void LookupHref();
  89. void TriggerReclone();
  90. void UnlinkSource();
  91. enum { ATTR_X, ATTR_Y, ATTR_WIDTH, ATTR_HEIGHT };
  92. nsSVGLength2 mLengthAttributes[4];
  93. static LengthInfo sLengthInfo[4];
  94. enum { HREF, XLINK_HREF };
  95. nsSVGString mStringAttributes[2];
  96. static StringInfo sStringInfo[2];
  97. nsCOMPtr<nsIContent> mOriginal; // if we've been cloned, our "real" copy
  98. nsCOMPtr<nsIContent> mClone; // cloned tree
  99. SourceReference mSource; // observed element
  100. };
  101. } // namespace dom
  102. } // namespace mozilla
  103. #endif // mozilla_dom_SVGUseElement_h