Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / css / RuleFeature.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 RuleFeature_h
23 #define RuleFeature_h
24
25 #include "core/css/invalidation/DescendantInvalidationSet.h"
26 #include "wtf/Forward.h"
27 #include "wtf/HashSet.h"
28 #include "wtf/text/AtomicStringHash.h"
29
30 namespace WebCore {
31
32 class CSSSelector;
33 class CSSSelectorList;
34 class Document;
35 class Node;
36 class QualifiedName;
37 class RuleData;
38 class ShadowRoot;
39 class SpaceSplitString;
40 class StyleRule;
41
42 struct RuleFeature {
43     RuleFeature(StyleRule* rule, unsigned selectorIndex, bool hasDocumentSecurityOrigin)
44         : rule(rule)
45         , selectorIndex(selectorIndex)
46         , hasDocumentSecurityOrigin(hasDocumentSecurityOrigin)
47     {
48     }
49     StyleRule* rule;
50     unsigned selectorIndex;
51     bool hasDocumentSecurityOrigin;
52 };
53
54 class RuleFeatureSet {
55 public:
56     RuleFeatureSet();
57
58     void add(const RuleFeatureSet&);
59     void clear();
60
61     void collectFeaturesFromSelector(const CSSSelector&);
62     void collectFeaturesFromRuleData(const RuleData&);
63
64     bool usesSiblingRules() const { return !siblingRules.isEmpty(); }
65     bool usesFirstLineRules() const { return m_metadata.usesFirstLineRules; }
66
67     unsigned maxDirectAdjacentSelectors() const { return m_metadata.maxDirectAdjacentSelectors; }
68     void setMaxDirectAdjacentSelectors(unsigned value)  { m_metadata.maxDirectAdjacentSelectors = std::max(value, m_metadata.maxDirectAdjacentSelectors); }
69
70     inline bool hasSelectorForAttribute(const AtomicString& attributeName) const
71     {
72         ASSERT(!attributeName.isEmpty());
73         return m_attributeInvalidationSets.get(attributeName);
74     }
75
76     inline bool hasSelectorForClass(const AtomicString& classValue) const
77     {
78         ASSERT(!classValue.isEmpty());
79         return m_classInvalidationSets.get(classValue);
80     }
81
82     inline bool hasSelectorForId(const AtomicString& idValue) const
83     {
84         return m_metadata.idsInRules.contains(idValue);
85     }
86
87     void scheduleStyleInvalidationForClassChange(const SpaceSplitString& changedClasses, Element*);
88     void scheduleStyleInvalidationForClassChange(const SpaceSplitString& oldClasses, const SpaceSplitString& newClasses, Element*);
89
90     void scheduleStyleInvalidationForAttributeChange(const QualifiedName& attributeName, Element*);
91
92     // Clears all style invalidation state for the passed node.
93     void clearStyleInvalidation(Node*);
94
95     int hasIdsInSelectors() const
96     {
97         return m_metadata.idsInRules.size() > 0;
98     }
99
100     // Marks the given attribute name as "appearing in a selector". Used for
101     // CSS properties such as content: ... attr(...) ...
102     // FIXME: record these internally to this class instead calls from StyleResolver to here.
103     void addContentAttr(const AtomicString& attributeName);
104
105     Vector<RuleFeature> siblingRules;
106     Vector<RuleFeature> uncommonAttributeRules;
107
108     typedef Vector<RefPtr<DescendantInvalidationSet> > InvalidationList;
109     typedef HashMap<Element*, OwnPtr<InvalidationList> > PendingInvalidationMap;
110
111     PendingInvalidationMap& pendingInvalidationMap();
112
113 private:
114     typedef HashMap<AtomicString, RefPtr<DescendantInvalidationSet> > InvalidationSetMap;
115
116     struct FeatureMetadata {
117         FeatureMetadata()
118             : usesFirstLineRules(false)
119             , foundSiblingSelector(false)
120             , maxDirectAdjacentSelectors(0)
121         { }
122         void add(const FeatureMetadata& other);
123         void clear();
124
125         bool usesFirstLineRules;
126         bool foundSiblingSelector;
127         unsigned maxDirectAdjacentSelectors;
128         HashSet<AtomicString> idsInRules;
129     };
130
131     enum InvalidationSetMode {
132         AddFeatures,
133         UseLocalStyleChange,
134         UseSubtreeStyleChange
135     };
136
137     static InvalidationSetMode invalidationSetModeForSelector(const CSSSelector&);
138
139     void collectFeaturesFromSelector(const CSSSelector&, FeatureMetadata&, InvalidationSetMode);
140     void collectFeaturesFromSelectorList(const CSSSelectorList*, FeatureMetadata&, InvalidationSetMode);
141
142     DescendantInvalidationSet& ensureClassInvalidationSet(const AtomicString& className);
143     DescendantInvalidationSet& ensureAttributeInvalidationSet(const AtomicString& attributeName);
144     DescendantInvalidationSet* invalidationSetForSelector(const CSSSelector&);
145
146     InvalidationSetMode updateInvalidationSets(const CSSSelector&);
147
148     struct InvalidationSetFeatures {
149         Vector<AtomicString> classes;
150         Vector<AtomicString> attributes;
151         AtomicString id;
152         AtomicString tagName;
153     };
154
155     static void extractInvalidationSetFeature(const CSSSelector&, InvalidationSetFeatures&);
156     const CSSSelector* extractInvalidationSetFeatures(const CSSSelector&, InvalidationSetFeatures&);
157     void addFeaturesToInvalidationSets(const CSSSelector&, const InvalidationSetFeatures&);
158
159     void addClassToInvalidationSet(const AtomicString& className, Element*);
160
161     InvalidationList& ensurePendingInvalidationList(Element*);
162
163     FeatureMetadata m_metadata;
164     InvalidationSetMap m_classInvalidationSets;
165     InvalidationSetMap m_attributeInvalidationSets;
166
167     PendingInvalidationMap m_pendingInvalidationMap;
168
169     bool m_targetedStyleRecalcEnabled;
170 };
171
172
173 } // namespace WebCore
174
175 #endif // RuleFeature_h