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
use crate::attr::CaseSensitivity;
use crate::bloom::BloomFilter;
use crate::nth_index_cache::NthIndexCache;
use crate::parser::SelectorImpl;
use crate::tree::{Element, OpaqueElement};
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum MatchingMode {
Normal,
ForStatelessPseudoElement,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum VisitedHandlingMode {
AllLinksUnvisited,
AllLinksVisitedAndUnvisited,
RelevantLinkVisited,
}
impl VisitedHandlingMode {
#[inline]
pub fn matches_visited(&self) -> bool {
matches!(
*self,
VisitedHandlingMode::RelevantLinkVisited |
VisitedHandlingMode::AllLinksVisitedAndUnvisited
)
}
#[inline]
pub fn matches_unvisited(&self) -> bool {
matches!(
*self,
VisitedHandlingMode::AllLinksUnvisited |
VisitedHandlingMode::AllLinksVisitedAndUnvisited
)
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum QuirksMode {
Quirks,
LimitedQuirks,
NoQuirks,
}
impl QuirksMode {
#[inline]
pub fn classes_and_ids_case_sensitivity(self) -> CaseSensitivity {
match self {
QuirksMode::NoQuirks | QuirksMode::LimitedQuirks => CaseSensitivity::CaseSensitive,
QuirksMode::Quirks => CaseSensitivity::AsciiCaseInsensitive,
}
}
}
pub struct MatchingContext<'a, Impl>
where
Impl: SelectorImpl,
{
matching_mode: MatchingMode,
pub bloom_filter: Option<&'a BloomFilter>,
pub nth_index_cache: Option<&'a mut NthIndexCache>,
pub scope_element: Option<OpaqueElement>,
pub current_host: Option<OpaqueElement>,
visited_handling: VisitedHandlingMode,
nesting_level: usize,
in_negation: bool,
pub pseudo_element_matching_fn: Option<&'a dyn Fn(&Impl::PseudoElement) -> bool>,
pub extra_data: Impl::ExtraMatchingData,
quirks_mode: QuirksMode,
classes_and_ids_case_sensitivity: CaseSensitivity,
_impl: ::std::marker::PhantomData<Impl>,
}
impl<'a, Impl> MatchingContext<'a, Impl>
where
Impl: SelectorImpl,
{
pub fn new(
matching_mode: MatchingMode,
bloom_filter: Option<&'a BloomFilter>,
nth_index_cache: Option<&'a mut NthIndexCache>,
quirks_mode: QuirksMode,
) -> Self {
Self::new_for_visited(
matching_mode,
bloom_filter,
nth_index_cache,
VisitedHandlingMode::AllLinksUnvisited,
quirks_mode,
)
}
pub fn new_for_visited(
matching_mode: MatchingMode,
bloom_filter: Option<&'a BloomFilter>,
nth_index_cache: Option<&'a mut NthIndexCache>,
visited_handling: VisitedHandlingMode,
quirks_mode: QuirksMode,
) -> Self {
Self {
matching_mode,
bloom_filter,
visited_handling,
nth_index_cache,
quirks_mode,
classes_and_ids_case_sensitivity: quirks_mode.classes_and_ids_case_sensitivity(),
scope_element: None,
current_host: None,
nesting_level: 0,
in_negation: false,
pseudo_element_matching_fn: None,
extra_data: Default::default(),
_impl: ::std::marker::PhantomData,
}
}
#[inline]
pub fn is_nested(&self) -> bool {
self.nesting_level != 0
}
#[inline]
pub fn in_negation(&self) -> bool {
self.in_negation
}
#[inline]
pub fn quirks_mode(&self) -> QuirksMode {
self.quirks_mode
}
#[inline]
pub fn matching_mode(&self) -> MatchingMode {
self.matching_mode
}
#[inline]
pub fn classes_and_ids_case_sensitivity(&self) -> CaseSensitivity {
self.classes_and_ids_case_sensitivity
}
#[inline]
pub fn nest<F, R>(&mut self, f: F) -> R
where
F: FnOnce(&mut Self) -> R,
{
self.nesting_level += 1;
let result = f(self);
self.nesting_level -= 1;
result
}
#[inline]
pub fn nest_for_negation<F, R>(&mut self, f: F) -> R
where
F: FnOnce(&mut Self) -> R,
{
let old_in_negation = self.in_negation;
self.in_negation = true;
let result = self.nest(f);
self.in_negation = old_in_negation;
result
}
#[inline]
pub fn visited_handling(&self) -> VisitedHandlingMode {
self.visited_handling
}
#[inline]
pub fn with_visited_handling_mode<F, R>(
&mut self,
handling_mode: VisitedHandlingMode,
f: F,
) -> R
where
F: FnOnce(&mut Self) -> R,
{
let original_handling_mode = self.visited_handling;
self.visited_handling = handling_mode;
let result = f(self);
self.visited_handling = original_handling_mode;
result
}
#[inline]
pub fn with_shadow_host<F, E, R>(&mut self, host: Option<E>, f: F) -> R
where
E: Element,
F: FnOnce(&mut Self) -> R,
{
let original_host = self.current_host.take();
self.current_host = host.map(|h| h.opaque());
let result = f(self);
self.current_host = original_host;
result
}
#[inline]
pub fn shadow_host(&self) -> Option<OpaqueElement> {
self.current_host
}
}