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 #include "mozilla/ServoStyleRuleMap.h"
9 #include "mozilla/css/GroupRule.h"
10 #include "mozilla/dom/CSSImportRule.h"
11 #include "mozilla/dom/CSSRuleBinding.h"
12 #include "mozilla/dom/CSSStyleRule.h"
13 #include "mozilla/dom/CSSNestedDeclarations.h"
14 #include "mozilla/dom/Document.h"
15 #include "mozilla/dom/Element.h"
16 #include "mozilla/dom/ShadowRoot.h"
17 #include "mozilla/IntegerRange.h"
18 #include "mozilla/ServoStyleSet.h"
19 #include "mozilla/StyleSheetInlines.h"
21 using namespace mozilla::dom
;
25 void ServoStyleRuleMap::EnsureTable(ServoStyleSet
& aStyleSet
) {
29 aStyleSet
.EnumerateStyleSheets(
30 [&](StyleSheet
& aSheet
) { FillTableFromStyleSheet(aSheet
); });
33 void ServoStyleRuleMap::EnsureTable(ShadowRoot
& aShadowRoot
) {
37 for (auto index
: IntegerRange(aShadowRoot
.SheetCount())) {
38 FillTableFromStyleSheet(*aShadowRoot
.SheetAt(index
));
40 for (const auto& sheet
: aShadowRoot
.AdoptedStyleSheets()) {
41 FillTableFromStyleSheet(*sheet
);
45 void ServoStyleRuleMap::SheetAdded(StyleSheet
& aStyleSheet
) {
47 FillTableFromStyleSheet(aStyleSheet
);
51 void ServoStyleRuleMap::SheetCloned(StyleSheet
& aStyleSheet
) {
52 // Invalidate all data inside. We could probably track down all the individual
53 // rules that changed etc, but it doesn't seem worth it.
55 // TODO: We can't do this until GetCssRulesInternal stops cloning.
59 void ServoStyleRuleMap::SheetRemoved(StyleSheet
& aStyleSheet
) {
60 // Invalidate all data inside. This isn't strictly necessary since
61 // we should always get update from document before new queries come.
62 // But it is probably still safer if we try to avoid having invalid
63 // pointers inside. Also if the document keep adding and removing
64 // stylesheets, this would also prevent us from infinitely growing
69 void ServoStyleRuleMap::RuleAdded(StyleSheet
& aStyleSheet
,
70 css::Rule
& aStyleRule
) {
72 FillTableFromRule(aStyleRule
);
76 void ServoStyleRuleMap::RuleRemoved(StyleSheet
& aStyleSheet
,
77 css::Rule
& aStyleRule
) {
82 switch (aStyleRule
.Type()) {
83 case StyleCssRuleType::Style
:
84 case StyleCssRuleType::NestedDeclarations
:
85 case StyleCssRuleType::Import
:
86 case StyleCssRuleType::Media
:
87 case StyleCssRuleType::Supports
:
88 case StyleCssRuleType::LayerBlock
:
89 case StyleCssRuleType::Container
:
90 case StyleCssRuleType::Document
:
91 case StyleCssRuleType::Scope
:
92 case StyleCssRuleType::StartingStyle
:
93 case StyleCssRuleType::PositionTry
: {
94 // See the comment in SheetRemoved.
98 case StyleCssRuleType::LayerStatement
:
99 case StyleCssRuleType::FontFace
:
100 case StyleCssRuleType::Page
:
101 case StyleCssRuleType::Property
:
102 case StyleCssRuleType::Keyframes
:
103 case StyleCssRuleType::Keyframe
:
104 case StyleCssRuleType::Margin
:
105 case StyleCssRuleType::Namespace
:
106 case StyleCssRuleType::CounterStyle
:
107 case StyleCssRuleType::FontFeatureValues
:
108 case StyleCssRuleType::FontPaletteValues
:
113 size_t ServoStyleRuleMap::SizeOfIncludingThis(
114 MallocSizeOf aMallocSizeOf
) const {
115 size_t n
= aMallocSizeOf(this);
116 n
+= mTable
.ShallowSizeOfExcludingThis(aMallocSizeOf
);
120 void ServoStyleRuleMap::RuleDeclarationsChanged(
121 css::Rule
& aRule
, const StyleLockedDeclarationBlock
* aOld
,
122 const StyleLockedDeclarationBlock
* aNew
) {
128 auto old
= mTable
.Extract(aOld
);
129 MOZ_ASSERT(old
.valueOr(nullptr) == &aRule
,
130 "We were tracking the wrong rule?");
131 mTable
.InsertOrUpdate(aNew
, &aRule
);
134 void ServoStyleRuleMap::FillTableFromRule(css::Rule
& aRule
) {
135 switch (aRule
.Type()) {
136 case StyleCssRuleType::NestedDeclarations
: {
137 auto& rule
= static_cast<CSSNestedDeclarations
&>(aRule
);
138 mTable
.InsertOrUpdate(rule
.RawStyle(), &rule
);
141 case StyleCssRuleType::Style
: {
142 auto& rule
= static_cast<CSSStyleRule
&>(aRule
);
143 mTable
.InsertOrUpdate(rule
.RawStyle(), &rule
);
146 case StyleCssRuleType::LayerBlock
:
147 case StyleCssRuleType::Media
:
148 case StyleCssRuleType::Supports
:
149 case StyleCssRuleType::Container
:
150 case StyleCssRuleType::Document
:
151 case StyleCssRuleType::Scope
:
152 case StyleCssRuleType::StartingStyle
: {
153 auto& rule
= static_cast<css::GroupRule
&>(aRule
);
154 FillTableFromRuleList(*rule
.CssRules());
157 case StyleCssRuleType::Import
: {
158 auto& rule
= static_cast<CSSImportRule
&>(aRule
);
159 if (auto* sheet
= rule
.GetStyleSheet()) {
160 FillTableFromStyleSheet(*sheet
);
164 case StyleCssRuleType::LayerStatement
:
165 case StyleCssRuleType::FontFace
:
166 case StyleCssRuleType::Page
:
167 case StyleCssRuleType::Property
:
168 case StyleCssRuleType::Keyframes
:
169 case StyleCssRuleType::Keyframe
:
170 case StyleCssRuleType::Margin
:
171 case StyleCssRuleType::Namespace
:
172 case StyleCssRuleType::CounterStyle
:
173 case StyleCssRuleType::FontFeatureValues
:
174 case StyleCssRuleType::FontPaletteValues
:
175 case StyleCssRuleType::PositionTry
:
180 void ServoStyleRuleMap::FillTableFromRuleList(ServoCSSRuleList
& aRuleList
) {
181 for (uint32_t i
: IntegerRange(aRuleList
.Length())) {
182 FillTableFromRule(*aRuleList
.GetRule(i
));
186 void ServoStyleRuleMap::FillTableFromStyleSheet(StyleSheet
& aSheet
) {
187 if (aSheet
.IsComplete()) {
188 FillTableFromRuleList(*aSheet
.GetCssRulesInternal());
192 } // namespace mozilla