File: RuleFeature.h

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (299 lines) | stat: -rw-r--r-- 10,985 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc.
 * All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */

#ifndef RuleFeature_h
#define RuleFeature_h

#include "core/CoreExport.h"
#include "core/css/CSSSelector.h"
#include "core/css/MediaQueryEvaluator.h"
#include "core/css/invalidation/InvalidationSet.h"
#include "platform/heap/Handle.h"
#include "wtf/Forward.h"
#include "wtf/HashSet.h"
#include "wtf/text/AtomicStringHash.h"

namespace blink {

struct InvalidationLists;
class QualifiedName;
class RuleData;
class StyleRule;

struct RuleFeature {
  DISALLOW_NEW_EXCEPT_PLACEMENT_NEW();

 public:
  RuleFeature(StyleRule*,
              unsigned selectorIndex,
              bool hasDocumentSecurityOrigin);

  DECLARE_TRACE();

  Member<StyleRule> rule;
  unsigned selectorIndex;
  bool hasDocumentSecurityOrigin;
};

}  // namespace blink

// Declare the VectorTraits specialization before RuleFeatureSet
// declares its vector members below.
WTF_ALLOW_MOVE_AND_INIT_WITH_MEM_FUNCTIONS(blink::RuleFeature);

namespace blink {

class CORE_EXPORT RuleFeatureSet {
  DISALLOW_NEW();
  WTF_MAKE_NONCOPYABLE(RuleFeatureSet);

 public:
  RuleFeatureSet() {}

  void add(const RuleFeatureSet&);
  void clear();

  enum SelectorPreMatch { SelectorNeverMatches, SelectorMayMatch };

  SelectorPreMatch collectFeaturesFromRuleData(const RuleData&);

  bool usesSiblingRules() const { return !m_siblingRules.isEmpty(); }
  bool usesFirstLineRules() const { return m_metadata.usesFirstLineRules; }
  bool usesWindowInactiveSelector() const {
    return m_metadata.usesWindowInactiveSelector;
  }
  bool needsFullRecalcForRuleSetInvalidation() const {
    return m_metadata.needsFullRecalcForRuleSetInvalidation;
  }

  unsigned maxDirectAdjacentSelectors() const {
    return m_metadata.maxDirectAdjacentSelectors;
  }

  bool hasSelectorForAttribute(const AtomicString& attributeName) const {
    DCHECK(!attributeName.isEmpty());
    return m_attributeInvalidationSets.contains(attributeName);
  }

  bool hasSelectorForClass(const AtomicString& classValue) const {
    DCHECK(!classValue.isEmpty());
    return m_classInvalidationSets.contains(classValue);
  }

  bool hasSelectorForId(const AtomicString& idValue) const {
    return m_idInvalidationSets.contains(idValue);
  }

  const HeapVector<RuleFeature>& siblingRules() const { return m_siblingRules; }
  const HeapVector<RuleFeature>& uncommonAttributeRules() const {
    return m_uncommonAttributeRules;
  }

  const MediaQueryResultList& viewportDependentMediaQueryResults() const {
    return m_viewportDependentMediaQueryResults;
  }
  const MediaQueryResultList& deviceDependentMediaQueryResults() const {
    return m_deviceDependentMediaQueryResults;
  }
  MediaQueryResultList& viewportDependentMediaQueryResults() {
    return m_viewportDependentMediaQueryResults;
  }
  MediaQueryResultList& deviceDependentMediaQueryResults() {
    return m_deviceDependentMediaQueryResults;
  }

  // Collect descendant and sibling invalidation sets.
  void collectInvalidationSetsForClass(InvalidationLists&,
                                       Element&,
                                       const AtomicString& className) const;
  void collectInvalidationSetsForId(InvalidationLists&,
                                    Element&,
                                    const AtomicString& id) const;
  void collectInvalidationSetsForAttribute(
      InvalidationLists&,
      Element&,
      const QualifiedName& attributeName) const;
  void collectInvalidationSetsForPseudoClass(InvalidationLists&,
                                             Element&,
                                             CSSSelector::PseudoType) const;

  void collectSiblingInvalidationSetForClass(InvalidationLists&,
                                             Element&,
                                             const AtomicString& className,
                                             unsigned minDirectAdjacent) const;
  void collectSiblingInvalidationSetForId(InvalidationLists&,
                                          Element&,
                                          const AtomicString& id,
                                          unsigned minDirectAdjacent) const;
  void collectSiblingInvalidationSetForAttribute(
      InvalidationLists&,
      Element&,
      const QualifiedName& attributeName,
      unsigned minDirectAdjacent) const;
  void collectUniversalSiblingInvalidationSet(InvalidationLists&,
                                              unsigned minDirectAdjacent) const;
  void collectNthInvalidationSet(InvalidationLists&) const;

  bool hasIdsInSelectors() const { return m_idInvalidationSets.size() > 0; }

  DECLARE_TRACE();

 protected:
  InvalidationSet* invalidationSetForSimpleSelector(const CSSSelector&,
                                                    InvalidationType);

 private:
  // Each map entry is either a DescendantInvalidationSet or
  // SiblingInvalidationSet.
  // When both are needed, we store the SiblingInvalidationSet, and use it to
  // hold the DescendantInvalidationSet.
  using InvalidationSetMap = HashMap<AtomicString, RefPtr<InvalidationSet>>;
  using PseudoTypeInvalidationSetMap =
      HashMap<CSSSelector::PseudoType,
              RefPtr<InvalidationSet>,
              WTF::IntHash<unsigned>,
              WTF::UnsignedWithZeroKeyHashTraits<unsigned>>;

