[whatwg/dom] Proposal: moveOrInsertBefore() (Issue #1406)

justinfagnani created an issue (whatwg/dom#1406)

### What problem are you trying to solve?

In adding support for `moveBefore()` to libraries, we have to add checks that mirror the Move operation in the spec. Something like:

```ts
// Do once per range of sibling nodes that need to be moved
const sameRoot = oldParent.getRootNode({composed: true}) === newParent.getRootNode({composed: true});   

const moveMethod = (node.nodeType !== 1 || node.nodeType !== 3 || node.nodeType !== 8)
    ? container.moveBefore
    : container.insertBefore;

moveMethod.call(container, node, refNode);
```

This is a bit tricky to get right because what preconditions you check depends on which are invariants for the particular type of move we're doing (many moves are within the same parent for instance, but some are not), and because the conditions are more complex than for methods like `insertBefore()`.

It also is redundant, as `moveBefore()` is already performing these checks, just to throw. In fact, we could do:

```ts
try {
  container.moveBefore(node, refNode);
} catch (e) {
  container.insertBefore(node, refNode);
}
```

but since moveBefore already knows whether we can use an atomic move or not, why can't we have a method that just does this check and fallback for us? IOW, we know we want to move the node - there's no exception where we decide to just not move it - and we want to preserve state if possible, so do that with as little ceremony as possible:

```ts
container.moveOrInsertBefore(node, refNode);
```

Frameworks and libraries really don't want to have to carry around extra code for checks the DOM could do, or perform redundant checks that the DOM is already doing.

### What solutions exist today?

_No response_

### How would you solve it?

_No response_

### Anything else?

Bulk mutation methods would help reduce repeated checks even in the DOM for ranges of nodes: https://github.com/whatwg/dom/issues/1369

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

Message ID: <whatwg/dom/issues/1406@github.com>

Received on Monday, 29 September 2025 02:16:51 UTC