}   nsCOMPtr<nsIStyleSet> styleSet; rv = CreateStyleSet(mDocument, getter_AddRefs(styleSet)); if (NS_FAILED(rv)) return rv;   mPresShell->EndObservingDocument(); mPresShell->Destroy();  mPresShell = nsnull; rv = newDoc->CreateShell(mPresContext, mViewManager, styleSet, getter_AddRefs(mPresShell)); if (NS_FAILED(rv)) return rv;  mPresShell->BeginObservingDocument();   if(mDocument) { nsCOMPtr<nsIDOMEventReceiver> erP = do_QueryInterface(mDocument, &rv); if(NS_FAILED(rv) || !erP) return rv ? rv : NS_ERROR_FAILURE;  rv = erP->AddEventListenerByIID(mFocusListener, NS_GET_IID(nsIDOMFocusListener)); NS_ASSERTION(NS_SUCCEEDED(rv), "failed to register focus listener");
DocumentViewerImpl::SetDOMDocument(nsIDOMDocument *aDocument){  // Assumptions:  //  // 1) this document viewer has been initialized with a call to Init().  // 2) the stylesheets associated with the document have been added to the document.  // XXX Right now, this method assumes that the layout of the current document  // hasn't started yet.  More cleanup will probably be necessary to make this  // method work for the case when layout *has* occurred for the current document.  // That work can happen when and if it is needed.    nsresult rv;  if (nsnull == aDocument)    return NS_ERROR_NULL_POINTER;  nsCOMPtr<nsIDocument> newDoc = do_QueryInterface(aDocument, &rv);  if (NS_FAILED(rv)) return rv;  // 0) Replace the old document with the new one  mDocument = newDoc;  // 1) Set the script global object on the new document  nsCOMPtr<nsIInterfaceRequestor> requestor(do_QueryInterface(mContainer));  if (requestor) {    nsCOMPtr<nsIScriptGlobalObjectOwner> owner;    requestor->GetInterface(NS_GET_IID(nsIScriptGlobalObjectOwner),       getter_AddRefs(owner));    if (nsnull != owner) {      nsCOMPtr<nsIScriptGlobalObject> global;      rv = owner->GetScriptGlobalObject(getter_AddRefs(global));      if (NS_SUCCEEDED(rv) && (nsnull != global)) {        mDocument->SetScriptGlobalObject(global);        global->SetNewDocument(aDocument, PR_TRUE);      }    }  }    // 2) Create a new style set for the document  nsCOMPtr<nsIStyleSet> styleSet;  rv = CreateStyleSet(mDocument, getter_AddRefs(styleSet));  if (NS_FAILED(rv)) return rv;  // 3) Replace the current pres shell with a new shell for the new document   mPresShell->EndObservingDocument();  mPresShell->Destroy();  mPresShell = nsnull;  rv = newDoc->CreateShell(mPresContext, mViewManager, styleSet,                           getter_AddRefs(mPresShell));  if (NS_FAILED(rv)) return rv;  mPresShell->BeginObservingDocument();  // 4) Register the focus listener on the new document  if(mDocument)  {        nsCOMPtr<nsIDOMEventReceiver> erP = do_QueryInterface(mDocument, &rv);    if(NS_FAILED(rv) || !erP)      return rv ? rv : NS_ERROR_FAILURE;    rv = erP->AddEventListenerByIID(mFocusListener, NS_GET_IID(nsIDOMFocusListener));    NS_ASSERTION(NS_SUCCEEDED(rv), "failed to register focus listener");  }  return rv;}