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