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