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

Side by Side Diff: third_party/WebKit/Source/core/dom/TreeScopeStyleSheetCollection.cpp

Issue 2572473006: Revert of Collect active stylesheets and and apply asynchronously. (Closed)
Patch Set: Created 4 years 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
1 /* 1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org) 3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller (mueller@kde.org) 4 * (C) 2001 Dirk Mueller (mueller@kde.org)
5 * (C) 2006 Alexey Proskuryakov (ap@webkit.org) 5 * (C) 2006 Alexey Proskuryakov (ap@webkit.org)
6 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012 Apple Inc. All 6 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012 Apple Inc. All
7 * rights reserved. 7 * rights reserved.
8 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. 8 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved.
9 * (http://www.torchmobile.com/) 9 * (http://www.torchmobile.com/)
10 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) 10 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
(...skipping 10 matching lines...) Expand all
21 * Library General Public License for more details. 21 * Library General Public License for more details.
22 * 22 *
23 * You should have received a copy of the GNU Library General Public License 23 * You should have received a copy of the GNU Library General Public License
24 * along with this library; see the file COPYING.LIB. If not, write to 24 * along with this library; see the file COPYING.LIB. If not, write to
25 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 25 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 * Boston, MA 02110-1301, USA. 26 * Boston, MA 02110-1301, USA.
27 */ 27 */
28 28
29 #include "core/dom/TreeScopeStyleSheetCollection.h" 29 #include "core/dom/TreeScopeStyleSheetCollection.h"
30 30
31 #include "core/css/ActiveStyleSheets.h"
32 #include "core/css/CSSStyleSheet.h" 31 #include "core/css/CSSStyleSheet.h"
33 #include "core/css/StyleRuleImport.h" 32 #include "core/css/StyleRuleImport.h"
34 #include "core/css/StyleSheetContents.h" 33 #include "core/css/StyleSheetContents.h"
34 #include "core/css/invalidation/StyleSheetInvalidationAnalysis.h"
35 #include "core/css/resolver/StyleResolver.h" 35 #include "core/css/resolver/StyleResolver.h"
36 #include "core/dom/Element.h" 36 #include "core/dom/Element.h"
37 #include "core/dom/StyleEngine.h" 37 #include "core/dom/StyleEngine.h"
38 #include "core/html/HTMLLinkElement.h" 38 #include "core/html/HTMLLinkElement.h"
39 #include "core/html/HTMLStyleElement.h" 39 #include "core/html/HTMLStyleElement.h"
40 40
41 namespace blink { 41 namespace blink {
42 42
43 TreeScopeStyleSheetCollection::TreeScopeStyleSheetCollection( 43 TreeScopeStyleSheetCollection::TreeScopeStyleSheetCollection(
44 TreeScope& treeScope) 44 TreeScope& treeScope)
45 : m_treeScope(treeScope) {} 45 : m_treeScope(treeScope), m_hadActiveLoadingStylesheet(false) {}
46 46
47 void TreeScopeStyleSheetCollection::addStyleSheetCandidateNode(Node& node) { 47 void TreeScopeStyleSheetCollection::addStyleSheetCandidateNode(Node& node) {
48 if (node.isConnected()) 48 if (!node.isConnected())
49 m_styleSheetCandidateNodes.add(&node); 49 return;
50
51 m_styleSheetCandidateNodes.add(&node);
50 } 52 }
51 53
52 bool TreeScopeStyleSheetCollection::mediaQueryAffectingValueChanged() { 54 TreeScopeStyleSheetCollection::StyleResolverUpdateType
53 bool needsActiveStyleUpdate = false; 55 TreeScopeStyleSheetCollection::compareStyleSheets(
54 for (const auto& activeSheet : m_activeAuthorStyleSheets) { 56 const HeapVector<Member<CSSStyleSheet>>& oldStyleSheets,
55 if (activeSheet.first->mediaQueries()) 57 const HeapVector<Member<CSSStyleSheet>>& newStylesheets,
56 needsActiveStyleUpdate = true; 58 HeapVector<Member<StyleSheetContents>>& addedSheets) {
57 StyleSheetContents* contents = activeSheet.first->contents(); 59 unsigned newStyleSheetCount = newStylesheets.size();
60 unsigned oldStyleSheetCount = oldStyleSheets.size();
61 DCHECK_GE(newStyleSheetCount, oldStyleSheetCount);
62
63 if (!newStyleSheetCount)
64 return Reconstruct;
65
66 unsigned newIndex = 0;
67 for (unsigned oldIndex = 0; oldIndex < oldStyleSheetCount; ++oldIndex) {
68 while (oldStyleSheets[oldIndex] != newStylesheets[newIndex]) {
69 addedSheets.append(newStylesheets[newIndex]->contents());
70 if (++newIndex == newStyleSheetCount)
71 return Reconstruct;
72 }
73 if (++newIndex == newStyleSheetCount)
74 return Reconstruct;
75 }
76 bool hasInsertions = !addedSheets.isEmpty();
77 while (newIndex < newStyleSheetCount) {
78 addedSheets.append(newStylesheets[newIndex]->contents());
79 ++newIndex;
80 }
81 // If all new sheets were added at the end of the list we can just add them to
82 // existing StyleResolver. If there were insertions we need to re-add all the
83 // stylesheets so rules are ordered correctly.
84 return hasInsertions ? Reset : Additive;
85 }
86
87 bool TreeScopeStyleSheetCollection::activeLoadingStyleSheetLoaded(
88 const HeapVector<Member<CSSStyleSheet>>& newStyleSheets) {
89 // StyleSheets of <style> elements that @import stylesheets are active but
90 // loading. We need to trigger a full recalc when such loads are done.
91 bool hasActiveLoadingStylesheet = false;
92 for (const auto& sheet : newStyleSheets) {
93 if (sheet->isLoading())
94 hasActiveLoadingStylesheet = true;
95 }
96 if (m_hadActiveLoadingStylesheet && !hasActiveLoadingStylesheet) {
97 m_hadActiveLoadingStylesheet = false;
98 return true;
99 }
100 m_hadActiveLoadingStylesheet = hasActiveLoadingStylesheet;
101 return false;
102 }
103
104 static bool findFontFaceRulesFromStyleSheetContents(
105 const HeapVector<Member<StyleSheetContents>>& sheets,
106 HeapVector<Member<const StyleRuleFontFace>>& fontFaceRules) {
107 bool hasFontFaceRule = false;
108
109 for (const auto& sheet : sheets) {
110 DCHECK(sheet);
111 if (sheet->hasFontFaceRule()) {
112 // FIXME: We don't need this for styles in shadow tree.
113 sheet->findFontFaceRules(fontFaceRules);
114 hasFontFaceRule = true;
115 }
116 }
117 return hasFontFaceRule;
118 }
119
120 void TreeScopeStyleSheetCollection::analyzeStyleSheetChange(
121 StyleResolverUpdateMode updateMode,
122 const HeapVector<Member<CSSStyleSheet>>& newActiveAuthorStyleSheets,
123 StyleSheetChange& change) {
124 if (activeLoadingStyleSheetLoaded(newActiveAuthorStyleSheets))
125 return;
126
127 if (updateMode != AnalyzedStyleUpdate)
128 return;
129
130 // Find out which stylesheets are new.
131 HeapVector<Member<StyleSheetContents>> addedSheets;
132 if (m_activeAuthorStyleSheets.size() <= newActiveAuthorStyleSheets.size()) {
133 change.styleResolverUpdateType = compareStyleSheets(
134 m_activeAuthorStyleSheets, newActiveAuthorStyleSheets, addedSheets);
135 } else {
136 StyleResolverUpdateType updateType = compareStyleSheets(
137 newActiveAuthorStyleSheets, m_activeAuthorStyleSheets, addedSheets);
138 if (updateType != Additive) {
139 change.styleResolverUpdateType = updateType;
140 } else {
141 change.styleResolverUpdateType = Reset;
142 // If @font-face is removed, needs full style recalc.
143 if (findFontFaceRulesFromStyleSheetContents(addedSheets,
144 change.fontFaceRulesToRemove))
145 return;
146 }
147 }
148
149 // FIXME: If styleResolverUpdateType is Reconstruct, we should return early
150 // here since we need to recalc the whole document. It's wrong to use
151 // StyleSheetInvalidationAnalysis since it only looks at the addedSheets.
152
153 // No point in doing the analysis work if we're just going to recalc the whole
154 // document anyways. This needs to be done after the compareStyleSheets calls
155 // above to ensure we don't throw away the StyleResolver if we don't need to.
156 if (document().hasPendingForcedStyleRecalc())
157 return;
158
159 // If we are already parsing the body and so may have significant amount of
160 // elements, put some effort into trying to avoid style recalcs.
161 if (!document().body() || document().hasNodesWithPlaceholderStyle())
162 return;
163 StyleSheetInvalidationAnalysis invalidationAnalysis(*m_treeScope,
164 addedSheets);
165 if (invalidationAnalysis.dirtiesAllStyle())
166 return;
167 invalidationAnalysis.invalidateStyle();
168 change.requiresFullStyleRecalc = false;
169 return;
170 }
171
172 void TreeScopeStyleSheetCollection::clearMediaQueryRuleSetStyleSheets() {
173 for (const auto& sheet : m_activeAuthorStyleSheets) {
174 StyleSheetContents* contents = sheet->contents();
58 if (contents->hasMediaQueries()) 175 if (contents->hasMediaQueries())
59 contents->clearRuleSet(); 176 contents->clearRuleSet();
60 } 177 }
61 return needsActiveStyleUpdate;
62 }
63
64 void TreeScopeStyleSheetCollection::applyActiveStyleSheetChanges(
65 StyleSheetCollection& newCollection) {
66 document().styleEngine().applyRuleSetChanges(
67 treeScope(), activeAuthorStyleSheets(),
68 newCollection.activeAuthorStyleSheets());
69 newCollection.swap(*this);
70 } 178 }
71 179
72 DEFINE_TRACE(TreeScopeStyleSheetCollection) { 180 DEFINE_TRACE(TreeScopeStyleSheetCollection) {
73 visitor->trace(m_treeScope); 181 visitor->trace(m_treeScope);
74 visitor->trace(m_styleSheetCandidateNodes); 182 visitor->trace(m_styleSheetCandidateNodes);
75 StyleSheetCollection::trace(visitor); 183 StyleSheetCollection::trace(visitor);
76 } 184 }
77 185
78 } // namespace blink 186 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698