  struct FeatureMetadata {
    DISALLOW_NEW();
    void add(const FeatureMetadata& other);
    void clear();

    bool usesFirstLineRules = false;
    bool usesWindowInactiveSelector = false;
    bool foundSiblingSelector = false;
    bool foundInsertionPointCrossing = false;
    bool needsFullRecalcForRuleSetInvalidation = false;
    unsigned maxDirectAdjacentSelectors = 0;
  };

  SelectorPreMatch collectFeaturesFromSelector(const CSSSelector&,
                                               FeatureMetadata&);

  InvalidationSet& ensureClassInvalidationSet(const AtomicString& className,
                                              InvalidationType);
  InvalidationSet& ensureAttributeInvalidationSet(
      const AtomicString& attributeName,
      InvalidationType);
  InvalidationSet& ensureIdInvalidationSet(const AtomicString& id,
                                           InvalidationType);
  InvalidationSet& ensurePseudoInvalidationSet(CSSSelector::PseudoType,
                                               InvalidationType);
  SiblingInvalidationSet& ensureUniversalSiblingInvalidationSet();
  DescendantInvalidationSet& ensureNthInvalidationSet();

  void updateInvalidationSets(const RuleData&);
  void updateInvalidationSetsForContentAttribute(const RuleData&);

  struct InvalidationSetFeatures {
    DISALLOW_NEW();

    void add(const InvalidationSetFeatures& other);
    bool hasFeatures() const;
    bool hasTagIdClassOrAttribute() const;

    Vector<AtomicString> classes;
    Vector<AtomicString> attributes;
    Vector<AtomicString> ids;
    Vector<AtomicString> tagNames;
    unsigned maxDirectAdjacentSelectors = 0;
    bool customPseudoElement = false;
    bool hasBeforeOrAfter = false;
    bool treeBoundaryCrossing = false;
    bool insertionPointCrossing = false;
    bool forceSubtree = false;
    bool contentPseudoCrossing = false;
    bool invalidatesSlotted = false;
    bool hasNthPseudo = false;
    bool hasFeaturesForRuleSetInvalidation = false;
  };

  static void extractInvalidationSetFeature(const CSSSelector&,
                                            InvalidationSetFeatures&);

  enum PositionType { Subject, Ancestor };
  enum FeatureInvalidationType {
    NormalInvalidation,
    RequiresSubtreeInvalidation
  };

  void extractInvalidationSetFeaturesFromSimpleSelector(
      const CSSSelector&,
      InvalidationSetFeatures&);
  const CSSSelector* extractInvalidationSetFeaturesFromCompound(
      const CSSSelector&,
      InvalidationSetFeatures&,
      PositionType,
      CSSSelector::PseudoType = CSSSelector::PseudoUnknown);
  FeatureInvalidationType extractInvalidationSetFeaturesFromSelectorList(
      const CSSSelector&,
      InvalidationSetFeatures&,
      PositionType);
  void updateFeaturesFromCombinator(
      const CSSSelector&,
      const CSSSelector* lastCompoundSelectorInAdjacentChain,
      InvalidationSetFeatures& lastCompoundInAdjacentChainFeatures,
      InvalidationSetFeatures*& siblingFeatures,
      InvalidationSetFeatures& descendantFeatures);

  void addFeaturesToInvalidationSet(InvalidationSet&,
                                    const InvalidationSetFeatures&);
  void addFeaturesToInvalidationSets(
      const CSSSelector&,
      InvalidationSetFeatures* siblingFeatures,
      InvalidationSetFeatures& descendantFeatures);
  const CSSSelector* addFeaturesToInvalidationSetsForCompoundSelector(
      const CSSSelector&,
      InvalidationSetFeatures* siblingFeatures,
      InvalidationSetFeatures& descendantFeatures);
  void addFeaturesToInvalidationSetsForSimpleSelector(
      const CSSSelector&,
      InvalidationSetFeatures* siblingFeatures,
      InvalidationSetFeatures& descendantFeatures);
  void addFeaturesToInvalidationSetsForSelectorList(
      const CSSSelector&,
      InvalidationSetFeatures* siblingFeatures,
      InvalidationSetFeatures& descendantFeatures);
  void addFeaturesToUniversalSiblingInvalidationSet(
      const InvalidationSetFeatures& siblingFeatures,
      const InvalidationSetFeatures& descendantFeatures);

  void addClassToInvalidationSet(const AtomicString& className, Element&);

  FeatureMetadata m_metadata;
  InvalidationSetMap m_classInvalidationSets;
  InvalidationSetMap m_attributeInvalidationSets;
  InvalidationSetMap m_idInvalidationSets;
  PseudoTypeInvalidationSetMap m_pseudoInvalidationSets;
  RefPtr<SiblingInvalidationSet> m_universalSiblingInvalidationSet;
  RefPtr<DescendantInvalidationSet> m_nthInvalidationSet;
  HeapVector<RuleFeature> m_siblingRules;
  HeapVector<RuleFeature> m_uncommonAttributeRules;
  MediaQueryResultList m_viewportDependentMediaQueryResults;
  MediaQueryResultList m_deviceDependentMediaQueryResults;

  friend class RuleFeatureSetTest;
};

}  // namespace blink

#endif  // RuleFeature_h