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
use std::fmt;
use std::iter;
use std::ops::Deref;
use crate::dom::{Document, Node, NodeId, StrTendril, NodeStack1};
#[derive(Copy, Clone)]
pub struct NodeRef<'a>{
doc: &'a Document,
id: NodeId
}
impl<'a> NodeRef<'a> {
#[inline]
pub fn new(doc: &'a Document, id: NodeId) -> Self {
NodeRef { doc, id }
}
pub fn id(&self) -> NodeId {
self.id
}
pub fn select_children<P>(&self, predicate: P)
-> impl Iterator<Item = NodeRef<'a>> + 'a
where P: FnMut(&NodeRef<'a>) -> bool + 'a
{
self.children().filter(predicate)
}
pub fn select<P>(&self, predicate: P) -> Selector<'a, P>
where P: FnMut(&NodeRef<'a>) -> bool + 'a
{
Selector::new(self.doc, self.first_child, predicate)
}
pub fn find_child<P>(&self, predicate: P) -> Option<NodeRef<'a>>
where P: FnMut(&NodeRef<'a>) -> bool
{
self.children().find(predicate)
}
pub fn find<P>(&self, predicate: P) -> Option<NodeRef<'a>>
where P: FnMut(&NodeRef<'a>) -> bool + 'a
{
Selector::new(self.doc, self.first_child, predicate).next()
}
pub fn children(&self) -> impl Iterator<Item = NodeRef<'a>> + 'a {
let this = *self;
iter::successors(
this.for_some_node(this.first_child),
move |nref| this.for_some_node(nref.next_sibling)
)
}
pub fn descendants(&self) -> Descender<'a>
{
Descender::new(*self)
}
pub fn node_and_ancestors(&'a self)
-> impl Iterator<Item = NodeRef<'a>> + 'a
{
iter::successors(
Some(*self),
move |nref| self.for_some_node(nref.parent)
)
}
pub fn parent(&self) -> Option<NodeRef<'a>> {
self.for_some_node(self.parent)
}
pub fn prev_sibling(&self) -> Option<NodeRef<'a>> {
self.for_some_node(self.prev_sibling)
}
pub fn next_sibling(&self) -> Option<NodeRef<'a>> {
self.for_some_node(self.next_sibling)
}
pub fn text(&self) -> Option<StrTendril> {
self.doc.text(self.id)
}
pub fn deep_clone(&self) -> Document {
self.doc.deep_clone(self.id)
}
#[inline]
fn for_some_node(&self, id: Option<NodeId>) -> Option<NodeRef<'a>> {
if let Some(id) = id {
Some(NodeRef::new(self.doc, id))
} else {
None
}
}
}
impl<'a> Deref for NodeRef<'a> {
type Target = Node;
#[inline]
fn deref(&self) -> &Node {
&self.doc[self.id]
}
}
impl PartialEq for NodeRef<'_> {
fn eq(&self, other: &Self) -> bool {
std::ptr::eq(self.doc, other.doc) && self.id == other.id
}
}
impl fmt::Debug for NodeRef<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "NodeRef({:p}, {:?})", self.doc, self.id)
}
}
pub struct Selector<'a, P> {
doc: &'a Document,
next: NodeStack1,
predicate: P,
}
impl<'a, P> Selector<'a, P> {
fn new(doc: &'a Document, first: Option<NodeId>, predicate: P) -> Self {
let mut next = NodeStack1::new();
next.push_if(first);
Selector { doc, next, predicate }
}
}
impl<'a, P> Iterator for Selector<'a, P>
where P: FnMut(&NodeRef<'a>) -> bool + 'a
{
type Item = NodeRef<'a>;
fn next(&mut self) -> Option<Self::Item> {
while let Some(id) = self.next.pop() {
let node = NodeRef::new(self.doc, id);
if (self.predicate)(&node) {
self.next.push_if(node.next_sibling);
return Some(node);
} else {
self.next.push_if(node.next_sibling);
self.next.push_if(node.first_child);
}
}
None
}
}
pub struct Descender<'a> {
doc: &'a Document,
first: Option<NodeId>,
next: NodeStack1
}
impl<'a> Descender<'a> {
fn new(first: NodeRef<'a>) -> Self {
let mut next = NodeStack1::new();
next.push_if(first.first_child);
Descender { doc: first.doc, first: Some(first.id), next }
}
}
impl<'a> Iterator for Descender<'a>
{
type Item = NodeRef<'a>;
fn next(&mut self) -> Option<Self::Item> {
if let Some(id) = self.first.take() {
return Some(NodeRef::new(self.doc, id));
}
if let Some(id) = self.next.pop() {
let node = NodeRef::new(self.doc, id);
self.next.push_if(node.next_sibling);
self.next.push_if(node.first_child);
Some(node)
} else {
None
}
}
}
impl Document {
pub fn document_node_ref(&self) -> NodeRef<'_> {
NodeRef::new(self, Document::DOCUMENT_NODE_ID)
}
pub fn root_element_ref(&self) -> Option<NodeRef<'_>> {
self.root_element().map(|r| NodeRef::new(self, r))
}
}