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