Upstream version 7.36.149.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/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"
47
48 namespace WebCore {
49
50 class CSSAnimationUpdate;
51 class CSSFontSelector;
52 class CSSRuleList;
53 class CSSSelector;
54 class CSSStyleSheet;
55 class CSSValue;
56 class ContainerNode;
57 class Document;
58 class DocumentTimeline;
59 class Element;
60 class ElementRuleCollector;
61 class KeyframeList;
62 class KeyframeValue;
63 class MediaQueryEvaluator;
64 class MediaQueryResult;
65 class RuleData;
66 class Settings;
67 class StyleKeyframe;
68 class StylePropertySet;
69 class StyleResolverStats;
70 class StyleRule;
71 class StyleRuleKeyframes;
72 class StyleRulePage;
73 class ViewportStyleResolver;
74
75 class MatchResult;
76
77 enum StyleSharingBehavior {
78     AllowStyleSharing,
79     DisallowStyleSharing,
80 };
81
82 enum RuleMatchingBehavior {
83     MatchAllRules,
84     MatchAllRulesExcludingSMIL
85 };
86
87 const unsigned styleSharingListSize = 15;
88 const unsigned styleSharingMaxDepth = 32;
89 typedef WTF::Deque<Element*, styleSharingListSize> StyleSharingList;
90
91 struct CSSPropertyValue {
92     STACK_ALLOCATED();
93 public:
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;
100 };
101
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;
105 public:
106     explicit StyleResolver(Document&);
107     virtual ~StyleResolver();
108
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&);
117
118     PassRefPtr<RenderStyle> styleForElement(Element*, RenderStyle* parentStyle = 0, StyleSharingBehavior = AllowStyleSharing,
119         RuleMatchingBehavior = MatchAllRules);
120
121     PassRefPtr<RenderStyle> styleForKeyframe(Element*, const RenderStyle&, RenderStyle* parentStyle, const StyleKeyframe*, const AtomicString& animationName);
122     static PassRefPtrWillBeRawPtr<AnimatableValue> createAnimatableValueSnapshot(Element&, CSSPropertyID, CSSValue*);
123
124     PassRefPtr<RenderStyle> pseudoStyleForElement(Element*, const PseudoStyleRequest&, RenderStyle* parentStyle);
125
126     PassRefPtr<RenderStyle> styleForPage(int pageIndex);
127     PassRefPtr<RenderStyle> defaultStyleForElement();
128     PassRefPtr<RenderStyle> styleForText(Text*);
129
130     static PassRefPtr<RenderStyle> styleForDocument(Document&);
131
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; }
135
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();
141
142     void processScopedRules(const RuleSet& authorRules, CSSStyleSheet*, ContainerNode& scope);
143
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; }
148
149     SelectorFilter& selectorFilter() { return m_selectorFilter; }
150
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(); }
155
156     ScopedStyleResolver* ensureScopedStyleResolver(ContainerNode* scope)
157     {
158         ASSERT(scope);
159         return m_styleTree.ensureScopedStyleResolver(*scope);
160     }
161
162     void styleTreeResolveScopedKeyframesRules(const Element* element, Vector<ScopedStyleResolver*, 8>& resolvers)
163     {
164         m_styleTree.resolveScopedKeyframesRules(element, resolvers);
165     }
166
167     // These methods will give back the set of rules that matched for a given element (or a pseudo-element).
168     enum CSSRuleFilter {
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,
175     };
176     PassRefPtrWillBeRawPtr<CSSRuleList> cssRulesForElement(Element*, unsigned rulesToInclude = AllButEmptyCSSRules);
177     PassRefPtrWillBeRawPtr<CSSRuleList> pseudoCSSRulesForElement(Element*, PseudoId, unsigned rulesToInclude = AllButEmptyCSSRules);
178     PassRefPtrWillBeRawPtr<StyleRuleList> styleRulesForElement(Element*, unsigned rulesToInclude);
179
180     // |properties| is an array with |count| elements.
181     void applyPropertiesToStyle(const CSSPropertyValue* properties, size_t count, RenderStyle*);
182
183     ViewportStyleResolver* viewportStyleResolver() { return m_viewportStyleResolver.get(); }
184
185     void addMediaQueryResults(const MediaQueryResultList&);
186     MediaQueryResultList* viewportDependentMediaQueryResults() { return &m_viewportDependentMediaQueryResults; }
187     bool hasViewportDependentMediaQueries() const { return !m_viewportDependentMediaQueryResults.isEmpty(); }
188     bool mediaQueryAffectedByViewportChange() const;
189
190     // FIXME: Rename to reflect the purpose, like didChangeFontSize or something.
191     void invalidateMatchedPropertiesCache();
192
193     void notifyResizeForViewportUnits();
194
195     // Exposed for RenderStyle::isStyleAvilable().
196     static RenderStyle* styleNotYetAvailable() { return s_styleNotYetAvailable; }
197
198     RuleFeatureSet& ensureUpdatedRuleFeatureSet()
199     {
200         if (hasPendingAuthorStyleSheets())
201             appendPendingAuthorStyleSheets();
202         return m_features;
203     }
204
205     RuleFeatureSet& ruleFeatureSet()
206     {
207         return m_features;
208     }
209
210     StyleSharingList& styleSharingList();
211
212     bool hasRulesForId(const AtomicString&) const;
213
214     void addToStyleSharingList(Element&);
215     void clearStyleSharingList();
216
217     StyleResolverStats* stats() { return m_styleResolverStats.get(); }
218     StyleResolverStats* statsTotals() { return m_styleResolverStatsTotals.get(); }
219     enum StatsReportType { ReportDefaultStats, ReportSlowStats };
220     void enableStats(StatsReportType = ReportDefaultStats);
221     void disableStats();
222     void printStats();
223
224     unsigned accessCount() const { return m_accessCount; }
225     void didAccess() { ++m_accessCount; }
226
227     void increaseStyleSharingDepth() { ++m_styleSharingDepth; }
228     void decreaseStyleSharingDepth() { --m_styleSharingDepth; }
229
230     PassRefPtr<PseudoElement> createPseudoElementIfNeeded(Element& parent, PseudoId);
231
232     virtual void trace(Visitor*) OVERRIDE;
233
234 private:
235     // CSSFontSelectorClient implementation.
236     virtual void fontsNeedUpdate(CSSFontSelector*) OVERRIDE;
237
238 private:
239     void initWatchedSelectorRules(const WillBeHeapVector<RefPtrWillBeMember<StyleRule> >& watchedSelectors);
240
241     // FIXME: This should probably go away, folded into FontBuilder.
242     void updateFont(StyleResolverState&);
243
244     void loadPendingResources(StyleResolverState&);
245     void adjustRenderStyle(StyleResolverState&, Element*);
246
247     void appendCSSStyleSheet(CSSStyleSheet*);
248
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();
259
260     bool fastRejectSelector(const RuleData&) const;
261
262     void applyMatchedProperties(StyleResolverState&, const MatchResult&);
263     bool applyAnimatedProperties(StyleResolverState&, Element* animatingElement);
264
265     enum StyleApplicationPass {
266         AnimationProperties,
267         HighPriorityProperties,
268         LowPriorityProperties
269     };
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(); }
282
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;
287
288     bool pseudoStyleForElementInternal(Element&, const PseudoStyleRequest&, RenderStyle* parentStyle, StyleResolverState&);
289
290     // FIXME: This likely belongs on RuleSet.
291     typedef WillBeHeapHashMap<StringImpl*, RefPtrWillBeMember<StyleRuleKeyframes> > KeyframesRuleMap;
292     KeyframesRuleMap m_keyframesRuleMap;
293
294     static RenderStyle* s_styleNotYetAvailable;
295
296     void cacheBorderAndBackground();
297
298     MatchedPropertiesCache m_matchedPropertiesCache;
299
300     OwnPtr<MediaQueryEvaluator> m_medium;
301     MediaQueryResultList m_viewportDependentMediaQueryResults;
302
303     Document& m_document;
304     SelectorFilter m_selectorFilter;
305
306     OwnPtrWillBeMember<ViewportStyleResolver> m_viewportStyleResolver;
307
308     WillBeHeapListHashSet<RawPtrWillBeMember<CSSStyleSheet>, 16> m_pendingStyleSheets;
309
310     ScopedStyleTree m_styleTree;
311
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;
317
318     // FIXME: watched selectors should be implemented using injected author stylesheets: http://crbug.com/316960
319     OwnPtrWillBeMember<RuleSet> m_watchedSelectorsRules;
320     TreeBoundaryCrossingRules m_treeBoundaryCrossingRules;
321
322     bool m_needCollectFeatures;
323
324     StyleResourceLoader m_styleResourceLoader;
325
326     unsigned m_styleSharingDepth;
327     Vector<OwnPtr<StyleSharingList>, styleSharingMaxDepth> m_styleSharingLists;
328
329     OwnPtr<StyleResolverStats> m_styleResolverStats;
330     OwnPtr<StyleResolverStats> m_styleResolverStatsTotals;
331     unsigned m_styleResolverStatsSequence;
332
333     // Use only for Internals::updateStyleAndReturnAffectedElementCount.
334     unsigned m_accessCount;
335 };
336
337 } // namespace WebCore
338
339 #endif // StyleResolver_h