mshtml: Added IWindowForBindingUI stub implementation.
[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
599     TRACE("(%p)->(%s)\n", This, debugstr_w(v));
600
601     if(!This->window) {
602         FIXME("No window available\n");
603         return E_FAIL;
604     }
605
606     return navigate_url(This->window, v, This->window->url);
607 }
608
609 static HRESULT WINAPI HTMLDocument_get_URL(IHTMLDocument2 *iface, BSTR *p)
610 {
611     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
612
613     static const WCHAR about_blank_url[] =
614         {'a','b','o','u','t',':','b','l','a','n','k',0};
615
616     TRACE("(%p)->(%p)\n", iface, p);
617
618     *p = SysAllocString(This->window->url ? This->window->url : about_blank_url);
619     return *p ? S_OK : E_OUTOFMEMORY;
620 }
621
622 static HRESULT WINAPI HTMLDocument_put_domain(IHTMLDocument2 *iface, BSTR v)
623 {
624     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
625     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
626     return E_NOTIMPL;
627 }
628
629 static HRESULT WINAPI HTMLDocument_get_domain(IHTMLDocument2 *iface, BSTR *p)
630 {
631     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
632     HRESULT hres;
633
634     TRACE("(%p)->(%p)\n", This, p);
635
636     if(!This->window || !This->window->uri) {
637         FIXME("No current URI\n");
638         return E_FAIL;
639     }
640
641     hres = IUri_GetHost(This->window->uri, p);
642     return FAILED(hres) ? hres : S_OK;
643 }
644
645 static HRESULT WINAPI HTMLDocument_put_cookie(IHTMLDocument2 *iface, BSTR v)
646 {
647     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
648     BOOL bret;
649
650     TRACE("(%p)->(%s)\n", This, debugstr_w(v));
651
652     bret = InternetSetCookieExW(This->window->url, NULL, v, 0, 0);
653     if(!bret) {
654         FIXME("InternetSetCookieExW failed: %u\n", GetLastError());
655         return HRESULT_FROM_WIN32(GetLastError());
656     }
657
658     return S_OK;
659 }
660
661 static HRESULT WINAPI HTMLDocument_get_cookie(IHTMLDocument2 *iface, BSTR *p)
662 {
663     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
664     DWORD size;
665     BOOL bret;
666
667     TRACE("(%p)->(%p)\n", This, p);
668
669     size = 0;
670     bret = InternetGetCookieExW(This->window->url, NULL, NULL, &size, 0, NULL);
671     if(!bret) {
672         switch(GetLastError()) {
673         case ERROR_INSUFFICIENT_BUFFER:
674             break;
675         case ERROR_NO_MORE_ITEMS:
676             *p = NULL;
677             return S_OK;
678         default:
679             FIXME("InternetGetCookieExW failed: %u\n", GetLastError());
680             return HRESULT_FROM_WIN32(GetLastError());
681         }
682     }
683
684     if(!size) {
685         *p = NULL;
686         return S_OK;
687     }
688
689     *p = SysAllocStringLen(NULL, size-1);
690     if(!*p)
691         return E_OUTOFMEMORY;
692
693     bret = InternetGetCookieExW(This->window->url, NULL, *p, &size, 0, NULL);
694     if(!bret) {
695         ERR("InternetGetCookieExW failed: %u\n", GetLastError());
696         return E_FAIL;
697     }
698
699     return S_OK;
700 }
701
702 static HRESULT WINAPI HTMLDocument_put_expando(IHTMLDocument2 *iface, VARIANT_BOOL v)
703 {
704     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
705     FIXME("(%p)->(%x)\n", This, v);
706     return E_NOTIMPL;
707 }
708
709 static HRESULT WINAPI HTMLDocument_get_expando(IHTMLDocument2 *iface, VARIANT_BOOL *p)
710 {
711     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
712     FIXME("(%p)->(%p)\n", This, p);
713     return E_NOTIMPL;
714 }
715
716 static HRESULT WINAPI HTMLDocument_put_charset(IHTMLDocument2 *iface, BSTR v)
717 {
718     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
719     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
720     return E_NOTIMPL;
721 }
722
723 static HRESULT WINAPI HTMLDocument_get_charset(IHTMLDocument2 *iface, BSTR *p)
724 {
725     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
726     FIXME("(%p)->(%p)\n", This, p);
727     return E_NOTIMPL;
728 }
729
730 static HRESULT WINAPI HTMLDocument_put_defaultCharset(IHTMLDocument2 *iface, BSTR v)
731 {
732     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
733     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
734     return E_NOTIMPL;
735 }
736
737 static HRESULT WINAPI HTMLDocument_get_defaultCharset(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_mimeType(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_fileSize(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_fileCreatedDate(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_fileModifiedDate(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_fileUpdatedDate(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_security(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_protocol(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 WINAPI HTMLDocument_get_nameProp(IHTMLDocument2 *iface, BSTR *p)
794 {
795     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
796     FIXME("(%p)->(%p)\n", This, p);
797     return E_NOTIMPL;
798 }
799
800 static HRESULT document_write(HTMLDocument *This, SAFEARRAY *psarray, BOOL ln)
801 {
802     VARIANT *var, tmp;
803     nsAString nsstr;
804     ULONG i, argc;
805     nsresult nsres;
806     HRESULT hres;
807
808     if(!This->doc_node->nsdoc) {
809         WARN("NULL nsdoc\n");
810         return E_UNEXPECTED;
811     }
812
813     if (!psarray)
814         return S_OK;
815
816     if(psarray->cDims != 1) {
817         FIXME("cDims=%d\n", psarray->cDims);
818         return E_INVALIDARG;
819     }
820
821     hres = SafeArrayAccessData(psarray, (void**)&var);
822     if(FAILED(hres)) {
823         WARN("SafeArrayAccessData failed: %08x\n", hres);
824         return hres;
825     }
826
827     V_VT(&tmp) = VT_EMPTY;
828
829     argc = psarray->rgsabound[0].cElements;
830     for(i=0; i < argc; i++) {
831         if(V_VT(var+i) == VT_BSTR) {
832             nsAString_InitDepend(&nsstr, V_BSTR(var+i));
833         }else {
834             hres = VariantChangeTypeEx(&tmp, var+i, MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT), 0, VT_BSTR);
835             if(FAILED(hres)) {
836                 WARN("Could not convert %s to string\n", debugstr_variant(var+i));
837                 break;
838             }
839             nsAString_InitDepend(&nsstr, V_BSTR(&tmp));
840         }
841
842         if(!ln || i != argc-1)
843             nsres = nsIDOMHTMLDocument_Write(This->doc_node->nsdoc, &nsstr, NULL /* FIXME! */);
844         else
845             nsres = nsIDOMHTMLDocument_Writeln(This->doc_node->nsdoc, &nsstr, NULL /* FIXME! */);
846         nsAString_Finish(&nsstr);
847         if(V_VT(var+i) != VT_BSTR)
848             VariantClear(&tmp);
849         if(NS_FAILED(nsres)) {
850             ERR("Write failed: %08x\n", nsres);
851             hres = E_FAIL;
852             break;
853         }
854     }
855
856     SafeArrayUnaccessData(psarray);
857
858     return hres;
859 }
860
861 static HRESULT WINAPI HTMLDocument_write(IHTMLDocument2 *iface, SAFEARRAY *psarray)
862 {
863     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
864
865     TRACE("(%p)->(%p)\n", iface, psarray);
866
867     return document_write(This, psarray, FALSE);
868 }
869
870 static HRESULT WINAPI HTMLDocument_writeln(IHTMLDocument2 *iface, SAFEARRAY *psarray)
871 {
872     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
873
874     TRACE("(%p)->(%p)\n", This, psarray);
875
876     return document_write(This, psarray, TRUE);
877 }
878
879 static HRESULT WINAPI HTMLDocument_open(IHTMLDocument2 *iface, BSTR url, VARIANT name,
880                         VARIANT features, VARIANT replace, IDispatch **pomWindowResult)
881 {
882     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
883     nsISupports *tmp;
884     nsresult nsres;
885
886     static const WCHAR text_htmlW[] = {'t','e','x','t','/','h','t','m','l',0};
887
888     TRACE("(%p)->(%s %s %s %s %p)\n", This, debugstr_w(url), debugstr_variant(&name),
889           debugstr_variant(&features), debugstr_variant(&replace), pomWindowResult);
890
891     if(!This->doc_node->nsdoc) {
892         ERR("!nsdoc\n");
893         return E_NOTIMPL;
894     }
895
896     if(!url || strcmpW(url, text_htmlW) || V_VT(&name) != VT_ERROR
897        || V_VT(&features) != VT_ERROR || V_VT(&replace) != VT_ERROR)
898         FIXME("unsupported args\n");
899
900     nsres = nsIDOMHTMLDocument_Open(This->doc_node->nsdoc, NULL, NULL, NULL, NULL, 0, &tmp);
901     if(NS_FAILED(nsres)) {
902         ERR("Open failed: %08x\n", nsres);
903         return E_FAIL;
904     }
905
906     if(tmp)
907         nsISupports_Release(tmp);
908
909     *pomWindowResult = (IDispatch*)&This->window->IHTMLWindow2_iface;
910     IHTMLWindow2_AddRef(&This->window->IHTMLWindow2_iface);
911     return S_OK;
912 }
913
914 static HRESULT WINAPI HTMLDocument_close(IHTMLDocument2 *iface)
915 {
916     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
917     nsresult nsres;
918
919     TRACE("(%p)\n", This);
920
921     if(!This->doc_node->nsdoc) {
922         ERR("!nsdoc\n");
923         return E_NOTIMPL;
924     }
925
926     nsres = nsIDOMHTMLDocument_Close(This->doc_node->nsdoc);
927     if(NS_FAILED(nsres)) {
928         ERR("Close failed: %08x\n", nsres);
929         return E_FAIL;
930     }
931
932     return S_OK;
933 }
934
935 static HRESULT WINAPI HTMLDocument_clear(IHTMLDocument2 *iface)
936 {
937     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
938     nsresult nsres;
939
940     TRACE("(%p)\n", This);
941
942     nsres = nsIDOMHTMLDocument_Clear(This->doc_node->nsdoc);
943     if(NS_FAILED(nsres)) {
944         ERR("Clear failed: %08x\n", nsres);
945         return E_FAIL;
946     }
947
948     return S_OK;
949 }
950
951 static HRESULT WINAPI HTMLDocument_queryCommandSupported(IHTMLDocument2 *iface, BSTR cmdID,
952                                                         VARIANT_BOOL *pfRet)
953 {
954     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
955     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
956     return E_NOTIMPL;
957 }
958
959 static HRESULT WINAPI HTMLDocument_queryCommandEnabled(IHTMLDocument2 *iface, BSTR cmdID,
960                                                         VARIANT_BOOL *pfRet)
961 {
962     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
963     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
964     return E_NOTIMPL;
965 }
966
967 static HRESULT WINAPI HTMLDocument_queryCommandState(IHTMLDocument2 *iface, BSTR cmdID,
968                                                         VARIANT_BOOL *pfRet)
969 {
970     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
971     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
972     return E_NOTIMPL;
973 }
974
975 static HRESULT WINAPI HTMLDocument_queryCommandIndeterm(IHTMLDocument2 *iface, BSTR cmdID,
976                                                         VARIANT_BOOL *pfRet)
977 {
978     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
979     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
980     return E_NOTIMPL;
981 }
982
983 static HRESULT WINAPI HTMLDocument_queryCommandText(IHTMLDocument2 *iface, BSTR cmdID,
984                                                         BSTR *pfRet)
985 {
986     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
987     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
988     return E_NOTIMPL;
989 }
990
991 static HRESULT WINAPI HTMLDocument_queryCommandValue(IHTMLDocument2 *iface, BSTR cmdID,
992                                                         VARIANT *pfRet)
993 {
994     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
995     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
996     return E_NOTIMPL;
997 }
998
999 static HRESULT WINAPI HTMLDocument_execCommand(IHTMLDocument2 *iface, BSTR cmdID,
1000                                 VARIANT_BOOL showUI, VARIANT value, VARIANT_BOOL *pfRet)
1001 {
1002     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1003     FIXME("(%p)->(%s %x %s %p)\n", This, debugstr_w(cmdID), showUI, debugstr_variant(&value), pfRet);
1004     return E_NOTIMPL;
1005 }
1006
1007 static HRESULT WINAPI HTMLDocument_execCommandShowHelp(IHTMLDocument2 *iface, BSTR cmdID,
1008                                                         VARIANT_BOOL *pfRet)
1009 {
1010     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1011     FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
1012     return E_NOTIMPL;
1013 }
1014
1015 static HRESULT WINAPI HTMLDocument_createElement(IHTMLDocument2 *iface, BSTR eTag,
1016                                                  IHTMLElement **newElem)
1017 {
1018     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1019     nsIDOMHTMLElement *nselem;
1020     HTMLElement *elem;
1021     HRESULT hres;
1022
1023     TRACE("(%p)->(%s %p)\n", This, debugstr_w(eTag), newElem);
1024
1025     hres = create_nselem(This->doc_node, eTag, &nselem);
1026     if(FAILED(hres))
1027         return hres;
1028
1029     hres = HTMLElement_Create(This->doc_node, (nsIDOMNode*)nselem, TRUE, &elem);
1030     nsIDOMHTMLElement_Release(nselem);
1031     if(FAILED(hres))
1032         return hres;
1033
1034     *newElem = &elem->IHTMLElement_iface;
1035     IHTMLElement_AddRef(&elem->IHTMLElement_iface);
1036     return S_OK;
1037 }
1038
1039 static HRESULT WINAPI HTMLDocument_put_onhelp(IHTMLDocument2 *iface, VARIANT v)
1040 {
1041     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1042     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1043     return E_NOTIMPL;
1044 }
1045
1046 static HRESULT WINAPI HTMLDocument_get_onhelp(IHTMLDocument2 *iface, VARIANT *p)
1047 {
1048     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1049     FIXME("(%p)->(%p)\n", This, p);
1050     return E_NOTIMPL;
1051 }
1052
1053 static HRESULT WINAPI HTMLDocument_put_onclick(IHTMLDocument2 *iface, VARIANT v)
1054 {
1055     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1056
1057     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1058
1059     return set_doc_event(This, EVENTID_CLICK, &v);
1060 }
1061
1062 static HRESULT WINAPI HTMLDocument_get_onclick(IHTMLDocument2 *iface, VARIANT *p)
1063 {
1064     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1065
1066     TRACE("(%p)->(%p)\n", This, p);
1067
1068     return get_doc_event(This, EVENTID_CLICK, p);
1069 }
1070
1071 static HRESULT WINAPI HTMLDocument_put_ondblclick(IHTMLDocument2 *iface, VARIANT v)
1072 {
1073     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1074     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1075     return E_NOTIMPL;
1076 }
1077
1078 static HRESULT WINAPI HTMLDocument_get_ondblclick(IHTMLDocument2 *iface, VARIANT *p)
1079 {
1080     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1081     FIXME("(%p)->(%p)\n", This, p);
1082     return E_NOTIMPL;
1083 }
1084
1085 static HRESULT WINAPI HTMLDocument_put_onkeyup(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_KEYUP, &v);
1092 }
1093
1094 static HRESULT WINAPI HTMLDocument_get_onkeyup(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_KEYUP, p);
1101 }
1102
1103 static HRESULT WINAPI HTMLDocument_put_onkeydown(IHTMLDocument2 *iface, VARIANT v)
1104 {
1105     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1106
1107     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1108
1109     return set_doc_event(This, EVENTID_KEYDOWN, &v);
1110 }
1111
1112 static HRESULT WINAPI HTMLDocument_get_onkeydown(IHTMLDocument2 *iface, VARIANT *p)
1113 {
1114     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1115
1116     TRACE("(%p)->(%p)\n", This, p);
1117
1118     return get_doc_event(This, EVENTID_KEYDOWN, p);
1119 }
1120
1121 static HRESULT WINAPI HTMLDocument_put_onkeypress(IHTMLDocument2 *iface, VARIANT v)
1122 {
1123     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1124
1125     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1126
1127     return set_doc_event(This, EVENTID_KEYPRESS, &v);
1128 }
1129
1130 static HRESULT WINAPI HTMLDocument_get_onkeypress(IHTMLDocument2 *iface, VARIANT *p)
1131 {
1132     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1133
1134     TRACE("(%p)->(%p)\n", This, p);
1135
1136     return get_doc_event(This, EVENTID_KEYPRESS, p);
1137 }
1138
1139 static HRESULT WINAPI HTMLDocument_put_onmouseup(IHTMLDocument2 *iface, VARIANT v)
1140 {
1141     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1142
1143     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1144
1145     return set_doc_event(This, EVENTID_MOUSEUP, &v);
1146 }
1147
1148 static HRESULT WINAPI HTMLDocument_get_onmouseup(IHTMLDocument2 *iface, VARIANT *p)
1149 {
1150     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1151
1152     TRACE("(%p)->(%p)\n", This, p);
1153
1154     return get_doc_event(This, EVENTID_MOUSEUP, p);
1155 }
1156
1157 static HRESULT WINAPI HTMLDocument_put_onmousedown(IHTMLDocument2 *iface, VARIANT v)
1158 {
1159     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1160
1161     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1162
1163     return set_doc_event(This, EVENTID_MOUSEDOWN, &v);
1164 }
1165
1166 static HRESULT WINAPI HTMLDocument_get_onmousedown(IHTMLDocument2 *iface, VARIANT *p)
1167 {
1168     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1169
1170     TRACE("(%p)->(%p)\n", This, p);
1171
1172     return get_doc_event(This, EVENTID_MOUSEDOWN, p);
1173 }
1174
1175 static HRESULT WINAPI HTMLDocument_put_onmousemove(IHTMLDocument2 *iface, VARIANT v)
1176 {
1177     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1178
1179     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1180
1181     return set_doc_event(This, EVENTID_MOUSEMOVE, &v);
1182 }
1183
1184 static HRESULT WINAPI HTMLDocument_get_onmousemove(IHTMLDocument2 *iface, VARIANT *p)
1185 {
1186     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1187
1188     TRACE("(%p)->(%p)\n", This, p);
1189
1190     return get_doc_event(This, EVENTID_MOUSEMOVE, p);
1191 }
1192
1193 static HRESULT WINAPI HTMLDocument_put_onmouseout(IHTMLDocument2 *iface, VARIANT v)
1194 {
1195     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1196
1197     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1198
1199     return set_doc_event(This, EVENTID_MOUSEOUT, &v);
1200 }
1201
1202 static HRESULT WINAPI HTMLDocument_get_onmouseout(IHTMLDocument2 *iface, VARIANT *p)
1203 {
1204     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1205
1206     TRACE("(%p)->(%p)\n", This, p);
1207
1208     return get_doc_event(This, EVENTID_MOUSEOUT, p);
1209 }
1210
1211 static HRESULT WINAPI HTMLDocument_put_onmouseover(IHTMLDocument2 *iface, VARIANT v)
1212 {
1213     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1214
1215     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1216
1217     return set_doc_event(This, EVENTID_MOUSEOVER, &v);
1218 }
1219
1220 static HRESULT WINAPI HTMLDocument_get_onmouseover(IHTMLDocument2 *iface, VARIANT *p)
1221 {
1222     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1223
1224     TRACE("(%p)->(%p)\n", This, p);
1225
1226     return get_doc_event(This, EVENTID_MOUSEOVER, p);
1227 }
1228
1229 static HRESULT WINAPI HTMLDocument_put_onreadystatechange(IHTMLDocument2 *iface, VARIANT v)
1230 {
1231     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1232
1233     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1234
1235     return set_doc_event(This, EVENTID_READYSTATECHANGE, &v);
1236 }
1237
1238 static HRESULT WINAPI HTMLDocument_get_onreadystatechange(IHTMLDocument2 *iface, VARIANT *p)
1239 {
1240     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1241
1242     TRACE("(%p)->(%p)\n", This, p);
1243
1244     return get_doc_event(This, EVENTID_READYSTATECHANGE, p);
1245 }
1246
1247 static HRESULT WINAPI HTMLDocument_put_onafterupdate(IHTMLDocument2 *iface, VARIANT v)
1248 {
1249     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1250     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1251     return E_NOTIMPL;
1252 }
1253
1254 static HRESULT WINAPI HTMLDocument_get_onafterupdate(IHTMLDocument2 *iface, VARIANT *p)
1255 {
1256     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1257     FIXME("(%p)->(%p)\n", This, p);
1258     return E_NOTIMPL;
1259 }
1260
1261 static HRESULT WINAPI HTMLDocument_put_onrowexit(IHTMLDocument2 *iface, VARIANT v)
1262 {
1263     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1264     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1265     return E_NOTIMPL;
1266 }
1267
1268 static HRESULT WINAPI HTMLDocument_get_onrowexit(IHTMLDocument2 *iface, VARIANT *p)
1269 {
1270     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1271     FIXME("(%p)->(%p)\n", This, p);
1272     return E_NOTIMPL;
1273 }
1274
1275 static HRESULT WINAPI HTMLDocument_put_onrowenter(IHTMLDocument2 *iface, VARIANT v)
1276 {
1277     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1278     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1279     return E_NOTIMPL;
1280 }
1281
1282 static HRESULT WINAPI HTMLDocument_get_onrowenter(IHTMLDocument2 *iface, VARIANT *p)
1283 {
1284     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1285     FIXME("(%p)->(%p)\n", This, p);
1286     return E_NOTIMPL;
1287 }
1288
1289 static HRESULT WINAPI HTMLDocument_put_ondragstart(IHTMLDocument2 *iface, VARIANT v)
1290 {
1291     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1292
1293     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1294
1295     return set_doc_event(This, EVENTID_DRAGSTART, &v);
1296 }
1297
1298 static HRESULT WINAPI HTMLDocument_get_ondragstart(IHTMLDocument2 *iface, VARIANT *p)
1299 {
1300     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1301
1302     TRACE("(%p)->(%p)\n", This, p);
1303
1304     return get_doc_event(This, EVENTID_DRAGSTART, p);
1305 }
1306
1307 static HRESULT WINAPI HTMLDocument_put_onselectstart(IHTMLDocument2 *iface, VARIANT v)
1308 {
1309     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1310
1311     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
1312
1313     return set_doc_event(This, EVENTID_SELECTSTART, &v);
1314 }
1315
1316 static HRESULT WINAPI HTMLDocument_get_onselectstart(IHTMLDocument2 *iface, VARIANT *p)
1317 {
1318     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1319
1320     TRACE("(%p)->(%p)\n", This, p);
1321
1322     return get_doc_event(This, EVENTID_SELECTSTART, p);
1323 }
1324
1325 static HRESULT WINAPI HTMLDocument_elementFromPoint(IHTMLDocument2 *iface, LONG x, LONG y,
1326                                                         IHTMLElement **elementHit)
1327 {
1328     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1329     nsIDOMElement *nselem;
1330     HTMLDOMNode *node;
1331     nsresult nsres;
1332     HRESULT hres;
1333
1334     TRACE("(%p)->(%d %d %p)\n", This, x, y, elementHit);
1335
1336     nsres = nsIDOMHTMLDocument_ElementFromPoint(This->doc_node->nsdoc, x, y, &nselem);
1337     if(NS_FAILED(nsres)) {
1338         ERR("ElementFromPoint failed: %08x\n", nsres);
1339         return E_FAIL;
1340     }
1341
1342     if(!nselem) {
1343         *elementHit = NULL;
1344         return S_OK;
1345     }
1346
1347     hres = get_node(This->doc_node, (nsIDOMNode*)nselem, TRUE, &node);
1348     nsIDOMElement_Release(nselem);
1349     if(FAILED(hres))
1350         return hres;
1351
1352     return IHTMLDOMNode_QueryInterface(&node->IHTMLDOMNode_iface, &IID_IHTMLElement, (void**)elementHit);
1353 }
1354
1355 static HRESULT WINAPI HTMLDocument_get_parentWindow(IHTMLDocument2 *iface, IHTMLWindow2 **p)
1356 {
1357     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1358
1359     TRACE("(%p)->(%p)\n", This, p);
1360
1361     *p = &This->window->IHTMLWindow2_iface;
1362     IHTMLWindow2_AddRef(*p);
1363     return S_OK;
1364 }
1365
1366 static HRESULT WINAPI HTMLDocument_get_styleSheets(IHTMLDocument2 *iface,
1367                                                    IHTMLStyleSheetsCollection **p)
1368 {
1369     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1370     nsIDOMStyleSheetList *nsstylelist;
1371     nsresult nsres;
1372
1373     TRACE("(%p)->(%p)\n", This, p);
1374
1375     *p = NULL;
1376
1377     if(!This->doc_node->nsdoc) {
1378         WARN("NULL nsdoc\n");
1379         return E_UNEXPECTED;
1380     }
1381
1382     nsres = nsIDOMHTMLDocument_GetStyleSheets(This->doc_node->nsdoc, &nsstylelist);
1383     if(NS_FAILED(nsres)) {
1384         ERR("GetStyleSheets failed: %08x\n", nsres);
1385         return E_FAIL;
1386     }
1387
1388     *p = HTMLStyleSheetsCollection_Create(nsstylelist);
1389     nsIDOMStyleSheetList_Release(nsstylelist);
1390
1391     return S_OK;
1392 }
1393
1394 static HRESULT WINAPI HTMLDocument_put_onbeforeupdate(IHTMLDocument2 *iface, VARIANT v)
1395 {
1396     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1397     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1398     return E_NOTIMPL;
1399 }
1400
1401 static HRESULT WINAPI HTMLDocument_get_onbeforeupdate(IHTMLDocument2 *iface, VARIANT *p)
1402 {
1403     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1404     FIXME("(%p)->(%p)\n", This, p);
1405     return E_NOTIMPL;
1406 }
1407
1408 static HRESULT WINAPI HTMLDocument_put_onerrorupdate(IHTMLDocument2 *iface, VARIANT v)
1409 {
1410     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1411     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1412     return E_NOTIMPL;
1413 }
1414
1415 static HRESULT WINAPI HTMLDocument_get_onerrorupdate(IHTMLDocument2 *iface, VARIANT *p)
1416 {
1417     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1418     FIXME("(%p)->(%p)\n", This, p);
1419     return E_NOTIMPL;
1420 }
1421
1422 static HRESULT WINAPI HTMLDocument_toString(IHTMLDocument2 *iface, BSTR *String)
1423 {
1424     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1425     FIXME("(%p)->(%p)\n", This, String);
1426     return E_NOTIMPL;
1427 }
1428
1429 static HRESULT WINAPI HTMLDocument_createStyleSheet(IHTMLDocument2 *iface, BSTR bstrHref,
1430                                             LONG lIndex, IHTMLStyleSheet **ppnewStyleSheet)
1431 {
1432     HTMLDocument *This = impl_from_IHTMLDocument2(iface);
1433
1434     FIXME("(%p)->(%s %d %p) semi-stub\n", This, debugstr_w(bstrHref), lIndex, ppnewStyleSheet);
1435
1436     *ppnewStyleSheet = HTMLStyleSheet_Create(NULL);
1437     return S_OK;
1438 }
1439
1440 static const IHTMLDocument2Vtbl HTMLDocumentVtbl = {
1441     HTMLDocument_QueryInterface,
1442     HTMLDocument_AddRef,
1443     HTMLDocument_Release,
1444     HTMLDocument_GetTypeInfoCount,
1445     HTMLDocument_GetTypeInfo,
1446     HTMLDocument_GetIDsOfNames,
1447     HTMLDocument_Invoke,
1448     HTMLDocument_get_Script,
1449     HTMLDocument_get_all,
1450     HTMLDocument_get_body,
1451     HTMLDocument_get_activeElement,
1452     HTMLDocument_get_images,
1453     HTMLDocument_get_applets,
1454     HTMLDocument_get_links,
1455     HTMLDocument_get_forms,
1456     HTMLDocument_get_anchors,
1457     HTMLDocument_put_title,
1458     HTMLDocument_get_title,
1459     HTMLDocument_get_scripts,
1460     HTMLDocument_put_designMode,
1461     HTMLDocument_get_designMode,
1462     HTMLDocument_get_selection,
1463     HTMLDocument_get_readyState,
1464     HTMLDocument_get_frames,
1465     HTMLDocument_get_embeds,
1466     HTMLDocument_get_plugins,
1467     HTMLDocument_put_alinkColor,
1468     HTMLDocument_get_alinkColor,
1469     HTMLDocument_put_bgColor,
1470     HTMLDocument_get_bgColor,
1471     HTMLDocument_put_fgColor,
1472     HTMLDocument_get_fgColor,
1473     HTMLDocument_put_linkColor,
1474     HTMLDocument_get_linkColor,
1475     HTMLDocument_put_vlinkColor,
1476     HTMLDocument_get_vlinkColor,
1477     HTMLDocument_get_referrer,
1478     HTMLDocument_get_location,
1479     HTMLDocument_get_lastModified,
1480     HTMLDocument_put_URL,
1481     HTMLDocument_get_URL,
1482     HTMLDocument_put_domain,
1483     HTMLDocument_get_domain,
1484     HTMLDocument_put_cookie,
1485     HTMLDocument_get_cookie,
1486     HTMLDocument_put_expando,
1487     HTMLDocument_get_expando,
1488     HTMLDocument_put_charset,
1489     HTMLDocument_get_charset,
1490     HTMLDocument_put_defaultCharset,
1491     HTMLDocument_get_defaultCharset,
1492     HTMLDocument_get_mimeType,
1493     HTMLDocument_get_fileSize,
1494     HTMLDocument_get_fileCreatedDate,
1495     HTMLDocument_get_fileModifiedDate,
1496     HTMLDocument_get_fileUpdatedDate,
1497     HTMLDocument_get_security,
1498     HTMLDocument_get_protocol,
1499     HTMLDocument_get_nameProp,
1500     HTMLDocument_write,
1501     HTMLDocument_writeln,
1502     HTMLDocument_open,
1503     HTMLDocument_close,
1504     HTMLDocument_clear,
1505     HTMLDocument_queryCommandSupported,
1506     HTMLDocument_queryCommandEnabled,
1507     HTMLDocument_queryCommandState,
1508     HTMLDocument_queryCommandIndeterm,
1509     HTMLDocument_queryCommandText,
1510     HTMLDocument_queryCommandValue,
1511     HTMLDocument_execCommand,
1512     HTMLDocument_execCommandShowHelp,
1513     HTMLDocument_createElement,
1514     HTMLDocument_put_onhelp,
1515     HTMLDocument_get_onhelp,
1516     HTMLDocument_put_onclick,
1517     HTMLDocument_get_onclick,
1518     HTMLDocument_put_ondblclick,
1519     HTMLDocument_get_ondblclick,
1520     HTMLDocument_put_onkeyup,
1521     HTMLDocument_get_onkeyup,
1522     HTMLDocument_put_onkeydown,
1523     HTMLDocument_get_onkeydown,
1524     HTMLDocument_put_onkeypress,
1525     HTMLDocument_get_onkeypress,
1526     HTMLDocument_put_onmouseup,
1527     HTMLDocument_get_onmouseup,
1528     HTMLDocument_put_onmousedown,
1529     HTMLDocument_get_onmousedown,
1530     HTMLDocument_put_onmousemove,
1531     HTMLDocument_get_onmousemove,
1532     HTMLDocument_put_onmouseout,
1533     HTMLDocument_get_onmouseout,
1534     HTMLDocument_put_onmouseover,
1535     HTMLDocument_get_onmouseover,
1536     HTMLDocument_put_onreadystatechange,
1537     HTMLDocument_get_onreadystatechange,
1538     HTMLDocument_put_onafterupdate,
1539     HTMLDocument_get_onafterupdate,
1540     HTMLDocument_put_onrowexit,
1541     HTMLDocument_get_onrowexit,
1542     HTMLDocument_put_onrowenter,
1543     HTMLDocument_get_onrowenter,
1544     HTMLDocument_put_ondragstart,
1545     HTMLDocument_get_ondragstart,
1546     HTMLDocument_put_onselectstart,
1547     HTMLDocument_get_onselectstart,
1548     HTMLDocument_elementFromPoint,
1549     HTMLDocument_get_parentWindow,
1550     HTMLDocument_get_styleSheets,
1551     HTMLDocument_put_onbeforeupdate,
1552     HTMLDocument_get_onbeforeupdate,
1553     HTMLDocument_put_onerrorupdate,
1554     HTMLDocument_get_onerrorupdate,
1555     HTMLDocument_toString,
1556     HTMLDocument_createStyleSheet
1557 };
1558
1559 static void HTMLDocument_on_advise(IUnknown *iface, cp_static_data_t *cp)
1560 {
1561     HTMLDocument *This = impl_from_IHTMLDocument2((IHTMLDocument2*)iface);
1562
1563     if(This->window)
1564         update_cp_events(This->window, &This->doc_node->node.event_target, cp, This->doc_node->node.nsnode);
1565 }
1566
1567 static inline HTMLDocument *impl_from_ISupportErrorInfo(ISupportErrorInfo *iface)
1568 {
1569     return CONTAINING_RECORD(iface, HTMLDocument, ISupportErrorInfo_iface);
1570 }
1571
1572 static HRESULT WINAPI SupportErrorInfo_QueryInterface(ISupportErrorInfo *iface, REFIID riid, void **ppv)
1573 {
1574     HTMLDocument *This = impl_from_ISupportErrorInfo(iface);
1575     return htmldoc_query_interface(This, riid, ppv);
1576 }
1577
1578 static ULONG WINAPI SupportErrorInfo_AddRef(ISupportErrorInfo *iface)
1579 {
1580     HTMLDocument *This = impl_from_ISupportErrorInfo(iface);
1581     return htmldoc_addref(This);
1582 }
1583
1584 static ULONG WINAPI SupportErrorInfo_Release(ISupportErrorInfo *iface)
1585 {
1586     HTMLDocument *This = impl_from_ISupportErrorInfo(iface);
1587     return htmldoc_release(This);
1588 }
1589
1590 static HRESULT WINAPI SupportErrorInfo_InterfaceSupportsErrorInfo(ISupportErrorInfo *iface, REFIID riid)
1591 {
1592     FIXME("(%p)->(%s)\n", iface, debugstr_guid(riid));
1593     return S_FALSE;
1594 }
1595
1596 static const ISupportErrorInfoVtbl SupportErrorInfoVtbl = {
1597     SupportErrorInfo_QueryInterface,
1598     SupportErrorInfo_AddRef,
1599     SupportErrorInfo_Release,
1600     SupportErrorInfo_InterfaceSupportsErrorInfo
1601 };
1602
1603 static inline HTMLDocument *impl_from_IDispatchEx(IDispatchEx *iface)
1604 {
1605     return CONTAINING_RECORD(iface, HTMLDocument, IDispatchEx_iface);
1606 }
1607
1608 static HRESULT dispid_from_elem_name(HTMLDocumentNode *This, BSTR name, DISPID *dispid)
1609 {
1610     nsIDOMNodeList *node_list;
1611     nsAString name_str;
1612     PRUint32 len;
1613     unsigned i;
1614     nsresult nsres;
1615
1616     if(!This->nsdoc)
1617         return DISP_E_UNKNOWNNAME;
1618
1619     nsAString_InitDepend(&name_str, name);
1620     nsres = nsIDOMHTMLDocument_GetElementsByName(This->nsdoc, &name_str, &node_list);
1621     nsAString_Finish(&name_str);
1622     if(NS_FAILED(nsres))
1623         return E_FAIL;
1624
1625     nsres = nsIDOMNodeList_GetLength(node_list, &len);
1626     nsIDOMNodeList_Release(node_list);
1627     if(NS_FAILED(nsres))
1628         return E_FAIL;
1629
1630     if(!len)
1631         return DISP_E_UNKNOWNNAME;
1632
1633     for(i=0; i < This->elem_vars_cnt; i++) {
1634         if(!strcmpW(name, This->elem_vars[i])) {
1635             *dispid = MSHTML_DISPID_CUSTOM_MIN+i;
1636             return S_OK;
1637         }
1638     }
1639
1640     if(This->elem_vars_cnt == This->elem_vars_size) {
1641         WCHAR **new_vars;
1642
1643         if(This->elem_vars_size) {
1644             new_vars = heap_realloc(This->elem_vars, This->elem_vars_size*2*sizeof(WCHAR*));
1645             if(!new_vars)
1646                 return E_OUTOFMEMORY;
1647             This->elem_vars_size *= 2;
1648         }else {
1649             new_vars = heap_alloc(16*sizeof(WCHAR*));
1650             if(!new_vars)
1651                 return E_OUTOFMEMORY;
1652             This->elem_vars_size = 16;
1653         }
1654
1655         This->elem_vars = new_vars;
1656     }
1657
1658     This->elem_vars[This->elem_vars_cnt] = heap_strdupW(name);
1659     if(!This->elem_vars[This->elem_vars_cnt])
1660         return E_OUTOFMEMORY;
1661
1662     *dispid = MSHTML_DISPID_CUSTOM_MIN+This->elem_vars_cnt++;
1663     return S_OK;
1664 }
1665
1666 static HRESULT WINAPI DocDispatchEx_QueryInterface(IDispatchEx *iface, REFIID riid, void **ppv)
1667 {
1668     HTMLDocument *This = impl_from_IDispatchEx(iface);
1669
1670     return htmldoc_query_interface(This, riid, ppv);
1671 }
1672
1673 static ULONG WINAPI DocDispatchEx_AddRef(IDispatchEx *iface)
1674 {
1675     HTMLDocument *This = impl_from_IDispatchEx(iface);
1676
1677     return htmldoc_addref(This);
1678 }
1679
1680 static ULONG WINAPI DocDispatchEx_Release(IDispatchEx *iface)
1681 {
1682     HTMLDocument *This = impl_from_IDispatchEx(iface);
1683
1684     return htmldoc_release(This);
1685 }
1686
1687 static HRESULT WINAPI DocDispatchEx_GetTypeInfoCount(IDispatchEx *iface, UINT *pctinfo)
1688 {
1689     HTMLDocument *This = impl_from_IDispatchEx(iface);
1690
1691     return IDispatchEx_GetTypeInfoCount(This->dispex, pctinfo);
1692 }
1693
1694 static HRESULT WINAPI DocDispatchEx_GetTypeInfo(IDispatchEx *iface, UINT iTInfo,
1695                                                LCID lcid, ITypeInfo **ppTInfo)
1696 {
1697     HTMLDocument *This = impl_from_IDispatchEx(iface);
1698
1699     return IDispatchEx_GetTypeInfo(This->dispex, iTInfo, lcid, ppTInfo);
1700 }
1701
1702 static HRESULT WINAPI DocDispatchEx_GetIDsOfNames(IDispatchEx *iface, REFIID riid,
1703                                                  LPOLESTR *rgszNames, UINT cNames,
1704                                                  LCID lcid, DISPID *rgDispId)
1705 {
1706     HTMLDocument *This = impl_from_IDispatchEx(iface);
1707
1708     return IDispatchEx_GetIDsOfNames(This->dispex, riid, rgszNames, cNames, lcid, rgDispId);
1709 }
1710
1711 static HRESULT WINAPI DocDispatchEx_Invoke(IDispatchEx *iface, DISPID dispIdMember,
1712                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
1713                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
1714 {
1715     HTMLDocument *This = impl_from_IDispatchEx(iface);
1716
1717     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
1718           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1719
1720     switch(dispIdMember) {
1721     case DISPID_READYSTATE:
1722         TRACE("DISPID_READYSTATE\n");
1723
1724         if(!(wFlags & DISPATCH_PROPERTYGET))
1725             return E_INVALIDARG;
1726
1727         V_VT(pVarResult) = VT_I4;
1728         V_I4(pVarResult) = This->window->readystate;
1729         return S_OK;
1730     }
1731
1732     return IDispatchEx_Invoke(This->dispex, dispIdMember, riid, lcid, wFlags, pDispParams,
1733                               pVarResult, pExcepInfo, puArgErr);
1734 }
1735
1736 static HRESULT WINAPI DocDispatchEx_GetDispID(IDispatchEx *iface, BSTR bstrName, DWORD grfdex, DISPID *pid)
1737 {
1738     HTMLDocument *This = impl_from_IDispatchEx(iface);
1739     HRESULT hres;
1740
1741     hres = IDispatchEx_GetDispID(This->dispex, bstrName, grfdex, pid);
1742     if(hres != DISP_E_UNKNOWNNAME)
1743         return hres;
1744
1745     return  dispid_from_elem_name(This->doc_node, bstrName, pid);
1746 }
1747
1748 static HRESULT WINAPI DocDispatchEx_InvokeEx(IDispatchEx *iface, DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp,
1749         VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
1750 {
1751     HTMLDocument *This = impl_from_IDispatchEx(iface);
1752
1753     if(This->window && id == DISPID_IHTMLDOCUMENT2_LOCATION && (wFlags & DISPATCH_PROPERTYPUT))
1754         return IDispatchEx_InvokeEx(&This->window->IDispatchEx_iface, DISPID_IHTMLWINDOW2_LOCATION,
1755                 lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1756
1757
1758     return IDispatchEx_InvokeEx(This->dispex, id, lcid, wFlags, pdp, pvarRes, pei, pspCaller);
1759 }
1760
1761 static HRESULT WINAPI DocDispatchEx_DeleteMemberByName(IDispatchEx *iface, BSTR bstrName, DWORD grfdex)
1762 {
1763     HTMLDocument *This = impl_from_IDispatchEx(iface);
1764
1765     return IDispatchEx_DeleteMemberByName(This->dispex, bstrName, grfdex);
1766 }
1767
1768 static HRESULT WINAPI DocDispatchEx_DeleteMemberByDispID(IDispatchEx *iface, DISPID id)
1769 {
1770     HTMLDocument *This = impl_from_IDispatchEx(iface);
1771
1772     return IDispatchEx_DeleteMemberByDispID(This->dispex, id);
1773 }
1774
1775 static HRESULT WINAPI DocDispatchEx_GetMemberProperties(IDispatchEx *iface, DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
1776 {
1777     HTMLDocument *This = impl_from_IDispatchEx(iface);
1778
1779     return IDispatchEx_GetMemberProperties(This->dispex, id, grfdexFetch, pgrfdex);
1780 }
1781
1782 static HRESULT WINAPI DocDispatchEx_GetMemberName(IDispatchEx *iface, DISPID id, BSTR *pbstrName)
1783 {
1784     HTMLDocument *This = impl_from_IDispatchEx(iface);
1785
1786     return IDispatchEx_GetMemberName(This->dispex, id, pbstrName);
1787 }
1788
1789 static HRESULT WINAPI DocDispatchEx_GetNextDispID(IDispatchEx *iface, DWORD grfdex, DISPID id, DISPID *pid)
1790 {
1791     HTMLDocument *This = impl_from_IDispatchEx(iface);
1792
1793     return IDispatchEx_GetNextDispID(This->dispex, grfdex, id, pid);
1794 }
1795
1796 static HRESULT WINAPI DocDispatchEx_GetNameSpaceParent(IDispatchEx *iface, IUnknown **ppunk)
1797 {
1798     HTMLDocument *This = impl_from_IDispatchEx(iface);
1799
1800     return IDispatchEx_GetNameSpaceParent(This->dispex, ppunk);
1801 }
1802
1803 static const IDispatchExVtbl DocDispatchExVtbl = {
1804     DocDispatchEx_QueryInterface,
1805     DocDispatchEx_AddRef,
1806     DocDispatchEx_Release,
1807     DocDispatchEx_GetTypeInfoCount,
1808     DocDispatchEx_GetTypeInfo,
1809     DocDispatchEx_GetIDsOfNames,
1810     DocDispatchEx_Invoke,
1811     DocDispatchEx_GetDispID,
1812     DocDispatchEx_InvokeEx,
1813     DocDispatchEx_DeleteMemberByName,
1814     DocDispatchEx_DeleteMemberByDispID,
1815     DocDispatchEx_GetMemberProperties,
1816     DocDispatchEx_GetMemberName,
1817     DocDispatchEx_GetNextDispID,
1818     DocDispatchEx_GetNameSpaceParent
1819 };
1820
1821 static inline HTMLDocument *impl_from_IProvideClassInfo(IProvideClassInfo *iface)
1822 {
1823     return CONTAINING_RECORD(iface, HTMLDocument, IProvideClassInfo_iface);
1824 }
1825
1826 static HRESULT WINAPI ProvideClassInfo_QueryInterface(IProvideClassInfo *iface,
1827         REFIID riid, void **ppv)
1828 {
1829     HTMLDocument *This = impl_from_IProvideClassInfo(iface);
1830     return htmldoc_query_interface(This, riid, ppv);
1831 }
1832
1833 static ULONG WINAPI ProvideClassInfo_AddRef(IProvideClassInfo *iface)
1834 {
1835     HTMLDocument *This = impl_from_IProvideClassInfo(iface);
1836     return htmldoc_addref(This);
1837 }
1838
1839 static ULONG WINAPI ProvideClassInfo_Release(IProvideClassInfo *iface)
1840 {
1841     HTMLDocument *This = impl_from_IProvideClassInfo(iface);
1842     return htmldoc_release(This);
1843 }
1844
1845 static HRESULT WINAPI ProvideClassInfo_GetClassInfo(IProvideClassInfo* iface,
1846         ITypeInfo **ppTI)
1847 {
1848     HTMLDocument *This = impl_from_IProvideClassInfo(iface);
1849     TRACE("(%p)->(%p)\n", This, ppTI);
1850     return get_htmldoc_classinfo(ppTI);
1851 }
1852
1853 static const IProvideClassInfoVtbl ProvideClassInfoVtbl = {
1854     ProvideClassInfo_QueryInterface,
1855     ProvideClassInfo_AddRef,
1856     ProvideClassInfo_Release,
1857     ProvideClassInfo_GetClassInfo
1858 };
1859
1860 static BOOL htmldoc_qi(HTMLDocument *This, REFIID riid, void **ppv)
1861 {
1862     *ppv = NULL;
1863
1864     if(IsEqualGUID(&IID_IUnknown, riid)) {
1865         TRACE("(%p)->(IID_IUnknown, %p)\n", This, ppv);
1866         *ppv = &This->IHTMLDocument2_iface;
1867     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
1868         TRACE("(%p)->(IID_IDispatch, %p)\n", This, ppv);
1869         *ppv = &This->IDispatchEx_iface;
1870     }else if(IsEqualGUID(&IID_IDispatchEx, riid)) {
1871         TRACE("(%p)->(IID_IDispatchEx, %p)\n", This, ppv);
1872         *ppv = &This->IDispatchEx_iface;
1873     }else if(IsEqualGUID(&IID_IHTMLDocument, riid)) {
1874         TRACE("(%p)->(IID_IHTMLDocument, %p)\n", This, ppv);
1875         *ppv = &This->IHTMLDocument2_iface;
1876     }else if(IsEqualGUID(&IID_IHTMLDocument2, riid)) {
1877         TRACE("(%p)->(IID_IHTMLDocument2, %p)\n", This, ppv);
1878         *ppv = &This->IHTMLDocument2_iface;
1879     }else if(IsEqualGUID(&IID_IHTMLDocument3, riid)) {
1880         TRACE("(%p)->(IID_IHTMLDocument3, %p)\n", This, ppv);
1881         *ppv = &This->IHTMLDocument3_iface;
1882     }else if(IsEqualGUID(&IID_IHTMLDocument4, riid)) {
1883         TRACE("(%p)->(IID_IHTMLDocument4, %p)\n", This, ppv);
1884         *ppv = &This->IHTMLDocument4_iface;
1885     }else if(IsEqualGUID(&IID_IHTMLDocument5, riid)) {
1886         TRACE("(%p)->(IID_IHTMLDocument5, %p)\n", This, ppv);
1887         *ppv = &This->IHTMLDocument5_iface;
1888     }else if(IsEqualGUID(&IID_IHTMLDocument6, riid)) {
1889         TRACE("(%p)->(IID_IHTMLDocument6, %p)\n", This, ppv);
1890         *ppv = &This->IHTMLDocument6_iface;
1891     }else if(IsEqualGUID(&IID_IPersist, riid)) {
1892         TRACE("(%p)->(IID_IPersist, %p)\n", This, ppv);
1893         *ppv = &This->IPersistFile_iface;
1894     }else if(IsEqualGUID(&IID_IPersistMoniker, riid)) {
1895         TRACE("(%p)->(IID_IPersistMoniker, %p)\n", This, ppv);
1896         *ppv = &This->IPersistMoniker_iface;
1897     }else if(IsEqualGUID(&IID_IPersistFile, riid)) {
1898         TRACE("(%p)->(IID_IPersistFile, %p)\n", This, ppv);
1899         *ppv = &This->IPersistFile_iface;
1900     }else if(IsEqualGUID(&IID_IMonikerProp, riid)) {
1901         TRACE("(%p)->(IID_IMonikerProp, %p)\n", This, ppv);
1902         *ppv = &This->IMonikerProp_iface;
1903     }else if(IsEqualGUID(&IID_IOleObject, riid)) {
1904         TRACE("(%p)->(IID_IOleObject, %p)\n", This, ppv);
1905         *ppv = &This->IOleObject_iface;
1906     }else if(IsEqualGUID(&IID_IOleDocument, riid)) {
1907         TRACE("(%p)->(IID_IOleDocument, %p)\n", This, ppv);
1908         *ppv = &This->IOleDocument_iface;
1909     }else if(IsEqualGUID(&IID_IOleDocumentView, riid)) {
1910         TRACE("(%p)->(IID_IOleDocumentView, %p)\n", This, ppv);
1911         *ppv = &This->IOleDocumentView_iface;
1912     }else if(IsEqualGUID(&IID_IOleInPlaceActiveObject, riid)) {
1913         TRACE("(%p)->(IID_IOleInPlaceActiveObject, %p)\n", This, ppv);
1914         *ppv = &This->IOleInPlaceActiveObject_iface;
1915     }else if(IsEqualGUID(&IID_IViewObject, riid)) {
1916         TRACE("(%p)->(IID_IViewObject, %p)\n", This, ppv);
1917         *ppv = &This->IViewObjectEx_iface;
1918     }else if(IsEqualGUID(&IID_IViewObject2, riid)) {
1919         TRACE("(%p)->(IID_IViewObject2, %p)\n", This, ppv);
1920         *ppv = &This->IViewObjectEx_iface;
1921     }else if(IsEqualGUID(&IID_IViewObjectEx, riid)) {
1922         TRACE("(%p)->(IID_IViewObjectEx, %p)\n", This, ppv);
1923         *ppv = &This->IViewObjectEx_iface;
1924     }else if(IsEqualGUID(&IID_IOleWindow, riid)) {
1925         TRACE("(%p)->(IID_IOleWindow, %p)\n", This, ppv);
1926         *ppv = &This->IOleInPlaceActiveObject_iface;
1927     }else if(IsEqualGUID(&IID_IOleInPlaceObject, riid)) {
1928         TRACE("(%p)->(IID_IOleInPlaceObject, %p)\n", This, ppv);
1929         *ppv = &This->IOleInPlaceObjectWindowless_iface;
1930     }else if(IsEqualGUID(&IID_IOleInPlaceObjectWindowless, riid)) {
1931         TRACE("(%p)->(IID_IOleInPlaceObjectWindowless, %p)\n", This, ppv);
1932         *ppv = &This->IOleInPlaceObjectWindowless_iface;
1933     }else if(IsEqualGUID(&IID_IServiceProvider, riid)) {
1934         TRACE("(%p)->(IID_IServiceProvider, %p)\n", This, ppv);
1935         *ppv = &This->IServiceProvider_iface;
1936     }else if(IsEqualGUID(&IID_IOleCommandTarget, riid)) {
1937         TRACE("(%p)->(IID_IOleCommandTarget, %p)\n", This, ppv);
1938         *ppv = &This->IOleCommandTarget_iface;
1939     }else if(IsEqualGUID(&IID_IOleControl, riid)) {
1940         TRACE("(%p)->(IID_IOleControl, %p)\n", This, ppv);
1941         *ppv = &This->IOleControl_iface;
1942     }else if(IsEqualGUID(&IID_IHlinkTarget, riid)) {
1943         TRACE("(%p)->(IID_IHlinkTarget, %p)\n", This, ppv);
1944         *ppv = &This->IHlinkTarget_iface;
1945     }else if(IsEqualGUID(&IID_IConnectionPointContainer, riid)) {
1946         TRACE("(%p)->(IID_IConnectionPointContainer %p)\n", This, ppv);
1947         *ppv = &This->cp_container.IConnectionPointContainer_iface;
1948     }else if(IsEqualGUID(&IID_IPersistStreamInit, riid)) {
1949         TRACE("(%p)->(IID_IPersistStreamInit %p)\n", This, ppv);
1950         *ppv = &This->IPersistStreamInit_iface;
1951     }else if(IsEqualGUID(&DIID_DispHTMLDocument, riid)) {
1952         TRACE("(%p)->(DIID_DispHTMLDocument %p)\n", This, ppv);
1953         *ppv = &This->IHTMLDocument2_iface;
1954     }else if(IsEqualGUID(&IID_ISupportErrorInfo, riid)) {
1955         TRACE("(%p)->(IID_ISupportErrorInfo %p)\n", This, ppv);
1956         *ppv = &This->ISupportErrorInfo_iface;
1957     }else if(IsEqualGUID(&IID_IPersistHistory, riid)) {
1958         TRACE("(%p)->(IID_IPersistHistory %p)\n", This, ppv);
1959         *ppv = &This->IPersistHistory_iface;
1960     }else if(IsEqualGUID(&CLSID_CMarkup, riid)) {
1961         FIXME("(%p)->(CLSID_CMarkup %p)\n", This, ppv);
1962         *ppv = NULL;
1963     }else if(IsEqualGUID(&IID_IRunnableObject, riid)) {
1964         TRACE("(%p)->(IID_IRunnableObject %p) returning NULL\n", This, ppv);
1965         *ppv = NULL;
1966     }else if(IsEqualGUID(&IID_IPersistPropertyBag, riid)) {
1967         TRACE("(%p)->(IID_IPersistPropertyBag %p) returning NULL\n", This, ppv);
1968         *ppv = NULL;
1969     }else if(IsEqualGUID(&IID_IMarshal, riid)) {
1970         TRACE("(%p)->(IID_IMarshal %p) returning NULL\n", This, ppv);
1971         *ppv = NULL;
1972     }else if(IsEqualGUID(&IID_IExternalConnection, riid)) {
1973         TRACE("(%p)->(IID_IExternalConnection %p) returning NULL\n", This, ppv);
1974         *ppv = NULL;
1975     }else if(IsEqualGUID(&IID_IStdMarshalInfo, riid)) {
1976         TRACE("(%p)->(IID_IStdMarshalInfo %p) returning NULL\n", This, ppv);
1977         *ppv = NULL;
1978     }else if(IsEqualGUID(&IID_IObjectWithSite, riid)) {
1979         TRACE("(%p)->(IID_IObjectWithSite %p)\n", This, ppv);
1980         *ppv = &This->IObjectWithSite_iface;
1981     }else if(IsEqualGUID(&IID_IOleContainer, riid)) {
1982         TRACE("(%p)->(IID_IOleContainer %p)\n", This, ppv);
1983         *ppv = &This->IOleContainer_iface;
1984     }else if(IsEqualGUID(&IID_IObjectSafety, riid)) {
1985         TRACE("(%p)->(IID_IObjectSafety %p)\n", This, ppv);
1986         *ppv = &This->IObjectSafety_iface;
1987     }else if(IsEqualGUID(&IID_IProvideClassInfo, riid)) {
1988         TRACE("(%p)->(IID_IProvideClassInfo, %p)\n", This, ppv);
1989         *ppv = &This->IProvideClassInfo_iface;
1990     }else {
1991         return FALSE;
1992     }
1993
1994     if(*ppv)
1995         IUnknown_AddRef((IUnknown*)*ppv);
1996     return TRUE;
1997 }
1998
1999 static cp_static_data_t HTMLDocumentEvents_data = { HTMLDocumentEvents_tid, HTMLDocument_on_advise };
2000
2001 static void init_doc(HTMLDocument *doc, IUnknown *unk_impl, IDispatchEx *dispex)
2002 {
2003     doc->IHTMLDocument2_iface.lpVtbl = &HTMLDocumentVtbl;
2004     doc->IDispatchEx_iface.lpVtbl = &DocDispatchExVtbl;
2005     doc->ISupportErrorInfo_iface.lpVtbl = &SupportErrorInfoVtbl;
2006     doc->IProvideClassInfo_iface.lpVtbl = &ProvideClassInfoVtbl;
2007
2008     doc->unk_impl = unk_impl;
2009     doc->dispex = dispex;
2010     doc->task_magic = get_task_target_magic();
2011
2012     HTMLDocument_HTMLDocument3_Init(doc);
2013     HTMLDocument_HTMLDocument5_Init(doc);
2014     HTMLDocument_Persist_Init(doc);
2015     HTMLDocument_OleCmd_Init(doc);
2016     HTMLDocument_OleObj_Init(doc);
2017     HTMLDocument_View_Init(doc);
2018     HTMLDocument_Window_Init(doc);
2019     HTMLDocument_Service_Init(doc);
2020     HTMLDocument_Hlink_Init(doc);
2021
2022     ConnectionPointContainer_Init(&doc->cp_container, (IUnknown*)&doc->IHTMLDocument2_iface);
2023     ConnectionPoint_Init(&doc->cp_dispatch, &doc->cp_container, &IID_IDispatch, &HTMLDocumentEvents_data);
2024     ConnectionPoint_Init(&doc->cp_propnotif, &doc->cp_container, &IID_IPropertyNotifySink, NULL);
2025     ConnectionPoint_Init(&doc->cp_htmldocevents, &doc->cp_container, &DIID_HTMLDocumentEvents, &HTMLDocumentEvents_data);
2026     ConnectionPoint_Init(&doc->cp_htmldocevents2, &doc->cp_container, &DIID_HTMLDocumentEvents2, NULL);
2027 }
2028
2029 static void destroy_htmldoc(HTMLDocument *This)
2030 {
2031     remove_target_tasks(This->task_magic);
2032
2033     ConnectionPointContainer_Destroy(&This->cp_container);
2034 }
2035
2036 static inline HTMLDocumentNode *impl_from_HTMLDOMNode(HTMLDOMNode *iface)
2037 {
2038     return CONTAINING_RECORD(iface, HTMLDocumentNode, node);
2039 }
2040
2041 static HRESULT HTMLDocumentNode_QI(HTMLDOMNode *iface, REFIID riid, void **ppv)
2042 {
2043     HTMLDocumentNode *This = impl_from_HTMLDOMNode(iface);
2044
2045     if(htmldoc_qi(&This->basedoc, riid, ppv))
2046         return *ppv ? S_OK : E_NOINTERFACE;
2047
2048     if(IsEqualGUID(&IID_IInternetHostSecurityManager, riid)) {
2049         TRACE("(%p)->(IID_IInternetHostSecurityManager %p)\n", This, ppv);
2050         *ppv = &This->IInternetHostSecurityManager_iface;
2051     }else {
2052         return HTMLDOMNode_QI(&This->node, riid, ppv);
2053     }
2054
2055     IUnknown_AddRef((IUnknown*)*ppv);
2056     return S_OK;
2057 }
2058
2059 static void HTMLDocumentNode_destructor(HTMLDOMNode *iface)
2060 {
2061     HTMLDocumentNode *This = impl_from_HTMLDOMNode(iface);
2062     unsigned i;
2063
2064     for(i=0; i < This->elem_vars_cnt; i++)
2065         heap_free(This->elem_vars[i]);
2066     heap_free(This->elem_vars);
2067
2068     detach_events(This);
2069     if(This->body_event_target)
2070         release_event_target(This->body_event_target);
2071     if(This->catmgr)
2072         ICatInformation_Release(This->catmgr);
2073
2074     detach_selection(This);
2075     detach_ranges(This);
2076     release_nodes(This);
2077
2078     while(!list_empty(&This->plugin_hosts))
2079         detach_plugin_host(LIST_ENTRY(list_head(&This->plugin_hosts), PluginHost, entry));
2080
2081     if(This->nsdoc) {
2082         release_document_mutation(This);
2083         nsIDOMHTMLDocument_Release(This->nsdoc);
2084     }
2085
2086     heap_free(This->event_vector);
2087     destroy_htmldoc(&This->basedoc);
2088 }
2089
2090 static HRESULT HTMLDocumentNode_clone(HTMLDOMNode *iface, nsIDOMNode *nsnode, HTMLDOMNode **ret)
2091 {
2092     HTMLDocumentNode *This = impl_from_HTMLDOMNode(iface);
2093     FIXME("%p\n", This);
2094     return E_NOTIMPL;
2095 }
2096
2097 static const NodeImplVtbl HTMLDocumentNodeImplVtbl = {
2098     HTMLDocumentNode_QI,
2099     HTMLDocumentNode_destructor,
2100     HTMLDocumentNode_clone
2101 };
2102
2103 static HRESULT HTMLDocumentFragment_clone(HTMLDOMNode *iface, nsIDOMNode *nsnode, HTMLDOMNode **ret)
2104 {
2105     HTMLDocumentNode *This = impl_from_HTMLDOMNode(iface);
2106     HTMLDocumentNode *new_node;
2107     HRESULT hres;
2108
2109     hres = create_document_fragment(nsnode, This->node.doc, &new_node);
2110     if(FAILED(hres))
2111         return hres;
2112
2113     *ret = &new_node->node;
2114     return S_OK;
2115 }
2116
2117 static inline HTMLDocumentNode *impl_from_DispatchEx(DispatchEx *iface)
2118 {
2119     return CONTAINING_RECORD(iface, HTMLDocumentNode, node.dispex);
2120 }
2121
2122 static HRESULT HTMLDocumentNode_invoke(DispatchEx *dispex, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
2123         VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
2124 {
2125     HTMLDocumentNode *This = impl_from_DispatchEx(dispex);
2126     nsIDOMNodeList *node_list;
2127     nsAString name_str;
2128     nsIDOMNode *nsnode;
2129     HTMLDOMNode *node;
2130     unsigned i;
2131     nsresult nsres;
2132     HRESULT hres;
2133
2134     if(flags != DISPATCH_PROPERTYGET) {
2135         FIXME("unsupported flags %x\n", flags);
2136         return E_NOTIMPL;
2137     }
2138
2139     i = id - MSHTML_DISPID_CUSTOM_MIN;
2140
2141     if(!This->nsdoc || i >= This->elem_vars_cnt)
2142         return DISP_E_UNKNOWNNAME;
2143
2144     nsAString_InitDepend(&name_str, This->elem_vars[i]);
2145     nsres = nsIDOMHTMLDocument_GetElementsByName(This->nsdoc, &name_str, &node_list);
2146     nsAString_Finish(&name_str);
2147     if(NS_FAILED(nsres))
2148         return E_FAIL;
2149
2150     nsres = nsIDOMNodeList_Item(node_list, 0, &nsnode);
2151     nsIDOMNodeList_Release(node_list);
2152     if(NS_FAILED(nsres) || !nsnode)
2153         return DISP_E_UNKNOWNNAME;
2154
2155     hres = get_node(This, nsnode, TRUE, &node);
2156     if(FAILED(hres))
2157         return hres;
2158
2159     IHTMLDOMNode_AddRef(&node->IHTMLDOMNode_iface);
2160     V_VT(res) = VT_DISPATCH;
2161     V_DISPATCH(res) = (IDispatch*)&node->IHTMLDOMNode_iface;
2162     return S_OK;
2163 }
2164
2165
2166 static const dispex_static_data_vtbl_t HTMLDocumentNode_dispex_vtbl = {
2167     NULL,
2168     NULL,
2169     HTMLDocumentNode_invoke,
2170     NULL
2171 };
2172
2173 static const NodeImplVtbl HTMLDocumentFragmentImplVtbl = {
2174     HTMLDocumentNode_QI,
2175     HTMLDocumentNode_destructor,
2176     HTMLDocumentFragment_clone
2177 };
2178
2179 static const tid_t HTMLDocumentNode_iface_tids[] = {
2180     IHTMLDOMNode_tid,
2181     IHTMLDOMNode2_tid,
2182     IHTMLDocument2_tid,
2183     IHTMLDocument3_tid,
2184     IHTMLDocument4_tid,
2185     IHTMLDocument5_tid,
2186     0
2187 };
2188
2189 static dispex_static_data_t HTMLDocumentNode_dispex = {
2190     &HTMLDocumentNode_dispex_vtbl,
2191     DispHTMLDocument_tid,
2192     NULL,
2193     HTMLDocumentNode_iface_tids
2194 };
2195
2196 static HTMLDocumentNode *alloc_doc_node(HTMLDocumentObj *doc_obj, HTMLWindow *window)
2197 {
2198     HTMLDocumentNode *doc;
2199
2200     doc = heap_alloc_zero(sizeof(HTMLDocumentNode));
2201     if(!doc)
2202         return NULL;
2203
2204     doc->ref = 1;
2205     doc->basedoc.doc_node = doc;
2206     doc->basedoc.doc_obj = doc_obj;
2207     doc->basedoc.window = window;
2208
2209     init_dispex(&doc->node.dispex, (IUnknown*)&doc->node.IHTMLDOMNode_iface,
2210             &HTMLDocumentNode_dispex);
2211     init_doc(&doc->basedoc, (IUnknown*)&doc->node.IHTMLDOMNode_iface,
2212             &doc->node.dispex.IDispatchEx_iface);
2213     HTMLDocumentNode_SecMgr_Init(doc);
2214
2215     list_init(&doc->bindings);
2216     list_init(&doc->selection_list);
2217     list_init(&doc->range_list);
2218     list_init(&doc->plugin_hosts);
2219
2220     return doc;
2221 }
2222
2223 HRESULT create_doc_from_nsdoc(nsIDOMHTMLDocument *nsdoc, HTMLDocumentObj *doc_obj, HTMLWindow *window, HTMLDocumentNode **ret)
2224 {
2225     HTMLDocumentNode *doc;
2226
2227     doc = alloc_doc_node(doc_obj, window);
2228     if(!doc)
2229         return E_OUTOFMEMORY;
2230
2231     if(!doc_obj->basedoc.window || window == doc_obj->basedoc.window)
2232         doc->basedoc.cp_container.forward_container = &doc_obj->basedoc.cp_container;
2233
2234     nsIDOMHTMLDocument_AddRef(nsdoc);
2235     doc->nsdoc = nsdoc;
2236     init_document_mutation(doc);
2237     doc_init_events(doc);
2238
2239     HTMLDOMNode_Init(doc, &doc->node, (nsIDOMNode*)nsdoc);
2240     doc->node.vtbl = &HTMLDocumentNodeImplVtbl;
2241     doc->node.cp_container = &doc->basedoc.cp_container;
2242
2243     *ret = doc;
2244     return S_OK;
2245 }
2246
2247 HRESULT create_document_fragment(nsIDOMNode *nsnode, HTMLDocumentNode *doc_node, HTMLDocumentNode **ret)
2248 {
2249     HTMLDocumentNode *doc_frag;
2250
2251     doc_frag = alloc_doc_node(doc_node->basedoc.doc_obj, doc_node->basedoc.window);
2252     if(!doc_frag)
2253         return E_OUTOFMEMORY;
2254
2255     HTMLDOMNode_Init(doc_node, &doc_frag->node, nsnode);
2256     doc_frag->node.vtbl = &HTMLDocumentFragmentImplVtbl;
2257     doc_frag->node.cp_container = &doc_frag->basedoc.cp_container;
2258
2259     htmldoc_addref(&doc_frag->basedoc);
2260     *ret = doc_frag;
2261     return S_OK;
2262 }
2263
2264 /**********************************************************
2265  * ICustomDoc implementation
2266  */
2267
2268 static inline HTMLDocumentObj *impl_from_ICustomDoc(ICustomDoc *iface)
2269 {
2270     return CONTAINING_RECORD(iface, HTMLDocumentObj, ICustomDoc_iface);
2271 }
2272
2273 static HRESULT WINAPI CustomDoc_QueryInterface(ICustomDoc *iface, REFIID riid, void **ppv)
2274 {
2275     HTMLDocumentObj *This = impl_from_ICustomDoc(iface);
2276
2277     if(htmldoc_qi(&This->basedoc, riid, ppv))
2278         return *ppv ? S_OK : E_NOINTERFACE;
2279
2280     if(IsEqualGUID(&IID_ICustomDoc, riid)) {
2281         TRACE("(%p)->(IID_ICustomDoc %p)\n", This, ppv);
2282         *ppv = &This->ICustomDoc_iface;
2283     }else if(IsEqualGUID(&IID_ITargetContainer, riid)) {
2284         TRACE("(%p)->(IID_ITargetContainer %p)\n", This, ppv);
2285         *ppv = &This->ITargetContainer_iface;
2286     }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
2287         return *ppv ? S_OK : E_NOINTERFACE;
2288     }else {
2289         FIXME("Unimplemented interface %s\n", debugstr_guid(riid));
2290         *ppv = NULL;
2291         return E_NOINTERFACE;
2292     }
2293
2294     IUnknown_AddRef((IUnknown*)*ppv);
2295     return S_OK;
2296 }
2297
2298 static ULONG WINAPI CustomDoc_AddRef(ICustomDoc *iface)
2299 {
2300     HTMLDocumentObj *This = impl_from_ICustomDoc(iface);
2301     ULONG ref = InterlockedIncrement(&This->ref);
2302
2303     TRACE("(%p) ref = %u\n", This, ref);
2304
2305     return ref;
2306 }
2307
2308 static ULONG WINAPI CustomDoc_Release(ICustomDoc *iface)
2309 {
2310     HTMLDocumentObj *This = impl_from_ICustomDoc(iface);
2311     ULONG ref = InterlockedDecrement(&This->ref);
2312
2313     TRACE("(%p) ref = %u\n", This, ref);
2314
2315     if(!ref) {
2316         if(This->basedoc.doc_node) {
2317             This->basedoc.doc_node->basedoc.doc_obj = NULL;
2318             htmldoc_release(&This->basedoc.doc_node->basedoc);
2319         }
2320         if(This->basedoc.window) {
2321             This->basedoc.window->doc_obj = NULL;
2322             IHTMLWindow2_Release(&This->basedoc.window->IHTMLWindow2_iface);
2323         }
2324         if(This->basedoc.advise_holder)
2325             IOleAdviseHolder_Release(This->basedoc.advise_holder);
2326
2327         if(This->view_sink)
2328             IAdviseSink_Release(This->view_sink);
2329         if(This->client)
2330             IOleObject_SetClientSite(&This->basedoc.IOleObject_iface, NULL);
2331         if(This->hostui)
2332             ICustomDoc_SetUIHandler(&This->ICustomDoc_iface, NULL);
2333         if(This->in_place_active)
2334             IOleInPlaceObjectWindowless_InPlaceDeactivate(&This->basedoc.IOleInPlaceObjectWindowless_iface);
2335         if(This->ipsite)
2336             IOleDocumentView_SetInPlaceSite(&This->basedoc.IOleDocumentView_iface, NULL);
2337         if(This->undomgr)
2338             IOleUndoManager_Release(This->undomgr);
2339         if(This->tooltips_hwnd)
2340             DestroyWindow(This->tooltips_hwnd);
2341
2342         if(This->hwnd)
2343             DestroyWindow(This->hwnd);
2344         heap_free(This->mime);
2345
2346         destroy_htmldoc(&This->basedoc);
2347         release_dispex(&This->dispex);
2348
2349         if(This->nscontainer)
2350             NSContainer_Release(This->nscontainer);
2351         heap_free(This);
2352     }
2353
2354     return ref;
2355 }
2356
2357 static HRESULT WINAPI CustomDoc_SetUIHandler(ICustomDoc *iface, IDocHostUIHandler *pUIHandler)
2358 {
2359     HTMLDocumentObj *This = impl_from_ICustomDoc(iface);
2360     IOleCommandTarget *cmdtrg;
2361     HRESULT hres;
2362
2363     TRACE("(%p)->(%p)\n", This, pUIHandler);
2364
2365     if(This->custom_hostui && This->hostui == pUIHandler)
2366         return S_OK;
2367
2368     This->custom_hostui = TRUE;
2369
2370     if(This->hostui)
2371         IDocHostUIHandler_Release(This->hostui);
2372     if(pUIHandler)
2373         IDocHostUIHandler_AddRef(pUIHandler);
2374     This->hostui = pUIHandler;
2375     if(!pUIHandler)
2376         return S_OK;
2377
2378     hres = IDocHostUIHandler_QueryInterface(pUIHandler, &IID_IOleCommandTarget, (void**)&cmdtrg);
2379     if(SUCCEEDED(hres)) {
2380         FIXME("custom UI handler supports IOleCommandTarget\n");
2381         IOleCommandTarget_Release(cmdtrg);
2382     }
2383
2384     return S_OK;
2385 }
2386
2387 static const ICustomDocVtbl CustomDocVtbl = {
2388     CustomDoc_QueryInterface,
2389     CustomDoc_AddRef,
2390     CustomDoc_Release,
2391     CustomDoc_SetUIHandler
2392 };
2393
2394 static const tid_t HTMLDocumentObj_iface_tids[] = {
2395     IHTMLDocument2_tid,
2396     IHTMLDocument3_tid,
2397     IHTMLDocument4_tid,
2398     IHTMLDocument5_tid,
2399     0
2400 };
2401 static dispex_static_data_t HTMLDocumentObj_dispex = {
2402     NULL,
2403     DispHTMLDocument_tid,
2404     NULL,
2405     HTMLDocumentObj_iface_tids
2406 };
2407
2408 HRESULT HTMLDocument_Create(IUnknown *pUnkOuter, REFIID riid, void** ppvObject)
2409 {
2410     HTMLDocumentObj *doc;
2411     nsIDOMWindow *nswindow = NULL;
2412     nsresult nsres;
2413     HRESULT hres;
2414
2415     TRACE("(%p %s %p)\n", pUnkOuter, debugstr_guid(riid), ppvObject);
2416
2417     doc = heap_alloc_zero(sizeof(HTMLDocumentObj));
2418     if(!doc)
2419         return E_OUTOFMEMORY;
2420
2421     init_dispex(&doc->dispex, (IUnknown*)&doc->ICustomDoc_iface, &HTMLDocumentObj_dispex);
2422     init_doc(&doc->basedoc, (IUnknown*)&doc->ICustomDoc_iface, &doc->dispex.IDispatchEx_iface);
2423     TargetContainer_Init(doc);
2424
2425     doc->ICustomDoc_iface.lpVtbl = &CustomDocVtbl;
2426     doc->ref = 1;
2427     doc->basedoc.doc_obj = doc;
2428
2429     doc->usermode = UNKNOWN_USERMODE;
2430
2431     init_binding_ui(doc);
2432
2433     hres = create_nscontainer(doc, &doc->nscontainer);
2434     if(FAILED(hres)) {
2435         ERR("Failed to init Gecko, returning CLASS_E_CLASSNOTAVAILABLE\n");
2436         htmldoc_release(&doc->basedoc);
2437         return hres;
2438     }
2439
2440     hres = htmldoc_query_interface(&doc->basedoc, riid, ppvObject);
2441     htmldoc_release(&doc->basedoc);
2442     if(FAILED(hres))
2443         return hres;
2444
2445
2446     nsres = nsIWebBrowser_GetContentDOMWindow(doc->nscontainer->webbrowser, &nswindow);
2447     if(NS_FAILED(nsres))
2448         ERR("GetContentDOMWindow failed: %08x\n", nsres);
2449
2450     hres = HTMLWindow_Create(doc, nswindow, NULL /* FIXME */, &doc->basedoc.window);
2451     if(nswindow)
2452         nsIDOMWindow_Release(nswindow);
2453     if(FAILED(hres)) {
2454         htmldoc_release(&doc->basedoc);
2455         return hres;
2456     }
2457
2458     if(!doc->basedoc.doc_node && doc->basedoc.window->doc) {
2459         doc->basedoc.doc_node = doc->basedoc.window->doc;
2460         htmldoc_addref(&doc->basedoc.doc_node->basedoc);
2461     }
2462
2463     get_thread_hwnd();
2464
2465     return S_OK;
2466 }