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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use node::{self, Node};

/// A trait implemented by all `Node` matchers.
pub trait Predicate {
    fn matches(&self, node: &Node) -> bool;
    fn or<T: Predicate>(self, other: T) -> Or<Self, T>
    where
        Self: Sized,
    {
        Or(self, other)
    }
    fn and<T: Predicate>(self, other: T) -> And<Self, T>
    where
        Self: Sized,
    {
        And(self, other)
    }
    fn not(self) -> Not<Self>
    where
        Self: Sized,
    {
        Not(self)
    }
    fn child<T: Predicate>(self, other: T) -> Child<Self, T>
    where
        Self: Sized,
    {
        Child(self, other)
    }
    fn descendant<T: Predicate>(self, other: T) -> Descendant<Self, T>
    where
        Self: Sized,
    {
        Descendant(self, other)
    }
}

/// Matches any Node.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Any;

impl Predicate for Any {
    fn matches(&self, _: &Node) -> bool {
        true
    }
}

/// Matches Element Node with name `T`.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Name<T>(pub T);

impl<'a> Predicate for Name<&'a str> {
    fn matches(&self, node: &Node) -> bool {
        node.name() == Some(self.0)
    }
}

/// Matches Element Node containing class `T`.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Class<T>(pub T);

impl<'a> Predicate for Class<&'a str> {
    fn matches(&self, node: &Node) -> bool {
        node.attr("class").map_or(false, |classes| {
            classes.split_whitespace().any(|class| class == self.0)
        })
    }
}

/// Matches if the Predicate `T` does not match.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Not<T>(pub T);

impl<T: Predicate> Predicate for Not<T> {
    fn matches(&self, node: &Node) -> bool {
        !self.0.matches(node)
    }
}

/// Matches Element Node containing attribute `N` with value `V` if `V` is an
/// `&str`, or any value if `V` is `()`.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Attr<N, V>(pub N, pub V);

impl<'a> Predicate for Attr<&'a str, &'a str> {
    fn matches(&self, node: &Node) -> bool {
        node.attr(self.0) == Some(self.1)
    }
}

impl<'a> Predicate for Attr<&'a str, ()> {
    fn matches(&self, node: &Node) -> bool {
        node.attr(self.0).is_some()
    }
}

/// Matches if the function returns true.
impl<F: Fn(&Node) -> bool> Predicate for F {
    fn matches(&self, node: &Node) -> bool {
        self(node)
    }
}

/// Matches any Element Node.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Element;

impl Predicate for Element {
    fn matches(&self, node: &Node) -> bool {
        match *node.data() {
            node::Data::Element(..) => true,
            _ => false,
        }
    }
}

/// Matches any Text Node.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Text;

impl Predicate for Text {
    fn matches(&self, node: &Node) -> bool {
        match *node.data() {
            node::Data::Text(..) => true,
            _ => false,
        }
    }
}

/// Matches any Comment Node.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Comment;

impl Predicate for Comment {
    fn matches(&self, node: &Node) -> bool {
        match *node.data() {
            node::Data::Comment(..) => true,
            _ => false,
        }
    }
}

/// Matches if either inner Predicate `A` or `B` matches the Node.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Or<A, B>(pub A, pub B);

impl<A: Predicate, B: Predicate> Predicate for Or<A, B> {
    fn matches(&self, node: &Node) -> bool {
        self.0.matches(node) || self.1.matches(node)
    }
}

/// Matches if the inner Predicate `A` and `B` both match the Node.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct And<A, B>(pub A, pub B);

impl<A: Predicate, B: Predicate> Predicate for And<A, B> {
    fn matches(&self, node: &Node) -> bool {
        self.0.matches(node) && self.1.matches(node)
    }
}

/// Matches if inner Predicate `B` matches the node and `A` matches the parent
/// of the node.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Child<A, B>(pub A, pub B);

impl<A: Predicate, B: Predicate> Predicate for Child<A, B> {
    fn matches(&self, node: &Node) -> bool {
        if let Some(parent) = node.parent() {
            self.1.matches(node) && self.0.matches(&parent)
        } else {
            false
        }
    }
}

/// Matches if inner Predicate `B` matches the node and `A` matches any of the
/// parents of node.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Descendant<A, B>(pub A, pub B);

impl<A: Predicate, B: Predicate> Predicate for Descendant<A, B> {
    fn matches(&self, node: &Node) -> bool {
        if self.1.matches(node) {
            let mut node = *node;
            while let Some(parent) = node.parent() {
                if self.0.matches(&parent) {
                    return true;
                }
                node = parent;
            }
        }
        false
    }
}