Upstream version 5.34.104.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 "wtf/Deque.h"
41 #include "wtf/HashMap.h"
42 #include "wtf/HashSet.h"
43 #include "wtf/ListHashSet.h"
44 #include "wtf/RefPtr.h"
45 #include "wtf/Vector.h"
46
47 namespace WebCore {
48
49 class CSSAnimationUpdate;
50 class CSSFontSelector;
51 class CSSRuleList;
52 class CSSSelector;
53 class CSSStyleSheet;
54 class CSSValue;
55 class ContainerNode;
56 class Document;
57 class DocumentTimeline;
58 class Element;
59 class ElementRuleCollector;
60 class KeyframeList;
61 class KeyframeValue;
62 class MediaQueryEvaluator;
63 class MediaQueryResult;
64 class RuleData;
65 class Settings;
66 class StyleKeyframe;
67 class StylePropertySet;
68 class StyleResolverStats;
69 class StyleRule;
70 class StyleRuleKeyframes;
71 class StyleRulePage;
72 class ViewportStyleResolver;
73
74 struct MatchResult;
75
76 enum StyleSharingBehavior {
77     AllowStyleSharing,
78     DisallowStyleSharing,
79 };
80
81 // MatchOnlyUserAgentRules is used in media queries, where relative units
82 // are interpreted according to the document root element style, and styled only
83 // from the User Agent Stylesheet rules.
84 enum RuleMatchingBehavior {
85     MatchAllRules,
86     MatchAllRulesExcludingSMIL,
87     MatchOnlyUserAgentRules,
88 };
89
90 const unsigned styleSharingListSize = 40;
91 typedef WTF::Deque<Element*, styleSharingListSize> StyleSharingList;
92
93 struct CSSPropertyValue {
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     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;
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 PassRefPtr<KeyframeEffectModel> createKeyframeEffectModel(Element&, const Vector<RefPtr<MutableStylePropertySet> >&, KeyframeEffectModel::KeyframeVector&);
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&, CSSFontSelector* = 0);
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(unsigned firstNew, const Vector<RefPtr<CSSStyleSheet> >&);
139     void resetAuthorStyle(const ContainerNode*);
140     void finishAppendAuthorStyleSheets();
141
142     TreeBoundaryCrossingRules& treeBoundaryCrossingRules() { return m_treeBoundaryCrossingRules; }
143     void processScopedRules(const RuleSet& authorRules, const KURL&, ContainerNode* scope = 0);
144
145     void lazyAppendAuthorStyleSheets(unsigned firstNew, const Vector<RefPtr<CSSStyleSheet> >&);
146     void removePendingAuthorStyleSheets(const Vector<RefPtr<CSSStyleSheet> >&);
147     void appendPendingAuthorStyleSheets();
148     bool hasPendingAuthorStyleSheets() const { return m_pendingStyleSheets.size() > 0 || m_needCollectFeatures; }
149
150     SelectorFilter& selectorFilter() { return m_selectorFilter; }
151
152     void setBuildScopedStyleTreeInDocumentOrder(bool enabled) { m_styleTree.setBuildInDocumentOrder(enabled); }
153     bool buildScopedStyleTreeInDocumentOrder() const { return m_styleTree.buildInDocumentOrder(); }
154     bool styleTreeHasOnlyScopedResolverForDocument() const { return m_styleTree.hasOnlyScopedResolverForDocument(); }
155     ScopedStyleResolver* styleTreeScopedStyleResolverForDocument() const { return m_styleTree.scopedStyleResolverForDocument(); }
156
157     ScopedStyleResolver* ensureScopedStyleResolver(ContainerNode* scope)
158     {
159         ASSERT(scope);
160         return m_styleTree.ensureScopedStyleResolver(*scope);
161     }
162
163     void styleTreeResolveScopedKeyframesRules(const Element* element, Vector<ScopedStyleResolver*, 8>& resolvers)
164     {
165         m_styleTree.resolveScopedKeyframesRules(element, resolvers);
166     }
167
168     // These methods will give back the set of rules that matched for a given element (or a pseudo-element).
169     enum CSSRuleFilter {
170         UAAndUserCSSRules   = 1 << 1,
171         AuthorCSSRules      = 1 << 2,
172         EmptyCSSRules       = 1 << 3,
173         CrossOriginCSSRules = 1 << 4,
174         AllButEmptyCSSRules = UAAndUserCSSRules | AuthorCSSRules | CrossOriginCSSRules,
175         AllCSSRules         = AllButEmptyCSSRules | EmptyCSSRules,
176     };
177     PassRefPtr<CSSRuleList> cssRulesForElement(Element*, unsigned rulesToInclude = AllButEmptyCSSRules);
178     PassRefPtr<CSSRuleList> pseudoCSSRulesForElement(Element*, PseudoId, unsigned rulesToInclude = AllButEmptyCSSRules);
179     PassRefPtr<StyleRuleList> styleRulesForElement(Element*, unsigned rulesToInclude);
180
181     // |properties| is an array with |count| elements.
182     void applyPropertiesToStyle(const CSSPropertyValue* properties, size_t count, RenderStyle*);
183
184     ViewportStyleResolver* viewportStyleResolver() { return m_viewportStyleResolver.get(); }
185
186     void addMediaQueryResults(const MediaQueryResultList&);
187     MediaQueryResultList* viewportDependentMediaQueryResults() { return &m_viewportDependentMediaQueryResults; }
188     bool hasViewportDependentMediaQueries() const { return !m_viewportDependentMediaQueryResults.isEmpty(); }
189     bool mediaQueryAffectedByViewportChange() const;
190
191     // FIXME: Rename to reflect the purpose, like didChangeFontSize or something.
192     void invalidateMatchedPropertiesCache();
193
194     void notifyResizeForViewportUnits();
195
196     // Exposed for RenderStyle::isStyleAvilable().
197     static RenderStyle* styleNotYetAvailable() { return s_styleNotYetAvailable; }
198
199     RuleFeatureSet& ensureRuleFeatureSet()
200     {
201         if (hasPendingAuthorStyleSheets())
202             appendPendingAuthorStyleSheets();
203         return m_features;
204     }
205
206     StyleSharingList& styleSharingList() { return m_styleSharingList; }
207
208     bool hasRulesForId(const AtomicString&) const;
209
210     void addToStyleSharingList(Element&);
211     void clearStyleSharingList();
212
213     StyleResolverStats* stats() { return m_styleResolverStats.get(); }
214     StyleResolverStats* statsTotals() { return m_styleResolverStatsTotals.get(); }
215     enum StatsReportType { ReportDefaultStats, ReportSlowStats };
216     void enableStats(StatsReportType = ReportDefaultStats);
217     void disableStats();
218     void printStats();
219
220     unsigned accessCount() const { return m_accessCount; }
221     void didAccess() { ++m_accessCount; }
222
223     PassRefPtr<PseudoElement> createPseudoElementIfNeeded(Element& parent, PseudoId);
224
225 private:
226     // CSSFontSelectorClient implementation.
227     virtual void fontsNeedUpdate(CSSFontSelector*) OVERRIDE;
228
229 private:
230     void initWatchedSelectorRules(const Vector<RefPtr<StyleRule> >& watchedSelectors);
231
232     void addTreeBoundaryCrossingRules(const Vector<MinimalRuleData>&, ContainerNode* scope);
233
234     // FIXME: This should probably go away, folded into FontBuilder.
235     void updateFont(StyleResolverState&);
236
237     void appendCSSStyleSheet(CSSStyleSheet*);
238
239     void collectPseudoRulesForElement(Element*, ElementRuleCollector&, PseudoId, unsigned rulesToInclude);
240     void matchUARules(ElementRuleCollector&, RuleSet*);
241     void matchAuthorRules(Element*, ElementRuleCollector&, bool includeEmptyRules);
242     void matchAuthorRulesForShadowHost(Element*, ElementRuleCollector&, bool includeEmptyRules, Vector<ScopedStyleResolver*, 8>& resolvers, Vector<ScopedStyleResolver*, 8>& resolversInShadowTree);
243     void matchAllRules(StyleResolverState&, ElementRuleCollector&, bool includeSMILProperties);
244     void matchUARules(ElementRuleCollector&);
245     // FIXME: watched selectors should be implemented using injected author stylesheets: http://crbug.com/316960
246     void matchWatchSelectorRules(ElementRuleCollector&);
247     void collectFeatures();
248     void collectTreeBoundaryCrossingRules(Element*, ElementRuleCollector&, bool includeEmptyRules);
249     void resetRuleFeatures();
250
251     bool fastRejectSelector(const RuleData&) const;
252
253     void applyMatchedProperties(StyleResolverState&, const MatchResult&);
254     void applyAnimatedProperties(StyleResolverState&, Element* animatingElement);
255
256     enum StyleApplicationPass {
257         AnimationProperties,
258         HighPriorityProperties,
259         LowPriorityProperties
260     };
261     template <StyleResolver::StyleApplicationPass pass>
262     static inline bool isPropertyForPass(CSSPropertyID);
263     template <StyleApplicationPass pass>
264     void applyMatchedProperties(StyleResolverState&, const MatchResult&, bool important, int startIndex, int endIndex, bool inheritedOnly);
265     template <StyleApplicationPass pass>
266     void applyProperties(StyleResolverState&, const StylePropertySet* properties, StyleRule*, bool isImportant, bool inheritedOnly, PropertyWhitelistType = PropertyWhitelistNone);
267     template <StyleApplicationPass pass>
268     void applyAnimatedProperties(StyleResolverState&, const AnimationEffect::CompositableValueMap&);
269     void matchPageRules(MatchResult&, RuleSet*, bool isLeftPage, bool isFirstPage, const String& pageName);
270     void matchPageRulesForList(Vector<StyleRulePage*>& matchedRules, const Vector<StyleRulePage*>&, bool isLeftPage, bool isFirstPage, const String& pageName);
271     void collectViewportRules();
272     Settings* documentSettings() { return m_document.settings(); }
273
274     bool isLeftPage(int pageIndex) const;
275     bool isRightPage(int pageIndex) const { return !isLeftPage(pageIndex); }
276     bool isFirstPage(int pageIndex) const;
277     String pageName(int pageIndex) const;
278
279     bool pseudoStyleForElementInternal(Element&, const PseudoStyleRequest&, RenderStyle* parentStyle, StyleResolverState&);
280
281     // FIXME: This likely belongs on RuleSet.
282     typedef HashMap<StringImpl*, RefPtr<StyleRuleKeyframes> > KeyframesRuleMap;
283     KeyframesRuleMap m_keyframesRuleMap;
284
285     static RenderStyle* s_styleNotYetAvailable;
286
287     void cacheBorderAndBackground();
288
289     MatchedPropertiesCache m_matchedPropertiesCache;
290
291     OwnPtr<MediaQueryEvaluator> m_medium;
292     MediaQueryResultList m_viewportDependentMediaQueryResults;
293
294     RefPtr<RenderStyle> m_rootDefaultStyle;
295
296     Document& m_document;
297     SelectorFilter m_selectorFilter;
298
299     RefPtr<ViewportStyleResolver> m_viewportStyleResolver;
300
301     ListHashSet<CSSStyleSheet*, 16> m_pendingStyleSheets;
302
303     ScopedStyleTree m_styleTree;
304
305     // FIXME: The entire logic of collecting features on StyleResolver, as well as transferring them
306     // between various parts of machinery smells wrong. This needs to be better somehow.
307     RuleFeatureSet m_features;
308     OwnPtr<RuleSet> m_siblingRuleSet;
309     OwnPtr<RuleSet> m_uncommonAttributeRuleSet;
310
311     // FIXME: watched selectors should be implemented using injected author stylesheets: http://crbug.com/316960
312     OwnPtr<RuleSet> m_watchedSelectorsRules;
313     TreeBoundaryCrossingRules m_treeBoundaryCrossingRules;
314
315     bool m_needCollectFeatures;
316
317     StyleResourceLoader m_styleResourceLoader;
318
319     StyleSharingList m_styleSharingList;
320
321     OwnPtr<StyleResolverStats> m_styleResolverStats;
322     OwnPtr<StyleResolverStats> m_styleResolverStatsTotals;
323     unsigned m_styleResolverStatsSequence;
324
325     // Use only for Internals::updateStyleAndReturnAffectedElementCount.
326     unsigned m_accessCount;
327 };
328
329 } // namespace WebCore
330
331 #endif // StyleResolver_h