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