NS_IMETHOD InsertBefore(nsIDOMNode* aNewChild, nsIDOMNode* aRefChild, nsIDOMNode** aReturn) { return nsGenericElement::doInsertBefore(aNewChild, aRefChild, aReturn); }
nsGenericElement::InsertBefore(nsIDOMNode *aNewChild, nsIDOMNode *aRefChild, nsIDOMNode **aReturn) { if (!aReturn) { return NS_ERROR_NULL_POINTER; }  *aReturn = nsnull;  if (!aNewChild) { return NS_ERROR_NULL_POINTER; }  nsCOMPtr<nsIContent> refContent; nsresult res = NS_OK; PRInt32 refPos = 0;  if (aRefChild) { refContent = do_QueryInterface(aRefChild, &res);  if (NS_FAILED(res)) {  return NS_ERROR_DOM_NOT_FOUND_ERR; }  refPos = IndexOf(refContent);  if (refPos < 0) { return NS_ERROR_DOM_NOT_FOUND_ERR; } } else { refPos = GetChildCount(); }  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; }  nsCOMPtr<nsIDocument> old_doc = newContent->GetDocument(); if (old_doc && old_doc != GetDocument() && !nsContentUtils::CanCallerAccess(aNewChild)) { return NS_ERROR_DOM_SECURITY_ERR; }   if (isSelfOrAncestor(this, newContent)) { return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR; }   if (nodeType == nsIDOMNode::DOCUMENT_FRAGMENT_NODE) { nsCOMPtr<nsIDocumentFragment> doc_fragment(do_QueryInterface(newContent)); NS_ENSURE_TRUE(doc_fragment, NS_ERROR_UNEXPECTED);  doc_fragment->DisconnectChildren();  PRUint32 count = newContent->GetChildCount();  PRUint32 old_count = GetChildCount();  PRBool do_notify = !!aRefChild;      mozAutoDocUpdate updateBatch(GetDocument(), UPDATE_CONTENT_MODEL, count && !do_notify);    nsCOMPtr<nsIContent> childContent;  for (PRUint32 i = 0; i < count; ++i) {      childContent = newContent->GetChildAt(i);   res = InsertChildAt(childContent, refPos++, do_notify);  if (NS_FAILED(res)) { break; } }  if (NS_FAILED(res)) {     doc_fragment->ReconnectChildren();  return res; }  nsIDocument *doc = GetDocument();  if (count && !do_notify && doc) { doc->ContentAppended(this, old_count); }  doc_fragment->DropChildReferences(); } else { nsCOMPtr<nsIDOMNode> oldParent; res = aNewChild->GetParentNode(getter_AddRefs(oldParent));  if (NS_FAILED(res)) { return res; }   if (oldParent) { nsCOMPtr<nsIDOMNode> tmpNode;  PRUint32 origChildCount = GetChildCount();   oldParent->RemoveChild(aNewChild, getter_AddRefs(tmpNode));  PRUint32 newChildCount = GetChildCount();   if (refPos && origChildCount != newChildCount) { if (refContent) {   if (refContent == GetChildAt(refPos - 1)) { refPos--; } } else {  refPos--; } } }  if (!newContent->IsContentOfType(eXUL)) { nsContentUtils::ReparentContentWrapper(newContent, this, GetDocument(), old_doc); }  res = InsertChildAt(newContent, refPos, PR_TRUE);  if (NS_FAILED(res)) { return res; } }  *aReturn = aNewChild; NS_ADDREF(*aReturn);  return res; }
  NS_IMETHOD InsertBefore(nsIDOMNode* aNewChild, nsIDOMNode* aRefChild,                          nsIDOMNode** aReturn)  {    return nsGenericElement::doInsertBefore(aNewChild, aRefChild, aReturn);  }