nipper_trunk/property.rs
1use crate::Document;
2use crate::Selection;
3use tendril::StrTendril;
4
5impl Document {
6 /// Gets the HTML contents of the document. It includes
7 /// the text and comment nodes.
8 pub fn html(&self) -> StrTendril {
9 self.tree.root().html()
10 }
11
12 /// Gets the text content of the document.
13 pub fn text(&self) -> StrTendril {
14 self.tree.root().text()
15 }
16}
17
18impl<'a> Selection<'a> {
19 /// Gets the specified attribute's value for the first element in the
20 /// selection. To get the value for each element individually, use a looping
21 /// construct such as map method.
22 pub fn attr(&self, name: &str) -> Option<StrTendril> {
23 self.nodes().first().and_then(|node| node.attr(name))
24 }
25
26 /// Works like `attr` but returns default value if attribute is not present.
27 pub fn attr_or(&self, name: &str, default: &str) -> StrTendril {
28 self.attr(name).unwrap_or_else(|| StrTendril::from(default))
29 }
30
31 /// Sets the given attribute to each element in the set of matched elements.
32 pub fn set_attr(&mut self, name: &str, val: &str) {
33 for node in self.nodes() {
34 node.set_attr(name, val);
35 }
36 }
37
38 /// Removes the named attribute from each element in the set of matched elements.
39 pub fn remove_attr(&mut self, name: &str) {
40 for node in self.nodes() {
41 node.remove_attr(name);
42 }
43 }
44
45 /// Adds the given class to each element in the set of matched elements.
46 /// Multiple class names can be specified, separated by a space via multiple arguments.
47 pub fn add_class(&mut self, class: &str) {
48 for node in self.nodes() {
49 node.add_class(class);
50 }
51 }
52
53 /// Determines whether any of the matched elements are assigned the
54 /// given class.
55 pub fn has_class(&self, class: &str) -> bool {
56 self.nodes().iter().any(|node| node.has_class(class))
57 }
58
59 /// Removes the given class from each element in the set of matched elements.
60 /// Multiple class names can be specified, separated by a space via multiple arguments.
61 pub fn remove_class(&mut self, class: &str) {
62 for node in self.nodes() {
63 node.remove_class(class);
64 }
65 }
66
67 /// Returns the number of elements in the selection object.
68 pub fn length(&self) -> usize {
69 self.nodes().len()
70 }
71
72 /// Is an alias for `length`.
73 pub fn size(&self) -> usize {
74 self.length()
75 }
76
77 /// Is there any matched elements.
78 pub fn exists(&self) -> bool {
79 self.length() > 0
80 }
81
82 /// Gets the HTML contents of the first element in the set of matched
83 /// elements. It includes the text and comment nodes.
84 pub fn html(&self) -> StrTendril {
85 if self.length() > 0 {
86 return self.nodes().first().unwrap().html();
87 }
88
89 StrTendril::new()
90 }
91
92 /// Gets the combined text content of each element in the set of matched
93 /// elements, including their descendants.
94 pub fn text(&self) -> StrTendril {
95 let mut s = StrTendril::new();
96
97 for node in self.nodes() {
98 s.push_tendril(&node.text());
99 }
100
101 s
102 }
103}