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
use super::{Comments, ElementIterator, Elements, Select, TextNodes};
use crate::tree::NodeRef;
/// Convenience methods for node iterators.
pub trait NodeIterator: Sized + Iterator<Item = NodeRef> {
/// Filter this element iterator to elements.
#[inline]
fn elements(self) -> Elements<Self> {
Elements(self)
}
/// Filter this node iterator to text nodes.
#[inline]
fn text_nodes(self) -> TextNodes<Self> {
TextNodes(self)
}
/// Filter this node iterator to comment nodes.
#[inline]
fn comments(self) -> Comments<Self> {
Comments(self)
}
/// Filter this node iterator to elements maching the given selectors.
///
/// # Errors
///
/// Returns `Err(())` if the selector string fails to parse.
#[inline]
fn select(self, selectors: &str) -> Result<Select<Elements<Self>>, ()> {
self.elements().select(selectors)
}
/// Detach all nodes in this iterator from their parents.
///
/// # Examples
///
/// ```
/// use brik::parse_html;
/// use brik::traits::*;
///
/// let html = r#"<div><p>One</p><p>Two</p><p>Three</p></div>"#;
///
/// let doc = parse_html().one(html);
/// let div = doc.select_first("div").unwrap();
///
/// // Detach all paragraph elements
/// let paragraphs: Vec<_> = div
/// .as_node()
/// .descendants()
/// .select("p")
/// .unwrap()
/// .collect();
///
/// paragraphs
/// .into_iter()
/// .map(|p| p.as_node().clone())
/// .detach_all();
///
/// assert_eq!(div.as_node().children().elements().count(), 0);
/// ```
#[inline]
fn detach_all(self) {
for node in self {
node.detach();
}
}
}
impl<I> NodeIterator for I where I: Iterator<Item = NodeRef> {}
#[cfg(test)]
mod tests {
use crate::html5ever::tendril::TendrilSink;
use crate::iter::NodeIterator;
use crate::parse_html;
/// Tests filtering iterator to text nodes.
///
/// Verifies that text_nodes() correctly filters a node iterator to
/// include only text nodes, finding all text content in the tree.
#[test]
fn text_nodes() {
let html = "<div>text1<p>text2</p>text3</div>";
let doc = parse_html().one(html);
let div = doc.select("div").unwrap().next().unwrap();
let text_nodes: Vec<_> = div.as_node().descendants().text_nodes().collect();
assert_eq!(text_nodes.len(), 3);
}
/// Tests filtering iterator to comment nodes.
///
/// Verifies that comments() correctly filters a node iterator to
/// include only comment nodes.
#[test]
fn comments() {
let html = "<div><!-- comment1 --><p>text</p><!-- comment2 --></div>";
let doc = parse_html().one(html);
let div = doc.select("div").unwrap().next().unwrap();
let comments: Vec<_> = div.as_node().descendants().comments().collect();
assert_eq!(comments.len(), 2);
}
/// Tests detaching all nodes in an iterator.
///
/// Verifies that detach_all() removes all nodes in the iterator
/// from their parents, leaving the parent element empty.
#[test]
fn detach_all() {
let html = "<div><p>One</p><p>Two</p><p>Three</p></div>";
let doc = parse_html().one(html);
let div = doc.select("div").unwrap().next().unwrap();
let paragraphs: Vec<_> = div.as_node().descendants().select("p").unwrap().collect();
paragraphs
.into_iter()
.map(|p| p.as_node().clone())
.detach_all();
assert_eq!(div.as_node().children().elements().count(), 0);
}
}