ntoskrnl.exe: Add -norelay flag for _vsnprintf.
[wine.git] / dlls / mshtml / htmlform.c
blob63e8f1bb2d04aaab29589da5fe5567fd93d03a85
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 static const WCHAR urlencodedW[] = {'a','p','p','l','i','c','a','t','i','o','n','/',
231 'x','-','w','w','w','-','f','o','r','m','-','u','r','l','e','n','c','o','d','e','d',0};
232 static const WCHAR dataW[] = {'m','u','l','t','i','p','a','r','t','/',
233 'f','o','r','m','-','d','a','t','a',0};
234 static const WCHAR plainW[] = {'t','e','x','t','/','p','l','a','i','n',0};
236 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
237 nsAString encoding_str;
238 nsresult nsres;
240 TRACE("(%p)->(%s)\n", This, wine_dbgstr_w(v));
242 if(lstrcmpiW(v, urlencodedW) && lstrcmpiW(v, dataW) && lstrcmpiW(v, plainW)) {
243 WARN("incorrect enctype\n");
244 return E_INVALIDARG;
247 nsAString_InitDepend(&encoding_str, v);
248 nsres = nsIDOMHTMLFormElement_SetEnctype(This->nsform, &encoding_str);
249 nsAString_Finish(&encoding_str);
250 if(NS_FAILED(nsres))
251 return E_FAIL;
253 return S_OK;
256 static HRESULT WINAPI HTMLFormElement_get_encoding(IHTMLFormElement *iface, BSTR *p)
258 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
259 nsAString encoding_str;
260 nsresult nsres;
262 TRACE("(%p)->(%p)\n", This, p);
264 nsAString_Init(&encoding_str, NULL);
265 nsres = nsIDOMHTMLFormElement_GetEnctype(This->nsform, &encoding_str);
266 return return_nsstr(nsres, &encoding_str, p);
269 static HRESULT WINAPI HTMLFormElement_put_method(IHTMLFormElement *iface, BSTR v)
271 static const WCHAR postW[] = {'P','O','S','T',0};
272 static const WCHAR getW[] = {'G','E','T',0};
274 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
275 nsAString method_str;
276 nsresult nsres;
278 TRACE("(%p)->(%s)\n", This, wine_dbgstr_w(v));
280 if(lstrcmpiW(v, postW) && lstrcmpiW(v, getW)) {
281 WARN("unrecognized method\n");
282 return E_INVALIDARG;
285 nsAString_InitDepend(&method_str, v);
286 nsres = nsIDOMHTMLFormElement_SetMethod(This->nsform, &method_str);
287 nsAString_Finish(&method_str);
288 if(NS_FAILED(nsres))
289 return E_FAIL;
291 return S_OK;
294 static HRESULT WINAPI HTMLFormElement_get_method(IHTMLFormElement *iface, BSTR *p)
296 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
297 nsAString method_str;
298 nsresult nsres;
300 TRACE("(%p)->(%p)\n", This, p);
302 nsAString_Init(&method_str, NULL);
303 nsres = nsIDOMHTMLFormElement_GetMethod(This->nsform, &method_str);
304 return return_nsstr(nsres, &method_str, p);
307 static HRESULT WINAPI HTMLFormElement_get_elements(IHTMLFormElement *iface, IDispatch **p)
309 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
310 nsIDOMHTMLCollection *elements;
311 nsresult nsres;
313 TRACE("(%p)->(%p)\n", This, p);
315 if(dispex_compat_mode(&This->element.node.event_target.dispex) < COMPAT_MODE_IE9) {
316 IDispatch_AddRef(*p = (IDispatch*)&This->IHTMLFormElement_iface);
317 return S_OK;
320 nsres = nsIDOMHTMLFormElement_GetElements(This->nsform, &elements);
321 if(NS_FAILED(nsres)) {
322 ERR("GetElements failed: %08x\n", nsres);
323 return E_FAIL;
326 *p = (IDispatch*)create_collection_from_htmlcol(elements, dispex_compat_mode(&This->element.node.event_target.dispex));
327 nsIDOMHTMLCollection_Release(elements);
328 return S_OK;
331 static HRESULT WINAPI HTMLFormElement_put_target(IHTMLFormElement *iface, BSTR v)
333 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
334 nsAString str;
335 nsresult nsres;
337 TRACE("(%p)->(%s)\n", This, wine_dbgstr_w(v));
339 nsAString_InitDepend(&str, v);
341 nsres = nsIDOMHTMLFormElement_SetTarget(This->nsform, &str);
343 nsAString_Finish(&str);
344 if (NS_FAILED(nsres)) {
345 ERR("Set Target(%s) failed: %08x\n", wine_dbgstr_w(v), nsres);
346 return E_FAIL;
349 return S_OK;
352 static HRESULT WINAPI HTMLFormElement_get_target(IHTMLFormElement *iface, BSTR *p)
354 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
355 nsAString str;
356 nsresult nsres;
358 TRACE("(%p)->(%p)\n", This, p);
360 nsAString_Init(&str, NULL);
361 nsres = nsIDOMHTMLFormElement_GetTarget(This->nsform, &str);
363 return return_nsstr(nsres, &str, p);
366 static HRESULT WINAPI HTMLFormElement_put_name(IHTMLFormElement *iface, BSTR v)
368 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
369 nsAString name_str;
370 nsresult nsres;
372 TRACE("(%p)->(%s)\n", This, wine_dbgstr_w(v));
374 nsAString_InitDepend(&name_str, v);
375 nsres = nsIDOMHTMLFormElement_SetName(This->nsform, &name_str);
376 nsAString_Finish(&name_str);
377 if(NS_FAILED(nsres))
378 return E_FAIL;
380 return S_OK;
383 static HRESULT WINAPI HTMLFormElement_get_name(IHTMLFormElement *iface, BSTR *p)
385 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
386 nsAString name_str;
387 nsresult nsres;
389 TRACE("(%p)->(%p)\n", This, p);
391 nsAString_Init(&name_str, NULL);
392 nsres = nsIDOMHTMLFormElement_GetName(This->nsform, &name_str);
393 return return_nsstr(nsres, &name_str, p);
396 static HRESULT WINAPI HTMLFormElement_put_onsubmit(IHTMLFormElement *iface, VARIANT v)
398 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
400 TRACE("(%p)->(%s)\n", This, debugstr_variant(&v));
402 return set_node_event(&This->element.node, EVENTID_SUBMIT, &v);
405 static HRESULT WINAPI HTMLFormElement_get_onsubmit(IHTMLFormElement *iface, VARIANT *p)
407 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
409 TRACE("(%p)->(%p)\n", This, p);
411 return get_node_event(&This->element.node, EVENTID_SUBMIT, p);
414 static HRESULT WINAPI HTMLFormElement_put_onreset(IHTMLFormElement *iface, VARIANT v)
416 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
417 FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
418 return E_NOTIMPL;
421 static HRESULT WINAPI HTMLFormElement_get_onreset(IHTMLFormElement *iface, VARIANT *p)
423 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
424 FIXME("(%p)->(%p)\n", This, p);
425 return E_NOTIMPL;
428 static HRESULT WINAPI HTMLFormElement_submit(IHTMLFormElement *iface)
430 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
431 HTMLOuterWindow *window = NULL, *this_window = NULL;
432 nsAString action_uri_str, target_str, method_str;
433 nsIInputStream *post_stream;
434 BOOL is_post_submit = FALSE;
435 IUri *uri;
436 nsresult nsres;
437 HRESULT hres;
438 BOOL use_new_window = FALSE;
440 TRACE("(%p)\n", This);
442 if(This->element.node.doc) {
443 HTMLDocumentNode *doc = This->element.node.doc;
444 if(doc->window && doc->window->base.outer_window)
445 this_window = doc->window->base.outer_window;
447 if(!this_window) {
448 TRACE("No outer window\n");
449 return S_OK;
452 nsAString_Init(&target_str, NULL);
453 nsres = nsIDOMHTMLFormElement_GetTarget(This->nsform, &target_str);
454 if(NS_SUCCEEDED(nsres))
455 window = get_target_window(this_window, &target_str, &use_new_window);
457 if(!window && !use_new_window) {
458 nsAString_Finish(&target_str);
459 return S_OK;
462 nsAString_Init(&method_str, NULL);
463 nsres = nsIDOMHTMLFormElement_GetMethod(This->nsform, &method_str);
464 if(NS_SUCCEEDED(nsres)) {
465 const PRUnichar *method;
467 static const PRUnichar postW[] = {'p','o','s','t',0};
469 nsAString_GetData(&method_str, &method);
470 TRACE("method is %s\n", debugstr_w(method));
471 is_post_submit = !wcsicmp(method, postW);
473 nsAString_Finish(&method_str);
476 * FIXME: We currently use our submit implementation for POST submit. We should always use it.
478 if(window && !is_post_submit) {
479 nsres = nsIDOMHTMLFormElement_Submit(This->nsform);
480 nsAString_Finish(&target_str);
481 IHTMLWindow2_Release(&window->base.IHTMLWindow2_iface);
482 if(NS_FAILED(nsres)) {
483 ERR("Submit failed: %08x\n", nsres);
484 return E_FAIL;
487 return S_OK;
490 nsAString_Init(&action_uri_str, NULL);
491 nsres = nsIDOMHTMLFormElement_GetFormData(This->nsform, NULL, &action_uri_str, &post_stream);
492 if(NS_SUCCEEDED(nsres)) {
493 const PRUnichar *action_uri;
495 nsAString_GetData(&action_uri_str, &action_uri);
496 hres = create_uri(action_uri, 0, &uri);
497 }else {
498 ERR("GetFormData failed: %08x\n", nsres);
499 hres = E_FAIL;
501 nsAString_Finish(&action_uri_str);
502 if(SUCCEEDED(hres)) {
503 const PRUnichar *target;
505 nsAString_GetData(&target_str, &target);
506 hres = submit_form(window, target, uri, post_stream);
507 IUri_Release(uri);
510 nsAString_Finish(&target_str);
511 if(window)
512 IHTMLWindow2_Release(&window->base.IHTMLWindow2_iface);
513 if(post_stream)
514 nsIInputStream_Release(post_stream);
515 return hres;
518 static HRESULT WINAPI HTMLFormElement_reset(IHTMLFormElement *iface)
520 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
521 nsresult nsres;
523 TRACE("(%p)->()\n", This);
524 nsres = nsIDOMHTMLFormElement_Reset(This->nsform);
525 if (NS_FAILED(nsres)) {
526 ERR("Reset failed: %08x\n", nsres);
527 return E_FAIL;
530 return S_OK;
533 static HRESULT WINAPI HTMLFormElement_put_length(IHTMLFormElement *iface, LONG v)
535 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
536 FIXME("(%p)->(%d)\n", This, v);
537 return E_NOTIMPL;
540 static HRESULT WINAPI HTMLFormElement_get_length(IHTMLFormElement *iface, LONG *p)
542 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
543 nsresult nsres;
545 TRACE("(%p)->(%p)\n", This, p);
547 nsres = nsIDOMHTMLFormElement_GetLength(This->nsform, p);
548 if(NS_FAILED(nsres)) {
549 ERR("GetLength failed: %08x\n", nsres);
550 return E_FAIL;
553 return S_OK;
556 static HRESULT WINAPI HTMLFormElement__newEnum(IHTMLFormElement *iface, IUnknown **p)
558 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
559 FIXME("(%p)->(%p)\n", This, p);
560 return E_NOTIMPL;
563 static HRESULT WINAPI HTMLFormElement_item(IHTMLFormElement *iface, VARIANT name,
564 VARIANT index, IDispatch **pdisp)
566 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
568 TRACE("(%p)->(%s %s %p)\n", This, debugstr_variant(&name), debugstr_variant(&index), pdisp);
570 if(!pdisp)
571 return E_INVALIDARG;
572 *pdisp = NULL;
574 if(V_VT(&name) == VT_I4) {
575 if(V_I4(&name) < 0) {
576 *pdisp = NULL;
577 return dispex_compat_mode(&This->element.node.event_target.dispex) >= COMPAT_MODE_IE9
578 ? S_OK : E_INVALIDARG;
580 return htmlform_item(This, V_I4(&name), pdisp);
583 FIXME("Unsupported args\n");
584 return E_NOTIMPL;
587 static HRESULT WINAPI HTMLFormElement_tags(IHTMLFormElement *iface, VARIANT tagName,
588 IDispatch **pdisp)
590 HTMLFormElement *This = impl_from_IHTMLFormElement(iface);
591 FIXME("(%p)->(v %p)\n", This, pdisp);
592 return E_NOTIMPL;
595 static const IHTMLFormElementVtbl HTMLFormElementVtbl = {
596 HTMLFormElement_QueryInterface,
597 HTMLFormElement_AddRef,
598 HTMLFormElement_Release,
599 HTMLFormElement_GetTypeInfoCount,
600 HTMLFormElement_GetTypeInfo,
601 HTMLFormElement_GetIDsOfNames,
602 HTMLFormElement_Invoke,
603 HTMLFormElement_put_action,
604 HTMLFormElement_get_action,
605 HTMLFormElement_put_dir,
606 HTMLFormElement_get_dir,
607 HTMLFormElement_put_encoding,
608 HTMLFormElement_get_encoding,
609 HTMLFormElement_put_method,
610 HTMLFormElement_get_method,
611 HTMLFormElement_get_elements,
612 HTMLFormElement_put_target,
613 HTMLFormElement_get_target,
614 HTMLFormElement_put_name,
615 HTMLFormElement_get_name,
616 HTMLFormElement_put_onsubmit,
617 HTMLFormElement_get_onsubmit,
618 HTMLFormElement_put_onreset,
619 HTMLFormElement_get_onreset,
620 HTMLFormElement_submit,
621 HTMLFormElement_reset,
622 HTMLFormElement_put_length,
623 HTMLFormElement_get_length,
624 HTMLFormElement__newEnum,
625 HTMLFormElement_item,
626 HTMLFormElement_tags
629 static inline HTMLFormElement *impl_from_HTMLDOMNode(HTMLDOMNode *iface)
631 return CONTAINING_RECORD(iface, HTMLFormElement, element.node);
634 static HRESULT HTMLFormElement_QI(HTMLDOMNode *iface, REFIID riid, void **ppv)
636 HTMLFormElement *This = impl_from_HTMLDOMNode(iface);
638 *ppv = NULL;
640 if(IsEqualGUID(&IID_IUnknown, riid)) {
641 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
642 *ppv = &This->IHTMLFormElement_iface;
643 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
644 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
645 *ppv = &This->IHTMLFormElement_iface;
646 }else if(IsEqualGUID(&IID_IHTMLFormElement, riid)) {
647 TRACE("(%p)->(IID_IHTMLFormElement %p)\n", This, ppv);
648 *ppv = &This->IHTMLFormElement_iface;
649 }else if(IsEqualGUID(&DIID_DispHTMLFormElement, riid)) {
650 TRACE("(%p)->(DIID_DispHTMLFormElement %p)\n", This, ppv);
651 *ppv = &This->IHTMLFormElement_iface;
654 if(*ppv) {
655 IUnknown_AddRef((IUnknown*)*ppv);
656 return S_OK;
659 return HTMLElement_QI(&This->element.node, riid, ppv);
662 static HRESULT HTMLFormElement_get_dispid(HTMLDOMNode *iface,
663 BSTR name, DWORD grfdex, DISPID *pid)
665 HTMLFormElement *This = impl_from_HTMLDOMNode(iface);
666 nsIDOMHTMLCollection *elements;
667 nsAString nsstr, name_str;
668 UINT32 len, i;
669 nsresult nsres;
670 HRESULT hres = DISP_E_UNKNOWNNAME;
672 static const PRUnichar nameW[] = {'n','a','m','e',0};
674 TRACE("(%p)->(%s %x %p)\n", This, wine_dbgstr_w(name), grfdex, pid);
676 nsres = nsIDOMHTMLFormElement_GetElements(This->nsform, &elements);
677 if(NS_FAILED(nsres)) {
678 FIXME("GetElements failed: 0x%08x\n", nsres);
679 return E_FAIL;
682 nsres = nsIDOMHTMLCollection_GetLength(elements, &len);
683 if(NS_FAILED(nsres)) {
684 FIXME("GetLength failed: 0x%08x\n", nsres);
685 nsIDOMHTMLCollection_Release(elements);
686 return E_FAIL;
689 if(len > MSHTML_CUSTOM_DISPID_CNT)
690 len = MSHTML_CUSTOM_DISPID_CNT;
692 /* FIXME: Implement in more generic way */
693 if('0' <= *name && *name <= '9') {
694 WCHAR *end_ptr;
696 i = wcstoul(name, &end_ptr, 10);
697 if(!*end_ptr && i < len) {
698 *pid = MSHTML_DISPID_CUSTOM_MIN + i;
699 return S_OK;
703 nsAString_Init(&nsstr, NULL);
704 for(i = 0; i < len; ++i) {
705 nsIDOMNode *nsitem;
706 nsIDOMElement *elem;
707 const PRUnichar *str;
709 nsres = nsIDOMHTMLCollection_Item(elements, i, &nsitem);
710 if(NS_FAILED(nsres)) {
711 FIXME("Item failed: 0x%08x\n", nsres);
712 hres = E_FAIL;
713 break;
716 nsres = nsIDOMNode_QueryInterface(nsitem, &IID_nsIDOMElement, (void**)&elem);
717 nsIDOMNode_Release(nsitem);
718 if(NS_FAILED(nsres)) {
719 FIXME("Failed to get nsIDOMHTMLNode interface: 0x%08x\n", nsres);
720 hres = E_FAIL;
721 break;
724 /* compare by id attr */
725 nsres = nsIDOMElement_GetId(elem, &nsstr);
726 if(NS_FAILED(nsres)) {
727 FIXME("GetId failed: 0x%08x\n", nsres);
728 nsIDOMElement_Release(elem);
729 hres = E_FAIL;
730 break;
732 nsAString_GetData(&nsstr, &str);
733 if(!wcsicmp(str, name)) {
734 nsIDOMElement_Release(elem);
735 /* FIXME: using index for dispid */
736 *pid = MSHTML_DISPID_CUSTOM_MIN + i;
737 hres = S_OK;
738 break;
741 /* compare by name attr */
742 nsres = get_elem_attr_value(elem, nameW, &name_str, &str);
743 nsIDOMElement_Release(elem);
744 if(NS_SUCCEEDED(nsres)) {
745 if(!wcsicmp(str, name)) {
746 nsAString_Finish(&name_str);
747 /* FIXME: using index for dispid */
748 *pid = MSHTML_DISPID_CUSTOM_MIN + i;
749 hres = S_OK;
750 break;
752 nsAString_Finish(&name_str);
756 nsAString_Finish(&nsstr);
757 nsIDOMHTMLCollection_Release(elements);
758 return hres;
761 static HRESULT HTMLFormElement_invoke(HTMLDOMNode *iface,
762 DISPID id, LCID lcid, WORD flags, DISPPARAMS *params, VARIANT *res,
763 EXCEPINFO *ei, IServiceProvider *caller)
765 HTMLFormElement *This = impl_from_HTMLDOMNode(iface);
766 IDispatch *ret;
767 HRESULT hres;
769 TRACE("(%p)->(%x %x %x %p %p %p %p)\n", This, id, lcid, flags, params, res, ei, caller);
771 hres = htmlform_item(This, id - MSHTML_DISPID_CUSTOM_MIN, &ret);
772 if(FAILED(hres))
773 return hres;
775 if(ret) {
776 V_VT(res) = VT_DISPATCH;
777 V_DISPATCH(res) = ret;
778 }else {
779 V_VT(res) = VT_NULL;
781 return S_OK;
784 static HRESULT HTMLFormElement_handle_event(HTMLDOMNode *iface, DWORD eid, nsIDOMEvent *event, BOOL *prevent_default)
786 HTMLFormElement *This = impl_from_HTMLDOMNode(iface);
788 if(eid == EVENTID_SUBMIT) {
789 *prevent_default = TRUE;
790 return IHTMLFormElement_submit(&This->IHTMLFormElement_iface);
793 return HTMLElement_handle_event(&This->element.node, eid, event, prevent_default);
796 static void HTMLFormElement_traverse(HTMLDOMNode *iface, nsCycleCollectionTraversalCallback *cb)
798 HTMLFormElement *This = impl_from_HTMLDOMNode(iface);
800 if(This->nsform)
801 note_cc_edge((nsISupports*)This->nsform, "This->nsform", cb);
804 static void HTMLFormElement_unlink(HTMLDOMNode *iface)
806 HTMLFormElement *This = impl_from_HTMLDOMNode(iface);
808 if(This->nsform) {
809 nsIDOMHTMLFormElement *nsform = This->nsform;
811 This->nsform = NULL;
812 nsIDOMHTMLFormElement_Release(nsform);
816 static const NodeImplVtbl HTMLFormElementImplVtbl = {
817 &CLSID_HTMLFormElement,
818 HTMLFormElement_QI,
819 HTMLElement_destructor,
820 HTMLElement_cpc,
821 HTMLElement_clone,
822 HTMLFormElement_handle_event,
823 HTMLElement_get_attr_col,
824 NULL,
825 NULL,
826 NULL,
827 NULL,
828 NULL,
829 HTMLFormElement_get_dispid,
830 HTMLFormElement_invoke,
831 NULL,
832 HTMLFormElement_traverse,
833 HTMLFormElement_unlink
836 static const tid_t HTMLFormElement_iface_tids[] = {
837 HTMLELEMENT_TIDS,
838 IHTMLFormElement_tid,
842 static dispex_static_data_t HTMLFormElement_dispex = {
843 NULL,
844 DispHTMLFormElement_tid,
845 HTMLFormElement_iface_tids,
846 HTMLElement_init_dispex_info
849 HRESULT HTMLFormElement_Create(HTMLDocumentNode *doc, nsIDOMElement *nselem, HTMLElement **elem)
851 HTMLFormElement *ret;
852 nsresult nsres;
854 ret = heap_alloc_zero(sizeof(HTMLFormElement));
855 if(!ret)
856 return E_OUTOFMEMORY;
858 ret->IHTMLFormElement_iface.lpVtbl = &HTMLFormElementVtbl;
859 ret->element.node.vtbl = &HTMLFormElementImplVtbl;
861 HTMLElement_Init(&ret->element, doc, nselem, &HTMLFormElement_dispex);
863 nsres = nsIDOMElement_QueryInterface(nselem, &IID_nsIDOMHTMLFormElement, (void**)&ret->nsform);
864 assert(nsres == NS_OK);
866 *elem = &ret->element;
867 return S_OK;