mshtml: Added script execution time tests.
[wine] / dlls / mshtml / htmloption.c
1 /*
2  * Copyright 2007 Jacek Caban for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include <stdarg.h>
20
21 #define COBJMACROS
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "ole2.h"
27
28 #include "wine/debug.h"
29
30 #include "mshtml_private.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
33
34 struct HTMLOptionElement {
35     HTMLElement element;
36
37     IHTMLOptionElement IHTMLOptionElement_iface;
38
39     nsIDOMHTMLOptionElement *nsoption;
40 };
41
42 static inline HTMLOptionElement *impl_from_IHTMLOptionElement(IHTMLOptionElement *iface)
43 {
44     return CONTAINING_RECORD(iface, HTMLOptionElement, IHTMLOptionElement_iface);
45 }
46
47 static HRESULT WINAPI HTMLOptionElement_QueryInterface(IHTMLOptionElement *iface,
48         REFIID riid, void **ppv)
49 {
50     HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
51
52     return IHTMLDOMNode_QueryInterface(&This->element.node.IHTMLDOMNode_iface, riid, ppv);
53 }
54
55 static ULONG WINAPI HTMLOptionElement_AddRef(IHTMLOptionElement *iface)
56 {
57     HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
58
59     return IHTMLDOMNode_AddRef(&This->element.node.IHTMLDOMNode_iface);
60 }
61
62 static ULONG WINAPI HTMLOptionElement_Release(IHTMLOptionElement *iface)
63 {
64     HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
65
66     return IHTMLDOMNode_Release(&This->element.node.IHTMLDOMNode_iface);
67 }
68
69 static HRESULT WINAPI HTMLOptionElement_GetTypeInfoCount(IHTMLOptionElement *iface, UINT *pctinfo)
70 {
71     HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
72     return IDispatchEx_GetTypeInfoCount(&This->element.node.dispex.IDispatchEx_iface, pctinfo);
73 }
74
75 static HRESULT WINAPI HTMLOptionElement_GetTypeInfo(IHTMLOptionElement *iface, UINT iTInfo,
76                                               LCID lcid, ITypeInfo **ppTInfo)
77 {
78     HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
79     return IDispatchEx_GetTypeInfo(&This->element.node.dispex.IDispatchEx_iface, iTInfo, lcid,
80             ppTInfo);
81 }
82
83 static HRESULT WINAPI HTMLOptionElement_GetIDsOfNames(IHTMLOptionElement *iface, REFIID riid,
84                                                 LPOLESTR *rgszNames, UINT cNames,
85                                                 LCID lcid, DISPID *rgDispId)
86 {
87     HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
88     return IDispatchEx_GetIDsOfNames(&This->element.node.dispex.IDispatchEx_iface, riid, rgszNames,
89             cNames, lcid, rgDispId);
90 }
91
92 static HRESULT WINAPI HTMLOptionElement_Invoke(IHTMLOptionElement *iface, DISPID dispIdMember,
93                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
94                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
95 {
96     HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
97     return IDispatchEx_Invoke(&This->element.node.dispex.IDispatchEx_iface, dispIdMember, riid,
98             lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
99 }
100
101 static HRESULT WINAPI HTMLOptionElement_put_selected(IHTMLOptionElement *iface, VARIANT_BOOL v)
102 {
103     HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
104     nsresult nsres;
105
106     TRACE("(%p)->(%x)\n", This, v);
107
108     nsres = nsIDOMHTMLOptionElement_SetSelected(This->nsoption, v != VARIANT_FALSE);
109     if(NS_FAILED(nsres)) {
110         ERR("SetSelected failed: %08x\n", nsres);
111         return E_FAIL;
112     }
113
114     return S_OK;
115 }
116
117 static HRESULT WINAPI HTMLOptionElement_get_selected(IHTMLOptionElement *iface, VARIANT_BOOL *p)
118 {
119     HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
120     PRBool selected;
121     nsresult nsres;
122
123     TRACE("(%p)->(%p)\n", This, p);
124
125     nsres = nsIDOMHTMLOptionElement_GetSelected(This->nsoption, &selected);
126     if(NS_FAILED(nsres)) {
127         ERR("GetSelected failed: %08x\n", nsres);
128         return E_FAIL;
129     }
130
131     *p = selected ? VARIANT_TRUE : VARIANT_FALSE;
132     return S_OK;
133 }
134
135 static HRESULT WINAPI HTMLOptionElement_put_value(IHTMLOptionElement *iface, BSTR v)
136 {
137     HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
138     nsAString value_str;
139     nsresult nsres;
140
141     TRACE("(%p)->(%s)\n", This, debugstr_w(v));
142
143     nsAString_InitDepend(&value_str, v);
144     nsres = nsIDOMHTMLOptionElement_SetValue(This->nsoption, &value_str);
145     nsAString_Finish(&value_str);
146     if(NS_FAILED(nsres))
147         ERR("SetValue failed: %08x\n", nsres);
148
149     return S_OK;
150 }
151
152 static HRESULT WINAPI HTMLOptionElement_get_value(IHTMLOptionElement *iface, BSTR *p)
153 {
154     HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
155     nsAString value_str;
156     const PRUnichar *value;
157     nsresult nsres;
158
159     TRACE("(%p)->(%p)\n", This, p);
160
161     nsAString_Init(&value_str, NULL);
162     nsres = nsIDOMHTMLOptionElement_GetValue(This->nsoption, &value_str);
163     if(NS_SUCCEEDED(nsres)) {
164         nsAString_GetData(&value_str, &value);
165         *p = SysAllocString(value);
166     }else {
167         ERR("GetValue failed: %08x\n", nsres);
168         *p = NULL;
169     }
170     nsAString_Finish(&value_str);
171
172     return S_OK;
173 }
174
175 static HRESULT WINAPI HTMLOptionElement_put_defaultSelected(IHTMLOptionElement *iface, VARIANT_BOOL v)
176 {
177     HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
178     FIXME("(%p)->(%x)\n", This, v);
179     return E_NOTIMPL;
180 }
181
182 static HRESULT WINAPI HTMLOptionElement_get_defaultSelected(IHTMLOptionElement *iface, VARIANT_BOOL *p)
183 {
184     HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
185     FIXME("(%p)->(%p)\n", This, p);
186     return E_NOTIMPL;
187 }
188
189 static HRESULT WINAPI HTMLOptionElement_put_index(IHTMLOptionElement *iface, LONG v)
190 {
191     HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
192     FIXME("(%p)->(%d)\n", This, v);
193     return E_NOTIMPL;
194 }
195
196 static HRESULT WINAPI HTMLOptionElement_get_index(IHTMLOptionElement *iface, LONG *p)
197 {
198     HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
199     FIXME("(%p)->(%p)\n", This, p);
200     return E_NOTIMPL;
201 }
202
203 static HRESULT WINAPI HTMLOptionElement_put_text(IHTMLOptionElement *iface, BSTR v)
204 {
205     HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
206     nsIDOMText *text_node;
207     nsAString text_str;
208     nsIDOMNode *tmp;
209     nsresult nsres;
210
211     TRACE("(%p)->(%s)\n", This, debugstr_w(v));
212
213     if(!This->element.node.doc->nsdoc) {
214         WARN("NULL nsdoc\n");
215         return E_UNEXPECTED;
216     }
217
218     while(1) {
219         nsIDOMNode *child;
220
221         nsres = nsIDOMHTMLOptionElement_GetFirstChild(This->nsoption, &child);
222         if(NS_FAILED(nsres) || !child)
223             break;
224
225         nsres = nsIDOMHTMLOptionElement_RemoveChild(This->nsoption, child, &tmp);
226         nsIDOMNode_Release(child);
227         if(NS_SUCCEEDED(nsres)) {
228             nsIDOMNode_Release(tmp);
229         }else {
230             ERR("RemoveChild failed: %08x\n", nsres);
231             break;
232         }
233     }
234
235     nsAString_InitDepend(&text_str, v);
236     nsres = nsIDOMHTMLDocument_CreateTextNode(This->element.node.doc->nsdoc, &text_str, &text_node);
237     nsAString_Finish(&text_str);
238     if(NS_FAILED(nsres)) {
239         ERR("CreateTextNode failed: %08x\n", nsres);
240         return E_FAIL;
241     }
242
243     nsres = nsIDOMHTMLOptionElement_AppendChild(This->nsoption, (nsIDOMNode*)text_node, &tmp);
244     if(NS_SUCCEEDED(nsres))
245         nsIDOMNode_Release(tmp);
246     else
247         ERR("AppendChild failed: %08x\n", nsres);
248
249     return S_OK;
250 }
251
252 static HRESULT WINAPI HTMLOptionElement_get_text(IHTMLOptionElement *iface, BSTR *p)
253 {
254     HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
255     nsAString text_str;
256     const PRUnichar *text;
257     nsresult nsres;
258
259     TRACE("(%p)->(%p)\n", This, p);
260
261     nsAString_Init(&text_str, NULL);
262     nsres = nsIDOMHTMLOptionElement_GetText(This->nsoption, &text_str);
263     if(NS_SUCCEEDED(nsres)) {
264         nsAString_GetData(&text_str, &text);
265         *p = SysAllocString(text);
266     }else {
267         ERR("GetText failed: %08x\n", nsres);
268         *p = NULL;
269     }
270     nsAString_Finish(&text_str);
271
272     return S_OK;
273 }
274
275 static HRESULT WINAPI HTMLOptionElement_get_form(IHTMLOptionElement *iface, IHTMLFormElement **p)
276 {
277     HTMLOptionElement *This = impl_from_IHTMLOptionElement(iface);
278     FIXME("(%p)->(%p)\n", This, p);
279     return E_NOTIMPL;
280 }
281
282 static const IHTMLOptionElementVtbl HTMLOptionElementVtbl = {
283     HTMLOptionElement_QueryInterface,
284     HTMLOptionElement_AddRef,
285     HTMLOptionElement_Release,
286     HTMLOptionElement_GetTypeInfoCount,
287     HTMLOptionElement_GetTypeInfo,
288     HTMLOptionElement_GetIDsOfNames,
289     HTMLOptionElement_Invoke,
290     HTMLOptionElement_put_selected,
291     HTMLOptionElement_get_selected,
292     HTMLOptionElement_put_value,
293     HTMLOptionElement_get_value,
294     HTMLOptionElement_put_defaultSelected,
295     HTMLOptionElement_get_defaultSelected,
296     HTMLOptionElement_put_index,
297     HTMLOptionElement_get_index,
298     HTMLOptionElement_put_text,
299     HTMLOptionElement_get_text,
300     HTMLOptionElement_get_form
301 };
302
303 static inline HTMLOptionElement *impl_from_HTMLDOMNode(HTMLDOMNode *iface)
304 {
305     return CONTAINING_RECORD(iface, HTMLOptionElement, element.node);
306 }
307
308 static HRESULT HTMLOptionElement_QI(HTMLDOMNode *iface, REFIID riid, void **ppv)
309 {
310     HTMLOptionElement *This = impl_from_HTMLDOMNode(iface);
311
312     *ppv = NULL;
313
314     if(IsEqualGUID(&IID_IUnknown, riid)) {
315         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
316         *ppv = &This->IHTMLOptionElement_iface;
317     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
318         TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
319         *ppv = &This->IHTMLOptionElement_iface;
320     }else if(IsEqualGUID(&IID_IHTMLOptionElement, riid)) {
321         TRACE("(%p)->(IID_IHTMLOptionElement %p)\n", This, ppv);
322         *ppv = &This->IHTMLOptionElement_iface;
323     }
324
325     if(*ppv) {
326         IUnknown_AddRef((IUnknown*)*ppv);
327         return S_OK;
328     }
329
330     return HTMLElement_QI(&This->element.node, riid, ppv);
331 }
332
333 static void HTMLOptionElement_destructor(HTMLDOMNode *iface)
334 {
335     HTMLOptionElement *This = impl_from_HTMLDOMNode(iface);
336
337     if(This->nsoption)
338         nsIDOMHTMLOptionElement_Release(This->nsoption);
339
340     HTMLElement_destructor(&This->element.node);
341 }
342
343 static const NodeImplVtbl HTMLOptionElementImplVtbl = {
344     HTMLOptionElement_QI,
345     HTMLOptionElement_destructor,
346     HTMLElement_clone
347 };
348
349 static const tid_t HTMLOptionElement_iface_tids[] = {
350     HTMLELEMENT_TIDS,
351     IHTMLOptionElement_tid,
352     0
353 };
354 static dispex_static_data_t HTMLOptionElement_dispex = {
355     NULL,
356     DispHTMLOptionElement_tid,
357     NULL,
358     HTMLOptionElement_iface_tids
359 };
360
361 HRESULT HTMLOptionElement_Create(HTMLDocumentNode *doc, nsIDOMHTMLElement *nselem, HTMLElement **elem)
362 {
363     HTMLOptionElement *ret;
364     nsresult nsres;
365
366     ret = heap_alloc_zero(sizeof(HTMLOptionElement));
367     if(!ret)
368         return E_OUTOFMEMORY;
369
370     ret->IHTMLOptionElement_iface.lpVtbl = &HTMLOptionElementVtbl;
371     ret->element.node.vtbl = &HTMLOptionElementImplVtbl;
372
373     nsres = nsIDOMHTMLElement_QueryInterface(nselem, &IID_nsIDOMHTMLOptionElement, (void**)&ret->nsoption);
374     if(NS_FAILED(nsres)) {
375         ERR("Could not get nsIDOMHTMLOptionElement interface: %08x\n", nsres);
376         heap_free(ret);
377         return E_FAIL;
378     }
379
380     HTMLElement_Init(&ret->element, doc, nselem, &HTMLOptionElement_dispex);
381
382     *elem = &ret->element;
383     return S_OK;
384 }
385
386 static inline HTMLOptionElementFactory *impl_from_IHTMLOptionElementFactory(IHTMLOptionElementFactory *iface)
387 {
388     return CONTAINING_RECORD(iface, HTMLOptionElementFactory, IHTMLOptionElementFactory_iface);
389 }
390
391 static HRESULT WINAPI HTMLOptionElementFactory_QueryInterface(IHTMLOptionElementFactory *iface,
392                                                               REFIID riid, void **ppv)
393 {
394     HTMLOptionElementFactory *This = impl_from_IHTMLOptionElementFactory(iface);
395
396     *ppv = NULL;
397
398     if(IsEqualGUID(&IID_IUnknown, riid)) {
399         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
400         *ppv = &This->IHTMLOptionElementFactory_iface;
401     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
402         TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
403         *ppv = &This->IHTMLOptionElementFactory_iface;
404     }else if(IsEqualGUID(&IID_IHTMLOptionElementFactory, riid)) {
405         TRACE("(%p)->(IID_IHTMLOptionElementFactory %p)\n", This, ppv);
406         *ppv = &This->IHTMLOptionElementFactory_iface;
407     }
408
409     if(*ppv) {
410         IUnknown_AddRef((IUnknown*)*ppv);
411         return S_OK;
412     }
413
414     WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
415     return E_NOINTERFACE;
416 }
417
418 static ULONG WINAPI HTMLOptionElementFactory_AddRef(IHTMLOptionElementFactory *iface)
419 {
420     HTMLOptionElementFactory *This = impl_from_IHTMLOptionElementFactory(iface);
421     LONG ref = InterlockedIncrement(&This->ref);
422
423     TRACE("(%p) ref=%d\n", This, ref);
424
425     return ref;
426 }
427
428 static ULONG WINAPI HTMLOptionElementFactory_Release(IHTMLOptionElementFactory *iface)
429 {
430     HTMLOptionElementFactory *This = impl_from_IHTMLOptionElementFactory(iface);
431     LONG ref = InterlockedDecrement(&This->ref);
432
433     TRACE("(%p) ref=%d\n", This, ref);
434
435     if(!ref)
436         heap_free(This);
437
438     return ref;
439 }
440
441 static HRESULT WINAPI HTMLOptionElementFactory_GetTypeInfoCount(IHTMLOptionElementFactory *iface, UINT *pctinfo)
442 {
443     HTMLOptionElementFactory *This = impl_from_IHTMLOptionElementFactory(iface);
444     FIXME("(%p)->(%p)\n", This, pctinfo);
445     return E_NOTIMPL;
446 }
447
448 static HRESULT WINAPI HTMLOptionElementFactory_GetTypeInfo(IHTMLOptionElementFactory *iface, UINT iTInfo,
449                                               LCID lcid, ITypeInfo **ppTInfo)
450 {
451     HTMLOptionElementFactory *This = impl_from_IHTMLOptionElementFactory(iface);
452     FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
453     return E_NOTIMPL;
454 }
455
456 static HRESULT WINAPI HTMLOptionElementFactory_GetIDsOfNames(IHTMLOptionElementFactory *iface, REFIID riid,
457                                                 LPOLESTR *rgszNames, UINT cNames,
458                                                 LCID lcid, DISPID *rgDispId)
459 {
460     HTMLOptionElementFactory *This = impl_from_IHTMLOptionElementFactory(iface);
461     FIXME("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
462                                         lcid, rgDispId);
463     return E_NOTIMPL;
464 }
465
466 static HRESULT WINAPI HTMLOptionElementFactory_Invoke(IHTMLOptionElementFactory *iface, DISPID dispIdMember,
467                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
468                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
469 {
470     HTMLOptionElementFactory *This = impl_from_IHTMLOptionElementFactory(iface);
471     FIXME("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
472             lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
473     return E_NOTIMPL;
474 }
475
476 static HRESULT WINAPI HTMLOptionElementFactory_create(IHTMLOptionElementFactory *iface,
477         VARIANT text, VARIANT value, VARIANT defaultselected, VARIANT selected,
478         IHTMLOptionElement **optelem)
479 {
480     HTMLOptionElementFactory *This = impl_from_IHTMLOptionElementFactory(iface);
481     nsIDOMHTMLElement *nselem;
482     HTMLDOMNode *node;
483     HRESULT hres;
484
485     static const PRUnichar optionW[] = {'O','P','T','I','O','N',0};
486
487     TRACE("(%p)->(%s %s %s %s %p)\n", This, debugstr_variant(&text), debugstr_variant(&value),
488           debugstr_variant(&defaultselected), debugstr_variant(&selected), optelem);
489
490     if(!This->window || !This->window->doc) {
491         WARN("NULL doc\n");
492         return E_UNEXPECTED;
493     }
494
495     *optelem = NULL;
496
497     hres = create_nselem(This->window->doc, optionW, &nselem);
498     if(FAILED(hres))
499         return hres;
500
501     hres = get_node(This->window->doc, (nsIDOMNode*)nselem, TRUE, &node);
502     nsIDOMHTMLElement_Release(nselem);
503     if(FAILED(hres))
504         return hres;
505
506     hres = IHTMLDOMNode_QueryInterface(&node->IHTMLDOMNode_iface,
507             &IID_IHTMLOptionElement, (void**)optelem);
508
509     if(V_VT(&text) == VT_BSTR)
510         IHTMLOptionElement_put_text(*optelem, V_BSTR(&text));
511     else if(V_VT(&text) != VT_EMPTY)
512         FIXME("Unsupported text vt=%d\n", V_VT(&text));
513
514     if(V_VT(&value) == VT_BSTR)
515         IHTMLOptionElement_put_value(*optelem, V_BSTR(&value));
516     else if(V_VT(&value) != VT_EMPTY)
517         FIXME("Unsupported value vt=%d\n", V_VT(&value));
518
519     if(V_VT(&defaultselected) != VT_EMPTY)
520         FIXME("Unsupported defaultselected vt=%d\n", V_VT(&defaultselected));
521     if(V_VT(&selected) != VT_EMPTY)
522         FIXME("Unsupported selected vt=%d\n", V_VT(&selected));
523
524     return S_OK;
525 }
526
527 static const IHTMLOptionElementFactoryVtbl HTMLOptionElementFactoryVtbl = {
528     HTMLOptionElementFactory_QueryInterface,
529     HTMLOptionElementFactory_AddRef,
530     HTMLOptionElementFactory_Release,
531     HTMLOptionElementFactory_GetTypeInfoCount,
532     HTMLOptionElementFactory_GetTypeInfo,
533     HTMLOptionElementFactory_GetIDsOfNames,
534     HTMLOptionElementFactory_Invoke,
535     HTMLOptionElementFactory_create
536 };
537
538 HTMLOptionElementFactory *HTMLOptionElementFactory_Create(HTMLWindow *window)
539 {
540     HTMLOptionElementFactory *ret;
541
542     ret = heap_alloc(sizeof(HTMLOptionElementFactory));
543
544     ret->IHTMLOptionElementFactory_iface.lpVtbl = &HTMLOptionElementFactoryVtbl;
545     ret->ref = 1;
546     ret->window = window;
547
548     return ret;
549 }