2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
22 #ifndef StyleResolver_h
23 #define StyleResolver_h
25 #include "core/animation/KeyframeEffectModel.h"
26 #include "core/css/CSSFontSelectorClient.h"
27 #include "core/css/PseudoStyleRequest.h"
28 #include "core/css/RuleFeature.h"
29 #include "core/css/RuleSet.h"
30 #include "core/css/SelectorChecker.h"
31 #include "core/css/SelectorFilter.h"
32 #include "core/css/SiblingTraversalStrategies.h"
33 #include "core/css/TreeBoundaryCrossingRules.h"
34 #include "core/css/resolver/MatchedPropertiesCache.h"
35 #include "core/css/resolver/ScopedStyleResolver.h"
36 #include "core/css/resolver/ScopedStyleTree.h"
37 #include "core/css/resolver/StyleBuilder.h"
38 #include "core/css/resolver/StyleResolverState.h"
39 #include "core/css/resolver/StyleResourceLoader.h"
40 #include "wtf/Deque.h"
41 #include "wtf/HashMap.h"
42 #include "wtf/HashSet.h"
43 #include "wtf/ListHashSet.h"
44 #include "wtf/RefPtr.h"
45 #include "wtf/Vector.h"
49 class CSSAnimationUpdate;
50 class CSSFontSelector;
57 class DocumentTimeline;
59 class ElementRuleCollector;
62 class MediaQueryEvaluator;
63 class MediaQueryResult;
67 class StylePropertySet;
68 class StyleResolverStats;
70 class StyleRuleKeyframes;
72 class ViewportStyleResolver;
76 enum StyleSharingBehavior {
81 // MatchOnlyUserAgentRules is used in media queries, where relative units
82 // are interpreted according to the document root element style, and styled only
83 // from the User Agent Stylesheet rules.
84 enum RuleMatchingBehavior {
86 MatchAllRulesExcludingSMIL,
87 MatchOnlyUserAgentRules,
90 const unsigned styleSharingListSize = 40;
91 typedef WTF::Deque<Element*, styleSharingListSize> StyleSharingList;
93 struct CSSPropertyValue {
94 CSSPropertyValue(CSSPropertyID property, CSSValue* value)
95 : property(property), value(value) { }
96 // Stores value=propertySet.getPropertyCSSValue(id).get().
97 CSSPropertyValue(CSSPropertyID, const StylePropertySet&);
98 CSSPropertyID property;
102 // This class selects a RenderStyle for a given element based on a collection of stylesheets.
103 class StyleResolver FINAL : public CSSFontSelectorClient {
104 WTF_MAKE_NONCOPYABLE(StyleResolver); WTF_MAKE_FAST_ALLOCATED;
106 explicit StyleResolver(Document&);
107 virtual ~StyleResolver();
109 // FIXME: StyleResolver should not be keeping tree-walk state.
110 // These should move to some global tree-walk state, or should be contained in a
111 // TreeWalkContext or similar which is passed in to StyleResolver methods when available.
112 // Using these during tree walk will allow style selector to optimize child and descendant selector lookups.
113 void pushParentElement(Element&);
114 void popParentElement(Element&);
115 void pushParentShadowRoot(const ShadowRoot&);
116 void popParentShadowRoot(const ShadowRoot&);
118 PassRefPtr<RenderStyle> styleForElement(Element*, RenderStyle* parentStyle = 0, StyleSharingBehavior = AllowStyleSharing,
119 RuleMatchingBehavior = MatchAllRules);
121 PassRefPtr<RenderStyle> styleForKeyframe(Element*, const RenderStyle&, RenderStyle* parentStyle, const StyleKeyframe*, const AtomicString& animationName);
122 static PassRefPtr<KeyframeEffectModel> createKeyframeEffectModel(Element&, const Vector<RefPtr<MutableStylePropertySet> >&, KeyframeEffectModel::KeyframeVector&);
124 PassRefPtr<RenderStyle> pseudoStyleForElement(Element*, const PseudoStyleRequest&, RenderStyle* parentStyle);
126 PassRefPtr<RenderStyle> styleForPage(int pageIndex);
127 PassRefPtr<RenderStyle> defaultStyleForElement();
128 PassRefPtr<RenderStyle> styleForText(Text*);
130 static PassRefPtr<RenderStyle> styleForDocument(Document&, CSSFontSelector* = 0);
132 // FIXME: This only has 5 callers and should be removed. Callers should be explicit about
133 // their dependency on Document* instead of grabbing one through StyleResolver.
134 Document& document() { return m_document; }
136 // FIXME: It could be better to call appendAuthorStyleSheets() directly after we factor StyleResolver further.
137 // https://bugs.webkit.org/show_bug.cgi?id=108890
138 void appendAuthorStyleSheets(unsigned firstNew, const Vector<RefPtr<CSSStyleSheet> >&);
139 void resetAuthorStyle(const ContainerNode*);
140 void finishAppendAuthorStyleSheets();
142 TreeBoundaryCrossingRules& treeBoundaryCrossingRules() { return m_treeBoundaryCrossingRules; }
143 void processScopedRules(const RuleSet& authorRules, const KURL&, ContainerNode* scope = 0);
145 void lazyAppendAuthorStyleSheets(unsigned firstNew, const Vector<RefPtr<CSSStyleSheet> >&);
146 void removePendingAuthorStyleSheets(const Vector<RefPtr<CSSStyleSheet> >&);
147 void appendPendingAuthorStyleSheets();
148 bool hasPendingAuthorStyleSheets() const { return m_pendingStyleSheets.size() > 0 || m_needCollectFeatures; }
150 SelectorFilter& selectorFilter() { return m_selectorFilter; }
152 void setBuildScopedStyleTreeInDocumentOrder(bool enabled) { m_styleTree.setBuildInDocumentOrder(enabled); }
153 bool buildScopedStyleTreeInDocumentOrder() const { return m_styleTree.buildInDocumentOrder(); }
154 bool styleTreeHasOnlyScopedResolverForDocument() const { return m_styleTree.hasOnlyScopedResolverForDocument(); }
155 ScopedStyleResolver* styleTreeScopedStyleResolverForDocument() const { return m_styleTree.scopedStyleResolverForDocument(); }
157 ScopedStyleResolver* ensureScopedStyleResolver(ContainerNode* scope)
160 return m_styleTree.ensureScopedStyleResolver(*scope);
163 void styleTreeResolveScopedKeyframesRules(const Element* element, Vector<ScopedStyleResolver*, 8>& resolvers)
165 m_styleTree.resolveScopedKeyframesRules(element, resolvers);
168 // These methods will give back the set of rules that matched for a given element (or a pseudo-element).
170 UAAndUserCSSRules = 1 << 1,
171 AuthorCSSRules = 1 << 2,
172 EmptyCSSRules = 1 << 3,
173 CrossOriginCSSRules = 1 << 4,
174 AllButEmptyCSSRules = UAAndUserCSSRules | AuthorCSSRules | CrossOriginCSSRules,
175 AllCSSRules = AllButEmptyCSSRules | EmptyCSSRules,
177 PassRefPtr<CSSRuleList> cssRulesForElement(Element*, unsigned rulesToInclude = AllButEmptyCSSRules);
178 PassRefPtr<CSSRuleList> pseudoCSSRulesForElement(Element*, PseudoId, unsigned rulesToInclude = AllButEmptyCSSRules);
179 PassRefPtr<StyleRuleList> styleRulesForElement(Element*, unsigned rulesToInclude);
181 // |properties| is an array with |count| elements.
182 void applyPropertiesToStyle(const CSSPropertyValue* properties, size_t count, RenderStyle*);
184 ViewportStyleResolver* viewportStyleResolver() { return m_viewportStyleResolver.get(); }
186 void addMediaQueryResults(const MediaQueryResultList&);
187 MediaQueryResultList* viewportDependentMediaQueryResults() { return &m_viewportDependentMediaQueryResults; }
188 bool hasViewportDependentMediaQueries() const { return !m_viewportDependentMediaQueryResults.isEmpty(); }
189 bool mediaQueryAffectedByViewportChange() const;
191 // FIXME: Rename to reflect the purpose, like didChangeFontSize or something.
192 void invalidateMatchedPropertiesCache();
194 void notifyResizeForViewportUnits();
196 // Exposed for RenderStyle::isStyleAvilable().
197 static RenderStyle* styleNotYetAvailable() { return s_styleNotYetAvailable; }
199 RuleFeatureSet& ensureRuleFeatureSet()
201 if (hasPendingAuthorStyleSheets())
202 appendPendingAuthorStyleSheets();
206 StyleSharingList& styleSharingList() { return m_styleSharingList; }
208 bool hasRulesForId(const AtomicString&) const;
210 void addToStyleSharingList(Element&);
211 void clearStyleSharingList();
213 StyleResolverStats* stats() { return m_styleResolverStats.get(); }
214 StyleResolverStats* statsTotals() { return m_styleResolverStatsTotals.get(); }
215 enum StatsReportType { ReportDefaultStats, ReportSlowStats };
216 void enableStats(StatsReportType = ReportDefaultStats);
220 unsigned accessCount() const { return m_accessCount; }
221 void didAccess() { ++m_accessCount; }
223 PassRefPtr<PseudoElement> createPseudoElementIfNeeded(Element& parent, PseudoId);
226 // CSSFontSelectorClient implementation.
227 virtual void fontsNeedUpdate(CSSFontSelector*) OVERRIDE;
230 void initWatchedSelectorRules(const Vector<RefPtr<StyleRule> >& watchedSelectors);
232 void addTreeBoundaryCrossingRules(const Vector<MinimalRuleData>&, ContainerNode* scope);
234 // FIXME: This should probably go away, folded into FontBuilder.
235 void updateFont(StyleResolverState&);
237 void appendCSSStyleSheet(CSSStyleSheet*);
239 void collectPseudoRulesForElement(Element*, ElementRuleCollector&, PseudoId, unsigned rulesToInclude);
240 void matchUARules(ElementRuleCollector&, RuleSet*);
241 void matchAuthorRules(Element*, ElementRuleCollector&, bool includeEmptyRules);
242 void matchAuthorRulesForShadowHost(Element*, ElementRuleCollector&, bool includeEmptyRules, Vector<ScopedStyleResolver*, 8>& resolvers, Vector<ScopedStyleResolver*, 8>& resolversInShadowTree);
243 void matchAllRules(StyleResolverState&, ElementRuleCollector&, bool includeSMILProperties);
244 void matchUARules(ElementRuleCollector&);
245 // FIXME: watched selectors should be implemented using injected author stylesheets: http://crbug.com/316960
246 void matchWatchSelectorRules(ElementRuleCollector&);
247 void collectFeatures();
248 void collectTreeBoundaryCrossingRules(Element*, ElementRuleCollector&, bool includeEmptyRules);
249 void resetRuleFeatures();
251 bool fastRejectSelector(const RuleData&) const;
253 void applyMatchedProperties(StyleResolverState&, const MatchResult&);
254 void applyAnimatedProperties(StyleResolverState&, Element* animatingElement);
256 enum StyleApplicationPass {
258 HighPriorityProperties,
259 LowPriorityProperties
261 template <StyleResolver::StyleApplicationPass pass>
262 static inline bool isPropertyForPass(CSSPropertyID);
263 template <StyleApplicationPass pass>
264 void applyMatchedProperties(StyleResolverState&, const MatchResult&, bool important, int startIndex, int endIndex, bool inheritedOnly);
265 template <StyleApplicationPass pass>
266 void applyProperties(StyleResolverState&, const StylePropertySet* properties, StyleRule*, bool isImportant, bool inheritedOnly, PropertyWhitelistType = PropertyWhitelistNone);
267 template <StyleApplicationPass pass>
268 void applyAnimatedProperties(StyleResolverState&, const AnimationEffect::CompositableValueMap&);
269 void matchPageRules(MatchResult&, RuleSet*, bool isLeftPage, bool isFirstPage, const String& pageName);
270 void matchPageRulesForList(Vector<StyleRulePage*>& matchedRules, const Vector<StyleRulePage*>&, bool isLeftPage, bool isFirstPage, const String& pageName);
271 void collectViewportRules();
272 Settings* documentSettings() { return m_document.settings(); }
274 bool isLeftPage(int pageIndex) const;
275 bool isRightPage(int pageIndex) const { return !isLeftPage(pageIndex); }
276 bool isFirstPage(int pageIndex) const;
277 String pageName(int pageIndex) const;
279 bool pseudoStyleForElementInternal(Element&, const PseudoStyleRequest&, RenderStyle* parentStyle, StyleResolverState&);
281 // FIXME: This likely belongs on RuleSet.
282 typedef HashMap<StringImpl*, RefPtr<StyleRuleKeyframes> > KeyframesRuleMap;
283 KeyframesRuleMap m_keyframesRuleMap;
285 static RenderStyle* s_styleNotYetAvailable;
287 void cacheBorderAndBackground();
289 MatchedPropertiesCache m_matchedPropertiesCache;
291 OwnPtr<MediaQueryEvaluator> m_medium;
292 MediaQueryResultList m_viewportDependentMediaQueryResults;
294 RefPtr<RenderStyle> m_rootDefaultStyle;
296 Document& m_document;
297 SelectorFilter m_selectorFilter;
299 RefPtr<ViewportStyleResolver> m_viewportStyleResolver;
301 ListHashSet<CSSStyleSheet*, 16> m_pendingStyleSheets;
303 ScopedStyleTree m_styleTree;
305 // FIXME: The entire logic of collecting features on StyleResolver, as well as transferring them
306 // between various parts of machinery smells wrong. This needs to be better somehow.
307 RuleFeatureSet m_features;
308 OwnPtr<RuleSet> m_siblingRuleSet;
309 OwnPtr<RuleSet> m_uncommonAttributeRuleSet;
311 // FIXME: watched selectors should be implemented using injected author stylesheets: http://crbug.com/316960
312 OwnPtr<RuleSet> m_watchedSelectorsRules;
313 TreeBoundaryCrossingRules m_treeBoundaryCrossingRules;
315 bool m_needCollectFeatures;
317 StyleResourceLoader m_styleResourceLoader;
319 StyleSharingList m_styleSharingList;
321 OwnPtr<StyleResolverStats> m_styleResolverStats;
322 OwnPtr<StyleResolverStats> m_styleResolverStatsTotals;
323 unsigned m_styleResolverStatsSequence;
325 // Use only for Internals::updateStyleAndReturnAffectedElementCount.
326 unsigned m_accessCount;
329 } // namespace WebCore
331 #endif // StyleResolver_h