mshtml: Added more events tests.
[wine] / dlls / mshtml / htmldoc3.c
1 /*
2  * Copyright 2005 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 "ole2.h"
30
31 #include "wine/debug.h"
32
33 #include "mshtml_private.h"
34 #include "htmlevent.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
37
38 static inline HTMLDocument *impl_from_IHTMLDocument3(IHTMLDocument3 *iface)
39 {
40     return CONTAINING_RECORD(iface, HTMLDocument, IHTMLDocument3_iface);
41 }
42
43 static HRESULT WINAPI HTMLDocument3_QueryInterface(IHTMLDocument3 *iface,
44                                                   REFIID riid, void **ppv)
45 {
46     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
47     return htmldoc_query_interface(This, riid, ppv);
48 }
49
50 static ULONG WINAPI HTMLDocument3_AddRef(IHTMLDocument3 *iface)
51 {
52     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
53     return htmldoc_addref(This);
54 }
55
56 static ULONG WINAPI HTMLDocument3_Release(IHTMLDocument3 *iface)
57 {
58     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
59     return htmldoc_release(This);
60 }
61
62 static HRESULT WINAPI HTMLDocument3_GetTypeInfoCount(IHTMLDocument3 *iface, UINT *pctinfo)
63 {
64     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
65     return IDispatchEx_GetTypeInfoCount(&This->IDispatchEx_iface, pctinfo);
66 }
67
68 static HRESULT WINAPI HTMLDocument3_GetTypeInfo(IHTMLDocument3 *iface, UINT iTInfo,
69                                                 LCID lcid, ITypeInfo **ppTInfo)
70 {
71     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
72     return IDispatchEx_GetTypeInfo(&This->IDispatchEx_iface, iTInfo, lcid, ppTInfo);
73 }
74
75 static HRESULT WINAPI HTMLDocument3_GetIDsOfNames(IHTMLDocument3 *iface, REFIID riid,
76                                                 LPOLESTR *rgszNames, UINT cNames,
77                                                 LCID lcid, DISPID *rgDispId)
78 {
79     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
80     return IDispatchEx_GetIDsOfNames(&This->IDispatchEx_iface, riid, rgszNames, cNames, lcid,
81             rgDispId);
82 }
83
84 static HRESULT WINAPI HTMLDocument3_Invoke(IHTMLDocument3 *iface, DISPID dispIdMember,
85                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
86                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
87 {
88     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
89     return IDispatchEx_Invoke(&This->IDispatchEx_iface, dispIdMember, riid, lcid, wFlags,
90             pDispParams, pVarResult, pExcepInfo, puArgErr);
91 }
92
93 static HRESULT WINAPI HTMLDocument3_releaseCapture(IHTMLDocument3 *iface)
94 {
95     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
96     FIXME("(%p)\n", This);
97     return E_NOTIMPL;
98 }
99
100 static HRESULT WINAPI HTMLDocument3_recalc(IHTMLDocument3 *iface, VARIANT_BOOL fForce)
101 {
102     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
103     FIXME("(%p)->(%x)\n", This, fForce);
104     return E_NOTIMPL;
105 }
106
107 static HRESULT WINAPI HTMLDocument3_createTextNode(IHTMLDocument3 *iface, BSTR text,
108                                                    IHTMLDOMNode **newTextNode)
109 {
110     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
111     nsIDOMText *nstext;
112     HTMLDOMNode *node;
113     nsAString text_str;
114     nsresult nsres;
115     HRESULT hres;
116
117     TRACE("(%p)->(%s %p)\n", This, debugstr_w(text), newTextNode);
118
119     if(!This->doc_node->nsdoc) {
120         WARN("NULL nsdoc\n");
121         return E_UNEXPECTED;
122     }
123
124     nsAString_InitDepend(&text_str, text);
125     nsres = nsIDOMHTMLDocument_CreateTextNode(This->doc_node->nsdoc, &text_str, &nstext);
126     nsAString_Finish(&text_str);
127     if(NS_FAILED(nsres)) {
128         ERR("CreateTextNode failed: %08x\n", nsres);
129         return E_FAIL;
130     }
131
132     hres = HTMLDOMTextNode_Create(This->doc_node, (nsIDOMNode*)nstext, &node);
133     nsIDOMText_Release(nstext);
134     if(FAILED(hres))
135         return hres;
136
137     *newTextNode = &node->IHTMLDOMNode_iface;
138     return S_OK;
139 }
140
141 static HRESULT WINAPI HTMLDocument3_get_documentElement(IHTMLDocument3 *iface, IHTMLElement **p)
142 {
143     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
144     nsIDOMElement *nselem = NULL;
145     HTMLDOMNode *node;
146     nsresult nsres;
147     HRESULT hres;
148
149     TRACE("(%p)->(%p)\n", This, p);
150
151     if(This->window->readystate == READYSTATE_UNINITIALIZED) {
152         *p = NULL;
153         return S_OK;
154     }
155
156     if(!This->doc_node->nsdoc) {
157         WARN("NULL nsdoc\n");
158         return E_UNEXPECTED;
159     }
160
161     nsres = nsIDOMHTMLDocument_GetDocumentElement(This->doc_node->nsdoc, &nselem);
162     if(NS_FAILED(nsres)) {
163         ERR("GetDocumentElement failed: %08x\n", nsres);
164         return E_FAIL;
165     }
166
167     if(!nselem) {
168         *p = NULL;
169         return S_OK;
170     }
171
172     hres = get_node(This->doc_node, (nsIDOMNode *)nselem, TRUE, &node);
173     nsIDOMElement_Release(nselem);
174     if(FAILED(hres))
175         return hres;
176
177     hres = IHTMLDOMNode_QueryInterface(&node->IHTMLDOMNode_iface, &IID_IHTMLElement, (void**)p);
178     node_release(node);
179     return hres;
180 }
181
182 static HRESULT WINAPI HTMLDocument3_uniqueID(IHTMLDocument3 *iface, BSTR *p)
183 {
184     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
185     FIXME("(%p)->(%p)\n", This, p);
186     return E_NOTIMPL;
187 }
188
189 static HRESULT WINAPI HTMLDocument3_attachEvent(IHTMLDocument3 *iface, BSTR event,
190                                                 IDispatch* pDisp, VARIANT_BOOL *pfResult)
191 {
192     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
193
194     TRACE("(%p)->(%s %p %p)\n", This, debugstr_w(event), pDisp, pfResult);
195
196     return attach_event(&This->doc_node->node.event_target, This->doc_node->node.nsnode, This, event, pDisp, pfResult);
197 }
198
199 static HRESULT WINAPI HTMLDocument3_detachEvent(IHTMLDocument3 *iface, BSTR event,
200                                                 IDispatch *pDisp)
201 {
202     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
203
204     TRACE("(%p)->(%s %p)\n", This, debugstr_w(event), pDisp);
205
206     return detach_event(This->doc_node->node.event_target, This, event, pDisp);
207 }
208
209 static HRESULT WINAPI HTMLDocument3_put_onrowsdelete(IHTMLDocument3 *iface, VARIANT v)
210 {
211     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
212     FIXME("(%p)->()\n", This);
213     return E_NOTIMPL;
214 }
215
216 static HRESULT WINAPI HTMLDocument3_get_onrowsdelete(IHTMLDocument3 *iface, VARIANT *p)
217 {
218     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
219     FIXME("(%p)->(%p)\n", This, p);
220     return E_NOTIMPL;
221 }
222
223 static HRESULT WINAPI HTMLDocument3_put_onrowsinserted(IHTMLDocument3 *iface, VARIANT v)
224 {
225     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
226     FIXME("(%p)->()\n", This);
227     return E_NOTIMPL;
228 }
229
230 static HRESULT WINAPI HTMLDocument3_get_onrowsinserted(IHTMLDocument3 *iface, VARIANT *p)
231 {
232     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
233     FIXME("(%p)->(%p)\n", This, p);
234     return E_NOTIMPL;
235 }
236
237 static HRESULT WINAPI HTMLDocument3_put_oncellchange(IHTMLDocument3 *iface, VARIANT v)
238 {
239     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
240     FIXME("(%p)->()\n", This);
241     return E_NOTIMPL;
242 }
243
244 static HRESULT WINAPI HTMLDocument3_get_oncellchange(IHTMLDocument3 *iface, VARIANT *p)
245 {
246     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
247     FIXME("(%p)->(%p)\n", This, p);
248     return E_NOTIMPL;
249 }
250
251 static HRESULT WINAPI HTMLDocument3_put_ondatasetchanged(IHTMLDocument3 *iface, VARIANT v)
252 {
253     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
254     FIXME("(%p)->()\n", This);
255     return E_NOTIMPL;
256 }
257
258 static HRESULT WINAPI HTMLDocument3_get_ondatasetchanged(IHTMLDocument3 *iface, VARIANT *p)
259 {
260     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
261     FIXME("(%p)->(%p)\n", This, p);
262     return E_NOTIMPL;
263 }
264
265 static HRESULT WINAPI HTMLDocument3_put_ondataavailable(IHTMLDocument3 *iface, VARIANT v)
266 {
267     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
268     FIXME("(%p)->()\n", This);
269     return E_NOTIMPL;
270 }
271
272 static HRESULT WINAPI HTMLDocument3_get_ondataavailable(IHTMLDocument3 *iface, VARIANT *p)
273 {
274     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
275     FIXME("(%p)->(%p)\n", This, p);
276     return E_NOTIMPL;
277 }
278
279 static HRESULT WINAPI HTMLDocument3_put_ondatasetcomplete(IHTMLDocument3 *iface, VARIANT v)
280 {
281     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
282     FIXME("(%p)->()\n", This);
283     return E_NOTIMPL;
284 }
285
286 static HRESULT WINAPI HTMLDocument3_get_ondatasetcomplete(IHTMLDocument3 *iface, VARIANT *p)
287 {
288     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
289     FIXME("(%p)->(%p)\n", This, p);
290     return E_NOTIMPL;
291 }
292
293 static HRESULT WINAPI HTMLDocument3_put_onpropertychange(IHTMLDocument3 *iface, VARIANT v)
294 {
295     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
296     FIXME("(%p)->()\n", This);
297     return E_NOTIMPL;
298 }
299
300 static HRESULT WINAPI HTMLDocument3_get_onpropertychange(IHTMLDocument3 *iface, VARIANT *p)
301 {
302     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
303     FIXME("(%p)->(%p)\n", This, p);
304     return E_NOTIMPL;
305 }
306
307 static HRESULT WINAPI HTMLDocument3_put_dir(IHTMLDocument3 *iface, BSTR v)
308 {
309     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
310     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
311     return E_NOTIMPL;
312 }
313
314 static HRESULT WINAPI HTMLDocument3_get_dir(IHTMLDocument3 *iface, BSTR *p)
315 {
316     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
317     FIXME("(%p)->(%p)\n", This, p);
318     return E_NOTIMPL;
319 }
320
321 static HRESULT WINAPI HTMLDocument3_put_oncontextmenu(IHTMLDocument3 *iface, VARIANT v)
322 {
323     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
324
325     TRACE("(%p)->()\n", This);
326
327     return set_doc_event(This, EVENTID_CONTEXTMENU, &v);
328 }
329
330 static HRESULT WINAPI HTMLDocument3_get_oncontextmenu(IHTMLDocument3 *iface, VARIANT *p)
331 {
332     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
333
334     TRACE("(%p)->(%p)\n", This, p);
335
336     return get_doc_event(This, EVENTID_CONTEXTMENU, p);
337 }
338
339 static HRESULT WINAPI HTMLDocument3_put_onstop(IHTMLDocument3 *iface, VARIANT v)
340 {
341     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
342     FIXME("(%p)->()\n", This);
343     return E_NOTIMPL;
344 }
345
346 static HRESULT WINAPI HTMLDocument3_get_onstop(IHTMLDocument3 *iface, VARIANT *p)
347 {
348     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
349     FIXME("(%p)->(%p)\n", This, p);
350     return E_NOTIMPL;
351 }
352
353 static HRESULT WINAPI HTMLDocument3_createDocumentFragment(IHTMLDocument3 *iface,
354                                                            IHTMLDocument2 **ppNewDoc)
355 {
356     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
357     nsIDOMDocumentFragment *doc_frag;
358     HTMLDocumentNode *docnode;
359     nsresult nsres;
360     HRESULT hres;
361
362     TRACE("(%p)->(%p)\n", This, ppNewDoc);
363
364     if(!This->doc_node->nsdoc) {
365         FIXME("NULL nsdoc\n");
366         return E_NOTIMPL;
367     }
368
369     nsres = nsIDOMHTMLDocument_CreateDocumentFragment(This->doc_node->nsdoc, &doc_frag);
370     if(NS_FAILED(nsres)) {
371         ERR("CreateDocumentFragment failed: %08x\n", nsres);
372         return E_FAIL;
373     }
374
375     hres = create_document_fragment((nsIDOMNode*)doc_frag, This->doc_node, &docnode);
376     nsIDOMDocumentFragment_Release(doc_frag);
377     if(FAILED(hres))
378         return hres;
379
380     *ppNewDoc = &docnode->basedoc.IHTMLDocument2_iface;
381     return S_OK;
382 }
383
384 static HRESULT WINAPI HTMLDocument3_get_parentDocument(IHTMLDocument3 *iface,
385                                                        IHTMLDocument2 **p)
386 {
387     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
388     FIXME("(%p)->(%p)\n", This, p);
389     return E_NOTIMPL;
390 }
391
392 static HRESULT WINAPI HTMLDocument3_put_enableDownload(IHTMLDocument3 *iface,
393                                                        VARIANT_BOOL v)
394 {
395     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
396     FIXME("(%p)->(%x)\n", This, v);
397     return E_NOTIMPL;
398 }
399
400 static HRESULT WINAPI HTMLDocument3_get_enableDownload(IHTMLDocument3 *iface,
401                                                        VARIANT_BOOL *p)
402 {
403     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
404     FIXME("(%p)->(%p)\n", This, p);
405     return E_NOTIMPL;
406 }
407
408 static HRESULT WINAPI HTMLDocument3_put_baseUrl(IHTMLDocument3 *iface, BSTR v)
409 {
410     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
411     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
412     return E_NOTIMPL;
413 }
414
415 static HRESULT WINAPI HTMLDocument3_get_baseUrl(IHTMLDocument3 *iface, BSTR *p)
416 {
417     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
418     FIXME("(%p)->(%p)\n", This, p);
419     return E_NOTIMPL;
420 }
421
422 static HRESULT WINAPI HTMLDocument3_get_childNodes(IHTMLDocument3 *iface, IDispatch **p)
423 {
424     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
425     FIXME("(%p)->(%p)\n", This, p);
426     return E_NOTIMPL;
427 }
428
429 static HRESULT WINAPI HTMLDocument3_put_inheritStyleSheets(IHTMLDocument3 *iface,
430                                                            VARIANT_BOOL v)
431 {
432     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
433     FIXME("(%p)->()\n", This);
434     return E_NOTIMPL;
435 }
436
437 static HRESULT WINAPI HTMLDocument3_get_inheritStyleSheets(IHTMLDocument3 *iface,
438                                                            VARIANT_BOOL *p)
439 {
440     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
441     FIXME("(%p)->(%p)\n", This, p);
442     return E_NOTIMPL;
443 }
444
445 static HRESULT WINAPI HTMLDocument3_put_onbeforeeditfocus(IHTMLDocument3 *iface, VARIANT v)
446 {
447     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
448     FIXME("(%p)->()\n", This);
449     return E_NOTIMPL;
450 }
451
452 static HRESULT WINAPI HTMLDocument3_get_onbeforeeditfocus(IHTMLDocument3 *iface, VARIANT *p)
453 {
454     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
455     FIXME("(%p)->(%p)\n", This, p);
456     return E_NOTIMPL;
457 }
458
459 static HRESULT WINAPI HTMLDocument3_getElementsByName(IHTMLDocument3 *iface, BSTR v,
460                                                       IHTMLElementCollection **ppelColl)
461 {
462     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
463     FIXME("(%p)->(%s %p)\n", This, debugstr_w(v), ppelColl);
464     return E_NOTIMPL;
465 }
466
467
468 static HRESULT WINAPI HTMLDocument3_getElementById(IHTMLDocument3 *iface, BSTR v,
469                                                    IHTMLElement **pel)
470 {
471     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
472     nsIDOMElement *nselem;
473     HTMLDOMNode *node;
474     nsIDOMNode *nsnode, *nsnode_by_id, *nsnode_by_name;
475     nsIDOMNodeList *nsnode_list;
476     nsAString id_str;
477     nsresult nsres;
478     HRESULT hres;
479
480     TRACE("(%p)->(%s %p)\n", This, debugstr_w(v), pel);
481
482     if(!This->doc_node->nsdoc) {
483         WARN("NULL nsdoc\n");
484         return E_UNEXPECTED;
485     }
486
487     nsAString_InitDepend(&id_str, v);
488     /* get element by id attribute */
489     nsres = nsIDOMHTMLDocument_GetElementById(This->doc_node->nsdoc, &id_str, &nselem);
490     if(FAILED(nsres)) {
491         ERR("GetElementById failed: %08x\n", nsres);
492         nsAString_Finish(&id_str);
493         return E_FAIL;
494     }
495     nsnode_by_id = (nsIDOMNode*)nselem;
496
497     /* get first element by name attribute */
498     nsres = nsIDOMHTMLDocument_GetElementsByName(This->doc_node->nsdoc, &id_str, &nsnode_list);
499     nsAString_Finish(&id_str);
500     if(FAILED(nsres)) {
501         ERR("getElementsByName failed: %08x\n", nsres);
502         if(nsnode_by_id)
503             nsIDOMNode_Release(nsnode_by_id);
504         return E_FAIL;
505     }
506     nsIDOMNodeList_Item(nsnode_list, 0, &nsnode_by_name);
507     nsIDOMNodeList_Release(nsnode_list);
508
509
510     if(nsnode_by_name && nsnode_by_id) {
511         PRUint16 pos;
512
513         nsres = nsIDOMNode_CompareDocumentPosition(nsnode_by_name, nsnode_by_id, &pos);
514         if(NS_FAILED(nsres)) {
515             FIXME("CompareDocumentPosition failed: 0x%08x\n", nsres);
516             nsIDOMNode_Release(nsnode_by_name);
517             nsIDOMNode_Release(nsnode_by_id);
518             return E_FAIL;
519         }
520
521         TRACE("CompareDocumentPosition gave: 0x%x\n", pos);
522         if(pos & (DOCUMENT_POSITION_PRECEDING | DOCUMENT_POSITION_CONTAINS)) {
523             nsnode = nsnode_by_id;
524             nsIDOMNode_Release(nsnode_by_name);
525         }else {
526             nsnode = nsnode_by_name;
527             nsIDOMNode_Release(nsnode_by_id);
528         }
529     }else
530         nsnode = nsnode_by_name ? nsnode_by_name : nsnode_by_id;
531
532     if(nsnode) {
533         hres = get_node(This->doc_node, nsnode, TRUE, &node);
534         nsIDOMNode_Release(nsnode);
535
536         if(SUCCEEDED(hres)) {
537             hres = IHTMLDOMNode_QueryInterface(&node->IHTMLDOMNode_iface, &IID_IHTMLElement, (void**)pel);
538             node_release(node);
539         }
540     }else {
541         *pel = NULL;
542         hres = S_OK;
543     }
544
545     return hres;
546 }
547
548
549 static HRESULT WINAPI HTMLDocument3_getElementsByTagName(IHTMLDocument3 *iface, BSTR v,
550                                                          IHTMLElementCollection **pelColl)
551 {
552     HTMLDocument *This = impl_from_IHTMLDocument3(iface);
553     nsIDOMNodeList *nslist;
554     nsAString id_str, ns_str;
555     nsresult nsres;
556     static const WCHAR str[] = {'*',0};
557
558     TRACE("(%p)->(%s %p)\n", This, debugstr_w(v), pelColl);
559
560     if(!This->doc_node->nsdoc) {
561         WARN("NULL nsdoc\n");
562         return E_UNEXPECTED;
563     }
564
565     nsAString_InitDepend(&id_str, v);
566     nsAString_InitDepend(&ns_str, str);
567     nsres = nsIDOMHTMLDocument_GetElementsByTagNameNS(This->doc_node->nsdoc, &ns_str, &id_str, &nslist);
568     nsAString_Finish(&id_str);
569     nsAString_Finish(&ns_str);
570     if(FAILED(nsres)) {
571         ERR("GetElementByName failed: %08x\n", nsres);
572         return E_FAIL;
573     }
574
575     *pelColl = create_collection_from_nodelist(This->doc_node, nslist);
576     nsIDOMNodeList_Release(nslist);
577
578     return S_OK;
579 }
580
581 static const IHTMLDocument3Vtbl HTMLDocument3Vtbl = {
582     HTMLDocument3_QueryInterface,
583     HTMLDocument3_AddRef,
584     HTMLDocument3_Release,
585     HTMLDocument3_GetTypeInfoCount,
586     HTMLDocument3_GetTypeInfo,
587     HTMLDocument3_GetIDsOfNames,
588     HTMLDocument3_Invoke,
589     HTMLDocument3_releaseCapture,
590     HTMLDocument3_recalc,
591     HTMLDocument3_createTextNode,
592     HTMLDocument3_get_documentElement,
593     HTMLDocument3_uniqueID,
594     HTMLDocument3_attachEvent,
595     HTMLDocument3_detachEvent,
596     HTMLDocument3_put_onrowsdelete,
597     HTMLDocument3_get_onrowsdelete,
598     HTMLDocument3_put_onrowsinserted,
599     HTMLDocument3_get_onrowsinserted,
600     HTMLDocument3_put_oncellchange,
601     HTMLDocument3_get_oncellchange,
602     HTMLDocument3_put_ondatasetchanged,
603     HTMLDocument3_get_ondatasetchanged,
604     HTMLDocument3_put_ondataavailable,
605     HTMLDocument3_get_ondataavailable,
606     HTMLDocument3_put_ondatasetcomplete,
607     HTMLDocument3_get_ondatasetcomplete,
608     HTMLDocument3_put_onpropertychange,
609     HTMLDocument3_get_onpropertychange,
610     HTMLDocument3_put_dir,
611     HTMLDocument3_get_dir,
612     HTMLDocument3_put_oncontextmenu,
613     HTMLDocument3_get_oncontextmenu,
614     HTMLDocument3_put_onstop,
615     HTMLDocument3_get_onstop,
616     HTMLDocument3_createDocumentFragment,
617     HTMLDocument3_get_parentDocument,
618     HTMLDocument3_put_enableDownload,
619     HTMLDocument3_get_enableDownload,
620     HTMLDocument3_put_baseUrl,
621     HTMLDocument3_get_baseUrl,
622     HTMLDocument3_get_childNodes,
623     HTMLDocument3_put_inheritStyleSheets,
624     HTMLDocument3_get_inheritStyleSheets,
625     HTMLDocument3_put_onbeforeeditfocus,
626     HTMLDocument3_get_onbeforeeditfocus,
627     HTMLDocument3_getElementsByName,
628     HTMLDocument3_getElementById,
629     HTMLDocument3_getElementsByTagName
630 };
631
632 static inline HTMLDocument *impl_from_IHTMLDocument4(IHTMLDocument4 *iface)
633 {
634     return CONTAINING_RECORD(iface, HTMLDocument, IHTMLDocument4_iface);
635 }
636
637 static HRESULT WINAPI HTMLDocument4_QueryInterface(IHTMLDocument4 *iface,
638                                                    REFIID riid, void **ppv)
639 {
640     HTMLDocument *This = impl_from_IHTMLDocument4(iface);
641     return htmldoc_query_interface(This, riid, ppv);
642 }
643
644 static ULONG WINAPI HTMLDocument4_AddRef(IHTMLDocument4 *iface)
645 {
646     HTMLDocument *This = impl_from_IHTMLDocument4(iface);
647     return htmldoc_addref(This);
648 }
649
650 static ULONG WINAPI HTMLDocument4_Release(IHTMLDocument4 *iface)
651 {
652     HTMLDocument *This = impl_from_IHTMLDocument4(iface);
653     return htmldoc_release(This);
654 }
655
656 static HRESULT WINAPI HTMLDocument4_GetTypeInfoCount(IHTMLDocument4 *iface, UINT *pctinfo)
657 {
658     HTMLDocument *This = impl_from_IHTMLDocument4(iface);
659     return IDispatchEx_GetTypeInfoCount(&This->IDispatchEx_iface, pctinfo);
660 }
661
662 static HRESULT WINAPI HTMLDocument4_GetTypeInfo(IHTMLDocument4 *iface, UINT iTInfo,
663                                                 LCID lcid, ITypeInfo **ppTInfo)
664 {
665     HTMLDocument *This = impl_from_IHTMLDocument4(iface);
666     return IDispatchEx_GetTypeInfo(&This->IDispatchEx_iface, iTInfo, lcid, ppTInfo);
667 }
668
669 static HRESULT WINAPI HTMLDocument4_GetIDsOfNames(IHTMLDocument4 *iface, REFIID riid,
670                                                 LPOLESTR *rgszNames, UINT cNames,
671                                                 LCID lcid, DISPID *rgDispId)
672 {
673     HTMLDocument *This = impl_from_IHTMLDocument4(iface);
674     return IDispatchEx_GetIDsOfNames(&This->IDispatchEx_iface, riid, rgszNames, cNames, lcid,
675             rgDispId);
676 }
677
678 static HRESULT WINAPI HTMLDocument4_Invoke(IHTMLDocument4 *iface, DISPID dispIdMember,
679                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
680                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
681 {
682     HTMLDocument *This = impl_from_IHTMLDocument4(iface);
683     return IDispatchEx_Invoke(&This->IDispatchEx_iface, dispIdMember, riid, lcid, wFlags,
684             pDispParams, pVarResult, pExcepInfo, puArgErr);
685 }
686
687 static HRESULT WINAPI HTMLDocument4_focus(IHTMLDocument4 *iface)
688 {
689     HTMLDocument *This = impl_from_IHTMLDocument4(iface);
690     nsIDOMHTMLElement *nsbody;
691     nsresult nsres;
692
693     TRACE("(%p)->()\n", This);
694
695     nsres = nsIDOMHTMLDocument_GetBody(This->doc_node->nsdoc, &nsbody);
696     if(NS_FAILED(nsres) || !nsbody) {
697         ERR("GetBody failed: %08x\n", nsres);
698         return E_FAIL;
699     }
700
701     nsres = nsIDOMHTMLElement_Focus(nsbody);
702     nsIDOMHTMLElement_Release(nsbody);
703     if(NS_FAILED(nsres)) {
704         ERR("Focus failed: %08x\n", nsres);
705         return E_FAIL;
706     }
707
708     return S_OK;
709 }
710
711 static HRESULT WINAPI HTMLDocument4_hasFocus(IHTMLDocument4 *iface, VARIANT_BOOL *pfFocus)
712 {
713     HTMLDocument *This = impl_from_IHTMLDocument4(iface);
714     FIXME("(%p)->(%p)\n", This, pfFocus);
715     return E_NOTIMPL;
716 }
717
718 static HRESULT WINAPI HTMLDocument4_put_onselectionchange(IHTMLDocument4 *iface, VARIANT v)
719 {
720     HTMLDocument *This = impl_from_IHTMLDocument4(iface);
721     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
722     return E_NOTIMPL;
723 }
724
725 static HRESULT WINAPI HTMLDocument4_get_onselectionchange(IHTMLDocument4 *iface, VARIANT *p)
726 {
727     HTMLDocument *This = impl_from_IHTMLDocument4(iface);
728     FIXME("(%p)->(%p)\n", This, p);
729     return E_NOTIMPL;
730 }
731
732 static HRESULT WINAPI HTMLDocument4_get_namespace(IHTMLDocument4 *iface, IDispatch **p)
733 {
734     HTMLDocument *This = impl_from_IHTMLDocument4(iface);
735     FIXME("(%p)->(%p)\n", This, p);
736     return E_NOTIMPL;
737 }
738
739 static HRESULT WINAPI HTMLDocument4_createDocumentFromUrl(IHTMLDocument4 *iface, BSTR bstrUrl,
740         BSTR bstrOptions, IHTMLDocument2 **newDoc)
741 {
742     HTMLDocument *This = impl_from_IHTMLDocument4(iface);
743     FIXME("(%p)->(%s %s %p)\n", This, debugstr_w(bstrUrl), debugstr_w(bstrOptions), newDoc);
744     return E_NOTIMPL;
745 }
746
747 static HRESULT WINAPI HTMLDocument4_put_media(IHTMLDocument4 *iface, BSTR v)
748 {
749     HTMLDocument *This = impl_from_IHTMLDocument4(iface);
750     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
751     return E_NOTIMPL;
752 }
753
754 static HRESULT WINAPI HTMLDocument4_get_media(IHTMLDocument4 *iface, BSTR *p)
755 {
756     HTMLDocument *This = impl_from_IHTMLDocument4(iface);
757     FIXME("(%p)->(%p)\n", This, p);
758     return E_NOTIMPL;
759 }
760
761 static HRESULT WINAPI HTMLDocument4_createEventObject(IHTMLDocument4 *iface,
762         VARIANT *pvarEventObject, IHTMLEventObj **ppEventObj)
763 {
764     HTMLDocument *This = impl_from_IHTMLDocument4(iface);
765
766     TRACE("(%p)->(%s %p)\n", This, debugstr_variant(pvarEventObject), ppEventObj);
767
768     if(pvarEventObject && V_VT(pvarEventObject) != VT_ERROR && V_VT(pvarEventObject) != VT_EMPTY) {
769         FIXME("unsupported pvarEventObject %s\n", debugstr_variant(pvarEventObject));
770         return E_NOTIMPL;
771     }
772
773     return create_event_obj(ppEventObj);
774 }
775
776 static HRESULT WINAPI HTMLDocument4_fireEvent(IHTMLDocument4 *iface, BSTR bstrEventName,
777         VARIANT *pvarEventObject, VARIANT_BOOL *pfCanceled)
778 {
779     HTMLDocument *This = impl_from_IHTMLDocument4(iface);
780     FIXME("(%p)->(%s %p %p)\n", This, debugstr_w(bstrEventName), pvarEventObject, pfCanceled);
781     return E_NOTIMPL;
782 }
783
784 static HRESULT WINAPI HTMLDocument4_createRenderStyle(IHTMLDocument4 *iface, BSTR v,
785         IHTMLRenderStyle **ppIHTMLRenderStyle)
786 {
787     HTMLDocument *This = impl_from_IHTMLDocument4(iface);
788     FIXME("(%p)->(%s %p)\n", This, debugstr_w(v), ppIHTMLRenderStyle);
789     return E_NOTIMPL;
790 }
791
792 static HRESULT WINAPI HTMLDocument4_put_oncontrolselect(IHTMLDocument4 *iface, VARIANT v)
793 {
794     HTMLDocument *This = impl_from_IHTMLDocument4(iface);
795     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
796     return E_NOTIMPL;
797 }
798
799 static HRESULT WINAPI HTMLDocument4_get_oncontrolselect(IHTMLDocument4 *iface, VARIANT *p)
800 {
801     HTMLDocument *This = impl_from_IHTMLDocument4(iface);
802     FIXME("(%p)->(%p)\n", This, p);
803     return E_NOTIMPL;
804 }
805
806 static HRESULT WINAPI HTMLDocument4_get_URLEncoded(IHTMLDocument4 *iface, BSTR *p)
807 {
808     HTMLDocument *This = impl_from_IHTMLDocument4(iface);
809     FIXME("(%p)->(%p)\n", This, p);
810     return E_NOTIMPL;
811 }
812
813 static const IHTMLDocument4Vtbl HTMLDocument4Vtbl = {
814     HTMLDocument4_QueryInterface,
815     HTMLDocument4_AddRef,
816     HTMLDocument4_Release,
817     HTMLDocument4_GetTypeInfoCount,
818     HTMLDocument4_GetTypeInfo,
819     HTMLDocument4_GetIDsOfNames,
820     HTMLDocument4_Invoke,
821     HTMLDocument4_focus,
822     HTMLDocument4_hasFocus,
823     HTMLDocument4_put_onselectionchange,
824     HTMLDocument4_get_onselectionchange,
825     HTMLDocument4_get_namespace,
826     HTMLDocument4_createDocumentFromUrl,
827     HTMLDocument4_put_media,
828     HTMLDocument4_get_media,
829     HTMLDocument4_createEventObject,
830     HTMLDocument4_fireEvent,
831     HTMLDocument4_createRenderStyle,
832     HTMLDocument4_put_oncontrolselect,
833     HTMLDocument4_get_oncontrolselect,
834     HTMLDocument4_get_URLEncoded
835 };
836
837 void HTMLDocument_HTMLDocument3_Init(HTMLDocument *This)
838 {
839     This->IHTMLDocument3_iface.lpVtbl = &HTMLDocument3Vtbl;
840     This->IHTMLDocument4_iface.lpVtbl = &HTMLDocument4Vtbl;
841 }