use crate::node::{self, Node}; /// A trait implemented by all `Node` matchers. pub trait Predicate { fn matches(&self, node: &Node) -> bool; fn or(self, other: T) -> Or where Self: Sized, { Or(self, other) } fn and(self, other: T) -> And where Self: Sized, { And(self, other) } fn not(self) -> Not where Self: Sized, { Not(self) } fn child(self, other: T) -> Child where Self: Sized, { Child(self, other) } fn descendant(self, other: T) -> Descendant where Self: Sized, { Descendant(self, other) } } /// Matches any Node. #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct Any; impl Predicate for Any { fn matches(&self, _: &Node) -> bool { true } } /// Matches Element Node with name `T`. #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct Name(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, Eq)] pub struct Class(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, Eq)] pub struct Not(pub T); impl Predicate for Not { 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, Eq)] pub struct Attr(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 bool> Predicate for F { fn matches(&self, node: &Node) -> bool { self(node) } } /// Matches any Element Node. #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct Element; impl Predicate for Element { fn matches(&self, node: &Node) -> bool { matches!(*node.data(), node::Data::Element(..)) } } /// Matches any Text Node. #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct Text; impl Predicate for Text { fn matches(&self, node: &Node) -> bool { matches!(*node.data(), node::Data::Text(..)) } } /// Matches any Comment Node. #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct Comment; impl Predicate for Comment { fn matches(&self, node: &Node) -> bool { matches!(*node.data(), node::Data::Comment(..)) } } /// Matches if either inner Predicate `A` or `B` matches the Node. #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub struct Or(pub A, pub B); impl Predicate for Or { 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, Eq)] pub struct And(pub A, pub B); impl Predicate for And { 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, Eq)] pub struct Child(pub A, pub B); impl Predicate for Child { 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, Eq)] pub struct Descendant(pub A, pub B); impl Predicate for Descendant { 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 } }