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