nsIDOMDocumentFragment* docFrag = nsnull; if (NS_OK == aNewChild->QueryInterface(kIDOMDocumentFragmentIID, (void **)&docFrag)) {  nsIContent* docFragContent; res = aNewChild->QueryInterface(kIContentIID, (void **)&docFragContent);  if (NS_OK == res) { nsIContent* refContent = nsnull; nsIContent* childContent = nsnull; PRInt32 refPos = 0; PRInt32 i, count;  if (nsnull != aRefChild) { res = aRefChild->QueryInterface(kIContentIID, (void **)&refContent); NS_ASSERTION(NS_OK == res, "Ref child must be an nsIContent"); IndexOf(refContent, refPos); }  docFragContent->ChildCount(count);    for (i = 0; i < count; i++) {   res = docFragContent->ChildAt(0, childContent); if (NS_OK == res) { res = docFragContent->RemoveChildAt(0, PR_FALSE); if (NS_OK == res) { SetDocumentInChildrenOf(childContent, mDocument); if (nsnull == refContent) {  res = AppendChildTo(childContent, PR_TRUE); } else {  res = InsertChildAt(childContent, refPos++, PR_TRUE); } if (NS_OK != res) {  break; } } else {  break; } } else {  break; } } NS_RELEASE(docFragContent);    if (NS_OK == res) { *aReturn = aNewChild; NS_ADDREF(aNewChild); } NS_IF_RELEASE(refContent); } else { res = NS_ERROR_DOM_HIERARCHY_REQUEST_ERR; } NS_RELEASE(docFrag); } else {  nsIContent* newContent = nsnull; res = aNewChild->QueryInterface(kIContentIID, (void**)&newContent); NS_ASSERTION(NS_OK == res, "New child must be an nsIContent"); if (NS_OK == res) { nsIContent* oldParent; res = newContent->GetParent(oldParent); if (NS_OK == res) {  if (nsnull != oldParent) { PRInt32 index; oldParent->IndexOf(newContent, index); if (-1 != index) { oldParent->RemoveChildAt(index, PR_TRUE); } NS_RELEASE(oldParent); }  if (nsnull == aRefChild) {  SetDocumentInChildrenOf(newContent, mDocument); res = AppendChildTo(newContent, PR_TRUE); } else {  nsIContent* refContent = nsnull; res = aRefChild->QueryInterface(kIContentIID, (void**)&refContent); NS_ASSERTION(NS_OK == res, "Ref child must be an nsIContent"); if (NS_OK == res) { PRInt32 pos; IndexOf(refContent, pos); if (pos >= 0) { SetDocumentInChildrenOf(newContent, mDocument); res = InsertChildAt(newContent, pos, PR_TRUE); } else { res = NS_ERROR_DOM_NOT_FOUND_ERR; } NS_RELEASE(refContent); } else { res = NS_ERROR_DOM_HIERARCHY_REQUEST_ERR; } } } NS_RELEASE(newContent);  *aReturn = aNewChild; NS_ADDREF(aNewChild); } else { res = NS_ERROR_DOM_HIERARCHY_REQUEST_ERR; } }  return res; }
  NS_IMETHOD InsertBefore(nsIDOMNode* aNewChild, nsIDOMNode* aRefChild,                        nsIDOMNode** aReturn)  {    return nsGenericElement::doInsertBefore(aNewChild, aRefChild, aReturn);  }