Make element and document RemoveChild methods share code. Bug 312522, patch by

Alex Vincent <ajvincent@gmail.com>, r=sicking, sr=bzbarsky
This commit is contained in:
bzbarsky@mit.edu
2006-01-12 16:33:42 +00:00
parent f5c2310daa
commit 9b784dc515
3 changed files with 45 additions and 54 deletions

View File

@@ -3025,6 +3025,13 @@ nsGenericElement::ReplaceChild(nsIDOMNode* aNewChild, nsIDOMNode* aOldChild,
mAttrsAndChildren, aReturn);
}
NS_IMETHODIMP
nsGenericElement::RemoveChild(nsIDOMNode *aOldChild, nsIDOMNode **aReturn)
{
return doRemoveChild(aOldChild, this, GetCurrentDoc(),
mAttrsAndChildren, aReturn);
}
// When replacing, aRefContent is the content being replaced; when
// inserting it's the content before which we're inserting. In the
// latter case it may be null.
@@ -3688,42 +3695,35 @@ nsGenericElement::doReplaceChild(nsIDOMNode* aNewChild, nsIDOMNode* aOldChild,
return CallQueryInterface(replacedChild, aReturn);
}
NS_IMETHODIMP
nsGenericElement::RemoveChild(nsIDOMNode *aOldChild, nsIDOMNode **aReturn)
/* static */
nsresult
nsGenericElement::doRemoveChild(nsIDOMNode* aOldChild,
nsIContent* aParent, nsIDocument* aDocument,
nsAttrAndChildArray& aChildArray,
nsIDOMNode** aReturn)
{
NS_PRECONDITION(aParent || aDocument, "Must have document if no parent!");
NS_PRECONDITION(!aParent || aParent->GetCurrentDoc() == aDocument,
"Incorrect aDocument");
*aReturn = nsnull;
NS_ENSURE_TRUE(aOldChild, NS_ERROR_NULL_POINTER);
if (!aOldChild) {
return NS_ERROR_NULL_POINTER;
}
nsresult res;
nsCOMPtr<nsIContent> content(do_QueryInterface(aOldChild, &res));
if (NS_FAILED(res)) {
/*
* If we're asked to remove something that doesn't support nsIContent
* it can not be one of our children, i.e. we return NOT_FOUND_ERR.
*/
nsCOMPtr<nsIContent> content = do_QueryInterface(aOldChild);
// fix children to be a passed argument
PRInt32 index = aChildArray.IndexOfChild(content);
if (index == -1) {
// aOldChild isn't one of our children.
return NS_ERROR_DOM_NOT_FOUND_ERR;
}
PRInt32 pos = IndexOf(content);
if (pos < 0) {
/*
* aOldChild isn't one of our children.
*/
return NS_ERROR_DOM_NOT_FOUND_ERR;
}
res = RemoveChildAt(pos, PR_TRUE);
nsContentOrDocument container(aParent, aDocument);
nsresult rv = container.RemoveChildAt(index, PR_TRUE, aChildArray);
*aReturn = aOldChild;
NS_ADDREF(aOldChild);
return res;
return rv;
}
//----------------------------------------------------------------------