OLD | NEW |
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 r
ights reserved. | 6 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012 Apple Inc. All r
ights reserved. |
7 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.t
orchmobile.com/) | 7 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.t
orchmobile.com/) |
8 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) | 8 * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies) |
9 * Copyright (C) 2013 Google Inc. All rights reserved. | 9 * Copyright (C) 2013 Google Inc. All rights reserved. |
10 * | 10 * |
11 * This library is free software; you can redistribute it and/or | 11 * This library is free software; you can redistribute it and/or |
12 * modify it under the terms of the GNU Library General Public | 12 * modify it under the terms of the GNU Library General Public |
13 * License as published by the Free Software Foundation; either | 13 * License as published by the Free Software Foundation; either |
14 * version 2 of the License, or (at your option) any later version. | 14 * version 2 of the License, or (at your option) any later version. |
15 * | 15 * |
16 * This library is distributed in the hope that it will be useful, | 16 * This library is distributed in the hope that it will be useful, |
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
19 * Library General Public License for more details. | 19 * Library General Public License for more details. |
20 * | 20 * |
21 * You should have received a copy of the GNU Library General Public License | 21 * You should have received a copy of the GNU Library General Public License |
22 * along with this library; see the file COPYING.LIB. If not, write to | 22 * along with this library; see the file COPYING.LIB. If not, write to |
23 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 23 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
24 * Boston, MA 02110-1301, USA. | 24 * Boston, MA 02110-1301, USA. |
25 */ | 25 */ |
26 | 26 |
27 #include "core/dom/TreeScopeStyleSheetCollection.h" | 27 #include "core/dom/TreeScopeStyleSheetCollection.h" |
28 | 28 |
| 29 #include "core/css/ActiveStyleSheets.h" |
29 #include "core/css/CSSStyleSheet.h" | 30 #include "core/css/CSSStyleSheet.h" |
30 #include "core/css/StyleRuleImport.h" | 31 #include "core/css/StyleRuleImport.h" |
31 #include "core/css/StyleSheetContents.h" | 32 #include "core/css/StyleSheetContents.h" |
32 #include "core/css/invalidation/StyleSheetInvalidationAnalysis.h" | |
33 #include "core/css/resolver/StyleResolver.h" | 33 #include "core/css/resolver/StyleResolver.h" |
34 #include "core/dom/Element.h" | 34 #include "core/dom/Element.h" |
35 #include "core/dom/StyleEngine.h" | 35 #include "core/dom/StyleEngine.h" |
36 #include "core/html/HTMLLinkElement.h" | 36 #include "core/html/HTMLLinkElement.h" |
37 #include "core/html/HTMLStyleElement.h" | 37 #include "core/html/HTMLStyleElement.h" |
38 | 38 |
39 namespace blink { | 39 namespace blink { |
40 | 40 |
41 TreeScopeStyleSheetCollection::TreeScopeStyleSheetCollection(TreeScope& treeScop
e) | 41 TreeScopeStyleSheetCollection::TreeScopeStyleSheetCollection(TreeScope& treeScop
e) |
42 : m_treeScope(treeScope) | 42 : m_treeScope(treeScope) |
43 , m_hadActiveLoadingStylesheet(false) | |
44 { | 43 { |
45 } | 44 } |
46 | 45 |
47 void TreeScopeStyleSheetCollection::addStyleSheetCandidateNode(Node* node) | 46 void TreeScopeStyleSheetCollection::addStyleSheetCandidateNode(Node* node) |
48 { | 47 { |
49 if (!node->inShadowIncludingDocument()) | 48 if (node->inShadowIncludingDocument()) |
50 return; | 49 m_styleSheetCandidateNodes.add(node); |
51 | |
52 m_styleSheetCandidateNodes.add(node); | |
53 } | 50 } |
54 | 51 |
55 TreeScopeStyleSheetCollection::StyleResolverUpdateType TreeScopeStyleSheetCollec
tion::compareStyleSheets(const HeapVector<Member<CSSStyleSheet>>& oldStyleSheets
, const HeapVector<Member<CSSStyleSheet>>& newStylesheets, HeapVector<Member<Sty
leSheetContents>>& addedSheets) | 52 bool TreeScopeStyleSheetCollection::mediaQueryAffectingValueChanged() |
56 { | 53 { |
57 unsigned newStyleSheetCount = newStylesheets.size(); | 54 bool needsActiveStyleUpdate = false; |
58 unsigned oldStyleSheetCount = oldStyleSheets.size(); | |
59 DCHECK_GE(newStyleSheetCount, oldStyleSheetCount); | |
60 | |
61 if (!newStyleSheetCount) | |
62 return Reconstruct; | |
63 | |
64 unsigned newIndex = 0; | |
65 for (unsigned oldIndex = 0; oldIndex < oldStyleSheetCount; ++oldIndex) { | |
66 while (oldStyleSheets[oldIndex] != newStylesheets[newIndex]) { | |
67 addedSheets.append(newStylesheets[newIndex]->contents()); | |
68 if (++newIndex == newStyleSheetCount) | |
69 return Reconstruct; | |
70 } | |
71 if (++newIndex == newStyleSheetCount) | |
72 return Reconstruct; | |
73 } | |
74 bool hasInsertions = !addedSheets.isEmpty(); | |
75 while (newIndex < newStyleSheetCount) { | |
76 addedSheets.append(newStylesheets[newIndex]->contents()); | |
77 ++newIndex; | |
78 } | |
79 // If all new sheets were added at the end of the list we can just add them
to existing StyleResolver. | |
80 // If there were insertions we need to re-add all the stylesheets so rules a
re ordered correctly. | |
81 return hasInsertions ? Reset : Additive; | |
82 } | |
83 | |
84 bool TreeScopeStyleSheetCollection::activeLoadingStyleSheetLoaded(const HeapVect
or<Member<CSSStyleSheet>>& newStyleSheets) | |
85 { | |
86 // StyleSheets of <style> elements that @import stylesheets are active but l
oading. We need to trigger a full recalc when such loads are done. | |
87 bool hasActiveLoadingStylesheet = false; | |
88 unsigned newStylesheetCount = newStyleSheets.size(); | |
89 for (unsigned i = 0; i < newStylesheetCount; ++i) { | |
90 if (newStyleSheets[i]->isLoading()) | |
91 hasActiveLoadingStylesheet = true; | |
92 } | |
93 if (m_hadActiveLoadingStylesheet && !hasActiveLoadingStylesheet) { | |
94 m_hadActiveLoadingStylesheet = false; | |
95 return true; | |
96 } | |
97 m_hadActiveLoadingStylesheet = hasActiveLoadingStylesheet; | |
98 return false; | |
99 } | |
100 | |
101 static bool findFontFaceRulesFromStyleSheetContents(const HeapVector<Member<Styl
eSheetContents>>& sheets, HeapVector<Member<const StyleRuleFontFace>>& fontFaceR
ules) | |
102 { | |
103 bool hasFontFaceRule = false; | |
104 | |
105 for (unsigned i = 0; i < sheets.size(); ++i) { | |
106 DCHECK(sheets[i]); | |
107 if (sheets[i]->hasFontFaceRule()) { | |
108 // FIXME: We don't need this for styles in shadow tree. | |
109 sheets[i]->findFontFaceRules(fontFaceRules); | |
110 hasFontFaceRule = true; | |
111 } | |
112 } | |
113 return hasFontFaceRule; | |
114 } | |
115 | |
116 void TreeScopeStyleSheetCollection::analyzeStyleSheetChange(StyleResolverUpdateM
ode updateMode, const StyleSheetCollection& newCollection, StyleSheetChange& cha
nge) | |
117 { | |
118 if (activeLoadingStyleSheetLoaded(newCollection.activeAuthorStyleSheets())) | |
119 return; | |
120 | |
121 if (updateMode != AnalyzedStyleUpdate) | |
122 return; | |
123 | |
124 // Find out which stylesheets are new. | |
125 HeapVector<Member<StyleSheetContents>> addedSheets; | |
126 if (m_activeAuthorStyleSheets.size() <= newCollection.activeAuthorStyleSheet
s().size()) { | |
127 change.styleResolverUpdateType = compareStyleSheets(m_activeAuthorStyleS
heets, newCollection.activeAuthorStyleSheets(), addedSheets); | |
128 } else { | |
129 StyleResolverUpdateType updateType = compareStyleSheets(newCollection.ac
tiveAuthorStyleSheets(), m_activeAuthorStyleSheets, addedSheets); | |
130 if (updateType != Additive) { | |
131 change.styleResolverUpdateType = updateType; | |
132 } else { | |
133 change.styleResolverUpdateType = Reset; | |
134 // If @font-face is removed, needs full style recalc. | |
135 if (findFontFaceRulesFromStyleSheetContents(addedSheets, change.font
FaceRulesToRemove)) | |
136 return; | |
137 } | |
138 } | |
139 | |
140 // FIXME: If styleResolverUpdateType is Reconstruct, we should return early
here since | |
141 // we need to recalc the whole document. It's wrong to use StyleSheetInvalid
ationAnalysis since | |
142 // it only looks at the addedSheets. | |
143 | |
144 // No point in doing the analysis work if we're just going to recalc the who
le document anyways. | |
145 // This needs to be done after the compareStyleSheets calls above to ensure
we don't throw away | |
146 // the StyleResolver if we don't need to. | |
147 if (document().hasPendingForcedStyleRecalc()) | |
148 return; | |
149 | |
150 // If we are already parsing the body and so may have significant amount of
elements, put some effort into trying to avoid style recalcs. | |
151 if (!document().body() || document().hasNodesWithPlaceholderStyle()) | |
152 return; | |
153 StyleSheetInvalidationAnalysis invalidationAnalysis(*m_treeScope, addedSheet
s); | |
154 if (invalidationAnalysis.dirtiesAllStyle()) | |
155 return; | |
156 invalidationAnalysis.invalidateStyle(); | |
157 change.requiresFullStyleRecalc = false; | |
158 return; | |
159 } | |
160 | |
161 void TreeScopeStyleSheetCollection::clearMediaQueryRuleSetStyleSheets() | |
162 { | |
163 for (size_t i = 0; i < m_activeAuthorStyleSheets.size(); ++i) { | 55 for (size_t i = 0; i < m_activeAuthorStyleSheets.size(); ++i) { |
164 StyleSheetContents* contents = m_activeAuthorStyleSheets[i]->contents(); | 56 if (m_activeAuthorStyleSheets[i].first->mediaQueries()) |
| 57 needsActiveStyleUpdate = true; |
| 58 StyleSheetContents* contents = m_activeAuthorStyleSheets[i].first->conte
nts(); |
165 if (contents->hasMediaQueries()) | 59 if (contents->hasMediaQueries()) |
166 contents->clearRuleSet(); | 60 contents->clearRuleSet(); |
167 } | 61 } |
| 62 return needsActiveStyleUpdate; |
| 63 } |
| 64 |
| 65 void TreeScopeStyleSheetCollection::applyActiveStyleSheetChanges(StyleSheetColle
ction& newCollection) |
| 66 { |
| 67 applyRuleSetChanges(document().styleEngine(), treeScope(), activeAuthorStyle
Sheets(), newCollection.activeAuthorStyleSheets()); |
| 68 newCollection.swap(*this); |
168 } | 69 } |
169 | 70 |
170 DEFINE_TRACE(TreeScopeStyleSheetCollection) | 71 DEFINE_TRACE(TreeScopeStyleSheetCollection) |
171 { | 72 { |
172 visitor->trace(m_treeScope); | 73 visitor->trace(m_treeScope); |
173 visitor->trace(m_styleSheetCandidateNodes); | 74 visitor->trace(m_styleSheetCandidateNodes); |
174 StyleSheetCollection::trace(visitor); | 75 StyleSheetCollection::trace(visitor); |
175 } | 76 } |
176 | 77 |
177 } // namespace blink | 78 } // namespace blink |
OLD | NEW |