Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

findOne does not recurse properly #1856

Closed
zachsents opened this issue Dec 31, 2024 · 0 comments
Closed

findOne does not recurse properly #1856

zachsents opened this issue Dec 31, 2024 · 0 comments

Comments

@zachsents
Copy link
Contributor

The findOne helper is meant to recursively find an element in a tree that passes the provided test function. However, the implementation is written such that the function will return the result of the search through the first node that has any children, regardless of whether a passing element was found in that branch.

See the implementation here. The problem occurs on Line 115.

Consider the tree:

<div>
  <p>Paragraph</p>
  <b>Text</b>
</div>

When searching for the paragraph, it's successful:

const found = findOne(el => el.tagName === "p", nodes)
console.log(found)
// Element { ... }

When searching for the bold tag, however, it fails:

const found = findOne(el => el.tagName === "b", nodes)
console.log(found)
// undefined

This happens because the paragraph has children, so the current implementation of findOne enters that recursion branch and returns regardless of the result.

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

No branches or pull requests

1 participant