Skip to content

findOne does not recurse properly #1856

@zachsents

Description

@zachsents

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions