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); }
nsGenericContainerElement::InsertBefore(nsIDOMNode* aNewChild,                                        nsIDOMNode* aRefChild,                                        nsIDOMNode** aReturn){  nsresult res;  *aReturn = nsnull;  if (nsnull == aNewChild) {    return NS_ERROR_NULL_POINTER;  }  // Check if this is a document fragment. If it is, we need  // to remove the children of the document fragment and add them  // individually (i.e. we don't add the actual document fragment).  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);      // Iterate through the fragments children, removing each from      // the fragment and inserting it into the child list of its      // new parent.      for (i = 0; i < count; i++) {        // Always get and remove the first child, since the child indexes        // change as we go along.        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) {              // Append the new child to the end              res = AppendChildTo(childContent, PR_TRUE);            }            else {              // Insert the child and increment the insertion position              res = InsertChildAt(childContent, refPos++, PR_TRUE);            }            if (NS_OK != res) {              // Stop inserting and indicate failure              break;            }          }          else {            // Stop inserting and indicate failure            break;          }        }        else {          // Stop inserting and indicate failure          break;        }      }      NS_RELEASE(docFragContent);            // XXX Should really batch notification till the end      // rather than doing it for each element added      if (NS_OK == res) {        *aReturn = aNewChild;        NS_ADDREF(aNewChild);      }      NS_IF_RELEASE(refContent);    }    else {      res = NS_ERROR_DOM_HIERARCHY_REQUEST_ERR;    }    NS_RELEASE(docFrag);  }