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