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 ElementRuleCollector_h
23#define ElementRuleCollector_h
24
25#include "MediaQueryEvaluator.h"
26#include "SelectorChecker.h"
27#include "StyleResolver.h"
28#include <wtf/RefPtr.h>
29#include <wtf/Vector.h>
30
31namespace WebCore {
32
33class DocumentRuleSets;
34class RenderRegion;
35class RuleData;
36class RuleSet;
37class SelectorFilter;
38class StyleScopeResolver;
39
40class ElementRuleCollector {
41public:
42    ElementRuleCollector(StyleResolver* styleResolver, const StyleResolver::State& state)
43        : m_state(state)
44        , m_ruleSets(styleResolver->ruleSets())
45        , m_selectorFilter(styleResolver->selectorFilter())
46        , m_inspectorCSSOMWrappers(styleResolver->inspectorCSSOMWrappers())
47        , m_scopeResolver(styleResolver->scopeResolver())
48        , m_isPrintStyle(false)
49        , m_regionForStyling(0)
50        , m_pseudoStyleRequest(NOPSEUDO)
51        , m_sameOriginOnly(false)
52        , m_mode(SelectorChecker::ResolvingStyle)
53        , m_canUseFastReject(m_selectorFilter.parentStackIsConsistent(state.parentNode()))
54        , m_behaviorAtBoundary(SelectorChecker::DoesNotCrossBoundary) { }
55
56    void matchAllRules(bool matchAuthorAndUserStyles, bool includeSMILProperties);
57    void matchUARules();
58    void matchAuthorRules(bool includeEmptyRules);
59    void matchUserRules(bool includeEmptyRules);
60
61    void setMode(SelectorChecker::Mode mode) { m_mode = mode; }
62    void setPseudoStyleRequest(const PseudoStyleRequest& request) { m_pseudoStyleRequest = request; }
63    void setSameOriginOnly(bool f) { m_sameOriginOnly = f; }
64    void setRegionForStyling(RenderRegion* regionForStyling) { m_regionForStyling = regionForStyling; }
65    void setMedium(const MediaQueryEvaluator* medium) { m_isPrintStyle = medium->mediaTypeMatchSpecific("print"); }
66
67    bool hasAnyMatchingRules(RuleSet*);
68
69    StyleResolver::MatchResult& matchedResult();
70    const Vector<RefPtr<StyleRuleBase> >& matchedRuleList() const;
71
72private:
73    Document* document() { return m_state.document(); }
74    void addElementStyleProperties(const StylePropertySet*, bool isCacheable = true);
75
76    void matchUARules(RuleSet*);
77    void matchScopedAuthorRules(bool includeEmptyRules);
78    void matchHostRules(bool includeEmptyRules);
79
80    void collectMatchingRules(const MatchRequest&, StyleResolver::RuleRange&);
81    void collectMatchingRulesForRegion(const MatchRequest&, StyleResolver::RuleRange&);
82    void collectMatchingRulesForList(const Vector<RuleData>*, const MatchRequest&, StyleResolver::RuleRange&);
83    bool ruleMatches(const RuleData&, const ContainerNode* scope, PseudoId&);
84
85    void sortMatchedRules();
86    void sortAndTransferMatchedRules();
87
88    void addMatchedRule(const RuleData*);
89    void clearMatchedRules();
90
91private:
92    template<bool hasInspectorFrontends>
93    void doCollectMatchingRulesForList(const Vector<RuleData>*, const MatchRequest&, StyleResolver::RuleRange&);
94
95    const StyleResolver::State& m_state;
96    DocumentRuleSets& m_ruleSets;
97    SelectorFilter& m_selectorFilter;
98    InspectorCSSOMWrappers& m_inspectorCSSOMWrappers;
99    StyleScopeResolver* m_scopeResolver;
100
101    bool m_isPrintStyle;
102    RenderRegion* m_regionForStyling;
103    PseudoStyleRequest m_pseudoStyleRequest;
104    bool m_sameOriginOnly;
105    SelectorChecker::Mode m_mode;
106    bool m_canUseFastReject;
107    SelectorChecker::BehaviorAtBoundary m_behaviorAtBoundary;
108
109    OwnPtr<Vector<const RuleData*, 32> > m_matchedRules;
110
111    // Output.
112    Vector<RefPtr<StyleRuleBase> > m_matchedRuleList;
113    StyleResolver::MatchResult m_result;
114};
115
116} // namespace WebCore
117
118#endif // ElementRuleCollector_h
119