Skip to content

Commit b3570f3

Browse files
committed
is and has support
1 parent e8e3cc4 commit b3570f3

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/selector.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ pub struct Parser;
8686
impl<'i> parser::Parser<'i> for Parser {
8787
type Impl = Simple;
8888
type Error = SelectorParseErrorKind<'i>;
89+
90+
fn parse_is_and_where(&self) -> bool {
91+
true
92+
}
93+
94+
fn parse_has(&self) -> bool {
95+
true
96+
}
8997
}
9098

9199
/// A simple implementation of `SelectorImpl` with no pseudo-classes or pseudo-elements.
@@ -222,4 +230,22 @@ mod tests {
222230
let s = "<failing selector>";
223231
let _sel: Selector = s.try_into().unwrap();
224232
}
233+
234+
#[test]
235+
fn has_selector() {
236+
let s = ":has(a)";
237+
let _sel: Selector = s.try_into().unwrap();
238+
}
239+
240+
#[test]
241+
fn is_selector() {
242+
let s = ":is(a)";
243+
let _sel: Selector = s.try_into().unwrap();
244+
}
245+
246+
#[test]
247+
fn where_selector() {
248+
let s = ":where(a)";
249+
let _sel: Selector = s.try_into().unwrap();
250+
}
225251
}

src/test.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,27 @@ fn tag_with_newline() {
2020
Some("https://github.com/causal-agent/scraper")
2121
);
2222
}
23+
24+
#[test]
25+
fn has_selector() {
26+
let document = Html::parse_fragment(
27+
r#"
28+
<div>
29+
<div id="foo">
30+
Hi There!
31+
</div>
32+
</div>
33+
<ul>
34+
<li>first</li>
35+
<li>second</li>
36+
<li>third</li>
37+
</ul>
38+
"#,
39+
);
40+
41+
let selector = Selector::parse("div:has(div#foo) + ul > li:nth-child(2)").unwrap();
42+
43+
let mut iter = document.select(&selector);
44+
let li = iter.next().unwrap();
45+
assert_eq!(li.inner_html(), "second");
46+
}

0 commit comments

Comments
 (0)