include: Add event trace flags and guid to evntrace.h.
[wine.git] / dlls / mshtml / htmlform.c
blob1f38ad676d76bf9318dd6551491ff60210d03245
1 /*
2 * Copyright 2009 Andrew Eikum for CodeWeavers
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.
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.
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
19 #include <stdarg.h>
20 #include <assert.h>
22 #define COBJMACROS
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "ole2.h"
29 #include "wine/debug.h"
31 #include "mshtml_private.h"
32 #include "htmlevent.h"
33 #include "binding.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
37 struct HTMLFormElement {
38 HTMLElement element;
40 IHTMLFormElement IHTMLFormElement_iface;
42 nsIDOMHTMLFormElement *nsform;
45 static HRESULT htmlform_item(HTMLFormElement *This, int i, IDispatch **ret)
47 nsIDOMHTMLCollection *elements;
48 nsIDOMNode *item;
49 HTMLDOMNode *node;
50 nsresult nsres;
51 HRESULT hres;
53 nsres = nsIDOMHTMLFormElement_GetElements(This->nsform, &elements);
54 if(NS_FAILED(nsres)) {
55 FIXME("GetElements failed: 0x%08x\n", nsres);
56 return E_FAIL;
59 nsres = nsIDOMHTMLCollection_Item(elements, i, &item);
60 nsIDOMHTMLCollection_Release(elements);
61 if(NS_FAILED(nsres)) {
62 FIXME("Item failed: 0x%08x\n", nsres);
63 return E_FAIL;
66 if(item) {
67 hres = get_node(This->element.node.doc, item, TRUE, &node);
68 if(FAILED(hres))
69 return hres;
71 nsIDOMNode_Release(item);
72 *ret = (IDispatch*)&node->IHTMLDOMNode_iface;
73 }else {
74 *ret = NULL;
77 return S_OK;
80 static inline HTMLFormElement *impl_from_IHTMLFormElement(IHTMLFormElement *iface)
82 return CONTAINING_RECORD(iface, HTMLFormElement, IHTMLFormElement_iface);
85 static HRESULT WINAPI HTMLFormElement_QueryInterface(IHTMLFormElement *iface,
86 REFIID riid, void **ppv)
88 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
90 return IHTMLDOMNode_QueryInterface(&This->element.node.IHTMLDOMNode_iface, riid, ppv);
93 static ULONG WINAPI HTMLFormElement_AddRef(IHTMLFormElement *iface)
95 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
97 return IHTMLDOMNode_AddRef(&This->element.node.IHTMLDOMNode_iface);
100 static ULONG WINAPI HTMLFormElement_Release(IHTMLFormElement *iface)
102 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
104 return IHTMLDOMNode_Release(&This->element.node.IHTMLDOMNode_iface);
107 static HRESULT WINAPI HTMLFormElement_GetTypeInfoCount(IHTMLFormElement *iface, UINT *pctinfo)
109 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
110 return IDispatchEx_GetTypeInfoCount(&This->element.node.event_target.dispex.IDispatchEx_iface, pctinfo);
113 static HRESULT WINAPI HTMLFormElement_GetTypeInfo(IHTMLFormElement *iface, UINT iTInfo,
114 LCID lcid, ITypeInfo **ppTInfo)
116 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
117 return IDispatchEx_GetTypeInfo(&This->element.node.event_target.dispex.IDispatchEx_iface, iTInfo, lcid,
118 ppTInfo);
121 static HRESULT WINAPI HTMLFormElement_GetIDsOfNames(IHTMLFormElement *iface, REFIID riid,
122 LPOLESTR *rgszNames, UINT cNames,
123 LCID lcid, DISPID *rgDispId)
125 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
126 return IDispatchEx_GetIDsOfNames(&This->element.node.event_target.dispex.IDispatchEx_iface, riid, rgszNames,
127 cNames, lcid, rgDispId);
130 static HRESULT WINAPI HTMLFormElement_Invoke(IHTMLFormElement *iface, DISPID dispIdMember,
131 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
132 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
134 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
135 return IDispatchEx_Invoke(&This->element.node.event_target.dispex.IDispatchEx_iface, dispIdMember, riid,
136 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
139 static HRESULT WINAPI HTMLFormElement_put_action(IHTMLFormElement *iface, BSTR v)
141 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
142 nsAString action_str;
143 nsresult nsres;
145 TRACE("(%p)->(%s)\n", This, wine_dbgstr_w(v));
147 nsAString_InitDepend(&action_str, v);
148 nsres = nsIDOMHTMLFormElement_SetAction(This->nsform, &action_str);
149 nsAString_Finish(&action_str);
150 if(NS_FAILED(nsres)) {
151 ERR("SetAction failed: %08x\n", nsres);
152 return E_FAIL;
155 return S_OK;
158 static HRESULT WINAPI HTMLFormElement_get_action(IHTMLFormElement *iface, BSTR *p)
160 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
161 nsAString action_str;
162 nsresult nsres;
163 HRESULT hres;
165 TRACE("(%p)->(%p)\n", This, p);
167 nsAString_Init(&action_str, NULL);
168 nsres = nsIDOMHTMLFormElement_GetAction(This->nsform, &action_str);
169 if(NS_SUCCEEDED(nsres)) {
170 const PRUnichar *action;
171 nsAString_GetData(&action_str, &action);
172 hres = nsuri_to_url(action, FALSE, p);
173 }else {
174 ERR("GetAction failed: %08x\n", nsres);
175 hres = E_FAIL;
178 nsAString_Finish(&action_str);
179 return hres;
182 static HRESULT WINAPI HTMLFormElement_put_dir(IHTMLFormElement *iface, BSTR v)
184 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
185 FIXME("(%p)->(%s)\n", This, wine_dbgstr_w(v));
186 return E_NOTIMPL;
189 static HRESULT WINAPI HTMLFormElement_get_dir(IHTMLFormElement *iface, BSTR *p)
191 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
192 FIXME("(%p)->(%p)\n", This, p);
193 return E_NOTIMPL;
196 static HRESULT WINAPI HTMLFormElement_put_encoding(IHTMLFormElement *iface, BSTR v)
198 static const WCHAR urlencodedW[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
199 'x','-','w','w','w','-','f','o','r','m','-','u','r','l','e','n','c','o','d','e','d',0};
200 static const WCHAR dataW[] = {'m','u','l','t','i','p','a','r','t','/',
201 'f','o','r','m','-','d','a','t','a',0};
202 static const WCHAR plainW[] = {'t','e','x','t','/','p','l','a','i','n',0};
204 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
205 nsAString encoding_str;
206 nsresult nsres;
208 TRACE("(%p)->(%s)\n", This, wine_dbgstr_w(v));
210 if(lstrcmpiW(v, urlencodedW) && lstrcmpiW(v, dataW) && lstrcmpiW(v, plainW)) {
211 WARN("incorrect enctype\n");
212 return E_INVALIDARG;
215 nsAString_InitDepend(&encoding_str, v);
216 nsres = nsIDOMHTMLFormElement_SetEnctype(This->nsform, &encoding_str);
217 nsAString_Finish(&encoding_str);
218 if(NS_FAILED(nsres))
219 return E_FAIL;
221 return S_OK;
224 static HRESULT WINAPI HTMLFormElement_get_encoding(IHTMLFormElement *iface, BSTR *p)
226 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
227 nsAString encoding_str;
228 nsresult nsres;
230 TRACE("(%p)->(%p)\n", This, p);
232 nsAString_Init(&encoding_str, NULL);
233 nsres = nsIDOMHTMLFormElement_GetEnctype(This->nsform, &encoding_str);
234 return return_nsstr(nsres, &encoding_str, p);
237 static HRESULT WINAPI HTMLFormElement_put_method(IHTMLFormElement *iface, BSTR v)
239 static const WCHAR postW[] = {'P','O','S','T',0};
240 static const WCHAR getW[] = {'G','E','T',0};
242 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
243 nsAString method_str;
244 nsresult nsres;
246 TRACE("(%p)->(%s)\n", This, wine_dbgstr_w(v));
248 if(lstrcmpiW(v, postW) && lstrcmpiW(v, getW)) {
249 WARN("unrecognized method\n");
250 return E_INVALIDARG;
253 nsAString_InitDepend(&method_str, v);
254 nsres = nsIDOMHTMLFormElement_SetMethod(This->nsform, &method_str);
255 nsAString_Finish(&method_str);
256 if(NS_FAILED(nsres))
257 return E_FAIL;
259 return S_OK;
262 static HRESULT WINAPI HTMLFormElement_get_method(IHTMLFormElement *iface, BSTR *p)
264 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
265 nsAString method_str;
266 nsresult nsres;
268 TRACE("(%p)->(%p)\n", This, p);
270 nsAString_Init(&method_str, NULL);
271 nsres = nsIDOMHTMLFormElement_GetMethod(This->nsform, &method_str);
272 return return_nsstr(nsres, &method_str, p);
275 static HRESULT WINAPI HTMLFormElement_get_elements(IHTMLFormElement *iface, IDispatch **p)
277 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
279 TRACE("(%p)->(%p)\n", This, p);
281 *p = (IDispatch*)&This->IHTMLFormElement_iface;
282 IDispatch_AddRef(*p);
283 return S_OK;
286 static HRESULT WINAPI HTMLFormElement_put_target(IHTMLFormElement *iface, BSTR v)
288 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
289 nsAString str;
290 nsresult nsres;
292 TRACE("(%p)->(%s)\n", This, wine_dbgstr_w(v));
294 nsAString_InitDepend(&str, v);
296 nsres = nsIDOMHTMLFormElement_SetTarget(This->nsform, &str);
298 nsAString_Finish(&str);
299 if (NS_FAILED(nsres)) {
300 ERR("Set Target(%s) failed: %08x\n", wine_dbgstr_w(v), nsres);
301 return E_FAIL;
304 return S_OK;
307 static HRESULT WINAPI HTMLFormElement_get_target(IHTMLFormElement *iface, BSTR *p)
309 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
310 nsAString str;
311 nsresult nsres;
313 TRACE("(%p)->(%p)\n", This, p);
315 nsAString_Init(&str, NULL);
316 nsres = nsIDOMHTMLFormElement_GetTarget(This->nsform, &str);
318 return return_nsstr(nsres, &str, p);
321 static HRESULT WINAPI HTMLFormElement_put_name(IHTMLFormElement *iface, BSTR v)
323 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
324 nsAString name_str;
325 nsresult nsres;
327 TRACE("(%p)->(%s)\n", This, wine_dbgstr_w(v));
329 nsAString_InitDepend(&name_str, v);
330 nsres = nsIDOMHTMLFormElement_SetName(This->nsform, &name_str);
331 nsAString_Finish(&name_str);
332 if(NS_FAILED(nsres))
333 return E_FAIL;
335 return S_OK;
338 static HRESULT WINAPI HTMLFormElement_get_name(IHTMLFormElement *iface, BSTR *p)
340 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
341 nsAString name_str;
342 nsresult nsres;
344 TRACE("(%p)->(%p)\n", This, p);
346 nsAString_Init(&name_str, NULL);
347 nsres = nsIDOMHTMLFormElement_GetName(This->nsform, &name_str);
348 return return_nsstr(nsres, &name_str, p);
351 static HRESULT WINAPI HTMLFormElement_put_onsubmit(IHTMLFormElement *iface, VARIANT v)
353 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
355 TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
357 return set_node_event(&This->element.node, EVENTID_SUBMIT, &v);
360 static HRESULT WINAPI HTMLFormElement_get_onsubmit(IHTMLFormElement *iface, VARIANT *p)
362 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
364 TRACE("(%p)->(%p)\n", This, p);
366 return get_node_event(&This->element.node, EVENTID_SUBMIT, p);
369 static HRESULT WINAPI HTMLFormElement_put_onreset(IHTMLFormElement *iface, VARIANT v)
371 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
372 FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
373 return E_NOTIMPL;
376 static HRESULT WINAPI HTMLFormElement_get_onreset(IHTMLFormElement *iface, VARIANT *p)
378 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
379 FIXME("(%p)->(%p)\n", This, p);
380 return E_NOTIMPL;
383 static HRESULT WINAPI HTMLFormElement_submit(IHTMLFormElement *iface)
385 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
386 HTMLOuterWindow *window = NULL, *this_window = NULL;
387 nsAString action_uri_str, target_str, method_str;
388 nsIInputStream *post_stream;
389 BOOL is_post_submit = FALSE;
390 IUri *uri;
391 nsresult nsres;
392 HRESULT hres;
393 BOOL use_new_window = FALSE;
395 TRACE("(%p)\n", This);
397 if(This->element.node.doc) {
398 HTMLDocumentNode *doc = This->element.node.doc;
399 if(doc->window && doc->window->base.outer_window)
400 this_window = doc->window->base.outer_window;
402 if(!this_window) {
403 TRACE("No outer window\n");
404 return S_OK;
407 nsAString_Init(&target_str, NULL);
408 nsres = nsIDOMHTMLFormElement_GetTarget(This->nsform, &target_str);
409 if(NS_SUCCEEDED(nsres))
410 window = get_target_window(this_window, &target_str, &use_new_window);
412 if(!window && !use_new_window) {
413 nsAString_Finish(&target_str);
414 return S_OK;
417 nsAString_Init(&method_str, NULL);
418 nsres = nsIDOMHTMLFormElement_GetMethod(This->nsform, &method_str);
419 if(NS_SUCCEEDED(nsres)) {
420 const PRUnichar *method;
422 static const PRUnichar postW[] = {'p','o','s','t',0};
424 nsAString_GetData(&method_str, &method);
425 TRACE("method is %s\n", debugstr_w(method));
426 is_post_submit = !strcmpiW(method, postW);
428 nsAString_Finish(&method_str);
431 * FIXME: We currently use our submit implementation for POST submit. We should always use it.
433 if(window && !is_post_submit) {
434 nsres = nsIDOMHTMLFormElement_Submit(This->nsform);
435 nsAString_Finish(&target_str);
436 IHTMLWindow2_Release(&window->base.IHTMLWindow2_iface);
437 if(NS_FAILED(nsres)) {
438 ERR("Submit failed: %08x\n", nsres);
439 return E_FAIL;
442 return S_OK;
445 nsAString_Init(&action_uri_str, NULL);
446 nsres = nsIDOMHTMLFormElement_GetFormData(This->nsform, NULL, &action_uri_str, &post_stream);
447 if(NS_SUCCEEDED(nsres)) {
448 const PRUnichar *action_uri;
450 nsAString_GetData(&action_uri_str, &action_uri);
451 hres = create_uri(action_uri, 0, &uri);
452 }else {
453 ERR("GetFormData failed: %08x\n", nsres);
454 hres = E_FAIL;
456 nsAString_Finish(&action_uri_str);
457 if(SUCCEEDED(hres)) {
458 const PRUnichar *target;
460 nsAString_GetData(&target_str, &target);
461 hres = submit_form(window, target, uri, post_stream);
462 IUri_Release(uri);
465 nsAString_Finish(&target_str);
466 if(window)
467 IHTMLWindow2_Release(&window->base.IHTMLWindow2_iface);
468 if(post_stream)
469 nsIInputStream_Release(post_stream);
470 return hres;
473 static HRESULT WINAPI HTMLFormElement_reset(IHTMLFormElement *iface)
475 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
476 nsresult nsres;
478 TRACE("(%p)->()\n", This);
479 nsres = nsIDOMHTMLFormElement_Reset(This->nsform);
480 if (NS_FAILED(nsres)) {
481 ERR("Reset failed: %08x\n", nsres);
482 return E_FAIL;
485 return S_OK;
488 static HRESULT WINAPI HTMLFormElement_put_length(IHTMLFormElement *iface, LONG v)
490 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
491 FIXME("(%p)->(%d)\n", This, v);
492 return E_NOTIMPL;
495 static HRESULT WINAPI HTMLFormElement_get_length(IHTMLFormElement *iface, LONG *p)
497 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
498 nsresult nsres;
500 TRACE("(%p)->(%p)\n", This, p);
502 nsres = nsIDOMHTMLFormElement_GetLength(This->nsform, p);
503 if(NS_FAILED(nsres)) {
504 ERR("GetLength failed: %08x\n", nsres);
505 return E_FAIL;
508 return S_OK;
511 static HRESULT WINAPI HTMLFormElement__newEnum(IHTMLFormElement *iface, IUnknown **p)
513 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
514 FIXME("(%p)->(%p)\n", This, p);
515 return E_NOTIMPL;
518 static HRESULT WINAPI HTMLFormElement_item(IHTMLFormElement *iface, VARIANT name,
519 VARIANT index, IDispatch **pdisp)
521 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
523 TRACE("(%p)->(%s %s %p)\n", This, debugstr_variant(&name), debugstr_variant(&index), pdisp);
525 if(!pdisp)
526 return E_INVALIDARG;
527 *pdisp = NULL;
529 if(V_VT(&name) == VT_I4) {
530 if(V_I4(&name) < 0)
531 return E_INVALIDARG;
532 return htmlform_item(This, V_I4(&name), pdisp);
535 FIXME("Unsupported args\n");
536 return E_NOTIMPL;
539 static HRESULT WINAPI HTMLFormElement_tags(IHTMLFormElement *iface, VARIANT tagName,
540 IDispatch **pdisp)
542 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
543 FIXME("(%p)->(v %p)\n", This, pdisp);
544 return E_NOTIMPL;
547 static const IHTMLFormElementVtbl HTMLFormElementVtbl = {
548 HTMLFormElement_QueryInterface,
549 HTMLFormElement_AddRef,
550 HTMLFormElement_Release,
551 HTMLFormElement_GetTypeInfoCount,
552 HTMLFormElement_GetTypeInfo,
553 HTMLFormElement_GetIDsOfNames,
554 HTMLFormElement_Invoke,
555 HTMLFormElement_put_action,
556 HTMLFormElement_get_action,
557 HTMLFormElement_put_dir,
558 HTMLFormElement_get_dir,
559 HTMLFormElement_put_encoding,
560 HTMLFormElement_get_encoding,
561 HTMLFormElement_put_method,
562 HTMLFormElement_get_method,
563 HTMLFormElement_get_elements,
564 HTMLFormElement_put_target,
565 HTMLFormElement_get_target,
566 HTMLFormElement_put_name,
567 HTMLFormElement_get_name,
568 HTMLFormElement_put_onsubmit,
569 HTMLFormElement_get_onsubmit,
570 HTMLFormElement_put_onreset,
571 HTMLFormElement_get_onreset,
572 HTMLFormElement_submit,
573 HTMLFormElement_reset,
574 HTMLFormElement_put_length,
575 HTMLFormElement_get_length,
576 HTMLFormElement__newEnum,
577 HTMLFormElement_item,
578 HTMLFormElement_tags
581 static inline HTMLFormElement *impl_from_HTMLDOMNode(HTMLDOMNode *iface)
583 return CONTAINING_RECORD(iface, HTMLFormElement, element.node);
586 static HRESULT HTMLFormElement_QI(HTMLDOMNode *iface, REFIID riid, void **ppv)
588 HTMLFormElement *This = impl_from_HTMLDOMNode(iface);
590 *ppv = NULL;
592 if(IsEqualGUID(&IID_IUnknown, riid)) {
593 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
594 *ppv = &This->IHTMLFormElement_iface;
595 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
596 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
597 *ppv = &This->IHTMLFormElement_iface;
598 }else if(IsEqualGUID(&IID_IHTMLFormElement, riid)) {
599 TRACE("(%p)->(IID_IHTMLFormElement %p)\n", This, ppv);
600 *ppv = &This->IHTMLFormElement_iface;
601 }else if(IsEqualGUID(&DIID_DispHTMLFormElement, riid)) {
602 TRACE("(%p)->(DIID_DispHTMLFormElement %p)\n", This, ppv);
603 *ppv = &This->IHTMLFormElement_iface;
606 if(*ppv) {
607 IUnknown_AddRef((IUnknown*)*ppv);
608 return S_OK;
611 return HTMLElement_QI(&This->element.node, riid, ppv);
614 static HRESULT HTMLFormElement_get_dispid(HTMLDOMNode *iface,
615 BSTR name, DWORD grfdex, DISPID *pid)
617 HTMLFormElement *This = impl_from_HTMLDOMNode(iface);
618 nsIDOMHTMLCollection *elements;
619 nsAString nsstr, name_str;
620 UINT32 len, i;
621 nsresult nsres;
622 HRESULT hres = DISP_E_UNKNOWNNAME;
624 static const PRUnichar nameW[] = {'n','a','m','e',0};
626 TRACE("(%p)->(%s %x %p)\n", This, wine_dbgstr_w(name), grfdex, pid);
628 nsres = nsIDOMHTMLFormElement_GetElements(This->nsform, &elements);
629 if(NS_FAILED(nsres)) {
630 FIXME("GetElements failed: 0x%08x\n", nsres);
631 return E_FAIL;
634 nsres = nsIDOMHTMLCollection_GetLength(elements, &len);
635 if(NS_FAILED(nsres)) {
636 FIXME("GetLength failed: 0x%08x\n", nsres);
637 nsIDOMHTMLCollection_Release(elements);
638 return E_FAIL;
641 if(len > MSHTML_CUSTOM_DISPID_CNT)
642 len = MSHTML_CUSTOM_DISPID_CNT;
644 /* FIXME: Implement in more generic way */
645 if('0' <= *name && *name <= '9') {
646 WCHAR *end_ptr;
648 i = strtoulW(name, &end_ptr, 10);
649 if(!*end_ptr && i < len) {
650 *pid = MSHTML_DISPID_CUSTOM_MIN + i;
651 return S_OK;
655 nsAString_Init(&nsstr, NULL);
656 for(i = 0; i < len; ++i) {
657 nsIDOMNode *nsitem;
658 nsIDOMHTMLElement *nshtml_elem;
659 const PRUnichar *str;
661 nsres = nsIDOMHTMLCollection_Item(elements, i, &nsitem);
662 if(NS_FAILED(nsres)) {
663 FIXME("Item failed: 0x%08x\n", nsres);
664 hres = E_FAIL;
665 break;
668 nsres = nsIDOMNode_QueryInterface(nsitem, &IID_nsIDOMHTMLElement, (void**)&nshtml_elem);
669 nsIDOMNode_Release(nsitem);
670 if(NS_FAILED(nsres)) {
671 FIXME("Failed to get nsIDOMHTMLNode interface: 0x%08x\n", nsres);
672 hres = E_FAIL;
673 break;
676 /* compare by id attr */
677 nsres = nsIDOMHTMLElement_GetId(nshtml_elem, &nsstr);
678 if(NS_FAILED(nsres)) {
679 FIXME("GetId failed: 0x%08x\n", nsres);
680 nsIDOMHTMLElement_Release(nshtml_elem);
681 hres = E_FAIL;
682 break;
684 nsAString_GetData(&nsstr, &str);
685 if(!strcmpiW(str, name)) {
686 nsIDOMHTMLElement_Release(nshtml_elem);
687 /* FIXME: using index for dispid */
688 *pid = MSHTML_DISPID_CUSTOM_MIN + i;
689 hres = S_OK;
690 break;
693 /* compare by name attr */
694 nsres = get_elem_attr_value(nshtml_elem, nameW, &name_str, &str);
695 nsIDOMHTMLElement_Release(nshtml_elem);
696 if(NS_SUCCEEDED(nsres)) {
697 if(!strcmpiW(str, name)) {
698 nsAString_Finish(&name_str);
699 /* FIXME: using index for dispid */
700 *pid = MSHTML_DISPID_CUSTOM_MIN + i;
701 hres = S_OK;
702 break;
704 nsAString_Finish(&name_str);
708 nsAString_Finish(&nsstr);
709 nsIDOMHTMLCollection_Release(elements);
710 return hres;
713 static HRESULT HTMLFormElement_invoke(HTMLDOMNode *iface,
714 DISPID id, LCID lcid, WORD flags, DISPPARAMS *params, VARIANT *res,
715 EXCEPINFO *ei, IServiceProvider *caller)
717 HTMLFormElement *This = impl_from_HTMLDOMNode(iface);
718 IDispatch *ret;
719 HRESULT hres;
721 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, flags, params, res, ei, caller);
723 hres = htmlform_item(This, id - MSHTML_DISPID_CUSTOM_MIN, &ret);
724 if(FAILED(hres))
725 return hres;
727 if(ret) {
728 V_VT(res) = VT_DISPATCH;
729 V_DISPATCH(res) = ret;
730 }else {
731 V_VT(res) = VT_NULL;
733 return S_OK;
736 static HRESULT HTMLFormElement_handle_event(HTMLDOMNode *iface, eventid_t eid, nsIDOMEvent *event, BOOL *prevent_default)
738 HTMLFormElement *This = impl_from_HTMLDOMNode(iface);
740 if(eid == EVENTID_SUBMIT) {
741 *prevent_default = TRUE;
742 return IHTMLFormElement_submit(&This->IHTMLFormElement_iface);
745 return HTMLElement_handle_event(&This->element.node, eid, event, prevent_default);
748 static void HTMLFormElement_traverse(HTMLDOMNode *iface, nsCycleCollectionTraversalCallback *cb)
750 HTMLFormElement *This = impl_from_HTMLDOMNode(iface);
752 if(This->nsform)
753 note_cc_edge((nsISupports*)This->nsform, "This->nsform", cb);
756 static void HTMLFormElement_unlink(HTMLDOMNode *iface)
758 HTMLFormElement *This = impl_from_HTMLDOMNode(iface);
760 if(This->nsform) {
761 nsIDOMHTMLFormElement *nsform = This->nsform;
763 This->nsform = NULL;
764 nsIDOMHTMLFormElement_Release(nsform);
768 static const NodeImplVtbl HTMLFormElementImplVtbl = {
769 HTMLFormElement_QI,
770 HTMLElement_destructor,
771 HTMLElement_cpc,
772 HTMLElement_clone,
773 HTMLFormElement_handle_event,
774 HTMLElement_get_attr_col,
775 NULL,
776 NULL,
777 NULL,
778 NULL,
779 NULL,
780 NULL,
781 HTMLFormElement_get_dispid,
782 HTMLFormElement_invoke,
783 NULL,
784 HTMLFormElement_traverse,
785 HTMLFormElement_unlink
788 static const tid_t HTMLFormElement_iface_tids[] = {
789 HTMLELEMENT_TIDS,
790 IHTMLFormElement_tid,
794 static dispex_static_data_t HTMLFormElement_dispex = {
795 NULL,
796 DispHTMLFormElement_tid,
797 NULL,
798 HTMLFormElement_iface_tids
801 HRESULT HTMLFormElement_Create(HTMLDocumentNode *doc, nsIDOMHTMLElement *nselem, HTMLElement **elem)
803 HTMLFormElement *ret;
804 nsresult nsres;
806 ret = heap_alloc_zero(sizeof(HTMLFormElement));
807 if(!ret)
808 return E_OUTOFMEMORY;
810 ret->IHTMLFormElement_iface.lpVtbl = &HTMLFormElementVtbl;
811 ret->element.node.vtbl = &HTMLFormElementImplVtbl;
813 HTMLElement_Init(&ret->element, doc, nselem, &HTMLFormElement_dispex);
815 nsres = nsIDOMHTMLElement_QueryInterface(nselem, &IID_nsIDOMHTMLFormElement, (void**)&ret->nsform);
816 assert(nsres == NS_OK);
818 *elem = &ret->element;
819 return S_OK;