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
use crate::Document;
use crate::Selection;
use tendril::StrTendril;
impl Document {
pub fn html(&self) -> StrTendril {
self.tree.root().html()
}
pub fn text(&self) -> StrTendril {
self.tree.root().text()
}
}
impl<'a> Selection<'a> {
pub fn attr(&self, name: &str) -> Option<StrTendril> {
self.nodes().first().and_then(|node| node.attr(name))
}
pub fn attr_or(&self, name: &str, default: &str) -> StrTendril {
self.attr(name).unwrap_or_else(|| StrTendril::from(default))
}
pub fn set_attr(&mut self, name: &str, val: &str) {
for node in self.nodes() {
node.set_attr(name, val);
}
}
pub fn remove_attr(&mut self, name: &str) {
for node in self.nodes() {
node.remove_attr(name);
}
}
pub fn add_class(&mut self, class: &str) {
for node in self.nodes() {
node.add_class(class);
}
}
pub fn has_class(&self, class: &str) -> bool {
self.nodes().iter().any(|node| node.has_class(class))
}
pub fn remove_class(&mut self, class: &str) {
for node in self.nodes() {
node.remove_class(class);
}
}
pub fn length(&self) -> usize {
self.nodes().len()
}
pub fn size(&self) -> usize {
self.length()
}
pub fn exists(&self) -> bool {
self.length() > 0
}
pub fn html(&self) -> StrTendril {
if self.length() > 0 {
return self.nodes().first().unwrap().html();
}
StrTendril::new()
}
pub fn text(&self) -> StrTendril {
let mut s = StrTendril::new();
for node in self.nodes() {
s.push_tendril(&node.text());
}
s
}
}