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
use std::fmt;
use std::iter;
use std::ops::Deref;
use crate::dom::{Document, Node, NodeId, StrTendril, push_if};
#[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>(&'a self, predicate: P)
-> impl Iterator<Item = NodeRef<'a>> + 'a
where P: FnMut(&NodeRef<'a>) -> bool + 'a
{
self.children().filter(predicate)
}
pub fn select<P>(&'a 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>(&'a self, predicate: P) -> Option<NodeRef<'a>>
where P: FnMut(&NodeRef<'a>) -> bool
{
self.children().find(predicate)
}
pub fn find<P>(&'a 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(&'a self) -> impl Iterator<Item = NodeRef<'a>> + 'a {
iter::successors(
self.for_some_node(self.first_child),
move |nref| self.for_some_node(nref.next_sibling)
)
}
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(&'a self) -> Option<NodeRef<'a>> {
self.for_some_node(self.parent)
}
pub fn prev_sibling(&'a self) -> Option<NodeRef<'a>> {
self.for_some_node(self.prev_sibling)
}
pub fn next_sibling(&'a self) -> Option<NodeRef<'a>> {
self.for_some_node(self.next_sibling)
}
pub fn text(&'a self) -> Option<StrTendril> {
self.doc.text(self.id)
}
pub fn deep_clone(&'a self) -> Document {
self.doc.deep_clone(self.id)
}
#[inline]
fn for_some_node(&'a 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: Vec<NodeId>,
predicate: P,
}
impl<'a, P> Selector<'a, P> {
fn new(doc: &'a Document, first: Option<NodeId>, predicate: P)
-> Selector<'a, P>
{
let next = if let Some(id) = first {
vec![id]
} else {
vec![]
};
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) {
push_if(&mut self.next, node.next_sibling);
return Some(node);
} else {
push_if(&mut self.next, node.next_sibling);
push_if(&mut self.next, node.first_child);
}
}
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))
}
}