if (ec)
return false;
- NodeVector targets;
- collectTargetNodes(newChild.get(), targets);
+ bool isFragment = newChild->nodeType() == DOCUMENT_FRAGMENT_NODE && !newChild->isShadowRoot();
// Add the new child(ren)
- for (NodeVector::const_iterator it = targets.begin(); it != targets.end(); ++it) {
- Node* child = it->get();
-
+ RefPtr<Node> child = isFragment ? newChild->firstChild() : newChild;
+ while (child) {
// If the new child is already in the right place, we're done.
if (prev && (prev == child || prev == child->previousSibling()))
break;
+ // For a fragment we have more children to do.
+ RefPtr<Node> nextChild = isFragment ? child->nextSibling() : 0;
+
// Remove child from its old position.
if (ContainerNode* oldParent = child->parentNode())
- oldParent->removeChild(child, ec);
+ oldParent->removeChild(child.get(), ec);
if (ec)
return false;
// Due to arbitrary code running in response to a DOM mutation event it's
- // possible that "next" is no longer a child of "this".
+ // possible that "prev" is no longer a child of "this".
// It's also possible that "child" has been inserted elsewhere.
// In either of those cases, we'll just stop.
- if (next && next->parentNode() != this)
+ if (prev && prev->parentNode() != this)
break;
if (child->parentNode())
break;
ASSERT(!child->previousSibling());
#if ENABLE(INSPECTOR)
- InspectorInstrumentation::willInsertDOMNode(document(), child, this);
+ InspectorInstrumentation::willInsertDOMNode(document(), child.get(), this);
#endif
- treeScope()->adoptIfNeeded(child);
+ treeScope()->adoptIfNeeded(child.get());
- // Add child before "next".
+ // Add child after "prev".
forbidEventDispatch();
- if (next)
- insertBeforeCommon(next.get(), child);
- else
- appendChildToContainer(child, this);
+ Node* next;
+ if (prev) {
+ next = prev->nextSibling();
+ ASSERT(m_firstChild != next);
+ prev->setNextSibling(child.get());
+ } else {
+ next = m_firstChild;
+ m_firstChild = child.get();
+ }
+ if (next) {
+ ASSERT(m_lastChild != prev);
+ ASSERT(next->previousSibling() == prev);
+ next->setPreviousSibling(child.get());
+ } else {
+ ASSERT(m_lastChild == prev);
+ m_lastChild = child.get();
+ }
+ child->setParent(this);
+ child->setPreviousSibling(prev.get());
+ child->setNextSibling(next);
allowEventDispatch();
- childrenChanged(false, prev.get(), next.get(), 1);
- notifyChildInserted(child);
-
+ childrenChanged(false, prev.get(), next, 1);
+ notifyChildInserted(child.get());
+
// Add child to the rendering tree
if (attached() && !child->attached() && child->parentNode() == this) {
if (shouldLazyAttach)
// Now that the child is attached to the render tree, dispatch
// the relevant mutation events.
- dispatchChildInsertionEvents(child);
+ dispatchChildInsertionEvents(child.get());
+
prev = child;
+ child = nextChild.release();
}
dispatchSubtreeModifiedEvent();
// Append child to the end of the list
forbidEventDispatch();
- appendChildToContainer(child, this);
+ child->setParent(this);
+ if (m_lastChild) {
+ child->setPreviousSibling(m_lastChild);
+ m_lastChild->setNextSibling(child);
+ } else
+ m_firstChild = child;
+ m_lastChild = child;
allowEventDispatch();
// Send notification about the children change.
forbidEventDispatch();
Node* last = m_lastChild;
// FIXME: This method should take a PassRefPtr.
- appendChildToContainer(newChild.get(), this);
+ appendChildToContainer<Node, ContainerNode>(newChild.get(), this);
treeScope()->adoptIfNeeded(newChild.get());
allowEventDispatch();