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
use html5ever::rcdom::{self, Handle, NodeData};
use std::collections::BTreeMap;
/// Adds some convenience methods to the `html5ever::rcdom::Node` type
pub trait NodeExt: Sized {
/// Retrieves the node that these methods will work on
fn get_node(&self) -> &rcdom::Node;
/// Returns `true` if node is of type Document
fn is_document(&self) -> bool {
let node = self.get_node();
match node.data {
NodeData::Document { .. } => true,
_ => false,
}
}
/// Returns `true` if node is of type Doctype
fn is_doctype(&self) -> bool {
let node = self.get_node();
match node.data {
NodeData::Doctype { .. } => true,
_ => false,
}
}
/// Returns `true` if node is of type Text
fn is_text(&self) -> bool {
let node = self.get_node();
match node.data {
NodeData::Text { .. } => true,
_ => false,
}
}
/// Returns `true` if node is of type Comment
fn is_comment(&self) -> bool {
let node = self.get_node();
match node.data {
NodeData::Comment { .. } => true,
_ => false,
}
}
/// Returns `true` if node is of type ProcessingInstruction
fn is_processing_instruction(&self) -> bool {
let node = self.get_node();
match node.data {
NodeData::ProcessingInstruction { .. } => true,
_ => false,
}
}
/// Returns `true` if node is of type Element
fn is_element(&self) -> bool {
let node = self.get_node();
match node.data {
NodeData::Element { .. } => true,
_ => false,
}
}
/// Retrieves the name of the node
///
/// If this node is an element, the name of that element is returned.
/// Otherwise, special names are used:
///
/// * Document -> "\[document\]"
/// * Doctype -> "\[doctype\]"
/// * Text -> "\[text\]"
/// * Comment -> "\[comment\]"
/// * ProcessingInstruction -> "\[processing-instruction\]"
fn name(&self) -> &str {
let node = self.get_node();
match node.data {
NodeData::Document {
..
} => "[document]",
NodeData::Doctype {
..
} => "[doctype]",
NodeData::Text {
..
} => "[text]",
NodeData::Comment {
..
} => "[comment]",
NodeData::ProcessingInstruction {
..
} => "[processing-instruction]",
NodeData::Element {
ref name, ..
} => name.local.as_ref(),
}
}
/// Looks for an attribute named `attr` and returns it's value as a string
///
/// # Example
///
/// ```rust
/// # extern crate soup;
/// # use std::error::Error;
/// # use soup::prelude::*;
/// # fn main() -> Result<(), Box<Error>> {
/// let soup = Soup::new(r#"<div class="foo bar"></div>"#);
/// let div = soup.tag("div").find().expect("Couldn't find div");
/// assert_eq!(div.get("class"), Some("foo bar".to_string()));
/// # Ok(())
/// # }
/// ```
fn get(&self, attr: &str) -> Option<String> {
let node = self.get_node();
match node.data {
NodeData::Element {
ref attrs, ..
} => {
let attrs = attrs.borrow();
for it in attrs.iter() {
let name = it.name.local.as_ref();
if name.to_lowercase() == attr.to_lowercase() {
return Some(it.value.to_string());
}
}
None
},
_ => None,
}
}
/// Returns the node's attributes as a BTreeMap
fn attrs(&self) -> BTreeMap<String, String> {
let node = self.get_node();
match node.data {
NodeData::Element {
ref attrs, ..
} => {
let attrs = attrs.borrow();
attrs
.iter()
.map(|attr| (attr.name.local.to_string(), attr.value.to_string()))
.collect()
},
_ => BTreeMap::new(),
}
}
/// Retrieves the text value of this element, as well as it's child elements
fn text(&self) -> String {
let node = self.get_node();
let mut result = vec![];
extract_text(node, &mut result);
result.join("")
}
/// Returns the node as an html tag
fn display(&self) -> String {
let node = self.get_node();
match node.data {
NodeData::Element {
ref name,
ref attrs,
..
} => {
let c = node
.children
.borrow()
.iter()
.map(|child| child.display())
.collect::<Vec<_>>()
.join("");
let mut a = attrs
.borrow()
.iter()
.map(|attr| format!(r#"{}="{}""#, attr.name.local, attr.value.as_ref()))
.collect::<Vec<_>>();
a.sort();
let a = a.join(" ");
if a.is_empty() {
format!("<{}>{}</{}>", name.local.as_ref(), c, name.local.as_ref())
} else {
format!(
"<{} {}>{}</{}>",
name.local.as_ref(),
a,
c,
name.local.as_ref()
)
}
},
NodeData::Text {
ref contents, ..
} => contents.borrow().as_ref().to_string(),
NodeData::Comment {
ref contents, ..
} => format!("<!--{}-->", contents.as_ref()),
_ => "".to_string(),
}
}
/// Navigates to the parent of the node, if there is one
///
/// # Example
///
/// ```rust
/// extern crate soup;
///
/// use soup::prelude::*;
/// # use std::error::Error;
/// # fn main() -> Result<(), Box<Error>> {
/// let soup = Soup::new(r#"<div id=""><b>FOO</b></div>"#);
/// let b = soup.tag("b").find().expect("Couldn't find tag 'b'");
/// let div = b.parent().expect("Couldn't get parent of tag 'b'");
/// assert_eq!(div.name(), "div".to_string());
/// # Ok(())
/// # }
/// ```
fn parent(&self) -> Option<Handle> {
let node = self.get_node();
let parent = node.parent.take(); // leaves node.parent as Cell(None)
let parent_node = parent.clone();
node.parent.set(parent); // puts original parent back?
parent_node.and_then(|node| node.upgrade())
}
}
fn extract_text(node: &rcdom::Node, result: &mut Vec<String>) {
match node.data {
NodeData::Text {
ref contents, ..
} => result.push(contents.borrow().to_string()),
_ => (),
}
let children = node.children.borrow();
for child in children.iter() {
extract_text(child, result);
}
}
impl NodeExt for Handle {
#[inline(always)]
fn get_node(&self) -> &rcdom::Node {
&*self
}
}
impl<'node> NodeExt for &'node rcdom::Node {
#[inline(always)]
fn get_node(&self) -> &rcdom::Node {
self
}
}
#[cfg(test)]
mod tests {
use crate::prelude::*;
use std::collections::BTreeMap;
#[test]
fn name() {
let soup = Soup::new("<b>some text</b>");
let b = soup.tag("b").find().expect("Couldn't find tag 'b'");
let name = b.name();
assert_eq!(name, "b");
}
#[test]
fn get() {
let soup = Soup::new(r#"<div class="one two"></div>"#);
let div = soup.tag("div").find().expect("Couldn't find tag 'div'");
let class = div.get("class");
assert_eq!(class, Some("one two".to_string()));
}
#[test]
fn attrs() {
let soup = Soup::new(r#"<div class="one two" id="some-id"></div>"#);
let div = soup.tag("div").find().expect("Couldn't find tag 'div'");
let attrs = div.attrs();
let mut expected = BTreeMap::new();
expected.insert("class".to_string(), "one two".to_string());
expected.insert("id".to_string(), "some-id".to_string());
assert_eq!(attrs, expected);
}
#[test]
fn case_sensitive() {
let soup = Soup::new(r#"<div class="ONE TWO"></div>"#);
let one = soup.attr("class", "ONE").find();
assert!(one.is_some());
let one = soup.attr("class", "one").find();
assert!(one.is_none());
}
#[test]
fn display() {
let soup = Soup::new(r#"<div class="foo bar" id="baz"></div>"#);
let div = soup.tag("div").find().expect("Couldn't find tag 'div'");
assert_eq!(div.display(), r#"<div class="foo bar" id="baz"></div>"#);
let soup = Soup::new(r#"<div class="foo bar" id="baz"><b>SOME TEXT</b></div>"#);
let div = soup.tag("div").find().expect("Couldn't find tag 'div'");
assert_eq!(
div.display(),
r#"<div class="foo bar" id="baz"><b>SOME TEXT</b></div>"#
);
let soup = Soup::new(
r#"<div class="foo bar" id="baz"><b>SOME TEXT <!-- and a comment --></b></div>"#,
);
let div = soup.tag("div").find().expect("Couldn't find tag 'div'");
let b = div.tag("b").find().expect("Couldn't find tag 'b'");
assert_eq!(b.display(), r#"<b>SOME TEXT <!-- and a comment --></b>"#);
}
}