-
Notifications
You must be signed in to change notification settings - Fork 35
/
axis.rs
429 lines (355 loc) · 11.5 KB
/
axis.rs
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
use std::fmt;
use crate::context;
use crate::node_test::NodeTest;
use crate::nodeset::{self, Node, OrderedNodes};
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum PrincipalNodeType {
Attribute,
Element,
Namespace,
}
/// A directed traversal of Nodes.
pub trait AxisLike: fmt::Debug {
/// Applies the given node test to the nodes selected by this axis,
/// adding matching nodes to the nodeset.
fn select_nodes<'c, 'd>(
&self,
context: &context::Evaluation<'c, 'd>,
node_test: &dyn NodeTest,
) -> OrderedNodes<'d>;
/// Describes what node type is naturally selected by this axis.
fn principal_node_type(&self) -> PrincipalNodeType {
PrincipalNodeType::Element
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Axis {
Ancestor,
AncestorOrSelf,
Attribute,
Namespace,
Child,
Descendant,
DescendantOrSelf,
Parent,
PrecedingSibling,
FollowingSibling,
Preceding,
Following,
SelfAxis,
}
struct CompleteNodeTest<'c, 'd> {
context: &'c context::Evaluation<'c, 'd>,
node_test: &'c dyn NodeTest,
result: OrderedNodes<'d>,
}
impl<'c, 'd> CompleteNodeTest<'c, 'd> {
fn new(context: &'c context::Evaluation<'c, 'd>, node_test: &'c dyn NodeTest) -> Self {
CompleteNodeTest {
context,
node_test,
result: OrderedNodes::new(),
}
}
fn run(&mut self, node: Node<'d>) {
let new_context = self.context.new_context_for(node);
self.node_test.test(&new_context, &mut self.result);
}
}
impl AxisLike for Axis {
fn select_nodes<'c, 'd>(
&self,
context: &context::Evaluation<'c, 'd>,
node_test: &dyn NodeTest,
) -> OrderedNodes<'d> {
use self::Axis::*;
let mut node_test = CompleteNodeTest::new(context, node_test);
match *self {
Ancestor => each_parent(context.node, |n| node_test.run(n)),
AncestorOrSelf => node_and_each_parent(context.node, |n| node_test.run(n)),
Attribute => {
if let Node::Element(ref e) = context.node {
for attr in e.attributes() {
node_test.run(Node::Attribute(attr));
}
}
}
Namespace => {
if let Node::Element(ref e) = context.node {
for ns in e.namespaces_in_scope() {
let ns = Node::Namespace(nodeset::Namespace {
parent: *e,
prefix: ns.prefix(),
uri: ns.uri(),
});
node_test.run(ns);
}
}
}
Child => {
for child in context.node.children() {
node_test.run(child);
}
}
Descendant => {
for child in context.node.children() {
preorder_left_to_right(child, |n| node_test.run(n));
}
}
DescendantOrSelf => preorder_left_to_right(context.node, |n| node_test.run(n)),
Parent => {
if let Some(parent) = context.node.parent() {
node_test.run(parent);
}
}
PrecedingSibling => {
for sibling in context.node.preceding_siblings() {
node_test.run(sibling)
}
}
FollowingSibling => {
for sibling in context.node.following_siblings() {
node_test.run(sibling)
}
}
Preceding => node_and_each_parent(context.node, |node| {
for sibling in node.preceding_siblings() {
postorder_right_to_left(sibling, |n| node_test.run(n));
}
}),
Following => node_and_each_parent(context.node, |node| {
for sibling in node.following_siblings() {
preorder_left_to_right(sibling, |n| node_test.run(n));
}
}),
SelfAxis => node_test.run(context.node),
}
node_test.result
}
fn principal_node_type(&self) -> PrincipalNodeType {
use self::Axis::*;
match *self {
Attribute => PrincipalNodeType::Attribute,
Namespace => PrincipalNodeType::Namespace,
_ => PrincipalNodeType::Element,
}
}
}
fn preorder_left_to_right<'d, F>(node: Node<'d>, mut f: F)
where
F: FnMut(Node<'d>),
{
let mut stack = vec![node];
while let Some(current) = stack.pop() {
f(current);
for child in current.children().into_iter().rev() {
stack.push(child);
}
}
}
// There's other implementations that only require a single stack; are
// those applicable? Are they better?
fn postorder_right_to_left<'d, F>(node: Node<'d>, mut f: F)
where
F: FnMut(Node<'d>),
{
let mut stack = vec![node];
let mut stack2 = vec![];
while let Some(current) = stack.pop() {
for child in current.children().into_iter().rev() {
stack.push(child);
}
stack2.push(current);
}
for current in stack2.into_iter().rev() {
f(current);
}
}
fn node_and_each_parent<'d, F>(node: Node<'d>, mut f: F)
where
F: FnMut(Node<'d>),
{
f(node);
each_parent(node, f);
}
fn each_parent<'d, F>(mut node: Node<'d>, mut f: F)
where
F: FnMut(Node<'d>),
{
while let Some(parent) = node.parent() {
f(parent);
node = parent;
}
}
#[cfg(test)]
mod test {
use sxd_document::dom;
use sxd_document::Package;
use crate::context::{self, Context};
use crate::node_test::NodeTest;
use crate::nodeset::{Node, OrderedNodes};
use super::Axis::*;
use super::*;
#[derive(Debug)]
struct DummyNodeTest;
impl NodeTest for DummyNodeTest {
fn test<'c, 'd>(
&self,
context: &context::Evaluation<'c, 'd>,
result: &mut OrderedNodes<'d>,
) {
result.add(context.node)
}
}
fn execute<'n, N>(axis: Axis, node: N) -> OrderedNodes<'n>
where
N: Into<Node<'n>>,
{
let context = Context::without_core_functions();
let context = context::Evaluation::new(&context, node.into());
let node_test = &DummyNodeTest;
axis.select_nodes(&context, node_test)
}
#[test]
fn ancestor_includes_parents() {
let package = Package::new();
let doc = package.as_document();
let level0 = doc.root();
let level1 = doc.create_element("b");
let level2 = doc.create_text("c");
level0.append_child(level1);
level1.append_child(level2);
let result = execute(Ancestor, level2);
assert_eq!(result, ordered_nodes![level1, level0]);
}
#[test]
fn ancestor_or_self_also_includes_self() {
let package = Package::new();
let doc = package.as_document();
let level0 = doc.root();
let level1 = doc.create_element("b");
let level2 = doc.create_text("c");
level0.append_child(level1);
level1.append_child(level2);
let result = execute(AncestorOrSelf, level2);
assert_eq!(result, ordered_nodes![level2, level1, level0]);
}
#[test]
fn descendant_includes_parents() {
let package = Package::new();
let doc = package.as_document();
let level0 = doc.root();
let level1 = doc.create_element("b");
let level2 = doc.create_text("c");
level0.append_child(level1);
level1.append_child(level2);
let result = execute(Descendant, level0);
assert_eq!(result, ordered_nodes![level1, level2]);
}
#[test]
fn descendant_or_self_also_includes_self() {
let package = Package::new();
let doc = package.as_document();
let level0 = doc.root();
let level1 = doc.create_element("b");
let level2 = doc.create_text("c");
level0.append_child(level1);
level1.append_child(level2);
let result = execute(DescendantOrSelf, level0);
assert_eq!(result, ordered_nodes![level0, level1, level2]);
}
#[test]
fn preceding_sibling_selects_in_reverse_document_order() {
let package = Package::new();
let doc = package.as_document();
let root = doc.root();
let child1 = doc.create_element("a");
let child2 = doc.create_comment("b");
let child3 = doc.create_processing_instruction("c", None);
root.append_child(child1);
root.append_child(child2);
root.append_child(child3);
let result = execute(PrecedingSibling, child3);
assert_eq!(result, ordered_nodes![child2, child1]);
}
#[test]
fn following_sibling_selects_in_document_order() {
let package = Package::new();
let doc = package.as_document();
let root = doc.root();
let child1 = doc.create_element("a");
let child2 = doc.create_comment("b");
let child3 = doc.create_processing_instruction("c", None);
root.append_child(child1);
root.append_child(child2);
root.append_child(child3);
let result = execute(FollowingSibling, child1);
assert_eq!(result, ordered_nodes![child2, child3]);
}
// <a0>
// <b0>
// <c0 />
// <c1 />
// </b0>
// <b1>
// <c2 />
// <c3 />
// <c4 />
// </b1>
// <b2>
// <c5 />
// <c6 />
// </b2>
// </a0>
struct PrecedingFollowing<'d> {
b: [dom::Element<'d>; 3],
c: [dom::Element<'d>; 7],
midpoint: dom::Element<'d>,
}
impl<'d> PrecedingFollowing<'d> {
fn new(doc: dom::Document<'d>) -> Self {
let a = doc.create_element("a");
let b0 = doc.create_element("b0");
let b1 = doc.create_element("b1");
let b2 = doc.create_element("b2");
let c0 = doc.create_element("c0");
let c1 = doc.create_element("c1");
let c2 = doc.create_element("c2");
let c3 = doc.create_element("c3");
let c4 = doc.create_element("c4");
let c5 = doc.create_element("c5");
let c6 = doc.create_element("c6");
a.append_child(b0);
a.append_child(b1);
a.append_child(b2);
b0.append_child(c0);
b0.append_child(c1);
b1.append_child(c2);
b1.append_child(c3);
b1.append_child(c4);
b2.append_child(c5);
b2.append_child(c6);
PrecedingFollowing {
midpoint: c3,
b: [b0, b1, b2],
c: [c0, c1, c2, c3, c4, c5, c6],
}
}
}
#[test]
fn preceding_selects_in_reverse_document_order() {
let package = Package::new();
let doc = package.as_document();
let PrecedingFollowing { b, c, midpoint } = PrecedingFollowing::new(doc);
let result = execute(Preceding, midpoint);
assert_eq!(result, ordered_nodes![c[2], c[1], c[0], b[0]]);
}
#[test]
fn following_selects_in_document_order() {
let package = Package::new();
let doc = package.as_document();
let PrecedingFollowing { b, c, midpoint } = PrecedingFollowing::new(doc);
let result = execute(Following, midpoint);
assert_eq!(result, ordered_nodes![c[4], b[2], c[5], c[6]]);
}
}