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;
40 class SpaceSplitString;
44 ALLOW_ONLY_INLINE_ALLOCATION();
46 RuleFeature(StyleRule* rule, unsigned selectorIndex, bool hasDocumentSecurityOrigin);
50 RawPtrWillBeMember<StyleRule> rule;
51 unsigned selectorIndex;
52 bool hasDocumentSecurityOrigin;
55 class RuleFeatureSet {
56 DISALLOW_ALLOCATION();
61 void add(const RuleFeatureSet&);
64 void collectFeaturesFromSelector(const CSSSelector&);
65 void collectFeaturesFromRuleData(const RuleData&);
67 bool usesSiblingRules() const { return !siblingRules.isEmpty(); }
68 bool usesFirstLineRules() const { return m_metadata.usesFirstLineRules; }
70 unsigned maxDirectAdjacentSelectors() const { return m_metadata.maxDirectAdjacentSelectors; }
71 void setMaxDirectAdjacentSelectors(unsigned value) { m_metadata.maxDirectAdjacentSelectors = std::max(value, m_metadata.maxDirectAdjacentSelectors); }
73 inline bool hasSelectorForAttribute(const AtomicString& attributeName) const
75 ASSERT(!attributeName.isEmpty());
76 return m_attributeInvalidationSets.contains(attributeName);
79 inline bool hasSelectorForClass(const AtomicString& classValue) const
81 ASSERT(!classValue.isEmpty());
82 return m_classInvalidationSets.contains(classValue);
85 inline bool hasSelectorForId(const AtomicString& idValue) const
87 return m_idInvalidationSets.contains(idValue);
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&);
96 bool hasIdsInSelectors() const
98 return m_idInvalidationSets.size() > 0;
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);
106 StyleInvalidator& styleInvalidator();
108 void trace(Visitor*);
110 WillBeHeapVector<RuleFeature> siblingRules;
111 WillBeHeapVector<RuleFeature> uncommonAttributeRules;
114 typedef WillBeHeapHashMap<AtomicString, RefPtrWillBeMember<DescendantInvalidationSet> > InvalidationSetMap;
115 typedef WillBeHeapHashMap<CSSSelector::PseudoType, RefPtrWillBeMember<DescendantInvalidationSet>, WTF::IntHash<unsigned>, WTF::UnsignedWithZeroKeyHashTraits<unsigned> > PseudoTypeInvalidationSetMap;
117 struct FeatureMetadata {
119 : usesFirstLineRules(false)
120 , foundSiblingSelector(false)
121 , maxDirectAdjacentSelectors(0)
123 void add(const FeatureMetadata& other);
126 bool usesFirstLineRules;
127 bool foundSiblingSelector;
128 unsigned maxDirectAdjacentSelectors;
131 enum InvalidationSetMode {
134 UseSubtreeStyleChange
137 static InvalidationSetMode invalidationSetModeForSelector(const CSSSelector&);
139 void collectFeaturesFromSelector(const CSSSelector&, FeatureMetadata&, InvalidationSetMode);
140 void collectFeaturesFromSelectorList(const CSSSelectorList*, FeatureMetadata&, InvalidationSetMode);
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&);
148 InvalidationSetMode updateInvalidationSets(const CSSSelector&);
150 struct InvalidationSetFeatures {
151 InvalidationSetFeatures()
152 : customPseudoElement(false)
153 , treeBoundaryCrossing(false)
154 , wholeSubtree(false)
156 Vector<AtomicString> classes;
157 Vector<AtomicString> attributes;
159 AtomicString tagName;
160 bool customPseudoElement;
161 bool treeBoundaryCrossing;
165 static void extractInvalidationSetFeature(const CSSSelector&, InvalidationSetFeatures&);
166 const CSSSelector* extractInvalidationSetFeatures(const CSSSelector&, 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 bool m_targetedStyleRecalcEnabled;
177 StyleInvalidator m_styleInvalidator;
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;
193 #endif // RuleFeature_h