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/CSSMarginRule.h"
21 #include "mozilla/dom/CSSMediaRule.h"
22 #include "mozilla/dom/CSSMozDocumentRule.h"
23 #include "mozilla/dom/CSSNamespaceRule.h"
24 #include "mozilla/dom/CSSPageRule.h"
25 #include "mozilla/dom/CSSPropertyRule.h"
26 #include "mozilla/dom/CSSScopeRule.h"
27 #include "mozilla/dom/CSSStartingStyleRule.h"
28 #include "mozilla/dom/CSSStyleRule.h"
29 #include "mozilla/dom/CSSSupportsRule.h"
30 #include "mozilla/IntegerRange.h"
31 #include "mozilla/ServoBindings.h"
32 #include "mozilla/StyleSheet.h"
33 #include "mozilla/dom/Document.h"
35 using namespace mozilla::dom
;
39 ServoCSSRuleList::ServoCSSRuleList(
40 already_AddRefed
<StyleLockedCssRules
> aRawRules
, StyleSheet
* aSheet
,
41 css::GroupRule
* aParentRule
)
42 : mStyleSheet(aSheet
), mParentRule(aParentRule
), mRawRules(aRawRules
) {
46 // QueryInterface implementation for ServoCSSRuleList
47 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ServoCSSRuleList
)
48 NS_INTERFACE_MAP_END_INHERITING(dom::CSSRuleList
)
50 NS_IMPL_ADDREF_INHERITED(ServoCSSRuleList
, dom::CSSRuleList
)
51 NS_IMPL_RELEASE_INHERITED(ServoCSSRuleList
, dom::CSSRuleList
)
53 NS_IMPL_CYCLE_COLLECTION_CLASS(ServoCSSRuleList
)
55 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ServoCSSRuleList
)
57 NS_IMPL_CYCLE_COLLECTION_UNLINK_END_INHERITED(dom::CSSRuleList
)
58 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(ServoCSSRuleList
,
60 tmp
->EnumerateInstantiatedRules([&](css::Rule
* aRule
, uint32_t) {
61 if (!aRule
->IsCCLeaf()) {
62 NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb
, "mRules[i]");
63 cb
.NoteXPCOMChild(aRule
);
66 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
68 css::Rule
* ServoCSSRuleList::GetRule(uint32_t aIndex
) {
69 uintptr_t rule
= mRules
[aIndex
];
70 if (rule
<= kMaxRuleType
) {
71 RefPtr
<css::Rule
> ruleObj
= nullptr;
72 switch (StyleCssRuleType(rule
)) {
73 #define CASE_RULE_WITH_PREFIX(const_, prefix_, name_) \
74 case StyleCssRuleType::const_: { \
75 uint32_t line = 0, column = 0; \
76 RefPtr<Style##prefix_##const_##Rule> raw = \
77 Servo_CssRules_Get##const_##RuleAt(mRawRules, aIndex, &line, &column) \
80 ruleObj = new CSS##name_##Rule(raw.forget(), mStyleSheet, mParentRule, \
82 MOZ_ASSERT(ruleObj->Type() == StyleCssRuleType(rule)); \
85 #define CASE_RULE_LOCKED(const_, name_) \
86 CASE_RULE_WITH_PREFIX(const_, Locked, name_)
87 #define CASE_RULE_UNLOCKED(const_, name_) CASE_RULE_WITH_PREFIX(const_, , name_)
88 CASE_RULE_LOCKED(Style
, Style
)
89 CASE_RULE_LOCKED(Keyframes
, Keyframes
)
90 CASE_RULE_UNLOCKED(Media
, Media
)
91 CASE_RULE_UNLOCKED(Namespace
, Namespace
)
92 CASE_RULE_UNLOCKED(Margin
, Margin
)
93 CASE_RULE_LOCKED(Page
, Page
)
94 CASE_RULE_UNLOCKED(Property
, Property
)
95 CASE_RULE_UNLOCKED(Supports
, Supports
)
96 CASE_RULE_UNLOCKED(Document
, MozDocument
)
97 CASE_RULE_LOCKED(Import
, Import
)
98 CASE_RULE_UNLOCKED(FontFeatureValues
, FontFeatureValues
)
99 CASE_RULE_UNLOCKED(FontPaletteValues
, FontPaletteValues
)
100 CASE_RULE_LOCKED(FontFace
, FontFace
)
101 CASE_RULE_LOCKED(CounterStyle
, CounterStyle
)
102 CASE_RULE_UNLOCKED(LayerBlock
, LayerBlock
)
103 CASE_RULE_UNLOCKED(LayerStatement
, LayerStatement
)
104 CASE_RULE_UNLOCKED(Container
, Container
)
105 CASE_RULE_UNLOCKED(Scope
, Scope
)
106 CASE_RULE_UNLOCKED(StartingStyle
, StartingStyle
)
107 #undef CASE_RULE_LOCKED
108 #undef CASE_RULE_UNLOCKED
109 #undef CASE_RULE_WITH_PREFIX
110 case StyleCssRuleType::Keyframe
:
111 MOZ_ASSERT_UNREACHABLE("keyframe rule cannot be here");
114 rule
= CastToUint(ruleObj
.forget().take());
115 mRules
[aIndex
] = rule
;
117 return CastToPtr(rule
);
120 css::Rule
* ServoCSSRuleList::IndexedGetter(uint32_t aIndex
, bool& aFound
) {
121 if (aIndex
>= mRules
.Length()) {
126 return GetRule(aIndex
);
129 template <typename Func
>
130 void ServoCSSRuleList::EnumerateInstantiatedRules(Func aCallback
) {
132 for (uintptr_t rule
: mRules
) {
133 if (rule
> kMaxRuleType
) {
134 aCallback(CastToPtr(rule
), index
);
140 static void DropRule(already_AddRefed
<css::Rule
> aRule
) {
141 RefPtr
<css::Rule
> rule
= aRule
;
142 rule
->DropReferences();
145 void ServoCSSRuleList::ResetRules() {
146 // DropRule could reenter here via the cycle collector.
147 auto rules
= std::move(mRules
);
148 for (uintptr_t rule
: rules
) {
149 if (rule
> kMaxRuleType
) {
150 DropRule(already_AddRefed
<css::Rule
>(CastToPtr(rule
)));
153 MOZ_ASSERT(mRules
.IsEmpty());
155 Servo_CssRules_ListTypes(mRawRules
, &mRules
);
159 void ServoCSSRuleList::DropAllRules() {
160 mStyleSheet
= nullptr;
161 mParentRule
= nullptr;
167 void ServoCSSRuleList::DropSheetReference() {
168 // If mStyleSheet is not set on this rule list, then it is not set on any of
169 // its instantiated rules either. To avoid O(N^2) beavhior in the depth of
170 // group rule nesting, which can happen if we are unlinked starting from the
171 // deepest nested group rule, skip recursing into the rule list if we know we
176 mStyleSheet
= nullptr;
177 EnumerateInstantiatedRules(
178 [](css::Rule
* rule
, uint32_t) { rule
->DropSheetReference(); });
181 void ServoCSSRuleList::DropParentRuleReference() {
182 mParentRule
= nullptr;
183 EnumerateInstantiatedRules(
184 [](css::Rule
* rule
, uint32_t) { rule
->DropParentRuleReference(); });
187 nsresult
ServoCSSRuleList::InsertRule(const nsACString
& aRule
,
189 MOZ_ASSERT(mStyleSheet
,
190 "Caller must ensure that "
191 "the list is not unlinked from stylesheet");
193 if (!mRawRules
|| IsReadOnly()) {
197 mStyleSheet
->WillDirty();
199 css::Loader
* loader
= nullptr;
200 auto allowImportRules
= mStyleSheet
->SelfOrAncestorIsConstructed()
201 ? StyleAllowImportRules::No
202 : StyleAllowImportRules::Yes
;
204 // TODO(emilio, bug 1535456): Should probably always be able to get a handle
205 // to some loader if we're parsing an @import rule, but which one?
207 // StyleSheet::ReparseSheet just mints a new loader, but that'd be wrong in
208 // this case I think, since such a load will bypass CSP checks.
209 if (Document
* doc
= mStyleSheet
->GetAssociatedDocument()) {
210 loader
= doc
->CSSLoader();
212 StyleCssRuleType type
;
213 uint32_t containingTypes
= 0;
214 Maybe
<StyleCssRuleType
> parseRelativeRuleType
;
215 for (css::Rule
* rule
= mParentRule
; rule
; rule
= rule
->GetParentRule()) {
216 const auto ruleType
= rule
->Type();
217 containingTypes
|= (1 << uint32_t(ruleType
));
218 if (parseRelativeRuleType
.isNothing() &&
219 (ruleType
== StyleCssRuleType::Style
||
220 ruleType
== StyleCssRuleType::Scope
)) {
221 // Only the closest applicable type to this rule matters.
222 parseRelativeRuleType
= Some(ruleType
);
225 nsresult rv
= Servo_CssRules_InsertRule(
226 mRawRules
, mStyleSheet
->RawContents(), &aRule
, aIndex
, containingTypes
,
227 parseRelativeRuleType
.ptrOr(nullptr), loader
, allowImportRules
,
229 NS_ENSURE_SUCCESS(rv
, rv
);
230 mRules
.InsertElementAt(aIndex
, uintptr_t(type
));
234 nsresult
ServoCSSRuleList::DeleteRule(uint32_t aIndex
) {
235 if (!mRawRules
|| IsReadOnly()) {
239 nsresult rv
= Servo_CssRules_DeleteRule(mRawRules
, aIndex
);
240 if (!NS_FAILED(rv
)) {
241 uintptr_t rule
= mRules
[aIndex
];
242 mRules
.RemoveElementAt(aIndex
);
243 if (rule
> kMaxRuleType
) {
244 DropRule(already_AddRefed
<css::Rule
>(CastToPtr(rule
)));
250 void ServoCSSRuleList::SetRawContents(RefPtr
<StyleLockedCssRules
> aNewRules
,
252 mRawRules
= std::move(aNewRules
);
258 EnumerateInstantiatedRules([&](css::Rule
* aRule
, uint32_t aIndex
) {
259 #define RULE_CASE_WITH_PREFIX(constant_, prefix_, type_) \
260 case StyleCssRuleType::constant_: { \
261 uint32_t line = 0, column = 0; \
262 RefPtr<Style##prefix_##constant_##Rule> raw = \
263 Servo_CssRules_Get##constant_##RuleAt(mRawRules, aIndex, &line, \
266 static_cast<dom::CSS##type_##Rule*>(aRule)->SetRawAfterClone( \
270 #define RULE_CASE_LOCKED(constant_, type_) \
271 RULE_CASE_WITH_PREFIX(constant_, Locked, type_)
272 #define RULE_CASE_UNLOCKED(constant_, type_) \
273 RULE_CASE_WITH_PREFIX(constant_, , type_)
274 switch (aRule
->Type()) {
275 RULE_CASE_LOCKED(Style
, Style
)
276 RULE_CASE_LOCKED(Keyframes
, Keyframes
)
277 RULE_CASE_UNLOCKED(Media
, Media
)
278 RULE_CASE_UNLOCKED(Namespace
, Namespace
)
279 RULE_CASE_UNLOCKED(Margin
, Margin
)
280 RULE_CASE_LOCKED(Page
, Page
)
281 RULE_CASE_UNLOCKED(Property
, Property
)
282 RULE_CASE_UNLOCKED(Supports
, Supports
)
283 RULE_CASE_UNLOCKED(Document
, MozDocument
)
284 RULE_CASE_LOCKED(Import
, Import
)
285 RULE_CASE_UNLOCKED(FontFeatureValues
, FontFeatureValues
)
286 RULE_CASE_UNLOCKED(FontPaletteValues
, FontPaletteValues
)
287 RULE_CASE_LOCKED(FontFace
, FontFace
)
288 RULE_CASE_LOCKED(CounterStyle
, CounterStyle
)
289 RULE_CASE_UNLOCKED(LayerBlock
, LayerBlock
)
290 RULE_CASE_UNLOCKED(LayerStatement
, LayerStatement
)
291 RULE_CASE_UNLOCKED(Container
, Container
)
292 RULE_CASE_UNLOCKED(Scope
, Scope
)
293 RULE_CASE_UNLOCKED(StartingStyle
, StartingStyle
)
294 case StyleCssRuleType::Keyframe
:
295 MOZ_ASSERT_UNREACHABLE("keyframe rule cannot be here");
298 #undef RULE_CASE_WITH_PREFIX
299 #undef RULE_CASE_LOCKED
300 #undef RULE_CASE_UNLOCKED
304 ServoCSSRuleList::~ServoCSSRuleList() {
305 MOZ_ASSERT(!mStyleSheet
, "Backpointer should have been cleared");
306 MOZ_ASSERT(!mParentRule
, "Backpointer should have been cleared");
310 bool ServoCSSRuleList::IsReadOnly() const {
311 MOZ_ASSERT(!mStyleSheet
|| !mParentRule
||
312 mStyleSheet
->IsReadOnly() == mParentRule
->IsReadOnly(),
313 "a parent rule should be read only iff the owning sheet is "
315 return mStyleSheet
&& mStyleSheet
->IsReadOnly();
318 } // namespace mozilla