summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/css/resolver/match_result.h
blob: 2a6f8ad74edf7394f572a6d588fb6a11cc1019b1 (plain)
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
/*
 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc.
 * All rights reserved.
 * Copyright (C) 2013 Google 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 THIRD_PARTY_BLINK_RENDERER_CORE_CSS_RESOLVER_MATCH_RESULT_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_CSS_RESOLVER_MATCH_RESULT_H_

#include "base/macros.h"
#include "base/memory/scoped_refptr.h"
#include "third_party/blink/renderer/core/css/rule_set.h"
#include "third_party/blink/renderer/core/css/selector_checker.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"

namespace blink {

class CSSPropertyValueSet;

struct CORE_EXPORT MatchedProperties {
  DISALLOW_NEW();

 public:
  MatchedProperties();
  ~MatchedProperties();

  void Trace(blink::Visitor*);

  Member<CSSPropertyValueSet> properties;

  struct Data {
    unsigned link_match_type : 2;
    unsigned valid_property_filter : 2;
    // This is approximately equivalent to the 'shadow-including tree order'.
    // It can be used to evaluate the 'Shadow Tree' criteria. Note that the
    // number stored here is 'local' to each origin (user, author), and is
    // not used at all for the UA origin. Hence, it is not possible to compare
    // tree_orders from two different origins.
    //
    // Note also that the tree_order will start at ~0u and then decrease.
    // This is because we currently store the matched properties in reverse
    // order.
    //
    // https://drafts.csswg.org/css-scoping/#shadow-cascading
    uint16_t tree_order;
  };
  Data types_;
};

}  // namespace blink

WTF_ALLOW_MOVE_AND_INIT_WITH_MEM_FUNCTIONS(blink::MatchedProperties)

namespace blink {

using MatchedPropertiesVector = HeapVector<MatchedProperties, 64>;

// MatchedPropertiesRange is used to represent a subset of the matched
// properties from a given origin, for instance UA rules, author rules, or a
// shadow tree scope.  This is needed because rules from different origins are
// applied in the opposite order for !important rules, yet in the same order as
// for normal rules within the same origin.

class MatchedPropertiesRange {
 public:
  MatchedPropertiesRange(MatchedPropertiesVector::const_iterator begin,
                         MatchedPropertiesVector::const_iterator end)
      : begin_(begin), end_(end) {}

  MatchedPropertiesVector::const_iterator begin() const { return begin_; }
  MatchedPropertiesVector::const_iterator end() const { return end_; }

  bool IsEmpty() const { return begin() == end(); }

 private:
  MatchedPropertiesVector::const_iterator begin_;
  MatchedPropertiesVector::const_iterator end_;
};

class CORE_EXPORT MatchResult {
  STACK_ALLOCATED();

 public:
  MatchResult() = default;

  void AddMatchedProperties(
      const CSSPropertyValueSet* properties,
      unsigned link_match_type = CSSSelector::kMatchAll,
      ValidPropertyFilter = ValidPropertyFilter::kNoFilter);
  bool HasMatchedProperties() const { return matched_properties_.size(); }

  void FinishAddingUARules();
  void FinishAddingUserRules();
  void FinishAddingAuthorRulesForTreeScope();

  void SetIsCacheable(bool cacheable) { is_cacheable_ = cacheable; }
  bool IsCacheable() const { return is_cacheable_; }

  MatchedPropertiesRange AllRules() const {
    return MatchedPropertiesRange(matched_properties_.begin(),
                                  matched_properties_.end());
  }
  MatchedPropertiesRange UaRules() const {
    MatchedPropertiesVector::const_iterator begin = matched_properties_.begin();
    MatchedPropertiesVector::const_iterator end =
        matched_properties_.begin() + ua_range_end_;
    return MatchedPropertiesRange(begin, end);
  }
  MatchedPropertiesRange UserRules() const {
    MatchedPropertiesVector::const_iterator begin =
        matched_properties_.begin() + ua_range_end_;
    MatchedPropertiesVector::const_iterator end =
        matched_properties_.begin() + (user_range_ends_.IsEmpty()
                                           ? ua_range_end_
                                           : user_range_ends_.back());
    return MatchedPropertiesRange(begin, end);
  }
  MatchedPropertiesRange AuthorRules() const {
    MatchedPropertiesVector::const_iterator begin =
        matched_properties_.begin() + (user_range_ends_.IsEmpty()
                                           ? ua_range_end_
                                           : user_range_ends_.back());
    MatchedPropertiesVector::const_iterator end = matched_properties_.end();
    return MatchedPropertiesRange(begin, end);
  }

  const MatchedPropertiesVector& GetMatchedProperties() const {
    return matched_properties_;
  }

 private:
  friend class ImportantUserRanges;
  friend class ImportantUserRangeIterator;
  friend class ImportantAuthorRanges;
  friend class ImportantAuthorRangeIterator;

  MatchedPropertiesVector matched_properties_;
  Vector<unsigned, 16> user_range_ends_;
  Vector<unsigned, 16> author_range_ends_;
  unsigned ua_range_end_ = 0;
  bool is_cacheable_ = true;
  uint16_t current_tree_order_ = 0;
  DISALLOW_COPY_AND_ASSIGN(MatchResult);
};

class ImportantUserRangeIterator {
  STACK_ALLOCATED();

 public:
  ImportantUserRangeIterator(const MatchResult& result, int end_index)
      : result_(result), end_index_(end_index) {}

  MatchedPropertiesRange operator*() const {
    unsigned range_end = result_.user_range_ends_[end_index_];
    unsigned range_begin = end_index_
                               ? result_.user_range_ends_[end_index_ - 1]
                               : result_.ua_range_end_;
    return MatchedPropertiesRange(
        result_.GetMatchedProperties().begin() + range_begin,
        result_.GetMatchedProperties().begin() + range_end);
  }

  ImportantUserRangeIterator& operator++() {
    --end_index_;
    return *this;
  }

  bool operator==(const ImportantUserRangeIterator& other) const {
    return end_index_ == other.end_index_ && &result_ == &other.result_;
  }
  bool operator!=(const ImportantUserRangeIterator& other) const {
    return !(*this == other);
  }

 private:
  const MatchResult& result_;
  unsigned end_index_;
};

class ImportantUserRanges {
  STACK_ALLOCATED();

 public:
  explicit ImportantUserRanges(const MatchResult& result) : result_(result) {}

  ImportantUserRangeIterator begin() const {
    return ImportantUserRangeIterator(result_,
                                        result_.user_range_ends_.size() - 1);
  }
  ImportantUserRangeIterator end() const {
    return ImportantUserRangeIterator(result_, -1);
  }

 private:
  const MatchResult& result_;
};

class ImportantAuthorRangeIterator {
  STACK_ALLOCATED();

 public:
  ImportantAuthorRangeIterator(const MatchResult& result, int end_index)
      : result_(result), end_index_(end_index) {}

  MatchedPropertiesRange operator*() const {
    unsigned range_end = result_.author_range_ends_[end_index_];
    unsigned range_begin = end_index_
                               ? result_.author_range_ends_[end_index_ - 1]
                               : (result_.user_range_ends_.IsEmpty()
                                      ? result_.ua_range_end_
                                      : result_.user_range_ends_.back());
    return MatchedPropertiesRange(
        result_.GetMatchedProperties().begin() + range_begin,
        result_.GetMatchedProperties().begin() + range_end);
  }

  ImportantAuthorRangeIterator& operator++() {
    --end_index_;
    return *this;
  }

  bool operator==(const ImportantAuthorRangeIterator& other) const {
    return end_index_ == other.end_index_ && &result_ == &other.result_;
  }
  bool operator!=(const ImportantAuthorRangeIterator& other) const {
    return !(*this == other);
  }

 private:
  const MatchResult& result_;
  unsigned end_index_;
};

class ImportantAuthorRanges {
  STACK_ALLOCATED();

 public:
  explicit ImportantAuthorRanges(const MatchResult& result) : result_(result) {}

  ImportantAuthorRangeIterator begin() const {
    return ImportantAuthorRangeIterator(result_,
                                        result_.author_range_ends_.size() - 1);
  }
  ImportantAuthorRangeIterator end() const {
    return ImportantAuthorRangeIterator(result_, -1);
  }

 private:
  const MatchResult& result_;
};

inline bool operator==(const MatchedProperties& a, const MatchedProperties& b) {
  return a.properties == b.properties &&
         a.types_.link_match_type == b.types_.link_match_type;
}

inline bool operator!=(const MatchedProperties& a, const MatchedProperties& b) {
  return !(a == b);
}

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_CSS_RESOLVER_MATCH_RESULT_H_