[whatwg/dom] Node.setChildren() (#862)

`element.insertBefore(child, reference)` and `element.appendChild(child)` are not the most convenient way to update for example a list of items. `element.setChildren(children)` would make it so much easier to work with lists.

Here's an example solution for updating the children:

```js
function setChildren (parent, children) {
  let traverse = parent.firstChild;

  // go through children and place if not in place
  for (let i = 0; i < children.length; i++) {
    const child = children[i];

    if (traverse === child) {
      // already in place
      traverse = traverse.nextSibling;
    } else if (traverse) {
      parent.insertBefore(child, traverse);
    } else {
      parent.appendChild(child);
    }
  }

  // remove all the rest
  while (traverse) {
    const next = traverse.nextSibling;
    parent.removeChild(traverse);
    traverse = next;
  }
}
```

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/862

Received on Wednesday, 6 May 2020 10:11:02 UTC