nsSVGString.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 __NS_SVGSTRING_H__
  6. #define __NS_SVGSTRING_H__
  7. #include "nsAutoPtr.h"
  8. #include "nsError.h"
  9. #include "nsSVGElement.h"
  10. #include "mozilla/Attributes.h"
  11. #include "mozilla/dom/SVGAnimatedString.h"
  12. class nsSVGString
  13. {
  14. public:
  15. void Init(uint8_t aAttrEnum) {
  16. mAnimVal = nullptr;
  17. mAttrEnum = aAttrEnum;
  18. mIsBaseSet = false;
  19. }
  20. void SetBaseValue(const nsAString& aValue,
  21. nsSVGElement *aSVGElement,
  22. bool aDoSetAttr);
  23. void GetBaseValue(nsAString& aValue, const nsSVGElement *aSVGElement) const
  24. { aSVGElement->GetStringBaseValue(mAttrEnum, aValue); }
  25. void SetAnimValue(const nsAString& aValue, nsSVGElement *aSVGElement);
  26. void GetAnimValue(nsAString& aValue, const nsSVGElement *aSVGElement) const;
  27. // Returns true if the animated value of this string has been explicitly
  28. // set (either by animation, or by taking on the base value which has been
  29. // explicitly set by markup or a DOM call), false otherwise.
  30. // If this returns false, the animated value is still valid, that is,
  31. // useable, and represents the default base value of the attribute.
  32. bool IsExplicitlySet() const
  33. { return !!mAnimVal || mIsBaseSet; }
  34. already_AddRefed<mozilla::dom::SVGAnimatedString>
  35. ToDOMAnimatedString(nsSVGElement* aSVGElement);
  36. // Returns a new nsISMILAttr object that the caller must delete
  37. nsISMILAttr* ToSMILAttr(nsSVGElement *aSVGElement);
  38. private:
  39. nsAutoPtr<nsString> mAnimVal;
  40. uint8_t mAttrEnum; // element specified tracking for attribute
  41. bool mIsBaseSet;
  42. public:
  43. struct DOMAnimatedString final : public mozilla::dom::SVGAnimatedString
  44. {
  45. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  46. NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMAnimatedString)
  47. DOMAnimatedString(nsSVGString* aVal, nsSVGElement* aSVGElement)
  48. : mozilla::dom::SVGAnimatedString(aSVGElement)
  49. , mVal(aVal)
  50. {}
  51. nsSVGString* mVal; // kept alive because it belongs to content
  52. void GetBaseVal(nsAString & aResult) override
  53. {
  54. mVal->GetBaseValue(aResult, mSVGElement);
  55. }
  56. void SetBaseVal(const nsAString & aValue) override
  57. {
  58. mVal->SetBaseValue(aValue, mSVGElement, true);
  59. }
  60. void GetAnimVal(nsAString & aResult) override
  61. {
  62. mSVGElement->FlushAnimations();
  63. mVal->GetAnimValue(aResult, mSVGElement);
  64. }
  65. private:
  66. virtual ~DOMAnimatedString();
  67. };
  68. struct SMILString : public nsISMILAttr
  69. {
  70. public:
  71. SMILString(nsSVGString *aVal, nsSVGElement *aSVGElement)
  72. : mVal(aVal), mSVGElement(aSVGElement) {}
  73. // These will stay alive because a nsISMILAttr only lives as long
  74. // as the Compositing step, and DOM elements don't get a chance to
  75. // die during that.
  76. nsSVGString* mVal;
  77. nsSVGElement* mSVGElement;
  78. // nsISMILAttr methods
  79. virtual nsresult ValueFromString(const nsAString& aStr,
  80. const mozilla::dom::SVGAnimationElement *aSrcElement,
  81. nsSMILValue& aValue,
  82. bool& aPreventCachingOfSandwich) const override;
  83. virtual nsSMILValue GetBaseValue() const override;
  84. virtual void ClearAnimValue() override;
  85. virtual nsresult SetAnimValue(const nsSMILValue& aValue) override;
  86. };
  87. };
  88. #endif //__NS_SVGSTRING_H__