hidclass.sys: Support parsing of explicit usage page.
[wine.git] / dlls / mshtml / htmlform.c
blob5906202f1c748ab43f66dd53c6969c4e49057808
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 HRESULT return_nsform(nsresult nsres, nsIDOMHTMLFormElement *form, IHTMLFormElement **p)
47 nsIDOMNode *form_node;
48 HTMLDOMNode *node;
49 HRESULT hres;
51 if (NS_FAILED(nsres)) {
52 ERR("GetForm failed: %08x\n", nsres);
53 return E_FAIL;
56 if(!form) {
57 *p = NULL;
58 TRACE("NULL\n");
59 return S_OK;
62 nsres = nsIDOMHTMLFormElement_QueryInterface(form, &IID_nsIDOMNode, (void**)&form_node);
63 nsIDOMHTMLFormElement_Release(form);
64 assert(nsres == NS_OK);
66 hres = get_node(form_node, TRUE, &node);
67 nsIDOMNode_Release(form_node);
68 if (FAILED(hres))
69 return hres;
71 TRACE("node %p\n", node);
72 hres = IHTMLDOMNode_QueryInterface(&node->IHTMLDOMNode_iface, &IID_IHTMLFormElement, (void**)p);
73 node_release(node);
74 return hres;
77 static HRESULT htmlform_item(HTMLFormElement *This, int i, IDispatch **ret)
79 nsIDOMHTMLCollection *elements;
80 nsIDOMNode *item;
81 HTMLDOMNode *node;
82 nsresult nsres;
83 HRESULT hres;
85 nsres = nsIDOMHTMLFormElement_GetElements(This->nsform, &elements);
86 if(NS_FAILED(nsres)) {
87 FIXME("GetElements failed: 0x%08x\n", nsres);
88 return E_FAIL;
91 nsres = nsIDOMHTMLCollection_Item(elements, i, &item);
92 nsIDOMHTMLCollection_Release(elements);
93 if(NS_FAILED(nsres)) {
94 FIXME("Item failed: 0x%08x\n", nsres);
95 return E_FAIL;
98 if(item) {
99 hres = get_node(item, TRUE, &node);
100 if(FAILED(hres))
101 return hres;
103 nsIDOMNode_Release(item);
104 *ret = (IDispatch*)&node->IHTMLDOMNode_iface;
105 }else {
106 *ret = NULL;
109 return S_OK;
112 static inline HTMLFormElement *impl_from_IHTMLFormElement(IHTMLFormElement *iface)
114 return CONTAINING_RECORD(iface, HTMLFormElement, IHTMLFormElement_iface);
117 static HRESULT WINAPI HTMLFormElement_QueryInterface(IHTMLFormElement *iface,
118 REFIID riid, void **ppv)
120 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
122 return IHTMLDOMNode_QueryInterface(&This->element.node.IHTMLDOMNode_iface, riid, ppv);
125 static ULONG WINAPI HTMLFormElement_AddRef(IHTMLFormElement *iface)
127 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
129 return IHTMLDOMNode_AddRef(&This->element.node.IHTMLDOMNode_iface);
132 static ULONG WINAPI HTMLFormElement_Release(IHTMLFormElement *iface)
134 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
136 return IHTMLDOMNode_Release(&This->element.node.IHTMLDOMNode_iface);
139 static HRESULT WINAPI HTMLFormElement_GetTypeInfoCount(IHTMLFormElement *iface, UINT *pctinfo)
141 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
142 return IDispatchEx_GetTypeInfoCount(&This->element.node.event_target.dispex.IDispatchEx_iface, pctinfo);
145 static HRESULT WINAPI HTMLFormElement_GetTypeInfo(IHTMLFormElement *iface, UINT iTInfo,
146 LCID lcid, ITypeInfo **ppTInfo)
148 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
149 return IDispatchEx_GetTypeInfo(&This->element.node.event_target.dispex.IDispatchEx_iface, iTInfo, lcid,
150 ppTInfo);
153 static HRESULT WINAPI HTMLFormElement_GetIDsOfNames(IHTMLFormElement *iface, REFIID riid,
154 LPOLESTR *rgszNames, UINT cNames,
155 LCID lcid, DISPID *rgDispId)
157 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
158 return IDispatchEx_GetIDsOfNames(&This->element.node.event_target.dispex.IDispatchEx_iface, riid, rgszNames,
159 cNames, lcid, rgDispId);
162 static HRESULT WINAPI HTMLFormElement_Invoke(IHTMLFormElement *iface, DISPID dispIdMember,
163 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
164 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
166 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
167 return IDispatchEx_Invoke(&This->element.node.event_target.dispex.IDispatchEx_iface, dispIdMember, riid,
168 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
171 static HRESULT WINAPI HTMLFormElement_put_action(IHTMLFormElement *iface, BSTR v)
173 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
174 nsAString action_str;
175 nsresult nsres;
177 TRACE("(%p)->(%s)\n", This, wine_dbgstr_w(v));
179 nsAString_InitDepend(&action_str, v);
180 nsres = nsIDOMHTMLFormElement_SetAction(This->nsform, &action_str);
181 nsAString_Finish(&action_str);
182 if(NS_FAILED(nsres)) {
183 ERR("SetAction failed: %08x\n", nsres);
184 return E_FAIL;
187 return S_OK;
190 static HRESULT WINAPI HTMLFormElement_get_action(IHTMLFormElement *iface, BSTR *p)
192 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
193 nsAString action_str;
194 nsresult nsres;
195 HRESULT hres;
197 TRACE("(%p)->(%p)\n", This, p);
199 nsAString_Init(&action_str, NULL);
200 nsres = nsIDOMHTMLFormElement_GetAction(This->nsform, &action_str);
201 if(NS_SUCCEEDED(nsres)) {
202 const PRUnichar *action;
203 nsAString_GetData(&action_str, &action);
204 hres = nsuri_to_url(action, FALSE, p);
205 }else {
206 ERR("GetAction failed: %08x\n", nsres);
207 hres = E_FAIL;
210 nsAString_Finish(&action_str);
211 return hres;
214 static HRESULT WINAPI HTMLFormElement_put_dir(IHTMLFormElement *iface, BSTR v)
216 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
217 FIXME("(%p)->(%s)\n", This, wine_dbgstr_w(v));
218 return E_NOTIMPL;
221 static HRESULT WINAPI HTMLFormElement_get_dir(IHTMLFormElement *iface, BSTR *p)
223 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
224 FIXME("(%p)->(%p)\n", This, p);
225 return E_NOTIMPL;
228 static HRESULT WINAPI HTMLFormElement_put_encoding(IHTMLFormElement *iface, BSTR v)
230 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
231 nsAString encoding_str;
232 nsresult nsres;
234 TRACE("(%p)->(%s)\n", This, wine_dbgstr_w(v));
236 if(lstrcmpiW(v, L"application/x-www-form-urlencoded") && lstrcmpiW(v, L"multipart/form-data")
237 && lstrcmpiW(v, L"text/plain")) {
238 WARN("incorrect enctype\n");
239 return E_INVALIDARG;
242 nsAString_InitDepend(&encoding_str, v);
243 nsres = nsIDOMHTMLFormElement_SetEnctype(This->nsform, &encoding_str);
244 nsAString_Finish(&encoding_str);
245 if(NS_FAILED(nsres))
246 return E_FAIL;
248 return S_OK;
251 static HRESULT WINAPI HTMLFormElement_get_encoding(IHTMLFormElement *iface, BSTR *p)
253 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
254 nsAString encoding_str;
255 nsresult nsres;
257 TRACE("(%p)->(%p)\n", This, p);
259 nsAString_Init(&encoding_str, NULL);
260 nsres = nsIDOMHTMLFormElement_GetEnctype(This->nsform, &encoding_str);
261 return return_nsstr(nsres, &encoding_str, p);
264 static HRESULT WINAPI HTMLFormElement_put_method(IHTMLFormElement *iface, BSTR v)
266 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
267 nsAString method_str;
268 nsresult nsres;
270 TRACE("(%p)->(%s)\n", This, wine_dbgstr_w(v));
272 if(lstrcmpiW(v, L"POST") && lstrcmpiW(v, L"GET")) {
273 WARN("unrecognized method\n");
274 return E_INVALIDARG;
277 nsAString_InitDepend(&method_str, v);
278 nsres = nsIDOMHTMLFormElement_SetMethod(This->nsform, &method_str);
279 nsAString_Finish(&method_str);
280 if(NS_FAILED(nsres))
281 return E_FAIL;
283 return S_OK;
286 static HRESULT WINAPI HTMLFormElement_get_method(IHTMLFormElement *iface, BSTR *p)
288 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
289 nsAString method_str;
290 nsresult nsres;
292 TRACE("(%p)->(%p)\n", This, p);
294 nsAString_Init(&method_str, NULL);
295 nsres = nsIDOMHTMLFormElement_GetMethod(This->nsform, &method_str);
296 return return_nsstr(nsres, &method_str, p);
299 static HRESULT WINAPI HTMLFormElement_get_elements(IHTMLFormElement *iface, IDispatch **p)
301 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
302 nsIDOMHTMLCollection *elements;
303 nsresult nsres;
305 TRACE("(%p)->(%p)\n", This, p);
307 if(dispex_compat_mode(&This->element.node.event_target.dispex) < COMPAT_MODE_IE9) {
308 IDispatch_AddRef(*p = (IDispatch*)&This->IHTMLFormElement_iface);
309 return S_OK;
312 nsres = nsIDOMHTMLFormElement_GetElements(This->nsform, &elements);
313 if(NS_FAILED(nsres)) {
314 ERR("GetElements failed: %08x\n", nsres);
315 return E_FAIL;
318 *p = (IDispatch*)create_collection_from_htmlcol(elements, dispex_compat_mode(&This->element.node.event_target.dispex));
319 nsIDOMHTMLCollection_Release(elements);
320 return S_OK;
323 static HRESULT WINAPI HTMLFormElement_put_target(IHTMLFormElement *iface, BSTR v)
325 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
326 nsAString str;
327 nsresult nsres;
329 TRACE("(%p)->(%s)\n", This, wine_dbgstr_w(v));
331 nsAString_InitDepend(&str, v);
333 nsres = nsIDOMHTMLFormElement_SetTarget(This->nsform, &str);
335 nsAString_Finish(&str);
336 if (NS_FAILED(nsres)) {
337 ERR("Set Target(%s) failed: %08x\n", wine_dbgstr_w(v), nsres);
338 return E_FAIL;
341 return S_OK;
344 static HRESULT WINAPI HTMLFormElement_get_target(IHTMLFormElement *iface, BSTR *p)
346 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
347 nsAString str;
348 nsresult nsres;
350 TRACE("(%p)->(%p)\n", This, p);
352 nsAString_Init(&str, NULL);
353 nsres = nsIDOMHTMLFormElement_GetTarget(This->nsform, &str);
355 return return_nsstr(nsres, &str, p);
358 static HRESULT WINAPI HTMLFormElement_put_name(IHTMLFormElement *iface, BSTR v)
360 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
361 nsAString name_str;
362 nsresult nsres;
364 TRACE("(%p)->(%s)\n", This, wine_dbgstr_w(v));
366 nsAString_InitDepend(&name_str, v);
367 nsres = nsIDOMHTMLFormElement_SetName(This->nsform, &name_str);
368 nsAString_Finish(&name_str);
369 if(NS_FAILED(nsres))
370 return E_FAIL;
372 return S_OK;
375 static HRESULT WINAPI HTMLFormElement_get_name(IHTMLFormElement *iface, BSTR *p)
377 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
378 nsAString name_str;
379 nsresult nsres;
381 TRACE("(%p)->(%p)\n", This, p);
383 nsAString_Init(&name_str, NULL);
384 nsres = nsIDOMHTMLFormElement_GetName(This->nsform, &name_str);
385 return return_nsstr(nsres, &name_str, p);
388 static HRESULT WINAPI HTMLFormElement_put_onsubmit(IHTMLFormElement *iface, VARIANT v)
390 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
392 TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
394 return set_node_event(&This->element.node, EVENTID_SUBMIT, &v);
397 static HRESULT WINAPI HTMLFormElement_get_onsubmit(IHTMLFormElement *iface, VARIANT *p)
399 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
401 TRACE("(%p)->(%p)\n", This, p);
403 return get_node_event(&This->element.node, EVENTID_SUBMIT, p);
406 static HRESULT WINAPI HTMLFormElement_put_onreset(IHTMLFormElement *iface, VARIANT v)
408 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
409 FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
410 return E_NOTIMPL;
413 static HRESULT WINAPI HTMLFormElement_get_onreset(IHTMLFormElement *iface, VARIANT *p)
415 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
416 FIXME("(%p)->(%p)\n", This, p);
417 return E_NOTIMPL;
420 static HRESULT WINAPI HTMLFormElement_submit(IHTMLFormElement *iface)
422 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
423 HTMLOuterWindow *window = NULL, *this_window = NULL;
424 nsAString action_uri_str, target_str, method_str;
425 nsIInputStream *post_stream;
426 BOOL is_post_submit = FALSE;
427 IUri *uri;
428 nsresult nsres;
429 HRESULT hres;
430 BOOL use_new_window = FALSE;
432 TRACE("(%p)\n", This);
434 if(This->element.node.doc) {
435 HTMLDocumentNode *doc = This->element.node.doc;
436 if(doc->window && doc->window->base.outer_window)
437 this_window = doc->window->base.outer_window;
439 if(!this_window) {
440 TRACE("No outer window\n");
441 return S_OK;
444 nsAString_Init(&target_str, NULL);
445 nsres = nsIDOMHTMLFormElement_GetTarget(This->nsform, &target_str);
446 if(NS_SUCCEEDED(nsres))
447 window = get_target_window(this_window, &target_str, &use_new_window);
449 if(!window && !use_new_window) {
450 nsAString_Finish(&target_str);
451 return S_OK;
454 nsAString_Init(&method_str, NULL);
455 nsres = nsIDOMHTMLFormElement_GetMethod(This->nsform, &method_str);
456 if(NS_SUCCEEDED(nsres)) {
457 const PRUnichar *method;
459 nsAString_GetData(&method_str, &method);
460 TRACE("method is %s\n", debugstr_w(method));
461 is_post_submit = !wcsicmp(method, L"post");
463 nsAString_Finish(&method_str);
466 * FIXME: We currently use our submit implementation for POST submit. We should always use it.
468 if(window && !is_post_submit) {
469 nsres = nsIDOMHTMLFormElement_Submit(This->nsform);
470 nsAString_Finish(&target_str);
471 IHTMLWindow2_Release(&window->base.IHTMLWindow2_iface);
472 if(NS_FAILED(nsres)) {
473 ERR("Submit failed: %08x\n", nsres);
474 return E_FAIL;
477 return S_OK;
480 nsAString_Init(&action_uri_str, NULL);
481 nsres = nsIDOMHTMLFormElement_GetFormData(This->nsform, NULL, &action_uri_str, &post_stream);
482 if(NS_SUCCEEDED(nsres)) {
483 const PRUnichar *action_uri;
485 nsAString_GetData(&action_uri_str, &action_uri);
486 hres = create_uri(action_uri, 0, &uri);
487 }else {
488 ERR("GetFormData failed: %08x\n", nsres);
489 hres = E_FAIL;
491 nsAString_Finish(&action_uri_str);
492 if(SUCCEEDED(hres)) {
493 const PRUnichar *target;
495 nsAString_GetData(&target_str, &target);
496 hres = submit_form(window, target, uri, post_stream);
497 IUri_Release(uri);
500 nsAString_Finish(&target_str);
501 if(window)
502 IHTMLWindow2_Release(&window->base.IHTMLWindow2_iface);
503 if(post_stream)
504 nsIInputStream_Release(post_stream);
505 return hres;
508 static HRESULT WINAPI HTMLFormElement_reset(IHTMLFormElement *iface)
510 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
511 nsresult nsres;
513 TRACE("(%p)->()\n", This);
514 nsres = nsIDOMHTMLFormElement_Reset(This->nsform);
515 if (NS_FAILED(nsres)) {
516 ERR("Reset failed: %08x\n", nsres);
517 return E_FAIL;
520 return S_OK;
523 static HRESULT WINAPI HTMLFormElement_put_length(IHTMLFormElement *iface, LONG v)
525 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
526 FIXME("(%p)->(%d)\n", This, v);
527 return E_NOTIMPL;
530 static HRESULT WINAPI HTMLFormElement_get_length(IHTMLFormElement *iface, LONG *p)
532 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
533 nsresult nsres;
535 TRACE("(%p)->(%p)\n", This, p);
537 nsres = nsIDOMHTMLFormElement_GetLength(This->nsform, p);
538 if(NS_FAILED(nsres)) {
539 ERR("GetLength failed: %08x\n", nsres);
540 return E_FAIL;
543 return S_OK;
546 static HRESULT WINAPI HTMLFormElement__newEnum(IHTMLFormElement *iface, IUnknown **p)
548 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
549 FIXME("(%p)->(%p)\n", This, p);
550 return E_NOTIMPL;
553 static HRESULT WINAPI HTMLFormElement_item(IHTMLFormElement *iface, VARIANT name,
554 VARIANT index, IDispatch **pdisp)
556 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
558 TRACE("(%p)->(%s %s %p)\n", This, debugstr_variant(&name), debugstr_variant(&index), pdisp);
560 if(!pdisp)
561 return E_INVALIDARG;
562 *pdisp = NULL;
564 if(V_VT(&name) == VT_I4) {
565 if(V_I4(&name) < 0) {
566 *pdisp = NULL;
567 return dispex_compat_mode(&This->element.node.event_target.dispex) >= COMPAT_MODE_IE9
568 ? S_OK : E_INVALIDARG;
570 return htmlform_item(This, V_I4(&name), pdisp);
573 FIXME("Unsupported args\n");
574 return E_NOTIMPL;
577 static HRESULT WINAPI HTMLFormElement_tags(IHTMLFormElement *iface, VARIANT tagName,
578 IDispatch **pdisp)
580 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
581 FIXME("(%p)->(v %p)\n", This, pdisp);
582 return E_NOTIMPL;
585 static const IHTMLFormElementVtbl HTMLFormElementVtbl = {
586 HTMLFormElement_QueryInterface,
587 HTMLFormElement_AddRef,
588 HTMLFormElement_Release,
589 HTMLFormElement_GetTypeInfoCount,
590 HTMLFormElement_GetTypeInfo,
591 HTMLFormElement_GetIDsOfNames,
592 HTMLFormElement_Invoke,
593 HTMLFormElement_put_action,
594 HTMLFormElement_get_action,
595 HTMLFormElement_put_dir,
596 HTMLFormElement_get_dir,
597 HTMLFormElement_put_encoding,
598 HTMLFormElement_get_encoding,
599 HTMLFormElement_put_method,
600 HTMLFormElement_get_method,
601 HTMLFormElement_get_elements,
602 HTMLFormElement_put_target,
603 HTMLFormElement_get_target,
604 HTMLFormElement_put_name,
605 HTMLFormElement_get_name,
606 HTMLFormElement_put_onsubmit,
607 HTMLFormElement_get_onsubmit,
608 HTMLFormElement_put_onreset,
609 HTMLFormElement_get_onreset,
610 HTMLFormElement_submit,
611 HTMLFormElement_reset,
612 HTMLFormElement_put_length,
613 HTMLFormElement_get_length,
614 HTMLFormElement__newEnum,
615 HTMLFormElement_item,
616 HTMLFormElement_tags
619 static inline HTMLFormElement *impl_from_HTMLDOMNode(HTMLDOMNode *iface)
621 return CONTAINING_RECORD(iface, HTMLFormElement, element.node);
624 static HRESULT HTMLFormElement_QI(HTMLDOMNode *iface, REFIID riid, void **ppv)
626 HTMLFormElement *This = impl_from_HTMLDOMNode(iface);
628 *ppv = NULL;
630 if(IsEqualGUID(&IID_IUnknown, riid)) {
631 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
632 *ppv = &This->IHTMLFormElement_iface;
633 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
634 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
635 *ppv = &This->IHTMLFormElement_iface;
636 }else if(IsEqualGUID(&IID_IHTMLFormElement, riid)) {
637 TRACE("(%p)->(IID_IHTMLFormElement %p)\n", This, ppv);
638 *ppv = &This->IHTMLFormElement_iface;
639 }else if(IsEqualGUID(&DIID_DispHTMLFormElement, riid)) {
640 TRACE("(%p)->(DIID_DispHTMLFormElement %p)\n", This, ppv);
641 *ppv = &This->IHTMLFormElement_iface;
644 if(*ppv) {
645 IUnknown_AddRef((IUnknown*)*ppv);
646 return S_OK;
649 return HTMLElement_QI(&This->element.node, riid, ppv);
652 static HRESULT HTMLFormElement_get_dispid(HTMLDOMNode *iface,
653 BSTR name, DWORD grfdex, DISPID *pid)
655 HTMLFormElement *This = impl_from_HTMLDOMNode(iface);
656 nsIDOMHTMLCollection *elements;
657 nsAString nsstr, name_str;
658 UINT32 len, i;
659 nsresult nsres;
660 HRESULT hres = DISP_E_UNKNOWNNAME;
662 TRACE("(%p)->(%s %x %p)\n", This, wine_dbgstr_w(name), grfdex, pid);
664 nsres = nsIDOMHTMLFormElement_GetElements(This->nsform, &elements);
665 if(NS_FAILED(nsres)) {
666 FIXME("GetElements failed: 0x%08x\n", nsres);
667 return E_FAIL;
670 nsres = nsIDOMHTMLCollection_GetLength(elements, &len);
671 if(NS_FAILED(nsres)) {
672 FIXME("GetLength failed: 0x%08x\n", nsres);
673 nsIDOMHTMLCollection_Release(elements);
674 return E_FAIL;
677 if(len > MSHTML_CUSTOM_DISPID_CNT)
678 len = MSHTML_CUSTOM_DISPID_CNT;
680 /* FIXME: Implement in more generic way */
681 if('0' <= *name && *name <= '9') {
682 WCHAR *end_ptr;
684 i = wcstoul(name, &end_ptr, 10);
685 if(!*end_ptr && i < len) {
686 *pid = MSHTML_DISPID_CUSTOM_MIN + i;
687 return S_OK;
691 nsAString_Init(&nsstr, NULL);
692 for(i = 0; i < len; ++i) {
693 nsIDOMNode *nsitem;
694 nsIDOMElement *elem;
695 const PRUnichar *str;
697 nsres = nsIDOMHTMLCollection_Item(elements, i, &nsitem);
698 if(NS_FAILED(nsres)) {
699 FIXME("Item failed: 0x%08x\n", nsres);
700 hres = E_FAIL;
701 break;
704 nsres = nsIDOMNode_QueryInterface(nsitem, &IID_nsIDOMElement, (void**)&elem);
705 nsIDOMNode_Release(nsitem);
706 if(NS_FAILED(nsres)) {
707 FIXME("Failed to get nsIDOMHTMLNode interface: 0x%08x\n", nsres);
708 hres = E_FAIL;
709 break;
712 /* compare by id attr */
713 nsres = nsIDOMElement_GetId(elem, &nsstr);
714 if(NS_FAILED(nsres)) {
715 FIXME("GetId failed: 0x%08x\n", nsres);
716 nsIDOMElement_Release(elem);
717 hres = E_FAIL;
718 break;
720 nsAString_GetData(&nsstr, &str);
721 if(!wcsicmp(str, name)) {
722 nsIDOMElement_Release(elem);
723 /* FIXME: using index for dispid */
724 *pid = MSHTML_DISPID_CUSTOM_MIN + i;
725 hres = S_OK;
726 break;
729 /* compare by name attr */
730 nsres = get_elem_attr_value(elem, L"name", &name_str, &str);
731 nsIDOMElement_Release(elem);
732 if(NS_SUCCEEDED(nsres)) {
733 if(!wcsicmp(str, name)) {
734 nsAString_Finish(&name_str);
735 /* FIXME: using index for dispid */
736 *pid = MSHTML_DISPID_CUSTOM_MIN + i;
737 hres = S_OK;
738 break;
740 nsAString_Finish(&name_str);
744 nsAString_Finish(&nsstr);
745 nsIDOMHTMLCollection_Release(elements);
746 return hres;
749 static HRESULT HTMLFormElement_invoke(HTMLDOMNode *iface,
750 DISPID id, LCID lcid, WORD flags, DISPPARAMS *params, VARIANT *res,
751 EXCEPINFO *ei, IServiceProvider *caller)
753 HTMLFormElement *This = impl_from_HTMLDOMNode(iface);
754 IDispatch *ret;
755 HRESULT hres;
757 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, flags, params, res, ei, caller);
759 hres = htmlform_item(This, id - MSHTML_DISPID_CUSTOM_MIN, &ret);
760 if(FAILED(hres))
761 return hres;
763 if(ret) {
764 V_VT(res) = VT_DISPATCH;
765 V_DISPATCH(res) = ret;
766 }else {
767 V_VT(res) = VT_NULL;
769 return S_OK;
772 static HRESULT HTMLFormElement_handle_event(HTMLDOMNode *iface, DWORD eid, nsIDOMEvent *event, BOOL *prevent_default)
774 HTMLFormElement *This = impl_from_HTMLDOMNode(iface);
776 if(eid == EVENTID_SUBMIT) {
777 *prevent_default = TRUE;
778 return IHTMLFormElement_submit(&This->IHTMLFormElement_iface);
781 return HTMLElement_handle_event(&This->element.node, eid, event, prevent_default);
784 static void HTMLFormElement_traverse(HTMLDOMNode *iface, nsCycleCollectionTraversalCallback *cb)
786 HTMLFormElement *This = impl_from_HTMLDOMNode(iface);
788 if(This->nsform)
789 note_cc_edge((nsISupports*)This->nsform, "This->nsform", cb);
792 static void HTMLFormElement_unlink(HTMLDOMNode *iface)
794 HTMLFormElement *This = impl_from_HTMLDOMNode(iface);
796 if(This->nsform) {
797 nsIDOMHTMLFormElement *nsform = This->nsform;
799 This->nsform = NULL;
800 nsIDOMHTMLFormElement_Release(nsform);
804 static const NodeImplVtbl HTMLFormElementImplVtbl = {
805 &CLSID_HTMLFormElement,
806 HTMLFormElement_QI,
807 HTMLElement_destructor,
808 HTMLElement_cpc,
809 HTMLElement_clone,
810 HTMLFormElement_handle_event,
811 HTMLElement_get_attr_col,
812 NULL,
813 NULL,
814 NULL,
815 NULL,
816 NULL,
817 HTMLFormElement_get_dispid,
818 HTMLFormElement_invoke,
819 NULL,
820 HTMLFormElement_traverse,
821 HTMLFormElement_unlink
824 static const tid_t HTMLFormElement_iface_tids[] = {
825 HTMLELEMENT_TIDS,
826 IHTMLFormElement_tid,
830 static dispex_static_data_t HTMLFormElement_dispex = {
831 NULL,
832 DispHTMLFormElement_tid,
833 HTMLFormElement_iface_tids,
834 HTMLElement_init_dispex_info
837 HRESULT HTMLFormElement_Create(HTMLDocumentNode *doc, nsIDOMElement *nselem, HTMLElement **elem)
839 HTMLFormElement *ret;
840 nsresult nsres;
842 ret = heap_alloc_zero(sizeof(HTMLFormElement));
843 if(!ret)
844 return E_OUTOFMEMORY;
846 ret->IHTMLFormElement_iface.lpVtbl = &HTMLFormElementVtbl;
847 ret->element.node.vtbl = &HTMLFormElementImplVtbl;
849 HTMLElement_Init(&ret->element, doc, nselem, &HTMLFormElement_dispex);
851 nsres = nsIDOMElement_QueryInterface(nselem, &IID_nsIDOMHTMLFormElement, (void**)&ret->nsform);
852 assert(nsres == NS_OK);
854 *elem = &ret->element;
855 return S_OK;