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/css/PseudoStyleRequest.h"
26 #include "core/css/RuleFeature.h"
27 #include "core/css/RuleSet.h"
28 #include "core/css/SelectorChecker.h"
29 #include "core/css/SelectorFilter.h"
30 #include "core/css/SiblingTraversalStrategies.h"
31 #include "core/css/TreeBoundaryCrossingRules.h"
32 #include "core/css/resolver/MatchedPropertiesCache.h"
33 #include "core/css/resolver/ScopedStyleResolver.h"
34 #include "core/css/resolver/StyleBuilder.h"
35 #include "core/css/resolver/StyleResourceLoader.h"
36 #include "platform/heap/Handle.h"
37 #include "wtf/Deque.h"
38 #include "wtf/HashMap.h"
39 #include "wtf/HashSet.h"
40 #include "wtf/ListHashSet.h"
41 #include "wtf/RefPtr.h"
42 #include "wtf/Vector.h"
46 class AnimatableValue;
53 class ElementRuleCollector;
55 class MediaQueryEvaluator;
58 class StylePropertySet;
59 class StyleResolverStats;
61 class StyleRuleKeyframes;
63 class ViewportStyleResolver;
67 enum StyleSharingBehavior {
72 enum RuleMatchingBehavior {
74 MatchAllRulesExcludingSMIL
77 const unsigned styleSharingListSize = 15;
78 const unsigned styleSharingMaxDepth = 32;
79 typedef WillBeHeapDeque<RawPtrWillBeMember<Element>, styleSharingListSize> StyleSharingList;
81 struct CSSPropertyValue {
84 CSSPropertyValue(CSSPropertyID property, CSSValue* value)
85 : property(property), value(value) { }
86 // Stores value=propertySet.getPropertyCSSValue(id).get().
87 CSSPropertyValue(CSSPropertyID, const StylePropertySet&);
88 CSSPropertyID property;
89 RawPtrWillBeMember<CSSValue> value;
92 // This class selects a RenderStyle for a given element based on a collection of stylesheets.
93 class StyleResolver final : public NoBaseWillBeGarbageCollectedFinalized<StyleResolver> {
94 WTF_MAKE_NONCOPYABLE(StyleResolver); WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
96 explicit StyleResolver(Document&);
97 virtual ~StyleResolver();
99 // FIXME: StyleResolver should not be keeping tree-walk state.
100 // These should move to some global tree-walk state, or should be contained in a
101 // TreeWalkContext or similar which is passed in to StyleResolver methods when available.
102 // Using these during tree walk will allow style selector to optimize child and descendant selector lookups.
103 void pushParentElement(Element&);
104 void popParentElement(Element&);
106 PassRefPtr<RenderStyle> styleForElement(Element*, RenderStyle* parentStyle = 0, StyleSharingBehavior = AllowStyleSharing,
107 RuleMatchingBehavior = MatchAllRules);
109 PassRefPtr<RenderStyle> styleForKeyframe(Element&, const RenderStyle&, RenderStyle* parentStyle, const StyleKeyframe*, const AtomicString& animationName);
110 static PassRefPtrWillBeRawPtr<AnimatableValue> createAnimatableValueSnapshot(Element&, CSSPropertyID, CSSValue&);
111 static PassRefPtrWillBeRawPtr<AnimatableValue> createAnimatableValueSnapshot(StyleResolverState&, CSSPropertyID, CSSValue&);
113 PassRefPtr<RenderStyle> pseudoStyleForElement(Element*, const PseudoStyleRequest&, RenderStyle* parentStyle);
115 PassRefPtr<RenderStyle> styleForPage(int pageIndex);
116 PassRefPtr<RenderStyle> defaultStyleForElement();
117 PassRefPtr<RenderStyle> styleForText(Text*);
119 static PassRefPtr<RenderStyle> styleForDocument(Document&);
121 // FIXME: It could be better to call appendAuthorStyleSheets() directly after we factor StyleResolver further.
122 // https://bugs.webkit.org/show_bug.cgi?id=108890
123 void appendAuthorStyleSheets(const WillBeHeapVector<RefPtrWillBeMember<CSSStyleSheet> >&);
124 void resetAuthorStyle(TreeScope&);
125 void finishAppendAuthorStyleSheets();
127 void processScopedRules(const RuleSet& authorRules, CSSStyleSheet*, unsigned sheetIndex, ContainerNode& scope);
129 void lazyAppendAuthorStyleSheets(unsigned firstNew, const WillBeHeapVector<RefPtrWillBeMember<CSSStyleSheet> >&);
130 void removePendingAuthorStyleSheets(const WillBeHeapVector<RefPtrWillBeMember<CSSStyleSheet> >&);
131 void appendPendingAuthorStyleSheets();
132 bool hasPendingAuthorStyleSheets() const { return m_pendingStyleSheets.size() > 0 || m_needCollectFeatures; }
134 SelectorFilter& selectorFilter() { return m_selectorFilter; }
136 void styleTreeResolveScopedKeyframesRules(const Element*, WillBeHeapVector<RawPtrWillBeMember<ScopedStyleResolver>, 8>&);
138 // These methods will give back the set of rules that matched for a given element (or a pseudo-element).
140 UAAndUserCSSRules = 1 << 1,
141 AuthorCSSRules = 1 << 2,
142 EmptyCSSRules = 1 << 3,
143 CrossOriginCSSRules = 1 << 4,
144 AllButEmptyCSSRules = UAAndUserCSSRules | AuthorCSSRules | CrossOriginCSSRules,
145 AllCSSRules = AllButEmptyCSSRules | EmptyCSSRules,
147 PassRefPtrWillBeRawPtr<CSSRuleList> cssRulesForElement(Element*, unsigned rulesToInclude = AllButEmptyCSSRules);
148 PassRefPtrWillBeRawPtr<CSSRuleList> pseudoCSSRulesForElement(Element*, PseudoId, unsigned rulesToInclude = AllButEmptyCSSRules);
149 PassRefPtrWillBeRawPtr<StyleRuleList> styleRulesForElement(Element*, unsigned rulesToInclude);
151 // |properties| is an array with |count| elements.
152 void applyPropertiesToStyle(const CSSPropertyValue* properties, size_t count, RenderStyle*);
154 ViewportStyleResolver* viewportStyleResolver() { return m_viewportStyleResolver.get(); }
156 void addMediaQueryResults(const MediaQueryResultList&);
157 MediaQueryResultList* viewportDependentMediaQueryResults() { return &m_viewportDependentMediaQueryResults; }
158 bool hasViewportDependentMediaQueries() const { return !m_viewportDependentMediaQueryResults.isEmpty(); }
159 bool mediaQueryAffectedByViewportChange() const;
161 // FIXME: Rename to reflect the purpose, like didChangeFontSize or something.
162 void invalidateMatchedPropertiesCache();
164 void notifyResizeForViewportUnits();
166 // Exposed for RenderStyle::isStyleAvilable().
167 static RenderStyle* styleNotYetAvailable() { return s_styleNotYetAvailable; }
169 RuleFeatureSet& ensureUpdatedRuleFeatureSet()
171 if (hasPendingAuthorStyleSheets())
172 appendPendingAuthorStyleSheets();
176 RuleFeatureSet& ruleFeatureSet()
181 StyleSharingList& styleSharingList();
183 bool hasRulesForId(const AtomicString&) const;
185 void addToStyleSharingList(Element&);
186 void clearStyleSharingList();
188 StyleResolverStats* stats() { return m_styleResolverStats.get(); }
189 StyleResolverStats* statsTotals() { return m_styleResolverStatsTotals.get(); }
190 enum StatsReportType { ReportDefaultStats, ReportSlowStats };
191 void enableStats(StatsReportType = ReportDefaultStats);
195 unsigned accessCount() const { return m_accessCount; }
196 void didAccess() { ++m_accessCount; }
198 void increaseStyleSharingDepth() { ++m_styleSharingDepth; }
199 void decreaseStyleSharingDepth() { --m_styleSharingDepth; }
201 PassRefPtrWillBeRawPtr<PseudoElement> createPseudoElementIfNeeded(Element& parent, PseudoId);
203 void trace(Visitor*);
206 void initWatchedSelectorRules(const WillBeHeapVector<RefPtrWillBeMember<StyleRule> >& watchedSelectors);
208 // FIXME: This should probably go away, folded into FontBuilder.
209 void updateFont(StyleResolverState&);
211 void loadPendingResources(StyleResolverState&);
212 void adjustRenderStyle(StyleResolverState&, Element*);
214 void appendCSSStyleSheet(CSSStyleSheet*);
215 void addRulesFromSheet(CSSStyleSheet*, TreeScope*, unsigned);
217 void collectPseudoRulesForElement(Element*, ElementRuleCollector&, PseudoId, unsigned rulesToInclude);
218 void matchRuleSet(ElementRuleCollector&, RuleSet*);
219 void matchUARules(ElementRuleCollector&);
220 void matchAuthorRules(Element*, ElementRuleCollector&, bool includeEmptyRules);
221 void matchAuthorRulesForShadowHost(Element*, ElementRuleCollector&, bool includeEmptyRules, WillBeHeapVector<RawPtrWillBeMember<ScopedStyleResolver>, 8>& resolvers, WillBeHeapVector<RawPtrWillBeMember<ScopedStyleResolver>, 8>& resolversInShadowTree);
222 void matchAllRules(StyleResolverState&, ElementRuleCollector&, bool includeSMILProperties);
223 void collectFeatures();
224 void resetRuleFeatures();
226 bool fastRejectSelector(const RuleData&) const;
228 void applyMatchedProperties(StyleResolverState&, const MatchResult&);
229 bool applyAnimatedProperties(StyleResolverState&, const Element* animatingElement);
230 void applyCallbackSelectors(StyleResolverState&);
232 void resolveScopedStyles(const Element*, WillBeHeapVector<RawPtrWillBeMember<ScopedStyleResolver>, 8>&);
233 void collectScopedResolversForHostedShadowTrees(const Element*, WillBeHeapVector<RawPtrWillBeMember<ScopedStyleResolver>, 8>&);
235 enum StyleApplicationPass {
236 HighPriorityProperties,
237 LowPriorityProperties
239 template <StyleResolver::StyleApplicationPass pass>
240 static inline CSSPropertyID firstCSSPropertyId();
241 template <StyleResolver::StyleApplicationPass pass>
242 static inline CSSPropertyID lastCSSPropertyId();
243 template <StyleResolver::StyleApplicationPass pass>
244 static inline bool isPropertyForPass(CSSPropertyID);
245 template <StyleApplicationPass pass>
246 void applyMatchedProperties(StyleResolverState&, const MatchResult&, bool important, int startIndex, int endIndex, bool inheritedOnly);
247 template <StyleApplicationPass pass>
248 void applyProperties(StyleResolverState&, const StylePropertySet* properties, bool isImportant, bool inheritedOnly, PropertyWhitelistType = PropertyWhitelistNone);
249 template <StyleApplicationPass pass>
250 void applyAnimatedProperties(StyleResolverState&, const WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> >&);
251 template <StyleResolver::StyleApplicationPass pass>
252 void applyAllProperty(StyleResolverState&, CSSValue*);
254 void matchPageRules(MatchResult&, RuleSet*, bool isLeftPage, bool isFirstPage, const String& pageName);
255 void matchPageRulesForList(WillBeHeapVector<RawPtrWillBeMember<StyleRulePage> >& matchedRules, const WillBeHeapVector<RawPtrWillBeMember<StyleRulePage> >&, bool isLeftPage, bool isFirstPage, const String& pageName);
256 void collectViewportRules();
258 bool isLeftPage(int pageIndex) const;
259 bool isRightPage(int pageIndex) const { return !isLeftPage(pageIndex); }
260 bool isFirstPage(int pageIndex) const;
261 String pageName(int pageIndex) const;
263 bool pseudoStyleForElementInternal(Element&, const PseudoStyleRequest&, RenderStyle* parentStyle, StyleResolverState&);
265 Document& document() { return *m_document; }
267 // FIXME: This likely belongs on RuleSet.
268 typedef WillBeHeapHashMap<StringImpl*, RefPtrWillBeMember<StyleRuleKeyframes> > KeyframesRuleMap;
269 KeyframesRuleMap m_keyframesRuleMap;
271 static RenderStyle* s_styleNotYetAvailable;
273 void cacheBorderAndBackground();
275 MatchedPropertiesCache m_matchedPropertiesCache;
277 OwnPtr<MediaQueryEvaluator> m_medium;
278 MediaQueryResultList m_viewportDependentMediaQueryResults;
280 RawPtrWillBeMember<Document> m_document;
281 SelectorFilter m_selectorFilter;
283 OwnPtrWillBeMember<ViewportStyleResolver> m_viewportStyleResolver;
285 WillBeHeapListHashSet<RawPtrWillBeMember<CSSStyleSheet>, 16> m_pendingStyleSheets;
287 // FIXME: The entire logic of collecting features on StyleResolver, as well as transferring them
288 // between various parts of machinery smells wrong. This needs to be better somehow.
289 RuleFeatureSet m_features;
290 OwnPtrWillBeMember<RuleSet> m_siblingRuleSet;
291 OwnPtrWillBeMember<RuleSet> m_uncommonAttributeRuleSet;
292 OwnPtrWillBeMember<RuleSet> m_watchedSelectorsRules;
293 TreeBoundaryCrossingRules m_treeBoundaryCrossingRules;
295 bool m_needCollectFeatures;
296 bool m_printMediaType;
298 StyleResourceLoader m_styleResourceLoader;
300 unsigned m_styleSharingDepth;
301 WillBeHeapVector<OwnPtrWillBeMember<StyleSharingList>, styleSharingMaxDepth> m_styleSharingLists;
303 OwnPtr<StyleResolverStats> m_styleResolverStats;
304 OwnPtr<StyleResolverStats> m_styleResolverStatsTotals;
305 unsigned m_styleResolverStatsSequence;
307 // Use only for Internals::updateStyleAndReturnAffectedElementCount.
308 unsigned m_accessCount;
313 #endif // StyleResolver_h