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/dom/ContainerNode.h" | |
32 #include "core/dom/Document.h" | |
33 #include "core/dom/ElementTraversal.h" | |
34 #include "core/dom/StyleChangeReason.h" | |
35 #include "core/dom/TreeScope.h" | |
36 #include "core/dom/shadow/ShadowRoot.h" | |
37 #include "core/html/HTMLStyleElement.h" | |
38 | |
39 namespace blink { | |
40 | |
41 StyleSheetInvalidationAnalysis::StyleSheetInvalidationAnalysis(const TreeScope&
treeScope, const HeapVector<Member<StyleSheetContents>>& sheets) | |
42 : m_treeScope(&treeScope) | |
43 { | |
44 for (unsigned i = 0; i < sheets.size() && !m_dirtiesAllStyle; ++i) | |
45 analyzeStyleSheet(sheets[i]); | |
46 } | |
47 | |
48 static bool determineSelectorScopes(const CSSSelectorList& selectorList, HashSet
<StringImpl*>& idScopes, HashSet<StringImpl*>& classScopes) | |
49 { | |
50 for (const CSSSelector* selector = selectorList.first(); selector; selector
= CSSSelectorList::next(*selector)) { | |
51 const CSSSelector* scopeSelector = 0; | |
52 // This picks the widest scope, not the narrowest, to minimize the numbe
r of found scopes. | |
53 for (const CSSSelector* current = selector; current; current = current->
tagHistory()) { | |
54 // Prefer ids over classes. | |
55 if (current->match() == CSSSelector::Id) | |
56 scopeSelector = current; | |
57 else if (current->match() == CSSSelector::Class && (!scopeSelector |
| scopeSelector->match() != CSSSelector::Id)) | |
58 scopeSelector = current; | |
59 CSSSelector::RelationType relation = current->relation(); | |
60 // FIXME: it would be better to use setNeedsStyleRecalc for all shad
ow hosts matching | |
61 // scopeSelector. Currently requests full style recalc. | |
62 if (relation == CSSSelector::ShadowDeep || relation == CSSSelector::
ShadowPseudo) | |
63 return false; | |
64 if (relation != CSSSelector::Descendant && relation != CSSSelector::
Child && relation != CSSSelector::SubSelector) | |
65 break; | |
66 } | |
67 if (!scopeSelector) | |
68 return false; | |
69 ASSERT(scopeSelector->match() == CSSSelector::Class || scopeSelector->ma
tch() == CSSSelector::Id); | |
70 if (scopeSelector->match() == CSSSelector::Id) | |
71 idScopes.add(scopeSelector->value().impl()); | |
72 else | |
73 classScopes.add(scopeSelector->value().impl()); | |
74 } | |
75 return true; | |
76 } | |
77 | |
78 static bool ruleAdditionMightRequireDocumentStyleRecalc(StyleRuleBase* rule) | |
79 { | |
80 // This funciton is conservative. We only return false when we know that | |
81 // the added @rule can't require style recalcs. | |
82 switch (rule->type()) { | |
83 case StyleRule::Import: // Whatever we import should do its own analysis, we
don't need to invalidate the document here! | |
84 case StyleRule::Page: // Page rules apply only during printing, we force a f
ull-recalc before printing. | |
85 return false; | |
86 | |
87 case StyleRule::Media: // If the media rule doesn't apply, we could avoid re
calc. | |
88 case StyleRule::FontFace: // If the fonts aren't in use, we could avoid reca
lc. | |
89 case StyleRule::Supports: // If we evaluated the supports-clause we could av
oid recalc. | |
90 case StyleRule::Viewport: // If the viewport doesn't match, we could avoid r
ecalcing. | |
91 case StyleRule::Keyframes: // If the animation doesn't match an element, we
could avoid recalc. | |
92 return true; | |
93 | |
94 // These should all be impossible to reach: | |
95 case StyleRule::Charset: | |
96 case StyleRule::Keyframe: | |
97 case StyleRule::Namespace: | |
98 case StyleRule::Style: | |
99 break; | |
100 } | |
101 ASSERT_NOT_REACHED(); | |
102 return true; | |
103 } | |
104 | |
105 void StyleSheetInvalidationAnalysis::analyzeStyleSheet(StyleSheetContents* style
SheetContents) | |
106 { | |
107 // Updating the style on the shadow DOM for image fallback content can bring
us here when imports | |
108 // are still getting loaded in the main document. Just need to exit early as
we will return here | |
109 // when the imports finish loading. | |
110 if (styleSheetContents->isLoading()) | |
111 return; | |
112 | |
113 // See if all rules on the sheet are scoped to some specific ids or classes. | |
114 // Then test if we actually have any of those in the tree at the moment. | |
115 const HeapVector<Member<StyleRuleImport>>& importRules = styleSheetContents-
>importRules(); | |
116 for (unsigned i = 0; i < importRules.size(); ++i) { | |
117 if (!importRules[i]->styleSheet()) | |
118 continue; | |
119 analyzeStyleSheet(importRules[i]->styleSheet()); | |
120 if (m_dirtiesAllStyle) | |
121 return; | |
122 } | |
123 | |
124 if (m_treeScope->rootNode().isShadowRoot()) | |
125 return; | |
126 | |
127 const HeapVector<Member<StyleRuleBase>>& rules = styleSheetContents->childRu
les(); | |
128 for (unsigned i = 0; i < rules.size(); i++) { | |
129 StyleRuleBase* rule = rules[i].get(); | |
130 if (!rule->isStyleRule()) { | |
131 if (ruleAdditionMightRequireDocumentStyleRecalc(rule)) { | |
132 m_dirtiesAllStyle = true; | |
133 return; | |
134 } | |
135 continue; | |
136 } | |
137 StyleRule* styleRule = toStyleRule(rule); | |
138 if (!determineSelectorScopes(styleRule->selectorList(), m_idScopes, m_cl
assScopes)) { | |
139 m_dirtiesAllStyle = true; | |
140 return; | |
141 } | |
142 } | |
143 } | |
144 | |
145 static bool elementMatchesSelectorScopes(const Element* element, const HashSet<S
tringImpl*>& idScopes, const HashSet<StringImpl*>& classScopes) | |
146 { | |
147 if (!idScopes.isEmpty() && element->hasID() && idScopes.contains(element->id
ForStyleResolution().impl())) | |
148 return true; | |
149 if (classScopes.isEmpty() || !element->hasClass()) | |
150 return false; | |
151 const SpaceSplitString& classNames = element->classNames(); | |
152 for (unsigned i = 0; i < classNames.size(); ++i) { | |
153 if (classScopes.contains(classNames[i].impl())) | |
154 return true; | |
155 } | |
156 return false; | |
157 } | |
158 | |
159 void StyleSheetInvalidationAnalysis::invalidateStyle() | |
160 { | |
161 ASSERT(!m_dirtiesAllStyle); | |
162 | |
163 if (m_treeScope->rootNode().isShadowRoot()) { | |
164 ContainerNode& shadowHost = toShadowRoot(m_treeScope->rootNode()).host()
; | |
165 shadowHost.setNeedsStyleRecalc(SubtreeStyleChange, StyleChangeReasonForT
racing::create(StyleChangeReason::StyleSheetChange)); | |
166 return; | |
167 } | |
168 | |
169 if (m_idScopes.isEmpty() && m_classScopes.isEmpty()) | |
170 return; | |
171 Element* element = ElementTraversal::firstWithin(m_treeScope->document()); | |
172 while (element) { | |
173 if (elementMatchesSelectorScopes(element, m_idScopes, m_classScopes)) { | |
174 element->setNeedsStyleRecalc(SubtreeStyleChange, StyleChangeReasonFo
rTracing::create(StyleChangeReason::StyleSheetChange)); | |
175 // The whole subtree is now invalidated, we can skip to the next sib
ling. | |
176 element = ElementTraversal::nextSkippingChildren(*element); | |
177 continue; | |
178 } | |
179 element = ElementTraversal::next(*element); | |
180 } | |
181 } | |
182 | |
183 } // namespace blink | |
OLD | NEW |