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/. */
8 * representation of a declaration block in a CSS stylesheet, or of
12 #ifndef mozilla_DeclarationBlock_h
13 #define mozilla_DeclarationBlock_h
15 #include "mozilla/Atomics.h"
16 #include "mozilla/ServoBindings.h"
18 #include "nsCSSPropertyID.h"
20 class nsHTMLCSSStyleSheet
;
29 class DeclarationBlock final
{
30 DeclarationBlock(const DeclarationBlock
& aCopy
)
31 : mRaw(Servo_DeclarationBlock_Clone(aCopy
.mRaw
).Consume()),
38 explicit DeclarationBlock(already_AddRefed
<RawServoDeclarationBlock
> aRaw
)
39 : mRaw(aRaw
), mImmutable(false), mIsDirty(false) {
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));
53 * Return whether |this| may be modified.
55 bool IsMutable() const { return !mImmutable
; }
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");
66 * Mark this declaration as unmodifiable.
68 void SetImmutable() { mImmutable
= true; }
71 * Return whether |this| has been restyled after modified.
73 bool IsDirty() const { return mIsDirty
; }
76 * Mark this declaration as dirty.
78 void SetDirty() { mIsDirty
= true; }
81 * Mark this declaration as not dirty.
83 void UnsetDirty() { mIsDirty
= false; }
86 * Copy |this|, if necessary to ensure that it can be modified.
88 already_AddRefed
<DeclarationBlock
> EnsureMutable() {
89 MOZ_ASSERT(!OwnerIsReadOnly());
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
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.
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) {
123 return mContainer
.mOwningRule
;
126 void SetHTMLCSSStyleSheet(nsHTMLCSSStyleSheet
* aHTMLCSSStyleSheet
) {
127 MOZ_ASSERT(!mContainer
.mHTMLCSSStyleSheet
|| !aHTMLCSSStyleSheet
,
128 "should never overwrite one sheet with another");
129 mContainer
.mHTMLCSSStyleSheet
= aHTMLCSSStyleSheet
;
130 if (aHTMLCSSStyleSheet
) {
131 mContainer
.mRaw
|= uintptr_t(1);
135 nsHTMLCSSStyleSheet
* GetHTMLCSSStyleSheet() const {
136 if (!(mContainer
.mRaw
& 0x1)) {
140 c
.mRaw
&= ~uintptr_t(1);
141 return c
.mHTMLCSSStyleSheet
;
144 bool IsReadOnly() const;
146 size_t SizeofIncludingThis(MallocSizeOf
);
148 static already_AddRefed
<DeclarationBlock
> FromCssText(
149 const nsAString
& aCssText
, URLExtraData
* aExtraData
,
150 nsCompatibility aMode
, css::Loader
* aLoader
);
152 RawServoDeclarationBlock
* Raw() const { return mRaw
; }
153 RawServoDeclarationBlock
* const* RefRaw() const {
154 static_assert(sizeof(RefPtr
<RawServoDeclarationBlock
>) ==
155 sizeof(RawServoDeclarationBlock
*),
156 "RefPtr should just be a pointer");
157 return reinterpret_cast<RawServoDeclarationBlock
* const*>(&mRaw
);
160 const StyleStrong
<RawServoDeclarationBlock
>* RefRawStrong() const {
161 static_assert(sizeof(RefPtr
<RawServoDeclarationBlock
>) ==
162 sizeof(RawServoDeclarationBlock
*),
163 "RefPtr should just be a pointer");
165 sizeof(RefPtr
<RawServoDeclarationBlock
>) ==
166 sizeof(StyleStrong
<RawServoDeclarationBlock
>),
167 "RawServoDeclarationBlockStrong should be the same as RefPtr");
168 return reinterpret_cast<const StyleStrong
<RawServoDeclarationBlock
>*>(
172 void ToString(nsAString
& aResult
) const {
173 Servo_DeclarationBlock_GetCssText(mRaw
, &aResult
);
176 uint32_t Count() const { return Servo_DeclarationBlock_Count(mRaw
); }
178 bool GetNthProperty(uint32_t aIndex
, nsACString
& aReturn
) const {
180 return Servo_DeclarationBlock_GetNthProperty(mRaw
, aIndex
, &aReturn
);
183 void GetPropertyValue(const nsACString
& aProperty
, nsAString
& aValue
) const {
184 Servo_DeclarationBlock_GetPropertyValue(mRaw
, &aProperty
, &aValue
);
187 void GetPropertyValueByID(nsCSSPropertyID aPropID
, nsAString
& aValue
) const {
188 Servo_DeclarationBlock_GetPropertyValueById(mRaw
, aPropID
, &aValue
);
191 bool GetPropertyIsImportant(const nsACString
& aProperty
) const {
192 return Servo_DeclarationBlock_GetPropertyIsImportant(mRaw
, &aProperty
);
195 // Returns whether the property was removed.
196 bool RemoveProperty(const nsACString
& aProperty
,
197 DeclarationBlockMutationClosure aClosure
= {}) {
199 return Servo_DeclarationBlock_RemoveProperty(mRaw
, &aProperty
, aClosure
);
202 // Returns whether the property was removed.
203 bool RemovePropertyByID(nsCSSPropertyID aProperty
,
204 DeclarationBlockMutationClosure aClosure
= {}) {
206 return Servo_DeclarationBlock_RemovePropertyById(mRaw
, aProperty
, aClosure
);
210 ~DeclarationBlock() = default;
212 bool OwnerIsReadOnly() const;
215 // We only ever have one of these since we have an
216 // nsHTMLCSSStyleSheet only for style attributes, and style
217 // attributes never have an owning rule.
219 // It's an nsHTMLCSSStyleSheet if the low bit is set.
223 // The style rule that owns this declaration. May be null.
224 css::Rule
* mOwningRule
;
226 // The nsHTMLCSSStyleSheet that is responsible for this declaration.
227 // Only non-null for style attributes.
228 nsHTMLCSSStyleSheet
* mHTMLCSSStyleSheet
;
231 RefPtr
<RawServoDeclarationBlock
> mRaw
;
233 // set when declaration put in the rule tree;
236 // True if this declaration has not been restyled after modified.
238 // Since we can clear this flag from style worker threads, we use an Atomic.
240 // Note that although a single DeclarationBlock can be shared between
241 // different rule nodes (due to the style="" attribute cache), whenever a
242 // DeclarationBlock has its mIsDirty flag set to true, we always clone it to
243 // a unique object first. So when we clear this flag during Servo traversal,
244 // we know that we are clearing it on a DeclarationBlock that has a single
245 // reference, and there is no problem with another user of the same
246 // DeclarationBlock thinking that it is not dirty.
247 Atomic
<bool, MemoryOrdering::Relaxed
> mIsDirty
;
250 } // namespace mozilla
252 #endif // mozilla_DeclarationBlock_h