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