PRBool wasInserted = PR_FALSE;  if (rv == NS_CONTENT_ATTR_HAS_VALUE) { PRInt32 pos = posStr.ToInteger(NS_REINTERPRET_CAST(PRInt32*, &rv)); if (NS_SUCCEEDED(rv)) { rv = parent->InsertChildAt(element, pos, PR_FALSE); if (NS_FAILED(rv)) return rv;  wasInserted = PR_TRUE; } }  if (! wasInserted) { rv = parent->AppendChildTo(element, PR_FALSE); if (NS_FAILED(rv)) return rv; }    nsCOMPtr<nsIXULDocument> xuldoc = do_QueryInterface(mDocument); if (! xuldoc) return NS_ERROR_UNEXPECTED;
XULContentSinkImpl::OpenTag(const nsIParserNode& aNode, PRInt32 aNameSpaceID, nsIAtom* aTag){    nsresult rv;    if ((aNameSpaceID == kNameSpaceID_HTML) && (aTag == kScriptAtom)) {        // Oops, it's a script!        return OpenScript(aNode);    }    // Create the element    nsCOMPtr<nsIContent> element;    if (aNameSpaceID == kNameSpaceID_HTML) {        rv = CreateHTMLElement(aTag, &element);    }    else {        rv = CreateXULElement(aNameSpaceID, aTag, &element);    }    if (NS_FAILED(rv)) {        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()));        return rv;    }    // Add the attributes    rv = AddAttributes(aNode, element);    if (NS_FAILED(rv)) return rv;    // Deal with somebody telling us that it has a 'pos'    nsCOMPtr<nsIContent> parent;    rv = mContextStack.GetTopElement(getter_AddRefs(parent));    if (NS_FAILED(rv)) return rv;    nsAutoString posStr;    rv = element->GetAttribute(kNameSpaceID_None, kPositionAtom, posStr);    if (NS_FAILED(rv)) return rv;    PRBool wasInserted = PR_FALSE;    if (rv == NS_CONTENT_ATTR_HAS_VALUE) {        PRInt32 pos = posStr.ToInteger(NS_REINTERPRET_CAST(PRInt32*, &rv));        if (NS_SUCCEEDED(rv)) {            rv = parent->InsertChildAt(element, pos, PR_FALSE);            if (NS_FAILED(rv)) return rv;            wasInserted = PR_TRUE;        }    }    if (! wasInserted) {        rv = parent->AppendChildTo(element, PR_FALSE);        if (NS_FAILED(rv)) return rv;    }    // Add the element to the XUL document's ID-to-element map.    nsCOMPtr<nsIXULDocument> xuldoc = do_QueryInterface(mDocument);    if (! xuldoc)        return NS_ERROR_UNEXPECTED;    // check for an 'id' attribute    nsAutoString id;    rv = element->GetAttribute(kNameSpaceID_None, kIdAtom, id);    if (NS_FAILED(rv)) return rv;    if (rv != NS_CONTENT_ATTR_HAS_VALUE && mContextStack.IsInsideXULTemplate()) {        // If we're inside a XUL template, then _everything_ needs to        // have an ID for the 'template' attribute hookup.        id = "$";        id.Append(PRInt32(element.get()), 16);        rv = element->SetAttribute(kNameSpaceID_None, kIdAtom, id, PR_FALSE);        if (NS_FAILED(rv)) return rv;        rv = NS_CONTENT_ATTR_HAS_VALUE;    }    if (rv == NS_CONTENT_ATTR_HAS_VALUE) {        rv = xuldoc->AddElementForID(id, element);        if (NS_FAILED(rv)) return rv;    }    // ...and a 'ref' attribute    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;}