Skip to content

Commit

Permalink
Merge pull request #187 from causal-agent/support-has-selector
Browse files Browse the repository at this point in the history
`is` and `has` support
  • Loading branch information
cfvescovo authored Jul 22, 2024
2 parents e8e3cc4 + b3570f3 commit 27dd786
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -222,4 +230,22 @@ mod tests {
let s = "<failing selector>";
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();
}
}
24 changes: 24 additions & 0 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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#"
<div>
<div id="foo">
Hi There!
</div>
</div>
<ul>
<li>first</li>
<li>second</li>
<li>third</li>
</ul>
"#,
);

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");
}

0 comments on commit 27dd786

Please sign in to comment.