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/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"
33 class CSSSelectorList;
34 class DescendantInvalidationSet;
37 class SpaceSplitString;
41 ALLOW_ONLY_INLINE_ALLOCATION();
43 RuleFeature(StyleRule* rule, unsigned selectorIndex, bool hasDocumentSecurityOrigin);
47 RawPtrWillBeMember<StyleRule> rule;
48 unsigned selectorIndex;
49 bool hasDocumentSecurityOrigin;
52 class RuleFeatureSet {
53 DISALLOW_ALLOCATION();
58 void add(const RuleFeatureSet&);
61 void collectFeaturesFromRuleData(const RuleData&);
63 bool usesSiblingRules() const { return !siblingRules.isEmpty(); }
64 bool usesFirstLineRules() const { return m_metadata.usesFirstLineRules; }
66 unsigned maxDirectAdjacentSelectors() const { return m_metadata.maxDirectAdjacentSelectors; }
67 void setMaxDirectAdjacentSelectors(unsigned value) { m_metadata.maxDirectAdjacentSelectors = std::max(value, m_metadata.maxDirectAdjacentSelectors); }
69 bool hasSelectorForAttribute(const AtomicString& attributeName) const
71 ASSERT(!attributeName.isEmpty());
72 return m_attributeInvalidationSets.contains(attributeName);
75 bool hasSelectorForClass(const AtomicString& classValue) const
77 ASSERT(!classValue.isEmpty());
78 return m_classInvalidationSets.contains(classValue);
81 bool hasSelectorForId(const AtomicString& idValue) const { return m_idInvalidationSets.contains(idValue); }
82 bool hasSelectorForPseudoType(CSSSelector::PseudoType pseudo) const { return m_pseudoInvalidationSets.contains(pseudo); }
84 void scheduleStyleInvalidationForClassChange(const SpaceSplitString& changedClasses, Element&);
85 void scheduleStyleInvalidationForClassChange(const SpaceSplitString& oldClasses, const SpaceSplitString& newClasses, Element&);
86 void scheduleStyleInvalidationForAttributeChange(const QualifiedName& attributeName, Element&);
87 void scheduleStyleInvalidationForIdChange(const AtomicString& oldId, const AtomicString& newId, Element&);
88 void scheduleStyleInvalidationForPseudoChange(CSSSelector::PseudoType, Element&);
90 bool hasIdsInSelectors() const
92 return m_idInvalidationSets.size() > 0;
95 // Marks the given attribute name as "appearing in a selector". Used for
96 // CSS properties such as content: ... attr(...) ...
97 // FIXME: record these internally to this class instead calls from StyleResolver to here.
98 void addContentAttr(const AtomicString& attributeName);
100 StyleInvalidator& styleInvalidator();
102 void trace(Visitor*);
104 WillBeHeapVector<RuleFeature> siblingRules;
105 WillBeHeapVector<RuleFeature> uncommonAttributeRules;
108 DescendantInvalidationSet* invalidationSetForSelector(const CSSSelector&);
111 typedef WillBeHeapHashMap<AtomicString, RefPtrWillBeMember<DescendantInvalidationSet> > InvalidationSetMap;
112 typedef WillBeHeapHashMap<CSSSelector::PseudoType, RefPtrWillBeMember<DescendantInvalidationSet>, WTF::IntHash<unsigned>, WTF::UnsignedWithZeroKeyHashTraits<unsigned> > PseudoTypeInvalidationSetMap;
114 struct FeatureMetadata {
116 : usesFirstLineRules(false)
117 , foundSiblingSelector(false)
118 , maxDirectAdjacentSelectors(0)
120 void add(const FeatureMetadata& other);
123 bool usesFirstLineRules;
124 bool foundSiblingSelector;
125 unsigned maxDirectAdjacentSelectors;
128 enum InvalidationSetMode {
131 UseSubtreeStyleChange
134 static InvalidationSetMode invalidationSetModeForSelector(const CSSSelector&);
136 void collectFeaturesFromSelector(const CSSSelector&, FeatureMetadata&, InvalidationSetMode);
137 void collectFeaturesFromSelectorList(const CSSSelectorList*, FeatureMetadata&, InvalidationSetMode);
139 DescendantInvalidationSet& ensureClassInvalidationSet(const AtomicString& className);
140 DescendantInvalidationSet& ensureAttributeInvalidationSet(const AtomicString& attributeName);
141 DescendantInvalidationSet& ensureIdInvalidationSet(const AtomicString& attributeName);
142 DescendantInvalidationSet& ensurePseudoInvalidationSet(CSSSelector::PseudoType);
144 InvalidationSetMode updateInvalidationSets(const CSSSelector&);
146 struct InvalidationSetFeatures {
147 InvalidationSetFeatures()
148 : customPseudoElement(false)
149 , treeBoundaryCrossing(false)
151 , insertionPointCrossing(false)
153 Vector<AtomicString> classes;
154 Vector<AtomicString> attributes;
156 AtomicString tagName;
157 bool customPseudoElement;
158 bool treeBoundaryCrossing;
160 bool insertionPointCrossing;
163 static void extractInvalidationSetFeature(const CSSSelector&, InvalidationSetFeatures&);
164 const CSSSelector* extractInvalidationSetFeatures(const CSSSelector&, InvalidationSetFeatures&, bool negated);
166 void addFeaturesToInvalidationSet(DescendantInvalidationSet&, const InvalidationSetFeatures&);
167 void addFeaturesToInvalidationSets(const CSSSelector&, InvalidationSetFeatures&);
169 void addClassToInvalidationSet(const AtomicString& className, Element&);
171 FeatureMetadata m_metadata;
172 InvalidationSetMap m_classInvalidationSets;
173 InvalidationSetMap m_attributeInvalidationSets;
174 InvalidationSetMap m_idInvalidationSets;
175 PseudoTypeInvalidationSetMap m_pseudoInvalidationSets;
176 StyleInvalidator m_styleInvalidator;
182 #endif // RuleFeature_h