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