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