nsGenericContainerElement::ReplaceChild(nsIDOMNode* aNewChild, nsIDOMNode* aOldChild, nsIDOMNode** aReturn) { *aReturn = nsnull; if (nsnull == aOldChild) { return NS_ERROR_NULL_POINTER;
NS_IMETHOD ReplaceChild(nsIDOMNode* aNewChild, nsIDOMNode* aOldChild, nsIDOMNode** aReturn) { return doReplaceChild(this, aNewChild, aOldChild, aReturn);
nsGenericContainerElement::ReplaceChild(nsIDOMNode* aNewChild,                                        nsIDOMNode* aOldChild,                                        nsIDOMNode** aReturn){  *aReturn = nsnull;  if (nsnull == aOldChild) {    return NS_ERROR_NULL_POINTER;  }  if (nsnull == aNewChild) {    return NS_ERROR_NULL_POINTER;  }  nsIContent* content = nsnull;  nsresult res = aOldChild->QueryInterface(kIContentIID, (void**)&content);  NS_ASSERTION(NS_SUCCEEDED(res), "Must be an nsIContent");  if (NS_SUCCEEDED(res)) {    PRInt32 pos;    IndexOf(content, pos);    if (pos >= 0) {      nsIContent* newContent = nsnull;      res = aNewChild->QueryInterface(kIContentIID, (void**)&newContent);      NS_ASSERTION(NS_SUCCEEDED(res), "Must be an nsIContent");      if (NS_SUCCEEDED(res)) {        // 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_SUCCEEDED(res)) {            PRInt32 count;            docFragContent->ChildCount(count);            // If there are children of the document            if (count > 0) {              nsIContent* childContent;              // Remove the last child of the document fragment              // and do a replace with it              res = docFragContent->ChildAt(count-1, childContent);              if (NS_SUCCEEDED(res)) {                res = docFragContent->RemoveChildAt(count-1, PR_FALSE);                if (NS_SUCCEEDED(res)) {                  SetDocumentInChildrenOf(childContent, mDocument);                  res = ReplaceChildAt(childContent, pos, PR_TRUE);                  // If there are more children, then insert them before                  // the newly replaced child                  if ((NS_OK == res) && (count > 1)) {                    nsIDOMNode* childNode = nsnull;                    res = childContent->QueryInterface(kIDOMNodeIID,                                                       (void **)&childNode);                    if (NS_SUCCEEDED(res)) {                      nsIDOMNode* rv;                      res = InsertBefore(aNewChild, childNode, &rv);                      if (NS_SUCCEEDED(res)) {                        NS_IF_RELEASE(rv);                      }                      NS_RELEASE(childNode);                    }                  }                }                NS_RELEASE(childContent);              }            }            NS_RELEASE(docFragContent);          }          else {            res = NS_ERROR_DOM_HIERARCHY_REQUEST_ERR;          }          NS_RELEASE(docFrag);        }        else {          nsIContent* oldParent;          res = newContent->GetParent(oldParent);          if (NS_OK == res) {            // Remove the element from the old parent if one exists            if (nsnull != oldParent) {              PRInt32 index;              oldParent->IndexOf(newContent, index);              if (-1 != index) {                oldParent->RemoveChildAt(index, PR_TRUE);              }              NS_RELEASE(oldParent);            }                       SetDocumentInChildrenOf(newContent, mDocument);            res = ReplaceChildAt(newContent, pos, PR_TRUE);          }        }        NS_RELEASE(newContent);      }      else {        res = NS_ERROR_DOM_HIERARCHY_REQUEST_ERR;      }      *aReturn = aOldChild;      NS_ADDREF(aOldChild);    }    else {      res = NS_ERROR_DOM_NOT_FOUND_ERR;    }    NS_RELEASE(content);  }