Upstream version 5.34.92.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/analyzer/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 StyleRule;
33 class CSSSelector;
34 class CSSSelectorList;
35 class RuleData;
36
37 struct RuleFeature {
38     RuleFeature(StyleRule* rule, unsigned selectorIndex, bool hasDocumentSecurityOrigin)
39         : rule(rule)
40         , selectorIndex(selectorIndex)
41         , hasDocumentSecurityOrigin(hasDocumentSecurityOrigin)
42     {
43     }
44     StyleRule* rule;
45     unsigned selectorIndex;
46     bool hasDocumentSecurityOrigin;
47 };
48
49 class RuleFeatureSet {
50 public:
51     RuleFeatureSet() { }
52
53     void add(const RuleFeatureSet&);
54     void clear();
55
56     void collectFeaturesFromSelector(const CSSSelector*);
57     void collectFeaturesFromRuleData(const RuleData&);
58
59     bool usesSiblingRules() const { return !siblingRules.isEmpty(); }
60     bool usesFirstLineRules() const { return m_metadata.usesFirstLineRules; }
61
62     unsigned maxDirectAdjacentSelectors() const { return m_metadata.maxDirectAdjacentSelectors; }
63     void setMaxDirectAdjacentSelectors(unsigned value)  { m_metadata.maxDirectAdjacentSelectors = std::max(value, m_metadata.maxDirectAdjacentSelectors); }
64
65     inline bool hasSelectorForAttribute(const AtomicString& attributeName) const
66     {
67         ASSERT(!attributeName.isEmpty());
68         return m_metadata.attrsInRules.contains(attributeName);
69     }
70
71     inline bool hasSelectorForClass(const AtomicString& classValue) const
72     {
73         ASSERT(!classValue.isEmpty());
74         return m_metadata.classesInRules.contains(classValue);
75     }
76
77     inline bool hasSelectorForId(const AtomicString& idValue) const
78     {
79         return m_metadata.idsInRules.contains(idValue);
80     }
81
82     int hasIdsInSelectors() const
83     {
84         return m_metadata.idsInRules.size() > 0;
85     }
86
87     // Marks the given attribute name as "appearing in a selector". Used for
88     // CSS properties such as content: ... attr(...) ...
89     void addAttributeInASelector(const AtomicString& attributeName);
90
91     Vector<RuleFeature> siblingRules;
92     Vector<RuleFeature> uncommonAttributeRules;
93
94 private:
95     typedef HashMap<AtomicString, RefPtr<DescendantInvalidationSet> > InvalidationSetMap;
96     struct FeatureMetadata {
97         FeatureMetadata()
98             : usesFirstLineRules(false)
99             , foundSiblingSelector(false)
100             , maxDirectAdjacentSelectors(0)
101         { }
102         void add(const FeatureMetadata& other);
103         void clear();
104
105         bool usesFirstLineRules;
106         bool foundSiblingSelector;
107         unsigned maxDirectAdjacentSelectors;
108         HashSet<AtomicString> idsInRules;
109         HashSet<AtomicString> classesInRules;
110         HashSet<AtomicString> attrsInRules;
111     };
112
113     void collectFeaturesFromSelector(const CSSSelector*, FeatureMetadata&);
114     void collectFeaturesFromSelectorList(const CSSSelectorList*, FeatureMetadata&);
115     DescendantInvalidationSet& ensureClassInvalidationSet(const AtomicString& className);
116     bool updateClassInvalidationSets(const CSSSelector*);
117
118     InvalidationSetMap m_classInvalidationSets;
119     FeatureMetadata m_metadata;
120 };
121
122 } // namespace WebCore
123
124 #endif // RuleFeature_h