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