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 "platform/heap/Handle.h"
41 #include "wtf/Deque.h"
42 #include "wtf/HashMap.h"
43 #include "wtf/HashSet.h"
44 #include "wtf/ListHashSet.h"
45 #include "wtf/RefPtr.h"
46 #include "wtf/Vector.h"
50 class CSSAnimationUpdate;
51 class CSSFontSelector;
58 class DocumentTimeline;
60 class ElementRuleCollector;
63 class MediaQueryEvaluator;
64 class MediaQueryResult;
68 class StylePropertySet;
69 class StyleResolverStats;
71 class StyleRuleKeyframes;
73 class ViewportStyleResolver;
77 enum StyleSharingBehavior {
82 enum RuleMatchingBehavior {
84 MatchAllRulesExcludingSMIL
87 const unsigned styleSharingListSize = 15;
88 const unsigned styleSharingMaxDepth = 32;
89 typedef WTF::Deque<Element*, styleSharingListSize> StyleSharingList;
91 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;
99 RawPtrWillBeMember<CSSValue> value;
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_WILL_BE_REMOVED;
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 PassRefPtrWillBeRawPtr<AnimatableValue> createAnimatableValueSnapshot(Element&, CSSPropertyID, CSSValue*);
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&);
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(const WillBeHeapVector<RefPtrWillBeMember<CSSStyleSheet> >&);
139 void resetAuthorStyle(const ContainerNode*);
140 void finishAppendAuthorStyleSheets();
142 void processScopedRules(const RuleSet& authorRules, CSSStyleSheet*, ContainerNode& scope);
144 void lazyAppendAuthorStyleSheets(unsigned firstNew, const WillBeHeapVector<RefPtrWillBeMember<CSSStyleSheet> >&);
145 void removePendingAuthorStyleSheets(const WillBeHeapVector<RefPtrWillBeMember<CSSStyleSheet> >&);
146 void appendPendingAuthorStyleSheets();
147 bool hasPendingAuthorStyleSheets() const { return m_pendingStyleSheets.size() > 0 || m_needCollectFeatures; }
149 SelectorFilter& selectorFilter() { return m_selectorFilter; }
151 void setBuildScopedStyleTreeInDocumentOrder(bool enabled) { m_styleTree.setBuildInDocumentOrder(enabled); }
152 bool buildScopedStyleTreeInDocumentOrder() const { return m_styleTree.buildInDocumentOrder(); }
153 bool styleTreeHasOnlyScopedResolverForDocument() const { return m_styleTree.hasOnlyScopedResolverForDocument(); }
154 ScopedStyleResolver* styleTreeScopedStyleResolverForDocument() const { return m_styleTree.scopedStyleResolverForDocument(); }
156 ScopedStyleResolver* ensureScopedStyleResolver(ContainerNode* scope)
159 return m_styleTree.ensureScopedStyleResolver(*scope);
162 void styleTreeResolveScopedKeyframesRules(const Element* element, Vector<ScopedStyleResolver*, 8>& resolvers)
164 m_styleTree.resolveScopedKeyframesRules(element, resolvers);
167 // These methods will give back the set of rules that matched for a given element (or a pseudo-element).
169 UAAndUserCSSRules = 1 << 1,
170 AuthorCSSRules = 1 << 2,
171 EmptyCSSRules = 1 << 3,
172 CrossOriginCSSRules = 1 << 4,
173 AllButEmptyCSSRules = UAAndUserCSSRules | AuthorCSSRules | CrossOriginCSSRules,
174 AllCSSRules = AllButEmptyCSSRules | EmptyCSSRules,
176 PassRefPtrWillBeRawPtr<CSSRuleList> cssRulesForElement(Element*, unsigned rulesToInclude = AllButEmptyCSSRules);
177 PassRefPtrWillBeRawPtr<CSSRuleList> pseudoCSSRulesForElement(Element*, PseudoId, unsigned rulesToInclude = AllButEmptyCSSRules);
178 PassRefPtrWillBeRawPtr<StyleRuleList> styleRulesForElement(Element*, unsigned rulesToInclude);
180 // |properties| is an array with |count| elements.
181 void applyPropertiesToStyle(const CSSPropertyValue* properties, size_t count, RenderStyle*);
183 ViewportStyleResolver* viewportStyleResolver() { return m_viewportStyleResolver.get(); }
185 void addMediaQueryResults(const MediaQueryResultList&);
186 MediaQueryResultList* viewportDependentMediaQueryResults() { return &m_viewportDependentMediaQueryResults; }
187 bool hasViewportDependentMediaQueries() const { return !m_viewportDependentMediaQueryResults.isEmpty(); }
188 bool mediaQueryAffectedByViewportChange() const;
190 // FIXME: Rename to reflect the purpose, like didChangeFontSize or something.
191 void invalidateMatchedPropertiesCache();
193 void notifyResizeForViewportUnits();
195 // Exposed for RenderStyle::isStyleAvilable().
196 static RenderStyle* styleNotYetAvailable() { return s_styleNotYetAvailable; }
198 RuleFeatureSet& ensureUpdatedRuleFeatureSet()
200 if (hasPendingAuthorStyleSheets())
201 appendPendingAuthorStyleSheets();
205 RuleFeatureSet& ruleFeatureSet()
210 StyleSharingList& styleSharingList();
212 bool hasRulesForId(const AtomicString&) const;
214 void addToStyleSharingList(Element&);
215 void clearStyleSharingList();
217 StyleResolverStats* stats() { return m_styleResolverStats.get(); }
218 StyleResolverStats* statsTotals() { return m_styleResolverStatsTotals.get(); }
219 enum StatsReportType { ReportDefaultStats, ReportSlowStats };
220 void enableStats(StatsReportType = ReportDefaultStats);
224 unsigned accessCount() const { return m_accessCount; }
225 void didAccess() { ++m_accessCount; }
227 void increaseStyleSharingDepth() { ++m_styleSharingDepth; }
228 void decreaseStyleSharingDepth() { --m_styleSharingDepth; }
230 PassRefPtr<PseudoElement> createPseudoElementIfNeeded(Element& parent, PseudoId);
232 virtual void trace(Visitor*) OVERRIDE;
235 // CSSFontSelectorClient implementation.
236 virtual void fontsNeedUpdate(CSSFontSelector*) OVERRIDE;
239 void initWatchedSelectorRules(const WillBeHeapVector<RefPtrWillBeMember<StyleRule> >& watchedSelectors);
241 // FIXME: This should probably go away, folded into FontBuilder.
242 void updateFont(StyleResolverState&);
244 void loadPendingResources(StyleResolverState&);
245 void adjustRenderStyle(StyleResolverState&, Element*);
247 void appendCSSStyleSheet(CSSStyleSheet*);
249 void collectPseudoRulesForElement(Element*, ElementRuleCollector&, PseudoId, unsigned rulesToInclude);
250 void matchUARules(ElementRuleCollector&, RuleSet*);
251 void matchAuthorRules(Element*, ElementRuleCollector&, bool includeEmptyRules);
252 void matchAuthorRulesForShadowHost(Element*, ElementRuleCollector&, bool includeEmptyRules, Vector<ScopedStyleResolver*, 8>& resolvers, Vector<ScopedStyleResolver*, 8>& resolversInShadowTree);
253 void matchAllRules(StyleResolverState&, ElementRuleCollector&, bool includeSMILProperties);
254 void matchUARules(ElementRuleCollector&);
255 // FIXME: watched selectors should be implemented using injected author stylesheets: http://crbug.com/316960
256 void matchWatchSelectorRules(ElementRuleCollector&);
257 void collectFeatures();
258 void resetRuleFeatures();
260 bool fastRejectSelector(const RuleData&) const;
262 void applyMatchedProperties(StyleResolverState&, const MatchResult&);
263 bool applyAnimatedProperties(StyleResolverState&, Element* animatingElement);
265 enum StyleApplicationPass {
267 HighPriorityProperties,
268 LowPriorityProperties
270 template <StyleResolver::StyleApplicationPass pass>
271 static inline bool isPropertyForPass(CSSPropertyID);
272 template <StyleApplicationPass pass>
273 void applyMatchedProperties(StyleResolverState&, const MatchResult&, bool important, int startIndex, int endIndex, bool inheritedOnly);
274 template <StyleApplicationPass pass>
275 void applyProperties(StyleResolverState&, const StylePropertySet* properties, StyleRule*, bool isImportant, bool inheritedOnly, PropertyWhitelistType = PropertyWhitelistNone);
276 template <StyleApplicationPass pass>
277 void applyAnimatedProperties(StyleResolverState&, const WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> >&);
278 void matchPageRules(MatchResult&, RuleSet*, bool isLeftPage, bool isFirstPage, const String& pageName);
279 void matchPageRulesForList(WillBeHeapVector<RawPtrWillBeMember<StyleRulePage> >& matchedRules, const WillBeHeapVector<RawPtrWillBeMember<StyleRulePage> >&, bool isLeftPage, bool isFirstPage, const String& pageName);
280 void collectViewportRules();
281 Settings* documentSettings() { return m_document.settings(); }
283 bool isLeftPage(int pageIndex) const;
284 bool isRightPage(int pageIndex) const { return !isLeftPage(pageIndex); }
285 bool isFirstPage(int pageIndex) const;
286 String pageName(int pageIndex) const;
288 bool pseudoStyleForElementInternal(Element&, const PseudoStyleRequest&, RenderStyle* parentStyle, StyleResolverState&);
290 // FIXME: This likely belongs on RuleSet.
291 typedef WillBeHeapHashMap<StringImpl*, RefPtrWillBeMember<StyleRuleKeyframes> > KeyframesRuleMap;
292 KeyframesRuleMap m_keyframesRuleMap;
294 static RenderStyle* s_styleNotYetAvailable;
296 void cacheBorderAndBackground();
298 MatchedPropertiesCache m_matchedPropertiesCache;
300 OwnPtr<MediaQueryEvaluator> m_medium;
301 MediaQueryResultList m_viewportDependentMediaQueryResults;
303 Document& m_document;
304 SelectorFilter m_selectorFilter;
306 OwnPtrWillBeMember<ViewportStyleResolver> m_viewportStyleResolver;
308 WillBeHeapListHashSet<RawPtrWillBeMember<CSSStyleSheet>, 16> m_pendingStyleSheets;
310 ScopedStyleTree m_styleTree;
312 // FIXME: The entire logic of collecting features on StyleResolver, as well as transferring them
313 // between various parts of machinery smells wrong. This needs to be better somehow.
314 RuleFeatureSet m_features;
315 OwnPtrWillBeMember<RuleSet> m_siblingRuleSet;
316 OwnPtrWillBeMember<RuleSet> m_uncommonAttributeRuleSet;
318 // FIXME: watched selectors should be implemented using injected author stylesheets: http://crbug.com/316960
319 OwnPtrWillBeMember<RuleSet> m_watchedSelectorsRules;
320 TreeBoundaryCrossingRules m_treeBoundaryCrossingRules;
322 bool m_needCollectFeatures;
324 StyleResourceLoader m_styleResourceLoader;
326 unsigned m_styleSharingDepth;
327 Vector<OwnPtr<StyleSharingList>, styleSharingMaxDepth> m_styleSharingLists;
329 OwnPtr<StyleResolverStats> m_styleResolverStats;
330 OwnPtr<StyleResolverStats> m_styleResolverStatsTotals;
331 unsigned m_styleResolverStatsSequence;
333 // Use only for Internals::updateStyleAndReturnAffectedElementCount.
334 unsigned m_accessCount;
337 } // namespace WebCore
339 #endif // StyleResolver_h