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