nsDOMCSSDeclaration.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* -*- Mode: C++; tab-width: 2; 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. /* base class for DOM objects for element.style and cssStyleRule.style */
  6. #ifndef nsDOMCSSDeclaration_h___
  7. #define nsDOMCSSDeclaration_h___
  8. #include "nsICSSDeclaration.h"
  9. #include "mozilla/Attributes.h"
  10. #include "nsIURI.h"
  11. #include "nsCOMPtr.h"
  12. class nsIPrincipal;
  13. class nsIDocument;
  14. struct JSContext;
  15. class JSObject;
  16. namespace mozilla {
  17. class DeclarationBlock;
  18. namespace css {
  19. class Loader;
  20. class Rule;
  21. } // namespace css
  22. } // namespace mozilla
  23. class nsDOMCSSDeclaration : public nsICSSDeclaration
  24. {
  25. public:
  26. // Only implement QueryInterface; subclasses have the responsibility
  27. // of implementing AddRef/Release.
  28. NS_IMETHOD QueryInterface(REFNSIID aIID, void** aInstancePtr) override;
  29. // Declare addref and release so they can be called on us, but don't
  30. // implement them. Our subclasses must handle their own
  31. // refcounting.
  32. NS_IMETHOD_(MozExternalRefCountType) AddRef() override = 0;
  33. NS_IMETHOD_(MozExternalRefCountType) Release() override = 0;
  34. NS_DECL_NSICSSDECLARATION
  35. using nsICSSDeclaration::GetLength;
  36. // Require subclasses to implement |GetParentRule|.
  37. //NS_DECL_NSIDOMCSSSTYLEDECLARATION
  38. NS_IMETHOD GetCssText(nsAString & aCssText) override;
  39. NS_IMETHOD SetCssText(const nsAString & aCssText) override;
  40. NS_IMETHOD GetPropertyValue(const nsAString & propertyName,
  41. nsAString & _retval) override;
  42. virtual already_AddRefed<mozilla::dom::CSSValue>
  43. GetPropertyCSSValue(const nsAString & propertyName,
  44. mozilla::ErrorResult& aRv) override;
  45. using nsICSSDeclaration::GetPropertyCSSValue;
  46. NS_IMETHOD RemoveProperty(const nsAString & propertyName,
  47. nsAString & _retval) override;
  48. NS_IMETHOD GetPropertyPriority(const nsAString & propertyName,
  49. nsAString & _retval) override;
  50. NS_IMETHOD SetProperty(const nsAString & propertyName,
  51. const nsAString & value, const nsAString & priority) override;
  52. NS_IMETHOD GetLength(uint32_t *aLength) override;
  53. NS_IMETHOD GetParentRule(nsIDOMCSSRule * *aParentRule) override = 0;
  54. // WebIDL interface for CSS2Properties
  55. #define CSS_PROP_PUBLIC_OR_PRIVATE(publicname_, privatename_) publicname_
  56. #define CSS_PROP(name_, id_, method_, flags_, pref_, parsevariant_, \
  57. kwtable_, stylestruct_, stylestructoffset_, animtype_) \
  58. void \
  59. Get##method_(nsAString& aValue, mozilla::ErrorResult& rv) \
  60. { \
  61. rv = GetPropertyValue(eCSSProperty_##id_, aValue); \
  62. } \
  63. \
  64. void \
  65. Set##method_(const nsAString& aValue, mozilla::ErrorResult& rv) \
  66. { \
  67. rv = SetPropertyValue(eCSSProperty_##id_, aValue); \
  68. }
  69. #define CSS_PROP_LIST_EXCLUDE_INTERNAL
  70. #define CSS_PROP_LIST_INCLUDE_LOGICAL
  71. #define CSS_PROP_SHORTHAND(name_, id_, method_, flags_, pref_) \
  72. CSS_PROP(name_, id_, method_, flags_, pref_, X, X, X, X, X)
  73. #include "nsCSSPropList.h"
  74. #define CSS_PROP_ALIAS(aliasname_, propid_, aliasmethod_, pref_) \
  75. CSS_PROP(X, propid_, aliasmethod_, X, pref_, X, X, X, X, X)
  76. #include "nsCSSPropAliasList.h"
  77. #undef CSS_PROP_ALIAS
  78. #undef CSS_PROP_SHORTHAND
  79. #undef CSS_PROP_LIST_INCLUDE_LOGICAL
  80. #undef CSS_PROP_LIST_EXCLUDE_INTERNAL
  81. #undef CSS_PROP
  82. #undef CSS_PROP_PUBLIC_OR_PRIVATE
  83. virtual void IndexedGetter(uint32_t aIndex, bool& aFound, nsAString& aPropName) override;
  84. virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
  85. protected:
  86. // The reason for calling GetCSSDeclaration.
  87. enum Operation {
  88. // We are calling GetCSSDeclaration so that we can read from it. Does not
  89. // allocate a new declaration if we don't have one yet; returns nullptr in
  90. // this case.
  91. eOperation_Read,
  92. // We are calling GetCSSDeclaration so that we can set a property on it
  93. // or re-parse the whole declaration. Allocates a new declaration if we
  94. // don't have one yet and calls AttributeWillChange. A nullptr return value
  95. // indicates an error allocating the declaration.
  96. eOperation_Modify,
  97. // We are calling GetCSSDeclaration so that we can remove a property from
  98. // it. Does not allocates a new declaration if we don't have one yet;
  99. // returns nullptr in this case. If we do have a declaration, calls
  100. // AttributeWillChange.
  101. eOperation_RemoveProperty
  102. };
  103. virtual mozilla::DeclarationBlock* GetCSSDeclaration(Operation aOperation) = 0;
  104. virtual nsresult SetCSSDeclaration(mozilla::DeclarationBlock* aDecl) = 0;
  105. // Document that we must call BeginUpdate/EndUpdate on around the
  106. // calls to SetCSSDeclaration and the style rule mutation that leads
  107. // to it.
  108. virtual nsIDocument* DocToUpdate() = 0;
  109. // Information neded to parse a declaration. We need the mSheetURI
  110. // for error reporting, mBaseURI to resolve relative URIs,
  111. // mPrincipal for subresource loads, and mCSSLoader for determining
  112. // whether we're in quirks mode. mBaseURI needs to be a strong
  113. // pointer because of xml:base possibly creating base URIs on the
  114. // fly. This is why we don't use CSSParsingEnvironment as a return
  115. // value, to avoid multiple-refcounting of mBaseURI.
  116. struct CSSParsingEnvironment {
  117. nsIURI* MOZ_UNSAFE_REF("user of CSSParsingEnviroment must hold an owning "
  118. "reference; reference counting here has unacceptable "
  119. "performance overhead (see bug 649163)") mSheetURI;
  120. nsCOMPtr<nsIURI> mBaseURI;
  121. nsIPrincipal* MOZ_UNSAFE_REF("user of CSSParsingEnviroment must hold an owning "
  122. "reference; reference counting here has unacceptable "
  123. "performance overhead (see bug 649163)") mPrincipal;
  124. mozilla::css::Loader* MOZ_UNSAFE_REF("user of CSSParsingEnviroment must hold an owning "
  125. "reference; reference counting here has unacceptable "
  126. "performance overhead (see bug 649163)") mCSSLoader;
  127. };
  128. // On failure, mPrincipal should be set to null in aCSSParseEnv.
  129. // If mPrincipal is null, the other members may not be set to
  130. // anything meaningful.
  131. virtual void GetCSSParsingEnvironment(CSSParsingEnvironment& aCSSParseEnv) = 0;
  132. // An implementation for GetCSSParsingEnvironment for callers wrapping
  133. // an css::Rule.
  134. static void GetCSSParsingEnvironmentForRule(mozilla::css::Rule* aRule,
  135. CSSParsingEnvironment& aCSSParseEnv);
  136. nsresult ParsePropertyValue(const nsCSSPropertyID aPropID,
  137. const nsAString& aPropValue,
  138. bool aIsImportant);
  139. nsresult ParseCustomPropertyValue(const nsAString& aPropertyName,
  140. const nsAString& aPropValue,
  141. bool aIsImportant);
  142. nsresult RemovePropertyInternal(nsCSSPropertyID aPropID);
  143. nsresult RemovePropertyInternal(const nsAString& aProperty);
  144. protected:
  145. virtual ~nsDOMCSSDeclaration();
  146. };
  147. #endif // nsDOMCSSDeclaration_h___