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