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): This is a hack for ::first-line and transitions starting
99 // due to CSSOM changes when other transitions are already running. Try
100 // to simplify this setup.
108 return do_AddRef(this);
111 void SetOwningRule(css::Rule
* aRule
) {
112 MOZ_ASSERT(!mContainer
.mOwningRule
|| !aRule
,
113 "should never overwrite one rule with another");
114 mContainer
.mOwningRule
= aRule
;
117 css::Rule
* GetOwningRule() const {
118 if (mContainer
.mRaw
& 0x1) {
121 return mContainer
.mOwningRule
;
124 void SetHTMLCSSStyleSheet(nsHTMLCSSStyleSheet
* aHTMLCSSStyleSheet
) {
125 MOZ_ASSERT(!mContainer
.mHTMLCSSStyleSheet
|| !aHTMLCSSStyleSheet
,
126 "should never overwrite one sheet with another");
127 mContainer
.mHTMLCSSStyleSheet
= aHTMLCSSStyleSheet
;
128 if (aHTMLCSSStyleSheet
) {
129 mContainer
.mRaw
|= uintptr_t(1);
133 nsHTMLCSSStyleSheet
* GetHTMLCSSStyleSheet() const {
134 if (!(mContainer
.mRaw
& 0x1)) {
138 c
.mRaw
&= ~uintptr_t(1);
139 return c
.mHTMLCSSStyleSheet
;
142 bool IsReadOnly() const;
144 size_t SizeofIncludingThis(MallocSizeOf
);
146 static already_AddRefed
<DeclarationBlock
> FromCssText(
147 const nsAString
& aCssText
, URLExtraData
* aExtraData
,
148 nsCompatibility aMode
, css::Loader
* aLoader
);
150 RawServoDeclarationBlock
* Raw() const { return mRaw
; }
151 RawServoDeclarationBlock
* const* RefRaw() const {
152 static_assert(sizeof(RefPtr
<RawServoDeclarationBlock
>) ==
153 sizeof(RawServoDeclarationBlock
*),
154 "RefPtr should just be a pointer");
155 return reinterpret_cast<RawServoDeclarationBlock
* const*>(&mRaw
);
158 const StyleStrong
<RawServoDeclarationBlock
>* RefRawStrong() const {
159 static_assert(sizeof(RefPtr
<RawServoDeclarationBlock
>) ==
160 sizeof(RawServoDeclarationBlock
*),
161 "RefPtr should just be a pointer");
163 sizeof(RefPtr
<RawServoDeclarationBlock
>) ==
164 sizeof(StyleStrong
<RawServoDeclarationBlock
>),
165 "RawServoDeclarationBlockStrong should be the same as RefPtr");
166 return reinterpret_cast<const StyleStrong
<RawServoDeclarationBlock
>*>(
170 void ToString(nsAString
& aResult
) const {
171 Servo_DeclarationBlock_GetCssText(mRaw
, &aResult
);
174 uint32_t Count() const { return Servo_DeclarationBlock_Count(mRaw
); }
176 bool GetNthProperty(uint32_t aIndex
, nsAString
& aReturn
) const {
178 return Servo_DeclarationBlock_GetNthProperty(mRaw
, aIndex
, &aReturn
);
181 void GetPropertyValue(const nsAString
& aProperty
, nsAString
& aValue
) const {
182 NS_ConvertUTF16toUTF8
property(aProperty
);
183 Servo_DeclarationBlock_GetPropertyValue(mRaw
, &property
, &aValue
);
186 void GetPropertyValueByID(nsCSSPropertyID aPropID
, nsAString
& aValue
) const {
187 Servo_DeclarationBlock_GetPropertyValueById(mRaw
, aPropID
, &aValue
);
190 bool GetPropertyIsImportant(const nsAString
& aProperty
) const {
191 NS_ConvertUTF16toUTF8
property(aProperty
);
192 return Servo_DeclarationBlock_GetPropertyIsImportant(mRaw
, &property
);
195 // Returns whether the property was removed.
196 bool RemoveProperty(const nsAString
& aProperty
,
197 DeclarationBlockMutationClosure aClosure
= {}) {
199 NS_ConvertUTF16toUTF8
property(aProperty
);
200 return Servo_DeclarationBlock_RemoveProperty(mRaw
, &property
, aClosure
);
203 // Returns whether the property was removed.
204 bool RemovePropertyByID(nsCSSPropertyID aProperty
,
205 DeclarationBlockMutationClosure aClosure
= {}) {
207 return Servo_DeclarationBlock_RemovePropertyById(mRaw
, aProperty
, aClosure
);
211 ~DeclarationBlock() = default;
213 bool OwnerIsReadOnly() const;
216 // We only ever have one of these since we have an
217 // nsHTMLCSSStyleSheet only for style attributes, and style
218 // attributes never have an owning rule.
220 // It's an nsHTMLCSSStyleSheet if the low bit is set.
224 // The style rule that owns this declaration. May be null.
225 css::Rule
* mOwningRule
;
227 // The nsHTMLCSSStyleSheet that is responsible for this declaration.
228 // Only non-null for style attributes.
229 nsHTMLCSSStyleSheet
* mHTMLCSSStyleSheet
;
232 RefPtr
<RawServoDeclarationBlock
> mRaw
;
234 // set when declaration put in the rule tree;
237 // True if this declaration has not been restyled after modified.
239 // Since we can clear this flag from style worker threads, we use an Atomic.
241 // Note that although a single DeclarationBlock can be shared between
242 // different rule nodes (due to the style="" attribute cache), whenever a
243 // DeclarationBlock has its mIsDirty flag set to true, we always clone it to
244 // a unique object first. So when we clear this flag during Servo traversal,
245 // we know that we are clearing it on a DeclarationBlock that has a single
246 // reference, and there is no problem with another user of the same
247 // DeclarationBlock thinking that it is not dirty.
248 Atomic
<bool, MemoryOrdering::Relaxed
> mIsDirty
;
251 } // namespace mozilla
253 #endif // mozilla_DeclarationBlock_h