OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2012 Apple Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without | |
5 * modification, are permitted provided that the following conditions | |
6 * are met: | |
7 * 1. Redistributions of source code must retain the above copyright | |
8 * notice, this list of conditions and the following disclaimer. | |
9 * 2. Redistributions in binary form must reproduce the above copyright | |
10 * notice, this list of conditions and the following disclaimer in the | |
11 * documentation and/or other materials provided with the distribution. | |
12 * | |
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | |
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | |
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
24 */ | |
25 | |
26 #include "core/css/invalidation/StyleSheetInvalidationAnalysis.h" | |
27 | |
28 #include "core/css/CSSSelectorList.h" | |
29 #include "core/css/StyleRuleImport.h" | |
30 #include "core/css/StyleSheetContents.h" | |
31 #include "core/css/resolver/ScopedStyleResolver.h" | |
32 #include "core/dom/ContainerNode.h" | |
33 #include "core/dom/Document.h" | |
34 #include "core/dom/ElementTraversal.h" | |
35 #include "core/dom/StyleChangeReason.h" | |
36 #include "core/dom/StyleEngine.h" | |
37 #include "core/dom/TreeScope.h" | |
38 #include "core/dom/shadow/ShadowRoot.h" | |
39 #include "core/html/HTMLStyleElement.h" | |
40 | |
41 namespace blink { | |
42 | |
43 StyleSheetInvalidationAnalysis::StyleSheetInvalidationAnalysis( | |
44 const TreeScope& treeScope, | |
45 const HeapVector<Member<StyleSheetContents>>& sheets) | |
46 : m_treeScope(&treeScope) { | |
47 for (unsigned i = 0; i < sheets.size() && !m_dirtiesAllStyle; ++i) | |
48 analyzeStyleSheet(sheets[i]); | |
49 } | |
50 | |
51 static bool determineSelectorScopes(const CSSSelectorList& selectorList, | |
52 HashSet<StringImpl*>& idScopes, | |
53 HashSet<StringImpl*>& classScopes) { | |
54 for (const CSSSelector* selector = selectorList.first(); selector; | |
55 selector = CSSSelectorList::next(*selector)) { | |
56 const CSSSelector* scopeSelector = 0; | |
57 // This picks the widest scope, not the narrowest, to minimize the number of | |
58 // found scopes. | |
59 for (const CSSSelector* current = selector; current; | |
60 current = current->tagHistory()) { | |
61 // Prefer ids over classes. | |
62 if (current->match() == CSSSelector::Id) | |
63 scopeSelector = current; | |
64 else if (current->match() == CSSSelector::Class && | |
65 (!scopeSelector || scopeSelector->match() != CSSSelector::Id)) | |
66 scopeSelector = current; | |
67 CSSSelector::RelationType relation = current->relation(); | |
68 // FIXME: it would be better to use setNeedsStyleRecalc for all shadow | |
69 // hosts matching | |
70 // scopeSelector. Currently requests full style recalc. | |
71 if (relation == CSSSelector::ShadowDeep || | |
72 relation == CSSSelector::ShadowPseudo) | |
73 return false; | |
74 if (relation != CSSSelector::Descendant && | |
75 relation != CSSSelector::Child && | |
76 relation != CSSSelector::SubSelector) | |
77 break; | |
78 } | |
79 if (!scopeSelector) | |
80 return false; | |
81 ASSERT(scopeSelector->match() == CSSSelector::Class || | |
82 scopeSelector->match() == CSSSelector::Id); | |
83 if (scopeSelector->match() == CSSSelector::Id) | |
84 idScopes.add(scopeSelector->value().impl()); | |
85 else | |
86 classScopes.add(scopeSelector->value().impl()); | |
87 } | |
88 return true; | |
89 } | |
90 | |
91 static bool ruleAdditionMightRequireDocumentStyleRecalc(StyleRuleBase* rule) { | |
92 // This function is conservative. We only return false when we know that | |
93 // the added @rule can't require style recalcs. | |
94 switch (rule->type()) { | |
95 case StyleRule::Import: // Whatever we import should do its own analysis, | |
96 // we don't need to invalidate the document here! | |
97 case StyleRule::Page: // Page rules apply only during printing, we force a | |
98 // full-recalc before printing. | |
99 return false; | |
100 | |
101 case StyleRule::Media: // If the media rule doesn't apply, we could avoid | |
102 // recalc. | |
103 case StyleRule::FontFace: // If the fonts aren't in use, we could avoid | |
104 // recalc. | |
105 case StyleRule::Supports: // If we evaluated the supports-clause we could | |
106 // avoid recalc. | |
107 case StyleRule::Viewport: // If the viewport doesn't match, we could avoid | |
108 // recalcing. | |
109 return true; | |
110 | |
111 // These should all be impossible to reach: | |
112 case StyleRule::Charset: | |
113 case StyleRule::Keyframe: | |
114 case StyleRule::Namespace: | |
115 case StyleRule::Style: | |
116 case StyleRule::Keyframes: | |
117 break; | |
118 } | |
119 ASSERT_NOT_REACHED(); | |
120 return true; | |
121 } | |
122 | |
123 void StyleSheetInvalidationAnalysis::analyzeStyleSheet( | |
124 StyleSheetContents* styleSheetContents) { | |
125 // Updating the style on the shadow DOM for image fallback content can bring | |
126 // us here when imports are still getting loaded in the main document. Just | |
127 // need to exit early as we will return here when the imports finish loading. | |
128 if (styleSheetContents->isLoading()) | |
129 return; | |
130 | |
131 // See if all rules on the sheet are scoped to some specific ids or classes. | |
132 // Then test if we actually have any of those in the tree at the moment. | |
133 const HeapVector<Member<StyleRuleImport>>& importRules = | |
134 styleSheetContents->importRules(); | |
135 for (unsigned i = 0; i < importRules.size(); ++i) { | |
136 if (!importRules[i]->styleSheet()) | |
137 continue; | |
138 analyzeStyleSheet(importRules[i]->styleSheet()); | |
139 if (m_dirtiesAllStyle) | |
140 return; | |
141 } | |
142 | |
143 if (m_treeScope->rootNode().isShadowRoot()) | |
144 return; | |
145 | |
146 const HeapVector<Member<StyleRuleBase>>& rules = | |
147 styleSheetContents->childRules(); | |
148 for (unsigned i = 0; i < rules.size(); i++) { | |
149 StyleRuleBase* rule = rules[i].get(); | |
150 if (!rule->isStyleRule()) { | |
151 if (rule->type() == StyleRule::Keyframes) { | |
152 m_addsKeyframes = true; | |
153 continue; | |
154 } | |
155 if (ruleAdditionMightRequireDocumentStyleRecalc(rule)) { | |
156 m_dirtiesAllStyle = true; | |
157 return; | |
158 } | |
159 continue; | |
160 } | |
161 StyleRule* styleRule = toStyleRule(rule); | |
162 if (!determineSelectorScopes(styleRule->selectorList(), m_idScopes, | |
163 m_classScopes)) { | |
164 m_dirtiesAllStyle = true; | |
165 return; | |
166 } | |
167 } | |
168 } | |
169 | |
170 static bool elementMatchesSelectorScopes( | |
171 const Element* element, | |
172 const HashSet<StringImpl*>& idScopes, | |
173 const HashSet<StringImpl*>& classScopes) { | |
174 if (!idScopes.isEmpty() && element->hasID() && | |
175 idScopes.contains(element->idForStyleResolution().impl())) | |
176 return true; | |
177 if (classScopes.isEmpty() || !element->hasClass()) | |
178 return false; | |
179 const SpaceSplitString& classNames = element->classNames(); | |
180 for (unsigned i = 0; i < classNames.size(); ++i) { | |
181 if (classScopes.contains(classNames[i].impl())) | |
182 return true; | |
183 } | |
184 return false; | |
185 } | |
186 | |
187 void StyleSheetInvalidationAnalysis::invalidateStyle() { | |
188 ASSERT(!m_dirtiesAllStyle); | |
189 | |
190 if (m_addsKeyframes) | |
191 ScopedStyleResolver::keyframesRulesAdded(*m_treeScope); | |
192 | |
193 if (m_treeScope->rootNode().isShadowRoot()) { | |
194 ContainerNode& shadowHost = toShadowRoot(m_treeScope->rootNode()).host(); | |
195 shadowHost.setNeedsStyleRecalc(SubtreeStyleChange, | |
196 StyleChangeReasonForTracing::create( | |
197 StyleChangeReason::StyleSheetChange)); | |
198 return; | |
199 } | |
200 | |
201 if (m_idScopes.isEmpty() && m_classScopes.isEmpty()) | |
202 return; | |
203 Element* element = ElementTraversal::firstWithin(m_treeScope->document()); | |
204 while (element) { | |
205 if (elementMatchesSelectorScopes(element, m_idScopes, m_classScopes)) { | |
206 element->setNeedsStyleRecalc(SubtreeStyleChange, | |
207 StyleChangeReasonForTracing::create( | |
208 StyleChangeReason::StyleSheetChange)); | |
209 // The whole subtree is now invalidated, we can skip to the next sibling. | |
210 element = ElementTraversal::nextSkippingChildren(*element); | |
211 continue; | |
212 } | |
213 element = ElementTraversal::next(*element); | |
214 } | |
215 } | |
216 | |
217 } // namespace blink | |
OLD | NEW |