Skip to content

Recursive interate and skip some tags #154

Answered by renggli
filipenanclarez asked this question in Q&A
Discussion options

You must be logged in to vote

Probably the easiest is to verify that none of the ancestors of your matches also satisfy the condition:

final nodes = document.descendants
    // Find all the nodes that satisfy the condition.
    .where((node) => predicate(node))
    // Exclude the nodes that have parents satisfying the condition.
    .where((node) => !node.ancestors.any(predicate));

If you are only interested in elements, you might want to replace descendants and ancestors with descendantElements and ancestorElements. Also you might want to combine the where clauses into a single expression.

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by renggli
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants