Skip to content

Conversation

@xamgore
Copy link

@xamgore xamgore commented Jun 7, 2023

Imagine you would like to iterate through Text nodes. It may be helpful when you have non-structured text intermixed with markup nodes.

<div class="cls">
  <strong>Title</strong>
  1 | 2 | 3
  Love you
  <span>18+</span>
  John Doe
</div>
node.select(".cls").nodes().iter().filter(|it| it.query(|n| match n.data {
    NodeData::Text{ .. } => true,
    _ => false,
})),

The other way around would look like this:

node
  .select(".cls")
  .nodes()
  .iter()
  .flat_map(|node| node.children())
  .filter(|node| node.is_text())
  .map(|node| node.text())
  .collect::<Vec<_>>();

But this approach won't work for HTML comments.

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.

1 participant