dplayx: Introduce impl_from_IDirectPlayLobby3A().
[wine] / dlls / mshtml / htmlelem.c
1 /*
2  * Copyright 2006 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 <stdarg.h>
20 #include <assert.h>
21
22 #define COBJMACROS
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "winreg.h"
28 #include "ole2.h"
29 #include "shlwapi.h"
30
31 #include "wine/debug.h"
32
33 #include "mshtml_private.h"
34 #include "htmlevent.h"
35 #include "htmlstyle.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
38
39 static const WCHAR aW[]        = {'A',0};
40 static const WCHAR bodyW[]     = {'B','O','D','Y',0};
41 static const WCHAR buttonW[]   = {'B','U','T','T','O','N',0};
42 static const WCHAR embedW[]    = {'E','M','B','E','D',0};
43 static const WCHAR formW[]     = {'F','O','R','M',0};
44 static const WCHAR frameW[]    = {'F','R','A','M','E',0};
45 static const WCHAR headW[]     = {'H','E','A','D',0};
46 static const WCHAR iframeW[]   = {'I','F','R','A','M','E',0};
47 static const WCHAR imgW[]      = {'I','M','G',0};
48 static const WCHAR inputW[]    = {'I','N','P','U','T',0};
49 static const WCHAR labelW[]    = {'L','A','B','E','L',0};
50 static const WCHAR linkW[]     = {'L','I','N','K',0};
51 static const WCHAR metaW[]     = {'M','E','T','A',0};
52 static const WCHAR objectW[]   = {'O','B','J','E','C','T',0};
53 static const WCHAR optionW[]   = {'O','P','T','I','O','N',0};
54 static const WCHAR scriptW[]   = {'S','C','R','I','P','T',0};
55 static const WCHAR selectW[]   = {'S','E','L','E','C','T',0};
56 static const WCHAR styleW[]    = {'S','T','Y','L','E',0};
57 static const WCHAR tableW[]    = {'T','A','B','L','E',0};
58 static const WCHAR tdW[]       = {'T','D',0};
59 static const WCHAR textareaW[] = {'T','E','X','T','A','R','E','A',0};
60 static const WCHAR title_tagW[]= {'T','I','T','L','E',0};
61 static const WCHAR trW[]       = {'T','R',0};
62
63 typedef struct {
64     const WCHAR *name;
65     HRESULT (*constructor)(HTMLDocumentNode*,nsIDOMHTMLElement*,HTMLElement**);
66 } tag_desc_t;
67
68 static const tag_desc_t tag_descs[] = {
69     {aW,         HTMLAnchorElement_Create},
70     {bodyW,      HTMLBodyElement_Create},
71     {buttonW,    HTMLButtonElement_Create},
72     {embedW,     HTMLEmbedElement_Create},
73     {formW,      HTMLFormElement_Create},
74     {frameW,     HTMLFrameElement_Create},
75     {headW,      HTMLHeadElement_Create},
76     {iframeW,    HTMLIFrame_Create},
77     {imgW,       HTMLImgElement_Create},
78     {inputW,     HTMLInputElement_Create},
79     {labelW,     HTMLLabelElement_Create},
80     {linkW,      HTMLLinkElement_Create},
81     {metaW,      HTMLMetaElement_Create},
82     {objectW,    HTMLObjectElement_Create},
83     {optionW,    HTMLOptionElement_Create},
84     {scriptW,    HTMLScriptElement_Create},
85     {selectW,    HTMLSelectElement_Create},
86     {styleW,     HTMLStyleElement_Create},
87     {tableW,     HTMLTable_Create},
88     {tdW,        HTMLTableCell_Create},
89     {textareaW,  HTMLTextAreaElement_Create},
90     {title_tagW, HTMLTitleElement_Create},
91     {trW,        HTMLTableRow_Create}
92 };
93
94 static const tag_desc_t *get_tag_desc(const WCHAR *tag_name)
95 {
96     DWORD min=0, max=sizeof(tag_descs)/sizeof(*tag_descs)-1, i;
97     int r;
98
99     while(min <= max) {
100         i = (min+max)/2;
101         r = strcmpW(tag_name, tag_descs[i].name);
102         if(!r)
103             return tag_descs+i;
104
105         if(r < 0)
106             max = i-1;
107         else
108             min = i+1;
109     }
110
111     return NULL;
112 }
113
114 HRESULT replace_node_by_html(nsIDOMHTMLDocument *nsdoc, nsIDOMNode *nsnode, const WCHAR *html)
115 {
116     nsIDOMDocumentFragment *nsfragment;
117     nsIDOMNode *nsparent;
118     nsIDOMRange *range;
119     nsAString html_str;
120     nsresult nsres;
121     HRESULT hres = S_OK;
122
123     nsres = nsIDOMHTMLDocument_CreateRange(nsdoc, &range);
124     if(NS_FAILED(nsres)) {
125         ERR("CreateRange failed: %08x\n", nsres);
126         return E_FAIL;
127     }
128
129     nsAString_InitDepend(&html_str, html);
130     nsIDOMRange_CreateContextualFragment(range, &html_str, &nsfragment);
131     nsIDOMRange_Release(range);
132     nsAString_Finish(&html_str);
133     if(NS_FAILED(nsres)) {
134         ERR("CreateContextualFragment failed: %08x\n", nsres);
135         return E_FAIL;
136     }
137
138     nsres = nsIDOMNode_GetParentNode(nsnode, &nsparent);
139     if(NS_SUCCEEDED(nsres) && nsparent) {
140         nsIDOMNode *nstmp;
141
142         nsres = nsIDOMNode_ReplaceChild(nsparent, (nsIDOMNode*)nsfragment, nsnode, &nstmp);
143         nsIDOMNode_Release(nsparent);
144         if(NS_FAILED(nsres)) {
145             ERR("ReplaceChild failed: %08x\n", nsres);
146             hres = E_FAIL;
147         }else if(nstmp) {
148             nsIDOMNode_Release(nstmp);
149         }
150     }else {
151         ERR("GetParentNode failed: %08x\n", nsres);
152         hres = E_FAIL;
153     }
154
155     nsIDOMDocumentFragment_Release(nsfragment);
156     return hres;
157 }
158
159 typedef struct
160 {
161     DispatchEx dispex;
162     IHTMLFiltersCollection IHTMLFiltersCollection_iface;
163
164     LONG ref;
165 } HTMLFiltersCollection;
166
167 static inline HTMLFiltersCollection *impl_from_IHTMLFiltersCollection(IHTMLFiltersCollection *iface)
168 {
169     return CONTAINING_RECORD(iface, HTMLFiltersCollection, IHTMLFiltersCollection_iface);
170 }
171
172 static IHTMLFiltersCollection *HTMLFiltersCollection_Create(void);
173
174 static inline HTMLElement *impl_from_IHTMLElement(IHTMLElement *iface)
175 {
176     return CONTAINING_RECORD(iface, HTMLElement, IHTMLElement_iface);
177 }
178
179 HRESULT create_nselem(HTMLDocumentNode *doc, const WCHAR *tag, nsIDOMHTMLElement **ret)
180 {
181     nsIDOMElement *nselem;
182     nsAString tag_str;
183     nsresult nsres;
184
185     if(!doc->nsdoc) {
186         WARN("NULL nsdoc\n");
187         return E_UNEXPECTED;
188     }
189
190     nsAString_InitDepend(&tag_str, tag);
191     nsres = nsIDOMHTMLDocument_CreateElement(doc->nsdoc, &tag_str, &nselem);
192     nsAString_Finish(&tag_str);
193     if(NS_FAILED(nsres)) {
194         ERR("CreateElement failed: %08x\n", nsres);
195         return E_FAIL;
196     }
197
198     nsres = nsIDOMElement_QueryInterface(nselem, &IID_nsIDOMHTMLElement, (void**)ret);
199     nsIDOMElement_Release(nselem);
200     if(NS_FAILED(nsres)) {
201         ERR("Could not get nsIDOMHTMLElement iface: %08x\n", nsres);
202         return E_FAIL;
203     }
204
205     return S_OK;
206 }
207
208 HRESULT create_element(HTMLDocumentNode *doc, const WCHAR *tag, HTMLElement **ret)
209 {
210     nsIDOMHTMLElement *nselem;
211     HRESULT hres;
212
213     /* Use owner doc if called on document fragment */
214     if(!doc->nsdoc)
215         doc = doc->node.doc;
216
217     hres = create_nselem(doc, tag, &nselem);
218     if(FAILED(hres))
219         return hres;
220
221     hres = HTMLElement_Create(doc, (nsIDOMNode*)nselem, TRUE, ret);
222     nsIDOMHTMLElement_Release(nselem);
223     return hres;
224 }
225
226 static HRESULT WINAPI HTMLElement_QueryInterface(IHTMLElement *iface,
227                                                  REFIID riid, void **ppv)
228 {
229     HTMLElement *This = impl_from_IHTMLElement(iface);
230
231     return IHTMLDOMNode_QueryInterface(&This->node.IHTMLDOMNode_iface, riid, ppv);
232 }
233
234 static ULONG WINAPI HTMLElement_AddRef(IHTMLElement *iface)
235 {
236     HTMLElement *This = impl_from_IHTMLElement(iface);
237
238     return IHTMLDOMNode_AddRef(&This->node.IHTMLDOMNode_iface);
239 }
240
241 static ULONG WINAPI HTMLElement_Release(IHTMLElement *iface)
242 {
243     HTMLElement *This = impl_from_IHTMLElement(iface);
244
245     return IHTMLDOMNode_Release(&This->node.IHTMLDOMNode_iface);
246 }
247
248 static HRESULT WINAPI HTMLElement_GetTypeInfoCount(IHTMLElement *iface, UINT *pctinfo)
249 {
250     HTMLElement *This = impl_from_IHTMLElement(iface);
251     return IDispatchEx_GetTypeInfoCount(&This->node.dispex.IDispatchEx_iface, pctinfo);
252 }
253
254 static HRESULT WINAPI HTMLElement_GetTypeInfo(IHTMLElement *iface, UINT iTInfo,
255                                               LCID lcid, ITypeInfo **ppTInfo)
256 {
257     HTMLElement *This = impl_from_IHTMLElement(iface);
258     return IDispatchEx_GetTypeInfo(&This->node.dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
259 }
260
261 static HRESULT WINAPI HTMLElement_GetIDsOfNames(IHTMLElement *iface, REFIID riid,
262                                                 LPOLESTR *rgszNames, UINT cNames,
263                                                 LCID lcid, DISPID *rgDispId)
264 {
265     HTMLElement *This = impl_from_IHTMLElement(iface);
266     return IDispatchEx_GetIDsOfNames(&This->node.dispex.IDispatchEx_iface, riid, rgszNames, cNames,
267             lcid, rgDispId);
268 }
269
270 static HRESULT WINAPI HTMLElement_Invoke(IHTMLElement *iface, DISPID dispIdMember,
271                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
272                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
273 {
274     HTMLElement *This = impl_from_IHTMLElement(iface);
275     return IDispatchEx_Invoke(&This->node.dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
276             wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
277 }
278
279 static HRESULT WINAPI HTMLElement_setAttribute(IHTMLElement *iface, BSTR strAttributeName,
280                                                VARIANT AttributeValue, LONG lFlags)
281 {
282     HTMLElement *This = impl_from_IHTMLElement(iface);
283     HRESULT hres;
284     DISPID dispid, dispidNamed = DISPID_PROPERTYPUT;
285     DISPPARAMS dispParams;
286     EXCEPINFO excep;
287
288     TRACE("(%p)->(%s %s %08x)\n", This, debugstr_w(strAttributeName), debugstr_variant(&AttributeValue), lFlags);
289
290     hres = IDispatchEx_GetDispID(&This->node.dispex.IDispatchEx_iface, strAttributeName,
291             fdexNameCaseInsensitive | fdexNameEnsure, &dispid);
292     if(FAILED(hres))
293         return hres;
294
295     dispParams.cArgs = 1;
296     dispParams.cNamedArgs = 1;
297     dispParams.rgdispidNamedArgs = &dispidNamed;
298     dispParams.rgvarg = &AttributeValue;
299
300     hres = IDispatchEx_InvokeEx(&This->node.dispex.IDispatchEx_iface, dispid,
301             LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYPUT, &dispParams, NULL, &excep, NULL);
302     return hres;
303 }
304
305 static HRESULT WINAPI HTMLElement_getAttribute(IHTMLElement *iface, BSTR strAttributeName,
306                                                LONG lFlags, VARIANT *AttributeValue)
307 {
308     HTMLElement *This = impl_from_IHTMLElement(iface);
309     DISPID dispid;
310     HRESULT hres;
311     DISPPARAMS dispParams = {NULL, NULL, 0, 0};
312     EXCEPINFO excep;
313
314     TRACE("(%p)->(%s %08x %p)\n", This, debugstr_w(strAttributeName), lFlags, AttributeValue);
315
316     hres = IDispatchEx_GetDispID(&This->node.dispex.IDispatchEx_iface, strAttributeName,
317             fdexNameCaseInsensitive, &dispid);
318     if(hres == DISP_E_UNKNOWNNAME) {
319         V_VT(AttributeValue) = VT_NULL;
320         return S_OK;
321     }
322
323     if(FAILED(hres)) {
324         V_VT(AttributeValue) = VT_NULL;
325         return hres;
326     }
327
328     hres = IDispatchEx_InvokeEx(&This->node.dispex.IDispatchEx_iface, dispid, LOCALE_SYSTEM_DEFAULT,
329             DISPATCH_PROPERTYGET, &dispParams, AttributeValue, &excep, NULL);
330
331     return hres;
332 }
333
334 static HRESULT WINAPI HTMLElement_removeAttribute(IHTMLElement *iface, BSTR strAttributeName,
335                                                   LONG lFlags, VARIANT_BOOL *pfSuccess)
336 {
337     HTMLElement *This = impl_from_IHTMLElement(iface);
338
339     TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(strAttributeName), lFlags, pfSuccess);
340
341     return remove_prop(&This->node.dispex, strAttributeName, pfSuccess);
342 }
343
344 static HRESULT WINAPI HTMLElement_put_className(IHTMLElement *iface, BSTR v)
345 {
346     HTMLElement *This = impl_from_IHTMLElement(iface);
347     nsAString classname_str;
348     nsresult nsres;
349
350     TRACE("(%p)->(%s)\n", This, debugstr_w(v));
351
352     if(!This->nselem) {
353         FIXME("NULL nselem\n");
354         return E_NOTIMPL;
355     }
356
357     nsAString_InitDepend(&classname_str, v);
358     nsres = nsIDOMHTMLElement_SetClassName(This->nselem, &classname_str);
359     nsAString_Finish(&classname_str);
360     if(NS_FAILED(nsres))
361         ERR("SetClassName failed: %08x\n", nsres);
362
363     return S_OK;
364 }
365
366 static HRESULT WINAPI HTMLElement_get_className(IHTMLElement *iface, BSTR *p)
367 {
368     HTMLElement *This = impl_from_IHTMLElement(iface);
369     nsAString class_str;
370     nsresult nsres;
371
372     TRACE("(%p)->(%p)\n", This, p);
373
374     if(!This->nselem) {
375         FIXME("NULL nselem\n");
376         return E_NOTIMPL;
377     }
378
379     nsAString_Init(&class_str, NULL);
380     nsres = nsIDOMHTMLElement_GetClassName(This->nselem, &class_str);
381     return return_nsstr(nsres, &class_str, p);
382 }
383
384 static HRESULT WINAPI HTMLElement_put_id(IHTMLElement *iface, BSTR v)
385 {
386     HTMLElement *This = impl_from_IHTMLElement(iface);
387     nsAString id_str;
388     nsresult nsres;
389
390     TRACE("(%p)->(%s)\n", This, debugstr_w(v));
391
392     if(!This->nselem) {
393         FIXME("nselem == NULL\n");
394         return S_OK;
395     }
396
397     nsAString_InitDepend(&id_str, v);
398     nsres = nsIDOMHTMLElement_SetId(This->nselem, &id_str);
399     nsAString_Finish(&id_str);
400     if(NS_FAILED(nsres))
401         ERR("SetId failed: %08x\n", nsres);
402
403     return S_OK;
404 }
405
406 static HRESULT WINAPI HTMLElement_get_id(IHTMLElement *iface, BSTR *p)
407 {
408     HTMLElement *This = impl_from_IHTMLElement(iface);
409     nsAString id_str;
410     nsresult nsres;
411
412     TRACE("(%p)->(%p)\n", This, p);
413
414     if(!This->nselem) {
415         *p = NULL;
416         return S_OK;
417     }
418
419     nsAString_Init(&id_str, NULL);
420     nsres = nsIDOMHTMLElement_GetId(This->nselem, &id_str);
421     return return_nsstr(nsres, &id_str, p);
422 }
423
424 static HRESULT WINAPI HTMLElement_get_tagName(IHTMLElement *iface, BSTR *p)
425 {
426     HTMLElement *This = impl_from_IHTMLElement(iface);
427     nsAString tag_str;
428     nsresult nsres;
429
430     TRACE("(%p)->(%p)\n", This, p);
431
432     if(!This->nselem) {
433         static const WCHAR comment_tagW[] = {'!',0};
434
435         WARN("NULL nselem, assuming comment\n");
436
437         *p = SysAllocString(comment_tagW);
438         return *p ? S_OK : E_OUTOFMEMORY;
439     }
440
441     nsAString_Init(&tag_str, NULL);
442     nsres = nsIDOMHTMLElement_GetTagName(This->nselem, &tag_str);
443     return return_nsstr(nsres, &tag_str, p);
444 }
445
446 static HRESULT WINAPI HTMLElement_get_parentElement(IHTMLElement *iface, IHTMLElement **p)
447 {
448     HTMLElement *This = impl_from_IHTMLElement(iface);
449     IHTMLDOMNode *node;
450     HRESULT hres;
451
452     TRACE("(%p)->(%p)\n", This, p);
453
454     hres = IHTMLDOMNode_get_parentNode(&This->node.IHTMLDOMNode_iface, &node);
455     if(FAILED(hres))
456         return hres;
457
458     hres = IHTMLDOMNode_QueryInterface(node, &IID_IHTMLElement, (void**)p);
459     IHTMLDOMNode_Release(node);
460     if(FAILED(hres))
461         *p = NULL;
462
463     return S_OK;
464 }
465
466 static HRESULT WINAPI HTMLElement_get_style(IHTMLElement *iface, IHTMLStyle **p)
467 {
468     HTMLElement *This = impl_from_IHTMLElement(iface);
469
470     TRACE("(%p)->(%p)\n", This, p);
471
472     if(!This->style) {
473         HRESULT hres;
474
475         hres = HTMLStyle_Create(This, &This->style);
476         if(FAILED(hres))
477             return hres;
478     }
479
480     *p = &This->style->IHTMLStyle_iface;
481     IHTMLStyle_AddRef(*p);
482     return S_OK;
483 }
484
485 static HRESULT WINAPI HTMLElement_put_onhelp(IHTMLElement *iface, VARIANT v)
486 {
487     HTMLElement *This = impl_from_IHTMLElement(iface);
488     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
489     return E_NOTIMPL;
490 }
491
492 static HRESULT WINAPI HTMLElement_get_onhelp(IHTMLElement *iface, VARIANT *p)
493 {
494     HTMLElement *This = impl_from_IHTMLElement(iface);
495     FIXME("(%p)->(%p)\n", This, p);
496     return E_NOTIMPL;
497 }
498
499 static HRESULT WINAPI HTMLElement_put_onclick(IHTMLElement *iface, VARIANT v)
500 {
501     HTMLElement *This = impl_from_IHTMLElement(iface);
502
503     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
504
505     return set_node_event(&This->node, EVENTID_CLICK, &v);
506 }
507
508 static HRESULT WINAPI HTMLElement_get_onclick(IHTMLElement *iface, VARIANT *p)
509 {
510     HTMLElement *This = impl_from_IHTMLElement(iface);
511
512     TRACE("(%p)->(%p)\n", This, p);
513
514     return get_node_event(&This->node, EVENTID_CLICK, p);
515 }
516
517 static HRESULT WINAPI HTMLElement_put_ondblclick(IHTMLElement *iface, VARIANT v)
518 {
519     HTMLElement *This = impl_from_IHTMLElement(iface);
520
521     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
522
523     return set_node_event(&This->node, EVENTID_DBLCLICK, &v);
524 }
525
526 static HRESULT WINAPI HTMLElement_get_ondblclick(IHTMLElement *iface, VARIANT *p)
527 {
528     HTMLElement *This = impl_from_IHTMLElement(iface);
529
530     TRACE("(%p)->(%p)\n", This, p);
531
532     return get_node_event(&This->node, EVENTID_DBLCLICK, p);
533 }
534
535 static HRESULT WINAPI HTMLElement_put_onkeydown(IHTMLElement *iface, VARIANT v)
536 {
537     HTMLElement *This = impl_from_IHTMLElement(iface);
538
539     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
540
541     return set_node_event(&This->node, EVENTID_KEYDOWN, &v);
542 }
543
544 static HRESULT WINAPI HTMLElement_get_onkeydown(IHTMLElement *iface, VARIANT *p)
545 {
546     HTMLElement *This = impl_from_IHTMLElement(iface);
547
548     TRACE("(%p)->(%p)\n", This, p);
549
550     return get_node_event(&This->node, EVENTID_KEYDOWN, p);
551 }
552
553 static HRESULT WINAPI HTMLElement_put_onkeyup(IHTMLElement *iface, VARIANT v)
554 {
555     HTMLElement *This = impl_from_IHTMLElement(iface);
556
557     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
558
559     return set_node_event(&This->node, EVENTID_KEYUP, &v);
560 }
561
562 static HRESULT WINAPI HTMLElement_get_onkeyup(IHTMLElement *iface, VARIANT *p)
563 {
564     HTMLElement *This = impl_from_IHTMLElement(iface);
565     FIXME("(%p)->(%p)\n", This, p);
566     return E_NOTIMPL;
567 }
568
569 static HRESULT WINAPI HTMLElement_put_onkeypress(IHTMLElement *iface, VARIANT v)
570 {
571     HTMLElement *This = impl_from_IHTMLElement(iface);
572
573     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
574
575     return set_node_event(&This->node, EVENTID_KEYPRESS, &v);
576 }
577
578 static HRESULT WINAPI HTMLElement_get_onkeypress(IHTMLElement *iface, VARIANT *p)
579 {
580     HTMLElement *This = impl_from_IHTMLElement(iface);
581
582     TRACE("(%p)->(%p)\n", This, p);
583
584     return get_node_event(&This->node, EVENTID_KEYPRESS, p);
585 }
586
587 static HRESULT WINAPI HTMLElement_put_onmouseout(IHTMLElement *iface, VARIANT v)
588 {
589     HTMLElement *This = impl_from_IHTMLElement(iface);
590
591     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
592
593     return set_node_event(&This->node, EVENTID_MOUSEOUT, &v);
594 }
595
596 static HRESULT WINAPI HTMLElement_get_onmouseout(IHTMLElement *iface, VARIANT *p)
597 {
598     HTMLElement *This = impl_from_IHTMLElement(iface);
599
600     TRACE("(%p)->(%p)\n", This, p);
601
602     return get_node_event(&This->node, EVENTID_MOUSEOUT, p);
603 }
604
605 static HRESULT WINAPI HTMLElement_put_onmouseover(IHTMLElement *iface, VARIANT v)
606 {
607     HTMLElement *This = impl_from_IHTMLElement(iface);
608
609     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
610
611     return set_node_event(&This->node, EVENTID_MOUSEOVER, &v);
612 }
613
614 static HRESULT WINAPI HTMLElement_get_onmouseover(IHTMLElement *iface, VARIANT *p)
615 {
616     HTMLElement *This = impl_from_IHTMLElement(iface);
617
618     TRACE("(%p)->(%p)\n", This, p);
619
620     return get_node_event(&This->node, EVENTID_MOUSEOVER, p);
621 }
622
623 static HRESULT WINAPI HTMLElement_put_onmousemove(IHTMLElement *iface, VARIANT v)
624 {
625     HTMLElement *This = impl_from_IHTMLElement(iface);
626
627     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
628
629     return set_node_event(&This->node, EVENTID_MOUSEMOVE, &v);
630 }
631
632 static HRESULT WINAPI HTMLElement_get_onmousemove(IHTMLElement *iface, VARIANT *p)
633 {
634     HTMLElement *This = impl_from_IHTMLElement(iface);
635
636     TRACE("(%p)->(%p)\n", This, p);
637
638     return get_node_event(&This->node, EVENTID_MOUSEMOVE, p);
639 }
640
641 static HRESULT WINAPI HTMLElement_put_onmousedown(IHTMLElement *iface, VARIANT v)
642 {
643     HTMLElement *This = impl_from_IHTMLElement(iface);
644
645     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
646
647     return set_node_event(&This->node, EVENTID_MOUSEDOWN, &v);
648 }
649
650 static HRESULT WINAPI HTMLElement_get_onmousedown(IHTMLElement *iface, VARIANT *p)
651 {
652     HTMLElement *This = impl_from_IHTMLElement(iface);
653
654     TRACE("(%p)->(%p)\n", This, p);
655
656     return get_node_event(&This->node, EVENTID_MOUSEDOWN, p);
657 }
658
659 static HRESULT WINAPI HTMLElement_put_onmouseup(IHTMLElement *iface, VARIANT v)
660 {
661     HTMLElement *This = impl_from_IHTMLElement(iface);
662
663     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
664
665     return set_node_event(&This->node, EVENTID_MOUSEUP, &v);
666 }
667
668 static HRESULT WINAPI HTMLElement_get_onmouseup(IHTMLElement *iface, VARIANT *p)
669 {
670     HTMLElement *This = impl_from_IHTMLElement(iface);
671
672     TRACE("(%p)->(%p)\n", This, p);
673
674     return get_node_event(&This->node, EVENTID_MOUSEUP, p);
675 }
676
677 static HRESULT WINAPI HTMLElement_get_document(IHTMLElement *iface, IDispatch **p)
678 {
679     HTMLElement *This = impl_from_IHTMLElement(iface);
680
681     TRACE("(%p)->(%p)\n", This, p);
682
683     if(!p)
684         return E_POINTER;
685
686     if(This->node.vtbl->get_document)
687         return This->node.vtbl->get_document(&This->node, p);
688
689     *p = (IDispatch*)&This->node.doc->basedoc.IHTMLDocument2_iface;
690     IDispatch_AddRef(*p);
691     return S_OK;
692 }
693
694 static const WCHAR titleW[] = {'t','i','t','l','e',0};
695
696 static HRESULT WINAPI HTMLElement_put_title(IHTMLElement *iface, BSTR v)
697 {
698     HTMLElement *This = impl_from_IHTMLElement(iface);
699     nsAString title_str;
700     nsresult nsres;
701
702     TRACE("(%p)->(%s)\n", This, debugstr_w(v));
703
704     if(!This->nselem) {
705         VARIANT *var;
706         HRESULT hres;
707
708         hres = dispex_get_dprop_ref(&This->node.dispex, titleW, TRUE, &var);
709         if(FAILED(hres))
710             return hres;
711
712         VariantClear(var);
713         V_VT(var) = VT_BSTR;
714         V_BSTR(var) = v ? SysAllocString(v) : NULL;
715         return S_OK;
716     }
717
718     nsAString_InitDepend(&title_str, v);
719     nsres = nsIDOMHTMLElement_SetTitle(This->nselem, &title_str);
720     nsAString_Finish(&title_str);
721     if(NS_FAILED(nsres))
722         ERR("SetTitle failed: %08x\n", nsres);
723
724     return S_OK;
725 }
726
727 static HRESULT WINAPI HTMLElement_get_title(IHTMLElement *iface, BSTR *p)
728 {
729     HTMLElement *This = impl_from_IHTMLElement(iface);
730     nsAString title_str;
731     nsresult nsres;
732
733     TRACE("(%p)->(%p)\n", This, p);
734
735     if(!This->nselem) {
736         VARIANT *var;
737         HRESULT hres;
738
739         hres = dispex_get_dprop_ref(&This->node.dispex, titleW, FALSE, &var);
740         if(hres == DISP_E_UNKNOWNNAME) {
741             *p = NULL;
742         }else if(V_VT(var) != VT_BSTR) {
743             FIXME("title = %s\n", debugstr_variant(var));
744             return E_FAIL;
745         }else {
746             *p = V_BSTR(var) ? SysAllocString(V_BSTR(var)) : NULL;
747         }
748
749         return S_OK;
750     }
751
752     nsAString_Init(&title_str, NULL);
753     nsres = nsIDOMHTMLElement_GetTitle(This->nselem, &title_str);
754     return return_nsstr(nsres, &title_str, p);
755 }
756
757 static HRESULT WINAPI HTMLElement_put_language(IHTMLElement *iface, BSTR v)
758 {
759     HTMLElement *This = impl_from_IHTMLElement(iface);
760     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
761     return E_NOTIMPL;
762 }
763
764 static HRESULT WINAPI HTMLElement_get_language(IHTMLElement *iface, BSTR *p)
765 {
766     HTMLElement *This = impl_from_IHTMLElement(iface);
767     FIXME("(%p)->(%p)\n", This, p);
768     return E_NOTIMPL;
769 }
770
771 static HRESULT WINAPI HTMLElement_put_onselectstart(IHTMLElement *iface, VARIANT v)
772 {
773     HTMLElement *This = impl_from_IHTMLElement(iface);
774
775     TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
776
777     return set_node_event(&This->node, EVENTID_SELECTSTART, &v);
778 }
779
780 static HRESULT WINAPI HTMLElement_get_onselectstart(IHTMLElement *iface, VARIANT *p)
781 {
782     HTMLElement *This = impl_from_IHTMLElement(iface);
783
784     TRACE("(%p)->(%p)\n", This, p);
785
786     return get_node_event(&This->node, EVENTID_SELECTSTART, p);
787 }
788
789 static HRESULT WINAPI HTMLElement_scrollIntoView(IHTMLElement *iface, VARIANT varargStart)
790 {
791     HTMLElement *This = impl_from_IHTMLElement(iface);
792     FIXME("(%p)->(%s)\n", This, debugstr_variant(&varargStart));
793     return E_NOTIMPL;
794 }
795
796 static HRESULT WINAPI HTMLElement_contains(IHTMLElement *iface, IHTMLElement *pChild,
797                                            VARIANT_BOOL *pfResult)
798 {
799     HTMLElement *This = impl_from_IHTMLElement(iface);
800     HTMLElement *child;
801     cpp_bool result;
802     nsresult nsres;
803
804     TRACE("(%p)->(%p %p)\n", This, pChild, pfResult);
805
806     child = unsafe_impl_from_IHTMLElement(pChild);
807     if(!child) {
808         ERR("not our element\n");
809         return E_FAIL;
810     }
811
812     nsres = nsIDOMNode_Contains(This->node.nsnode, child->node.nsnode, &result);
813     if(NS_FAILED(nsres)) {
814         ERR("failed\n");
815         return E_FAIL;
816     }
817
818     *pfResult = result ? VARIANT_TRUE : VARIANT_FALSE;
819     return S_OK;
820 }
821
822 static HRESULT WINAPI HTMLElement_get_sourceIndex(IHTMLElement *iface, LONG *p)
823 {
824     HTMLElement *This = impl_from_IHTMLElement(iface);
825     FIXME("(%p)->(%p)\n", This, p);
826     return E_NOTIMPL;
827 }
828
829 static HRESULT WINAPI HTMLElement_get_recordNumber(IHTMLElement *iface, VARIANT *p)
830 {
831     HTMLElement *This = impl_from_IHTMLElement(iface);
832     FIXME("(%p)->(%p)\n", This, p);
833     return E_NOTIMPL;
834 }
835
836 static HRESULT WINAPI HTMLElement_put_lang(IHTMLElement *iface, BSTR v)
837 {
838     HTMLElement *This = impl_from_IHTMLElement(iface);
839     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
840     return E_NOTIMPL;
841 }
842
843 static HRESULT WINAPI HTMLElement_get_lang(IHTMLElement *iface, BSTR *p)
844 {
845     HTMLElement *This = impl_from_IHTMLElement(iface);
846     FIXME("(%p)->(%p)\n", This, p);
847     return E_NOTIMPL;
848 }
849
850 static HRESULT WINAPI HTMLElement_get_offsetLeft(IHTMLElement *iface, LONG *p)
851 {
852     HTMLElement *This = impl_from_IHTMLElement(iface);
853     nsresult nsres;
854
855     TRACE("(%p)->(%p)\n", This, p);
856
857     nsres = nsIDOMHTMLElement_GetOffsetLeft(This->nselem, p);
858     if(NS_FAILED(nsres)) {
859         ERR("GetOffsetLeft failed: %08x\n", nsres);
860         return E_FAIL;
861     }
862
863     return S_OK;
864 }
865
866 static HRESULT WINAPI HTMLElement_get_offsetTop(IHTMLElement *iface, LONG *p)
867 {
868     HTMLElement *This = impl_from_IHTMLElement(iface);
869     nsresult nsres;
870
871     TRACE("(%p)->(%p)\n", This, p);
872
873     nsres = nsIDOMHTMLElement_GetOffsetTop(This->nselem, p);
874     if(NS_FAILED(nsres)) {
875         ERR("GetOffsetTop failed: %08x\n", nsres);
876         return E_FAIL;
877     }
878
879     return S_OK;
880 }
881
882 static HRESULT WINAPI HTMLElement_get_offsetWidth(IHTMLElement *iface, LONG *p)
883 {
884     HTMLElement *This = impl_from_IHTMLElement(iface);
885     nsresult nsres;
886
887     TRACE("(%p)->(%p)\n", This, p);
888
889     nsres = nsIDOMHTMLElement_GetOffsetWidth(This->nselem, p);
890     if(NS_FAILED(nsres)) {
891         ERR("GetOffsetWidth failed: %08x\n", nsres);
892         return E_FAIL;
893     }
894
895     return S_OK;
896 }
897
898 static HRESULT WINAPI HTMLElement_get_offsetHeight(IHTMLElement *iface, LONG *p)
899 {
900     HTMLElement *This = impl_from_IHTMLElement(iface);
901     nsresult nsres;
902
903     TRACE("(%p)->(%p)\n", This, p);
904
905     nsres = nsIDOMHTMLElement_GetOffsetHeight(This->nselem, p);
906     if(NS_FAILED(nsres)) {
907         ERR("GetOffsetHeight failed: %08x\n", nsres);
908         return E_FAIL;
909     }
910
911     return S_OK;
912 }
913
914 static HRESULT WINAPI HTMLElement_get_offsetParent(IHTMLElement *iface, IHTMLElement **p)
915 {
916     HTMLElement *This = impl_from_IHTMLElement(iface);
917     nsIDOMElement *nsparent;
918     nsresult nsres;
919     HRESULT hres;
920
921     TRACE("(%p)->(%p)\n", This, p);
922
923     nsres = nsIDOMHTMLElement_GetOffsetParent(This->nselem, &nsparent);
924     if(NS_FAILED(nsres)) {
925         ERR("GetOffsetParent failed: %08x\n", nsres);
926         return E_FAIL;
927     }
928
929     if(nsparent) {
930         HTMLDOMNode *node;
931
932         hres = get_node(This->node.doc, (nsIDOMNode*)nsparent, TRUE, &node);
933         nsIDOMElement_Release(nsparent);
934         if(FAILED(hres))
935             return hres;
936
937         hres = IHTMLDOMNode_QueryInterface(&node->IHTMLDOMNode_iface, &IID_IHTMLElement, (void**)p);
938         node_release(node);
939     }else {
940         *p = NULL;
941         hres = S_OK;
942     }
943
944     return hres;
945 }
946
947 static HRESULT WINAPI HTMLElement_put_innerHTML(IHTMLElement *iface, BSTR v)
948 {
949     HTMLElement *This = impl_from_IHTMLElement(iface);
950     nsAString html_str;
951     nsresult nsres;
952
953     TRACE("(%p)->(%s)\n", This, debugstr_w(v));
954
955     if(!This->nselem) {
956         FIXME("NULL nselem\n");
957         return E_NOTIMPL;
958     }
959
960     nsAString_InitDepend(&html_str, v);
961     nsres = nsIDOMHTMLElement_SetInnerHTML(This->nselem, &html_str);
962     nsAString_Finish(&html_str);
963     if(NS_FAILED(nsres)) {
964         FIXME("SetInnerHtml failed %08x\n", nsres);
965         return E_FAIL;
966     }
967
968     return S_OK;
969 }
970
971 static HRESULT WINAPI HTMLElement_get_innerHTML(IHTMLElement *iface, BSTR *p)
972 {
973     HTMLElement *This = impl_from_IHTMLElement(iface);
974     nsAString html_str;
975     nsresult nsres;
976
977     TRACE("(%p)->(%p)\n", This, p);
978
979     if(!This->nselem) {
980         FIXME("NULL nselem\n");
981         return E_NOTIMPL;
982     }
983
984     nsAString_Init(&html_str, NULL);
985     nsres = nsIDOMHTMLElement_GetInnerHTML(This->nselem, &html_str);
986     return return_nsstr(nsres, &html_str, p);
987 }
988
989 static HRESULT WINAPI HTMLElement_put_innerText(IHTMLElement *iface, BSTR v)
990 {
991     HTMLElement *This = impl_from_IHTMLElement(iface);
992     nsIDOMNode *nschild, *tmp;
993     nsIDOMText *text_node;
994     nsAString text_str;
995     nsresult nsres;
996
997     TRACE("(%p)->(%s)\n", This, debugstr_w(v));
998
999     while(1) {
1000         nsres = nsIDOMHTMLElement_GetLastChild(This->nselem, &nschild);
1001         if(NS_FAILED(nsres)) {
1002             ERR("GetLastChild failed: %08x\n", nsres);
1003             return E_FAIL;
1004         }
1005         if(!nschild)
1006             break;
1007
1008         nsres = nsIDOMHTMLElement_RemoveChild(This->nselem, nschild, &tmp);
1009         nsIDOMNode_Release(nschild);
1010         if(NS_FAILED(nsres)) {
1011             ERR("RemoveChild failed: %08x\n", nsres);
1012             return E_FAIL;
1013         }
1014         nsIDOMNode_Release(tmp);
1015     }
1016
1017     nsAString_InitDepend(&text_str, v);
1018     nsres = nsIDOMHTMLDocument_CreateTextNode(This->node.doc->nsdoc, &text_str, &text_node);
1019     nsAString_Finish(&text_str);
1020     if(NS_FAILED(nsres)) {
1021         ERR("CreateTextNode failed: %08x\n", nsres);
1022         return E_FAIL;
1023     }
1024
1025     nsres = nsIDOMHTMLElement_AppendChild(This->nselem, (nsIDOMNode*)text_node, &tmp);
1026     if(NS_FAILED(nsres)) {
1027         ERR("AppendChild failed: %08x\n", nsres);
1028         return E_FAIL;
1029     }
1030
1031     nsIDOMNode_Release(tmp);
1032     return S_OK;
1033 }
1034
1035 static HRESULT WINAPI HTMLElement_get_innerText(IHTMLElement *iface, BSTR *p)
1036 {
1037     HTMLElement *This = impl_from_IHTMLElement(iface);
1038
1039     TRACE("(%p)->(%p)\n", This, p);
1040
1041     return get_node_text(&This->node, p);
1042 }
1043
1044 static HRESULT WINAPI HTMLElement_put_outerHTML(IHTMLElement *iface, BSTR v)
1045 {
1046     HTMLElement *This = impl_from_IHTMLElement(iface);
1047
1048     TRACE("(%p)->(%s)\n", This, debugstr_w(v));
1049
1050     return replace_node_by_html(This->node.doc->nsdoc, This->node.nsnode, v);
1051 }
1052
1053 static HRESULT WINAPI HTMLElement_get_outerHTML(IHTMLElement *iface, BSTR *p)
1054 {
1055     HTMLElement *This = impl_from_IHTMLElement(iface);
1056     nsAString html_str;
1057     HRESULT hres;
1058
1059     WARN("(%p)->(%p) semi-stub\n", This, p);
1060
1061     nsAString_Init(&html_str, NULL);
1062     hres = nsnode_to_nsstring(This->node.nsnode, &html_str);
1063     if(SUCCEEDED(hres)) {
1064         const PRUnichar *html;
1065
1066         nsAString_GetData(&html_str, &html);
1067         *p = SysAllocString(html);
1068         if(!*p)
1069             hres = E_OUTOFMEMORY;
1070     }
1071
1072     nsAString_Finish(&html_str);
1073
1074     TRACE("ret %s\n", debugstr_w(*p));
1075     return hres;
1076 }
1077
1078 static HRESULT WINAPI HTMLElement_put_outerText(IHTMLElement *iface, BSTR v)
1079 {
1080     HTMLElement *This = impl_from_IHTMLElement(iface);
1081     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
1082     return E_NOTIMPL;
1083 }
1084
1085 static HRESULT WINAPI HTMLElement_get_outerText(IHTMLElement *iface, BSTR *p)
1086 {
1087     HTMLElement *This = impl_from_IHTMLElement(iface);
1088     FIXME("(%p)->(%p)\n", This, p);
1089     return E_NOTIMPL;
1090 }
1091
1092 HRESULT insert_adjacent_node(HTMLElement *This, const WCHAR *where, nsIDOMNode *nsnode, HTMLDOMNode **ret_node)
1093 {
1094     nsIDOMNode *ret_nsnode;
1095     nsresult nsres;
1096     HRESULT hres = S_OK;
1097
1098     static const WCHAR beforebeginW[] = {'b','e','f','o','r','e','b','e','g','i','n',0};
1099     static const WCHAR afterbeginW[] = {'a','f','t','e','r','b','e','g','i','n',0};
1100     static const WCHAR beforeendW[] = {'b','e','f','o','r','e','e','n','d',0};
1101     static const WCHAR afterendW[] = {'a','f','t','e','r','e','n','d',0};
1102
1103     if (!strcmpiW(where, beforebeginW)) {
1104         nsIDOMNode *parent;
1105
1106         nsres = nsIDOMNode_GetParentNode(This->node.nsnode, &parent);
1107         if(NS_FAILED(nsres))
1108             return E_FAIL;
1109
1110         if(!parent)
1111             return E_INVALIDARG;
1112
1113         nsres = nsIDOMNode_InsertBefore(parent, nsnode, This->node.nsnode, &ret_nsnode);
1114         nsIDOMNode_Release(parent);
1115     }else if(!strcmpiW(where, afterbeginW)) {
1116         nsIDOMNode *first_child;
1117
1118         nsres = nsIDOMNode_GetFirstChild(This->node.nsnode, &first_child);
1119         if(NS_FAILED(nsres))
1120             return E_FAIL;
1121
1122         nsres = nsIDOMNode_InsertBefore(This->node.nsnode, nsnode, first_child, &ret_nsnode);
1123         if(NS_FAILED(nsres))
1124             return E_FAIL;
1125
1126         if (first_child)
1127             nsIDOMNode_Release(first_child);
1128     }else if (!strcmpiW(where, beforeendW)) {
1129         nsres = nsIDOMNode_AppendChild(This->node.nsnode, nsnode, &ret_nsnode);
1130     }else if (!strcmpiW(where, afterendW)) {
1131         nsIDOMNode *next_sibling, *parent;
1132
1133         nsres = nsIDOMNode_GetParentNode(This->node.nsnode, &parent);
1134         if(NS_FAILED(nsres))
1135             return E_FAIL;
1136         if(!parent)
1137             return E_INVALIDARG;
1138
1139         nsres = nsIDOMNode_GetNextSibling(This->node.nsnode, &next_sibling);
1140         if(NS_SUCCEEDED(nsres)) {
1141             if(next_sibling) {
1142                 nsres = nsIDOMNode_InsertBefore(parent, nsnode, next_sibling, &ret_nsnode);
1143                 nsIDOMNode_Release(next_sibling);
1144             }else {
1145                 nsres = nsIDOMNode_AppendChild(parent, nsnode, &ret_nsnode);
1146             }
1147         }
1148
1149         nsIDOMNode_Release(parent);
1150     }else {
1151         ERR("invalid where: %s\n", debugstr_w(where));
1152         return E_INVALIDARG;
1153     }
1154
1155     if (NS_FAILED(nsres))
1156         return E_FAIL;
1157
1158     if(ret_node)
1159         hres = get_node(This->node.doc, ret_nsnode, TRUE, ret_node);
1160     nsIDOMNode_Release(ret_nsnode);
1161     return hres;
1162 }
1163
1164 static HRESULT WINAPI HTMLElement_insertAdjacentHTML(IHTMLElement *iface, BSTR where,
1165                                                      BSTR html)
1166 {
1167     HTMLElement *This = impl_from_IHTMLElement(iface);
1168     nsIDOMRange *range;
1169     nsIDOMNode *nsnode;
1170     nsAString ns_html;
1171     nsresult nsres;
1172     HRESULT hr;
1173
1174     TRACE("(%p)->(%s %s)\n", This, debugstr_w(where), debugstr_w(html));
1175
1176     if(!This->node.doc->nsdoc) {
1177         WARN("NULL nsdoc\n");
1178         return E_UNEXPECTED;
1179     }
1180
1181     nsres = nsIDOMHTMLDocument_CreateRange(This->node.doc->nsdoc, &range);
1182     if(NS_FAILED(nsres))
1183     {
1184         ERR("CreateRange failed: %08x\n", nsres);
1185         return E_FAIL;
1186     }
1187
1188     nsIDOMRange_SetStartBefore(range, This->node.nsnode);
1189
1190     nsAString_InitDepend(&ns_html, html);
1191     nsres = nsIDOMRange_CreateContextualFragment(range, &ns_html, (nsIDOMDocumentFragment **)&nsnode);
1192     nsAString_Finish(&ns_html);
1193     nsIDOMRange_Release(range);
1194
1195     if(NS_FAILED(nsres) || !nsnode)
1196     {
1197         ERR("CreateTextNode failed: %08x\n", nsres);
1198         return E_FAIL;
1199     }
1200
1201     hr = insert_adjacent_node(This, where, nsnode, NULL);
1202     nsIDOMNode_Release(nsnode);
1203     return hr;
1204 }
1205
1206 static HRESULT WINAPI HTMLElement_insertAdjacentText(IHTMLElement *iface, BSTR where,
1207                                                      BSTR text)
1208 {
1209     HTMLElement *This = impl_from_IHTMLElement(iface);
1210     nsIDOMNode *nsnode;
1211     nsAString ns_text;
1212     nsresult nsres;
1213     HRESULT hr;
1214
1215     TRACE("(%p)->(%s %s)\n", This, debugstr_w(where), debugstr_w(text));
1216
1217     if(!This->node.doc->nsdoc) {
1218         WARN("NULL nsdoc\n");
1219         return E_UNEXPECTED;
1220     }
1221
1222
1223     nsAString_InitDepend(&ns_text, text);
1224     nsres = nsIDOMHTMLDocument_CreateTextNode(This->node.doc->nsdoc, &ns_text, (nsIDOMText **)&nsnode);
1225     nsAString_Finish(&ns_text);
1226
1227     if(NS_FAILED(nsres) || !nsnode)
1228     {
1229         ERR("CreateTextNode failed: %08x\n", nsres);
1230         return E_FAIL;
1231     }
1232
1233     hr = insert_adjacent_node(This, where, nsnode, NULL);
1234     nsIDOMNode_Release(nsnode);
1235
1236     return hr;
1237 }
1238
1239 static HRESULT WINAPI HTMLElement_get_parentTextEdit(IHTMLElement *iface, IHTMLElement **p)
1240 {
1241     HTMLElement *This = impl_from_IHTMLElement(iface);
1242     FIXME("(%p)->(%p)\n", This, p);
1243     return E_NOTIMPL;
1244 }
1245
1246 static HRESULT WINAPI HTMLElement_get_isTextEdit(IHTMLElement *iface, VARIANT_BOOL *p)
1247 {
1248     HTMLElement *This = impl_from_IHTMLElement(iface);
1249     FIXME("(%p)->(%p)\n", This, p);
1250     return E_NOTIMPL;
1251 }
1252
1253 static HRESULT WINAPI HTMLElement_click(IHTMLElement *iface)
1254 {
1255     HTMLElement *This = impl_from_IHTMLElement(iface);
1256
1257     TRACE("(%p)\n", This);
1258
1259     return call_fire_event(&This->node, EVENTID_CLICK);
1260 }
1261
1262 static HRESULT WINAPI HTMLElement_get_filters(IHTMLElement *iface,
1263                                               IHTMLFiltersCollection **p)
1264 {
1265     HTMLElement *This = impl_from_IHTMLElement(iface);
1266     TRACE("(%p)->(%p)\n", This, p);
1267
1268     if(!p)
1269         return E_POINTER;
1270
1271     *p = HTMLFiltersCollection_Create();
1272
1273     return S_OK;
1274 }
1275
1276 static HRESULT WINAPI HTMLElement_put_ondragstart(IHTMLElement *iface, VARIANT v)
1277 {
1278     HTMLElement *This = impl_from_IHTMLElement(iface);
1279     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1280     return E_NOTIMPL;
1281 }
1282
1283 static HRESULT WINAPI HTMLElement_get_ondragstart(IHTMLElement *iface, VARIANT *p)
1284 {
1285     HTMLElement *This = impl_from_IHTMLElement(iface);
1286     FIXME("(%p)->(%p)\n", This, p);
1287     return E_NOTIMPL;
1288 }
1289
1290 static HRESULT WINAPI HTMLElement_toString(IHTMLElement *iface, BSTR *String)
1291 {
1292     HTMLElement *This = impl_from_IHTMLElement(iface);
1293     FIXME("(%p)->(%p)\n", This, String);
1294     return E_NOTIMPL;
1295 }
1296
1297 static HRESULT WINAPI HTMLElement_put_onbeforeupdate(IHTMLElement *iface, VARIANT v)
1298 {
1299     HTMLElement *This = impl_from_IHTMLElement(iface);
1300     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1301     return E_NOTIMPL;
1302 }
1303
1304 static HRESULT WINAPI HTMLElement_get_onbeforeupdate(IHTMLElement *iface, VARIANT *p)
1305 {
1306     HTMLElement *This = impl_from_IHTMLElement(iface);
1307     FIXME("(%p)->(%p)\n", This, p);
1308     return E_NOTIMPL;
1309 }
1310
1311 static HRESULT WINAPI HTMLElement_put_onafterupdate(IHTMLElement *iface, VARIANT v)
1312 {
1313     HTMLElement *This = impl_from_IHTMLElement(iface);
1314     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1315     return E_NOTIMPL;
1316 }
1317
1318 static HRESULT WINAPI HTMLElement_get_onafterupdate(IHTMLElement *iface, VARIANT *p)
1319 {
1320     HTMLElement *This = impl_from_IHTMLElement(iface);
1321     FIXME("(%p)->(%p)\n", This, p);
1322     return E_NOTIMPL;
1323 }
1324
1325 static HRESULT WINAPI HTMLElement_put_onerrorupdate(IHTMLElement *iface, VARIANT v)
1326 {
1327     HTMLElement *This = impl_from_IHTMLElement(iface);
1328     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1329     return E_NOTIMPL;
1330 }
1331
1332 static HRESULT WINAPI HTMLElement_get_onerrorupdate(IHTMLElement *iface, VARIANT *p)
1333 {
1334     HTMLElement *This = impl_from_IHTMLElement(iface);
1335     FIXME("(%p)->(%p)\n", This, p);
1336     return E_NOTIMPL;
1337 }
1338
1339 static HRESULT WINAPI HTMLElement_put_onrowexit(IHTMLElement *iface, VARIANT v)
1340 {
1341     HTMLElement *This = impl_from_IHTMLElement(iface);
1342     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1343     return E_NOTIMPL;
1344 }
1345
1346 static HRESULT WINAPI HTMLElement_get_onrowexit(IHTMLElement *iface, VARIANT *p)
1347 {
1348     HTMLElement *This = impl_from_IHTMLElement(iface);
1349     FIXME("(%p)->(%p)\n", This, p);
1350     return E_NOTIMPL;
1351 }
1352
1353 static HRESULT WINAPI HTMLElement_put_onrowenter(IHTMLElement *iface, VARIANT v)
1354 {
1355     HTMLElement *This = impl_from_IHTMLElement(iface);
1356     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1357     return E_NOTIMPL;
1358 }
1359
1360 static HRESULT WINAPI HTMLElement_get_onrowenter(IHTMLElement *iface, VARIANT *p)
1361 {
1362     HTMLElement *This = impl_from_IHTMLElement(iface);
1363     FIXME("(%p)->(%p)\n", This, p);
1364     return E_NOTIMPL;
1365 }
1366
1367 static HRESULT WINAPI HTMLElement_put_ondatasetchanged(IHTMLElement *iface, VARIANT v)
1368 {
1369     HTMLElement *This = impl_from_IHTMLElement(iface);
1370     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1371     return E_NOTIMPL;
1372 }
1373
1374 static HRESULT WINAPI HTMLElement_get_ondatasetchanged(IHTMLElement *iface, VARIANT *p)
1375 {
1376     HTMLElement *This = impl_from_IHTMLElement(iface);
1377     FIXME("(%p)->(%p)\n", This, p);
1378     return E_NOTIMPL;
1379 }
1380
1381 static HRESULT WINAPI HTMLElement_put_ondataavailable(IHTMLElement *iface, VARIANT v)
1382 {
1383     HTMLElement *This = impl_from_IHTMLElement(iface);
1384
1385     FIXME("(%p)->(%s) semi-stub\n", This, debugstr_variant(&v));
1386
1387     return set_node_event(&This->node, EVENTID_DATAAVAILABLE, &v);
1388 }
1389
1390 static HRESULT WINAPI HTMLElement_get_ondataavailable(IHTMLElement *iface, VARIANT *p)
1391 {
1392     HTMLElement *This = impl_from_IHTMLElement(iface);
1393
1394     TRACE("(%p)->(%p)\n", This, p);
1395
1396     return get_node_event(&This->node, EVENTID_DATAAVAILABLE, p);
1397 }
1398
1399 static HRESULT WINAPI HTMLElement_put_ondatasetcomplete(IHTMLElement *iface, VARIANT v)
1400 {
1401     HTMLElement *This = impl_from_IHTMLElement(iface);
1402     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1403     return E_NOTIMPL;
1404 }
1405
1406 static HRESULT WINAPI HTMLElement_get_ondatasetcomplete(IHTMLElement *iface, VARIANT *p)
1407 {
1408     HTMLElement *This = impl_from_IHTMLElement(iface);
1409     FIXME("(%p)->(%p)\n", This, p);
1410     return E_NOTIMPL;
1411 }
1412
1413 static HRESULT WINAPI HTMLElement_put_onfilterchange(IHTMLElement *iface, VARIANT v)
1414 {
1415     HTMLElement *This = impl_from_IHTMLElement(iface);
1416     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
1417     return E_NOTIMPL;
1418 }
1419
1420 static HRESULT WINAPI HTMLElement_get_onfilterchange(IHTMLElement *iface, VARIANT *p)
1421 {
1422     HTMLElement *This = impl_from_IHTMLElement(iface);
1423     FIXME("(%p)->(%p)\n", This, p);
1424     return E_NOTIMPL;
1425 }
1426
1427 static HRESULT WINAPI HTMLElement_get_children(IHTMLElement *iface, IDispatch **p)
1428 {
1429     HTMLElement *This = impl_from_IHTMLElement(iface);
1430     nsIDOMNodeList *nsnode_list;
1431     nsresult nsres;
1432
1433     TRACE("(%p)->(%p)\n", This, p);
1434
1435     nsres = nsIDOMNode_GetChildNodes(This->node.nsnode, &nsnode_list);
1436     if(NS_FAILED(nsres)) {
1437         ERR("GetChildNodes failed: %08x\n", nsres);
1438         return E_FAIL;
1439     }
1440
1441     *p = (IDispatch*)create_collection_from_nodelist(This->node.doc, nsnode_list);
1442
1443     nsIDOMNodeList_Release(nsnode_list);
1444     return S_OK;
1445 }
1446
1447 static HRESULT WINAPI HTMLElement_get_all(IHTMLElement *iface, IDispatch **p)
1448 {
1449     HTMLElement *This = impl_from_IHTMLElement(iface);
1450
1451     TRACE("(%p)->(%p)\n", This, p);
1452
1453     *p = (IDispatch*)create_all_collection(&This->node, FALSE);
1454     return S_OK;
1455 }
1456
1457 static const IHTMLElementVtbl HTMLElementVtbl = {
1458     HTMLElement_QueryInterface,
1459     HTMLElement_AddRef,
1460     HTMLElement_Release,
1461     HTMLElement_GetTypeInfoCount,
1462     HTMLElement_GetTypeInfo,
1463     HTMLElement_GetIDsOfNames,
1464     HTMLElement_Invoke,
1465     HTMLElement_setAttribute,
1466     HTMLElement_getAttribute,
1467     HTMLElement_removeAttribute,
1468     HTMLElement_put_className,
1469     HTMLElement_get_className,
1470     HTMLElement_put_id,
1471     HTMLElement_get_id,
1472     HTMLElement_get_tagName,
1473     HTMLElement_get_parentElement,
1474     HTMLElement_get_style,
1475     HTMLElement_put_onhelp,
1476     HTMLElement_get_onhelp,
1477     HTMLElement_put_onclick,
1478     HTMLElement_get_onclick,
1479     HTMLElement_put_ondblclick,
1480     HTMLElement_get_ondblclick,
1481     HTMLElement_put_onkeydown,
1482     HTMLElement_get_onkeydown,
1483     HTMLElement_put_onkeyup,
1484     HTMLElement_get_onkeyup,
1485     HTMLElement_put_onkeypress,
1486     HTMLElement_get_onkeypress,
1487     HTMLElement_put_onmouseout,
1488     HTMLElement_get_onmouseout,
1489     HTMLElement_put_onmouseover,
1490     HTMLElement_get_onmouseover,
1491     HTMLElement_put_onmousemove,
1492     HTMLElement_get_onmousemove,
1493     HTMLElement_put_onmousedown,
1494     HTMLElement_get_onmousedown,
1495     HTMLElement_put_onmouseup,
1496     HTMLElement_get_onmouseup,
1497     HTMLElement_get_document,
1498     HTMLElement_put_title,
1499     HTMLElement_get_title,
1500     HTMLElement_put_language,
1501     HTMLElement_get_language,
1502     HTMLElement_put_onselectstart,
1503     HTMLElement_get_onselectstart,
1504     HTMLElement_scrollIntoView,
1505     HTMLElement_contains,
1506     HTMLElement_get_sourceIndex,
1507     HTMLElement_get_recordNumber,
1508     HTMLElement_put_lang,
1509     HTMLElement_get_lang,
1510     HTMLElement_get_offsetLeft,
1511     HTMLElement_get_offsetTop,
1512     HTMLElement_get_offsetWidth,
1513     HTMLElement_get_offsetHeight,
1514     HTMLElement_get_offsetParent,
1515     HTMLElement_put_innerHTML,
1516     HTMLElement_get_innerHTML,
1517     HTMLElement_put_innerText,
1518     HTMLElement_get_innerText,
1519     HTMLElement_put_outerHTML,
1520     HTMLElement_get_outerHTML,
1521     HTMLElement_put_outerText,
1522     HTMLElement_get_outerText,
1523     HTMLElement_insertAdjacentHTML,
1524     HTMLElement_insertAdjacentText,
1525     HTMLElement_get_parentTextEdit,
1526     HTMLElement_get_isTextEdit,
1527     HTMLElement_click,
1528     HTMLElement_get_filters,
1529     HTMLElement_put_ondragstart,
1530     HTMLElement_get_ondragstart,
1531     HTMLElement_toString,
1532     HTMLElement_put_onbeforeupdate,
1533     HTMLElement_get_onbeforeupdate,
1534     HTMLElement_put_onafterupdate,
1535     HTMLElement_get_onafterupdate,
1536     HTMLElement_put_onerrorupdate,
1537     HTMLElement_get_onerrorupdate,
1538     HTMLElement_put_onrowexit,
1539     HTMLElement_get_onrowexit,
1540     HTMLElement_put_onrowenter,
1541     HTMLElement_get_onrowenter,
1542     HTMLElement_put_ondatasetchanged,
1543     HTMLElement_get_ondatasetchanged,
1544     HTMLElement_put_ondataavailable,
1545     HTMLElement_get_ondataavailable,
1546     HTMLElement_put_ondatasetcomplete,
1547     HTMLElement_get_ondatasetcomplete,
1548     HTMLElement_put_onfilterchange,
1549     HTMLElement_get_onfilterchange,
1550     HTMLElement_get_children,
1551     HTMLElement_get_all
1552 };
1553
1554 HTMLElement *unsafe_impl_from_IHTMLElement(IHTMLElement *iface)
1555 {
1556     return iface->lpVtbl == &HTMLElementVtbl ? impl_from_IHTMLElement(iface) : NULL;
1557 }
1558
1559 static inline HTMLElement *impl_from_HTMLDOMNode(HTMLDOMNode *iface)
1560 {
1561     return CONTAINING_RECORD(iface, HTMLElement, node);
1562 }
1563
1564 HRESULT HTMLElement_QI(HTMLDOMNode *iface, REFIID riid, void **ppv)
1565 {
1566     HTMLElement *This = impl_from_HTMLDOMNode(iface);
1567
1568     *ppv =  NULL;
1569
1570     if(IsEqualGUID(&IID_IUnknown, riid)) {
1571         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
1572         *ppv = &This->IHTMLElement_iface;
1573     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
1574         TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
1575         *ppv = &This->IHTMLElement_iface;
1576     }else if(IsEqualGUID(&IID_IHTMLElement, riid)) {
1577         TRACE("(%p)->(IID_IHTMLElement %p)\n", This, ppv);
1578         *ppv = &This->IHTMLElement_iface;
1579     }else if(IsEqualGUID(&IID_IHTMLElement2, riid)) {
1580         TRACE("(%p)->(IID_IHTMLElement2 %p)\n", This, ppv);
1581         *ppv = &This->IHTMLElement2_iface;
1582     }else if(IsEqualGUID(&IID_IHTMLElement3, riid)) {
1583         TRACE("(%p)->(IID_IHTMLElement3 %p)\n", This, ppv);
1584         *ppv = &This->IHTMLElement3_iface;
1585     }else if(IsEqualGUID(&IID_IHTMLElement4, riid)) {
1586         TRACE("(%p)->(IID_IHTMLElement4 %p)\n", This, ppv);
1587         *ppv = &This->IHTMLElement4_iface;
1588     }else if(IsEqualGUID(&IID_IConnectionPointContainer, riid)) {
1589         TRACE("(%p)->(IID_IConnectionPointContainer %p)\n", This, ppv);
1590         *ppv = &This->cp_container.IConnectionPointContainer_iface;
1591     }
1592
1593     if(*ppv) {
1594         IHTMLElement_AddRef(&This->IHTMLElement_iface);
1595         return S_OK;
1596     }
1597
1598     return HTMLDOMNode_QI(&This->node, riid, ppv);
1599 }
1600
1601 void HTMLElement_destructor(HTMLDOMNode *iface)
1602 {
1603     HTMLElement *This = impl_from_HTMLDOMNode(iface);
1604
1605     ConnectionPointContainer_Destroy(&This->cp_container);
1606
1607     if(This->style) {
1608         This->style->elem = NULL;
1609         IHTMLStyle_Release(&This->style->IHTMLStyle_iface);
1610     }
1611     if(This->runtime_style) {
1612         This->runtime_style->elem = NULL;
1613         IHTMLStyle_Release(&This->runtime_style->IHTMLStyle_iface);
1614     }
1615     if(This->attrs) {
1616         HTMLDOMAttribute *attr;
1617
1618         LIST_FOR_EACH_ENTRY(attr, &This->attrs->attrs, HTMLDOMAttribute, entry)
1619             attr->elem = NULL;
1620
1621         This->attrs->elem = NULL;
1622         IHTMLAttributeCollection_Release(&This->attrs->IHTMLAttributeCollection_iface);
1623     }
1624
1625     heap_free(This->filter);
1626
1627     HTMLDOMNode_destructor(&This->node);
1628 }
1629
1630 HRESULT HTMLElement_clone(HTMLDOMNode *iface, nsIDOMNode *nsnode, HTMLDOMNode **ret)
1631 {
1632     HTMLElement *This = impl_from_HTMLDOMNode(iface);
1633     HTMLElement *new_elem;
1634     HRESULT hres;
1635
1636     hres = HTMLElement_Create(This->node.doc, nsnode, FALSE, &new_elem);
1637     if(FAILED(hres))
1638         return hres;
1639
1640     if(This->filter) {
1641         new_elem->filter = heap_strdupW(This->filter);
1642         if(!new_elem->filter) {
1643             IHTMLElement_Release(&This->IHTMLElement_iface);
1644             return E_OUTOFMEMORY;
1645         }
1646     }
1647
1648     *ret = &new_elem->node;
1649     return S_OK;
1650 }
1651
1652 HRESULT HTMLElement_handle_event(HTMLDOMNode *iface, DWORD eid, nsIDOMEvent *event, BOOL *prevent_default)
1653 {
1654     HTMLElement *This = impl_from_HTMLDOMNode(iface);
1655
1656     switch(eid) {
1657     case EVENTID_KEYDOWN: {
1658         nsIDOMKeyEvent *key_event;
1659         nsresult nsres;
1660
1661         nsres = nsIDOMEvent_QueryInterface(event, &IID_nsIDOMKeyEvent, (void**)&key_event);
1662         if(NS_SUCCEEDED(nsres)) {
1663             UINT32 code = 0;
1664
1665             nsIDOMKeyEvent_GetKeyCode(key_event, &code);
1666
1667             switch(code) {
1668             case VK_F1: /* DOM_VK_F1 */
1669                 TRACE("F1 pressed\n");
1670                 fire_event(This->node.doc, EVENTID_HELP, TRUE, This->node.nsnode, NULL, NULL);
1671                 *prevent_default = TRUE;
1672             }
1673
1674             nsIDOMKeyEvent_Release(key_event);
1675         }
1676     }
1677     }
1678
1679     return S_OK;
1680 }
1681
1682 cp_static_data_t HTMLElementEvents2_data = { HTMLElementEvents2_tid, NULL /* FIXME */, TRUE };
1683
1684 const cpc_entry_t HTMLElement_cpc[] = {
1685     HTMLELEMENT_CPC,
1686     {NULL}
1687 };
1688
1689 static const NodeImplVtbl HTMLElementImplVtbl = {
1690     HTMLElement_QI,
1691     HTMLElement_destructor,
1692     HTMLElement_cpc,
1693     HTMLElement_clone,
1694     HTMLElement_handle_event,
1695     HTMLElement_get_attr_col
1696 };
1697
1698 static inline HTMLElement *impl_from_DispatchEx(DispatchEx *iface)
1699 {
1700     return CONTAINING_RECORD(iface, HTMLElement, node.dispex);
1701 }
1702
1703 static HRESULT HTMLElement_get_dispid(DispatchEx *dispex, BSTR name,
1704         DWORD grfdex, DISPID *pid)
1705 {
1706     HTMLElement *This = impl_from_DispatchEx(dispex);
1707
1708     if(This->node.vtbl->get_dispid)
1709         return This->node.vtbl->get_dispid(&This->node, name, grfdex, pid);
1710
1711     return DISP_E_UNKNOWNNAME;
1712 }
1713
1714 static HRESULT HTMLElement_invoke(DispatchEx *dispex, DISPID id, LCID lcid,
1715         WORD flags, DISPPARAMS *params, VARIANT *res, EXCEPINFO *ei,
1716         IServiceProvider *caller)
1717 {
1718     HTMLElement *This = impl_from_DispatchEx(dispex);
1719
1720     if(This->node.vtbl->invoke)
1721         return This->node.vtbl->invoke(&This->node, id, lcid, flags,
1722                 params, res, ei, caller);
1723
1724     ERR("(%p): element has no invoke method\n", This);
1725     return E_NOTIMPL;
1726 }
1727
1728 static HRESULT HTMLElement_populate_props(DispatchEx *dispex)
1729 {
1730     HTMLElement *This = impl_from_DispatchEx(dispex);
1731     nsIDOMNamedNodeMap *attrs;
1732     nsIDOMNode *node;
1733     nsAString nsstr;
1734     const PRUnichar *str;
1735     BSTR name;
1736     VARIANT value;
1737     unsigned i;
1738     UINT32 len;
1739     DISPID id;
1740     nsresult nsres;
1741     HRESULT hres;
1742
1743     if(!This->nselem)
1744         return S_FALSE;
1745
1746     nsres = nsIDOMHTMLElement_GetAttributes(This->nselem, &attrs);
1747     if(NS_FAILED(nsres))
1748         return E_FAIL;
1749
1750     nsres = nsIDOMNamedNodeMap_GetLength(attrs, &len);
1751     if(NS_FAILED(nsres)) {
1752         nsIDOMNamedNodeMap_Release(attrs);
1753         return E_FAIL;
1754     }
1755
1756     nsAString_Init(&nsstr, NULL);
1757     for(i=0; i<len; i++) {
1758         nsres = nsIDOMNamedNodeMap_Item(attrs, i, &node);
1759         if(NS_FAILED(nsres))
1760             continue;
1761
1762         nsres = nsIDOMNode_GetNodeName(node, &nsstr);
1763         if(NS_FAILED(nsres)) {
1764             nsIDOMNode_Release(node);
1765             continue;
1766         }
1767
1768         nsAString_GetData(&nsstr, &str);
1769         name = SysAllocString(str);
1770         if(!name) {
1771             nsIDOMNode_Release(node);
1772             continue;
1773         }
1774
1775         hres = IDispatchEx_GetDispID(&dispex->IDispatchEx_iface, name, fdexNameCaseInsensitive, &id);
1776         if(hres != DISP_E_UNKNOWNNAME) {
1777             nsIDOMNode_Release(node);
1778             SysFreeString(name);
1779             continue;
1780         }
1781
1782         nsres = nsIDOMNode_GetNodeValue(node, &nsstr);
1783         nsIDOMNode_Release(node);
1784         if(NS_FAILED(nsres)) {
1785             SysFreeString(name);
1786             continue;
1787         }
1788
1789         nsAString_GetData(&nsstr, &str);
1790         V_VT(&value) = VT_BSTR;
1791         if(*str) {
1792             V_BSTR(&value) = SysAllocString(str);
1793             if(!V_BSTR(&value)) {
1794                 SysFreeString(name);
1795                 continue;
1796             }
1797         } else
1798             V_BSTR(&value) = NULL;
1799
1800         IHTMLElement_setAttribute(&This->IHTMLElement_iface, name, value, 0);
1801         SysFreeString(name);
1802         VariantClear(&value);
1803     }
1804     nsAString_Finish(&nsstr);
1805
1806     nsIDOMNamedNodeMap_Release(attrs);
1807     return S_OK;
1808 }
1809
1810 static const tid_t HTMLElement_iface_tids[] = {
1811     HTMLELEMENT_TIDS,
1812     0
1813 };
1814
1815 static dispex_static_data_vtbl_t HTMLElement_dispex_vtbl = {
1816     NULL,
1817     HTMLElement_get_dispid,
1818     HTMLElement_invoke,
1819     HTMLElement_populate_props
1820 };
1821
1822 static dispex_static_data_t HTMLElement_dispex = {
1823     &HTMLElement_dispex_vtbl,
1824     DispHTMLUnknownElement_tid,
1825     NULL,
1826     HTMLElement_iface_tids
1827 };
1828
1829 void HTMLElement_Init(HTMLElement *This, HTMLDocumentNode *doc, nsIDOMHTMLElement *nselem, dispex_static_data_t *dispex_data)
1830 {
1831     This->IHTMLElement_iface.lpVtbl = &HTMLElementVtbl;
1832
1833     HTMLElement2_Init(This);
1834     HTMLElement3_Init(This);
1835
1836     if(dispex_data && !dispex_data->vtbl)
1837         dispex_data->vtbl = &HTMLElement_dispex_vtbl;
1838     init_dispex(&This->node.dispex, (IUnknown*)&This->IHTMLElement_iface,
1839             dispex_data ? dispex_data : &HTMLElement_dispex);
1840
1841     if(nselem) {
1842         HTMLDOMNode_Init(doc, &This->node, (nsIDOMNode*)nselem);
1843
1844         /* No AddRef, share reference with HTMLDOMNode */
1845         assert((nsIDOMNode*)nselem == This->node.nsnode);
1846         This->nselem = nselem;
1847     }
1848
1849     This->node.cp_container = &This->cp_container;
1850     ConnectionPointContainer_Init(&This->cp_container, (IUnknown*)&This->IHTMLElement_iface, This->node.vtbl->cpc_entries);
1851 }
1852
1853 HRESULT HTMLElement_Create(HTMLDocumentNode *doc, nsIDOMNode *nsnode, BOOL use_generic, HTMLElement **ret)
1854 {
1855     nsIDOMHTMLElement *nselem;
1856     nsAString class_name_str;
1857     const PRUnichar *class_name;
1858     const tag_desc_t *tag;
1859     HTMLElement *elem;
1860     nsresult nsres;
1861     HRESULT hres;
1862
1863     nsres = nsIDOMNode_QueryInterface(nsnode, &IID_nsIDOMHTMLElement, (void**)&nselem);
1864     if(NS_FAILED(nsres))
1865         return E_FAIL;
1866
1867     nsAString_Init(&class_name_str, NULL);
1868     nsIDOMHTMLElement_GetTagName(nselem, &class_name_str);
1869
1870     nsAString_GetData(&class_name_str, &class_name);
1871
1872     tag = get_tag_desc(class_name);
1873     if(tag) {
1874         hres = tag->constructor(doc, nselem, &elem);
1875     }else if(use_generic) {
1876         hres = HTMLGenericElement_Create(doc, nselem, &elem);
1877     }else {
1878         elem = heap_alloc_zero(sizeof(HTMLElement));
1879         if(elem) {
1880             elem->node.vtbl = &HTMLElementImplVtbl;
1881             HTMLElement_Init(elem, doc, nselem, &HTMLElement_dispex);
1882             hres = S_OK;
1883         }else {
1884             hres = E_OUTOFMEMORY;
1885         }
1886     }
1887
1888     TRACE("%s ret %p\n", debugstr_w(class_name), elem);
1889
1890     nsIDOMHTMLElement_Release(nselem);
1891     nsAString_Finish(&class_name_str);
1892     if(FAILED(hres))
1893         return hres;
1894
1895     *ret = elem;
1896     return S_OK;
1897 }
1898
1899 HRESULT get_elem(HTMLDocumentNode *doc, nsIDOMElement *nselem, HTMLElement **ret)
1900 {
1901     HTMLDOMNode *node;
1902     HRESULT hres;
1903
1904     hres = get_node(doc, (nsIDOMNode*)nselem, TRUE, &node);
1905     if(FAILED(hres))
1906         return hres;
1907
1908     *ret = impl_from_HTMLDOMNode(node);
1909     return S_OK;
1910 }
1911
1912 /* interface IHTMLFiltersCollection */
1913 static HRESULT WINAPI HTMLFiltersCollection_QueryInterface(IHTMLFiltersCollection *iface, REFIID riid, void **ppv)
1914 {
1915     HTMLFiltersCollection *This = impl_from_IHTMLFiltersCollection(iface);
1916
1917     TRACE("%p %s %p\n", This, debugstr_guid( riid ), ppv );
1918
1919     if(IsEqualGUID(&IID_IUnknown, riid)) {
1920         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
1921         *ppv = &This->IHTMLFiltersCollection_iface;
1922     }else if(IsEqualGUID(&IID_IHTMLFiltersCollection, riid)) {
1923         TRACE("(%p)->(IID_IHTMLFiltersCollection %p)\n", This, ppv);
1924         *ppv = &This->IHTMLFiltersCollection_iface;
1925     }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
1926         return *ppv ? S_OK : E_NOINTERFACE;
1927     }
1928
1929     if(*ppv) {
1930         IUnknown_AddRef((IUnknown*)*ppv);
1931         return S_OK;
1932     }
1933
1934     FIXME("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
1935     return E_NOINTERFACE;
1936 }
1937
1938 static ULONG WINAPI HTMLFiltersCollection_AddRef(IHTMLFiltersCollection *iface)
1939 {
1940     HTMLFiltersCollection *This = impl_from_IHTMLFiltersCollection(iface);
1941     LONG ref = InterlockedIncrement(&This->ref);
1942
1943     TRACE("(%p) ref=%d\n", This, ref);
1944
1945     return ref;
1946 }
1947
1948 static ULONG WINAPI HTMLFiltersCollection_Release(IHTMLFiltersCollection *iface)
1949 {
1950     HTMLFiltersCollection *This = impl_from_IHTMLFiltersCollection(iface);
1951     LONG ref = InterlockedDecrement(&This->ref);
1952
1953     TRACE("(%p) ref=%d\n", This, ref);
1954
1955     if(!ref)
1956     {
1957         heap_free(This);
1958     }
1959
1960     return ref;
1961 }
1962
1963 static HRESULT WINAPI HTMLFiltersCollection_GetTypeInfoCount(IHTMLFiltersCollection *iface, UINT *pctinfo)
1964 {
1965     HTMLFiltersCollection *This = impl_from_IHTMLFiltersCollection(iface);
1966     return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
1967 }
1968
1969 static HRESULT WINAPI HTMLFiltersCollection_GetTypeInfo(IHTMLFiltersCollection *iface,
1970                                     UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
1971 {
1972     HTMLFiltersCollection *This = impl_from_IHTMLFiltersCollection(iface);
1973     return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
1974 }
1975
1976 static HRESULT WINAPI HTMLFiltersCollection_GetIDsOfNames(IHTMLFiltersCollection *iface,
1977                                     REFIID riid, LPOLESTR *rgszNames, UINT cNames,
1978                                     LCID lcid, DISPID *rgDispId)
1979 {
1980     HTMLFiltersCollection *This = impl_from_IHTMLFiltersCollection(iface);
1981     return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
1982             lcid, rgDispId);
1983 }
1984
1985 static HRESULT WINAPI HTMLFiltersCollection_Invoke(IHTMLFiltersCollection *iface, DISPID dispIdMember, REFIID riid,
1986                                     LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
1987                                     EXCEPINFO *pExcepInfo, UINT *puArgErr)
1988 {
1989     HTMLFiltersCollection *This = impl_from_IHTMLFiltersCollection(iface);
1990     return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
1991             wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
1992 }
1993
1994 static HRESULT WINAPI HTMLFiltersCollection_get_length(IHTMLFiltersCollection *iface, LONG *p)
1995 {
1996     HTMLFiltersCollection *This = impl_from_IHTMLFiltersCollection(iface);
1997
1998     if(!p)
1999         return E_POINTER;
2000
2001     FIXME("(%p)->(%p) Always returning 0\n", This, p);
2002     *p = 0;
2003
2004     return S_OK;
2005 }
2006
2007 static HRESULT WINAPI HTMLFiltersCollection_get__newEnum(IHTMLFiltersCollection *iface, IUnknown **p)
2008 {
2009     HTMLFiltersCollection *This = impl_from_IHTMLFiltersCollection(iface);
2010     FIXME("(%p)->(%p)\n", This, p);
2011     return E_NOTIMPL;
2012 }
2013
2014 static HRESULT WINAPI HTMLFiltersCollection_item(IHTMLFiltersCollection *iface, VARIANT *pvarIndex, VARIANT *pvarResult)
2015 {
2016     HTMLFiltersCollection *This = impl_from_IHTMLFiltersCollection(iface);
2017     FIXME("(%p)->(%p, %p)\n", This, pvarIndex, pvarResult);
2018     return E_NOTIMPL;
2019 }
2020
2021 static const IHTMLFiltersCollectionVtbl HTMLFiltersCollectionVtbl = {
2022     HTMLFiltersCollection_QueryInterface,
2023     HTMLFiltersCollection_AddRef,
2024     HTMLFiltersCollection_Release,
2025     HTMLFiltersCollection_GetTypeInfoCount,
2026     HTMLFiltersCollection_GetTypeInfo,
2027     HTMLFiltersCollection_GetIDsOfNames,
2028     HTMLFiltersCollection_Invoke,
2029     HTMLFiltersCollection_get_length,
2030     HTMLFiltersCollection_get__newEnum,
2031     HTMLFiltersCollection_item
2032 };
2033
2034 static HRESULT HTMLFiltersCollection_get_dispid(DispatchEx *dispex, BSTR name, DWORD flags, DISPID *dispid)
2035 {
2036     WCHAR *ptr;
2037     int idx = 0;
2038
2039     for(ptr = name; *ptr && isdigitW(*ptr); ptr++)
2040         idx = idx*10 + (*ptr-'0');
2041     if(*ptr)
2042         return DISP_E_UNKNOWNNAME;
2043
2044     *dispid = MSHTML_DISPID_CUSTOM_MIN + idx;
2045     TRACE("ret %x\n", *dispid);
2046     return S_OK;
2047 }
2048
2049 static HRESULT HTMLFiltersCollection_invoke(DispatchEx *dispex, DISPID id, LCID lcid, WORD flags, DISPPARAMS *params,
2050         VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
2051 {
2052     TRACE("(%p)->(%x %x %x %p %p %p)\n", dispex, id, lcid, flags, params, res, ei);
2053
2054     V_VT(res) = VT_DISPATCH;
2055     V_DISPATCH(res) = NULL;
2056
2057     FIXME("always returning NULL\n");
2058
2059     return S_OK;
2060 }
2061
2062 static const dispex_static_data_vtbl_t HTMLFiltersCollection_dispex_vtbl = {
2063     NULL,
2064     HTMLFiltersCollection_get_dispid,
2065     HTMLFiltersCollection_invoke,
2066     NULL
2067 };
2068
2069 static const tid_t HTMLFiltersCollection_iface_tids[] = {
2070     IHTMLFiltersCollection_tid,
2071     0
2072 };
2073 static dispex_static_data_t HTMLFiltersCollection_dispex = {
2074     &HTMLFiltersCollection_dispex_vtbl,
2075     IHTMLFiltersCollection_tid,
2076     NULL,
2077     HTMLFiltersCollection_iface_tids
2078 };
2079
2080 static IHTMLFiltersCollection *HTMLFiltersCollection_Create(void)
2081 {
2082     HTMLFiltersCollection *ret = heap_alloc(sizeof(HTMLFiltersCollection));
2083
2084     ret->IHTMLFiltersCollection_iface.lpVtbl = &HTMLFiltersCollectionVtbl;
2085     ret->ref = 1;
2086
2087     init_dispex(&ret->dispex, (IUnknown*)&ret->IHTMLFiltersCollection_iface,
2088             &HTMLFiltersCollection_dispex);
2089
2090     return &ret->IHTMLFiltersCollection_iface;
2091 }
2092
2093 /* interface IHTMLAttributeCollection */
2094 static inline HTMLAttributeCollection *impl_from_IHTMLAttributeCollection(IHTMLAttributeCollection *iface)
2095 {
2096     return CONTAINING_RECORD(iface, HTMLAttributeCollection, IHTMLAttributeCollection_iface);
2097 }
2098
2099 static HRESULT WINAPI HTMLAttributeCollection_QueryInterface(IHTMLAttributeCollection *iface, REFIID riid, void **ppv)
2100 {
2101     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection(iface);
2102
2103     *ppv = NULL;
2104
2105     if(IsEqualGUID(&IID_IUnknown, riid)) {
2106         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
2107         *ppv = &This->IHTMLAttributeCollection_iface;
2108     }else if(IsEqualGUID(&IID_IHTMLAttributeCollection, riid)) {
2109         TRACE("(%p)->(IID_IHTMLAttributeCollection %p)\n", This, ppv);
2110         *ppv = &This->IHTMLAttributeCollection_iface;
2111     }else if(IsEqualGUID(&IID_IHTMLAttributeCollection2, riid)) {
2112         TRACE("(%p)->(IID_IHTMLAttributeCollection2 %p)\n", This, ppv);
2113         *ppv = &This->IHTMLAttributeCollection2_iface;
2114     }else if(IsEqualGUID(&IID_IHTMLAttributeCollection3, riid)) {
2115         TRACE("(%p)->(IID_IHTMLAttributeCollection3 %p)\n", This, ppv);
2116         *ppv = &This->IHTMLAttributeCollection3_iface;
2117     }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
2118         return *ppv ? S_OK : E_NOINTERFACE;
2119     }
2120
2121     if(*ppv) {
2122         IUnknown_AddRef((IUnknown*)*ppv);
2123         return S_OK;
2124     }
2125
2126     WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
2127     return E_NOINTERFACE;
2128 }
2129
2130 static ULONG WINAPI HTMLAttributeCollection_AddRef(IHTMLAttributeCollection *iface)
2131 {
2132     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection(iface);
2133     LONG ref = InterlockedIncrement(&This->ref);
2134
2135     TRACE("(%p) ref=%d\n", This, ref);
2136
2137     return ref;
2138 }
2139
2140 static ULONG WINAPI HTMLAttributeCollection_Release(IHTMLAttributeCollection *iface)
2141 {
2142     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection(iface);
2143     LONG ref = InterlockedDecrement(&This->ref);
2144
2145     TRACE("(%p) ref=%d\n", This, ref);
2146
2147     if(!ref) {
2148         while(!list_empty(&This->attrs)) {
2149             HTMLDOMAttribute *attr = LIST_ENTRY(list_head(&This->attrs), HTMLDOMAttribute, entry);
2150
2151             list_remove(&attr->entry);
2152             attr->elem = NULL;
2153             IHTMLDOMAttribute_Release(&attr->IHTMLDOMAttribute_iface);
2154         }
2155
2156         heap_free(This);
2157     }
2158
2159     return ref;
2160 }
2161
2162 static HRESULT WINAPI HTMLAttributeCollection_GetTypeInfoCount(IHTMLAttributeCollection *iface, UINT *pctinfo)
2163 {
2164     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection(iface);
2165     return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
2166 }
2167
2168 static HRESULT WINAPI HTMLAttributeCollection_GetTypeInfo(IHTMLAttributeCollection *iface, UINT iTInfo,
2169         LCID lcid, ITypeInfo **ppTInfo)
2170 {
2171     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection(iface);
2172     return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
2173 }
2174
2175 static HRESULT WINAPI HTMLAttributeCollection_GetIDsOfNames(IHTMLAttributeCollection *iface, REFIID riid,
2176         LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
2177 {
2178     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection(iface);
2179     return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
2180             lcid, rgDispId);
2181 }
2182
2183 static HRESULT WINAPI HTMLAttributeCollection_Invoke(IHTMLAttributeCollection *iface, DISPID dispIdMember,
2184         REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
2185         VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
2186 {
2187     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection(iface);
2188     return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
2189             wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
2190 }
2191
2192 static HRESULT get_attr_dispid_by_idx(HTMLAttributeCollection *This, LONG *idx, DISPID *dispid)
2193 {
2194     IDispatchEx *dispex = &This->elem->node.dispex.IDispatchEx_iface;
2195     DISPID id = DISPID_STARTENUM;
2196     LONG len = -1;
2197     HRESULT hres;
2198
2199     FIXME("filter non-enumerable attributes out\n");
2200
2201     while(1) {
2202         hres = IDispatchEx_GetNextDispID(dispex, fdexEnumAll, id, &id);
2203         if(FAILED(hres))
2204             return hres;
2205         else if(hres == S_FALSE)
2206             break;
2207
2208         len++;
2209         if(len == *idx)
2210             break;
2211     }
2212
2213     if(dispid) {
2214         *dispid = id;
2215         return *idx==len ? S_OK : DISP_E_UNKNOWNNAME;
2216     }
2217
2218     *idx = len+1;
2219     return S_OK;
2220 }
2221
2222 static inline HRESULT get_attr_dispid_by_name(HTMLAttributeCollection *This, BSTR name, DISPID *id)
2223 {
2224     HRESULT hres;
2225
2226     if(name[0]>='0' && name[0]<='9') {
2227         WCHAR *end_ptr;
2228         LONG idx;
2229
2230         idx = strtoulW(name, &end_ptr, 10);
2231         if(!*end_ptr) {
2232             hres = get_attr_dispid_by_idx(This, &idx, id);
2233             if(SUCCEEDED(hres))
2234                 return hres;
2235         }
2236     }
2237
2238     if(!This->elem) {
2239         WARN("NULL elem\n");
2240         return E_UNEXPECTED;
2241     }
2242
2243     hres = IDispatchEx_GetDispID(&This->elem->node.dispex.IDispatchEx_iface,
2244             name, fdexNameCaseInsensitive, id);
2245     return hres;
2246 }
2247
2248 static inline HRESULT get_domattr(HTMLAttributeCollection *This, DISPID id, LONG *list_pos, HTMLDOMAttribute **attr)
2249 {
2250     HTMLDOMAttribute *iter;
2251     LONG pos = 0;
2252     HRESULT hres;
2253
2254     *attr = NULL;
2255     LIST_FOR_EACH_ENTRY(iter, &This->attrs, HTMLDOMAttribute, entry) {
2256         if(iter->dispid == id) {
2257             *attr = iter;
2258             break;
2259         }
2260         pos++;
2261     }
2262
2263     if(!*attr) {
2264         if(!This->elem) {
2265             WARN("NULL elem\n");
2266             return E_UNEXPECTED;
2267         }
2268
2269         hres = HTMLDOMAttribute_Create(NULL, This->elem, id, attr);
2270         if(FAILED(hres))
2271             return hres;
2272     }
2273
2274     IHTMLDOMAttribute_AddRef(&(*attr)->IHTMLDOMAttribute_iface);
2275     if(list_pos)
2276         *list_pos = pos;
2277     return S_OK;
2278 }
2279
2280 static HRESULT WINAPI HTMLAttributeCollection_get_length(IHTMLAttributeCollection *iface, LONG *p)
2281 {
2282     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection(iface);
2283     HRESULT hres;
2284
2285     TRACE("(%p)->(%p)\n", This, p);
2286
2287     *p = -1;
2288     hres = get_attr_dispid_by_idx(This, p, NULL);
2289     return hres;
2290 }
2291
2292 static HRESULT WINAPI HTMLAttributeCollection__newEnum(IHTMLAttributeCollection *iface, IUnknown **p)
2293 {
2294     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection(iface);
2295     FIXME("(%p)->(%p)\n", This, p);
2296     return E_NOTIMPL;
2297 }
2298
2299 static HRESULT WINAPI HTMLAttributeCollection_item(IHTMLAttributeCollection *iface, VARIANT *name, IDispatch **ppItem)
2300 {
2301     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection(iface);
2302     HTMLDOMAttribute *attr;
2303     DISPID id;
2304     HRESULT hres;
2305
2306     TRACE("(%p)->(%s %p)\n", This, debugstr_variant(name), ppItem);
2307
2308     switch(V_VT(name)) {
2309     case VT_I4:
2310         hres = get_attr_dispid_by_idx(This, &V_I4(name), &id);
2311         break;
2312     case VT_BSTR:
2313         hres = get_attr_dispid_by_name(This, V_BSTR(name), &id);
2314         break;
2315     default:
2316         FIXME("unsupported name %s\n", debugstr_variant(name));
2317         hres = E_NOTIMPL;
2318     }
2319     if(hres == DISP_E_UNKNOWNNAME)
2320         return E_INVALIDARG;
2321     if(FAILED(hres))
2322         return hres;
2323
2324     hres = get_domattr(This, id, NULL, &attr);
2325     if(FAILED(hres))
2326         return hres;
2327
2328     *ppItem = (IDispatch*)&attr->IHTMLDOMAttribute_iface;
2329     return S_OK;
2330 }
2331
2332 static const IHTMLAttributeCollectionVtbl HTMLAttributeCollectionVtbl = {
2333     HTMLAttributeCollection_QueryInterface,
2334     HTMLAttributeCollection_AddRef,
2335     HTMLAttributeCollection_Release,
2336     HTMLAttributeCollection_GetTypeInfoCount,
2337     HTMLAttributeCollection_GetTypeInfo,
2338     HTMLAttributeCollection_GetIDsOfNames,
2339     HTMLAttributeCollection_Invoke,
2340     HTMLAttributeCollection_get_length,
2341     HTMLAttributeCollection__newEnum,
2342     HTMLAttributeCollection_item
2343 };
2344
2345 static inline HTMLAttributeCollection *impl_from_IHTMLAttributeCollection2(IHTMLAttributeCollection2 *iface)
2346 {
2347     return CONTAINING_RECORD(iface, HTMLAttributeCollection, IHTMLAttributeCollection2_iface);
2348 }
2349
2350 static HRESULT WINAPI HTMLAttributeCollection2_QueryInterface(IHTMLAttributeCollection2 *iface, REFIID riid, void **ppv)
2351 {
2352     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection2(iface);
2353     return IHTMLAttributeCollection_QueryInterface(&This->IHTMLAttributeCollection_iface, riid, ppv);
2354 }
2355
2356 static ULONG WINAPI HTMLAttributeCollection2_AddRef(IHTMLAttributeCollection2 *iface)
2357 {
2358     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection2(iface);
2359     return IHTMLAttributeCollection_AddRef(&This->IHTMLAttributeCollection_iface);
2360 }
2361
2362 static ULONG WINAPI HTMLAttributeCollection2_Release(IHTMLAttributeCollection2 *iface)
2363 {
2364     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection2(iface);
2365     return IHTMLAttributeCollection_Release(&This->IHTMLAttributeCollection_iface);
2366 }
2367
2368 static HRESULT WINAPI HTMLAttributeCollection2_GetTypeInfoCount(IHTMLAttributeCollection2 *iface, UINT *pctinfo)
2369 {
2370     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection2(iface);
2371     return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
2372 }
2373
2374 static HRESULT WINAPI HTMLAttributeCollection2_GetTypeInfo(IHTMLAttributeCollection2 *iface, UINT iTInfo,
2375         LCID lcid, ITypeInfo **ppTInfo)
2376 {
2377     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection2(iface);
2378     return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
2379 }
2380
2381 static HRESULT WINAPI HTMLAttributeCollection2_GetIDsOfNames(IHTMLAttributeCollection2 *iface, REFIID riid,
2382         LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
2383 {
2384     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection2(iface);
2385     return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
2386             lcid, rgDispId);
2387 }
2388
2389 static HRESULT WINAPI HTMLAttributeCollection2_Invoke(IHTMLAttributeCollection2 *iface, DISPID dispIdMember,
2390         REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
2391         VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
2392 {
2393     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection2(iface);
2394     return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
2395             wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
2396 }
2397
2398 static HRESULT WINAPI HTMLAttributeCollection2_getNamedItem(IHTMLAttributeCollection2 *iface, BSTR bstrName,
2399         IHTMLDOMAttribute **newretNode)
2400 {
2401     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection2(iface);
2402     HTMLDOMAttribute *attr;
2403     DISPID id;
2404     HRESULT hres;
2405
2406     TRACE("(%p)->(%s %p)\n", This, debugstr_w(bstrName), newretNode);
2407
2408     hres = get_attr_dispid_by_name(This, bstrName, &id);
2409     if(hres == DISP_E_UNKNOWNNAME) {
2410         *newretNode = NULL;
2411         return S_OK;
2412     } else if(FAILED(hres)) {
2413         return hres;
2414     }
2415
2416     hres = get_domattr(This, id, NULL, &attr);
2417     if(FAILED(hres))
2418         return hres;
2419
2420     *newretNode = &attr->IHTMLDOMAttribute_iface;
2421     return S_OK;
2422 }
2423
2424 static HRESULT WINAPI HTMLAttributeCollection2_setNamedItem(IHTMLAttributeCollection2 *iface,
2425         IHTMLDOMAttribute *ppNode, IHTMLDOMAttribute **newretNode)
2426 {
2427     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection2(iface);
2428     FIXME("(%p)->(%p %p)\n", This, ppNode, newretNode);
2429     return E_NOTIMPL;
2430 }
2431
2432 static HRESULT WINAPI HTMLAttributeCollection2_removeNamedItem(IHTMLAttributeCollection2 *iface,
2433         BSTR bstrName, IHTMLDOMAttribute **newretNode)
2434 {
2435     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection2(iface);
2436     FIXME("(%p)->(%s %p)\n", This, debugstr_w(bstrName), newretNode);
2437     return E_NOTIMPL;
2438 }
2439
2440 static const IHTMLAttributeCollection2Vtbl HTMLAttributeCollection2Vtbl = {
2441     HTMLAttributeCollection2_QueryInterface,
2442     HTMLAttributeCollection2_AddRef,
2443     HTMLAttributeCollection2_Release,
2444     HTMLAttributeCollection2_GetTypeInfoCount,
2445     HTMLAttributeCollection2_GetTypeInfo,
2446     HTMLAttributeCollection2_GetIDsOfNames,
2447     HTMLAttributeCollection2_Invoke,
2448     HTMLAttributeCollection2_getNamedItem,
2449     HTMLAttributeCollection2_setNamedItem,
2450     HTMLAttributeCollection2_removeNamedItem
2451 };
2452
2453 static inline HTMLAttributeCollection *impl_from_IHTMLAttributeCollection3(IHTMLAttributeCollection3 *iface)
2454 {
2455     return CONTAINING_RECORD(iface, HTMLAttributeCollection, IHTMLAttributeCollection3_iface);
2456 }
2457
2458 static HRESULT WINAPI HTMLAttributeCollection3_QueryInterface(IHTMLAttributeCollection3 *iface, REFIID riid, void **ppv)
2459 {
2460     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection3(iface);
2461     return IHTMLAttributeCollection_QueryInterface(&This->IHTMLAttributeCollection_iface, riid, ppv);
2462 }
2463
2464 static ULONG WINAPI HTMLAttributeCollection3_AddRef(IHTMLAttributeCollection3 *iface)
2465 {
2466     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection3(iface);
2467     return IHTMLAttributeCollection_AddRef(&This->IHTMLAttributeCollection_iface);
2468 }
2469
2470 static ULONG WINAPI HTMLAttributeCollection3_Release(IHTMLAttributeCollection3 *iface)
2471 {
2472     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection3(iface);
2473     return IHTMLAttributeCollection_Release(&This->IHTMLAttributeCollection_iface);
2474 }
2475
2476 static HRESULT WINAPI HTMLAttributeCollection3_GetTypeInfoCount(IHTMLAttributeCollection3 *iface, UINT *pctinfo)
2477 {
2478     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection3(iface);
2479     return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
2480 }
2481
2482 static HRESULT WINAPI HTMLAttributeCollection3_GetTypeInfo(IHTMLAttributeCollection3 *iface, UINT iTInfo,
2483         LCID lcid, ITypeInfo **ppTInfo)
2484 {
2485     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection3(iface);
2486     return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
2487 }
2488
2489 static HRESULT WINAPI HTMLAttributeCollection3_GetIDsOfNames(IHTMLAttributeCollection3 *iface, REFIID riid,
2490         LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
2491 {
2492     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection3(iface);
2493     return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames, cNames,
2494             lcid, rgDispId);
2495 }
2496
2497 static HRESULT WINAPI HTMLAttributeCollection3_Invoke(IHTMLAttributeCollection3 *iface, DISPID dispIdMember,
2498         REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
2499         VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
2500 {
2501     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection3(iface);
2502     return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid, lcid,
2503             wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
2504 }
2505
2506 static HRESULT WINAPI HTMLAttributeCollection3_getNamedItem(IHTMLAttributeCollection3 *iface, BSTR bstrName,
2507         IHTMLDOMAttribute **ppNodeOut)
2508 {
2509     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection3(iface);
2510     return IHTMLAttributeCollection2_getNamedItem(&This->IHTMLAttributeCollection2_iface, bstrName, ppNodeOut);
2511 }
2512
2513 static HRESULT WINAPI HTMLAttributeCollection3_setNamedItem(IHTMLAttributeCollection3 *iface,
2514         IHTMLDOMAttribute *pNodeIn, IHTMLDOMAttribute **ppNodeOut)
2515 {
2516     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection3(iface);
2517     FIXME("(%p)->(%p %p)\n", This, pNodeIn, ppNodeOut);
2518     return E_NOTIMPL;
2519 }
2520
2521 static HRESULT WINAPI HTMLAttributeCollection3_removeNamedItem(IHTMLAttributeCollection3 *iface,
2522         BSTR bstrName, IHTMLDOMAttribute **ppNodeOut)
2523 {
2524     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection3(iface);
2525     FIXME("(%p)->(%s %p)\n", This, debugstr_w(bstrName), ppNodeOut);
2526     return E_NOTIMPL;
2527 }
2528
2529 static HRESULT WINAPI HTMLAttributeCollection3_item(IHTMLAttributeCollection3 *iface, LONG index, IHTMLDOMAttribute **ppNodeOut)
2530 {
2531     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection3(iface);
2532     HTMLDOMAttribute *attr;
2533     DISPID id;
2534     HRESULT hres;
2535
2536     TRACE("(%p)->(%d %p)\n", This, index, ppNodeOut);
2537
2538     hres = get_attr_dispid_by_idx(This, &index, &id);
2539     if(hres == DISP_E_UNKNOWNNAME)
2540         return E_INVALIDARG;
2541     if(FAILED(hres))
2542         return hres;
2543
2544     hres = get_domattr(This, id, NULL, &attr);
2545     if(FAILED(hres))
2546         return hres;
2547
2548     *ppNodeOut = &attr->IHTMLDOMAttribute_iface;
2549     return S_OK;
2550 }
2551
2552 static HRESULT WINAPI HTMLAttributeCollection3_get_length(IHTMLAttributeCollection3 *iface, LONG *p)
2553 {
2554     HTMLAttributeCollection *This = impl_from_IHTMLAttributeCollection3(iface);
2555     return IHTMLAttributeCollection_get_length(&This->IHTMLAttributeCollection_iface, p);
2556 }
2557
2558 static const IHTMLAttributeCollection3Vtbl HTMLAttributeCollection3Vtbl = {
2559     HTMLAttributeCollection3_QueryInterface,
2560     HTMLAttributeCollection3_AddRef,
2561     HTMLAttributeCollection3_Release,
2562     HTMLAttributeCollection3_GetTypeInfoCount,
2563     HTMLAttributeCollection3_GetTypeInfo,
2564     HTMLAttributeCollection3_GetIDsOfNames,
2565     HTMLAttributeCollection3_Invoke,
2566     HTMLAttributeCollection3_getNamedItem,
2567     HTMLAttributeCollection3_setNamedItem,
2568     HTMLAttributeCollection3_removeNamedItem,
2569     HTMLAttributeCollection3_item,
2570     HTMLAttributeCollection3_get_length
2571 };
2572
2573 static inline HTMLAttributeCollection *HTMLAttributeCollection_from_DispatchEx(DispatchEx *iface)
2574 {
2575     return CONTAINING_RECORD(iface, HTMLAttributeCollection, dispex);
2576 }
2577
2578 static HRESULT HTMLAttributeCollection_get_dispid(DispatchEx *dispex, BSTR name, DWORD flags, DISPID *dispid)
2579 {
2580     HTMLAttributeCollection *This = HTMLAttributeCollection_from_DispatchEx(dispex);
2581     HTMLDOMAttribute *attr;
2582     LONG pos;
2583     HRESULT hres;
2584
2585     TRACE("(%p)->(%s %x %p)\n", This, debugstr_w(name), flags, dispid);
2586
2587     hres = get_attr_dispid_by_name(This, name, dispid);
2588     if(FAILED(hres))
2589         return hres;
2590
2591     hres = get_domattr(This, *dispid, &pos, &attr);
2592     if(FAILED(hres))
2593         return hres;
2594     IHTMLDOMAttribute_Release(&attr->IHTMLDOMAttribute_iface);
2595
2596     *dispid = MSHTML_DISPID_CUSTOM_MIN+pos;
2597     return S_OK;
2598 }
2599
2600 static HRESULT HTMLAttributeCollection_invoke(DispatchEx *dispex, DISPID id, LCID lcid,
2601         WORD flags, DISPPARAMS *params, VARIANT *res, EXCEPINFO *ei, IServiceProvider *caller)
2602 {
2603     HTMLAttributeCollection *This = HTMLAttributeCollection_from_DispatchEx(dispex);
2604
2605     TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, flags, params, res, ei, caller);
2606
2607     switch(flags) {
2608     case DISPATCH_PROPERTYGET: {
2609         HTMLDOMAttribute *iter;
2610         DWORD pos;
2611
2612         pos = id-MSHTML_DISPID_CUSTOM_MIN;
2613
2614         LIST_FOR_EACH_ENTRY(iter, &This->attrs, HTMLDOMAttribute, entry) {
2615             if(!pos) {
2616                 IHTMLDOMAttribute_AddRef(&iter->IHTMLDOMAttribute_iface);
2617                 V_VT(res) = VT_DISPATCH;
2618                 V_DISPATCH(res) = (IDispatch*)&iter->IHTMLDOMAttribute_iface;
2619                 return S_OK;
2620             }
2621             pos--;
2622         }
2623
2624         WARN("invalid arg\n");
2625         return E_INVALIDARG;
2626     }
2627
2628     default:
2629         FIXME("unimplemented flags %x\n", flags);
2630         return E_NOTIMPL;
2631     }
2632 }
2633
2634 static const dispex_static_data_vtbl_t HTMLAttributeCollection_dispex_vtbl = {
2635     NULL,
2636     HTMLAttributeCollection_get_dispid,
2637     HTMLAttributeCollection_invoke,
2638     NULL
2639 };
2640
2641 static const tid_t HTMLAttributeCollection_iface_tids[] = {
2642     IHTMLAttributeCollection_tid,
2643     IHTMLAttributeCollection2_tid,
2644     IHTMLAttributeCollection3_tid,
2645     0
2646 };
2647
2648 static dispex_static_data_t HTMLAttributeCollection_dispex = {
2649     &HTMLAttributeCollection_dispex_vtbl,
2650     DispHTMLAttributeCollection_tid,
2651     NULL,
2652     HTMLAttributeCollection_iface_tids
2653 };
2654
2655 HRESULT HTMLElement_get_attr_col(HTMLDOMNode *iface, HTMLAttributeCollection **ac)
2656 {
2657     HTMLElement *This = impl_from_HTMLDOMNode(iface);
2658
2659     if(This->attrs) {
2660         IHTMLAttributeCollection_AddRef(&This->attrs->IHTMLAttributeCollection_iface);
2661         *ac = This->attrs;
2662         return S_OK;
2663     }
2664
2665     This->attrs = heap_alloc_zero(sizeof(HTMLAttributeCollection));
2666     if(!This->attrs)
2667         return E_OUTOFMEMORY;
2668
2669     This->attrs->IHTMLAttributeCollection_iface.lpVtbl = &HTMLAttributeCollectionVtbl;
2670     This->attrs->IHTMLAttributeCollection2_iface.lpVtbl = &HTMLAttributeCollection2Vtbl;
2671     This->attrs->IHTMLAttributeCollection3_iface.lpVtbl = &HTMLAttributeCollection3Vtbl;
2672     This->attrs->ref = 2;
2673
2674     This->attrs->elem = This;
2675     list_init(&This->attrs->attrs);
2676     init_dispex(&This->attrs->dispex, (IUnknown*)&This->attrs->IHTMLAttributeCollection_iface,
2677             &HTMLAttributeCollection_dispex);
2678
2679     *ac = This->attrs;
2680     return S_OK;
2681 }