stdweb/webapi/
parent_node.rs

1use webcore::reference_type::ReferenceType;
2use webapi::node_list::NodeList;
3use webapi::element::Element;
4use webapi::dom_exception::SyntaxError;
5
6/// The `ParentNode` mixin contains methods and properties
7/// that are common to all types of `Node` objects that can
8/// have children.
9///
10/// You most likely don't want to `use` this directly; instead
11/// you should `use stdweb::traits::*;`.
12///
13/// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/ParentNode)
14// https://dom.spec.whatwg.org/#parentnode
15pub trait IParentNode: ReferenceType {
16    /// Returns the first element that is a descendant of the element on which it is
17    /// invoked that matches the specified group of selectors.
18    ///
19    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Element/querySelector)
20    // https://dom.spec.whatwg.org/#ref-for-dom-parentnode-queryselector
21    fn query_selector( &self, selector: &str ) -> Result< Option< Element >, SyntaxError > {
22        js_try!(
23            return @{self.as_ref()}.querySelector(@{selector});
24        ).unwrap()
25    }
26
27    /// Returns a non-live [NodeList](struct.NodeList.html) of all elements descended
28    /// from the element on which it is invoked that matches the specified group of CSS selectors.
29    ///
30    /// [(JavaScript docs)](https://developer.mozilla.org/en-US/docs/Web/API/Element/querySelectorAll)
31    // https://dom.spec.whatwg.org/#ref-for-dom-parentnode-queryselectorall
32    fn query_selector_all( &self, selector: &str ) -> Result< NodeList, SyntaxError > {
33        js_try!(
34            return @{self.as_ref()}.querySelectorAll(@{selector});
35        ).unwrap()
36    }
37}
38
39#[cfg(all(test, feature = "web_test"))]
40mod tests {
41    use super::*;
42    use stdweb::web::document;
43    use webapi::node::INode;
44
45    #[test]
46    fn test_query_selector_finds_h1() {
47        let parent = document().create_element("div").unwrap();
48        let child = document().create_element("h1").unwrap();
49        parent.append_child(&child);
50
51        assert_eq!(parent.query_selector("h1").unwrap().unwrap().as_ref(), child.as_ref());
52    }
53
54
55    #[test]
56    fn test_query_selector_all_finds_h1s() {
57        let parent = document().create_element("div").unwrap();
58        let child = document().create_element("h1").unwrap();
59        let child2 = document().create_element("h1").unwrap();
60        parent.append_child(&child);
61        parent.append_child(&child2);
62
63        assert_eq!(parent.query_selector_all("h1").unwrap().len(), 2);
64    }
65
66    #[test]
67    fn test_query_selector_finds_nested_h1() {
68        let parent = document().create_element("div").unwrap();
69        let child = document().create_element("p").unwrap();
70        let title = document().create_element("h1").unwrap();
71        child.append_child(&title);
72        parent.append_child(&child);
73
74        assert_eq!(parent.query_selector("h1").unwrap().unwrap().as_ref(), title.as_ref());
75    }
76
77    #[test]
78    fn test_query_selector_all_finds_nested_h1s() {
79        let parent = document().create_element("div").unwrap();
80        let top_title = document().create_element("h1").unwrap();
81        let child = document().create_element("p").unwrap();
82        let title = document().create_element("h1").unwrap();
83        child.append_child(&title);
84        parent.append_child(&child);
85        parent.append_child(&top_title);
86
87        assert_eq!(parent.query_selector_all("h1").unwrap().len(), 2);
88    }
89
90    #[test]
91    fn test_query_selector_not_found() {
92        let parent = document().create_element("div").unwrap();
93        let child = document().create_element("h1").unwrap();
94        parent.append_child(&child);
95
96        assert!(parent.query_selector("p").unwrap().is_none());
97    }
98
99    #[test]
100    fn test_query_selector_all_not_found() {
101        let parent = document().create_element("div").unwrap();
102        let child = document().create_element("h1").unwrap();
103        parent.append_child(&child);
104
105        assert_eq!(parent.query_selector_all("p").unwrap().len(), 0);
106    }
107
108    #[test]
109    fn test_query_selector_syntax_error() {
110        let parent = document().create_element("div").unwrap();
111        let child = document().create_element("h1").unwrap();
112        parent.append_child(&child);
113
114        assert!(child.query_selector("invalid syntax +#8$()@!(#").is_err());
115    }
116
117    #[test]
118    fn test_query_selector_all_syntax_error() {
119        let parent = document().create_element("div").unwrap();
120        let child = document().create_element("h1").unwrap();
121        parent.append_child(&child);
122
123        assert!(child.query_selector_all("invalid syntax +#8$()@!(#").is_err());
124    }
125}