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