Upstream version 9.38.198.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/CSSSelector.h"
26 #include "core/css/invalidation/StyleInvalidator.h"
27 #include "wtf/Forward.h"
28 #include "wtf/HashSet.h"
29 #include "wtf/text/AtomicStringHash.h"
30
31 namespace blink {
32
33 class CSSSelectorList;
34 class DescendantInvalidationSet;
35 class Document;
36 class Node;
37 class QualifiedName;
38 class RuleData;
39 class ShadowRoot;
40 class SpaceSplitString;
41 class StyleRule;
42
43 struct RuleFeature {
44     ALLOW_ONLY_INLINE_ALLOCATION();
45 public:
46     RuleFeature(StyleRule* rule, unsigned selectorIndex, bool hasDocumentSecurityOrigin);
47
48     void trace(Visitor*);
49
50     RawPtrWillBeMember<StyleRule> rule;
51     unsigned selectorIndex;
52     bool hasDocumentSecurityOrigin;
53 };
54
55 class RuleFeatureSet {
56     DISALLOW_ALLOCATION();
57 public:
58     RuleFeatureSet();
59     ~RuleFeatureSet();
60
61     void add(const RuleFeatureSet&);
62     void clear();
63
64     void collectFeaturesFromSelector(const CSSSelector&);
65     void collectFeaturesFromRuleData(const RuleData&);
66
67     bool usesSiblingRules() const { return !siblingRules.isEmpty(); }
68     bool usesFirstLineRules() const { return m_metadata.usesFirstLineRules; }
69
70     unsigned maxDirectAdjacentSelectors() const { return m_metadata.maxDirectAdjacentSelectors; }
71     void setMaxDirectAdjacentSelectors(unsigned value)  { m_metadata.maxDirectAdjacentSelectors = std::max(value, m_metadata.maxDirectAdjacentSelectors); }
72
73     inline bool hasSelectorForAttribute(const AtomicString& attributeName) const
74     {
75         ASSERT(!attributeName.isEmpty());
76         return m_attributeInvalidationSets.contains(attributeName);
77     }
78
79     inline bool hasSelectorForClass(const AtomicString& classValue) const
80     {
81         ASSERT(!classValue.isEmpty());
82         return m_classInvalidationSets.contains(classValue);
83     }
84
85     inline bool hasSelectorForId(const AtomicString& idValue) const
86     {
87         return m_idInvalidationSets.contains(idValue);
88     }
89
90     void scheduleStyleInvalidationForClassChange(const SpaceSplitString& changedClasses, Element&);
91     void scheduleStyleInvalidationForClassChange(const SpaceSplitString& oldClasses, const SpaceSplitString& newClasses, Element&);
92     void scheduleStyleInvalidationForAttributeChange(const QualifiedName& attributeName, Element&);
93     void scheduleStyleInvalidationForIdChange(const AtomicString& oldId, const AtomicString& newId, Element&);
94     void scheduleStyleInvalidationForPseudoChange(CSSSelector::PseudoType, Element&);
95
96     bool hasIdsInSelectors() const
97     {
98         return m_idInvalidationSets.size() > 0;
99     }
100
101     // Marks the given attribute name as "appearing in a selector". Used for
102     // CSS properties such as content: ... attr(...) ...
103     // FIXME: record these internally to this class instead calls from StyleResolver to here.
104     void addContentAttr(const AtomicString& attributeName);
105
106     StyleInvalidator& styleInvalidator();
107
108     void trace(Visitor*);
109
110     WillBeHeapVector<RuleFeature> siblingRules;
111     WillBeHeapVector<RuleFeature> uncommonAttributeRules;
112
113 private:
114     typedef WillBeHeapHashMap<AtomicString, RefPtrWillBeMember<DescendantInvalidationSet> > InvalidationSetMap;
115     typedef WillBeHeapHashMap<CSSSelector::PseudoType, RefPtrWillBeMember<DescendantInvalidationSet>, WTF::IntHash<unsigned>, WTF::UnsignedWithZeroKeyHashTraits<unsigned> > PseudoTypeInvalidationSetMap;
116
117     struct FeatureMetadata {
118         FeatureMetadata()
119             : usesFirstLineRules(false)
120             , foundSiblingSelector(false)
121             , maxDirectAdjacentSelectors(0)
122         { }
123         void add(const FeatureMetadata& other);
124         void clear();
125
126         bool usesFirstLineRules;
127         bool foundSiblingSelector;
128         unsigned maxDirectAdjacentSelectors;
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& ensureIdInvalidationSet(const AtomicString& attributeName);
145     DescendantInvalidationSet& ensurePseudoInvalidationSet(CSSSelector::PseudoType);
146     DescendantInvalidationSet* invalidationSetForSelector(const CSSSelector&);
147
148     InvalidationSetMode updateInvalidationSets(const CSSSelector&);
149
150     struct InvalidationSetFeatures {
151         InvalidationSetFeatures()
152             : customPseudoElement(false)
153             , treeBoundaryCrossing(false)
154             , wholeSubtree(false)
155         { }
156         Vector<AtomicString> classes;
157         Vector<AtomicString> attributes;
158         AtomicString id;
159         AtomicString tagName;
160         bool customPseudoElement;
161         bool treeBoundaryCrossing;
162         bool wholeSubtree;
163     };
164
165     static void extractInvalidationSetFeature(const CSSSelector&, InvalidationSetFeatures&);
166     const CSSSelector* extractInvalidationSetFeatures(const CSSSelector&, InvalidationSetFeatures&);
167     void addFeaturesToInvalidationSets(const CSSSelector&, InvalidationSetFeatures&);
168
169     void addClassToInvalidationSet(const AtomicString& className, Element&);
170
171     FeatureMetadata m_metadata;
172     InvalidationSetMap m_classInvalidationSets;
173     InvalidationSetMap m_attributeInvalidationSets;
174     InvalidationSetMap m_idInvalidationSets;
175     PseudoTypeInvalidationSetMap m_pseudoInvalidationSets;
176     bool m_targetedStyleRecalcEnabled;
177     StyleInvalidator m_styleInvalidator;
178 };
179
180
181 } // namespace blink
182
183 namespace WTF {
184
185 template <> struct VectorTraits<blink::RuleFeature> : VectorTraitsBase<blink::RuleFeature> {
186     static const bool needsDestruction = false;
187     static const bool canInitializeWithMemset = true;
188     static const bool canMoveWithMemcpy = true;
189 };
190
191 } // namespace WTF
192
193 #endif // RuleFeature_h