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;
47 class AnimationTimeline;
48 class CSSAnimationUpdate;
49 class CSSFontSelector;
57 class ElementRuleCollector;
61 class MediaQueryEvaluator;
62 class MediaQueryResult;
66 class StylePropertySet;
67 class StyleResolverStats;
69 class StyleRuleKeyframes;
71 class ViewportStyleResolver;
75 enum StyleSharingBehavior {
80 enum RuleMatchingBehavior {
82 MatchAllRulesExcludingSMIL
85 const unsigned styleSharingListSize = 15;
86 const unsigned styleSharingMaxDepth = 32;
87 typedef WillBeHeapDeque<RawPtrWillBeMember<Element>, styleSharingListSize> StyleSharingList;
89 struct CSSPropertyValue {
92 CSSPropertyValue(CSSPropertyID property, CSSValue* value)
93 : property(property), value(value) { }
94 // Stores value=propertySet.getPropertyCSSValue(id).get().
95 CSSPropertyValue(CSSPropertyID, const StylePropertySet&);
96 CSSPropertyID property;
97 RawPtrWillBeMember<CSSValue> value;
100 // This class selects a RenderStyle for a given element based on a collection of stylesheets.
101 class StyleResolver FINAL : public NoBaseWillBeGarbageCollectedFinalized<StyleResolver> {
102 WTF_MAKE_NONCOPYABLE(StyleResolver); WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
104 explicit StyleResolver(Document&);
105 virtual ~StyleResolver();
107 // FIXME: StyleResolver should not be keeping tree-walk state.
108 // These should move to some global tree-walk state, or should be contained in a
109 // TreeWalkContext or similar which is passed in to StyleResolver methods when available.
110 // Using these during tree walk will allow style selector to optimize child and descendant selector lookups.
111 void pushParentElement(Element&);
112 void popParentElement(Element&);
114 PassRefPtr<RenderStyle> styleForElement(Element*, RenderStyle* parentStyle = 0, StyleSharingBehavior = AllowStyleSharing,
115 RuleMatchingBehavior = MatchAllRules);
117 PassRefPtr<RenderStyle> styleForKeyframe(Element*, const RenderStyle&, RenderStyle* parentStyle, const StyleKeyframe*, const AtomicString& animationName);
118 static PassRefPtrWillBeRawPtr<AnimatableValue> createAnimatableValueSnapshot(Element&, CSSPropertyID, CSSValue&);
119 static PassRefPtrWillBeRawPtr<AnimatableValue> createAnimatableValueSnapshot(StyleResolverState&, CSSPropertyID, CSSValue&);
121 PassRefPtr<RenderStyle> pseudoStyleForElement(Element*, const PseudoStyleRequest&, RenderStyle* parentStyle);
123 PassRefPtr<RenderStyle> styleForPage(int pageIndex);
124 PassRefPtr<RenderStyle> defaultStyleForElement();
125 PassRefPtr<RenderStyle> styleForText(Text*);
127 static PassRefPtr<RenderStyle> styleForDocument(Document&);
129 // FIXME: This only has 5 callers and should be removed. Callers should be explicit about
130 // their dependency on Document* instead of grabbing one through StyleResolver.
131 Document& document() { return *m_document; }
133 // FIXME: It could be better to call appendAuthorStyleSheets() directly after we factor StyleResolver further.
134 // https://bugs.webkit.org/show_bug.cgi?id=108890
135 void appendAuthorStyleSheets(const WillBeHeapVector<RefPtrWillBeMember<CSSStyleSheet> >&);
136 void resetAuthorStyle(TreeScope&);
137 void finishAppendAuthorStyleSheets();
139 void processScopedRules(const RuleSet& authorRules, CSSStyleSheet*, ContainerNode& scope);
141 void lazyAppendAuthorStyleSheets(unsigned firstNew, const WillBeHeapVector<RefPtrWillBeMember<CSSStyleSheet> >&);
142 void removePendingAuthorStyleSheets(const WillBeHeapVector<RefPtrWillBeMember<CSSStyleSheet> >&);
143 void appendPendingAuthorStyleSheets();
144 bool hasPendingAuthorStyleSheets() const { return m_pendingStyleSheets.size() > 0 || m_needCollectFeatures; }
146 SelectorFilter& selectorFilter() { return m_selectorFilter; }
148 void styleTreeResolveScopedKeyframesRules(const Element*, WillBeHeapVector<RawPtrWillBeMember<ScopedStyleResolver>, 8>&);
150 // These methods will give back the set of rules that matched for a given element (or a pseudo-element).
152 UAAndUserCSSRules = 1 << 1,
153 AuthorCSSRules = 1 << 2,
154 EmptyCSSRules = 1 << 3,
155 CrossOriginCSSRules = 1 << 4,
156 AllButEmptyCSSRules = UAAndUserCSSRules | AuthorCSSRules | CrossOriginCSSRules,
157 AllCSSRules = AllButEmptyCSSRules | EmptyCSSRules,
159 PassRefPtrWillBeRawPtr<CSSRuleList> cssRulesForElement(Element*, unsigned rulesToInclude = AllButEmptyCSSRules);
160 PassRefPtrWillBeRawPtr<CSSRuleList> pseudoCSSRulesForElement(Element*, PseudoId, unsigned rulesToInclude = AllButEmptyCSSRules);
161 PassRefPtrWillBeRawPtr<StyleRuleList> styleRulesForElement(Element*, unsigned rulesToInclude);
163 // |properties| is an array with |count| elements.
164 void applyPropertiesToStyle(const CSSPropertyValue* properties, size_t count, RenderStyle*);
166 ViewportStyleResolver* viewportStyleResolver() { return m_viewportStyleResolver.get(); }
168 void addMediaQueryResults(const MediaQueryResultList&);
169 MediaQueryResultList* viewportDependentMediaQueryResults() { return &m_viewportDependentMediaQueryResults; }
170 bool hasViewportDependentMediaQueries() const { return !m_viewportDependentMediaQueryResults.isEmpty(); }
171 bool mediaQueryAffectedByViewportChange() const;
173 // FIXME: Rename to reflect the purpose, like didChangeFontSize or something.
174 void invalidateMatchedPropertiesCache();
176 void notifyResizeForViewportUnits();
178 // Exposed for RenderStyle::isStyleAvilable().
179 static RenderStyle* styleNotYetAvailable() { return s_styleNotYetAvailable; }
181 RuleFeatureSet& ensureUpdatedRuleFeatureSet()
183 if (hasPendingAuthorStyleSheets())
184 appendPendingAuthorStyleSheets();
188 RuleFeatureSet& ruleFeatureSet()
193 StyleSharingList& styleSharingList();
195 bool hasRulesForId(const AtomicString&) const;
197 void addToStyleSharingList(Element&);
198 void clearStyleSharingList();
200 StyleResolverStats* stats() { return m_styleResolverStats.get(); }
201 StyleResolverStats* statsTotals() { return m_styleResolverStatsTotals.get(); }
202 enum StatsReportType { ReportDefaultStats, ReportSlowStats };
203 void enableStats(StatsReportType = ReportDefaultStats);
207 unsigned accessCount() const { return m_accessCount; }
208 void didAccess() { ++m_accessCount; }
210 void increaseStyleSharingDepth() { ++m_styleSharingDepth; }
211 void decreaseStyleSharingDepth() { --m_styleSharingDepth; }
213 PassRefPtrWillBeRawPtr<PseudoElement> createPseudoElementIfNeeded(Element& parent, PseudoId);
215 void trace(Visitor*);
218 void initWatchedSelectorRules(const WillBeHeapVector<RefPtrWillBeMember<StyleRule> >& watchedSelectors);
220 // FIXME: This should probably go away, folded into FontBuilder.
221 void updateFont(StyleResolverState&);
223 void loadPendingResources(StyleResolverState&);
224 void adjustRenderStyle(StyleResolverState&, Element*);
226 void appendCSSStyleSheet(CSSStyleSheet*);
228 void collectPseudoRulesForElement(Element*, ElementRuleCollector&, PseudoId, unsigned rulesToInclude);
229 void matchUARules(ElementRuleCollector&, RuleSet*);
230 void matchAuthorRules(Element*, ElementRuleCollector&, bool includeEmptyRules);
231 void matchAuthorRulesForShadowHost(Element*, ElementRuleCollector&, bool includeEmptyRules, WillBeHeapVector<RawPtrWillBeMember<ScopedStyleResolver>, 8>& resolvers, WillBeHeapVector<RawPtrWillBeMember<ScopedStyleResolver>, 8>& resolversInShadowTree);
232 void matchAllRules(StyleResolverState&, ElementRuleCollector&, bool includeSMILProperties);
233 void matchUARules(ElementRuleCollector&);
234 // FIXME: watched selectors should be implemented using injected author stylesheets: http://crbug.com/316960
235 void matchWatchSelectorRules(ElementRuleCollector&);
236 void collectFeatures();
237 void resetRuleFeatures();
239 bool fastRejectSelector(const RuleData&) const;
241 void applyMatchedProperties(StyleResolverState&, const MatchResult&);
242 bool applyAnimatedProperties(StyleResolverState&, Element* animatingElement);
244 void resolveScopedStyles(const Element*, WillBeHeapVector<RawPtrWillBeMember<ScopedStyleResolver>, 8>&);
245 void collectScopedResolversForHostedShadowTrees(const Element*, WillBeHeapVector<RawPtrWillBeMember<ScopedStyleResolver>, 8>&);
247 enum StyleApplicationPass {
248 HighPriorityProperties,
249 LowPriorityProperties
251 template <StyleResolver::StyleApplicationPass pass>
252 static inline CSSPropertyID firstCSSPropertyId();
253 template <StyleResolver::StyleApplicationPass pass>
254 static inline CSSPropertyID lastCSSPropertyId();
255 template <StyleResolver::StyleApplicationPass pass>
256 static inline bool isPropertyForPass(CSSPropertyID);
257 template <StyleApplicationPass pass>
258 void applyMatchedProperties(StyleResolverState&, const MatchResult&, bool important, int startIndex, int endIndex, bool inheritedOnly);
259 template <StyleApplicationPass pass>
260 void applyProperties(StyleResolverState&, const StylePropertySet* properties, StyleRule*, bool isImportant, bool inheritedOnly, PropertyWhitelistType = PropertyWhitelistNone);
261 template <StyleApplicationPass pass>
262 void applyAnimatedProperties(StyleResolverState&, const WillBeHeapHashMap<CSSPropertyID, RefPtrWillBeMember<Interpolation> >&);
263 template <StyleResolver::StyleApplicationPass pass>
264 void applyAllProperty(StyleResolverState&, CSSValue*);
266 void matchPageRules(MatchResult&, RuleSet*, bool isLeftPage, bool isFirstPage, const String& pageName);
267 void matchPageRulesForList(WillBeHeapVector<RawPtrWillBeMember<StyleRulePage> >& matchedRules, const WillBeHeapVector<RawPtrWillBeMember<StyleRulePage> >&, bool isLeftPage, bool isFirstPage, const String& pageName);
268 void collectViewportRules();
270 bool isLeftPage(int pageIndex) const;
271 bool isRightPage(int pageIndex) const { return !isLeftPage(pageIndex); }
272 bool isFirstPage(int pageIndex) const;
273 String pageName(int pageIndex) const;
275 bool pseudoStyleForElementInternal(Element&, const PseudoStyleRequest&, RenderStyle* parentStyle, StyleResolverState&);
277 // FIXME: This likely belongs on RuleSet.
278 typedef WillBeHeapHashMap<StringImpl*, RefPtrWillBeMember<StyleRuleKeyframes> > KeyframesRuleMap;
279 KeyframesRuleMap m_keyframesRuleMap;
281 static RenderStyle* s_styleNotYetAvailable;
283 void cacheBorderAndBackground();
285 MatchedPropertiesCache m_matchedPropertiesCache;
287 OwnPtr<MediaQueryEvaluator> m_medium;
288 MediaQueryResultList m_viewportDependentMediaQueryResults;
290 RawPtrWillBeMember<Document> m_document;
291 SelectorFilter m_selectorFilter;
293 OwnPtrWillBeMember<ViewportStyleResolver> m_viewportStyleResolver;
295 WillBeHeapListHashSet<RawPtrWillBeMember<CSSStyleSheet>, 16> m_pendingStyleSheets;
297 // FIXME: The entire logic of collecting features on StyleResolver, as well as transferring them
298 // between various parts of machinery smells wrong. This needs to be better somehow.
299 RuleFeatureSet m_features;
300 OwnPtrWillBeMember<RuleSet> m_siblingRuleSet;
301 OwnPtrWillBeMember<RuleSet> m_uncommonAttributeRuleSet;
303 // FIXME: watched selectors should be implemented using injected author stylesheets: http://crbug.com/316960
304 OwnPtrWillBeMember<RuleSet> m_watchedSelectorsRules;
305 TreeBoundaryCrossingRules m_treeBoundaryCrossingRules;
307 bool m_needCollectFeatures;
308 bool m_printMediaType;
310 StyleResourceLoader m_styleResourceLoader;
312 unsigned m_styleSharingDepth;
313 WillBeHeapVector<OwnPtrWillBeMember<StyleSharingList>, styleSharingMaxDepth> m_styleSharingLists;
315 OwnPtr<StyleResolverStats> m_styleResolverStats;
316 OwnPtr<StyleResolverStats> m_styleResolverStatsTotals;
317 unsigned m_styleResolverStatsSequence;
319 // Use only for Internals::updateStyleAndReturnAffectedElementCount.
320 unsigned m_accessCount;
325 #endif // StyleResolver_h