#pragma once
#include "CSSSelector.h"
#include <wtf/Forward.h>
#include <wtf/HashMap.h>
#include <wtf/HashSet.h>
#include <wtf/text/AtomString.h>
#include <wtf/text/AtomStringHash.h>
namespace WebCore {
class StyleRule;
namespace Style {
class RuleData;
enum class MatchElement { Subject, Parent, Ancestor, DirectSibling, IndirectSibling, AnySibling, ParentSibling, AncestorSibling, Host };
constexpr unsigned matchElementCount = static_cast<unsigned>(MatchElement::Host) + 1;
struct RuleFeature {
RuleFeature(const RuleData&, Optional<MatchElement> = WTF::nullopt, const CSSSelector* invalidationSelector = nullptr);
RefPtr<const StyleRule> styleRule;
unsigned selectorIndex;
unsigned selectorListIndex;
Optional<MatchElement> matchElement { };
const CSSSelector* invalidationSelector { nullptr };
};
struct RuleFeatureSet {
void add(const RuleFeatureSet&);
void clear();
void shrinkToFit();
void collectFeatures(const RuleData&);
void registerContentAttribute(const AtomString&);
HashSet<AtomString> idsInRules;
HashSet<AtomString> idsMatchingAncestorsInRules;
HashSet<AtomString> attributeCanonicalLocalNamesInRules;
HashSet<AtomString> attributeLocalNamesInRules;
HashSet<AtomString> contentAttributeNamesInRules;
Vector<RuleFeature> siblingRules;
Vector<RuleFeature> uncommonAttributeRules;
HashMap<AtomString, std::unique_ptr<Vector<RuleFeature>>> classRules;
HashMap<AtomString, std::unique_ptr<Vector<RuleFeature>>> attributeRules;
HashSet<AtomString> classesAffectingHost;
HashSet<AtomString> attributesAffectingHost;
bool usesFirstLineRules { false };
bool usesFirstLetterRules { false };
private:
static MatchElement computeNextMatchElement(MatchElement, CSSSelector::RelationType);
static MatchElement computeSubSelectorMatchElement(MatchElement, const CSSSelector&);
struct SelectorFeatures {
bool hasSiblingSelector { false };
Vector<std::pair<AtomString, MatchElement>, 32> classes;
Vector<std::pair<const CSSSelector*, MatchElement>, 32> attributes;
};
void recursivelyCollectFeaturesFromSelector(SelectorFeatures&, const CSSSelector&, MatchElement = MatchElement::Subject);
};
} }