• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "core/css/PseudoStyleRequest.h"
26 #include "core/css/SelectorChecker.h"
27 #include "core/css/resolver/ElementResolveContext.h"
28 #include "core/css/resolver/MatchRequest.h"
29 #include "core/css/resolver/MatchResult.h"
30 #include "wtf/RefPtr.h"
31 #include "wtf/Vector.h"
32 
33 namespace blink {
34 
35 class CSSStyleSheet;
36 class CSSRuleList;
37 class RuleData;
38 class RuleSet;
39 class SelectorFilter;
40 class StaticCSSRuleList;
41 
42 typedef unsigned CascadeScope;
43 typedef unsigned CascadeOrder;
44 
45 const CascadeScope ignoreCascadeScope = 0;
46 const CascadeOrder ignoreCascadeOrder = 0;
47 
48 class MatchedRule {
49     ALLOW_ONLY_INLINE_ALLOCATION();
50 public:
MatchedRule(const RuleData * ruleData,unsigned specificity,CascadeScope cascadeScope,CascadeOrder cascadeOrder,unsigned styleSheetIndex,const CSSStyleSheet * parentStyleSheet)51     MatchedRule(const RuleData* ruleData, unsigned specificity, CascadeScope cascadeScope, CascadeOrder cascadeOrder, unsigned styleSheetIndex, const CSSStyleSheet* parentStyleSheet)
52         : m_ruleData(ruleData)
53         , m_specificity(specificity)
54         , m_cascadeScope(cascadeScope)
55         , m_parentStyleSheet(parentStyleSheet)
56     {
57         ASSERT(m_ruleData);
58         static const unsigned BitsForPositionInRuleData = 18;
59         static const unsigned BitsForStyleSheetIndex = 32;
60         m_position = ((uint64_t)cascadeOrder << (BitsForStyleSheetIndex + BitsForPositionInRuleData)) + ((uint64_t)styleSheetIndex << BitsForPositionInRuleData)+ m_ruleData->position();
61     }
62 
ruleData()63     const RuleData* ruleData() const { return m_ruleData; }
cascadeScope()64     uint32_t cascadeScope() const { return m_cascadeScope; }
position()65     uint64_t position() const { return m_position; }
specificity()66     unsigned specificity() const { return ruleData()->specificity() + m_specificity; }
parentStyleSheet()67     const CSSStyleSheet* parentStyleSheet() const { return m_parentStyleSheet; }
trace(Visitor * visitor)68     void trace(Visitor* visitor)
69     {
70         visitor->trace(m_parentStyleSheet);
71     }
72 
73 private:
74     // FIXME: Oilpan: RuleData is in the oilpan heap and this pointer
75     // really should be traced. However, RuleData objects are
76     // allocated inside larger TerminatedArray objects and we cannot
77     // trace a raw rule data pointer at this point.
78     const RuleData* m_ruleData;
79     unsigned m_specificity;
80     CascadeScope m_cascadeScope;
81     uint64_t m_position;
82     RawPtrWillBeMember<const CSSStyleSheet> m_parentStyleSheet;
83 };
84 
85 } // namespace blink
86 
87 WTF_ALLOW_MOVE_AND_INIT_WITH_MEM_FUNCTIONS(blink::MatchedRule);
88 
89 namespace blink {
90 
91 // FIXME: oilpan: when transition types are gone this class can be replaced with HeapVector.
92 class StyleRuleList FINAL : public RefCountedWillBeGarbageCollected<StyleRuleList> {
93 public:
create()94     static PassRefPtrWillBeRawPtr<StyleRuleList> create() { return adoptRefWillBeNoop(new StyleRuleList()); }
95 
trace(Visitor * visitor)96     void trace(Visitor* visitor)
97     {
98 #if ENABLE(OILPAN)
99         visitor->trace(m_list);
100 #endif
101     }
102 
103     WillBeHeapVector<RawPtrWillBeMember<StyleRule> > m_list;
104 };
105 
106 // ElementRuleCollector is designed to be used as a stack object.
107 // Create one, ask what rules the ElementResolveContext matches
108 // and then let it go out of scope.
109 // FIXME: Currently it modifies the RenderStyle but should not!
110 class ElementRuleCollector {
111     STACK_ALLOCATED();
112     WTF_MAKE_NONCOPYABLE(ElementRuleCollector);
113 public:
114     ElementRuleCollector(const ElementResolveContext&, const SelectorFilter&, RenderStyle* = 0);
115     ~ElementRuleCollector();
116 
setMode(SelectorChecker::Mode mode)117     void setMode(SelectorChecker::Mode mode) { m_mode = mode; }
setPseudoStyleRequest(const PseudoStyleRequest & request)118     void setPseudoStyleRequest(const PseudoStyleRequest& request) { m_pseudoStyleRequest = request; }
setSameOriginOnly(bool f)119     void setSameOriginOnly(bool f) { m_sameOriginOnly = f; }
120 
setMatchingUARules(bool matchingUARules)121     void setMatchingUARules(bool matchingUARules) { m_matchingUARules = matchingUARules; }
122     bool hasAnyMatchingRules(RuleSet*);
123 
124     MatchResult& matchedResult();
125     PassRefPtrWillBeRawPtr<StyleRuleList> matchedStyleRuleList();
126     PassRefPtrWillBeRawPtr<CSSRuleList> matchedCSSRuleList();
127 
128     void collectMatchingRules(const MatchRequest&, RuleRange&, SelectorChecker::ContextFlags = SelectorChecker::DefaultBehavior, CascadeScope = ignoreCascadeScope, CascadeOrder = ignoreCascadeOrder, bool matchingTreeBoundaryRules = false);
129     void sortAndTransferMatchedRules();
130     void clearMatchedRules();
131     void addElementStyleProperties(const StylePropertySet*, bool isCacheable = true);
132 
133 private:
134     void collectRuleIfMatches(const RuleData&, SelectorChecker::ContextFlags, CascadeScope, CascadeOrder, const MatchRequest&, RuleRange&);
135 
136     template<typename RuleDataListType>
collectMatchingRulesForList(const RuleDataListType * rules,SelectorChecker::ContextFlags contextFlags,CascadeScope cascadeScope,CascadeOrder cascadeOrder,const MatchRequest & matchRequest,RuleRange & ruleRange)137     void collectMatchingRulesForList(const RuleDataListType* rules, SelectorChecker::ContextFlags contextFlags, CascadeScope cascadeScope, CascadeOrder cascadeOrder, const MatchRequest& matchRequest, RuleRange& ruleRange)
138     {
139         if (!rules)
140             return;
141 
142         for (typename RuleDataListType::const_iterator it = rules->begin(), end = rules->end(); it != end; ++it)
143             collectRuleIfMatches(*it, contextFlags, cascadeScope, cascadeOrder, matchRequest, ruleRange);
144     }
145 
146     bool ruleMatches(const RuleData&, const ContainerNode* scope, SelectorChecker::ContextFlags, SelectorChecker::MatchResult*);
147 
148     CSSRuleList* nestedRuleList(CSSRule*);
149     template<class CSSRuleCollection>
150     CSSRule* findStyleRule(CSSRuleCollection*, StyleRule*);
151     void appendCSSOMWrapperForRule(CSSStyleSheet*, StyleRule*);
152 
153     void sortMatchedRules();
154     void addMatchedRule(const RuleData*, unsigned specificity, CascadeScope, CascadeOrder, unsigned styleSheetIndex, const CSSStyleSheet* parentStyleSheet);
155 
156     StaticCSSRuleList* ensureRuleList();
157     StyleRuleList* ensureStyleRuleList();
158 
159 private:
160     const ElementResolveContext& m_context;
161     const SelectorFilter& m_selectorFilter;
162     RefPtr<RenderStyle> m_style; // FIXME: This can be mutated during matching!
163 
164     PseudoStyleRequest m_pseudoStyleRequest;
165     SelectorChecker::Mode m_mode;
166     bool m_canUseFastReject;
167     bool m_sameOriginOnly;
168     bool m_matchingUARules;
169 
170     OwnPtrWillBeMember<WillBeHeapVector<MatchedRule, 32> > m_matchedRules;
171 
172     // Output.
173     RefPtrWillBeMember<StaticCSSRuleList> m_cssRuleList;
174     RefPtrWillBeMember<StyleRuleList> m_styleRuleList;
175     MatchResult m_result;
176 };
177 
178 } // namespace blink
179 
180 #endif // ElementRuleCollector_h
181