nsCOMPtr<nsIRDFResource> refChild; if (NS_FAILED(rv = GetDOMNodeResource(aRefChild, getter_AddRefs(refChild)))) { NS_ERROR("ref child doesn't have a resource"); return rv; }
nsCOMPtr<nsIRDFNode> refChild; rv = GetGraphNodeForXULElement(aRefChild, getter_AddRefs(refChild)); NS_ASSERTION(NS_SUCCEEDED(rv), "ref child doesn't have a resource"); if (NS_FAILED(rv)) return rv;
RDFXULBuilderImpl::OnInsertBefore(nsIDOMNode* aParent, nsIDOMNode* aNewChild, nsIDOMNode* aRefChild){    NS_PRECONDITION(aParent != nsnull, "null ptr");    if (!aParent)        return NS_ERROR_NULL_POINTER;    NS_PRECONDITION(aNewChild != nsnull, "null ptr");    if (!aNewChild)        return NS_ERROR_NULL_POINTER;    NS_PRECONDITION(aRefChild != nsnull, "null ptr");    if (!aRefChild)        return NS_ERROR_NULL_POINTER;    nsresult rv;    // Translate each of the DOM nodes into the RDF resource for which    // they are acting as a proxy. This implementation will only    // update nodes that are an RDF:instanceOf a XUL:element.    // XXX If aNewChild doesn't have a resource, then somebody is    // inserting a non-RDF element into aParent. Panic for now.    nsCOMPtr<nsIRDFResource> newChild;    if (NS_FAILED(rv = GetDOMNodeResource(aNewChild, getter_AddRefs(newChild)))) {        NS_ERROR("new child doesn't have a resource");        return rv;    }    // If there was an old parent for newChild, then make sure to    // remove that relationship.    nsCOMPtr<nsIDOMNode> oldParentNode;    if (NS_FAILED(rv = aNewChild->GetParentNode(getter_AddRefs(oldParentNode)))) {        NS_ERROR("unable to get new child's parent");        return rv;    }    if (oldParentNode) {        nsCOMPtr<nsIRDFResource> oldParent;        // If the old parent has a resource...        if (NS_SUCCEEDED(rv = GetDOMNodeResource(oldParentNode, getter_AddRefs(oldParent)))) {            // ...and it's a XUL element...            PRBool isXULElement;            if (NS_SUCCEEDED(rv = mDB->HasAssertion(oldParent,                                                    kRDF_instanceOf,                                                    kXUL_element,                                                    PR_TRUE,                                                    &isXULElement))                && isXULElement) {                // remove the child from the old collection                if (NS_FAILED(rv = rdf_ContainerRemoveElement(mDB, oldParent, newChild))) {                    NS_ERROR("unable to remove newChild from oldParent");                    return rv;                }            }        }    }    // If the new parent has a resource...    nsCOMPtr<nsIRDFResource> parent;    if (NS_SUCCEEDED(rv = GetDOMNodeResource(aParent, getter_AddRefs(parent)))) {        // ...and it's a XUL element...        PRBool isXULElement;        if (NS_SUCCEEDED(rv = mDB->HasAssertion(parent,                                                kRDF_instanceOf,                                                kXUL_element,                                                PR_TRUE,                                                &isXULElement))            && isXULElement) {            // XXX For now, we panic if the refChild doesn't have a resouce            nsCOMPtr<nsIRDFResource> refChild;            if (NS_FAILED(rv = GetDOMNodeResource(aRefChild, getter_AddRefs(refChild)))) {                NS_ERROR("ref child doesn't have a resource");                return rv;            }            // Determine the index of the refChild in the container            PRInt32 index;            if (NS_FAILED(rv = rdf_ContainerIndexOf(mDB, parent, refChild, &index))) {                NS_ERROR("unable to determine index of refChild in container");                return rv;            }            // ...and insert the newChild before it.            if (NS_FAILED(rv = rdf_ContainerInsertElementAt(mDB, parent, newChild, index))) {                NS_ERROR("unable to insert new element into container");                return rv;            }        }    }    return NS_OK;}