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 /* representation of CSSRuleList for stylo */
9 #include "mozilla/ServoCSSRuleList.h"
11 #include "mozilla/dom/CSSCounterStyleRule.h"
12 #include "mozilla/dom/CSSFontFaceRule.h"
13 #include "mozilla/dom/CSSFontFeatureValuesRule.h"
14 #include "mozilla/dom/CSSFontPaletteValuesRule.h"
15 #include "mozilla/dom/CSSImportRule.h"
16 #include "mozilla/dom/CSSLayerBlockRule.h"
17 #include "mozilla/dom/CSSLayerStatementRule.h"
18 #include "mozilla/dom/CSSKeyframesRule.h"
19 #include "mozilla/dom/CSSContainerRule.h"
20 #include "mozilla/dom/CSSMediaRule.h"
21 #include "mozilla/dom/CSSMozDocumentRule.h"
22 #include "mozilla/dom/CSSNamespaceRule.h"
23 #include "mozilla/dom/CSSPageRule.h"
24 #include "mozilla/dom/CSSPropertyRule.h"
25 #include "mozilla/dom/CSSStyleRule.h"
26 #include "mozilla/dom/CSSSupportsRule.h"
27 #include "mozilla/IntegerRange.h"
28 #include "mozilla/ServoBindings.h"
29 #include "mozilla/StyleSheet.h"
30 #include "mozilla/dom/Document.h"
32 using namespace mozilla::dom
;
36 ServoCSSRuleList::ServoCSSRuleList(
37 already_AddRefed
<StyleLockedCssRules
> aRawRules
, StyleSheet
* aSheet
,
38 css::GroupRule
* aParentRule
)
39 : mStyleSheet(aSheet
), mParentRule(aParentRule
), mRawRules(aRawRules
) {
43 // QueryInterface implementation for ServoCSSRuleList
44 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ServoCSSRuleList
)
45 NS_INTERFACE_MAP_END_INHERITING(dom::CSSRuleList
)
47 NS_IMPL_ADDREF_INHERITED(ServoCSSRuleList
, dom::CSSRuleList
)
48 NS_IMPL_RELEASE_INHERITED(ServoCSSRuleList
, dom::CSSRuleList
)
50 NS_IMPL_CYCLE_COLLECTION_CLASS(ServoCSSRuleList
)
52 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ServoCSSRuleList
)
54 NS_IMPL_CYCLE_COLLECTION_UNLINK_END_INHERITED(dom::CSSRuleList
)
55 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(ServoCSSRuleList
,
57 tmp
->EnumerateInstantiatedRules([&](css::Rule
* aRule
, uint32_t) {
58 if (!aRule
->IsCCLeaf()) {
59 NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb
, "mRules[i]");
60 cb
.NoteXPCOMChild(aRule
);
63 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
65 css::Rule
* ServoCSSRuleList::GetRule(uint32_t aIndex
) {
66 uintptr_t rule
= mRules
[aIndex
];
67 if (rule
<= kMaxRuleType
) {
68 RefPtr
<css::Rule
> ruleObj
= nullptr;
69 switch (StyleCssRuleType(rule
)) {
70 #define CASE_RULE_WITH_PREFIX(const_, prefix_, name_) \
71 case StyleCssRuleType::const_: { \
72 uint32_t line = 0, column = 0; \
73 RefPtr<Style##prefix_##const_##Rule> raw = \
74 Servo_CssRules_Get##const_##RuleAt(mRawRules, aIndex, &line, &column) \
77 ruleObj = new CSS##name_##Rule(raw.forget(), mStyleSheet, mParentRule, \
79 MOZ_ASSERT(ruleObj->Type() == StyleCssRuleType(rule)); \
82 #define CASE_RULE_LOCKED(const_, name_) \
83 CASE_RULE_WITH_PREFIX(const_, Locked, name_)
84 #define CASE_RULE_UNLOCKED(const_, name_) CASE_RULE_WITH_PREFIX(const_, , name_)
85 CASE_RULE_LOCKED(Style
, Style
)
86 CASE_RULE_LOCKED(Keyframes
, Keyframes
)
87 CASE_RULE_UNLOCKED(Media
, Media
)
88 CASE_RULE_UNLOCKED(Namespace
, Namespace
)
89 CASE_RULE_LOCKED(Page
, Page
)
90 CASE_RULE_UNLOCKED(Property
, Property
)
91 CASE_RULE_UNLOCKED(Supports
, Supports
)
92 CASE_RULE_UNLOCKED(Document
, MozDocument
)
93 CASE_RULE_LOCKED(Import
, Import
)
94 CASE_RULE_UNLOCKED(FontFeatureValues
, FontFeatureValues
)
95 CASE_RULE_UNLOCKED(FontPaletteValues
, FontPaletteValues
)
96 CASE_RULE_LOCKED(FontFace
, FontFace
)
97 CASE_RULE_LOCKED(CounterStyle
, CounterStyle
)
98 CASE_RULE_UNLOCKED(LayerBlock
, LayerBlock
)
99 CASE_RULE_UNLOCKED(LayerStatement
, LayerStatement
)
100 CASE_RULE_UNLOCKED(Container
, Container
)
101 #undef CASE_RULE_LOCKED
102 #undef CASE_RULE_UNLOCKED
103 #undef CASE_RULE_WITH_PREFIX
104 case StyleCssRuleType::Keyframe
:
105 MOZ_ASSERT_UNREACHABLE("keyframe rule cannot be here");
107 case StyleCssRuleType::Margin
:
108 // Margin rules not implemented yet, see bug 1864737
111 rule
= CastToUint(ruleObj
.forget().take());
112 mRules
[aIndex
] = rule
;
114 return CastToPtr(rule
);
117 css::Rule
* ServoCSSRuleList::IndexedGetter(uint32_t aIndex
, bool& aFound
) {
118 if (aIndex
>= mRules
.Length()) {
123 return GetRule(aIndex
);
126 template <typename Func
>
127 void ServoCSSRuleList::EnumerateInstantiatedRules(Func aCallback
) {
129 for (uintptr_t rule
: mRules
) {
130 if (rule
> kMaxRuleType
) {
131 aCallback(CastToPtr(rule
), index
);
137 static void DropRule(already_AddRefed
<css::Rule
> aRule
) {
138 RefPtr
<css::Rule
> rule
= aRule
;
139 rule
->DropReferences();
142 void ServoCSSRuleList::ResetRules() {
143 // DropRule could reenter here via the cycle collector.
144 auto rules
= std::move(mRules
);
145 for (uintptr_t rule
: rules
) {
146 if (rule
> kMaxRuleType
) {
147 DropRule(already_AddRefed
<css::Rule
>(CastToPtr(rule
)));
150 MOZ_ASSERT(mRules
.IsEmpty());
152 Servo_CssRules_ListTypes(mRawRules
, &mRules
);
156 void ServoCSSRuleList::DropAllRules() {
157 mStyleSheet
= nullptr;
158 mParentRule
= nullptr;
164 void ServoCSSRuleList::DropSheetReference() {
165 // If mStyleSheet is not set on this rule list, then it is not set on any of
166 // its instantiated rules either. To avoid O(N^2) beavhior in the depth of
167 // group rule nesting, which can happen if we are unlinked starting from the
168 // deepest nested group rule, skip recursing into the rule list if we know we
173 mStyleSheet
= nullptr;
174 EnumerateInstantiatedRules(
175 [](css::Rule
* rule
, uint32_t) { rule
->DropSheetReference(); });
178 void ServoCSSRuleList::DropParentRuleReference() {
179 mParentRule
= nullptr;
180 EnumerateInstantiatedRules(
181 [](css::Rule
* rule
, uint32_t) { rule
->DropParentRuleReference(); });
184 nsresult
ServoCSSRuleList::InsertRule(const nsACString
& aRule
,
186 MOZ_ASSERT(mStyleSheet
,
187 "Caller must ensure that "
188 "the list is not unlinked from stylesheet");
190 if (!mRawRules
|| IsReadOnly()) {
194 mStyleSheet
->WillDirty();
196 css::Loader
* loader
= nullptr;
197 auto allowImportRules
= mStyleSheet
->SelfOrAncestorIsConstructed()
198 ? StyleAllowImportRules::No
199 : StyleAllowImportRules::Yes
;
201 // TODO(emilio, bug 1535456): Should probably always be able to get a handle
202 // to some loader if we're parsing an @import rule, but which one?
204 // StyleSheet::ReparseSheet just mints a new loader, but that'd be wrong in
205 // this case I think, since such a load will bypass CSP checks.
206 if (Document
* doc
= mStyleSheet
->GetAssociatedDocument()) {
207 loader
= doc
->CSSLoader();
209 StyleCssRuleType type
;
210 uint32_t containingTypes
= 0;
211 for (css::Rule
* rule
= mParentRule
; rule
; rule
= rule
->GetParentRule()) {
212 containingTypes
|= (1 << uint32_t(rule
->Type()));
214 nsresult rv
= Servo_CssRules_InsertRule(
215 mRawRules
, mStyleSheet
->RawContents(), &aRule
, aIndex
, containingTypes
,
216 loader
, allowImportRules
, mStyleSheet
, &type
);
217 NS_ENSURE_SUCCESS(rv
, rv
);
218 mRules
.InsertElementAt(aIndex
, uintptr_t(type
));
222 nsresult
ServoCSSRuleList::DeleteRule(uint32_t aIndex
) {
223 if (!mRawRules
|| IsReadOnly()) {
227 nsresult rv
= Servo_CssRules_DeleteRule(mRawRules
, aIndex
);
228 if (!NS_FAILED(rv
)) {
229 uintptr_t rule
= mRules
[aIndex
];
230 mRules
.RemoveElementAt(aIndex
);
231 if (rule
> kMaxRuleType
) {
232 DropRule(already_AddRefed
<css::Rule
>(CastToPtr(rule
)));
238 void ServoCSSRuleList::SetRawContents(RefPtr
<StyleLockedCssRules
> aNewRules
,
240 mRawRules
= std::move(aNewRules
);
246 EnumerateInstantiatedRules([&](css::Rule
* aRule
, uint32_t aIndex
) {
247 #define RULE_CASE_WITH_PREFIX(constant_, prefix_, type_) \
248 case StyleCssRuleType::constant_: { \
249 uint32_t line = 0, column = 0; \
250 RefPtr<Style##prefix_##constant_##Rule> raw = \
251 Servo_CssRules_Get##constant_##RuleAt(mRawRules, aIndex, &line, \
254 static_cast<dom::CSS##type_##Rule*>(aRule)->SetRawAfterClone( \
258 #define RULE_CASE_LOCKED(constant_, type_) \
259 RULE_CASE_WITH_PREFIX(constant_, Locked, type_)
260 #define RULE_CASE_UNLOCKED(constant_, type_) \
261 RULE_CASE_WITH_PREFIX(constant_, , type_)
262 switch (aRule
->Type()) {
263 RULE_CASE_LOCKED(Style
, Style
)
264 RULE_CASE_LOCKED(Keyframes
, Keyframes
)
265 RULE_CASE_UNLOCKED(Media
, Media
)
266 RULE_CASE_UNLOCKED(Namespace
, Namespace
)
267 RULE_CASE_LOCKED(Page
, Page
)
268 RULE_CASE_UNLOCKED(Property
, Property
)
269 RULE_CASE_UNLOCKED(Supports
, Supports
)
270 RULE_CASE_UNLOCKED(Document
, MozDocument
)
271 RULE_CASE_LOCKED(Import
, Import
)
272 RULE_CASE_UNLOCKED(FontFeatureValues
, FontFeatureValues
)
273 RULE_CASE_UNLOCKED(FontPaletteValues
, FontPaletteValues
)
274 RULE_CASE_LOCKED(FontFace
, FontFace
)
275 RULE_CASE_LOCKED(CounterStyle
, CounterStyle
)
276 RULE_CASE_UNLOCKED(LayerBlock
, LayerBlock
)
277 RULE_CASE_UNLOCKED(LayerStatement
, LayerStatement
)
278 RULE_CASE_UNLOCKED(Container
, Container
)
279 case StyleCssRuleType::Keyframe
:
280 MOZ_ASSERT_UNREACHABLE("keyframe rule cannot be here");
282 case StyleCssRuleType::Margin
:
283 // Margin rules not implemented yet, see bug 1864737
286 #undef RULE_CASE_WITH_PREFIX
287 #undef RULE_CASE_LOCKED
288 #undef RULE_CASE_UNLOCKED
292 ServoCSSRuleList::~ServoCSSRuleList() {
293 MOZ_ASSERT(!mStyleSheet
, "Backpointer should have been cleared");
294 MOZ_ASSERT(!mParentRule
, "Backpointer should have been cleared");
298 bool ServoCSSRuleList::IsReadOnly() const {
299 MOZ_ASSERT(!mStyleSheet
|| !mParentRule
||
300 mStyleSheet
->IsReadOnly() == mParentRule
->IsReadOnly(),
301 "a parent rule should be read only iff the owning sheet is "
303 return mStyleSheet
&& mStyleSheet
->IsReadOnly();
306 } // namespace mozilla