nsCOMPtr<nsIDocument> doc; mRoot->GetDocument(getter_AddRefs(doc));
nsCOMPtr<nsIDocument> doc = mRoot->GetDocument();
nsXULContentBuilder::CreateElement(PRInt32 aNameSpaceID,                                    nsIAtom* aTag,                                    nsIContent** aResult){    nsCOMPtr<nsIDocument> doc;    mRoot->GetDocument(getter_AddRefs(doc));    NS_ASSERTION(doc != nsnull, "not initialized");    if (! doc)        return NS_ERROR_NOT_INITIALIZED;    nsresult rv;    nsCOMPtr<nsIContent> result;    nsCOMPtr<nsINodeInfoManager> nodeInfoManager;    doc->GetNodeInfoManager(getter_AddRefs(nodeInfoManager));    NS_ENSURE_TRUE(nodeInfoManager, NS_ERROR_NOT_INITIALIZED);    nsCOMPtr<nsINodeInfo> nodeInfo;    nodeInfoManager->GetNodeInfo(aTag, nsnull, aNameSpaceID,                                 getter_AddRefs(nodeInfo));    if (aNameSpaceID == kNameSpaceID_XUL) {        rv = nsXULElement::Create(nodeInfo, getter_AddRefs(result));        if (NS_FAILED(rv)) return rv;    }    else if (aNameSpaceID == kNameSpaceID_XHTML) {        rv = gHTMLElementFactory->CreateInstanceByTag(nodeInfo,                                                      getter_AddRefs(result));        if (NS_FAILED(rv)) return rv;        if (! result)            return NS_ERROR_UNEXPECTED;    }    else {        nsCOMPtr<nsIElementFactory> elementFactory;        GetElementFactory(aNameSpaceID, getter_AddRefs(elementFactory));        rv = elementFactory->CreateInstanceByTag(nodeInfo,                                                 getter_AddRefs(result));        if (NS_FAILED(rv)) return rv;        if (! result)            return NS_ERROR_UNEXPECTED;    }    rv = result->SetDocument(doc, PR_FALSE, PR_TRUE);    NS_ASSERTION(NS_SUCCEEDED(rv), "unable to set element's document");    if (NS_FAILED(rv)) return rv;    *aResult = result;    NS_ADDREF(*aResult);    return NS_OK;}