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::Namespace
:
101 case StyleCssRuleType::CounterStyle
:
102 case StyleCssRuleType::FontFeatureValues
:
103 case StyleCssRuleType::FontPaletteValues
:
108 size_t ServoStyleRuleMap::SizeOfIncludingThis(
109 MallocSizeOf aMallocSizeOf
) const {
110 size_t n
= aMallocSizeOf(this);
111 n
+= mTable
.ShallowSizeOfExcludingThis(aMallocSizeOf
);
115 void ServoStyleRuleMap::FillTableFromRule(css::Rule
& aRule
) {
116 switch (aRule
.Type()) {
117 case StyleCssRuleType::Style
: {
118 auto& rule
= static_cast<CSSStyleRule
&>(aRule
);
119 mTable
.InsertOrUpdate(rule
.Raw(), &rule
);
122 case StyleCssRuleType::LayerBlock
:
123 case StyleCssRuleType::Media
:
124 case StyleCssRuleType::Supports
:
125 case StyleCssRuleType::Container
:
126 case StyleCssRuleType::Document
: {
127 auto& rule
= static_cast<css::GroupRule
&>(aRule
);
128 FillTableFromRuleList(*rule
.CssRules());
131 case StyleCssRuleType::Import
: {
132 auto& rule
= static_cast<CSSImportRule
&>(aRule
);
133 if (auto* sheet
= rule
.GetStyleSheet()) {
134 FillTableFromStyleSheet(*sheet
);
138 case StyleCssRuleType::LayerStatement
:
139 case StyleCssRuleType::FontFace
:
140 case StyleCssRuleType::Page
:
141 case StyleCssRuleType::Property
:
142 case StyleCssRuleType::Keyframes
:
143 case StyleCssRuleType::Keyframe
:
144 case StyleCssRuleType::Namespace
:
145 case StyleCssRuleType::CounterStyle
:
146 case StyleCssRuleType::FontFeatureValues
:
147 case StyleCssRuleType::FontPaletteValues
:
152 void ServoStyleRuleMap::FillTableFromRuleList(ServoCSSRuleList
& aRuleList
) {
153 for (uint32_t i
: IntegerRange(aRuleList
.Length())) {
154 FillTableFromRule(*aRuleList
.GetRule(i
));
158 void ServoStyleRuleMap::FillTableFromStyleSheet(StyleSheet
& aSheet
) {
159 if (aSheet
.IsComplete()) {
160 FillTableFromRuleList(*aSheet
.GetCssRulesInternal());
164 } // namespace mozilla