Bug 1839526 [wpt PR 40658] - Update wpt metadata, a=testonly
[gecko.git] / layout / style / ServoCSSRuleList.cpp
blob60c449dd2ca94fc35b2523f6a9e8842aab752373
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;
34 namespace mozilla {
36 ServoCSSRuleList::ServoCSSRuleList(
37 already_AddRefed<StyleLockedCssRules> aRawRules, StyleSheet* aSheet,
38 css::GroupRule* aParentRule)
39 : mStyleSheet(aSheet), mParentRule(aParentRule), mRawRules(aRawRules) {
40 ResetRules();
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)
53 tmp->DropAllRules();
54 NS_IMPL_CYCLE_COLLECTION_UNLINK_END_INHERITED(dom::CSSRuleList)
55 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(ServoCSSRuleList,
56 dom::CSSRuleList)
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);
62 });
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) \
75 .Consume(); \
76 MOZ_ASSERT(raw); \
77 ruleObj = new CSS##name_##Rule(raw.forget(), mStyleSheet, mParentRule, \
78 line, column); \
79 MOZ_ASSERT(ruleObj->Type() == StyleCssRuleType(rule)); \
80 break; \
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");
106 return nullptr;
108 rule = CastToUint(ruleObj.forget().take());
109 mRules[aIndex] = rule;
111 return CastToPtr(rule);
114 css::Rule* ServoCSSRuleList::IndexedGetter(uint32_t aIndex, bool& aFound) {
115 if (aIndex >= mRules.Length()) {
116 aFound = false;
117 return nullptr;
119 aFound = true;
120 return GetRule(aIndex);
123 template <typename Func>
124 void ServoCSSRuleList::EnumerateInstantiatedRules(Func aCallback) {
125 uint32_t index = 0;
126 for (uintptr_t rule : mRules) {
127 if (rule > kMaxRuleType) {
128 aCallback(CastToPtr(rule), index);
130 index++;
134 static void DropRule(already_AddRefed<css::Rule> aRule) {
135 RefPtr<css::Rule> rule = aRule;
136 rule->DropReferences();
139 void ServoCSSRuleList::ResetRules() {
140 // DropRule could reenter here via the cycle collector.
141 auto rules = std::move(mRules);
142 for (uintptr_t rule : rules) {
143 if (rule > kMaxRuleType) {
144 DropRule(already_AddRefed<css::Rule>(CastToPtr(rule)));
147 MOZ_ASSERT(mRules.IsEmpty());
148 if (mRawRules) {
149 Servo_CssRules_ListTypes(mRawRules, &mRules);
153 void ServoCSSRuleList::DropAllRules() {
154 mStyleSheet = nullptr;
155 mParentRule = nullptr;
156 mRawRules = nullptr;
158 ResetRules();
161 void ServoCSSRuleList::DropSheetReference() {
162 // If mStyleSheet is not set on this rule list, then it is not set on any of
163 // its instantiated rules either. To avoid O(N^2) beavhior in the depth of
164 // group rule nesting, which can happen if we are unlinked starting from the
165 // deepest nested group rule, skip recursing into the rule list if we know we
166 // don't need to.
167 if (!mStyleSheet) {
168 return;
170 mStyleSheet = nullptr;
171 EnumerateInstantiatedRules(
172 [](css::Rule* rule, uint32_t) { rule->DropSheetReference(); });
175 void ServoCSSRuleList::DropParentRuleReference() {
176 mParentRule = nullptr;
177 EnumerateInstantiatedRules(
178 [](css::Rule* rule, uint32_t) { rule->DropParentRuleReference(); });
181 nsresult ServoCSSRuleList::InsertRule(const nsACString& aRule,
182 uint32_t aIndex) {
183 MOZ_ASSERT(mStyleSheet,
184 "Caller must ensure that "
185 "the list is not unlinked from stylesheet");
187 if (!mRawRules || IsReadOnly()) {
188 return NS_OK;
191 mStyleSheet->WillDirty();
193 css::Loader* loader = nullptr;
194 auto allowImportRules = mStyleSheet->SelfOrAncestorIsConstructed()
195 ? StyleAllowImportRules::No
196 : StyleAllowImportRules::Yes;
198 // TODO(emilio, bug 1535456): Should probably always be able to get a handle
199 // to some loader if we're parsing an @import rule, but which one?
201 // StyleSheet::ReparseSheet just mints a new loader, but that'd be wrong in
202 // this case I think, since such a load will bypass CSP checks.
203 if (Document* doc = mStyleSheet->GetAssociatedDocument()) {
204 loader = doc->CSSLoader();
206 StyleCssRuleType type;
207 uint32_t containingTypes = 0;
208 for (css::Rule* rule = mParentRule; rule; rule = rule->GetParentRule()) {
209 containingTypes |= (1 << uint32_t(rule->Type()));
211 nsresult rv = Servo_CssRules_InsertRule(
212 mRawRules, mStyleSheet->RawContents(), &aRule, aIndex, containingTypes,
213 loader, allowImportRules, mStyleSheet, &type);
214 NS_ENSURE_SUCCESS(rv, rv);
215 mRules.InsertElementAt(aIndex, uintptr_t(type));
216 return rv;
219 nsresult ServoCSSRuleList::DeleteRule(uint32_t aIndex) {
220 if (!mRawRules || IsReadOnly()) {
221 return NS_OK;
224 nsresult rv = Servo_CssRules_DeleteRule(mRawRules, aIndex);
225 if (!NS_FAILED(rv)) {
226 uintptr_t rule = mRules[aIndex];
227 mRules.RemoveElementAt(aIndex);
228 if (rule > kMaxRuleType) {
229 DropRule(already_AddRefed<css::Rule>(CastToPtr(rule)));
232 return rv;
235 void ServoCSSRuleList::SetRawContents(RefPtr<StyleLockedCssRules> aNewRules,
236 bool aFromClone) {
237 mRawRules = std::move(aNewRules);
238 if (!aFromClone) {
239 ResetRules();
240 return;
243 EnumerateInstantiatedRules([&](css::Rule* aRule, uint32_t aIndex) {
244 #define RULE_CASE_WITH_PREFIX(constant_, prefix_, type_) \
245 case StyleCssRuleType::constant_: { \
246 uint32_t line = 0, column = 0; \
247 RefPtr<Style##prefix_##constant_##Rule> raw = \
248 Servo_CssRules_Get##constant_##RuleAt(mRawRules, aIndex, &line, \
249 &column) \
250 .Consume(); \
251 static_cast<dom::CSS##type_##Rule*>(aRule)->SetRawAfterClone( \
252 std::move(raw)); \
253 break; \
255 #define RULE_CASE_LOCKED(constant_, type_) \
256 RULE_CASE_WITH_PREFIX(constant_, Locked, type_)
257 #define RULE_CASE_UNLOCKED(constant_, type_) \
258 RULE_CASE_WITH_PREFIX(constant_, , type_)
259 switch (aRule->Type()) {
260 RULE_CASE_LOCKED(Style, Style)
261 RULE_CASE_LOCKED(Keyframes, Keyframes)
262 RULE_CASE_UNLOCKED(Media, Media)
263 RULE_CASE_UNLOCKED(Namespace, Namespace)
264 RULE_CASE_LOCKED(Page, Page)
265 RULE_CASE_UNLOCKED(Property, Property)
266 RULE_CASE_UNLOCKED(Supports, Supports)
267 RULE_CASE_UNLOCKED(Document, MozDocument)
268 RULE_CASE_LOCKED(Import, Import)
269 RULE_CASE_UNLOCKED(FontFeatureValues, FontFeatureValues)
270 RULE_CASE_UNLOCKED(FontPaletteValues, FontPaletteValues)
271 RULE_CASE_LOCKED(FontFace, FontFace)
272 RULE_CASE_LOCKED(CounterStyle, CounterStyle)
273 RULE_CASE_UNLOCKED(LayerBlock, LayerBlock)
274 RULE_CASE_UNLOCKED(LayerStatement, LayerStatement)
275 RULE_CASE_UNLOCKED(Container, Container)
276 case StyleCssRuleType::Keyframe:
277 MOZ_ASSERT_UNREACHABLE("keyframe rule cannot be here");
278 break;
280 #undef RULE_CASE_WITH_PREFIX
281 #undef RULE_CASE_LOCKED
282 #undef RULE_CASE_UNLOCKED
286 ServoCSSRuleList::~ServoCSSRuleList() {
287 MOZ_ASSERT(!mStyleSheet, "Backpointer should have been cleared");
288 MOZ_ASSERT(!mParentRule, "Backpointer should have been cleared");
289 DropAllRules();
292 bool ServoCSSRuleList::IsReadOnly() const {
293 MOZ_ASSERT(!mStyleSheet || !mParentRule ||
294 mStyleSheet->IsReadOnly() == mParentRule->IsReadOnly(),
295 "a parent rule should be read only iff the owning sheet is "
296 "read only");
297 return mStyleSheet && mStyleSheet->IsReadOnly();
300 } // namespace mozilla