diff --git a/dlls/mshtml/htmldoc.c b/dlls/mshtml/htmldoc.c
index 5e34ced52a8..3e07924f2c6 100644
--- a/dlls/mshtml/htmldoc.c
+++ b/dlls/mshtml/htmldoc.c
@@ -1998,7 +1998,11 @@ HRESULT HTMLDocument_Create(IUnknown *pUnkOuter, REFIID riid, void** ppvObject)
return hres;
}
- update_nsdocument(doc);
+ if(!doc->basedoc.doc_node && doc->basedoc.window->doc) {
+ doc->basedoc.doc_node = doc->basedoc.window->doc;
+ htmldoc_addref(&doc->basedoc.doc_node->basedoc);
+ }
+
get_thread_hwnd();
return S_OK;
diff --git a/dlls/mshtml/htmliframe.c b/dlls/mshtml/htmliframe.c
index 7af00703ebc..dc514d8bb07 100644
--- a/dlls/mshtml/htmliframe.c
+++ b/dlls/mshtml/htmliframe.c
@@ -353,9 +353,7 @@ static HRESULT WINAPI HTMLIFrameBase2_get_contentWindow(IHTMLFrameBase2 *iface,
if(!This->content_window) {
nsIDOMHTMLDocument *nshtmldoc;
- HTMLDocumentNode *content_doc;
nsIDOMDocument *nsdoc;
- HTMLWindow *window;
nsresult nsres;
HRESULT hres;
@@ -377,23 +375,10 @@ static HRESULT WINAPI HTMLIFrameBase2_get_contentWindow(IHTMLFrameBase2 *iface,
return E_FAIL;
}
- hres = create_content_window(This, nshtmldoc, &window);
- if(FAILED(hres)) {
- nsIDOMHTMLDocument_Release(nshtmldoc);
- return E_FAIL;
- }
-
- hres = create_doc_from_nsdoc(nshtmldoc, This->element.node.doc->basedoc.doc_obj, window, &content_doc);
+ hres = create_content_window(This, nshtmldoc, &This->content_window);
nsIDOMHTMLDocument_Release(nshtmldoc);
- if(SUCCEEDED(hres))
- window_set_docnode(window, content_doc);
- else
- IHTMLWindow2_Release(HTMLWINDOW2(window));
- htmldoc_release(&content_doc->basedoc);
if(FAILED(hres))
return hres;
-
- This->content_window = window;
}
IHTMLWindow2_AddRef(HTMLWINDOW2(This->content_window));
diff --git a/dlls/mshtml/htmlwindow.c b/dlls/mshtml/htmlwindow.c
index fe7444af50d..91e0e3480f5 100644
--- a/dlls/mshtml/htmlwindow.c
+++ b/dlls/mshtml/htmlwindow.c
@@ -36,7 +36,7 @@ WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
static struct list window_list = LIST_INIT(window_list);
-void window_set_docnode(HTMLWindow *window, HTMLDocumentNode *doc_node)
+static void window_set_docnode(HTMLWindow *window, HTMLDocumentNode *doc_node)
{
if(window->doc) {
window->doc->basedoc.window = NULL;
@@ -45,6 +45,14 @@ void window_set_docnode(HTMLWindow *window, HTMLDocumentNode *doc_node)
window->doc = doc_node;
if(doc_node)
htmldoc_addref(&doc_node->basedoc);
+
+ if(window->doc_obj && window->doc_obj->basedoc.window == window) {
+ if(window->doc_obj->basedoc.doc_node)
+ htmldoc_release(&window->doc_obj->basedoc.doc_node->basedoc);
+ window->doc_obj->basedoc.doc_node = doc_node;
+ if(doc_node)
+ htmldoc_addref(&doc_node->basedoc);
+ }
}
#define HTMLWINDOW2_THIS(iface) DEFINE_THIS(HTMLWindow, HTMLWindow2, iface)
@@ -1516,12 +1524,49 @@ HRESULT HTMLWindow_Create(HTMLDocumentObj *doc_obj, nsIDOMWindow *nswindow, HTML
window->scriptmode = SCRIPTMODE_GECKO;
list_init(&window->script_hosts);
+ update_window_doc(window);
+
list_add_head(&window_list, &window->entry);
*ret = window;
return S_OK;
}
+void update_window_doc(HTMLWindow *window)
+{
+ nsIDOMHTMLDocument *nshtmldoc;
+ nsIDOMDocument *nsdoc;
+ nsresult nsres;
+
+ nsres = nsIDOMWindow_GetDocument(window->nswindow, &nsdoc);
+ if(NS_FAILED(nsres) || !nsdoc) {
+ ERR("GetDocument failed: %08x\n", nsres);
+ return;
+ }
+
+ nsres = nsIDOMDocument_QueryInterface(nsdoc, &IID_nsIDOMHTMLDocument, (void**)&nshtmldoc);
+ nsIDOMDocument_Release(nsdoc);
+ if(NS_FAILED(nsres)) {
+ ERR("Could not get nsIDOMHTMLDocument iface: %08x\n", nsres);
+ return;
+ }
+
+ if(!window->doc || window->doc->nsdoc != nshtmldoc) {
+ HTMLDocumentNode *doc;
+ HRESULT hres;
+
+ hres = create_doc_from_nsdoc(nshtmldoc, window->doc_obj, window, &doc);
+ if(SUCCEEDED(hres)) {
+ window_set_docnode(window, doc);
+ htmldoc_release(&doc->basedoc);
+ }else {
+ ERR("create_doc_from_nsdoc failed: %08x\n", hres);
+ }
+ }
+
+ nsIDOMHTMLDocument_Release(nshtmldoc);
+}
+
HTMLWindow *nswindow_to_window(const nsIDOMWindow *nswindow)
{
HTMLWindow *iter;
diff --git a/dlls/mshtml/mshtml_private.h b/dlls/mshtml/mshtml_private.h
index 85421d117e9..8fc9b010ed6 100644
--- a/dlls/mshtml/mshtml_private.h
+++ b/dlls/mshtml/mshtml_private.h
@@ -572,7 +572,7 @@ HRESULT HTMLLoadOptions_Create(IUnknown*,REFIID,void**);
HRESULT create_doc_from_nsdoc(nsIDOMHTMLDocument*,HTMLDocumentObj*,HTMLWindow*,HTMLDocumentNode**);
HRESULT HTMLWindow_Create(HTMLDocumentObj*,nsIDOMWindow*,HTMLWindow**);
-void window_set_docnode(HTMLWindow*,HTMLDocumentNode*);
+void update_window_doc(HTMLWindow*);
HTMLWindow *nswindow_to_window(const nsIDOMWindow*);
HTMLOptionElementFactory *HTMLOptionElementFactory_Create(HTMLWindow*);
HTMLImageElementFactory *HTMLImageElementFactory_Create(HTMLWindow*);
@@ -647,7 +647,6 @@ void get_editor_controller(NSContainer*);
void init_nsevents(NSContainer*);
void add_nsevent_listener(HTMLWindow*,LPCWSTR);
nsresult get_nsinterface(nsISupports*,REFIID,void**);
-void update_nsdocument(HTMLDocumentObj*);
void set_document_bscallback(HTMLDocument*,nsChannelBSC*);
void set_current_mon(HTMLDocument*,IMoniker*);
diff --git a/dlls/mshtml/navigate.c b/dlls/mshtml/navigate.c
index 951b04dc67a..33bf1c1d2e8 100644
--- a/dlls/mshtml/navigate.c
+++ b/dlls/mshtml/navigate.c
@@ -952,7 +952,7 @@ static HRESULT read_stream_data(nsChannelBSC *This, IStream *stream)
/* events are reset when a new document URI is loaded, so re-initialise them here */
if(This->bsc.doc && This->bsc.doc->doc_obj->bscallback == This && This->bsc.doc->doc_obj->nscontainer) {
- update_nsdocument(This->bsc.doc->doc_obj);
+ update_window_doc(This->bsc.doc->window);
init_nsevents(This->bsc.doc->doc_obj->nscontainer);
}
}
diff --git a/dlls/mshtml/nsembed.c b/dlls/mshtml/nsembed.c
index 256c09a26bf..f6efd263e87 100644
--- a/dlls/mshtml/nsembed.c
+++ b/dlls/mshtml/nsembed.c
@@ -807,58 +807,6 @@ void set_ns_editmode(NSContainer *This)
nsIWebBrowser_SetParentURIContentListener(This->webbrowser, NSURICL(This));
}
-void update_nsdocument(HTMLDocumentObj *doc)
-{
- HTMLDocumentNode *doc_node;
- nsIDOMHTMLDocument *nsdoc;
- nsIDOMDocument *nsdomdoc;
- nsresult nsres;
- HRESULT hres;
-
- if(!doc->nscontainer || !doc->nscontainer->navigation)
- return;
-
- nsres = nsIWebNavigation_GetDocument(doc->nscontainer->navigation, &nsdomdoc);
- if(NS_FAILED(nsres) || !nsdomdoc) {
- ERR("GetDocument failed: %08x\n", nsres);
- return;
- }
-
- nsres = nsIDOMDocument_QueryInterface(nsdomdoc, &IID_nsIDOMHTMLDocument, (void**)&nsdoc);
- nsIDOMDocument_Release(nsdomdoc);
- if(NS_FAILED(nsres)) {
- ERR("Could not get nsIDOMHTMLDocument iface: %08x\n", nsres);
- return;
- }
-
- if(doc->basedoc.doc_node && nsdoc == doc->basedoc.doc_node->nsdoc) {
- nsIDOMHTMLDocument_Release(nsdoc);
- return;
- }
-
- if(doc->basedoc.doc_node && doc->basedoc.doc_node->nsdoc) {
- doc_node = doc->basedoc.doc_node;
- doc_node->basedoc.doc_obj = NULL;
- IHTMLDocument2_Release(HTMLDOC(&doc_node->basedoc));
- doc->basedoc.doc_node = NULL;
- }
-
- if(!nsdoc) {
- doc->basedoc.doc_node = NULL;
- window_set_docnode(doc->basedoc.window, NULL);
- return;
- }
-
- hres = create_doc_from_nsdoc(nsdoc, doc, doc->basedoc.window, &doc_node);
- if(FAILED(hres)) {
- ERR("Could not create document: %08x\n", hres);
- return;
- }
-
- doc->basedoc.doc_node = doc_node;
- window_set_docnode(doc->basedoc.window, doc_node);
-}
-
void close_gecko(void)
{
TRACE("()\n");
diff --git a/dlls/mshtml/nsevents.c b/dlls/mshtml/nsevents.c
index 2a0130a471f..e2758ce2986 100644
--- a/dlls/mshtml/nsevents.c
+++ b/dlls/mshtml/nsevents.c
@@ -137,7 +137,6 @@ static nsresult NSAPI handle_load(nsIDOMEventListener *iface, nsIDOMEvent *event
if(!This->doc)
return NS_OK;
- update_nsdocument(This->doc);
connect_scripts(This->doc->basedoc.window);
if(This->editor_controller) {