*aReturn = nsnull;  if (!aNewChild || !aOldChild) { return NS_ERROR_NULL_POINTER; }  nsresult res = NS_OK; PRInt32 oldPos = 0;  nsCOMPtr<nsIContent> oldContent = do_QueryInterface(aOldChild);    oldPos = IndexOf(oldContent); if (oldPos < 0) { return NS_ERROR_DOM_NOT_FOUND_ERR; }  nsCOMPtr<nsIContent> replacedChild = GetChildAt(oldPos);  PRUint16 nodeType = 0;  res = aNewChild->GetNodeType(&nodeType);  if (NS_FAILED(res)) { return res; }  switch (nodeType) { case nsIDOMNode::ELEMENT_NODE : case nsIDOMNode::TEXT_NODE : case nsIDOMNode::CDATA_SECTION_NODE : case nsIDOMNode::ENTITY_REFERENCE_NODE : case nsIDOMNode::PROCESSING_INSTRUCTION_NODE : case nsIDOMNode::COMMENT_NODE : case nsIDOMNode::DOCUMENT_FRAGMENT_NODE : break; default:   return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR; }  nsCOMPtr<nsIContent> newContent(do_QueryInterface(aNewChild, &res));  if (NS_FAILED(res)) { return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR; }  nsIDocument* elemDoc = GetDocument();  nsCOMPtr<nsIDocument> old_doc = newContent->GetDocument(); if (old_doc && old_doc != elemDoc && !nsContentUtils::CanCallerAccess(aNewChild)) { return NS_ERROR_DOM_SECURITY_ERR; }   if (isSelfOrAncestor(this, newContent)) { return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR; }   mozAutoDocUpdate updateBatch(elemDoc, UPDATE_CONTENT_MODEL, PR_TRUE);   if (nodeType == nsIDOMNode::DOCUMENT_FRAGMENT_NODE) { nsCOMPtr<nsIContent> childContent; PRUint32 i, count = newContent->GetChildCount(); res = RemoveChildAt(oldPos, PR_TRUE); NS_ENSURE_SUCCESS(res, res);   for (i = 0; i < count; ++i) {   childContent = newContent->GetChildAt(0); res = newContent->RemoveChildAt(0, PR_FALSE); NS_ENSURE_SUCCESS(res, res);   res = InsertChildAt(childContent, oldPos++, PR_TRUE); NS_ENSURE_SUCCESS(res, res); } } else { nsCOMPtr<nsIDOMNode> oldParent; res = aNewChild->GetParentNode(getter_AddRefs(oldParent)); NS_ENSURE_SUCCESS(res, res);   if (oldParent) { PRUint32 origChildCount = GetChildCount();   nsCOMPtr<nsIDOMNode> tmpNode; oldParent->RemoveChild(aNewChild, getter_AddRefs(tmpNode));  PRUint32 newChildCount = GetChildCount();   if (oldPos && origChildCount != newChildCount) {  nsIContent *tmpContent = GetChildAt(oldPos - 1);  if (oldContent == tmpContent) { oldPos--; } } }  if (!newContent->IsContentOfType(eXUL)) { nsContentUtils::ReparentContentWrapper(newContent, this, GetDocument(), old_doc); }    if (aNewChild != aOldChild) { res = RemoveChildAt(oldPos, PR_TRUE); NS_ENSURE_SUCCESS(res, res); }  res = InsertChildAt(newContent, oldPos, PR_TRUE); NS_ENSURE_SUCCESS(res, res); }  return CallQueryInterface(replacedChild, aReturn); }
  NS_IMETHOD ReplaceChild(nsIDOMNode* aNewChild, nsIDOMNode* aOldChild,                        nsIDOMNode** aReturn)  {    return nsGenericElement::doReplaceChild(aNewChild, aOldChild, aReturn);  }