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
use crate::matcher::Matcher;
use crate::Selection;
use std::collections::HashSet;

impl<'a> Selection<'a> {
    /// Checks the current matched set of elements against a selector and
    /// returns true if at least one of these elements matches.
    pub fn is(&self, sel: &str) -> bool {
        if self.length() > 0 {
            return Matcher::new(sel)
                .map(|matcher| self.is_matcher(&matcher))
                .unwrap_or(false);
        }

        false
    }

    /// Checks the current matched set of elements against a matcher and
    /// returns true if at least one of these elements matches.
    pub fn is_matcher(&self, matcher: &Matcher) -> bool {
        if self.length() > 0 {
            return self
                .nodes()
                .into_iter()
                .filter(|node| matcher.match_element(*node))
                .count()
                > 0;
        }

        false
    }

    /// Checks the current matches set of elemets against a selection and
    /// returns true if at least one of these elements matches.
    pub fn is_selection(&self, sel: &Selection) -> bool {
        if self.length() == 0 || sel.length() == 0 {
            return false;
        }

        let mut m = HashSet::with_capacity(sel.length());
        for node in sel.nodes() {
            m.insert(node.id);
        }

        for node in self.nodes() {
            if m.contains(&node.id) {
                return true;
            }
        }

        false
    }
}