Skip to content

Conversation

Noname-Official
Copy link
Contributor

use scraper::{Html, Selector};

const HTML: &str = r"
<div>
    <p>Hello</p>
    <p>World</p>
</div>
";

let html = Html::parse_fragment(HTML);
let selector = Selector::parse("div").unwrap();
let div = html.select(&selector).next().unwrap();
let selector = Selector::parse("p").unwrap();
let mut elements = div.select(&selector);
assert!(elements.next().is_some());
assert!(elements.next().is_some());
assert!(elements.next().is_none());
assert!(elements.next().is_some());

@adamreichold
Copy link
Member

adamreichold commented Jul 6, 2024

Sorry for introducing this bug and thank you for fixing it! I think I made the wrong assumption here that Traverse would be fused, but it reuses its initial state as its final state and therefore just restarts iterating the subtree after completing it once.

I do think it would be better to fix Traverse, e.g. make it fused by giving it a root: Option<NodeRef<'a, T>> field that is set to None once iteration is complete to differentiate its initial and final state (without increasing its size). But I fear, if we ever want to make changes to ego-tree, we'll have to fork it as the author seems unresponsive, so this certainly is the better short-term fix.

@cfvescovo
Copy link
Member

Merging this as a temporary fix

@cfvescovo cfvescovo merged commit 37b062e into rust-scraper:master Jul 16, 2024
@renovate renovate bot mentioned this pull request Jul 22, 2024
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants