popupDoc->mPopupElement = mPopupElement;
popupDoc->mPopupNode = mPopupNode;
XULDocumentImpl::CreatePopupDocument(nsIContent* aPopupElement, nsIDocument** aResult){    nsresult rv;    // Create and addref    XULDocumentImpl* popupDoc = new XULDocumentImpl();    if (popupDoc == nsnull)      return NS_ERROR_OUT_OF_MEMORY;    NS_ADDREF(popupDoc);    // Init our new document    if (NS_FAILED(rv = popupDoc->Init())) {      NS_RELEASE(popupDoc);      return rv;    }    // Indicate that we are a popup document    popupDoc->SetIsPopup(PR_TRUE);    // Our URL is exactly the same as the parent doc.    popupDoc->SetDocumentURLAndGroup(mDocumentURL);    // Set our character set.    popupDoc->SetDocumentCharacterSet(mCharSetID);    // Try to share the style sheets (this is evil, but it might just work)    // First do a prepare to pick up a new inline and attr style sheet    if (NS_FAILED(rv = popupDoc->PrepareStyleSheets(mDocumentURL))) {      NS_ERROR("problem initializing style sheets.");      return rv;    }    // Now we need to copy all of the style sheets from the parent document    PRInt32 count = mStyleSheets.Count();    for (PRInt32 i = 1; i < count-1; i++) {      // Don't bother addrefing. We don't live as long as our parent document        nsIStyleSheet* sheet = (nsIStyleSheet*) mStyleSheets.ElementAt(i);        nsCOMPtr<nsICSSStyleSheet> cssSheet = do_QueryInterface(sheet);        if (cssSheet) {          nsCOMPtr<nsICSSStyleSheet> clonedSheet;          if (NS_SUCCEEDED(cssSheet->Clone(*getter_AddRefs(clonedSheet)))) {            popupDoc->AddStyleSheet(clonedSheet);          }        }    }    // We don't share builders, but we do share the DB that the current    // XUL builder has.    // Create a XUL content model builder    rv = nsComponentManager::CreateInstance(kRDFXULBuilderCID,                                                nsnull,                                                kIRDFContentModelBuilderIID,                                                (void**) &(popupDoc->mXULBuilder));    if (NS_FAILED(rv)) {        NS_ERROR("couldn't create XUL builder");        return rv;    }    nsCOMPtr<nsIRDFCompositeDataSource> db;    mXULBuilder->GetDataBase(getter_AddRefs(db));    if (NS_FAILED(rv = popupDoc->mXULBuilder->SetDataBase(db))) {        NS_ERROR("couldn't set builder's db");        return rv;    }    if (NS_FAILED(rv = popupDoc->AddContentModelBuilder(popupDoc->mXULBuilder))) {        NS_ERROR("could't add XUL builder");        return rv;    }    // We share the same data sources    popupDoc->mLocalStore = mLocalStore;    popupDoc->mDocumentDataSource = mDocumentDataSource;    // We share the same namespace manager    popupDoc->mNameSpaceManager = mNameSpaceManager;    // We share the mPopup    popupDoc->mPopupElement = mPopupElement;    // Suck all of the root's content into our document.    // We need to make the XUL builder instantiate this node.    // Retrieve the resource that corresponds to this node.    nsAutoString idValue;    nsCOMPtr<nsIDOMElement> domRoot = do_QueryInterface(aPopupElement);    domRoot->GetAttribute("id", idValue);    // Use the absolute URL to retrieve a resource from the RDF    // service that corresponds to the root content.    nsCOMPtr<nsIRDFResource> rootResource;    rv = gXULUtils->MakeElementResource(this, idValue, getter_AddRefs(rootResource));    if (NS_FAILED(rv)) return rv;    // Tell the builder to create its root from this resrouce.    popupDoc->mXULBuilder->CreateRootContent(rootResource);    // Now the popup will happily use its own XUL builder to churn out nodal    // clones of the popup content.  They will get their own event handlers    // (properly scoped to the new global popup window context), and will    // belong to the popup document instead.  If this works, it will be    // more righteous than Star Wars Episode I.    // Return the doc    *aResult = popupDoc;    return NS_OK;}