Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(29)

Side by Side Diff: Source/core/css/StyleInvalidationAnalysis.cpp

Issue 199633020: Move all style invalidation code into the css/invalidation directory. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Merge. Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "config.h"
27 #include "core/css/StyleInvalidationAnalysis.h"
28
29 #include "core/css/CSSSelectorList.h"
30 #include "core/css/StyleRuleImport.h"
31 #include "core/css/StyleSheetContents.h"
32 #include "core/dom/ContainerNode.h"
33 #include "core/dom/Document.h"
34 #include "core/dom/ElementTraversal.h"
35 #include "core/dom/shadow/ShadowRoot.h"
36 #include "core/html/HTMLStyleElement.h"
37
38 namespace WebCore {
39
40 StyleInvalidationAnalysis::StyleInvalidationAnalysis(const WillBeHeapVector<RawP trWillBeMember<StyleSheetContents> >& sheets)
41 : m_dirtiesAllStyle(false)
42 {
43 for (unsigned i = 0; i < sheets.size() && !m_dirtiesAllStyle; ++i)
44 analyzeStyleSheet(sheets[i]);
45 }
46
47 static bool determineSelectorScopes(const CSSSelectorList& selectorList, HashSet <StringImpl*>& idScopes, HashSet<StringImpl*>& classScopes)
48 {
49 for (const CSSSelector* selector = selectorList.first(); selector; selector = CSSSelectorList::next(*selector)) {
50 const CSSSelector* scopeSelector = 0;
51 // This picks the widest scope, not the narrowest, to minimize the numbe r of found scopes.
52 for (const CSSSelector* current = selector; current; current = current-> tagHistory()) {
53 // Prefer ids over classes.
54 if (current->m_match == CSSSelector::Id)
55 scopeSelector = current;
56 else if (current->m_match == CSSSelector::Class && (!scopeSelector | | scopeSelector->m_match != CSSSelector::Id))
57 scopeSelector = current;
58 CSSSelector::Relation relation = current->relation();
59 // FIXME: it would be better to use setNeedsStyleRecalc for all shad ow hosts matching
60 // scopeSelector. Currently requests full style recalc.
61 if (relation == CSSSelector::ShadowDeep || relation == CSSSelector:: Shadow)
62 return false;
63 if (relation != CSSSelector::Descendant && relation != CSSSelector:: Child && relation != CSSSelector::SubSelector)
64 break;
65 }
66 if (!scopeSelector)
67 return false;
68 ASSERT(scopeSelector->m_match == CSSSelector::Class || scopeSelector->m_ match == CSSSelector::Id);
69 if (scopeSelector->m_match == CSSSelector::Id)
70 idScopes.add(scopeSelector->value().impl());
71 else
72 classScopes.add(scopeSelector->value().impl());
73 }
74 return true;
75 }
76
77 static bool hasDistributedRule(StyleSheetContents* styleSheetContents)
78 {
79 const WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase> >& rules = styleShe etContents->childRules();
80 for (unsigned i = 0; i < rules.size(); i++) {
81 const StyleRuleBase* rule = rules[i].get();
82 if (!rule->isStyleRule())
83 continue;
84
85 const StyleRule* styleRule = toStyleRule(rule);
86 const CSSSelectorList& selectorList = styleRule->selectorList();
87 for (size_t selectorIndex = 0; selectorIndex != kNotFound; selectorIndex = selectorList.indexOfNextSelectorAfter(selectorIndex)) {
88 if (selectorList.hasShadowDistributedAt(selectorIndex))
89 return true;
90 }
91 }
92 return false;
93 }
94
95 static Node* determineScopingNodeForStyleScoped(HTMLStyleElement* ownerElement, StyleSheetContents* styleSheetContents)
96 {
97 ASSERT(ownerElement && ownerElement->isRegisteredAsScoped());
98
99 if (ownerElement->isInShadowTree()) {
100 if (hasDistributedRule(styleSheetContents)) {
101 ContainerNode* scope = ownerElement;
102 do {
103 scope = scope->containingShadowRoot()->shadowHost();
104 } while (scope->isInShadowTree());
105
106 return scope;
107 }
108 if (ownerElement->isRegisteredAsScoped())
109 return ownerElement->containingShadowRoot()->shadowHost();
110 }
111
112 return ownerElement->isRegisteredInShadowRoot() ? ownerElement->containingSh adowRoot()->shadowHost() : ownerElement->parentNode();
113 }
114
115 static bool ruleAdditionMightRequireDocumentStyleRecalc(StyleRuleBase* rule)
116 {
117 // This funciton is conservative. We only return false when we know that
118 // the added @rule can't require style recalcs.
119 switch (rule->type()) {
120 case StyleRule::Import: // Whatever we import should do its own analysis, we don't need to invalidate the document here!
121 case StyleRule::Keyframes: // Keyframes never cause style invalidations and are handled during sheet insertion.
122 case StyleRule::Page: // Page rules apply only during printing, we force a f ull-recalc before printing.
123 return false;
124
125 case StyleRule::Media: // If the media rule doesn't apply, we could avoid re calc.
126 case StyleRule::FontFace: // If the fonts aren't in use, we could avoid reca lc.
127 case StyleRule::Supports: // If we evaluated the supports-clause we could av oid recalc.
128 case StyleRule::Viewport: // If the viewport doesn't match, we could avoid r ecalcing.
129 // FIXME: Unclear if any of the rest need to cause style recalc:
130 case StyleRule::Filter:
131 return true;
132
133 // These should all be impossible to reach:
134 case StyleRule::Unknown:
135 case StyleRule::Charset:
136 case StyleRule::Keyframe:
137 case StyleRule::Style:
138 break;
139 }
140 ASSERT_NOT_REACHED();
141 return true;
142 }
143
144 void StyleInvalidationAnalysis::analyzeStyleSheet(StyleSheetContents* styleSheet Contents)
145 {
146 ASSERT(!styleSheetContents->isLoading());
147
148 // See if all rules on the sheet are scoped to some specific ids or classes.
149 // Then test if we actually have any of those in the tree at the moment.
150 const WillBeHeapVector<RefPtrWillBeMember<StyleRuleImport> >& importRules = styleSheetContents->importRules();
151 for (unsigned i = 0; i < importRules.size(); ++i) {
152 if (!importRules[i]->styleSheet())
153 continue;
154 analyzeStyleSheet(importRules[i]->styleSheet());
155 if (m_dirtiesAllStyle)
156 return;
157 }
158 if (styleSheetContents->hasSingleOwnerNode()) {
159 Node* ownerNode = styleSheetContents->singleOwnerNode();
160 if (isHTMLStyleElement(ownerNode) && toHTMLStyleElement(*ownerNode).isRe gisteredAsScoped()) {
161 m_scopingNodes.append(determineScopingNodeForStyleScoped(toHTMLStyle Element(ownerNode), styleSheetContents));
162 return;
163 }
164 }
165
166 const WillBeHeapVector<RefPtrWillBeMember<StyleRuleBase> >& rules = styleShe etContents->childRules();
167 for (unsigned i = 0; i < rules.size(); i++) {
168 StyleRuleBase* rule = rules[i].get();
169 if (!rule->isStyleRule()) {
170 if (ruleAdditionMightRequireDocumentStyleRecalc(rule)) {
171 m_dirtiesAllStyle = true;
172 return;
173 }
174 continue;
175 }
176 StyleRule* styleRule = toStyleRule(rule);
177 if (!determineSelectorScopes(styleRule->selectorList(), m_idScopes, m_cl assScopes)) {
178 m_dirtiesAllStyle = true;
179 return;
180 }
181 }
182 }
183
184 static bool elementMatchesSelectorScopes(const Element* element, const HashSet<S tringImpl*>& idScopes, const HashSet<StringImpl*>& classScopes)
185 {
186 if (!idScopes.isEmpty() && element->hasID() && idScopes.contains(element->id ForStyleResolution().impl()))
187 return true;
188 if (classScopes.isEmpty() || !element->hasClass())
189 return false;
190 const SpaceSplitString& classNames = element->classNames();
191 for (unsigned i = 0; i < classNames.size(); ++i) {
192 if (classScopes.contains(classNames[i].impl()))
193 return true;
194 }
195 return false;
196 }
197
198 void StyleInvalidationAnalysis::invalidateStyle(Document& document)
199 {
200 ASSERT(!m_dirtiesAllStyle);
201
202 if (!m_scopingNodes.isEmpty()) {
203 for (unsigned i = 0; i < m_scopingNodes.size(); ++i)
204 m_scopingNodes.at(i)->setNeedsStyleRecalc(SubtreeStyleChange);
205 }
206
207 if (m_idScopes.isEmpty() && m_classScopes.isEmpty())
208 return;
209 Element* element = ElementTraversal::firstWithin(document);
210 while (element) {
211 if (elementMatchesSelectorScopes(element, m_idScopes, m_classScopes)) {
212 element->setNeedsStyleRecalc(SubtreeStyleChange);
213 // The whole subtree is now invalidated, we can skip to the next sib ling.
214 element = ElementTraversal::nextSkippingChildren(*element);
215 continue;
216 }
217 element = ElementTraversal::next(*element);
218 }
219 }
220
221 }
OLDNEW
« no previous file with comments | « Source/core/css/StyleInvalidationAnalysis.h ('k') | Source/core/css/analyzer/DescendantInvalidationSet.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698