Update To 11.40.268.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 CSSRuleList;
48 class CSSStyleSheet;
49 class CSSValue;
50 class ContainerNode;
51 class Document;
52 class Element;
53 class ElementRuleCollector;
54 class Interpolation;
55 class MediaQueryEvaluator;
56 class RuleData;
57 class StyleKeyframe;
58 class StylePropertySet;
59 class StyleResolverStats;
60 class StyleRule;
61 class StyleRuleKeyframes;
62 class StyleRulePage;
63 class ViewportStyleResolver;
64
65 class MatchResult;
66
67 enum StyleSharingBehavior {
68     AllowStyleSharing,
69     DisallowStyleSharing,
70 };
71
72 enum RuleMatchingBehavior {
73     MatchAllRules,
74     MatchAllRulesExcludingSMIL
75 };
76
77 const unsigned styleSharingListSize = 15;
78 const unsigned styleSharingMaxDepth = 32;
79 typedef WillBeHeapDeque<RawPtrWillBeMember<Element>, styleSharingListSize> StyleSharingList;
80
81 struct CSSPropertyValue {
82     STACK_ALLOCATED();
83 public:
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;
90 };
91
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;
95 public:
96     explicit StyleResolver(Document&);
97     virtual ~StyleResolver();
98
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&);
105
106     PassRefPtr<RenderStyle> styleForElement(Element*, RenderStyle* parentStyle = 0, StyleSharingBehavior = AllowStyleSharing,
107         RuleMatchingBehavior = MatchAllRules);
108
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&);
112
113     PassRefPtr<RenderStyle> pseudoStyleForElement(Element*, const PseudoStyleRequest&, RenderStyle* parentStyle);
114
115     PassRefPtr<RenderStyle> styleForPage(int pageIndex);
116     PassRefPtr<RenderStyle> defaultStyleForElement();
117     PassRefPtr<RenderStyle> styleForText(Text*);
118
119     static PassRefPtr<RenderStyle> styleForDocument(Document&);
120
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();
126
127     void processScopedRules(const RuleSet& authorRules, CSSStyleSheet*, unsigned sheetIndex, ContainerNode& scope);
128
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; }
133
134     SelectorFilter& selectorFilter() { return m_selectorFilter; }
135
136     void styleTreeResolveScopedKeyframesRules(const Element*, WillBeHeapVector<RawPtrWillBeMember<ScopedStyleResolver>, 8>&);
137
138     // These methods will give back the set of rules that matched for a given element (or a pseudo-element).
139     enum CSSRuleFilter {
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,
146     };
147     PassRefPtrWillBeRawPtr<CSSRuleList> cssRulesForElement(Element*, unsigned rulesToInclude = AllButEmptyCSSRules);
148     PassRefPtrWillBeRawPtr<CSSRuleList> pseudoCSSRulesForElement(Element*, PseudoId, unsigned rulesToInclude = AllButEmptyCSSRules);
149     PassRefPtrWillBeRawPtr<StyleRuleList> styleRulesForElement(Element*, unsigned rulesToInclude);
150
151     // |properties| is an array with |count| elements.
152     void applyPropertiesToStyle(const CSSPropertyValue* properties, size_t count, RenderStyle*);
153
154     ViewportStyleResolver* viewportStyleResolver() { return m_viewportStyleResolver.get(); }
155
156     void addMediaQueryResults(const MediaQueryResultList&);
157     MediaQueryResultList* viewportDependentMediaQueryResults() { return &m_viewportDependentMediaQueryResults; }
158     bool hasViewportDependentMediaQueries() const { return !m_viewportDependentMediaQueryResults.isEmpty(); }
159     bool mediaQueryAffectedByViewportChange() const;
160
161     // FIXME: Rename to reflect the purpose, like didChangeFontSize or something.
162     void invalidateMatchedPropertiesCache();
163
164     void notifyResizeForViewportUnits();
165
166     // Exposed for RenderStyle::isStyleAvilable().
167     static RenderStyle* styleNotYetAvailable() { return s_styleNotYetAvailable; }
168
169     RuleFeatureSet& ensureUpdatedRuleFeatureSet()
170     {
171         if (hasPendingAuthorStyleSheets())
172             appendPendingAuthorStyleSheets();
173         return m_features;
174     }
175
176     RuleFeatureSet& ruleFeatureSet()
177     {
178         return m_features;
179     }
180
181     StyleSharingList& styleSharingList();
182
183     bool hasRulesForId(const AtomicString&) const;
184
185     void addToStyleSharingList(Element&);
186     void clearStyleSharingList();
187
188     StyleResolverStats* stats() { return m_styleResolverStats.get(); }
189     StyleResolverStats* statsTotals() { return m_styleResolverStatsTotals.get(); }
190     enum StatsReportType { ReportDefaultStats, ReportSlowStats };
191     void enableStats(StatsReportType = ReportDefaultStats);
192     void disableStats();
193     void printStats();
194
195     unsigned accessCount() const { return m_accessCount; }
196     void didAccess() { ++m_accessCount; }
197
198     void increaseStyleSharingDepth() { ++m_styleSharingDepth; }
199     void decreaseStyleSharingDepth() { --m_styleSharingDepth; }
200
201     PassRefPtrWillBeRawPtr<PseudoElement> createPseudoElementIfNeeded(Element& parent, PseudoId);
202
203     void trace(Visitor*);
204
205 private:
206     void initWatchedSelectorRules(const WillBeHeapVector<RefPtrWillBeMember<StyleRule> >& watchedSelectors);
207
208     // FIXME: This should probably go away, folded into FontBuilder.
209     void updateFont(StyleResolverState&);
210
211     void loadPendingResources(StyleResolverState&);
212     void adjustRenderStyle(StyleResolverState&, Element*);
213
214     void appendCSSStyleSheet(CSSStyleSheet*);
215     void addRulesFromSheet(CSSStyleSheet*, TreeScope*, unsigned);
216
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();
225
226     bool fastRejectSelector(const RuleData&) const;
227
228     void applyMatchedProperties(StyleResolverState&, const MatchResult&);
229     bool applyAnimatedProperties(StyleResolverState&, const Element* animatingElement);
230     void applyCallbackSelectors(StyleResolverState&);
231
232     void resolveScopedStyles(const Element*, WillBeHeapVector<RawPtrWillBeMember<ScopedStyleResolver>, 8>&);
233     void collectScopedResolversForHostedShadowTrees(const Element*, WillBeHeapVector<RawPtrWillBeMember<ScopedStyleResolver>, 8>&);
234
235     enum StyleApplicationPass {
236         HighPriorityProperties,
237         LowPriorityProperties
238     };
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*);
253
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();
257
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;
262
263     bool pseudoStyleForElementInternal(Element&, const PseudoStyleRequest&, RenderStyle* parentStyle, StyleResolverState&);
264
265     Document& document() { return *m_document; }
266
267     // FIXME: This likely belongs on RuleSet.
268     typedef WillBeHeapHashMap<StringImpl*, RefPtrWillBeMember<StyleRuleKeyframes> > KeyframesRuleMap;
269     KeyframesRuleMap m_keyframesRuleMap;
270
271     static RenderStyle* s_styleNotYetAvailable;
272
273     void cacheBorderAndBackground();
274
275     MatchedPropertiesCache m_matchedPropertiesCache;
276
277     OwnPtr<MediaQueryEvaluator> m_medium;
278     MediaQueryResultList m_viewportDependentMediaQueryResults;
279
280     RawPtrWillBeMember<Document> m_document;
281     SelectorFilter m_selectorFilter;
282
283     OwnPtrWillBeMember<ViewportStyleResolver> m_viewportStyleResolver;
284
285     WillBeHeapListHashSet<RawPtrWillBeMember<CSSStyleSheet>, 16> m_pendingStyleSheets;
286
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;
294
295     bool m_needCollectFeatures;
296     bool m_printMediaType;
297
298     StyleResourceLoader m_styleResourceLoader;
299
300     unsigned m_styleSharingDepth;
301     WillBeHeapVector<OwnPtrWillBeMember<StyleSharingList>, styleSharingMaxDepth> m_styleSharingLists;
302
303     OwnPtr<StyleResolverStats> m_styleResolverStats;
304     OwnPtr<StyleResolverStats> m_styleResolverStatsTotals;
305     unsigned m_styleResolverStatsSequence;
306
307     // Use only for Internals::updateStyleAndReturnAffectedElementCount.
308     unsigned m_accessCount;
309 };
310
311 } // namespace blink
312
313 #endif // StyleResolver_h