No bug - tagging b4d3227540c9ebc43d64aac6168fdca7019c22d8 with FIREFOX_BETA_126_BASE...
[gecko.git] / layout / style / ServoCSSRuleList.cpp
blob133a9622567ac909fcde74eaa782c8ee517a3b76
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/CSSScopeRule.h"
26 #include "mozilla/dom/CSSStartingStyleRule.h"
27 #include "mozilla/dom/CSSStyleRule.h"
28 #include "mozilla/dom/CSSSupportsRule.h"
29 #include "mozilla/IntegerRange.h"
30 #include "mozilla/ServoBindings.h"
31 #include "mozilla/StyleSheet.h"
32 #include "mozilla/dom/Document.h"
34 using namespace mozilla::dom;
36 namespace mozilla {
38 ServoCSSRuleList::ServoCSSRuleList(
39 already_AddRefed<StyleLockedCssRules> aRawRules, StyleSheet* aSheet,
40 css::GroupRule* aParentRule)
41 : mStyleSheet(aSheet), mParentRule(aParentRule), mRawRules(aRawRules) {
42 ResetRules();
45 // QueryInterface implementation for ServoCSSRuleList
46 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ServoCSSRuleList)
47 NS_INTERFACE_MAP_END_INHERITING(dom::CSSRuleList)
49 NS_IMPL_ADDREF_INHERITED(ServoCSSRuleList, dom::CSSRuleList)
50 NS_IMPL_RELEASE_INHERITED(ServoCSSRuleList, dom::CSSRuleList)
52 NS_IMPL_CYCLE_COLLECTION_CLASS(ServoCSSRuleList)
54 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ServoCSSRuleList)
55 tmp->DropAllRules();
56 NS_IMPL_CYCLE_COLLECTION_UNLINK_END_INHERITED(dom::CSSRuleList)
57 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(ServoCSSRuleList,
58 dom::CSSRuleList)
59 tmp->EnumerateInstantiatedRules([&](css::Rule* aRule, uint32_t) {
60 if (!aRule->IsCCLeaf()) {
61 NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mRules[i]");
62 cb.NoteXPCOMChild(aRule);
64 });
65 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
67 css::Rule* ServoCSSRuleList::GetRule(uint32_t aIndex) {
68 uintptr_t rule = mRules[aIndex];
69 if (rule <= kMaxRuleType) {
70 RefPtr<css::Rule> ruleObj = nullptr;
71 switch (StyleCssRuleType(rule)) {
72 #define CASE_RULE_WITH_PREFIX(const_, prefix_, name_) \
73 case StyleCssRuleType::const_: { \
74 uint32_t line = 0, column = 0; \
75 RefPtr<Style##prefix_##const_##Rule> raw = \
76 Servo_CssRules_Get##const_##RuleAt(mRawRules, aIndex, &line, &column) \
77 .Consume(); \
78 MOZ_ASSERT(raw); \
79 ruleObj = new CSS##name_##Rule(raw.forget(), mStyleSheet, mParentRule, \
80 line, column); \
81 MOZ_ASSERT(ruleObj->Type() == StyleCssRuleType(rule)); \
82 break; \
84 #define CASE_RULE_LOCKED(const_, name_) \
85 CASE_RULE_WITH_PREFIX(const_, Locked, name_)
86 #define CASE_RULE_UNLOCKED(const_, name_) CASE_RULE_WITH_PREFIX(const_, , name_)
87 CASE_RULE_LOCKED(Style, Style)
88 CASE_RULE_LOCKED(Keyframes, Keyframes)
89 CASE_RULE_UNLOCKED(Media, Media)
90 CASE_RULE_UNLOCKED(Namespace, Namespace)
91 CASE_RULE_LOCKED(Page, Page)
92 CASE_RULE_UNLOCKED(Property, Property)
93 CASE_RULE_UNLOCKED(Supports, Supports)
94 CASE_RULE_UNLOCKED(Document, MozDocument)
95 CASE_RULE_LOCKED(Import, Import)
96 CASE_RULE_UNLOCKED(FontFeatureValues, FontFeatureValues)
97 CASE_RULE_UNLOCKED(FontPaletteValues, FontPaletteValues)
98 CASE_RULE_LOCKED(FontFace, FontFace)
99 CASE_RULE_LOCKED(CounterStyle, CounterStyle)
100 CASE_RULE_UNLOCKED(LayerBlock, LayerBlock)
101 CASE_RULE_UNLOCKED(LayerStatement, LayerStatement)
102 CASE_RULE_UNLOCKED(Container, Container)
103 CASE_RULE_UNLOCKED(Scope, Scope)
104 CASE_RULE_UNLOCKED(StartingStyle, StartingStyle)
105 #undef CASE_RULE_LOCKED
106 #undef CASE_RULE_UNLOCKED
107 #undef CASE_RULE_WITH_PREFIX
108 case StyleCssRuleType::Keyframe:
109 MOZ_ASSERT_UNREACHABLE("keyframe rule cannot be here");
110 return nullptr;
111 case StyleCssRuleType::Margin:
112 // Margin rules not implemented yet, see bug 1864737
113 return nullptr;
115 rule = CastToUint(ruleObj.forget().take());
116 mRules[aIndex] = rule;
118 return CastToPtr(rule);
121 css::Rule* ServoCSSRuleList::IndexedGetter(uint32_t aIndex, bool& aFound) {
122 if (aIndex >= mRules.Length()) {
123 aFound = false;
124 return nullptr;
126 aFound = true;
127 return GetRule(aIndex);
130 template <typename Func>
131 void ServoCSSRuleList::EnumerateInstantiatedRules(Func aCallback) {
132 uint32_t index = 0;
133 for (uintptr_t rule : mRules) {
134 if (rule > kMaxRuleType) {
135 aCallback(CastToPtr(rule), index);
137 index++;
141 static void DropRule(already_AddRefed<css::Rule> aRule) {
142 RefPtr<css::Rule> rule = aRule;
143 rule->DropReferences();
146 void ServoCSSRuleList::ResetRules() {
147 // DropRule could reenter here via the cycle collector.
148 auto rules = std::move(mRules);
149 for (uintptr_t rule : rules) {
150 if (rule > kMaxRuleType) {
151 DropRule(already_AddRefed<css::Rule>(CastToPtr(rule)));
154 MOZ_ASSERT(mRules.IsEmpty());
155 if (mRawRules) {
156 Servo_CssRules_ListTypes(mRawRules, &mRules);
160 void ServoCSSRuleList::DropAllRules() {
161 mStyleSheet = nullptr;
162 mParentRule = nullptr;
163 mRawRules = nullptr;
165 ResetRules();
168 void ServoCSSRuleList::DropSheetReference() {
169 // If mStyleSheet is not set on this rule list, then it is not set on any of
170 // its instantiated rules either. To avoid O(N^2) beavhior in the depth of
171 // group rule nesting, which can happen if we are unlinked starting from the
172 // deepest nested group rule, skip recursing into the rule list if we know we
173 // don't need to.
174 if (!mStyleSheet) {
175 return;
177 mStyleSheet = nullptr;
178 EnumerateInstantiatedRules(
179 [](css::Rule* rule, uint32_t) { rule->DropSheetReference(); });
182 void ServoCSSRuleList::DropParentRuleReference() {
183 mParentRule = nullptr;
184 EnumerateInstantiatedRules(
185 [](css::Rule* rule, uint32_t) { rule->DropParentRuleReference(); });
188 nsresult ServoCSSRuleList::InsertRule(const nsACString& aRule,
189 uint32_t aIndex) {
190 MOZ_ASSERT(mStyleSheet,
191 "Caller must ensure that "
192 "the list is not unlinked from stylesheet");
194 if (!mRawRules || IsReadOnly()) {
195 return NS_OK;
198 mStyleSheet->WillDirty();
200 css::Loader* loader = nullptr;
201 auto allowImportRules = mStyleSheet->SelfOrAncestorIsConstructed()
202 ? StyleAllowImportRules::No
203 : StyleAllowImportRules::Yes;
205 // TODO(emilio, bug 1535456): Should probably always be able to get a handle
206 // to some loader if we're parsing an @import rule, but which one?
208 // StyleSheet::ReparseSheet just mints a new loader, but that'd be wrong in
209 // this case I think, since such a load will bypass CSP checks.
210 if (Document* doc = mStyleSheet->GetAssociatedDocument()) {
211 loader = doc->CSSLoader();
213 StyleCssRuleType type;
214 uint32_t containingTypes = 0;
215 Maybe<StyleCssRuleType> parseRelativeRuleType;
216 for (css::Rule* rule = mParentRule; rule; rule = rule->GetParentRule()) {
217 const auto ruleType = rule->Type();
218 containingTypes |= (1 << uint32_t(ruleType));
219 if (parseRelativeRuleType.isNothing() &&
220 (ruleType == StyleCssRuleType::Style ||
221 ruleType == StyleCssRuleType::Scope)) {
222 // Only the closest applicable type to this rule matters.
223 parseRelativeRuleType = Some(ruleType);
226 nsresult rv = Servo_CssRules_InsertRule(
227 mRawRules, mStyleSheet->RawContents(), &aRule, aIndex, containingTypes,
228 parseRelativeRuleType.ptrOr(nullptr), loader, allowImportRules,
229 mStyleSheet, &type);
230 NS_ENSURE_SUCCESS(rv, rv);
231 mRules.InsertElementAt(aIndex, uintptr_t(type));
232 return rv;
235 nsresult ServoCSSRuleList::DeleteRule(uint32_t aIndex) {
236 if (!mRawRules || IsReadOnly()) {
237 return NS_OK;
240 nsresult rv = Servo_CssRules_DeleteRule(mRawRules, aIndex);
241 if (!NS_FAILED(rv)) {
242 uintptr_t rule = mRules[aIndex];
243 mRules.RemoveElementAt(aIndex);
244 if (rule > kMaxRuleType) {
245 DropRule(already_AddRefed<css::Rule>(CastToPtr(rule)));
248 return rv;
251 void ServoCSSRuleList::SetRawContents(RefPtr<StyleLockedCssRules> aNewRules,
252 bool aFromClone) {
253 mRawRules = std::move(aNewRules);
254 if (!aFromClone) {
255 ResetRules();
256 return;
259 EnumerateInstantiatedRules([&](css::Rule* aRule, uint32_t aIndex) {
260 #define RULE_CASE_WITH_PREFIX(constant_, prefix_, type_) \
261 case StyleCssRuleType::constant_: { \
262 uint32_t line = 0, column = 0; \
263 RefPtr<Style##prefix_##constant_##Rule> raw = \
264 Servo_CssRules_Get##constant_##RuleAt(mRawRules, aIndex, &line, \
265 &column) \
266 .Consume(); \
267 static_cast<dom::CSS##type_##Rule*>(aRule)->SetRawAfterClone( \
268 std::move(raw)); \
269 break; \
271 #define RULE_CASE_LOCKED(constant_, type_) \
272 RULE_CASE_WITH_PREFIX(constant_, Locked, type_)
273 #define RULE_CASE_UNLOCKED(constant_, type_) \
274 RULE_CASE_WITH_PREFIX(constant_, , type_)
275 switch (aRule->Type()) {
276 RULE_CASE_LOCKED(Style, Style)
277 RULE_CASE_LOCKED(Keyframes, Keyframes)
278 RULE_CASE_UNLOCKED(Media, Media)
279 RULE_CASE_UNLOCKED(Namespace, Namespace)
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");
296 break;
297 case StyleCssRuleType::Margin:
298 // Margin rules not implemented yet, see bug 1864737
299 break;
301 #undef RULE_CASE_WITH_PREFIX
302 #undef RULE_CASE_LOCKED
303 #undef RULE_CASE_UNLOCKED
307 ServoCSSRuleList::~ServoCSSRuleList() {
308 MOZ_ASSERT(!mStyleSheet, "Backpointer should have been cleared");
309 MOZ_ASSERT(!mParentRule, "Backpointer should have been cleared");
310 DropAllRules();
313 bool ServoCSSRuleList::IsReadOnly() const {
314 MOZ_ASSERT(!mStyleSheet || !mParentRule ||
315 mStyleSheet->IsReadOnly() == mParentRule->IsReadOnly(),
316 "a parent rule should be read only iff the owning sheet is "
317 "read only");
318 return mStyleSheet && mStyleSheet->IsReadOnly();
321 } // namespace mozilla