Bug 1826564 [wpt PR 39394] - Update mypy, a=testonly
[gecko.git] / layout / style / ServoCSSRuleList.cpp
blob3d8618ac5866c2be3227bec2befe0ead0a552db9
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/CSSStyleRule.h"
25 #include "mozilla/dom/CSSSupportsRule.h"
26 #include "mozilla/IntegerRange.h"
27 #include "mozilla/ServoBindings.h"
28 #include "mozilla/StyleSheet.h"
29 #include "mozilla/dom/Document.h"
31 using namespace mozilla::dom;
33 namespace mozilla {
35 ServoCSSRuleList::ServoCSSRuleList(already_AddRefed<ServoCssRules> aRawRules,
36 StyleSheet* aSheet,
37 css::GroupRule* aParentRule)
38 : mStyleSheet(aSheet), mParentRule(aParentRule), mRawRules(aRawRules) {
39 ResetRules();
42 // QueryInterface implementation for ServoCSSRuleList
43 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ServoCSSRuleList)
44 NS_INTERFACE_MAP_END_INHERITING(dom::CSSRuleList)
46 NS_IMPL_ADDREF_INHERITED(ServoCSSRuleList, dom::CSSRuleList)
47 NS_IMPL_RELEASE_INHERITED(ServoCSSRuleList, dom::CSSRuleList)
49 NS_IMPL_CYCLE_COLLECTION_CLASS(ServoCSSRuleList)
51 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ServoCSSRuleList)
52 tmp->DropAllRules();
53 NS_IMPL_CYCLE_COLLECTION_UNLINK_END_INHERITED(dom::CSSRuleList)
54 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(ServoCSSRuleList,
55 dom::CSSRuleList)
56 tmp->EnumerateInstantiatedRules([&](css::Rule* aRule, uint32_t) {
57 if (!aRule->IsCCLeaf()) {
58 NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mRules[i]");
59 cb.NoteXPCOMChild(aRule);
61 });
62 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
64 css::Rule* ServoCSSRuleList::GetRule(uint32_t aIndex) {
65 uintptr_t rule = mRules[aIndex];
66 if (rule <= kMaxRuleType) {
67 RefPtr<css::Rule> ruleObj = nullptr;
68 switch (StyleCssRuleType(rule)) {
69 #define CASE_RULE(const_, name_) \
70 case StyleCssRuleType::const_: { \
71 uint32_t line = 0, column = 0; \
72 RefPtr<RawServo##name_##Rule> raw = \
73 Servo_CssRules_Get##name_##RuleAt(mRawRules, aIndex, &line, &column) \
74 .Consume(); \
75 MOZ_ASSERT(raw); \
76 ruleObj = new CSS##name_##Rule(raw.forget(), mStyleSheet, mParentRule, \
77 line, column); \
78 MOZ_ASSERT(ruleObj->Type() == StyleCssRuleType(rule)); \
79 break; \
81 CASE_RULE(Style, Style)
82 CASE_RULE(Keyframes, Keyframes)
83 CASE_RULE(Media, Media)
84 CASE_RULE(Namespace, Namespace)
85 CASE_RULE(Page, Page)
86 CASE_RULE(Supports, Supports)
87 CASE_RULE(Document, MozDocument)
88 CASE_RULE(Import, Import)
89 CASE_RULE(FontFeatureValues, FontFeatureValues)
90 CASE_RULE(FontPaletteValues, FontPaletteValues)
91 CASE_RULE(FontFace, FontFace)
92 CASE_RULE(CounterStyle, CounterStyle)
93 CASE_RULE(LayerBlock, LayerBlock)
94 CASE_RULE(LayerStatement, LayerStatement)
95 CASE_RULE(Container, Container)
96 #undef CASE_RULE
97 case StyleCssRuleType::Viewport:
98 MOZ_ASSERT_UNREACHABLE("viewport is not implemented in Gecko");
99 return nullptr;
100 case StyleCssRuleType::Keyframe:
101 MOZ_ASSERT_UNREACHABLE("keyframe rule cannot be here");
102 return nullptr;
104 rule = CastToUint(ruleObj.forget().take());
105 mRules[aIndex] = rule;
107 return CastToPtr(rule);
110 css::Rule* ServoCSSRuleList::IndexedGetter(uint32_t aIndex, bool& aFound) {
111 if (aIndex >= mRules.Length()) {
112 aFound = false;
113 return nullptr;
115 aFound = true;
116 return GetRule(aIndex);
119 template <typename Func>
120 void ServoCSSRuleList::EnumerateInstantiatedRules(Func aCallback) {
121 uint32_t index = 0;
122 for (uintptr_t rule : mRules) {
123 if (rule > kMaxRuleType) {
124 aCallback(CastToPtr(rule), index);
126 index++;
130 static void DropRule(already_AddRefed<css::Rule> aRule) {
131 RefPtr<css::Rule> rule = aRule;
132 rule->DropReferences();
135 void ServoCSSRuleList::ResetRules() {
136 // DropRule could reenter here via the cycle collector.
137 auto rules = std::move(mRules);
138 for (uintptr_t rule : rules) {
139 if (rule > kMaxRuleType) {
140 DropRule(already_AddRefed<css::Rule>(CastToPtr(rule)));
143 MOZ_ASSERT(mRules.IsEmpty());
144 if (mRawRules) {
145 Servo_CssRules_ListTypes(mRawRules, &mRules);
149 void ServoCSSRuleList::DropAllRules() {
150 mStyleSheet = nullptr;
151 mParentRule = nullptr;
152 mRawRules = nullptr;
154 ResetRules();
157 void ServoCSSRuleList::DropSheetReference() {
158 // If mStyleSheet is not set on this rule list, then it is not set on any of
159 // its instantiated rules either. To avoid O(N^2) beavhior in the depth of
160 // group rule nesting, which can happen if we are unlinked starting from the
161 // deepest nested group rule, skip recursing into the rule list if we know we
162 // don't need to.
163 if (!mStyleSheet) {
164 return;
166 mStyleSheet = nullptr;
167 EnumerateInstantiatedRules(
168 [](css::Rule* rule, uint32_t) { rule->DropSheetReference(); });
171 void ServoCSSRuleList::DropParentRuleReference() {
172 mParentRule = nullptr;
173 EnumerateInstantiatedRules(
174 [](css::Rule* rule, uint32_t) { rule->DropParentRuleReference(); });
177 nsresult ServoCSSRuleList::InsertRule(const nsACString& aRule,
178 uint32_t aIndex) {
179 MOZ_ASSERT(mStyleSheet,
180 "Caller must ensure that "
181 "the list is not unlinked from stylesheet");
183 if (!mRawRules || IsReadOnly()) {
184 return NS_OK;
187 mStyleSheet->WillDirty();
189 bool nested = !!mParentRule;
190 css::Loader* loader = nullptr;
191 auto allowImportRules = mStyleSheet->SelfOrAncestorIsConstructed()
192 ? StyleAllowImportRules::No
193 : StyleAllowImportRules::Yes;
195 // TODO(emilio, bug 1535456): Should probably always be able to get a handle
196 // to some loader if we're parsing an @import rule, but which one?
198 // StyleSheet::ReparseSheet just mints a new loader, but that'd be wrong in
199 // this case I think, since such a load will bypass CSP checks.
200 if (Document* doc = mStyleSheet->GetAssociatedDocument()) {
201 loader = doc->CSSLoader();
203 StyleCssRuleType type;
204 nsresult rv = Servo_CssRules_InsertRule(mRawRules, mStyleSheet->RawContents(),
205 &aRule, aIndex, nested, loader,
206 allowImportRules, mStyleSheet, &type);
207 if (NS_FAILED(rv)) {
208 return rv;
210 mRules.InsertElementAt(aIndex, uintptr_t(type));
211 return rv;
214 nsresult ServoCSSRuleList::DeleteRule(uint32_t aIndex) {
215 if (!mRawRules || IsReadOnly()) {
216 return NS_OK;
219 nsresult rv = Servo_CssRules_DeleteRule(mRawRules, aIndex);
220 if (!NS_FAILED(rv)) {
221 uintptr_t rule = mRules[aIndex];
222 mRules.RemoveElementAt(aIndex);
223 if (rule > kMaxRuleType) {
224 DropRule(already_AddRefed<css::Rule>(CastToPtr(rule)));
227 return rv;
230 void ServoCSSRuleList::SetRawContents(RefPtr<ServoCssRules> aNewRules,
231 bool aFromClone) {
232 mRawRules = std::move(aNewRules);
233 if (!aFromClone) {
234 ResetRules();
235 return;
238 EnumerateInstantiatedRules([&](css::Rule* aRule, uint32_t aIndex) {
239 #define CASE_FOR(constant_, type_) \
240 case StyleCssRuleType::constant_: { \
241 uint32_t line = 0, column = 0; \
242 RefPtr<RawServo##type_##Rule> raw = \
243 Servo_CssRules_Get##type_##RuleAt(mRawRules, aIndex, &line, &column) \
244 .Consume(); \
245 static_cast<dom::CSS##type_##Rule*>(aRule)->SetRawAfterClone( \
246 std::move(raw)); \
247 break; \
249 switch (aRule->Type()) {
250 CASE_FOR(Style, Style)
251 CASE_FOR(Keyframes, Keyframes)
252 CASE_FOR(Media, Media)
253 CASE_FOR(Namespace, Namespace)
254 CASE_FOR(Page, Page)
255 CASE_FOR(Supports, Supports)
256 CASE_FOR(Document, MozDocument)
257 CASE_FOR(Import, Import)
258 CASE_FOR(FontFeatureValues, FontFeatureValues)
259 CASE_FOR(FontPaletteValues, FontPaletteValues)
260 CASE_FOR(FontFace, FontFace)
261 CASE_FOR(CounterStyle, CounterStyle)
262 CASE_FOR(LayerBlock, LayerBlock)
263 CASE_FOR(LayerStatement, LayerStatement)
264 CASE_FOR(Container, Container)
265 case StyleCssRuleType::Keyframe:
266 MOZ_ASSERT_UNREACHABLE("keyframe rule cannot be here");
267 break;
268 case StyleCssRuleType::Viewport:
269 MOZ_ASSERT_UNREACHABLE("Gecko doesn't implemente @viewport?");
270 break;
272 #undef CASE_FOR
276 ServoCSSRuleList::~ServoCSSRuleList() {
277 MOZ_ASSERT(!mStyleSheet, "Backpointer should have been cleared");
278 MOZ_ASSERT(!mParentRule, "Backpointer should have been cleared");
279 DropAllRules();
282 bool ServoCSSRuleList::IsReadOnly() const {
283 MOZ_ASSERT(!mStyleSheet || !mParentRule ||
284 mStyleSheet->IsReadOnly() == mParentRule->IsReadOnly(),
285 "a parent rule should be read only iff the owning sheet is "
286 "read only");
287 return mStyleSheet && mStyleSheet->IsReadOnly();
290 } // namespace mozilla