Bug 1874684 - Part 37: Fix unified compilation. r=allstarschh
[gecko.git] / layout / style / DeclarationBlock.h
blob837ab74bce0104ea3927c37beacf149b1e892c82
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/. */
7 /*
8 * representation of a declaration block in a CSS stylesheet, or of
9 * a style attribute
12 #ifndef mozilla_DeclarationBlock_h
13 #define mozilla_DeclarationBlock_h
15 #include "mozilla/Atomics.h"
16 #include "mozilla/ServoBindings.h"
18 #include "nsCSSPropertyID.h"
19 #include "nsString.h"
21 namespace mozilla {
23 class AttributeStyles;
24 namespace css {
25 class Declaration;
26 class Rule;
27 } // namespace css
29 class DeclarationBlock final {
30 DeclarationBlock(const DeclarationBlock& aCopy)
31 : mRaw(Servo_DeclarationBlock_Clone(aCopy.mRaw).Consume()),
32 mImmutable(false),
33 mIsDirty(false) {
34 mContainer.mRaw = 0;
37 public:
38 explicit DeclarationBlock(already_AddRefed<StyleLockedDeclarationBlock> aRaw)
39 : mRaw(aRaw), mImmutable(false), mIsDirty(false) {
40 mContainer.mRaw = 0;
43 DeclarationBlock()
44 : DeclarationBlock(Servo_DeclarationBlock_CreateEmpty().Consume()) {}
46 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(DeclarationBlock)
48 already_AddRefed<DeclarationBlock> Clone() const {
49 return do_AddRef(new DeclarationBlock(*this));
52 /**
53 * Return whether |this| may be modified.
55 bool IsMutable() const { return !mImmutable; }
57 /**
58 * Crash in debug builds if |this| cannot be modified.
60 void AssertMutable() const {
61 MOZ_ASSERT(IsMutable(), "someone forgot to call EnsureMutable");
62 MOZ_ASSERT(!OwnerIsReadOnly(), "User Agent sheets shouldn't be modified");
65 /**
66 * Mark this declaration as unmodifiable.
68 void SetImmutable() { mImmutable = true; }
70 /**
71 * Return whether |this| has been restyled after modified.
73 bool IsDirty() const { return mIsDirty; }
75 /**
76 * Mark this declaration as dirty.
78 void SetDirty() { mIsDirty = true; }
80 /**
81 * Mark this declaration as not dirty.
83 void UnsetDirty() { mIsDirty = false; }
85 /**
86 * Copy |this|, if necessary to ensure that it can be modified.
88 already_AddRefed<DeclarationBlock> EnsureMutable() {
89 MOZ_ASSERT(!OwnerIsReadOnly());
91 if (!IsDirty()) {
92 // In stylo, the old DeclarationBlock is stored in element's rule node
93 // tree directly, to avoid new values replacing the DeclarationBlock in
94 // the tree directly, we need to copy the old one here if we haven't yet
95 // copied. As a result the new value does not replace rule node tree until
96 // traversal happens.
98 // FIXME(emilio, bug 1606413): This is a hack for ::first-line and
99 // transitions starting due to CSSOM changes when other transitions are
100 // already running. Try to simplify this setup, so that rule tree updates
101 // find the mutated declaration block properly rather than having to
102 // insert the cloned declaration in the tree.
103 return Clone();
106 if (!IsMutable()) {
107 return Clone();
110 return do_AddRef(this);
113 void SetOwningRule(css::Rule* aRule) {
114 MOZ_ASSERT(!mContainer.mOwningRule || !aRule,
115 "should never overwrite one rule with another");
116 mContainer.mOwningRule = aRule;
119 css::Rule* GetOwningRule() const {
120 if (mContainer.mRaw & 0x1) {
121 return nullptr;
123 return mContainer.mOwningRule;
126 void SetAttributeStyles(AttributeStyles* aAttributeStyles) {
127 MOZ_ASSERT(!mContainer.mAttributeStyles || !aAttributeStyles,
128 "should never overwrite one sheet with another");
129 mContainer.mAttributeStyles = aAttributeStyles;
130 if (aAttributeStyles) {
131 mContainer.mRaw |= uintptr_t(1);
135 AttributeStyles* GetAttributeStyles() const {
136 if (!(mContainer.mRaw & 0x1)) {
137 return nullptr;
139 auto c = mContainer;
140 c.mRaw &= ~uintptr_t(1);
141 return c.mAttributeStyles;
144 bool IsReadOnly() const;
146 size_t SizeofIncludingThis(MallocSizeOf);
148 static already_AddRefed<DeclarationBlock> FromCssText(
149 const nsACString& aCssText, URLExtraData* aExtraData,
150 nsCompatibility aMode, css::Loader* aLoader, StyleCssRuleType aRuleType) {
151 RefPtr<StyleLockedDeclarationBlock> raw =
152 Servo_ParseStyleAttribute(&aCssText, aExtraData, aMode, aLoader,
153 aRuleType)
154 .Consume();
155 return MakeAndAddRef<DeclarationBlock>(raw.forget());
158 static already_AddRefed<DeclarationBlock> FromCssText(
159 const nsAString& aCssText, URLExtraData* aExtraData,
160 nsCompatibility aMode, css::Loader* aLoader, StyleCssRuleType aRuleType) {
161 NS_ConvertUTF16toUTF8 value(aCssText);
162 return FromCssText(value, aExtraData, aMode, aLoader, aRuleType);
165 StyleLockedDeclarationBlock* Raw() const { return mRaw; }
167 void ToString(nsACString& aResult) const {
168 Servo_DeclarationBlock_GetCssText(mRaw, &aResult);
171 uint32_t Count() const { return Servo_DeclarationBlock_Count(mRaw); }
173 bool GetNthProperty(uint32_t aIndex, nsACString& aReturn) const {
174 aReturn.Truncate();
175 return Servo_DeclarationBlock_GetNthProperty(mRaw, aIndex, &aReturn);
178 void GetPropertyValue(const nsACString& aProperty, nsACString& aValue) const {
179 Servo_DeclarationBlock_GetPropertyValue(mRaw, &aProperty, &aValue);
182 void GetPropertyValueByID(nsCSSPropertyID aPropID, nsACString& aValue) const {
183 Servo_DeclarationBlock_GetPropertyValueById(mRaw, aPropID, &aValue);
186 bool GetPropertyIsImportant(const nsACString& aProperty) const {
187 return Servo_DeclarationBlock_GetPropertyIsImportant(mRaw, &aProperty);
190 // Returns whether the property was removed.
191 bool RemoveProperty(const nsACString& aProperty,
192 DeclarationBlockMutationClosure aClosure = {}) {
193 AssertMutable();
194 return Servo_DeclarationBlock_RemoveProperty(mRaw, &aProperty, aClosure);
197 // Returns whether the property was removed.
198 bool RemovePropertyByID(nsCSSPropertyID aProperty,
199 DeclarationBlockMutationClosure aClosure = {}) {
200 AssertMutable();
201 return Servo_DeclarationBlock_RemovePropertyById(mRaw, aProperty, aClosure);
204 private:
205 ~DeclarationBlock() = default;
207 bool OwnerIsReadOnly() const;
209 union {
210 // We only ever have one of these since we have a AttributeStyles only for
211 // style attributes, and style attributes never have an owning rule. It's a
212 // AttributeStyles if the low bit is set.
214 uintptr_t mRaw;
216 // The style rule that owns this declaration. May be null.
217 css::Rule* mOwningRule;
219 // The AttributeStyles that is responsible for this declaration. Only
220 // non-null for style attributes.
221 AttributeStyles* mAttributeStyles;
222 } mContainer;
224 RefPtr<StyleLockedDeclarationBlock> mRaw;
226 // set when declaration put in the rule tree;
227 bool mImmutable;
229 // True if this declaration has not been restyled after modified.
231 // Since we can clear this flag from style worker threads, we use an Atomic.
233 // Note that although a single DeclarationBlock can be shared between
234 // different rule nodes (due to the style="" attribute cache), whenever a
235 // DeclarationBlock has its mIsDirty flag set to true, we always clone it to
236 // a unique object first. So when we clear this flag during Servo traversal,
237 // we know that we are clearing it on a DeclarationBlock that has a single
238 // reference, and there is no problem with another user of the same
239 // DeclarationBlock thinking that it is not dirty.
240 Atomic<bool, MemoryOrdering::Relaxed> mIsDirty;
243 } // namespace mozilla
245 #endif // mozilla_DeclarationBlock_h