DeclarationBlock.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. /*
  6. * representation of a declaration block in a CSS stylesheet, or of
  7. * a style attribute
  8. */
  9. #ifndef mozilla_DeclarationBlock_h
  10. #define mozilla_DeclarationBlock_h
  11. #include "mozilla/ServoUtils.h"
  12. #include "mozilla/StyleBackendType.h"
  13. #include "nsCSSPropertyID.h"
  14. class nsHTMLCSSStyleSheet;
  15. namespace mozilla {
  16. class ServoDeclarationBlock;
  17. namespace css {
  18. class Declaration;
  19. class Rule;
  20. } // namespace css
  21. class DeclarationBlock
  22. {
  23. protected:
  24. explicit DeclarationBlock(StyleBackendType aType)
  25. : mImmutable(false), mType(aType) { mContainer.mRaw = 0; }
  26. DeclarationBlock(const DeclarationBlock& aCopy)
  27. : DeclarationBlock(aCopy.mType) {}
  28. public:
  29. MOZ_DECL_STYLO_METHODS(css::Declaration, ServoDeclarationBlock)
  30. inline MozExternalRefCountType AddRef();
  31. inline MozExternalRefCountType Release();
  32. inline already_AddRefed<DeclarationBlock> Clone() const;
  33. /**
  34. * Return whether |this| may be modified.
  35. */
  36. bool IsMutable() const {
  37. return !mImmutable;
  38. }
  39. /**
  40. * Crash if |this| cannot be modified.
  41. */
  42. void AssertMutable() const {
  43. MOZ_ASSERT(IsMutable(), "someone forgot to call EnsureMutable");
  44. }
  45. /**
  46. * Mark this declaration as unmodifiable. It's 'const' so it can
  47. * be called from ToString.
  48. */
  49. void SetImmutable() const { mImmutable = true; }
  50. /**
  51. * Copy |this|, if necessary to ensure that it can be modified.
  52. */
  53. inline already_AddRefed<DeclarationBlock> EnsureMutable();
  54. void SetOwningRule(css::Rule* aRule) {
  55. MOZ_ASSERT(!mContainer.mOwningRule || !aRule,
  56. "should never overwrite one rule with another");
  57. mContainer.mOwningRule = aRule;
  58. }
  59. css::Rule* GetOwningRule() const {
  60. if (mContainer.mRaw & 0x1) {
  61. return nullptr;
  62. }
  63. return mContainer.mOwningRule;
  64. }
  65. void SetHTMLCSSStyleSheet(nsHTMLCSSStyleSheet* aHTMLCSSStyleSheet) {
  66. MOZ_ASSERT(!mContainer.mHTMLCSSStyleSheet || !aHTMLCSSStyleSheet,
  67. "should never overwrite one sheet with another");
  68. mContainer.mHTMLCSSStyleSheet = aHTMLCSSStyleSheet;
  69. if (aHTMLCSSStyleSheet) {
  70. mContainer.mRaw |= uintptr_t(1);
  71. }
  72. }
  73. nsHTMLCSSStyleSheet* GetHTMLCSSStyleSheet() const {
  74. if (!(mContainer.mRaw & 0x1)) {
  75. return nullptr;
  76. }
  77. auto c = mContainer;
  78. c.mRaw &= ~uintptr_t(1);
  79. return c.mHTMLCSSStyleSheet;
  80. }
  81. inline void ToString(nsAString& aString) const;
  82. inline uint32_t Count() const;
  83. inline bool GetNthProperty(uint32_t aIndex, nsAString& aReturn) const;
  84. inline void GetPropertyValue(const nsAString& aProperty,
  85. nsAString& aValue) const;
  86. inline void GetPropertyValueByID(nsCSSPropertyID aPropID,
  87. nsAString& aValue) const;
  88. inline void GetAuthoredPropertyValue(const nsAString& aProperty,
  89. nsAString& aValue) const;
  90. inline bool GetPropertyIsImportant(const nsAString& aProperty) const;
  91. inline void RemoveProperty(const nsAString& aProperty);
  92. inline void RemovePropertyByID(nsCSSPropertyID aProperty);
  93. private:
  94. union {
  95. // We only ever have one of these since we have an
  96. // nsHTMLCSSStyleSheet only for style attributes, and style
  97. // attributes never have an owning rule.
  98. // It's an nsHTMLCSSStyleSheet if the low bit is set.
  99. uintptr_t mRaw;
  100. // The style rule that owns this declaration. May be null.
  101. css::Rule* mOwningRule;
  102. // The nsHTMLCSSStyleSheet that is responsible for this declaration.
  103. // Only non-null for style attributes.
  104. nsHTMLCSSStyleSheet* mHTMLCSSStyleSheet;
  105. } mContainer;
  106. // set when declaration put in the rule tree;
  107. // also by ToString (hence the 'mutable').
  108. mutable bool mImmutable;
  109. const StyleBackendType mType;
  110. };
  111. } // namespace mozilla
  112. #endif // mozilla_DeclarationBlock_h