if (rv == NS_CONTENT_ATTR_HAS_VALUE) { rv = xuldoc->AddElementForID(id, element); if (NS_FAILED(rv)) return rv; }  rv = element->GetAttribute(kNameSpaceID_None, kRefAtom, id); if (NS_FAILED(rv)) return rv;  if (rv == NS_CONTENT_ATTR_HAS_VALUE) { rv = xuldoc->AddElementForID(id, element); if (NS_FAILED(rv)) return rv; }
XULContentSinkImpl::OpenRoot(const nsIParserNode& aNode, PRInt32 aNameSpaceID, nsIAtom* aTag){    nsresult rv;    if ((aNameSpaceID == kNameSpaceID_HTML) && (aTag == kScriptAtom)) {        PR_LOG(gLog, PR_LOG_ALWAYS,               ("xul: script tag not allowed as root content element"));        return NS_ERROR_UNEXPECTED;    }    // Create the element    nsCOMPtr<nsIContent> element;    if (aNameSpaceID == kNameSpaceID_HTML) {        rv = CreateHTMLElement(aTag, &element);    }    else {        rv = CreateXULElement(aNameSpaceID, aTag, &element);    }    if (NS_FAILED(rv)) {#ifdef PR_LOGGING        PR_LOG(gLog, PR_LOG_ALWAYS,               ("xul: unable to create element '%s' at line %d",                NS_STATIC_CAST(const char*, nsCAutoString(aNode.GetText())),                aNode.GetSourceLineNumber()));#endif        return rv;    }    // Okay, this is the root element. Do all the juju we need to    // do to get stuff hooked up.    NS_ASSERTION(mState == eInProlog, "how'd we get here?");    if (mState != eInProlog)        return NS_ERROR_UNEXPECTED;    mDocument->SetRootContent(element);    // Add the attributes    rv = AddAttributes(aNode, element);    if (NS_FAILED(rv)) return rv;    // Create the document's "hidden form" element which will wrap all    // HTML form elements that turn up.    nsCOMPtr<nsIHTMLContent> form;    rv = gHTMLElementFactory->CreateInstanceByTag(nsAutoString("form"),                                                  getter_AddRefs(form));    NS_ASSERTION(NS_SUCCEEDED(rv), "unable to create form element");    if (NS_FAILED(rv)) return rv;    nsCOMPtr<nsIDOMHTMLFormElement> htmlFormElement = do_QueryInterface(form);    NS_ASSERTION(htmlFormElement != nsnull, "not an nSIDOMHTMLFormElement");    if (! htmlFormElement)        return NS_ERROR_UNEXPECTED;    nsCOMPtr<nsIXULDocument> xuldoc = do_QueryInterface(mDocument);    if (! xuldoc)        return NS_ERROR_UNEXPECTED;    rv = xuldoc->SetForm(htmlFormElement);    NS_ASSERTION(NS_SUCCEEDED(rv), "unable to set form element");    if (NS_FAILED(rv)) return NS_ERROR_UNEXPECTED;    nsCOMPtr<nsIContent> content = do_QueryInterface(form);    NS_ASSERTION(content != nsnull, "not an nsIContent");    if (! content)        return NS_ERROR_UNEXPECTED;    // XXX Would like to make this anonymous, but still need the    // form's frame to get built. For now make it explicit.    rv = element->InsertChildAt(content, 0, PR_FALSE);     NS_ASSERTION(NS_SUCCEEDED(rv), "unable to add anonymous form element");    if (NS_FAILED(rv)) return rv;    // Add the element to the XUL document's ID-to-element map.    nsAutoString id;    rv = element->GetAttribute(kNameSpaceID_None, kIdAtom, id);    if (NS_FAILED(rv)) return rv;    if (rv == NS_CONTENT_ATTR_HAS_VALUE) {        rv = xuldoc->AddElementForID(id, element);        if (NS_FAILED(rv)) return rv;    }    rv = element->GetAttribute(kNameSpaceID_None, kRefAtom, id);    if (NS_FAILED(rv)) return rv;    if (rv == NS_CONTENT_ATTR_HAS_VALUE) {        rv = xuldoc->AddElementForID(id, element);        if (NS_FAILED(rv)) return rv;    }    // Push the element onto the context stack, so that child    // containers will hook up to us as their parent.    rv = mContextStack.Push(element, mState);    if (NS_FAILED(rv)) return rv;    mState = eInDocumentElement;    return NS_OK;}