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

Function removeDomNodes removes only half expected children #523

Closed
schoenobates opened this issue Dec 8, 2020 · 0 comments · Fixed by #583
Closed

Function removeDomNodes removes only half expected children #523

schoenobates opened this issue Dec 8, 2020 · 0 comments · Fixed by #583
Labels
bug Something isn't working hilla

Comments

@schoenobates
Copy link

The indexing in removeDomNodes is incorrect - as the nodes are removed the nodes.length decreases meaning it will remove half the nodes expected. The nodes list is an HTMLCollection which is a live reflection on the parents children.

function removeDomNodes(nodes) {
  if (nodes && nodes.length) {
    const parent = nodes[0].parentNode;

    for (let i = 0; i < nodes.length; i++) {
      parent.removeChild(nodes[i]);
    }
  }
}

This could be changed to:

function removeDomNodes(nodes) {
 if (nodes && nodes.length) {
   const parent = nodes[0].parentNode;
   const childrenCount = nodes.length - 1;

   for (let i = childrenCount; i >= 0; i--) {
     parent.removeChild(nodes[i]);
   }
 }
}

So should also work if a plain array is passed in...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working hilla
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants