mshtml: Detach also internal listeners in detach_events.
[wine] / dlls / mshtml / htmldoc.c
1 /*
2  * Copyright 2005-2009 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "config.h"
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "wininet.h"
30 #include "ole2.h"
31 #include "perhist.h"
32 #include "mshtmdid.h"
33
34 #include "wine/debug.h"
35
36 #include "mshtml_private.h"
37 #include "htmlevent.h"
38 #include "pluginhost.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
41
42 static inline HTMLDocument *impl_from_IHTMLDocument2(IHTMLDocument2 *iface)
43 {
44     return CONTAINING_RECORD(iface, HTMLDocument, IHTMLDocument2_iface);
45 }
46
47 static HRESULT WINAPI HTMLDocument_QueryInterface(IHTMLDocument2 *iface, REFIID riid, void **ppv)
48 {
49     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
50
51     return htmldoc_query_interface(This, riid, ppv);
52 }
53
54 static ULONG WINAPI HTMLDocument_AddRef(IHTMLDocument2 *iface)
55 {
56     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
57
58     return htmldoc_addref(This);
59 }
60
61 static ULONG WINAPI HTMLDocument_Release(IHTMLDocument2 *iface)
62 {
63     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
64
65     return htmldoc_release(This);
66 }
67
68 static HRESULT WINAPI HTMLDocument_GetTypeInfoCount(IHTMLDocument2 *iface, UINT *pctinfo)
69 {
70     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
71
72     return IDispatchEx_GetTypeInfoCount(&This->IDispatchEx_iface, pctinfo);
73 }
74
75 static HRESULT WINAPI HTMLDocument_GetTypeInfo(IHTMLDocument2 *iface, UINT iTInfo,
76                                                 LCID lcid, ITypeInfo **ppTInfo)
77 {
78     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
79
80     return IDispatchEx_GetTypeInfo(&This->IDispatchEx_iface, iTInfo, lcid, ppTInfo);
81 }
82
83 static HRESULT WINAPI HTMLDocument_GetIDsOfNames(IHTMLDocument2 *iface, REFIID riid,
84                                                 LPOLESTR *rgszNames, UINT cNames,
85                                                 LCID lcid, DISPID *rgDispId)
86 {
87     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
88
89     return IDispatchEx_GetIDsOfNames(&This->IDispatchEx_iface, riid, rgszNames, cNames, lcid,
90             rgDispId);
91 }
92
93 static HRESULT WINAPI HTMLDocument_Invoke(IHTMLDocument2 *iface, DISPID dispIdMember,
94                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
95                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
96 {
97     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
98
99     return IDispatchEx_Invoke(&This->IDispatchEx_iface, dispIdMember, riid, lcid, wFlags,
100             pDispParams, pVarResult, pExcepInfo, puArgErr);
101 }
102
103 static HRESULT WINAPI HTMLDocument_get_Script(IHTMLDocument2 *iface, IDispatch **p)
104 {
105     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
106
107     TRACE("(%p)->(%p)\n", This, p);
108
109     *p = (IDispatch*)&This->window->IHTMLWindow2_iface;
110     IDispatch_AddRef(*p);
111     return S_OK;
112 }
113
114 static HRESULT WINAPI HTMLDocument_get_all(IHTMLDocument2 *iface, IHTMLElementCollection **p)
115 {
116     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
117     nsIDOMElement *nselem = NULL;
118     HTMLDOMNode *node;
119     nsresult nsres;
120     HRESULT hres;
121
122     TRACE("(%p)->(%p)\n", This, p);
123
124     if(!This->doc_node->nsdoc) {
125         WARN("NULL nsdoc\n");
126         return E_UNEXPECTED;
127     }
128
129     nsres = nsIDOMHTMLDocument_GetDocumentElement(This->doc_node->nsdoc, &nselem);
130     if(NS_FAILED(nsres)) {
131         ERR("GetDocumentElement failed: %08x\n", nsres);
132         return E_FAIL;
133     }
134
135     if(!nselem) {
136         *p = NULL;
137         return S_OK;
138     }
139
140     hres = get_node(This->doc_node, (nsIDOMNode*)nselem, TRUE, &node);
141     if(SUCCEEDED(hres))
142         *p = create_all_collection(node, TRUE);
143     nsIDOMElement_Release(nselem);
144     return hres;
145 }
146
147 static HRESULT WINAPI HTMLDocument_get_body(IHTMLDocument2 *iface, IHTMLElement **p)
148 {
149     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
150     nsIDOMHTMLElement *nsbody = NULL;
151     HTMLDOMNode *node;
152     HRESULT hres;
153
154     TRACE("(%p)->(%p)\n", This, p);
155
156     if(This->doc_node->nsdoc) {
157         nsresult nsres;
158
159         nsres = nsIDOMHTMLDocument_GetBody(This->doc_node->nsdoc, &nsbody);
160         if(NS_FAILED(nsres)) {
161             TRACE("Could not get body: %08x\n", nsres);
162             return E_UNEXPECTED;
163         }
164     }
165
166     if(!nsbody) {
167         *p = NULL;
168         return S_OK;
169     }
170
171     hres = get_node(This->doc_node, (nsIDOMNode*)nsbody, TRUE, &node);
172     nsIDOMHTMLElement_Release(nsbody);
173     if(FAILED(hres))
174         return hres;
175
176     return IHTMLDOMNode_QueryInterface(&node->IHTMLDOMNode_iface, &IID_IHTMLElement, (void**)p);
177 }
178
179 static HRESULT WINAPI HTMLDocument_get_activeElement(IHTMLDocument2 *iface, IHTMLElement **p)
180 {
181     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
182     FIXME("(%p)->(%p)\n", This, p);
183     return E_NOTIMPL;
184 }
185
186 static HRESULT WINAPI HTMLDocument_get_images(IHTMLDocument2 *iface, IHTMLElementCollection **p)
187 {
188     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
189     nsIDOMHTMLCollection *nscoll = NULL;
190     nsresult nsres;
191
192     TRACE("(%p)->(%p)\n", This, p);
193
194     if(!p)
195         return E_INVALIDARG;
196
197     *p = NULL;
198
199     if(!This->doc_node->nsdoc) {
200         WARN("NULL nsdoc\n");
201         return E_UNEXPECTED;
202     }
203
204     nsres = nsIDOMHTMLDocument_GetImages(This->doc_node->nsdoc, &nscoll);
205     if(NS_FAILED(nsres)) {
206         ERR("GetImages failed: %08x\n", nsres);
207         return E_FAIL;
208     }
209
210     if(nscoll) {
211         *p = create_collection_from_htmlcol(This->doc_node, (IUnknown*)&This->IHTMLDocument2_iface, nscoll);
212         nsIDOMElement_Release(nscoll);
213     }
214
215     return S_OK;
216 }
217
218 static HRESULT WINAPI HTMLDocument_get_applets(IHTMLDocument2 *iface, IHTMLElementCollection **p)
219 {
220     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
221     nsIDOMHTMLCollection *nscoll = NULL;
222     nsresult nsres;
223
224     TRACE("(%p)->(%p)\n", This, p);
225
226     if(!p)
227         return E_INVALIDARG;
228
229     *p = NULL;
230
231     if(!This->doc_node->nsdoc) {
232         WARN("NULL nsdoc\n");
233         return E_UNEXPECTED;
234     }
235
236     nsres = nsIDOMHTMLDocument_GetApplets(This->doc_node->nsdoc, &nscoll);
237     if(NS_FAILED(nsres)) {
238         ERR("GetApplets failed: %08x\n", nsres);
239         return E_FAIL;
240     }
241
242     if(nscoll) {
243         *p = create_collection_from_htmlcol(This->doc_node, (IUnknown*)&This->IHTMLDocument2_iface, nscoll);
244         nsIDOMElement_Release(nscoll);
245     }
246
247     return S_OK;
248 }
249
250 static HRESULT WINAPI HTMLDocument_get_links(IHTMLDocument2 *iface, IHTMLElementCollection **p)
251 {
252     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
253     nsIDOMHTMLCollection *nscoll = NULL;
254     nsresult nsres;
255
256     TRACE("(%p)->(%p)\n", This, p);
257
258     if(!p)
259         return E_INVALIDARG;
260
261     *p = NULL;
262
263     if(!This->doc_node->nsdoc) {
264         WARN("NULL nsdoc\n");
265         return E_UNEXPECTED;
266     }
267
268     nsres = nsIDOMHTMLDocument_GetLinks(This->doc_node->nsdoc, &nscoll);
269     if(NS_FAILED(nsres)) {
270         ERR("GetLinks failed: %08x\n", nsres);
271         return E_FAIL;
272     }
273
274     if(nscoll) {
275         *p = create_collection_from_htmlcol(This->doc_node, (IUnknown*)&This->IHTMLDocument2_iface, nscoll);
276         nsIDOMElement_Release(nscoll);
277     }
278
279     return S_OK;
280 }
281
282 static HRESULT WINAPI HTMLDocument_get_forms(IHTMLDocument2 *iface, IHTMLElementCollection **p)
283 {
284     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
285     nsIDOMHTMLCollection *nscoll = NULL;
286     nsresult nsres;
287
288     TRACE("(%p)->(%p)\n", This, p);
289
290     if(!p)
291         return E_INVALIDARG;
292
293     *p = NULL;
294
295     if(!This->doc_node->nsdoc) {
296         WARN("NULL nsdoc\n");
297         return E_UNEXPECTED;
298     }
299
300     nsres = nsIDOMHTMLDocument_GetForms(This->doc_node->nsdoc, &nscoll);
301     if(NS_FAILED(nsres)) {
302         ERR("GetForms failed: %08x\n", nsres);
303         return E_FAIL;
304     }
305
306     if(nscoll) {
307         *p = create_collection_from_htmlcol(This->doc_node, (IUnknown*)&This->IHTMLDocument2_iface, nscoll);
308         nsIDOMElement_Release(nscoll);
309     }
310
311     return S_OK;
312 }
313
314 static HRESULT WINAPI HTMLDocument_get_anchors(IHTMLDocument2 *iface, IHTMLElementCollection **p)
315 {
316     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
317     nsIDOMHTMLCollection *nscoll = NULL;
318     nsresult nsres;
319
320     TRACE("(%p)->(%p)\n", This, p);
321
322     if(!p)
323         return E_INVALIDARG;
324
325     *p = NULL;
326
327     if(!This->doc_node->nsdoc) {
328         WARN("NULL nsdoc\n");
329         return E_UNEXPECTED;
330     }
331
332     nsres = nsIDOMHTMLDocument_GetAnchors(This->doc_node->nsdoc, &nscoll);
333     if(NS_FAILED(nsres)) {
334         ERR("GetAnchors failed: %08x\n", nsres);
335         return E_FAIL;
336     }
337
338     if(nscoll) {
339         *p = create_collection_from_htmlcol(This->doc_node, (IUnknown*)&This->IHTMLDocument2_iface, nscoll);
340         nsIDOMElement_Release(nscoll);
341     }
342
343     return S_OK;
344 }
345
346 static HRESULT WINAPI HTMLDocument_put_title(IHTMLDocument2 *iface, BSTR v)
347 {
348     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
349     nsAString nsstr;
350     nsresult nsres;
351
352     TRACE("(%p)->(%s)\n", This, debugstr_w(v));
353
354     if(!This->doc_node->nsdoc) {
355         WARN("NULL nsdoc\n");
356         return E_UNEXPECTED;
357     }
358
359     nsAString_InitDepend(&nsstr, v);
360     nsres = nsIDOMHTMLDocument_SetTitle(This->doc_node->nsdoc, &nsstr);
361     nsAString_Finish(&nsstr);
362     if(NS_FAILED(nsres))
363         ERR("SetTitle failed: %08x\n", nsres);
364
365     return S_OK;
366 }
367
368 static HRESULT WINAPI HTMLDocument_get_title(IHTMLDocument2 *iface, BSTR *p)
369 {
370     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
371     const PRUnichar *ret;
372     nsAString nsstr;
373     nsresult nsres;
374
375     TRACE("(%p)->(%p)\n", This, p);
376
377     if(!This->doc_node->nsdoc) {
378         WARN("NULL nsdoc\n");
379         return E_UNEXPECTED;
380     }
381
382
383     nsAString_Init(&nsstr, NULL);
384     nsres = nsIDOMHTMLDocument_GetTitle(This->doc_node->nsdoc, &nsstr);
385     if (NS_SUCCEEDED(nsres)) {
386         nsAString_GetData(&nsstr, &ret);
387         *p = SysAllocString(ret);
388     }
389     nsAString_Finish(&nsstr);
390
391     if(NS_FAILED(nsres)) {
392         ERR("GetTitle failed: %08x\n", nsres);
393         return E_FAIL;
394     }
395
396     return S_OK;
397 }
398
399 static HRESULT WINAPI HTMLDocument_get_scripts(IHTMLDocument2 *iface, IHTMLElementCollection **p)
400 {
401     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
402     FIXME("(%p)->(%p)\n", This, p);
403     return E_NOTIMPL;
404 }
405
406 static HRESULT WINAPI HTMLDocument_put_designMode(IHTMLDocument2 *iface, BSTR v)
407 {
408     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
409     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
410     return E_NOTIMPL;
411 }
412
413 static HRESULT WINAPI HTMLDocument_get_designMode(IHTMLDocument2 *iface, BSTR *p)
414 {
415     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
416     static WCHAR szOff[] = {'O','f','f',0};
417     FIXME("(%p)->(%p) always returning Off\n", This, p);
418
419     if(!p)
420         return E_INVALIDARG;
421
422     *p = SysAllocString(szOff);
423
424     return S_OK;
425 }
426
427 static HRESULT WINAPI HTMLDocument_get_selection(IHTMLDocument2 *iface, IHTMLSelectionObject **p)
428 {
429     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
430     nsISelection *nsselection;
431     nsresult nsres;
432
433     TRACE("(%p)->(%p)\n", This, p);
434
435     nsres = nsIDOMWindow_GetSelection(This->window->nswindow, &nsselection);
436     if(NS_FAILED(nsres)) {
437         ERR("GetSelection failed: %08x\n", nsres);
438         return E_FAIL;
439     }
440
441     return HTMLSelectionObject_Create(This->doc_node, nsselection, p);
442 }
443
444 static HRESULT WINAPI HTMLDocument_get_readyState(IHTMLDocument2 *iface, BSTR *p)
445 {
446     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
447
448     static const WCHAR wszUninitialized[] = {'u','n','i','n','i','t','i','a','l','i','z','e','d',0};
449     static const WCHAR wszLoading[] = {'l','o','a','d','i','n','g',0};
450     static const WCHAR wszLoaded[] = {'l','o','a','d','e','d',0};
451     static const WCHAR wszInteractive[] = {'i','n','t','e','r','a','c','t','i','v','e',0};
452     static const WCHAR wszComplete[] = {'c','o','m','p','l','e','t','e',0};
453
454     static const LPCWSTR readystate_str[] = {
455         wszUninitialized,
456         wszLoading,
457         wszLoaded,
458         wszInteractive,
459         wszComplete
460     };
461
462     TRACE("(%p)->(%p)\n", iface, p);
463
464     if(!p)
465         return E_POINTER;
466
467     *p = SysAllocString(readystate_str[This->window->readystate]);
468     return S_OK;
469 }
470
471 static HRESULT WINAPI HTMLDocument_get_frames(IHTMLDocument2 *iface, IHTMLFramesCollection2 **p)
472 {
473     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
474
475     TRACE("(%p)->(%p)\n", This, p);
476
477     return IHTMLWindow2_get_frames(&This->window->IHTMLWindow2_iface, p);
478 }
479
480 static HRESULT WINAPI HTMLDocument_get_embeds(IHTMLDocument2 *iface, IHTMLElementCollection **p)
481 {
482     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
483     FIXME("(%p)->(%p)\n", This, p);
484     return E_NOTIMPL;
485 }
486
487 static HRESULT WINAPI HTMLDocument_get_plugins(IHTMLDocument2 *iface, IHTMLElementCollection **p)
488 {
489     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
490     FIXME("(%p)->(%p)\n", This, p);
491     return E_NOTIMPL;
492 }
493
494 static HRESULT WINAPI HTMLDocument_put_alinkColor(IHTMLDocument2 *iface, VARIANT v)
495 {
496     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
497     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
498     return E_NOTIMPL;
499 }
500
501 static HRESULT WINAPI HTMLDocument_get_alinkColor(IHTMLDocument2 *iface, VARIANT *p)
502 {
503     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
504     FIXME("(%p)->(%p)\n", This, p);
505     return E_NOTIMPL;
506 }
507
508 static HRESULT WINAPI HTMLDocument_put_bgColor(IHTMLDocument2 *iface, VARIANT v)
509 {
510     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
511     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
512     return E_NOTIMPL;
513 }
514
515 static HRESULT WINAPI HTMLDocument_get_bgColor(IHTMLDocument2 *iface, VARIANT *p)
516 {
517     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
518     FIXME("(%p)->(%p)\n", This, p);
519     return E_NOTIMPL;
520 }
521
522 static HRESULT WINAPI HTMLDocument_put_fgColor(IHTMLDocument2 *iface, VARIANT v)
523 {
524     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
525     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
526     return E_NOTIMPL;
527 }
528
529 static HRESULT WINAPI HTMLDocument_get_fgColor(IHTMLDocument2 *iface, VARIANT *p)
530 {
531     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
532     FIXME("(%p)->(%p)\n", This, p);
533     return E_NOTIMPL;
534 }
535
536 static HRESULT WINAPI HTMLDocument_put_linkColor(IHTMLDocument2 *iface, VARIANT v)
537 {
538     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
539     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
540     return E_NOTIMPL;
541 }
542
543 static HRESULT WINAPI HTMLDocument_get_linkColor(IHTMLDocument2 *iface, VARIANT *p)
544 {
545     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
546     FIXME("(%p)->(%p)\n", This, p);
547     return E_NOTIMPL;
548 }
549
550 static HRESULT WINAPI HTMLDocument_put_vlinkColor(IHTMLDocument2 *iface, VARIANT v)
551 {
552     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
553     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
554     return E_NOTIMPL;
555 }
556
557 static HRESULT WINAPI HTMLDocument_get_vlinkColor(IHTMLDocument2 *iface, VARIANT *p)
558 {
559     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
560     FIXME("(%p)->(%p)\n", This, p);
561     return E_NOTIMPL;
562 }
563
564 static HRESULT WINAPI HTMLDocument_get_referrer(IHTMLDocument2 *iface, BSTR *p)
565 {
566     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
567
568     FIXME("(%p)->(%p)\n", This, p);
569
570     *p = NULL;
571     return S_OK;
572  }
573
574 static HRESULT WINAPI HTMLDocument_get_location(IHTMLDocument2 *iface, IHTMLLocation **p)
575 {
576     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
577
578     TRACE("(%p)->(%p)\n", This, p);
579
580     if(!This->doc_node->nsdoc) {
581         WARN("NULL nsdoc\n");
582         return E_UNEXPECTED;
583     }
584
585     return IHTMLWindow2_get_location(&This->window->IHTMLWindow2_iface, p);
586 }
587
588 static HRESULT WINAPI HTMLDocument_get_lastModified(IHTMLDocument2 *iface, BSTR *p)
589 {
590     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
591     FIXME("(%p)->(%p)\n", This, p);
592     return E_NOTIMPL;
593 }
594
595 static HRESULT WINAPI HTMLDocument_put_URL(IHTMLDocument2 *iface, BSTR v)
596 {
597     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
598     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
599     return E_NOTIMPL;
600 }
601
602 static HRESULT WINAPI HTMLDocument_get_URL(IHTMLDocument2 *iface, BSTR *p)
603 {
604     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
605
606     static const WCHAR about_blank_url[] =
607         {'a','b','o','u','t',':','b','l','a','n','k',0};
608
609     TRACE("(%p)->(%p)\n", iface, p);
610
611     *p = SysAllocString(This->window->url ? This->window->url : about_blank_url);
612     return *p ? S_OK : E_OUTOFMEMORY;
613 }
614
615 static HRESULT WINAPI HTMLDocument_put_domain(IHTMLDocument2 *iface, BSTR v)
616 {
617     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
618     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
619     return E_NOTIMPL;
620 }
621
622 static HRESULT WINAPI HTMLDocument_get_domain(IHTMLDocument2 *iface, BSTR *p)
623 {
624     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
625     HRESULT hres;
626
627     TRACE("(%p)->(%p)\n", This, p);
628
629     if(!This->window || !This->window->uri) {
630         FIXME("No current URI\n");
631         return E_FAIL;
632     }
633
634     hres = IUri_GetHost(This->window->uri, p);
635     return FAILED(hres) ? hres : S_OK;
636 }
637
638 static HRESULT WINAPI HTMLDocument_put_cookie(IHTMLDocument2 *iface, BSTR v)
639 {
640     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
641     BOOL bret;
642
643     TRACE("(%p)->(%s)\n", This, debugstr_w(v));
644
645     bret = InternetSetCookieExW(This->window->url, NULL, v, 0, 0);
646     if(!bret) {
647         FIXME("InternetSetCookieExW failed: %u\n", GetLastError());
648         return HRESULT_FROM_WIN32(GetLastError());
649     }
650
651     return S_OK;
652 }
653
654 static HRESULT WINAPI HTMLDocument_get_cookie(IHTMLDocument2 *iface, BSTR *p)
655 {
656     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
657     DWORD size;
658     BOOL bret;
659
660     TRACE("(%p)->(%p)\n", This, p);
661
662     size = 0;
663     bret = InternetGetCookieExW(This->window->url, NULL, NULL, &size, 0, NULL);
664     if(!bret) {
665         switch(GetLastError()) {
666         case ERROR_INSUFFICIENT_BUFFER:
667             break;
668         case ERROR_NO_MORE_ITEMS:
669             *p = NULL;
670             return S_OK;
671         default:
672             FIXME("InternetGetCookieExW failed: %u\n", GetLastError());
673             return HRESULT_FROM_WIN32(GetLastError());
674         }
675     }
676
677     if(!size) {
678         *p = NULL;
679         return S_OK;
680     }
681
682     *p = SysAllocStringLen(NULL, size-1);
683     if(!*p)
684         return E_OUTOFMEMORY;
685
686     bret = InternetGetCookieExW(This->window->url, NULL, *p, &size, 0, NULL);
687     if(!bret) {
688         ERR("InternetGetCookieExW failed: %u\n", GetLastError());
689         return E_FAIL;
690     }
691
692     return S_OK;
693 }
694
695 static HRESULT WINAPI HTMLDocument_put_expando(IHTMLDocument2 *iface, VARIANT_BOOL v)
696 {
697     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
698     FIXME("(%p)->(%x)\n", This, v);
699     return E_NOTIMPL;
700 }
701
702 static HRESULT WINAPI HTMLDocument_get_expando(IHTMLDocument2 *iface, VARIANT_BOOL *p)
703 {
704     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
705     FIXME("(%p)->(%p)\n", This, p);
706     return E_NOTIMPL;
707 }
708
709 static HRESULT WINAPI HTMLDocument_put_charset(IHTMLDocument2 *iface, BSTR v)
710 {
711     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
712     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
713     return E_NOTIMPL;
714 }
715
716 static HRESULT WINAPI HTMLDocument_get_charset(IHTMLDocument2 *iface, BSTR *p)
717 {
718     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
719     FIXME("(%p)->(%p)\n", This, p);
720     return E_NOTIMPL;
721 }
722
723 static HRESULT WINAPI HTMLDocument_put_defaultCharset(IHTMLDocument2 *iface, BSTR v)
724 {
725     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
726     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
727     return E_NOTIMPL;
728 }
729
730 static HRESULT WINAPI HTMLDocument_get_defaultCharset(IHTMLDocument2 *iface, BSTR *p)
731 {
732     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
733     FIXME("(%p)->(%p)\n", This, p);
734     return E_NOTIMPL;
735 }
736
737 static HRESULT WINAPI HTMLDocument_get_mimeType(IHTMLDocument2 *iface, BSTR *p)
738 {
739     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
740     FIXME("(%p)->(%p)\n", This, p);
741     return E_NOTIMPL;
742 }
743
744 static HRESULT WINAPI HTMLDocument_get_fileSize(IHTMLDocument2 *iface, BSTR *p)
745 {
746     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
747     FIXME("(%p)->(%p)\n", This, p);
748     return E_NOTIMPL;
749 }
750
751 static HRESULT WINAPI HTMLDocument_get_fileCreatedDate(IHTMLDocument2 *iface, BSTR *p)
752 {
753     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
754     FIXME("(%p)->(%p)\n", This, p);
755     return E_NOTIMPL;
756 }
757
758 static HRESULT WINAPI HTMLDocument_get_fileModifiedDate(IHTMLDocument2 *iface, BSTR *p)
759 {
760     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
761     FIXME("(%p)->(%p)\n", This, p);
762     return E_NOTIMPL;
763 }
764
765 static HRESULT WINAPI HTMLDocument_get_fileUpdatedDate(IHTMLDocument2 *iface, BSTR *p)
766 {
767     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
768     FIXME("(%p)->(%p)\n", This, p);
769     return E_NOTIMPL;
770 }
771
772 static HRESULT WINAPI HTMLDocument_get_security(IHTMLDocument2 *iface, BSTR *p)
773 {
774     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
775     FIXME("(%p)->(%p)\n", This, p);
776     return E_NOTIMPL;
777 }
778
779 static HRESULT WINAPI HTMLDocument_get_protocol(IHTMLDocument2 *iface, BSTR *p)
780 {
781     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
782     FIXME("(%p)->(%p)\n", This, p);
783     return E_NOTIMPL;
784 }
785
786 static HRESULT WINAPI HTMLDocument_get_nameProp(IHTMLDocument2 *iface, BSTR *p)
787 {
788     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
789     FIXME("(%p)->(%p)\n", This, p);
790     return E_NOTIMPL;
791 }
792
793 static HRESULT document_write(HTMLDocument *This, SAFEARRAY *psarray, BOOL ln)
794 {
795     nsAString nsstr;
796     VARIANT *var;
797     ULONG i, argc;
798     nsresult nsres;
799     HRESULT hres;
800
801     if(!This->doc_node->nsdoc) {
802         WARN("NULL nsdoc\n");
803         return E_UNEXPECTED;
804     }
805
806     if (!psarray)
807         return S_OK;
808
809     if(psarray->cDims != 1) {
810         FIXME("cDims=%d\n", psarray->cDims);
811         return E_INVALIDARG;
812     }
813
814     hres = SafeArrayAccessData(psarray, (void**)&var);
815     if(FAILED(hres)) {
816         WARN("SafeArrayAccessData failed: %08x\n", hres);
817         return hres;
818     }
819
820     nsAString_Init(&nsstr, NULL);
821
822     argc = psarray->rgsabound[0].cElements;
823     for(i=0; i < argc; i++) {
824         if(V_VT(var+i) == VT_BSTR) {
825             nsAString_SetData(&nsstr, V_BSTR(var+i));
826             if(!ln || i != argc-1)
827                 nsres = nsIDOMHTMLDocument_Write(This->doc_node->nsdoc, &nsstr, NULL /* FIXME! */);
828             else
829                 nsres = nsIDOMHTMLDocument_Writeln(This->doc_node->nsdoc, &nsstr, NULL /* FIXME! */);
830             if(NS_FAILED(nsres))
831                 ERR("Write failed: %08x\n", nsres);
832         }else {
833             FIXME("unsupported arg[%d] = %s\n", i, debugstr_variant(var+i));
834         }
835     }
836
837     nsAString_Finish(&nsstr);
838     SafeArrayUnaccessData(psarray);
839
840     return S_OK;
841 }
842
843 static HRESULT WINAPI HTMLDocument_write(IHTMLDocument2 *iface, SAFEARRAY *psarray)
844 {
845     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
846
847     TRACE("(%p)->(%p)\n", iface, psarray);
848
849     return document_write(This, psarray, FALSE);
850 }
851
852 static HRESULT WINAPI HTMLDocument_writeln(IHTMLDocument2 *iface, SAFEARRAY *psarray)
853 {
854     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
855
856     TRACE("(%p)->(%p)\n", This, psarray);
857
858     return document_write(This, psarray, TRUE);
859 }
860
861 static HRESULT WINAPI HTMLDocument_open(IHTMLDocument2 *iface, BSTR url, VARIANT name,
862                         VARIANT features, VARIANT replace, IDispatch **pomWindowResult)
863 {
864     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
865     nsISupports *tmp;
866     nsresult nsres;
867
868     static const WCHAR text_htmlW[] = {'t','e','x','t','/','h','t','m','l',0};
869
870     TRACE("(%p)->(%s %s %s %s %p)\n", This, debugstr_w(url), debugstr_variant(&name),
871           debugstr_variant(&features), debugstr_variant(&replace), pomWindowResult);
872
873     if(!This->doc_node->nsdoc) {
874         ERR("!nsdoc\n");
875         return E_NOTIMPL;
876     }
877
878     if(!url || strcmpW(url, text_htmlW) || V_VT(&name) != VT_ERROR
879        || V_VT(&features) != VT_ERROR || V_VT(&replace) != VT_ERROR)
880         FIXME("unsupported args\n");
881
882     nsres = nsIDOMHTMLDocument_Open(This->doc_node->nsdoc, NULL, NULL, NULL, NULL, 0, &tmp);
883     if(NS_FAILED(nsres)) {
884         ERR("Open failed: %08x\n", nsres);
885         return E_FAIL;
886     }
887
888     if(tmp)
889         nsISupports_Release(tmp);
890
891     *pomWindowResult = (IDispatch*)&This->window->IHTMLWindow2_iface;
892     IHTMLWindow2_AddRef(&This->window->IHTMLWindow2_iface);
893     return S_OK;
894 }
895
896 static HRESULT WINAPI HTMLDocument_close(IHTMLDocument2 *iface)
897 {
898     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
899     nsresult nsres;
900
901     TRACE("(%p)\n", This);
902
903     if(!This->doc_node->nsdoc) {
904         ERR("!nsdoc\n");
905         return E_NOTIMPL;
906     }
907
908     nsres = nsIDOMHTMLDocument_Close(This->doc_node->nsdoc);
909     if(NS_FAILED(nsres)) {
910         ERR("Close failed: %08x\n", nsres);
911         return E_FAIL;
912     }
913
914     return S_OK;
915 }
916
917 static HRESULT WINAPI HTMLDocument_clear(IHTMLDocument2 *iface)
918 {
919     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
920     nsresult nsres;
921
922     TRACE("(%p)\n", This);
923
924     nsres = nsIDOMHTMLDocument_Clear(This->doc_node->nsdoc);
925     if(NS_FAILED(nsres)) {
926         ERR("Clear failed: %08x\n", nsres);
927         return E_FAIL;
928     }
929
930     return S_OK;
931 }
932
933 static HRESULT WINAPI HTMLDocument_queryCommandSupported(IHTMLDocument2 *iface, BSTR cmdID,
934                                                         VARIANT_BOOL *pfRet)
935 {
936     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
937     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
938     return E_NOTIMPL;
939 }
940
941 static HRESULT WINAPI HTMLDocument_queryCommandEnabled(IHTMLDocument2 *iface, BSTR cmdID,
942                                                         VARIANT_BOOL *pfRet)
943 {
944     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
945     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
946     return E_NOTIMPL;
947 }
948
949 static HRESULT WINAPI HTMLDocument_queryCommandState(IHTMLDocument2 *iface, BSTR cmdID,
950                                                         VARIANT_BOOL *pfRet)
951 {
952     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
953     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
954     return E_NOTIMPL;
955 }
956
957 static HRESULT WINAPI HTMLDocument_queryCommandIndeterm(IHTMLDocument2 *iface, BSTR cmdID,
958                                                         VARIANT_BOOL *pfRet)
959 {
960     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
961     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
962     return E_NOTIMPL;
963 }
964
965 static HRESULT WINAPI HTMLDocument_queryCommandText(IHTMLDocument2 *iface, BSTR cmdID,
966                                                         BSTR *pfRet)
967 {
968     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
969     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
970     return E_NOTIMPL;
971 }
972
973 static HRESULT WINAPI HTMLDocument_queryCommandValue(IHTMLDocument2 *iface, BSTR cmdID,
974                                                         VARIANT *pfRet)
975 {
976     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
977     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
978     return E_NOTIMPL;
979 }
980
981 static HRESULT WINAPI HTMLDocument_execCommand(IHTMLDocument2 *iface, BSTR cmdID,
982                                 VARIANT_BOOL showUI, VARIANT value, VARIANT_BOOL *pfRet)
983 {
984     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
985     FIXME("(%p)->(%s %x %s %p)\n", This, debugstr_w(cmdID), showUI, debugstr_variant(&value), pfRet);
986     return E_NOTIMPL;
987 }
988
989 static HRESULT WINAPI HTMLDocument_execCommandShowHelp(IHTMLDocument2 *iface, BSTR cmdID,
990                                                         VARIANT_BOOL *pfRet)
991 {
992     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
993     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
994     return E_NOTIMPL;
995 }
996
997 static HRESULT WINAPI HTMLDocument_createElement(IHTMLDocument2 *iface, BSTR eTag,
998                                                  IHTMLElement **newElem)
999 {
1000     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1001     nsIDOMHTMLElement *nselem;
1002     HTMLElement *elem;
1003     HRESULT hres;
1004
1005     TRACE("(%p)->(%s %p)\n", This, debugstr_w(eTag), newElem);
1006
1007     hres = create_nselem(This->doc_node, eTag, &nselem);
1008     if(FAILED(hres))
1009         return hres;
1010
1011     hres = HTMLElement_Create(This->doc_node, (nsIDOMNode*)nselem, TRUE, &elem);
1012     nsIDOMHTMLElement_Release(nselem);
1013     if(FAILED(hres))
1014         return hres;
1015
1016     *newElem = &elem->IHTMLElement_iface;
1017     IHTMLElement_AddRef(&elem->IHTMLElement_iface);
1018     return S_OK;
1019 }
1020
1021 static HRESULT WINAPI HTMLDocument_put_onhelp(IHTMLDocument2 *iface, VARIANT v)
1022 {
1023     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1024     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1025     return E_NOTIMPL;
1026 }
1027
1028 static HRESULT WINAPI HTMLDocument_get_onhelp(IHTMLDocument2 *iface, VARIANT *p)
1029 {
1030     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1031     FIXME("(%p)->(%p)\n", This, p);
1032     return E_NOTIMPL;
1033 }
1034
1035 static HRESULT WINAPI HTMLDocument_put_onclick(IHTMLDocument2 *iface, VARIANT v)
1036 {
1037     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1038
1039     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1040
1041     return set_doc_event(This, EVENTID_CLICK, &v);
1042 }
1043
1044 static HRESULT WINAPI HTMLDocument_get_onclick(IHTMLDocument2 *iface, VARIANT *p)
1045 {
1046     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1047
1048     TRACE("(%p)->(%p)\n", This, p);
1049
1050     return get_doc_event(This, EVENTID_CLICK, p);
1051 }
1052
1053 static HRESULT WINAPI HTMLDocument_put_ondblclick(IHTMLDocument2 *iface, VARIANT v)
1054 {
1055     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1056     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1057     return E_NOTIMPL;
1058 }
1059
1060 static HRESULT WINAPI HTMLDocument_get_ondblclick(IHTMLDocument2 *iface, VARIANT *p)
1061 {
1062     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1063     FIXME("(%p)->(%p)\n", This, p);
1064     return E_NOTIMPL;
1065 }
1066
1067 static HRESULT WINAPI HTMLDocument_put_onkeyup(IHTMLDocument2 *iface, VARIANT v)
1068 {
1069     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1070
1071     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1072
1073     return set_doc_event(This, EVENTID_KEYUP, &v);
1074 }
1075
1076 static HRESULT WINAPI HTMLDocument_get_onkeyup(IHTMLDocument2 *iface, VARIANT *p)
1077 {
1078     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1079
1080     TRACE("(%p)->(%p)\n", This, p);
1081
1082     return get_doc_event(This, EVENTID_KEYUP, p);
1083 }
1084
1085 static HRESULT WINAPI HTMLDocument_put_onkeydown(IHTMLDocument2 *iface, VARIANT v)
1086 {
1087     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1088
1089     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1090
1091     return set_doc_event(This, EVENTID_KEYDOWN, &v);
1092 }
1093
1094 static HRESULT WINAPI HTMLDocument_get_onkeydown(IHTMLDocument2 *iface, VARIANT *p)
1095 {
1096     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1097
1098     TRACE("(%p)->(%p)\n", This, p);
1099
1100     return get_doc_event(This, EVENTID_KEYDOWN, p);
1101 }
1102
1103 static HRESULT WINAPI HTMLDocument_put_onkeypress(IHTMLDocument2 *iface, VARIANT v)
1104 {
1105     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1106     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1107     return E_NOTIMPL;
1108 }
1109
1110 static HRESULT WINAPI HTMLDocument_get_onkeypress(IHTMLDocument2 *iface, VARIANT *p)
1111 {
1112     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1113     FIXME("(%p)->(%p)\n", This, p);
1114     return E_NOTIMPL;
1115 }
1116
1117 static HRESULT WINAPI HTMLDocument_put_onmouseup(IHTMLDocument2 *iface, VARIANT v)
1118 {
1119     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1120
1121     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1122
1123     return set_doc_event(This, EVENTID_MOUSEUP, &v);
1124 }
1125
1126 static HRESULT WINAPI HTMLDocument_get_onmouseup(IHTMLDocument2 *iface, VARIANT *p)
1127 {
1128     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1129
1130     TRACE("(%p)->(%p)\n", This, p);
1131
1132     return get_doc_event(This, EVENTID_MOUSEUP, p);
1133 }
1134
1135 static HRESULT WINAPI HTMLDocument_put_onmousedown(IHTMLDocument2 *iface, VARIANT v)
1136 {
1137     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1138
1139     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1140
1141     return set_doc_event(This, EVENTID_MOUSEDOWN, &v);
1142 }
1143
1144 static HRESULT WINAPI HTMLDocument_get_onmousedown(IHTMLDocument2 *iface, VARIANT *p)
1145 {
1146     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1147
1148     TRACE("(%p)->(%p)\n", This, p);
1149
1150     return get_doc_event(This, EVENTID_MOUSEDOWN, p);
1151 }
1152
1153 static HRESULT WINAPI HTMLDocument_put_onmousemove(IHTMLDocument2 *iface, VARIANT v)
1154 {
1155     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1156
1157     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1158
1159     return set_doc_event(This, EVENTID_MOUSEMOVE, &v);
1160 }
1161
1162 static HRESULT WINAPI HTMLDocument_get_onmousemove(IHTMLDocument2 *iface, VARIANT *p)
1163 {
1164     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1165
1166     TRACE("(%p)->(%p)\n", This, p);
1167
1168     return get_doc_event(This, EVENTID_MOUSEMOVE, p);
1169 }
1170
1171 static HRESULT WINAPI HTMLDocument_put_onmouseout(IHTMLDocument2 *iface, VARIANT v)
1172 {
1173     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1174
1175     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1176
1177     return set_doc_event(This, EVENTID_MOUSEOUT, &v);
1178 }
1179
1180 static HRESULT WINAPI HTMLDocument_get_onmouseout(IHTMLDocument2 *iface, VARIANT *p)
1181 {
1182     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1183
1184     TRACE("(%p)->(%p)\n", This, p);
1185
1186     return get_doc_event(This, EVENTID_MOUSEOUT, p);
1187 }
1188
1189 static HRESULT WINAPI HTMLDocument_put_onmouseover(IHTMLDocument2 *iface, VARIANT v)
1190 {
1191     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1192
1193     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1194
1195     return set_doc_event(This, EVENTID_MOUSEOVER, &v);
1196 }
1197
1198 static HRESULT WINAPI HTMLDocument_get_onmouseover(IHTMLDocument2 *iface, VARIANT *p)
1199 {
1200     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1201
1202     TRACE("(%p)->(%p)\n", This, p);
1203
1204     return get_doc_event(This, EVENTID_MOUSEOVER, p);
1205 }
1206
1207 static HRESULT WINAPI HTMLDocument_put_onreadystatechange(IHTMLDocument2 *iface, VARIANT v)
1208 {
1209     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1210
1211     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1212
1213     return set_doc_event(This, EVENTID_READYSTATECHANGE, &v);
1214 }
1215
1216 static HRESULT WINAPI HTMLDocument_get_onreadystatechange(IHTMLDocument2 *iface, VARIANT *p)
1217 {
1218     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1219
1220     TRACE("(%p)->(%p)\n", This, p);
1221
1222     return get_doc_event(This, EVENTID_READYSTATECHANGE, p);
1223 }
1224
1225 static HRESULT WINAPI HTMLDocument_put_onafterupdate(IHTMLDocument2 *iface, VARIANT v)
1226 {
1227     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1228     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1229     return E_NOTIMPL;
1230 }
1231
1232 static HRESULT WINAPI HTMLDocument_get_onafterupdate(IHTMLDocument2 *iface, VARIANT *p)
1233 {
1234     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1235     FIXME("(%p)->(%p)\n", This, p);
1236     return E_NOTIMPL;
1237 }
1238
1239 static HRESULT WINAPI HTMLDocument_put_onrowexit(IHTMLDocument2 *iface, VARIANT v)
1240 {
1241     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1242     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1243     return E_NOTIMPL;
1244 }
1245
1246 static HRESULT WINAPI HTMLDocument_get_onrowexit(IHTMLDocument2 *iface, VARIANT *p)
1247 {
1248     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1249     FIXME("(%p)->(%p)\n", This, p);
1250     return E_NOTIMPL;
1251 }
1252
1253 static HRESULT WINAPI HTMLDocument_put_onrowenter(IHTMLDocument2 *iface, VARIANT v)
1254 {
1255     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1256     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1257     return E_NOTIMPL;
1258 }
1259
1260 static HRESULT WINAPI HTMLDocument_get_onrowenter(IHTMLDocument2 *iface, VARIANT *p)
1261 {
1262     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1263     FIXME("(%p)->(%p)\n", This, p);
1264     return E_NOTIMPL;
1265 }
1266
1267 static HRESULT WINAPI HTMLDocument_put_ondragstart(IHTMLDocument2 *iface, VARIANT v)
1268 {
1269     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1270
1271     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1272
1273     return set_doc_event(This, EVENTID_DRAGSTART, &v);
1274 }
1275
1276 static HRESULT WINAPI HTMLDocument_get_ondragstart(IHTMLDocument2 *iface, VARIANT *p)
1277 {
1278     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1279
1280     TRACE("(%p)->(%p)\n", This, p);
1281
1282     return get_doc_event(This, EVENTID_DRAGSTART, p);
1283 }
1284
1285 static HRESULT WINAPI HTMLDocument_put_onselectstart(IHTMLDocument2 *iface, VARIANT v)
1286 {
1287     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1288
1289     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1290
1291     return set_doc_event(This, EVENTID_SELECTSTART, &v);
1292 }
1293
1294 static HRESULT WINAPI HTMLDocument_get_onselectstart(IHTMLDocument2 *iface, VARIANT *p)
1295 {
1296     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1297
1298     TRACE("(%p)->(%p)\n", This, p);
1299
1300     return get_doc_event(This, EVENTID_SELECTSTART, p);
1301 }
1302
1303 static HRESULT WINAPI HTMLDocument_elementFromPoint(IHTMLDocument2 *iface, LONG x, LONG y,
1304                                                         IHTMLElement **elementHit)
1305 {
1306     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1307     nsIDOMElement *nselem;
1308     HTMLDOMNode *node;
1309     nsresult nsres;
1310     HRESULT hres;
1311
1312     TRACE("(%p)->(%d %d %p)\n", This, x, y, elementHit);
1313
1314     nsres = nsIDOMHTMLDocument_ElementFromPoint(This->doc_node->nsdoc, x, y, &nselem);
1315     if(NS_FAILED(nsres)) {
1316         ERR("ElementFromPoint failed: %08x\n", nsres);
1317         return E_FAIL;
1318     }
1319
1320     if(!nselem) {
1321         *elementHit = NULL;
1322         return S_OK;
1323     }
1324
1325     hres = get_node(This->doc_node, (nsIDOMNode*)nselem, TRUE, &node);
1326     nsIDOMElement_Release(nselem);
1327     if(FAILED(hres))
1328         return hres;
1329
1330     return IHTMLDOMNode_QueryInterface(&node->IHTMLDOMNode_iface, &IID_IHTMLElement, (void**)elementHit);
1331 }
1332
1333 static HRESULT WINAPI HTMLDocument_get_parentWindow(IHTMLDocument2 *iface, IHTMLWindow2 **p)
1334 {
1335     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1336
1337     TRACE("(%p)->(%p)\n", This, p);
1338
1339     *p = &This->window->IHTMLWindow2_iface;
1340     IHTMLWindow2_AddRef(*p);
1341     return S_OK;
1342 }
1343
1344 static HRESULT WINAPI HTMLDocument_get_styleSheets(IHTMLDocument2 *iface,
1345                                                    IHTMLStyleSheetsCollection **p)
1346 {
1347     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1348     nsIDOMStyleSheetList *nsstylelist;
1349     nsresult nsres;
1350
1351     TRACE("(%p)->(%p)\n", This, p);
1352
1353     *p = NULL;
1354
1355     if(!This->doc_node->nsdoc) {
1356         WARN("NULL nsdoc\n");
1357         return E_UNEXPECTED;
1358     }
1359
1360     nsres = nsIDOMHTMLDocument_GetStyleSheets(This->doc_node->nsdoc, &nsstylelist);
1361     if(NS_FAILED(nsres)) {
1362         ERR("GetStyleSheets failed: %08x\n", nsres);
1363         return E_FAIL;
1364     }
1365
1366     *p = HTMLStyleSheetsCollection_Create(nsstylelist);
1367     nsIDOMStyleSheetList_Release(nsstylelist);
1368
1369     return S_OK;
1370 }
1371
1372 static HRESULT WINAPI HTMLDocument_put_onbeforeupdate(IHTMLDocument2 *iface, VARIANT v)
1373 {
1374     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1375     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1376     return E_NOTIMPL;
1377 }
1378
1379 static HRESULT WINAPI HTMLDocument_get_onbeforeupdate(IHTMLDocument2 *iface, VARIANT *p)
1380 {
1381     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1382     FIXME("(%p)->(%p)\n", This, p);
1383     return E_NOTIMPL;
1384 }
1385
1386 static HRESULT WINAPI HTMLDocument_put_onerrorupdate(IHTMLDocument2 *iface, VARIANT v)
1387 {
1388     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1389     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1390     return E_NOTIMPL;
1391 }
1392
1393 static HRESULT WINAPI HTMLDocument_get_onerrorupdate(IHTMLDocument2 *iface, VARIANT *p)
1394 {
1395     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1396     FIXME("(%p)->(%p)\n", This, p);
1397     return E_NOTIMPL;
1398 }
1399
1400 static HRESULT WINAPI HTMLDocument_toString(IHTMLDocument2 *iface, BSTR *String)
1401 {
1402     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1403     FIXME("(%p)->(%p)\n", This, String);
1404     return E_NOTIMPL;
1405 }
1406
1407 static HRESULT WINAPI HTMLDocument_createStyleSheet(IHTMLDocument2 *iface, BSTR bstrHref,
1408                                             LONG lIndex, IHTMLStyleSheet **ppnewStyleSheet)
1409 {
1410     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1411
1412     FIXME("(%p)->(%s %d %p) semi-stub\n", This, debugstr_w(bstrHref), lIndex, ppnewStyleSheet);
1413
1414     *ppnewStyleSheet = HTMLStyleSheet_Create(NULL);
1415     return S_OK;
1416 }
1417
1418 static const IHTMLDocument2Vtbl HTMLDocumentVtbl = {
1419     HTMLDocument_QueryInterface,
1420     HTMLDocument_AddRef,
1421     HTMLDocument_Release,
1422     HTMLDocument_GetTypeInfoCount,
1423     HTMLDocument_GetTypeInfo,
1424     HTMLDocument_GetIDsOfNames,
1425     HTMLDocument_Invoke,
1426     HTMLDocument_get_Script,
1427     HTMLDocument_get_all,
1428     HTMLDocument_get_body,
1429     HTMLDocument_get_activeElement,
1430     HTMLDocument_get_images,
1431     HTMLDocument_get_applets,
1432     HTMLDocument_get_links,
1433     HTMLDocument_get_forms,
1434     HTMLDocument_get_anchors,
1435     HTMLDocument_put_title,
1436     HTMLDocument_get_title,
1437     HTMLDocument_get_scripts,
1438     HTMLDocument_put_designMode,
1439     HTMLDocument_get_designMode,
1440     HTMLDocument_get_selection,
1441     HTMLDocument_get_readyState,
1442     HTMLDocument_get_frames,
1443     HTMLDocument_get_embeds,
1444     HTMLDocument_get_plugins,
1445     HTMLDocument_put_alinkColor,
1446     HTMLDocument_get_alinkColor,
1447     HTMLDocument_put_bgColor,
1448     HTMLDocument_get_bgColor,
1449     HTMLDocument_put_fgColor,
1450     HTMLDocument_get_fgColor,
1451     HTMLDocument_put_linkColor,
1452     HTMLDocument_get_linkColor,
1453     HTMLDocument_put_vlinkColor,
1454     HTMLDocument_get_vlinkColor,
1455     HTMLDocument_get_referrer,
1456     HTMLDocument_get_location,
1457     HTMLDocument_get_lastModified,
1458     HTMLDocument_put_URL,
1459     HTMLDocument_get_URL,
1460     HTMLDocument_put_domain,
1461     HTMLDocument_get_domain,
1462     HTMLDocument_put_cookie,
1463     HTMLDocument_get_cookie,
1464     HTMLDocument_put_expando,
1465     HTMLDocument_get_expando,
1466     HTMLDocument_put_charset,
1467     HTMLDocument_get_charset,
1468     HTMLDocument_put_defaultCharset,
1469     HTMLDocument_get_defaultCharset,
1470     HTMLDocument_get_mimeType,
1471     HTMLDocument_get_fileSize,
1472     HTMLDocument_get_fileCreatedDate,
1473     HTMLDocument_get_fileModifiedDate,
1474     HTMLDocument_get_fileUpdatedDate,
1475     HTMLDocument_get_security,
1476     HTMLDocument_get_protocol,
1477     HTMLDocument_get_nameProp,
1478     HTMLDocument_write,
1479     HTMLDocument_writeln,
1480     HTMLDocument_open,
1481     HTMLDocument_close,
1482     HTMLDocument_clear,
1483     HTMLDocument_queryCommandSupported,
1484     HTMLDocument_queryCommandEnabled,
1485     HTMLDocument_queryCommandState,
1486     HTMLDocument_queryCommandIndeterm,
1487     HTMLDocument_queryCommandText,
1488     HTMLDocument_queryCommandValue,
1489     HTMLDocument_execCommand,
1490     HTMLDocument_execCommandShowHelp,
1491     HTMLDocument_createElement,
1492     HTMLDocument_put_onhelp,
1493     HTMLDocument_get_onhelp,
1494     HTMLDocument_put_onclick,
1495     HTMLDocument_get_onclick,
1496     HTMLDocument_put_ondblclick,
1497     HTMLDocument_get_ondblclick,
1498     HTMLDocument_put_onkeyup,
1499     HTMLDocument_get_onkeyup,
1500     HTMLDocument_put_onkeydown,
1501     HTMLDocument_get_onkeydown,
1502     HTMLDocument_put_onkeypress,
1503     HTMLDocument_get_onkeypress,
1504     HTMLDocument_put_onmouseup,
1505     HTMLDocument_get_onmouseup,
1506     HTMLDocument_put_onmousedown,
1507     HTMLDocument_get_onmousedown,
1508     HTMLDocument_put_onmousemove,
1509     HTMLDocument_get_onmousemove,
1510     HTMLDocument_put_onmouseout,
1511     HTMLDocument_get_onmouseout,
1512     HTMLDocument_put_onmouseover,
1513     HTMLDocument_get_onmouseover,
1514     HTMLDocument_put_onreadystatechange,
1515     HTMLDocument_get_onreadystatechange,
1516     HTMLDocument_put_onafterupdate,
1517     HTMLDocument_get_onafterupdate,
1518     HTMLDocument_put_onrowexit,
1519     HTMLDocument_get_onrowexit,
1520     HTMLDocument_put_onrowenter,
1521     HTMLDocument_get_onrowenter,
1522     HTMLDocument_put_ondragstart,
1523     HTMLDocument_get_ondragstart,
1524     HTMLDocument_put_onselectstart,
1525     HTMLDocument_get_onselectstart,
1526     HTMLDocument_elementFromPoint,
1527     HTMLDocument_get_parentWindow,
1528     HTMLDocument_get_styleSheets,
1529     HTMLDocument_put_onbeforeupdate,
1530     HTMLDocument_get_onbeforeupdate,
1531     HTMLDocument_put_onerrorupdate,
1532     HTMLDocument_get_onerrorupdate,
1533     HTMLDocument_toString,
1534     HTMLDocument_createStyleSheet
1535 };
1536
1537 static void HTMLDocument_on_advise(IUnknown *iface, cp_static_data_t *cp)
1538 {
1539     HTMLDocument *This = impl_from_IHTMLDocument2((IHTMLDocument2*)iface);
1540
1541     if(This->window)
1542         update_cp_events(This->window, &This->doc_node->node.event_target, cp, This->doc_node->node.nsnode);
1543 }
1544
1545 static inline HTMLDocument *impl_from_ISupportErrorInfo(ISupportErrorInfo *iface)
1546 {
1547     return CONTAINING_RECORD(iface, HTMLDocument, ISupportErrorInfo_iface);
1548 }
1549
1550 static HRESULT WINAPI SupportErrorInfo_QueryInterface(ISupportErrorInfo *iface, REFIID riid, void **ppv)
1551 {
1552     HTMLDocument *This = impl_from_ISupportErrorInfo(iface);
1553     return htmldoc_query_interface(This, riid, ppv);
1554 }
1555
1556 static ULONG WINAPI SupportErrorInfo_AddRef(ISupportErrorInfo *iface)
1557 {
1558     HTMLDocument *This = impl_from_ISupportErrorInfo(iface);
1559     return htmldoc_addref(This);
1560 }
1561
1562 static ULONG WINAPI SupportErrorInfo_Release(ISupportErrorInfo *iface)
1563 {
1564     HTMLDocument *This = impl_from_ISupportErrorInfo(iface);
1565     return htmldoc_release(This);
1566 }
1567
1568 static HRESULT WINAPI SupportErrorInfo_InterfaceSupportsErrorInfo(ISupportErrorInfo *iface, REFIID riid)
1569 {
1570     FIXME("(%p)->(%s)\n", iface, debugstr_guid(riid));
1571     return S_FALSE;
1572 }
1573
1574 static const ISupportErrorInfoVtbl SupportErrorInfoVtbl = {
1575     SupportErrorInfo_QueryInterface,
1576     SupportErrorInfo_AddRef,
1577     SupportErrorInfo_Release,
1578     SupportErrorInfo_InterfaceSupportsErrorInfo
1579 };
1580
1581 static inline HTMLDocument *impl_from_IDispatchEx(IDispatchEx *iface)
1582 {
1583     return CONTAINING_RECORD(iface, HTMLDocument, IDispatchEx_iface);
1584 }
1585
1586 static HRESULT dispid_from_elem_name(HTMLDocumentNode *This, BSTR name, DISPID *dispid)
1587 {
1588     nsIDOMNodeList *node_list;
1589     nsAString name_str;
1590     PRUint32 len;
1591     unsigned i;
1592     nsresult nsres;
1593
1594     if(!This->nsdoc)
1595         return DISP_E_UNKNOWNNAME;
1596
1597     nsAString_InitDepend(&name_str, name);
1598     nsres = nsIDOMHTMLDocument_GetElementsByName(This->nsdoc, &name_str, &node_list);
1599     nsAString_Finish(&name_str);
1600     if(NS_FAILED(nsres))
1601         return E_FAIL;
1602
1603     nsres = nsIDOMNodeList_GetLength(node_list, &len);
1604     nsIDOMNodeList_Release(node_list);
1605     if(NS_FAILED(nsres))
1606         return E_FAIL;
1607
1608     if(!len)
1609         return DISP_E_UNKNOWNNAME;
1610
1611     for(i=0; i < This->elem_vars_cnt; i++) {
1612         if(!strcmpW(name, This->elem_vars[i])) {
1613             *dispid = MSHTML_DISPID_CUSTOM_MIN+i;
1614             return S_OK;
1615         }
1616     }
1617
1618     if(This->elem_vars_cnt == This->elem_vars_size) {
1619         WCHAR **new_vars;
1620
1621         if(This->elem_vars_size) {
1622             new_vars = heap_realloc(This->elem_vars, This->elem_vars_size*2*sizeof(WCHAR*));
1623             if(!new_vars)
1624                 return E_OUTOFMEMORY;
1625             This->elem_vars_size *= 2;
1626         }else {
1627             new_vars = heap_alloc(16*sizeof(WCHAR*));
1628             if(!new_vars)
1629                 return E_OUTOFMEMORY;
1630             This->elem_vars_size = 16;
1631         }
1632
1633         This->elem_vars = new_vars;
1634     }
1635
1636     This->elem_vars[This->elem_vars_cnt] = heap_strdupW(name);
1637     if(!This->elem_vars[This->elem_vars_cnt])
1638         return E_OUTOFMEMORY;
1639
1640     *dispid = MSHTML_DISPID_CUSTOM_MIN+This->elem_vars_cnt++;
1641     return S_OK;
1642 }
1643
1644 static HRESULT WINAPI DocDispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
1645 {
1646     HTMLDocument *This = impl_from_IDispatchEx(iface);
1647
1648     return htmldoc_query_interface(This, riid, ppv);
1649 }
1650
1651 static ULONG WINAPI DocDispatchEx_AddRef(IDispatchEx *iface)
1652 {
1653     HTMLDocument *This = impl_from_IDispatchEx(iface);
1654
1655     return htmldoc_addref(This);
1656 }
1657
1658 static ULONG WINAPI DocDispatchEx_Release(IDispatchEx *iface)
1659 {
1660     HTMLDocument *This = impl_from_IDispatchEx(iface);
1661
1662     return htmldoc_release(This);
1663 }
1664
1665 static HRESULT WINAPI DocDispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
1666 {
1667     HTMLDocument *This = impl_from_IDispatchEx(iface);
1668
1669     return IDispatchEx_GetTypeInfoCount(This->dispex, pctinfo);
1670 }
1671
1672 static HRESULT WINAPI DocDispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
1673                                                LCID lcid, ITypeInfo **ppTInfo)
1674 {
1675     HTMLDocument *This = impl_from_IDispatchEx(iface);
1676
1677     return IDispatchEx_GetTypeInfo(This->dispex, iTInfo, lcid, ppTInfo);
1678 }
1679
1680 static HRESULT WINAPI DocDispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
1681                                                  LPOLESTR *rgszNames, UINT cNames,
1682                                                  LCID lcid, DISPID *rgDispId)
1683 {
1684     HTMLDocument *This = impl_from_IDispatchEx(iface);
1685
1686     return IDispatchEx_GetIDsOfNames(This->dispex, riid, rgszNames, cNames, lcid, rgDispId);
1687 }
1688
1689 static HRESULT WINAPI DocDispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
1690                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1691                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1692 {
1693     HTMLDocument *This = impl_from_IDispatchEx(iface);
1694
1695     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1696           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1697
1698     switch(dispIdMember) {
1699     case DISPID_READYSTATE:
1700         TRACE("DISPID_READYSTATE\n");
1701
1702         if(!(wFlags & DISPATCH_PROPERTYGET))
1703             return E_INVALIDARG;
1704
1705         V_VT(pVarResult) = VT_I4;
1706         V_I4(pVarResult) = This->window->readystate;
1707         return S_OK;
1708     }
1709
1710     return IDispatchEx_Invoke(This->dispex, dispIdMember, riid, lcid, wFlags, pDispParams,
1711                               pVarResult, pExcepInfo, puArgErr);
1712 }
1713
1714 static HRESULT WINAPI DocDispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
1715 {
1716     HTMLDocument *This = impl_from_IDispatchEx(iface);
1717     HRESULT hres;
1718
1719     hres = IDispatchEx_GetDispID(This->dispex, bstrName, grfdex, pid);
1720     if(hres != DISP_E_UNKNOWNNAME)
1721         return hres;
1722
1723     return  dispid_from_elem_name(This->doc_node, bstrName, pid);
1724 }
1725
1726 static HRESULT WINAPI DocDispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
1727         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
1728 {
1729     HTMLDocument *This = impl_from_IDispatchEx(iface);
1730
1731     if(This->window && id == DISPID_IHTMLDOCUMENT2_LOCATION && (wFlags & DISPATCH_PROPERTYPUT))
1732         return IDispatchEx_InvokeEx(&This->window->IDispatchEx_iface, DISPID_IHTMLWINDOW2_LOCATION,
1733                 lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1734
1735
1736     return IDispatchEx_InvokeEx(This->dispex, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1737 }
1738
1739 static HRESULT WINAPI DocDispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
1740 {
1741     HTMLDocument *This = impl_from_IDispatchEx(iface);
1742
1743     return IDispatchEx_DeleteMemberByName(This->dispex, bstrName, grfdex);
1744 }
1745
1746 static HRESULT WINAPI DocDispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
1747 {
1748     HTMLDocument *This = impl_from_IDispatchEx(iface);
1749
1750     return IDispatchEx_DeleteMemberByDispID(This->dispex, id);
1751 }
1752
1753 static HRESULT WINAPI DocDispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
1754 {
1755     HTMLDocument *This = impl_from_IDispatchEx(iface);
1756
1757     return IDispatchEx_GetMemberProperties(This->dispex, id, grfdexFetch, pgrfdex);
1758 }
1759
1760 static HRESULT WINAPI DocDispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
1761 {
1762     HTMLDocument *This = impl_from_IDispatchEx(iface);
1763
1764     return IDispatchEx_GetMemberName(This->dispex, id, pbstrName);
1765 }
1766
1767 static HRESULT WINAPI DocDispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
1768 {
1769     HTMLDocument *This = impl_from_IDispatchEx(iface);
1770
1771     return IDispatchEx_GetNextDispID(This->dispex, grfdex, id, pid);
1772 }
1773
1774 static HRESULT WINAPI DocDispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1775 {
1776     HTMLDocument *This = impl_from_IDispatchEx(iface);
1777
1778     return IDispatchEx_GetNameSpaceParent(This->dispex, ppunk);
1779 }
1780
1781 static const IDispatchExVtbl DocDispatchExVtbl = {
1782     DocDispatchEx_QueryInterface,
1783     DocDispatchEx_AddRef,
1784     DocDispatchEx_Release,
1785     DocDispatchEx_GetTypeInfoCount,
1786     DocDispatchEx_GetTypeInfo,
1787     DocDispatchEx_GetIDsOfNames,
1788     DocDispatchEx_Invoke,
1789     DocDispatchEx_GetDispID,
1790     DocDispatchEx_InvokeEx,
1791     DocDispatchEx_DeleteMemberByName,
1792     DocDispatchEx_DeleteMemberByDispID,
1793     DocDispatchEx_GetMemberProperties,
1794     DocDispatchEx_GetMemberName,
1795     DocDispatchEx_GetNextDispID,
1796     DocDispatchEx_GetNameSpaceParent
1797 };
1798
1799 static inline HTMLDocument *impl_from_IProvideClassInfo(IProvideClassInfo *iface)
1800 {
1801     return CONTAINING_RECORD(iface, HTMLDocument, IProvideClassInfo_iface);
1802 }
1803
1804 static HRESULT WINAPI ProvideClassInfo_QueryInterface(IProvideClassInfo *iface,
1805         REFIID riid, void **ppv)
1806 {
1807     HTMLDocument *This = impl_from_IProvideClassInfo(iface);
1808     return htmldoc_query_interface(This, riid, ppv);
1809 }
1810
1811 static ULONG WINAPI ProvideClassInfo_AddRef(IProvideClassInfo *iface)
1812 {
1813     HTMLDocument *This = impl_from_IProvideClassInfo(iface);
1814     return htmldoc_addref(This);
1815 }
1816
1817 static ULONG WINAPI ProvideClassInfo_Release(IProvideClassInfo *iface)
1818 {
1819     HTMLDocument *This = impl_from_IProvideClassInfo(iface);
1820     return htmldoc_release(This);
1821 }
1822
1823 static HRESULT WINAPI ProvideClassInfo_GetClassInfo(IProvideClassInfo* iface,
1824         ITypeInfo **ppTI)
1825 {
1826     HTMLDocument *This = impl_from_IProvideClassInfo(iface);
1827     TRACE("(%p)->(%p)\n", This, ppTI);
1828     return get_htmldoc_classinfo(ppTI);
1829 }
1830
1831 static const IProvideClassInfoVtbl ProvideClassInfoVtbl = {
1832     ProvideClassInfo_QueryInterface,
1833     ProvideClassInfo_AddRef,
1834     ProvideClassInfo_Release,
1835     ProvideClassInfo_GetClassInfo
1836 };
1837
1838 static BOOL htmldoc_qi(HTMLDocument *This, REFIID riid, void **ppv)
1839 {
1840     *ppv = NULL;
1841
1842     if(IsEqualGUID(&IID_IUnknown, riid)) {
1843         TRACE("(%p)->(IID_IUnknown, %p)\n", This, ppv);
1844         *ppv = &This->IHTMLDocument2_iface;
1845     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
1846         TRACE("(%p)->(IID_IDispatch, %p)\n", This, ppv);
1847         *ppv = &This->IDispatchEx_iface;
1848     }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
1849         TRACE("(%p)->(IID_IDispatchEx, %p)\n", This, ppv);
1850         *ppv = &This->IDispatchEx_iface;
1851     }else if(IsEqualGUID(&IID_IHTMLDocument, riid)) {
1852         TRACE("(%p)->(IID_IHTMLDocument, %p)\n", This, ppv);
1853         *ppv = &This->IHTMLDocument2_iface;
1854     }else if(IsEqualGUID(&IID_IHTMLDocument2, riid)) {
1855         TRACE("(%p)->(IID_IHTMLDocument2, %p)\n", This, ppv);
1856         *ppv = &This->IHTMLDocument2_iface;
1857     }else if(IsEqualGUID(&IID_IHTMLDocument3, riid)) {
1858         TRACE("(%p)->(IID_IHTMLDocument3, %p)\n", This, ppv);
1859         *ppv = &This->IHTMLDocument3_iface;
1860     }else if(IsEqualGUID(&IID_IHTMLDocument4, riid)) {
1861         TRACE("(%p)->(IID_IHTMLDocument4, %p)\n", This, ppv);
1862         *ppv = &This->IHTMLDocument4_iface;
1863     }else if(IsEqualGUID(&IID_IHTMLDocument5, riid)) {
1864         TRACE("(%p)->(IID_IHTMLDocument5, %p)\n", This, ppv);
1865         *ppv = &This->IHTMLDocument5_iface;
1866     }else if(IsEqualGUID(&IID_IHTMLDocument6, riid)) {
1867         TRACE("(%p)->(IID_IHTMLDocument6, %p)\n", This, ppv);
1868         *ppv = &This->IHTMLDocument6_iface;
1869     }else if(IsEqualGUID(&IID_IPersist, riid)) {
1870         TRACE("(%p)->(IID_IPersist, %p)\n", This, ppv);
1871         *ppv = &This->IPersistFile_iface;
1872     }else if(IsEqualGUID(&IID_IPersistMoniker, riid)) {
1873         TRACE("(%p)->(IID_IPersistMoniker, %p)\n", This, ppv);
1874         *ppv = &This->IPersistMoniker_iface;
1875     }else if(IsEqualGUID(&IID_IPersistFile, riid)) {
1876         TRACE("(%p)->(IID_IPersistFile, %p)\n", This, ppv);
1877         *ppv = &This->IPersistFile_iface;
1878     }else if(IsEqualGUID(&IID_IMonikerProp, riid)) {
1879         TRACE("(%p)->(IID_IMonikerProp, %p)\n", This, ppv);
1880         *ppv = &This->IMonikerProp_iface;
1881     }else if(IsEqualGUID(&IID_IOleObject, riid)) {
1882         TRACE("(%p)->(IID_IOleObject, %p)\n", This, ppv);
1883         *ppv = &This->IOleObject_iface;
1884     }else if(IsEqualGUID(&IID_IOleDocument, riid)) {
1885         TRACE("(%p)->(IID_IOleDocument, %p)\n", This, ppv);
1886         *ppv = &This->IOleDocument_iface;
1887     }else if(IsEqualGUID(&IID_IOleDocumentView, riid)) {
1888         TRACE("(%p)->(IID_IOleDocumentView, %p)\n", This, ppv);
1889         *ppv = &This->IOleDocumentView_iface;
1890     }else if(IsEqualGUID(&IID_IOleInPlaceActiveObject, riid)) {
1891         TRACE("(%p)->(IID_IOleInPlaceActiveObject, %p)\n", This, ppv);
1892         *ppv = &This->IOleInPlaceActiveObject_iface;
1893     }else if(IsEqualGUID(&IID_IViewObject, riid)) {
1894         TRACE("(%p)->(IID_IViewObject, %p)\n", This, ppv);
1895         *ppv = &This->IViewObjectEx_iface;
1896     }else if(IsEqualGUID(&IID_IViewObject2, riid)) {
1897         TRACE("(%p)->(IID_IViewObject2, %p)\n", This, ppv);
1898         *ppv = &This->IViewObjectEx_iface;
1899     }else if(IsEqualGUID(&IID_IViewObjectEx, riid)) {
1900         TRACE("(%p)->(IID_IViewObjectEx, %p)\n", This, ppv);
1901         *ppv = &This->IViewObjectEx_iface;
1902     }else if(IsEqualGUID(&IID_IOleWindow, riid)) {
1903         TRACE("(%p)->(IID_IOleWindow, %p)\n", This, ppv);
1904         *ppv = &This->IOleInPlaceActiveObject_iface;
1905     }else if(IsEqualGUID(&IID_IOleInPlaceObject, riid)) {
1906         TRACE("(%p)->(IID_IOleInPlaceObject, %p)\n", This, ppv);
1907         *ppv = &This->IOleInPlaceObjectWindowless_iface;
1908     }else if(IsEqualGUID(&IID_IOleInPlaceObjectWindowless, riid)) {
1909         TRACE("(%p)->(IID_IOleInPlaceObjectWindowless, %p)\n", This, ppv);
1910         *ppv = &This->IOleInPlaceObjectWindowless_iface;
1911     }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
1912         TRACE("(%p)->(IID_IServiceProvider, %p)\n", This, ppv);
1913         *ppv = &This->IServiceProvider_iface;
1914     }else if(IsEqualGUID(&IID_IOleCommandTarget, riid)) {
1915         TRACE("(%p)->(IID_IOleCommandTarget, %p)\n", This, ppv);
1916         *ppv = &This->IOleCommandTarget_iface;
1917     }else if(IsEqualGUID(&IID_IOleControl, riid)) {
1918         TRACE("(%p)->(IID_IOleControl, %p)\n", This, ppv);
1919         *ppv = &This->IOleControl_iface;
1920     }else if(IsEqualGUID(&IID_IHlinkTarget, riid)) {
1921         TRACE("(%p)->(IID_IHlinkTarget, %p)\n", This, ppv);
1922         *ppv = &This->IHlinkTarget_iface;
1923     }else if(IsEqualGUID(&IID_IConnectionPointContainer, riid)) {
1924         TRACE("(%p)->(IID_IConnectionPointContainer %p)\n", This, ppv);
1925         *ppv = &This->cp_container.IConnectionPointContainer_iface;
1926     }else if(IsEqualGUID(&IID_IPersistStreamInit, riid)) {
1927         TRACE("(%p)->(IID_IPersistStreamInit %p)\n", This, ppv);
1928         *ppv = &This->IPersistStreamInit_iface;
1929     }else if(IsEqualGUID(&DIID_DispHTMLDocument, riid)) {
1930         TRACE("(%p)->(DIID_DispHTMLDocument %p)\n", This, ppv);
1931         *ppv = &This->IHTMLDocument2_iface;
1932     }else if(IsEqualGUID(&IID_ISupportErrorInfo, riid)) {
1933         TRACE("(%p)->(IID_ISupportErrorInfo %p)\n", This, ppv);
1934         *ppv = &This->ISupportErrorInfo_iface;
1935     }else if(IsEqualGUID(&IID_IPersistHistory, riid)) {
1936         TRACE("(%p)->(IID_IPersistHistory %p)\n", This, ppv);
1937         *ppv = &This->IPersistHistory_iface;
1938     }else if(IsEqualGUID(&CLSID_CMarkup, riid)) {
1939         FIXME("(%p)->(CLSID_CMarkup %p)\n", This, ppv);
1940         *ppv = NULL;
1941     }else if(IsEqualGUID(&IID_IRunnableObject, riid)) {
1942         TRACE("(%p)->(IID_IRunnableObject %p) returning NULL\n", This, ppv);
1943         *ppv = NULL;
1944     }else if(IsEqualGUID(&IID_IPersistPropertyBag, riid)) {
1945         TRACE("(%p)->(IID_IPersistPropertyBag %p) returning NULL\n", This, ppv);
1946         *ppv = NULL;
1947     }else if(IsEqualGUID(&IID_IMarshal, riid)) {
1948         TRACE("(%p)->(IID_IMarshal %p) returning NULL\n", This, ppv);
1949         *ppv = NULL;
1950     }else if(IsEqualGUID(&IID_IExternalConnection, riid)) {
1951         TRACE("(%p)->(IID_IExternalConnection %p) returning NULL\n", This, ppv);
1952         *ppv = NULL;
1953     }else if(IsEqualGUID(&IID_IStdMarshalInfo, riid)) {
1954         TRACE("(%p)->(IID_IStdMarshalInfo %p) returning NULL\n", This, ppv);
1955         *ppv = NULL;
1956     }else if(IsEqualGUID(&IID_IObjectWithSite, riid)) {
1957         TRACE("(%p)->(IID_IObjectWithSite %p)\n", This, ppv);
1958         *ppv = &This->IObjectWithSite_iface;
1959     }else if(IsEqualGUID(&IID_IOleContainer, riid)) {
1960         TRACE("(%p)->(IID_IOleContainer %p)\n", This, ppv);
1961         *ppv = &This->IOleContainer_iface;
1962     }else if(IsEqualGUID(&IID_IObjectSafety, riid)) {
1963         TRACE("(%p)->(IID_IObjectSafety %p)\n", This, ppv);
1964         *ppv = &This->IObjectSafety_iface;
1965     }else if(IsEqualGUID(&IID_IProvideClassInfo, riid)) {
1966         TRACE("(%p)->(IID_IProvideClassInfo, %p)\n", This, ppv);
1967         *ppv = &This->IProvideClassInfo_iface;
1968     }else {
1969         return FALSE;
1970     }
1971
1972     if(*ppv)
1973         IUnknown_AddRef((IUnknown*)*ppv);
1974     return TRUE;
1975 }
1976
1977 static cp_static_data_t HTMLDocumentEvents_data = { HTMLDocumentEvents_tid, HTMLDocument_on_advise };
1978
1979 static void init_doc(HTMLDocument *doc, IUnknown *unk_impl, IDispatchEx *dispex)
1980 {
1981     doc->IHTMLDocument2_iface.lpVtbl = &HTMLDocumentVtbl;
1982     doc->IDispatchEx_iface.lpVtbl = &DocDispatchExVtbl;
1983     doc->ISupportErrorInfo_iface.lpVtbl = &SupportErrorInfoVtbl;
1984     doc->IProvideClassInfo_iface.lpVtbl = &ProvideClassInfoVtbl;
1985
1986     doc->unk_impl = unk_impl;
1987     doc->dispex = dispex;
1988     doc->task_magic = get_task_target_magic();
1989
1990     HTMLDocument_HTMLDocument3_Init(doc);
1991     HTMLDocument_HTMLDocument5_Init(doc);
1992     HTMLDocument_Persist_Init(doc);
1993     HTMLDocument_OleCmd_Init(doc);
1994     HTMLDocument_OleObj_Init(doc);
1995     HTMLDocument_View_Init(doc);
1996     HTMLDocument_Window_Init(doc);
1997     HTMLDocument_Service_Init(doc);
1998     HTMLDocument_Hlink_Init(doc);
1999
2000     ConnectionPointContainer_Init(&doc->cp_container, (IUnknown*)&doc->IHTMLDocument2_iface);
2001     ConnectionPoint_Init(&doc->cp_dispatch, &doc->cp_container, &IID_IDispatch, &HTMLDocumentEvents_data);
2002     ConnectionPoint_Init(&doc->cp_propnotif, &doc->cp_container, &IID_IPropertyNotifySink, NULL);
2003     ConnectionPoint_Init(&doc->cp_htmldocevents, &doc->cp_container, &DIID_HTMLDocumentEvents, &HTMLDocumentEvents_data);
2004     ConnectionPoint_Init(&doc->cp_htmldocevents2, &doc->cp_container, &DIID_HTMLDocumentEvents2, NULL);
2005 }
2006
2007 static void destroy_htmldoc(HTMLDocument *This)
2008 {
2009     remove_target_tasks(This->task_magic);
2010
2011     ConnectionPointContainer_Destroy(&This->cp_container);
2012 }
2013
2014 static inline HTMLDocumentNode *impl_from_HTMLDOMNode(HTMLDOMNode *iface)
2015 {
2016     return CONTAINING_RECORD(iface, HTMLDocumentNode, node);
2017 }
2018
2019 static HRESULT HTMLDocumentNode_QI(HTMLDOMNode *iface, REFIID riid, void **ppv)
2020 {
2021     HTMLDocumentNode *This = impl_from_HTMLDOMNode(iface);
2022
2023     if(htmldoc_qi(&This->basedoc, riid, ppv))
2024         return *ppv ? S_OK : E_NOINTERFACE;
2025
2026     if(IsEqualGUID(&IID_IInternetHostSecurityManager, riid)) {
2027         TRACE("(%p)->(IID_IInternetHostSecurityManager %p)\n", This, ppv);
2028         *ppv = &This->IInternetHostSecurityManager_iface;
2029     }else {
2030         return HTMLDOMNode_QI(&This->node, riid, ppv);
2031     }
2032
2033     IUnknown_AddRef((IUnknown*)*ppv);
2034     return S_OK;
2035 }
2036
2037 static void HTMLDocumentNode_destructor(HTMLDOMNode *iface)
2038 {
2039     HTMLDocumentNode *This = impl_from_HTMLDOMNode(iface);
2040     unsigned i;
2041
2042     for(i=0; i < This->elem_vars_cnt; i++)
2043         heap_free(This->elem_vars[i]);
2044     heap_free(This->elem_vars);
2045
2046     detach_events(This);
2047     if(This->body_event_target)
2048         release_event_target(This->body_event_target);
2049     if(This->catmgr)
2050         ICatInformation_Release(This->catmgr);
2051
2052     detach_selection(This);
2053     detach_ranges(This);
2054     release_nodes(This);
2055
2056     while(!list_empty(&This->plugin_hosts))
2057         detach_plugin_host(LIST_ENTRY(list_head(&This->plugin_hosts), PluginHost, entry));
2058
2059     if(This->nsdoc) {
2060         release_document_mutation(This);
2061         nsIDOMHTMLDocument_Release(This->nsdoc);
2062     }
2063
2064     heap_free(This->event_vector);
2065     destroy_htmldoc(&This->basedoc);
2066 }
2067
2068 static HRESULT HTMLDocumentNode_clone(HTMLDOMNode *iface, nsIDOMNode *nsnode, HTMLDOMNode **ret)
2069 {
2070     HTMLDocumentNode *This = impl_from_HTMLDOMNode(iface);
2071     FIXME("%p\n", This);
2072     return E_NOTIMPL;
2073 }
2074
2075 static const NodeImplVtbl HTMLDocumentNodeImplVtbl = {
2076     HTMLDocumentNode_QI,
2077     HTMLDocumentNode_destructor,
2078     HTMLDocumentNode_clone
2079 };
2080
2081 static HRESULT HTMLDocumentFragment_clone(HTMLDOMNode *iface, nsIDOMNode *nsnode, HTMLDOMNode **ret)
2082 {
2083     HTMLDocumentNode *This = impl_from_HTMLDOMNode(iface);
2084     HTMLDocumentNode *new_node;
2085     HRESULT hres;
2086
2087     hres = create_document_fragment(nsnode, This->node.doc, &new_node);
2088     if(FAILED(hres))
2089         return hres;
2090
2091     *ret = &new_node->node;
2092     return S_OK;
2093 }
2094
2095 static inline HTMLDocumentNode *impl_from_DispatchEx(DispatchEx *iface)
2096 {
2097     return CONTAINING_RECORD(iface, HTMLDocumentNode, node.dispex);
2098 }
2099
2100 static HRESULT HTMLDocumentNode_invoke(DispatchEx *dispex, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
2101         VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
2102 {
2103     HTMLDocumentNode *This = impl_from_DispatchEx(dispex);
2104     nsIDOMNodeList *node_list;
2105     nsAString name_str;
2106     nsIDOMNode *nsnode;
2107     HTMLDOMNode *node;
2108     unsigned i;
2109     nsresult nsres;
2110     HRESULT hres;
2111
2112     if(flags != DISPATCH_PROPERTYGET) {
2113         FIXME("unsupported flags %x\n", flags);
2114         return E_NOTIMPL;
2115     }
2116
2117     i = id - MSHTML_DISPID_CUSTOM_MIN;
2118
2119     if(!This->nsdoc || i >= This->elem_vars_cnt)
2120         return DISP_E_UNKNOWNNAME;
2121
2122     nsAString_InitDepend(&name_str, This->elem_vars[i]);
2123     nsres = nsIDOMHTMLDocument_GetElementsByName(This->nsdoc, &name_str, &node_list);
2124     nsAString_Finish(&name_str);
2125     if(NS_FAILED(nsres))
2126         return E_FAIL;
2127
2128     nsres = nsIDOMNodeList_Item(node_list, 0, &nsnode);
2129     nsIDOMNodeList_Release(node_list);
2130     if(NS_FAILED(nsres) || !nsnode)
2131         return DISP_E_UNKNOWNNAME;
2132
2133     hres = get_node(This, nsnode, TRUE, &node);
2134     if(FAILED(hres))
2135         return hres;
2136
2137     IHTMLDOMNode_AddRef(&node->IHTMLDOMNode_iface);
2138     V_VT(res) = VT_DISPATCH;
2139     V_DISPATCH(res) = (IDispatch*)&node->IHTMLDOMNode_iface;
2140     return S_OK;
2141 }
2142
2143
2144 static const dispex_static_data_vtbl_t HTMLDocumentNode_dispex_vtbl = {
2145     NULL,
2146     NULL,
2147     HTMLDocumentNode_invoke,
2148     NULL
2149 };
2150
2151 static const NodeImplVtbl HTMLDocumentFragmentImplVtbl = {
2152     HTMLDocumentNode_QI,
2153     HTMLDocumentNode_destructor,
2154     HTMLDocumentFragment_clone
2155 };
2156
2157 static const tid_t HTMLDocumentNode_iface_tids[] = {
2158     IHTMLDOMNode_tid,
2159     IHTMLDOMNode2_tid,
2160     IHTMLDocument2_tid,
2161     IHTMLDocument3_tid,
2162     IHTMLDocument4_tid,
2163     IHTMLDocument5_tid,
2164     0
2165 };
2166
2167 static dispex_static_data_t HTMLDocumentNode_dispex = {
2168     &HTMLDocumentNode_dispex_vtbl,
2169     DispHTMLDocument_tid,
2170     NULL,
2171     HTMLDocumentNode_iface_tids
2172 };
2173
2174 static HTMLDocumentNode *alloc_doc_node(HTMLDocumentObj *doc_obj, HTMLWindow *window)
2175 {
2176     HTMLDocumentNode *doc;
2177
2178     doc = heap_alloc_zero(sizeof(HTMLDocumentNode));
2179     if(!doc)
2180         return NULL;
2181
2182     doc->ref = 1;
2183     doc->basedoc.doc_node = doc;
2184     doc->basedoc.doc_obj = doc_obj;
2185     doc->basedoc.window = window;
2186
2187     init_dispex(&doc->node.dispex, (IUnknown*)&doc->node.IHTMLDOMNode_iface,
2188             &HTMLDocumentNode_dispex);
2189     init_doc(&doc->basedoc, (IUnknown*)&doc->node.IHTMLDOMNode_iface,
2190             &doc->node.dispex.IDispatchEx_iface);
2191     HTMLDocumentNode_SecMgr_Init(doc);
2192
2193     list_init(&doc->bindings);
2194     list_init(&doc->selection_list);
2195     list_init(&doc->range_list);
2196     list_init(&doc->plugin_hosts);
2197
2198     return doc;
2199 }
2200
2201 HRESULT create_doc_from_nsdoc(nsIDOMHTMLDocument *nsdoc, HTMLDocumentObj *doc_obj, HTMLWindow *window, HTMLDocumentNode **ret)
2202 {
2203     HTMLDocumentNode *doc;
2204
2205     doc = alloc_doc_node(doc_obj, window);
2206     if(!doc)
2207         return E_OUTOFMEMORY;
2208
2209     if(!doc_obj->basedoc.window || window == doc_obj->basedoc.window)
2210         doc->basedoc.cp_container.forward_container = &doc_obj->basedoc.cp_container;
2211
2212     nsIDOMHTMLDocument_AddRef(nsdoc);
2213     doc->nsdoc = nsdoc;
2214     init_document_mutation(doc);
2215     doc_init_events(doc);
2216
2217     HTMLDOMNode_Init(doc, &doc->node, (nsIDOMNode*)nsdoc);
2218     doc->node.vtbl = &HTMLDocumentNodeImplVtbl;
2219     doc->node.cp_container = &doc->basedoc.cp_container;
2220
2221     *ret = doc;
2222     return S_OK;
2223 }
2224
2225 HRESULT create_document_fragment(nsIDOMNode *nsnode, HTMLDocumentNode *doc_node, HTMLDocumentNode **ret)
2226 {
2227     HTMLDocumentNode *doc_frag;
2228
2229     doc_frag = alloc_doc_node(doc_node->basedoc.doc_obj, doc_node->basedoc.window);
2230     if(!doc_frag)
2231         return E_OUTOFMEMORY;
2232
2233     HTMLDOMNode_Init(doc_node, &doc_frag->node, nsnode);
2234     doc_frag->node.vtbl = &HTMLDocumentFragmentImplVtbl;
2235     doc_frag->node.cp_container = &doc_frag->basedoc.cp_container;
2236
2237     htmldoc_addref(&doc_frag->basedoc);
2238     *ret = doc_frag;
2239     return S_OK;
2240 }
2241
2242 /**********************************************************
2243  * ICustomDoc implementation
2244  */
2245
2246 static inline HTMLDocumentObj *impl_from_ICustomDoc(ICustomDoc *iface)
2247 {
2248     return CONTAINING_RECORD(iface, HTMLDocumentObj, ICustomDoc_iface);
2249 }
2250
2251 static HRESULT WINAPI CustomDoc_QueryInterface(ICustomDoc *iface, REFIID riid, void **ppv)
2252 {
2253     HTMLDocumentObj *This = impl_from_ICustomDoc(iface);
2254
2255     if(htmldoc_qi(&This->basedoc, riid, ppv))
2256         return *ppv ? S_OK : E_NOINTERFACE;
2257
2258     if(IsEqualGUID(&IID_ICustomDoc, riid)) {
2259         TRACE("(%p)->(IID_ICustomDoc %p)\n", This, ppv);
2260         *ppv = &This->ICustomDoc_iface;
2261     }else if(IsEqualGUID(&IID_ITargetContainer, riid)) {
2262         TRACE("(%p)->(IID_ITargetContainer %p)\n", This, ppv);
2263         *ppv = &This->ITargetContainer_iface;
2264     }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
2265         return *ppv ? S_OK : E_NOINTERFACE;
2266     }else {
2267         FIXME("Unimplemented interface %s\n", debugstr_guid(riid));
2268         *ppv = NULL;
2269         return E_NOINTERFACE;
2270     }
2271
2272     IUnknown_AddRef((IUnknown*)*ppv);
2273     return S_OK;
2274 }
2275
2276 static ULONG WINAPI CustomDoc_AddRef(ICustomDoc *iface)
2277 {
2278     HTMLDocumentObj *This = impl_from_ICustomDoc(iface);
2279     ULONG ref = InterlockedIncrement(&This->ref);
2280
2281     TRACE("(%p) ref = %u\n", This, ref);
2282
2283     return ref;
2284 }
2285
2286 static ULONG WINAPI CustomDoc_Release(ICustomDoc *iface)
2287 {
2288     HTMLDocumentObj *This = impl_from_ICustomDoc(iface);
2289     ULONG ref = InterlockedDecrement(&This->ref);
2290
2291     TRACE("(%p) ref = %u\n", This, ref);
2292
2293     if(!ref) {
2294         if(This->basedoc.doc_node) {
2295             This->basedoc.doc_node->basedoc.doc_obj = NULL;
2296             htmldoc_release(&This->basedoc.doc_node->basedoc);
2297         }
2298         if(This->basedoc.window) {
2299             This->basedoc.window->doc_obj = NULL;
2300             IHTMLWindow2_Release(&This->basedoc.window->IHTMLWindow2_iface);
2301         }
2302         if(This->basedoc.advise_holder)
2303             IOleAdviseHolder_Release(This->basedoc.advise_holder);
2304
2305         if(This->view_sink)
2306             IAdviseSink_Release(This->view_sink);
2307         if(This->client)
2308             IOleObject_SetClientSite(&This->basedoc.IOleObject_iface, NULL);
2309         if(This->hostui)
2310             ICustomDoc_SetUIHandler(&This->ICustomDoc_iface, NULL);
2311         if(This->in_place_active)
2312             IOleInPlaceObjectWindowless_InPlaceDeactivate(&This->basedoc.IOleInPlaceObjectWindowless_iface);
2313         if(This->ipsite)
2314             IOleDocumentView_SetInPlaceSite(&This->basedoc.IOleDocumentView_iface, NULL);
2315         if(This->undomgr)
2316             IOleUndoManager_Release(This->undomgr);
2317         if(This->tooltips_hwnd)
2318             DestroyWindow(This->tooltips_hwnd);
2319
2320         if(This->hwnd)
2321             DestroyWindow(This->hwnd);
2322         heap_free(This->mime);
2323
2324         destroy_htmldoc(&This->basedoc);
2325         release_dispex(&This->dispex);
2326
2327         if(This->nscontainer)
2328             NSContainer_Release(This->nscontainer);
2329         heap_free(This);
2330     }
2331
2332     return ref;
2333 }
2334
2335 static HRESULT WINAPI CustomDoc_SetUIHandler(ICustomDoc *iface, IDocHostUIHandler *pUIHandler)
2336 {
2337     HTMLDocumentObj *This = impl_from_ICustomDoc(iface);
2338     IOleCommandTarget *cmdtrg;
2339     HRESULT hres;
2340
2341     TRACE("(%p)->(%p)\n", This, pUIHandler);
2342
2343     if(This->custom_hostui && This->hostui == pUIHandler)
2344         return S_OK;
2345
2346     This->custom_hostui = TRUE;
2347
2348     if(This->hostui)
2349         IDocHostUIHandler_Release(This->hostui);
2350     if(pUIHandler)
2351         IDocHostUIHandler_AddRef(pUIHandler);
2352     This->hostui = pUIHandler;
2353     if(!pUIHandler)
2354         return S_OK;
2355
2356     hres = IDocHostUIHandler_QueryInterface(pUIHandler, &IID_IOleCommandTarget, (void**)&cmdtrg);
2357     if(SUCCEEDED(hres)) {
2358         FIXME("custom UI handler supports IOleCommandTarget\n");
2359         IOleCommandTarget_Release(cmdtrg);
2360     }
2361
2362     return S_OK;
2363 }
2364
2365 static const ICustomDocVtbl CustomDocVtbl = {
2366     CustomDoc_QueryInterface,
2367     CustomDoc_AddRef,
2368     CustomDoc_Release,
2369     CustomDoc_SetUIHandler
2370 };
2371
2372 static const tid_t HTMLDocumentObj_iface_tids[] = {
2373     IHTMLDocument2_tid,
2374     IHTMLDocument3_tid,
2375     IHTMLDocument4_tid,
2376     IHTMLDocument5_tid,
2377     0
2378 };
2379 static dispex_static_data_t HTMLDocumentObj_dispex = {
2380     NULL,
2381     DispHTMLDocument_tid,
2382     NULL,
2383     HTMLDocumentObj_iface_tids
2384 };
2385
2386 HRESULT HTMLDocument_Create(IUnknown *pUnkOuter, REFIID riid, void** ppvObject)
2387 {
2388     HTMLDocumentObj *doc;
2389     nsIDOMWindow *nswindow = NULL;
2390     nsresult nsres;
2391     HRESULT hres;
2392
2393     TRACE("(%p %s %p)\n", pUnkOuter, debugstr_guid(riid), ppvObject);
2394
2395     doc = heap_alloc_zero(sizeof(HTMLDocumentObj));
2396     if(!doc)
2397         return E_OUTOFMEMORY;
2398
2399     init_dispex(&doc->dispex, (IUnknown*)&doc->ICustomDoc_iface, &HTMLDocumentObj_dispex);
2400     init_doc(&doc->basedoc, (IUnknown*)&doc->ICustomDoc_iface, &doc->dispex.IDispatchEx_iface);
2401     TargetContainer_Init(doc);
2402
2403     doc->ICustomDoc_iface.lpVtbl = &CustomDocVtbl;
2404     doc->ref = 1;
2405     doc->basedoc.doc_obj = doc;
2406
2407     doc->usermode = UNKNOWN_USERMODE;
2408
2409     hres = create_nscontainer(doc, NULL, &doc->nscontainer);
2410     if(FAILED(hres)) {
2411         ERR("Failed to init Gecko, returning CLASS_E_CLASSNOTAVAILABLE\n");
2412         htmldoc_release(&doc->basedoc);
2413         return hres;
2414     }
2415
2416     hres = htmldoc_query_interface(&doc->basedoc, riid, ppvObject);
2417     htmldoc_release(&doc->basedoc);
2418     if(FAILED(hres))
2419         return hres;
2420
2421
2422     nsres = nsIWebBrowser_GetContentDOMWindow(doc->nscontainer->webbrowser, &nswindow);
2423     if(NS_FAILED(nsres))
2424         ERR("GetContentDOMWindow failed: %08x\n", nsres);
2425
2426     hres = HTMLWindow_Create(doc, nswindow, NULL /* FIXME */, &doc->basedoc.window);
2427     if(nswindow)
2428         nsIDOMWindow_Release(nswindow);
2429     if(FAILED(hres)) {
2430         htmldoc_release(&doc->basedoc);
2431         return hres;
2432     }
2433
2434     if(!doc->basedoc.doc_node && doc->basedoc.window->doc) {
2435         doc->basedoc.doc_node = doc->basedoc.window->doc;
2436         htmldoc_addref(&doc->basedoc.doc_node->basedoc);
2437     }
2438
2439     get_thread_hwnd();
2440
2441     return S_OK;
2442 }