rust-scraper/scraper

select() doesn't work on current ElementRef

jelmer opened this issue · 2 comments

The following code crashes, since on the last line, the selector doesn't find the "div" element. I would have expected it to succeed and for the selector to also look at the root element.

        let fragment = Html::parse_fragment("<div>foo</div>");
        let sel = Selector::parse("div").unwrap();

        let el1 = fragment.root_element().select(&sel).next().unwrap();
        assert_eq!(el1.value().name(), "div");
        let el2 = el1.select(&sel).next().unwrap();
        assert_eq!(el1, el2);

Not entirely sure if this is a bug, but at the very least I found it surprising - is this WAI?

Line https://github.com/causal-agent/scraper/blob/master/src/element_ref/mod.rs#L45C1-L45C48 explicitly skips the root. I'm guessing it's there for a reason, but none of the tests break if I removing it and removing that line fixes my test fragment above.

If you want to test the current element, you can use Selector::matches.

I'm not the author, but this is my preferred behavior so that repeatedly applying a selector won't just match the same element over and over again.