[:has() pseudo-class] Collect invalidation selectors for child invalidation
[webkit.git] / Source / WebCore / style / RuleFeature.h
blobd30416718f782a259533c5209f0cba0cd0812b47
1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2003-2011, 2014 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.
22 #pragma once
24 #include "CSSSelector.h"
25 #include <wtf/Forward.h>
26 #include <wtf/HashMap.h>
27 #include <wtf/HashSet.h>
28 #include <wtf/text/AtomString.h>
29 #include <wtf/text/AtomStringHash.h>
31 namespace WebCore {
33 class StyleRule;
35 namespace Style {
37 class RuleData;
39 // FIXME: Has* values should be separated so we could describe both the :has() argument and its position in the selector.
40 enum class MatchElement : uint8_t {
41 Subject,
42 Parent,
43 Ancestor,
44 DirectSibling,
45 IndirectSibling,
46 AnySibling,
47 ParentSibling,
48 AncestorSibling,
49 HasChild,
50 HasDescendant,
51 HasSibling,
52 HasSiblingDescendant,
53 HasNonSubject, // FIXME: This is a catch-all for cases where :has() is in non-subject position.
54 Host
56 constexpr unsigned matchElementCount = static_cast<unsigned>(MatchElement::Host) + 1;
58 enum class IsNegation : bool { No, Yes };
60 // For MSVC.
61 #pragma pack(push, 4)
62 struct RuleAndSelector {
63 RuleAndSelector(const RuleData&);
65 RefPtr<const StyleRule> styleRule;
66 uint16_t selectorIndex; // Keep in sync with RuleData's selectorIndex size.
67 uint16_t selectorListIndex; // Keep in sync with RuleData's selectorListIndex size.
70 struct RuleFeature : public RuleAndSelector {
71 RuleFeature(const RuleData&, MatchElement, IsNegation);
73 MatchElement matchElement;
74 IsNegation isNegation; // Whether the selector is in a (non-paired) :not() context.
76 static_assert(sizeof(RuleFeature) <= 16, "RuleFeature is a frquently alocated object. Keep it small.");
78 struct RuleFeatureWithInvalidationSelector : public RuleFeature {
79 RuleFeatureWithInvalidationSelector(const RuleData&, MatchElement, IsNegation, const CSSSelector* invalidationSelector);
81 const CSSSelector* invalidationSelector { nullptr };
83 #pragma pack(pop)
85 using PseudoClassInvalidationKey = std::tuple<unsigned, uint8_t, AtomString>;
87 using RuleFeatureVector = Vector<RuleFeature>;
89 struct RuleFeatureSet {
90 void add(const RuleFeatureSet&);
91 void clear();
92 void shrinkToFit();
93 void collectFeatures(const RuleData&);
94 void registerContentAttribute(const AtomString&);
96 bool usesHasPseudoClass() const;
97 bool usesMatchElement(MatchElement matchElement) const { return usedMatchElements[static_cast<uint8_t>(matchElement)]; }
98 void setUsesMatchElement(MatchElement matchElement) { usedMatchElements[static_cast<uint8_t>(matchElement)] = true; }
100 HashSet<AtomString> idsInRules;
101 HashSet<AtomString> idsMatchingAncestorsInRules;
102 HashSet<AtomString> attributeCanonicalLocalNamesInRules;
103 HashSet<AtomString> attributeLocalNamesInRules;
104 HashSet<AtomString> contentAttributeNamesInRules;
105 Vector<RuleAndSelector> siblingRules;
106 Vector<RuleAndSelector> uncommonAttributeRules;
108 HashMap<AtomString, std::unique_ptr<RuleFeatureVector>> idRules;
109 HashMap<AtomString, std::unique_ptr<RuleFeatureVector>> classRules;
110 HashMap<AtomString, std::unique_ptr<Vector<RuleFeatureWithInvalidationSelector>>> attributeRules;
111 HashMap<PseudoClassInvalidationKey, std::unique_ptr<RuleFeatureVector>> pseudoClassRules;
112 HashMap<PseudoClassInvalidationKey, std::unique_ptr<Vector<RuleFeatureWithInvalidationSelector>>> hasPseudoClassRules;
114 HashSet<AtomString> classesAffectingHost;
115 HashSet<AtomString> attributesAffectingHost;
116 HashSet<CSSSelector::PseudoClassType, IntHash<CSSSelector::PseudoClassType>, WTF::StrongEnumHashTraits<CSSSelector::PseudoClassType>> pseudoClassesAffectingHost;
117 HashSet<CSSSelector::PseudoClassType, IntHash<CSSSelector::PseudoClassType>, WTF::StrongEnumHashTraits<CSSSelector::PseudoClassType>> pseudoClassTypes;
119 std::array<bool, matchElementCount> usedMatchElements { };
121 bool usesFirstLineRules { false };
122 bool usesFirstLetterRules { false };
124 private:
125 struct SelectorFeatures {
126 bool hasSiblingSelector { false };
128 using InvalidationFeature = std::tuple<const CSSSelector*, MatchElement, IsNegation>;
130 Vector<InvalidationFeature> ids;
131 Vector<InvalidationFeature> classes;
132 Vector<InvalidationFeature> attributes;
133 Vector<InvalidationFeature> pseudoClasses;
134 Vector<InvalidationFeature> hasPseudoClasses;
136 void recursivelyCollectFeaturesFromSelector(SelectorFeatures&, const CSSSelector&, MatchElement = MatchElement::Subject, IsNegation = IsNegation::No);
139 bool isHasPseudoClassMatchElement(MatchElement);
140 MatchElement computeHasPseudoClassMatchElement(const CSSSelector&);
142 enum class InvalidationKeyType : uint8_t { Universal = 1, Class, Id, Tag };
143 PseudoClassInvalidationKey makePseudoClassInvalidationKey(CSSSelector::PseudoClassType, InvalidationKeyType, const AtomString& = starAtom());
145 inline bool isUniversalInvalidation(const PseudoClassInvalidationKey& key)
147 return static_cast<InvalidationKeyType>(std::get<1>(key)) == InvalidationKeyType::Universal;
150 inline bool RuleFeatureSet::usesHasPseudoClass() const
152 return usesMatchElement(MatchElement::HasChild)
153 || usesMatchElement(MatchElement::HasDescendant)
154 || usesMatchElement(MatchElement::HasSiblingDescendant)
155 || usesMatchElement(MatchElement::HasSibling)
156 || usesMatchElement(MatchElement::HasNonSubject);
159 } // namespace Style
160 } // namespace WebCore