Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / css / resolver / StyleResolver.h
1 /*
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.
4  *
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.
9  *
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.
14  *
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.
19  *
20  */
21
22 #ifndef StyleResolver_h
23 #define StyleResolver_h
24
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"
43
44 namespace blink {
45
46 class AnimatableValue;
47 class AnimationTimeline;
48 class CSSAnimationUpdate;
49 class CSSFontSelector;
50 class CSSRuleList;
51 class CSSSelector;
52 class CSSStyleSheet;
53 class CSSValue;
54 class ContainerNode;
55 class Document;
56 class Element;
57 class ElementRuleCollector;
58 class Interpolation;
59 class KeyframeList;
60 class KeyframeValue;
61 class MediaQueryEvaluator;
62 class MediaQueryResult;
63 class RuleData;
64 class Settings;
65 class StyleKeyframe;
66 class StylePropertySet;
67 class StyleResolverStats;
68 class StyleRule;
69 class StyleRuleKeyframes;
70 class StyleRulePage;
71 class ViewportStyleResolver;
72
73 class MatchResult;
74
75 enum StyleSharingBehavior {
76     AllowStyleSharing,
77     DisallowStyleSharing,
78 };
79
80 enum RuleMatchingBehavior {
81     MatchAllRules,
82     MatchAllRulesExcludingSMIL
83 };
84
85 const unsigned styleSharingListSize = 15;
86 const unsigned styleSharingMaxDepth = 32;
87 typedef WillBeHeapDeque<RawPtrWillBeMember<Element>, styleSharingListSize> StyleSharingList;
88
89 struct CSSPropertyValue {
90     STACK_ALLOCATED();
91 public:
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;
98 };
99
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;
103 public:
104     explicit StyleResolver(Document&);
105     virtual ~StyleResolver();
106
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&);
113
114     PassRefPtr<RenderStyle> styleForElement(Element*, RenderStyle* parentStyle = 0, StyleSharingBehavior = AllowStyleSharing,
115         RuleMatchingBehavior = MatchAllRules);
116
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&);
120
121     PassRefPtr<RenderStyle> pseudoStyleForElement(Element*, const PseudoStyleRequest&, RenderStyle* parentStyle);
122
123     PassRefPtr<RenderStyle> styleForPage(int pageIndex);
124     PassRefPtr<RenderStyle> defaultStyleForElement();
125     PassRefPtr<RenderStyle> styleForText(Text*);
126
127     static PassRefPtr<RenderStyle> styleForDocument(Document&);
128
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; }
132
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();
138
139     void processScopedRules(const RuleSet& authorRules, CSSStyleSheet*, ContainerNode& scope);
140
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; }
145
146     SelectorFilter& selectorFilter() { return m_selectorFilter; }
147
148     void styleTreeResolveScopedKeyframesRules(const Element*, WillBeHeapVector<RawPtrWillBeMember<ScopedStyleResolver>, 8>&);
149
150     // These methods will give back the set of rules that matched for a given element (or a pseudo-element).
151     enum CSSRuleFilter {
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,
158     };
159     PassRefPtrWillBeRawPtr<CSSRuleList> cssRulesForElement(Element*, unsigned rulesToInclude = AllButEmptyCSSRules);
160     PassRefPtrWillBeRawPtr<CSSRuleList> pseudoCSSRulesForElement(Element*, PseudoId, unsigned rulesToInclude = AllButEmptyCSSRules);
161     PassRefPtrWillBeRawPtr<StyleRuleList> styleRulesForElement(Element*, unsigned rulesToInclude);
162
163     // |properties| is an array with |count| elements.
164     void applyPropertiesToStyle(const CSSPropertyValue* properties, size_t count, RenderStyle*);
165
166     ViewportStyleResolver* viewportStyleResolver() { return m_viewportStyleResolver.get(); }
167
168     void addMediaQueryResults(const MediaQueryResultList&);
169     MediaQueryResultList* viewportDependentMediaQueryResults() { return &m_viewportDependentMediaQueryResults; }
170     bool hasViewportDependentMediaQueries() const { return !m_viewportDependentMediaQueryResults.isEmpty(); }
171     bool mediaQueryAffectedByViewportChange() const;
172
173     // FIXME: Rename to reflect the purpose, like didChangeFontSize or something.
174     void invalidateMatchedPropertiesCache();
175
176     void notifyResizeForViewportUnits();
177
178     // Exposed for RenderStyle::isStyleAvilable().
179     static RenderStyle* styleNotYetAvailable() { return s_styleNotYetAvailable; }
180
181     RuleFeatureSet& ensureUpdatedRuleFeatureSet()
182     {
183         if (hasPendingAuthorStyleSheets())
184             appendPendingAuthorStyleSheets();
185         return m_features;
186     }
187
188     RuleFeatureSet& ruleFeatureSet()
189     {
190         return m_features;
191     }
192
193     StyleSharingList& styleSharingList();
194
195     bool hasRulesForId(const AtomicString&) const;
196
197     void addToStyleSharingList(Element&);
198     void clearStyleSharingList();
199
200     StyleResolverStats* stats() { return m_styleResolverStats.get(); }
201     StyleResolverStats* statsTotals() { return m_styleResolverStatsTotals.get(); }
202     enum StatsReportType { ReportDefaultStats, ReportSlowStats };
203     void enableStats(StatsReportType = ReportDefaultStats);
204     void disableStats();
205     void printStats();
206
207     unsigned accessCount() const { return m_accessCount; }
208     void didAccess() { ++m_accessCount; }
209
210     void increaseStyleSharingDepth() { ++m_styleSharingDepth; }
211     void decreaseStyleSharingDepth() { --m_styleSharingDepth; }
212
213     PassRefPtrWillBeRawPtr<PseudoElement> createPseudoElementIfNeeded(Element& parent, PseudoId);
214
215     void trace(Visitor*);
216
217 private:
218     void initWatchedSelectorRules(const WillBeHeapVector<RefPtrWillBeMember<StyleRule> >& watchedSelectors);
219
220     // FIXME: This should probably go away, folded into FontBuilder.
221     void updateFont(StyleResolverState&);
222
223     void loadPendingResources(StyleResolverState&);
224     void adjustRenderStyle(StyleResolverState&, Element*);
225
226     void appendCSSStyleSheet(CSSStyleSheet*);
227
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();
238
239     bool fastRejectSelector(const RuleData&) const;
240
241     void applyMatchedProperties(StyleResolverState&, const MatchResult&);
242     bool applyAnimatedProperties(StyleResolverState&, Element* animatingElement);
243
244     void resolveScopedStyles(const Element*, WillBeHeapVector<RawPtrWillBeMember<ScopedStyleResolver>, 8>&);
245     void collectScopedResolversForHostedShadowTrees(const Element*, WillBeHeapVector<RawPtrWillBeMember<ScopedStyleResolver>, 8>&);
246
247     enum StyleApplicationPass {
248         HighPriorityProperties,
249         LowPriorityProperties
250     };
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*);
265
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();
269
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;
274
275     bool pseudoStyleForElementInternal(Element&, const PseudoStyleRequest&, RenderStyle* parentStyle, StyleResolverState&);
276
277     // FIXME: This likely belongs on RuleSet.
278     typedef WillBeHeapHashMap<StringImpl*, RefPtrWillBeMember<StyleRuleKeyframes> > KeyframesRuleMap;
279     KeyframesRuleMap m_keyframesRuleMap;
280
281     static RenderStyle* s_styleNotYetAvailable;
282
283     void cacheBorderAndBackground();
284
285     MatchedPropertiesCache m_matchedPropertiesCache;
286
287     OwnPtr<MediaQueryEvaluator> m_medium;
288     MediaQueryResultList m_viewportDependentMediaQueryResults;
289
290     RawPtrWillBeMember<Document> m_document;
291     SelectorFilter m_selectorFilter;
292
293     OwnPtrWillBeMember<ViewportStyleResolver> m_viewportStyleResolver;
294
295     WillBeHeapListHashSet<RawPtrWillBeMember<CSSStyleSheet>, 16> m_pendingStyleSheets;
296
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;
302
303     // FIXME: watched selectors should be implemented using injected author stylesheets: http://crbug.com/316960
304     OwnPtrWillBeMember<RuleSet> m_watchedSelectorsRules;
305     TreeBoundaryCrossingRules m_treeBoundaryCrossingRules;
306
307     bool m_needCollectFeatures;
308     bool m_printMediaType;
309
310     StyleResourceLoader m_styleResourceLoader;
311
312     unsigned m_styleSharingDepth;
313     WillBeHeapVector<OwnPtrWillBeMember<StyleSharingList>, styleSharingMaxDepth> m_styleSharingLists;
314
315     OwnPtr<StyleResolverStats> m_styleResolverStats;
316     OwnPtr<StyleResolverStats> m_styleResolverStatsTotals;
317     unsigned m_styleResolverStatsSequence;
318
319     // Use only for Internals::updateStyleAndReturnAffectedElementCount.
320     unsigned m_accessCount;
321 };
322
323 } // namespace blink
324
325 #endif // StyleResolver_h