nos/
query.rs

1use crate::matcher::Matcher;
2use crate::Selection;
3use std::collections::HashSet;
4
5impl<'a> Selection<'a> {
6    /// Checks the current matched set of elements against a selector and
7    /// returns true if at least one of these elements matches.
8    pub fn is(&self, sel: &str) -> bool {
9        if self.length() > 0 {
10            return Matcher::new(sel)
11                .map(|matcher| self.is_matcher(&matcher))
12                .unwrap_or(false);
13        }
14
15        false
16    }
17
18    /// Checks the current matched set of elements against a matcher and
19    /// returns true if at least one of these elements matches.
20    pub fn is_matcher(&self, matcher: &Matcher) -> bool {
21        if self.length() > 0 {
22            return self
23                .nodes()
24                .into_iter()
25                .filter(|node| matcher.match_element(*node))
26                .count()
27                > 0;
28        }
29
30        false
31    }
32
33    /// Checks the current matches set of elemets against a selection and
34    /// returns true if at least one of these elements matches.
35    pub fn is_selection(&self, sel: &Selection) -> bool {
36        if self.length() == 0 || sel.length() == 0 {
37            return false;
38        }
39
40        let mut m = HashSet::with_capacity(sel.length());
41        for node in sel.nodes() {
42            m.insert(node.id);
43        }
44
45        for node in self.nodes() {
46            if m.contains(&node.id) {
47                return true;
48            }
49        }
50
51        false
52    }
53}