File: MatchResult.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 (198 lines) | stat: -rw-r--r-- 6,128 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
/*
 * 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 MatchResult_h
#define MatchResult_h

#include "core/css/RuleSet.h"
#include "core/css/SelectorChecker.h"
#include "platform/heap/Handle.h"
#include "wtf/RefPtr.h"
#include "wtf/Vector.h"

namespace blink {

class StylePropertySet;

struct CORE_EXPORT MatchedProperties {
  DISALLOW_NEW_EXCEPT_PLACEMENT_NEW();

 public:
  MatchedProperties();
  ~MatchedProperties();

  DECLARE_TRACE();

  Member<StylePropertySet> properties;

  union {
    struct {
      unsigned linkMatchType : 2;
      unsigned whitelistType : 2;
    } m_types;
    // Used to make sure all memory is zero-initialized since we compute the
    // hash over the bytes of this object.
    void* possiblyPaddedMember;
  };
};

}  // 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)
      : m_begin(begin), m_end(end) {}

  MatchedPropertiesVector::const_iterator begin() const { return m_begin; }
  MatchedPropertiesVector::const_iterator end() const { return m_end; }

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

 private:
  MatchedPropertiesVector::const_iterator m_begin;
  MatchedPropertiesVector::const_iterator m_end;
};

class CORE_EXPORT MatchResult {
  WTF_MAKE_NONCOPYABLE(MatchResult);
  STACK_ALLOCATED();

 public:
  MatchResult() {}

  void addMatchedProperties(const StylePropertySet* properties,
                            unsigned linkMatchType = CSSSelector::MatchAll,
                            PropertyWhitelistType = PropertyWhitelistNone);
  bool hasMatchedProperties() const { return m_matchedProperties.size(); }

  void finishAddingUARules();
  void finishAddingAuthorRulesForTreeScope();

  void setIsCacheable(bool cacheable) { m_isCacheable = cacheable; }
  bool isCacheable() const { return m_isCacheable; }

  MatchedPropertiesRange allRules() const {
    return MatchedPropertiesRange(m_matchedProperties.begin(),
                                  m_matchedProperties.end());
  }
  MatchedPropertiesRange uaRules() const {
    return MatchedPropertiesRange(m_matchedProperties.begin(),
                                  m_matchedProperties.begin() + m_uaRangeEnd);
  }
  MatchedPropertiesRange authorRules() const {
    return MatchedPropertiesRange(m_matchedProperties.begin() + m_uaRangeEnd,
                                  m_matchedProperties.end());
  }

  const MatchedPropertiesVector& matchedProperties() const {
    return m_matchedProperties;
  }

 private:
  friend class ImportantAuthorRanges;
  friend class ImportantAuthorRangeIterator;

  MatchedPropertiesVector m_matchedProperties;
  Vector<unsigned, 16> m_authorRangeEnds;
  unsigned m_uaRangeEnd = 0;
  bool m_isCacheable = true;
};

class ImportantAuthorRangeIterator {
  STACK_ALLOCATED();

 public:
  ImportantAuthorRangeIterator(const MatchResult& result, int endIndex)
      : m_result(result), m_endIndex(endIndex) {}

  MatchedPropertiesRange operator*() const {
    unsigned rangeEnd = m_result.m_authorRangeEnds[m_endIndex];
    unsigned rangeBegin = m_endIndex
                              ? m_result.m_authorRangeEnds[m_endIndex - 1]
                              : m_result.m_uaRangeEnd;
    return MatchedPropertiesRange(
        m_result.matchedProperties().begin() + rangeBegin,
        m_result.matchedProperties().begin() + rangeEnd);
  }

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

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

 private:
  const MatchResult& m_result;
  unsigned m_endIndex;
};

class ImportantAuthorRanges {
  STACK_ALLOCATED();

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

  ImportantAuthorRangeIterator begin() const {
    return ImportantAuthorRangeIterator(m_result,
                                        m_result.m_authorRangeEnds.size() - 1);
  }
  ImportantAuthorRangeIterator end() const {
    return ImportantAuthorRangeIterator(m_result, -1);
  }

 private:
  const MatchResult& m_result;
};

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

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

}  // namespace blink

#endif  // MatchResult_h