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