From b3570f31e1e3346de9a13ee9fa054101e82bf429 Mon Sep 17 00:00:00 2001 From: Carlo Federico Vescovo <26970569+cfvescovo@users.noreply.github.com> Date: Tue, 16 Jul 2024 13:14:32 +0200 Subject: [PATCH] `is` and `has` support --- src/selector.rs | 26 ++++++++++++++++++++++++++ src/test.rs | 24 ++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/selector.rs b/src/selector.rs index 7ef13f2f..1e6da7df 100644 --- a/src/selector.rs +++ b/src/selector.rs @@ -86,6 +86,14 @@ pub struct Parser; impl<'i> parser::Parser<'i> for Parser { type Impl = Simple; type Error = SelectorParseErrorKind<'i>; + + fn parse_is_and_where(&self) -> bool { + true + } + + fn parse_has(&self) -> bool { + true + } } /// A simple implementation of `SelectorImpl` with no pseudo-classes or pseudo-elements. @@ -222,4 +230,22 @@ mod tests { let s = ""; let _sel: Selector = s.try_into().unwrap(); } + + #[test] + fn has_selector() { + let s = ":has(a)"; + let _sel: Selector = s.try_into().unwrap(); + } + + #[test] + fn is_selector() { + let s = ":is(a)"; + let _sel: Selector = s.try_into().unwrap(); + } + + #[test] + fn where_selector() { + let s = ":where(a)"; + let _sel: Selector = s.try_into().unwrap(); + } } diff --git a/src/test.rs b/src/test.rs index 199d450f..86d498dc 100644 --- a/src/test.rs +++ b/src/test.rs @@ -20,3 +20,27 @@ fn tag_with_newline() { Some("https://github.com/causal-agent/scraper") ); } + +#[test] +fn has_selector() { + let document = Html::parse_fragment( + r#" +
+
+ Hi There! +
+
+ + "#, + ); + + let selector = Selector::parse("div:has(div#foo) + ul > li:nth-child(2)").unwrap(); + + let mut iter = document.select(&selector); + let li = iter.next().unwrap(); + assert_eq!(li.inner_html(), "second"); +}