Upstream version 5.34.104.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 Document;
33 class ShadowRoot;
34 class StyleRule;
35 class CSSSelector;
36 class CSSSelectorList;
37 class RuleData;
38 class SpaceSplitString;
39
40 struct RuleFeature {
41     RuleFeature(StyleRule* rule, unsigned selectorIndex, bool hasDocumentSecurityOrigin)
42         : rule(rule)
43         , selectorIndex(selectorIndex)
44         , hasDocumentSecurityOrigin(hasDocumentSecurityOrigin)
45     {
46     }
47     StyleRule* rule;
48     unsigned selectorIndex;
49     bool hasDocumentSecurityOrigin;
50 };
51
52 class RuleFeatureSet {
53 public:
54     RuleFeatureSet();
55
56     void add(const RuleFeatureSet&);
57     void clear();
58
59     void collectFeaturesFromSelector(const CSSSelector&);
60     void collectFeaturesFromRuleData(const RuleData&);
61
62     bool usesSiblingRules() const { return !siblingRules.isEmpty(); }
63     bool usesFirstLineRules() const { return m_metadata.usesFirstLineRules; }
64
65     unsigned maxDirectAdjacentSelectors() const { return m_metadata.maxDirectAdjacentSelectors; }
66     void setMaxDirectAdjacentSelectors(unsigned value)  { m_metadata.maxDirectAdjacentSelectors = std::max(value, m_metadata.maxDirectAdjacentSelectors); }
67
68     inline bool hasSelectorForAttribute(const AtomicString& attributeName) const
69     {
70         ASSERT(!attributeName.isEmpty());
71         return m_metadata.attrsInRules.contains(attributeName);
72     }
73
74     inline bool hasSelectorForClass(const AtomicString& classValue) const
75     {
76         ASSERT(!classValue.isEmpty());
77         return m_classInvalidationSets.get(classValue);
78
79     }
80
81     inline bool hasSelectorForId(const AtomicString& idValue) const
82     {
83         return m_metadata.idsInRules.contains(idValue);
84     }
85
86     void scheduleStyleInvalidationForClassChange(const SpaceSplitString& changedClasses, Element*);
87     void scheduleStyleInvalidationForClassChange(const SpaceSplitString& oldClasses, const SpaceSplitString& newClasses, Element*);
88
89     void computeStyleInvalidation(Document&);
90
91     int hasIdsInSelectors() const
92     {
93         return m_metadata.idsInRules.size() > 0;
94     }
95
96     // Marks the given attribute name as "appearing in a selector". Used for
97     // CSS properties such as content: ... attr(...) ...
98     void addAttributeInASelector(const AtomicString& attributeName);
99
100     Vector<RuleFeature> siblingRules;
101     Vector<RuleFeature> uncommonAttributeRules;
102
103 private:
104     typedef HashMap<AtomicString, RefPtr<DescendantInvalidationSet> > InvalidationSetMap;
105     typedef Vector<DescendantInvalidationSet*> InvalidationList;
106     typedef HashMap<Element*, InvalidationList*> PendingInvalidationMap;
107     struct FeatureMetadata {
108         FeatureMetadata()
109             : usesFirstLineRules(false)
110             , foundSiblingSelector(false)
111             , maxDirectAdjacentSelectors(0)
112         { }
113         void add(const FeatureMetadata& other);
114         void clear();
115
116         bool usesFirstLineRules;
117         bool foundSiblingSelector;
118         unsigned maxDirectAdjacentSelectors;
119         HashSet<AtomicString> idsInRules;
120         HashSet<AtomicString> attrsInRules;
121     };
122
123     // These return true if setNeedsStyleRecalc() should be run on the Element, as a fallback.
124     bool computeInvalidationSetsForClassChange(const SpaceSplitString& changedClasses, Element*);
125     bool computeInvalidationSetsForClassChange(const SpaceSplitString& oldClasses, const SpaceSplitString& newClasses, Element*);
126
127     enum SelectorFeatureCollectionMode {
128         ProcessClasses,
129         DontProcessClasses
130     };
131
132     void collectFeaturesFromSelector(const CSSSelector&, FeatureMetadata&, SelectorFeatureCollectionMode processClasses);
133     void collectFeaturesFromSelectorList(const CSSSelectorList*, FeatureMetadata&, SelectorFeatureCollectionMode processClasses);
134
135     bool classInvalidationRequiresSubtreeRecalc(const AtomicString& className);
136     DescendantInvalidationSet& ensureClassInvalidationSet(const AtomicString& className);
137     bool updateClassInvalidationSets(const CSSSelector&);
138
139     void addClassToInvalidationSet(const AtomicString& className, Element*);
140
141     bool invalidateStyleForClassChange(Element*, Vector<AtomicString>&, bool foundInvalidationSet);
142     bool invalidateStyleForClassChangeOnChildren(Element*, Vector<AtomicString>& invalidationClasses, bool foundInvalidationSet);
143
144     InvalidationList& ensurePendingInvalidationList(Element*);
145
146     FeatureMetadata m_metadata;
147     InvalidationSetMap m_classInvalidationSets;
148     PendingInvalidationMap m_pendingInvalidationMap;
149
150     bool m_targetedStyleRecalcEnabled;
151 };
152
153 } // namespace WebCore
154
155 #endif // RuleFeature_h