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