This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.

Bug 27650 - Add range.insert(nodes...)
Summary: Add range.insert(nodes...)
Status: RESOLVED WONTFIX
Alias: None
Product: WebAppsWG
Classification: Unclassified
Component: DOM (show other bugs)
Version: unspecified
Hardware: All All
: P2 normal
Target Milestone: ---
Assignee: Anne
QA Contact: public-webapps-bugzilla
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-12-18 05:55 UTC by Glen Huang
Modified: 2016-08-19 12:23 UTC (History)
4 users (show)

See Also:


Attachments

Description Glen Huang 2014-12-18 05:55:06 UTC
Since DOM starts to allow multiple nodes to be manipulated in bulk (e.g., node.before, node.after), does it make sense to allow multiple nodes to be inserted into a range as well?
For example, range.insertNodes(nodes).

Also have a quick question on the algorithm of node.before and likes:

The before(nodes) method must run these steps:

1.If the context object does not have a parent, terminate these steps.
2.Run the mutation method macro.
3.Pre-insert node into the context object's parent before the context object.

Shouldn't step 3 be something like "For each node in nodes ...", node was never defined in the algorithm. But maybe I missed something.
Comment 1 Arkadiusz Michalski (Spirit) 2014-12-18 07:44:50 UTC
(In reply to Glen Huang from comment #0)
> Since DOM starts to allow multiple nodes to be manipulated in bulk (e.g.,
> node.before, node.after), does it make sense to allow multiple nodes to be
> inserted into a range as well?
> For example, range.insertNodes(nodes).

This can be done via range.insertNodes(documentFragment).

> The before(nodes) method must run these steps:
> 
> 1.If the context object does not have a parent, terminate these steps.
> 2.Run the mutation method macro.
> 3.Pre-insert node into the context object's parent before the context object.
> 
> Shouldn't step 3 be something like "For each node in nodes ...", node was
> never defined in the algorithm. But maybe I missed something.

In step 2. we have "mutation method macro" which make DocumentFragment (when we have more than one node) or node, so this is correct.
Comment 2 Glen Huang 2014-12-18 07:53:48 UTC
Ah, that macro, I know I missed something. Thank you.

About inserting document fragment into the range, this is exactly what I'm proposing. Instead of asking users to manually create it, DOM should take care of it, much like how it provides sugar like node.append, which is just document fragment + node.appendChild.

In this case, range.insertNodes is sugar for document fragment + range.insertNode.
Comment 3 Arkadiusz Michalski (Spirit) 2014-12-18 08:09:52 UTC
Or extend insertNode() if it doesn't breake anything:

void insertNode((Node or DOMString)... nodes);

but the name of the method will be misleading.
Comment 4 Glen Huang 2014-12-18 08:16:24 UTC
Or just range.insert(nodes), like node.appendChild -> node.append.
Comment 5 Joris van der Wel 2015-08-03 10:16:56 UTC
Why not make DocumentFragment easier to use instead?

For example: 

```
range.insertNode(new DocumentFragment(node1, 'a text node', node2));
range.insertNode(new DocumentFragment(document.getElementsByClassName('foo')));
range.insertNode([node1, node2]);
range.insertNode(new Set([node1, node2])); // iterable
```

This prevents having to change the spec in a lot of places.
Comment 6 Anne 2016-08-17 12:05:10 UTC
Do you know if there's any implementer interest for these ideas? And precedence in JavaScript libraries? I'm leaning towards closing this, but you can always open a new issue on GitHub.
Comment 7 Anne 2016-08-19 12:23:06 UTC
FWIW, https://github.com/whatwg/dom/issues/new is the new place.