1/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2003-2011, 2014 Apple Inc. All rights reserved.
4 *
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.
9 *
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.
14 *
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.
19 *
20 */
21
22#pragma once
23
24#include "CSSSelector.h"
25#include <wtf/Forward.h>
26#include <wtf/HashMap.h>
27#include <wtf/HashSet.h>
28#include <wtf/text/AtomicString.h>
29#include <wtf/text/AtomicStringHash.h>
30
31namespace WebCore {
32
33class RuleData;
34class StyleRule;
35
36enum class MatchElement { Subject, Parent, Ancestor, DirectSibling, IndirectSibling, AnySibling, ParentSibling, AncestorSibling, Host };
37constexpr unsigned matchElementCount = static_cast<unsigned>(MatchElement::Host) + 1;
38
39struct RuleFeature {
40 RuleFeature(StyleRule* rule, unsigned selectorIndex, unsigned selectorListIndex, Optional<MatchElement> matchElement = WTF::nullopt, const CSSSelector* invalidationSelector = nullptr)
41 : rule(rule)
42 , selectorIndex(selectorIndex)
43 , selectorListIndex(selectorListIndex)
44 , matchElement(matchElement)
45 , invalidationSelector(invalidationSelector)
46 {
47 }
48 StyleRule* rule;
49 unsigned selectorIndex;
50 unsigned selectorListIndex;
51 Optional<MatchElement> matchElement;
52 const CSSSelector* invalidationSelector;
53};
54
55struct RuleFeatureSet {
56 void add(const RuleFeatureSet&);
57 void clear();
58 void shrinkToFit();
59 void collectFeatures(const RuleData&);
60 void registerContentAttribute(const AtomicString&);
61
62 HashSet<AtomicString> idsInRules;
63 HashSet<AtomicString> idsMatchingAncestorsInRules;
64 HashSet<AtomicString> attributeCanonicalLocalNamesInRules;
65 HashSet<AtomicString> attributeLocalNamesInRules;
66 HashSet<AtomicString> contentAttributeNamesInRules;
67 Vector<RuleFeature> siblingRules;
68 Vector<RuleFeature> uncommonAttributeRules;
69
70 HashMap<AtomicString, std::unique_ptr<Vector<RuleFeature>>> classRules;
71 HashMap<AtomicString, std::unique_ptr<Vector<RuleFeature>>> attributeRules;
72 HashSet<AtomicString> classesAffectingHost;
73 HashSet<AtomicString> attributesAffectingHost;
74
75 bool usesFirstLineRules { false };
76 bool usesFirstLetterRules { false };
77
78private:
79 static MatchElement computeNextMatchElement(MatchElement, CSSSelector::RelationType);
80 static MatchElement computeSubSelectorMatchElement(MatchElement, const CSSSelector&);
81
82 struct SelectorFeatures {
83 bool hasSiblingSelector { false };
84
85 Vector<std::pair<AtomicString, MatchElement>, 32> classes;
86 Vector<std::pair<const CSSSelector*, MatchElement>, 32> attributes;
87 };
88 void recursivelyCollectFeaturesFromSelector(SelectorFeatures&, const CSSSelector&, MatchElement = MatchElement::Subject);
89};
90
91} // namespace WebCore
92