To insert a node node into a node parent before null or a node child, with an optional boolean suppressObservers (default false):
Let nodes be node’s children, if node is a
DocumentFragment node; otherwise « node ».
Let count be nodes’s size.
If count is 0, then return.
If node is a DocumentFragment node:
Remove its children with suppressObservers set to true.
Queue a tree mutation record for node with « », nodes, null, and null.
This step intentionally does not pay attention to suppressObservers.
If child is non-null:
For each live range whose start node is parent and start offset is greater than child’s index: increase its start offset by count.
For each live range whose end node is parent and end offset is greater than child’s index: increase its end offset by count.
Let previousSibling be child’s previous sibling or parent’s last child if child is null.
For each node in nodes, in tree order:
Adopt node into parent’s node document.
Otherwise, insert node into parent’s children before child’s index.
If parent is a shadow host whose shadow root’s
slot assignment is "named" and node is a
slottable, then assign a slot for node.
If parent’s root is a shadow root, and parent is a slot whose assigned nodes is the empty list, then run signal a slot change for parent.
Run assign slottables for a tree with node’s root.
For each shadow-including inclusive descendant inclusiveDescendant of node, in shadow-including tree order:
Run the insertion steps with inclusiveDescendant.
If inclusiveDescendant is an element and inclusiveDescendant’s custom element registry is non-null:
If inclusiveDescendant’s custom element registry’s is scoped is true, then append inclusiveDescendant’s node document to inclusiveDescendant’s custom element registry’s scoped document set.
If inclusiveDescendant is custom, then
enqueue a custom element callback reaction with inclusiveDescendant,
callback name "connectedCallback", and « ».
Otherwise, try to upgrade inclusiveDescendant.
If this successfully upgrades inclusiveDescendant, its
connectedCallback will be enqueued automatically during the
upgrade an element algorithm.
Otherwise, if inclusiveDescendant is a shadow root, inclusiveDescendant’s custom element registry is non-null, and inclusiveDescendant’s custom element registry’s is scoped is true, then append inclusiveDescendant’s node document to inclusiveDescendant’s custom element registry’s scoped document set.
If suppressObservers is false, then queue a tree mutation record for parent with nodes, « », previousSibling, and child.
Run the children changed steps for parent.
Let staticNodeList be a list of nodes, initially « ».
We collect all nodes before calling the post-connection steps on any one of them, instead of calling the post-connection steps while we’re traversing the node tree. This is because the post-connection steps can modify the tree’s structure, making live traversal unsafe, possibly leading to the post-connection steps being called multiple times on the same node.
For each node of nodes, in tree order:
For each shadow-including inclusive descendant inclusiveDescendant of node, in shadow-including tree order: append inclusiveDescendant to staticNodeList.
For each node of staticNodeList: if node is connected, then run the post-connection steps with node.