oldNode.replaceWith(...collection) edge case

Currently, for `oldNode.replaceWith(…collection)`, if `collection` is array of multiple nodes, and `oldNode` is in `collection`, after the mutation method macro,  `oldNode` lives in a doc frag. So in the replace algorithm, `parent` is the doc frag, `node` is also the doc frag, an `HierarchyRequestError` is thrown.

I wonder if an error really should be thrown in this case? Intuitively, `collection` should be inserted before `oldNode`’s original next sibling.

For example:

```
<div id="d1"></div>
<div id="d2"></div>
<div id="d3"></div>
<div id="d4"></div>
```

Imagine `oldNode` is #d2, `collection` is [#d1,#d2,#d4], executing `oldNode.replaceWith(…collection)` should give

```
<div id="d1"></div>
<div id="d2"></div>
<div id="d4"></div>
<div id="d3"></div>
```

Instead of throwing an error.

To make it this work, before executing the mutation method macro, `oldNode`’s parent should be saved. It’s next sibling should also be saved, but the next sibling need to be found recursively if it happens to be in `collection` too.

So, If I’m not wrong, this edge case could work in principle. I’m not sure if there is any interest to allow this?

Received on Friday, 16 January 2015 05:38:27 UTC