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.
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.
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.
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.
25 #include "core/css/analyzer/DescendantInvalidationSet.h"
26 #include "wtf/Forward.h"
27 #include "wtf/HashSet.h"
28 #include "wtf/text/AtomicStringHash.h"
36 class CSSSelectorList;
38 class SpaceSplitString;
41 RuleFeature(StyleRule* rule, unsigned selectorIndex, bool hasDocumentSecurityOrigin)
43 , selectorIndex(selectorIndex)
44 , hasDocumentSecurityOrigin(hasDocumentSecurityOrigin)
48 unsigned selectorIndex;
49 bool hasDocumentSecurityOrigin;
52 class RuleFeatureSet {
56 void add(const RuleFeatureSet&);
59 void collectFeaturesFromSelector(const CSSSelector&);
60 void collectFeaturesFromRuleData(const RuleData&);
62 bool usesSiblingRules() const { return !siblingRules.isEmpty(); }
63 bool usesFirstLineRules() const { return m_metadata.usesFirstLineRules; }
65 unsigned maxDirectAdjacentSelectors() const { return m_metadata.maxDirectAdjacentSelectors; }
66 void setMaxDirectAdjacentSelectors(unsigned value) { m_metadata.maxDirectAdjacentSelectors = std::max(value, m_metadata.maxDirectAdjacentSelectors); }
68 inline bool hasSelectorForAttribute(const AtomicString& attributeName) const
70 ASSERT(!attributeName.isEmpty());
71 return m_metadata.attrsInRules.contains(attributeName);
74 inline bool hasSelectorForClass(const AtomicString& classValue) const
76 ASSERT(!classValue.isEmpty());
77 return m_classInvalidationSets.get(classValue);
81 inline bool hasSelectorForId(const AtomicString& idValue) const
83 return m_metadata.idsInRules.contains(idValue);
86 void scheduleStyleInvalidationForClassChange(const SpaceSplitString& changedClasses, Element*);
87 void scheduleStyleInvalidationForClassChange(const SpaceSplitString& oldClasses, const SpaceSplitString& newClasses, Element*);
89 void computeStyleInvalidation(Document&);
91 int hasIdsInSelectors() const
93 return m_metadata.idsInRules.size() > 0;
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);
100 Vector<RuleFeature> siblingRules;
101 Vector<RuleFeature> uncommonAttributeRules;
104 typedef HashMap<AtomicString, RefPtr<DescendantInvalidationSet> > InvalidationSetMap;
105 typedef Vector<DescendantInvalidationSet*> InvalidationList;
106 typedef HashMap<Element*, InvalidationList*> PendingInvalidationMap;
107 struct FeatureMetadata {
109 : usesFirstLineRules(false)
110 , foundSiblingSelector(false)
111 , maxDirectAdjacentSelectors(0)
113 void add(const FeatureMetadata& other);
116 bool usesFirstLineRules;
117 bool foundSiblingSelector;
118 unsigned maxDirectAdjacentSelectors;
119 HashSet<AtomicString> idsInRules;
120 HashSet<AtomicString> attrsInRules;
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*);
127 enum SelectorFeatureCollectionMode {
132 void collectFeaturesFromSelector(const CSSSelector&, FeatureMetadata&, SelectorFeatureCollectionMode processClasses);
133 void collectFeaturesFromSelectorList(const CSSSelectorList*, FeatureMetadata&, SelectorFeatureCollectionMode processClasses);
135 bool classInvalidationRequiresSubtreeRecalc(const AtomicString& className);
136 DescendantInvalidationSet& ensureClassInvalidationSet(const AtomicString& className);
137 bool updateClassInvalidationSets(const CSSSelector&);
139 void addClassToInvalidationSet(const AtomicString& className, Element*);
141 bool invalidateStyleForClassChange(Element*, Vector<AtomicString>&, bool foundInvalidationSet);
142 bool invalidateStyleForClassChangeOnChildren(Element*, Vector<AtomicString>& invalidationClasses, bool foundInvalidationSet);
144 InvalidationList& ensurePendingInvalidationList(Element*);
146 FeatureMetadata m_metadata;
147 InvalidationSetMap m_classInvalidationSets;
148 PendingInvalidationMap m_pendingInvalidationMap;
150 bool m_targetedStyleRecalcEnabled;
153 } // namespace WebCore
155 #endif // RuleFeature_h