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
use webcore::reference_type::ReferenceType;
use webapi::node_list::NodeList;
use webapi::element::Element;
use webapi::dom_exception::SyntaxError;
pub trait IParentNode: ReferenceType {
fn query_selector( &self, selector: &str ) -> Result< Option< Element >, SyntaxError > {
js_try!(
return @{self.as_ref()}.querySelector(@{selector});
).unwrap()
}
fn query_selector_all( &self, selector: &str ) -> Result< NodeList, SyntaxError > {
js_try!(
return @{self.as_ref()}.querySelectorAll(@{selector});
).unwrap()
}
}
#[cfg(all(test, feature = "web_test"))]
mod tests {
use super::*;
use stdweb::web::document;
use webapi::node::INode;
#[test]
fn test_query_selector_finds_h1() {
let parent = document().create_element("div").unwrap();
let child = document().create_element("h1").unwrap();
parent.append_child(&child);
assert_eq!(parent.query_selector("h1").unwrap().unwrap().as_ref(), child.as_ref());
}
#[test]
fn test_query_selector_all_finds_h1s() {
let parent = document().create_element("div").unwrap();
let child = document().create_element("h1").unwrap();
let child2 = document().create_element("h1").unwrap();
parent.append_child(&child);
parent.append_child(&child2);
assert_eq!(parent.query_selector_all("h1").unwrap().len(), 2);
}
#[test]
fn test_query_selector_finds_nested_h1() {
let parent = document().create_element("div").unwrap();
let child = document().create_element("p").unwrap();
let title = document().create_element("h1").unwrap();
child.append_child(&title);
parent.append_child(&child);
assert_eq!(parent.query_selector("h1").unwrap().unwrap().as_ref(), title.as_ref());
}
#[test]
fn test_query_selector_all_finds_nested_h1s() {
let parent = document().create_element("div").unwrap();
let top_title = document().create_element("h1").unwrap();
let child = document().create_element("p").unwrap();
let title = document().create_element("h1").unwrap();
child.append_child(&title);
parent.append_child(&child);
parent.append_child(&top_title);
assert_eq!(parent.query_selector_all("h1").unwrap().len(), 2);
}
#[test]
fn test_query_selector_not_found() {
let parent = document().create_element("div").unwrap();
let child = document().create_element("h1").unwrap();
parent.append_child(&child);
assert!(parent.query_selector("p").unwrap().is_none());
}
#[test]
fn test_query_selector_all_not_found() {
let parent = document().create_element("div").unwrap();
let child = document().create_element("h1").unwrap();
parent.append_child(&child);
assert_eq!(parent.query_selector_all("p").unwrap().len(), 0);
}
#[test]
fn test_query_selector_syntax_error() {
let parent = document().create_element("div").unwrap();
let child = document().create_element("h1").unwrap();
parent.append_child(&child);
assert!(child.query_selector("invalid syntax +#8$()@!(#").is_err());
}
#[test]
fn test_query_selector_all_syntax_error() {
let parent = document().create_element("div").unwrap();
let child = document().create_element("h1").unwrap();
parent.append_child(&child);
assert!(child.query_selector_all("invalid syntax +#8$()@!(#").is_err());
}
}