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/Document.h"
14 #include "mozilla/dom/Element.h"
15 #include "mozilla/dom/ShadowRoot.h"
16 #include "mozilla/IntegerRange.h"
17 #include "mozilla/ServoStyleSet.h"
18 #include "mozilla/StyleSheetInlines.h"
19 #include "nsStyleSheetService.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::Import
:
85 case StyleCssRuleType::Media
:
86 case StyleCssRuleType::Supports
:
87 case StyleCssRuleType::LayerBlock
:
88 case StyleCssRuleType::Container
:
89 case StyleCssRuleType::Document
: {
90 // See the comment in SheetRemoved.
94 case StyleCssRuleType::LayerStatement
:
95 case StyleCssRuleType::FontFace
:
96 case StyleCssRuleType::Page
:
97 case StyleCssRuleType::Property
:
98 case StyleCssRuleType::Keyframes
:
99 case StyleCssRuleType::Keyframe
:
100 case StyleCssRuleType::Margin
:
101 case StyleCssRuleType::Namespace
:
102 case StyleCssRuleType::CounterStyle
:
103 case StyleCssRuleType::FontFeatureValues
:
104 case StyleCssRuleType::FontPaletteValues
:
109 size_t ServoStyleRuleMap::SizeOfIncludingThis(
110 MallocSizeOf aMallocSizeOf
) const {
111 size_t n
= aMallocSizeOf(this);
112 n
+= mTable
.ShallowSizeOfExcludingThis(aMallocSizeOf
);
116 void ServoStyleRuleMap::FillTableFromRule(css::Rule
& aRule
) {
117 switch (aRule
.Type()) {
118 case StyleCssRuleType::Style
: {
119 auto& rule
= static_cast<CSSStyleRule
&>(aRule
);
120 mTable
.InsertOrUpdate(rule
.Raw(), &rule
);
123 case StyleCssRuleType::LayerBlock
:
124 case StyleCssRuleType::Media
:
125 case StyleCssRuleType::Supports
:
126 case StyleCssRuleType::Container
:
127 case StyleCssRuleType::Document
: {
128 auto& rule
= static_cast<css::GroupRule
&>(aRule
);
129 FillTableFromRuleList(*rule
.CssRules());
132 case StyleCssRuleType::Import
: {
133 auto& rule
= static_cast<CSSImportRule
&>(aRule
);
134 if (auto* sheet
= rule
.GetStyleSheet()) {
135 FillTableFromStyleSheet(*sheet
);
139 case StyleCssRuleType::LayerStatement
:
140 case StyleCssRuleType::FontFace
:
141 case StyleCssRuleType::Page
:
142 case StyleCssRuleType::Property
:
143 case StyleCssRuleType::Keyframes
:
144 case StyleCssRuleType::Keyframe
:
145 case StyleCssRuleType::Margin
:
146 case StyleCssRuleType::Namespace
:
147 case StyleCssRuleType::CounterStyle
:
148 case StyleCssRuleType::FontFeatureValues
:
149 case StyleCssRuleType::FontPaletteValues
:
154 void ServoStyleRuleMap::FillTableFromRuleList(ServoCSSRuleList
& aRuleList
) {
155 for (uint32_t i
: IntegerRange(aRuleList
.Length())) {
156 FillTableFromRule(*aRuleList
.GetRule(i
));
160 void ServoStyleRuleMap::FillTableFromStyleSheet(StyleSheet
& aSheet
) {
161 if (aSheet
.IsComplete()) {
162 FillTableFromRuleList(*aSheet
.GetCssRulesInternal());
166 } // namespace mozilla