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
use blitz_traits::node_id::NodeId;
use selectors::SelectorList;
use smallvec::SmallVec;
use style::dom::{TDocument, TNode};
use style::dom_apis::{
MayUseInvalidation, QueryAll, QueryFirst, element_closest, element_matches, query_selector,
};
use style::selector_parser::{SelectorImpl, SelectorParser};
use style_traits::ParseError;
use crate::{BaseDocument, Node};
impl BaseDocument {
/// Find the node with the specified id attribute (if one exists).
/// If multiple nodes have the same id, the first in tree order is returned.
pub fn get_element_by_id(&self, id: &str) -> Option<NodeId> {
match self.nodes_to_id.get(id)?.as_slice() {
[] => None,
[node_id] => Some(*node_id),
candidates => self.first_in_tree_order(candidates),
}
}
/// Find the first of `candidates` in tree order
fn first_in_tree_order(&self, candidates: &[NodeId]) -> Option<NodeId> {
let mut stack = vec![self.root_node_id];
while let Some(node_id) = stack.pop() {
if candidates.contains(&node_id) {
return Some(node_id);
}
stack.extend(self.nodes[node_id].children.iter().rev().copied());
}
None
}
/// Add a node to the id-to-node map
pub(crate) fn add_to_id_map(&mut self, id: &str, node_id: NodeId) {
if id.is_empty() {
return;
}
let node_ids = self.nodes_to_id.entry(id.to_string()).or_default();
if !node_ids.contains(&node_id) {
node_ids.push(node_id);
}
}
/// Remove a node from the id-to-node map
pub(crate) fn remove_from_id_map(&mut self, id: &str, node_id: NodeId) {
if let Some(node_ids) = self.nodes_to_id.get_mut(id) {
node_ids.retain(|nid| *nid != node_id);
if node_ids.is_empty() {
self.nodes_to_id.remove(id);
}
}
}
/// Find the first node that matches the selector specified as a string
/// Returns:
/// - Err(_) if parsing the selector fails
/// - Ok(None) if nothing matches
/// - Ok(Some(node_id)) with the first node ID that matches if one is found
pub fn query_selector<'input>(
&self,
selector: &'input str,
) -> Result<Option<NodeId>, ParseError<'input>> {
self.query_selector_in(self.root_node_id, selector)
}
/// Find the first descendant of `scope` that matches the selector specified
/// as a string.
///
/// The scope node itself is never matched. Selector parts may match nodes
/// outside the scope while evaluating relationships between descendants.
///
/// Returns:
/// - `Err(_)` if parsing the selector fails
/// - `Ok(None)` if nothing matches
/// - `Ok(Some(node_id))` with the first matching descendant ID otherwise
pub fn query_selector_in<'input>(
&self,
scope: NodeId,
selector: &'input str,
) -> Result<Option<NodeId>, ParseError<'input>> {
let selector_list = self.try_parse_selector_list(selector)?;
Ok(self.query_selector_in_raw(scope, &selector_list))
}
/// Find the first descendant of the document root that matches the
/// selector(s) specified in `selector_list`.
///
/// The document root itself is never matched. Selector parts may match
/// nodes outside the scope while evaluating relationships between
/// descendants.
pub fn query_selector_raw(&self, selector_list: &SelectorList<SelectorImpl>) -> Option<NodeId> {
self.query_selector_in_raw(self.root_node_id, selector_list)
}
/// Find the first descendant of `scope` that matches the selector(s)
/// specified in `selector_list`.
///
/// The scope node itself is never matched. Selector parts may match nodes
/// outside the scope while evaluating relationships between descendants.
pub fn query_selector_in_raw(
&self,
scope: NodeId,
selector_list: &SelectorList<SelectorImpl>,
) -> Option<NodeId> {
let root_node = &self.nodes[scope];
let mut result = None;
query_selector::<&Node, QueryFirst>(
root_node,
selector_list,
&mut result,
self.may_use_invalidation_for(scope),
);
result.map(|node| node.id)
}
/// Find all nodes that match the selector specified as a string
/// Returns:
/// - `Err(_)` if parsing the selector fails
/// - `Ok(SmallVec<usize>)` with all matching nodes otherwise
pub fn query_selector_all<'input>(
&self,
selector: &'input str,
) -> Result<SmallVec<[NodeId; 32]>, ParseError<'input>> {
self.query_selector_all_in(self.root_node_id, selector)
}
/// Find all descendants of `scope` that match the selector specified as a
/// string, in tree order.
///
/// The scope node itself is never matched. Selector parts may match nodes
/// outside the scope while evaluating relationships between descendants.
///
/// Returns:
/// - `Err(_)` if parsing the selector fails
/// - `Ok(_)` with all matching descendant IDs otherwise
pub fn query_selector_all_in<'input>(
&self,
scope: NodeId,
selector: &'input str,
) -> Result<SmallVec<[NodeId; 32]>, ParseError<'input>> {
let selector_list = self.try_parse_selector_list(selector)?;
Ok(self.query_selector_all_in_raw(scope, &selector_list))
}
/// Find all descendants of the document root that match the selector(s)
/// specified in `selector_list`, in tree order.
///
/// The document root itself is never matched. Selector parts may match
/// nodes outside the scope while evaluating relationships between
/// descendants.
pub fn query_selector_all_raw(
&self,
selector_list: &SelectorList<SelectorImpl>,
) -> SmallVec<[NodeId; 32]> {
self.query_selector_all_in_raw(self.root_node_id, selector_list)
}
/// Find all descendants of `scope` that match the selector(s) specified in
/// `selector_list`, in tree order.
///
/// The scope node itself is never matched. Selector parts may match nodes
/// outside the scope while evaluating relationships between descendants.
pub fn query_selector_all_in_raw(
&self,
scope: NodeId,
selector_list: &SelectorList<SelectorImpl>,
) -> SmallVec<[NodeId; 32]> {
let root_node = &self.nodes[scope];
let mut results = SmallVec::new();
query_selector::<&Node, QueryAll>(
root_node,
selector_list,
&mut results,
self.may_use_invalidation_for(scope),
);
results.iter().map(|node| node.id).collect()
}
fn may_use_invalidation_for(&self, scope: NodeId) -> MayUseInvalidation {
if scope == self.root_node_id {
MayUseInvalidation::Yes
} else {
MayUseInvalidation::No
}
}
/// Test whether the node identified by `node_id` matches the selector
/// specified as a string.
///
/// Non-element nodes never match.
pub fn matches_selector<'input>(
&self,
node_id: NodeId,
selector: &'input str,
) -> Result<bool, ParseError<'input>> {
let selector_list = self.try_parse_selector_list(selector)?;
Ok(self.nodes[node_id].matches_selector_raw(&selector_list))
}
/// Find the closest matching element at or above the node identified by
/// `node_id`.
///
/// Non-element nodes never match and return `None`.
pub fn closest<'input>(
&self,
node_id: NodeId,
selector: &'input str,
) -> Result<Option<NodeId>, ParseError<'input>> {
let selector_list = self.try_parse_selector_list(selector)?;
Ok(self.nodes[node_id].closest_raw(&selector_list))
}
pub fn try_parse_selector_list<'input>(
&self,
input: &'input str,
) -> Result<SelectorList<SelectorImpl>, ParseError<'input>> {
let url_extra_data = self.url.url_extra_data();
SelectorParser::parse_author_origin_no_namespace(input, &url_extra_data)
}
}
impl Node {
/// Find the first descendant of this node that matches the selector(s)
/// specified in `selector_list`.
///
/// The scope node itself is never matched. Selector parts may match nodes
/// outside the scope while evaluating relationships between descendants.
///
/// Text and comment scope nodes return no matches.
pub fn query_selector_raw(&self, selector_list: &SelectorList<SelectorImpl>) -> Option<NodeId> {
let mut result = None;
query_selector::<&Node, QueryFirst>(
self,
selector_list,
&mut result,
MayUseInvalidation::No,
);
result.map(|node| node.id)
}
/// Find all descendants of this node that match the selector(s) specified
/// in `selector_list`, in tree order.
///
/// The scope node itself is never matched. Selector parts may match nodes
/// outside the scope while evaluating relationships between descendants.
///
/// Text and comment scope nodes return no matches.
pub fn query_selector_all_raw(
&self,
selector_list: &SelectorList<SelectorImpl>,
) -> SmallVec<[NodeId; 32]> {
let mut results = SmallVec::new();
query_selector::<&Node, QueryAll>(
self,
selector_list,
&mut results,
MayUseInvalidation::No,
);
results.iter().map(|node| node.id).collect()
}
/// Test whether this element matches the selector(s) specified in
/// `selector_list`.
///
/// Non-element nodes never match.
pub fn matches_selector_raw(&self, selector_list: &SelectorList<SelectorImpl>) -> bool {
if !self.is_element() {
return false;
}
element_matches(&self, selector_list, self.owner_doc().quirks_mode())
}
/// Find the closest matching element at or above this element.
///
/// Non-element nodes never match and return `None`.
pub fn closest_raw(&self, selector_list: &SelectorList<SelectorImpl>) -> Option<NodeId> {
if !self.is_element() {
return None;
}
element_closest(self, selector_list, self.owner_doc().quirks_mode()).map(|node| node.id)
}
}