mshtml: Store current IHTMLEventObj in DOMEvent and create it in fire_event_obj if...
[wine.git] / dlls / mshtml / htmlscript.c
blob4c3ea167547037b90e69fecbb3e523c268ce13a5
1 /*
2 * Copyright 2008 Jacek Caban 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 "htmlscript.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
37 static inline HTMLScriptElement *impl_from_IHTMLScriptElement(IHTMLScriptElement *iface)
39 return CONTAINING_RECORD(iface, HTMLScriptElement, IHTMLScriptElement_iface);
42 static HRESULT WINAPI HTMLScriptElement_QueryInterface(IHTMLScriptElement *iface,
43 REFIID riid, void **ppv)
45 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
47 return IHTMLDOMNode_QueryInterface(&This->element.node.IHTMLDOMNode_iface, riid, ppv);
50 static ULONG WINAPI HTMLScriptElement_AddRef(IHTMLScriptElement *iface)
52 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
54 return IHTMLDOMNode_AddRef(&This->element.node.IHTMLDOMNode_iface);
57 static ULONG WINAPI HTMLScriptElement_Release(IHTMLScriptElement *iface)
59 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
61 return IHTMLDOMNode_Release(&This->element.node.IHTMLDOMNode_iface);
64 static HRESULT WINAPI HTMLScriptElement_GetTypeInfoCount(IHTMLScriptElement *iface, UINT *pctinfo)
66 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
67 return IDispatchEx_GetTypeInfoCount(&This->element.node.event_target.dispex.IDispatchEx_iface, pctinfo);
70 static HRESULT WINAPI HTMLScriptElement_GetTypeInfo(IHTMLScriptElement *iface, UINT iTInfo,
71 LCID lcid, ITypeInfo **ppTInfo)
73 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
74 return IDispatchEx_GetTypeInfo(&This->element.node.event_target.dispex.IDispatchEx_iface, iTInfo, lcid,
75 ppTInfo);
78 static HRESULT WINAPI HTMLScriptElement_GetIDsOfNames(IHTMLScriptElement *iface, REFIID riid,
79 LPOLESTR *rgszNames, UINT cNames,
80 LCID lcid, DISPID *rgDispId)
82 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
83 return IDispatchEx_GetIDsOfNames(&This->element.node.event_target.dispex.IDispatchEx_iface, riid, rgszNames,
84 cNames, lcid, rgDispId);
87 static HRESULT WINAPI HTMLScriptElement_Invoke(IHTMLScriptElement *iface, DISPID dispIdMember,
88 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
89 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
91 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
92 return IDispatchEx_Invoke(&This->element.node.event_target.dispex.IDispatchEx_iface, dispIdMember, riid,
93 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
96 static HRESULT WINAPI HTMLScriptElement_put_src(IHTMLScriptElement *iface, BSTR v)
98 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
99 nsAString src_str;
100 nsresult nsres;
101 HRESULT hres;
103 TRACE("(%p)->(%s)\n", This, debugstr_w(v));
105 nsAString_InitDepend(&src_str, v);
106 nsres = nsIDOMHTMLScriptElement_SetSrc(This->nsscript, &src_str);
107 nsAString_Finish(&src_str);
108 if(NS_FAILED(nsres)) {
109 ERR("SetSrc failed: %08x\n", nsres);
110 return E_FAIL;
113 if(This->parsed) {
114 WARN("already parsed\n");
115 return S_OK;
118 if(This->binding) {
119 FIXME("binding in progress\n");
120 return E_FAIL;
123 nsAString_Init(&src_str, NULL);
124 nsres = nsIDOMHTMLScriptElement_GetSrc(This->nsscript, &src_str);
125 if(NS_SUCCEEDED(nsres)) {
126 const PRUnichar *src;
127 nsAString_GetData(&src_str, &src);
128 hres = load_script(This, src, TRUE);
129 }else {
130 ERR("SetSrc failed: %08x\n", nsres);
131 hres = E_FAIL;
133 nsAString_Finish(&src_str);
134 return hres;
137 static HRESULT WINAPI HTMLScriptElement_get_src(IHTMLScriptElement *iface, BSTR *p)
139 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
140 nsAString src_str;
141 nsresult nsres;
143 TRACE("(%p)->(%p)\n", This, p);
145 nsAString_Init(&src_str, NULL);
146 nsres = nsIDOMHTMLScriptElement_GetSrc(This->nsscript, &src_str);
147 return return_nsstr(nsres, &src_str, p);
150 static HRESULT WINAPI HTMLScriptElement_put_htmlFor(IHTMLScriptElement *iface, BSTR v)
152 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
153 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
154 return E_NOTIMPL;
157 static HRESULT WINAPI HTMLScriptElement_get_htmlFor(IHTMLScriptElement *iface, BSTR *p)
159 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
160 FIXME("(%p)->(%p)\n", This, p);
161 return E_NOTIMPL;
164 static HRESULT WINAPI HTMLScriptElement_put_event(IHTMLScriptElement *iface, BSTR v)
166 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
167 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
168 return E_NOTIMPL;
171 static HRESULT WINAPI HTMLScriptElement_get_event(IHTMLScriptElement *iface, BSTR *p)
173 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
174 FIXME("(%p)->(%p)\n", This, p);
175 return E_NOTIMPL;
178 static HRESULT WINAPI HTMLScriptElement_put_text(IHTMLScriptElement *iface, BSTR v)
180 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
181 HTMLInnerWindow *window;
182 nsIDOMNode *parent;
183 nsAString text_str;
184 nsresult nsres;
186 TRACE("(%p)->(%s)\n", This, debugstr_w(v));
188 if(!This->element.node.doc || !This->element.node.doc->window) {
189 WARN("no windoow\n");
190 return E_UNEXPECTED;
193 window = This->element.node.doc->window;
195 nsAString_InitDepend(&text_str, v);
196 nsres = nsIDOMHTMLScriptElement_SetText(This->nsscript, &text_str);
197 nsAString_Finish(&text_str);
198 if(NS_FAILED(nsres)) {
199 ERR("SetSrc failed: %08x\n", nsres);
200 return E_FAIL;
203 nsres = nsIDOMHTMLElement_GetParentNode(This->element.nselem, &parent);
204 if(NS_FAILED(nsres) || !parent) {
205 TRACE("No parent, not executing\n");
206 This->parse_on_bind = TRUE;
207 return S_OK;
210 nsIDOMNode_Release(parent);
211 doc_insert_script(window, This, FALSE);
212 return S_OK;
215 static HRESULT WINAPI HTMLScriptElement_get_text(IHTMLScriptElement *iface, BSTR *p)
217 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
218 nsAString nsstr;
219 nsresult nsres;
221 TRACE("(%p)->(%p)\n", This, p);
223 nsAString_Init(&nsstr, NULL);
224 nsres = nsIDOMHTMLScriptElement_GetText(This->nsscript, &nsstr);
225 return return_nsstr(nsres, &nsstr, p);
228 static HRESULT WINAPI HTMLScriptElement_put_defer(IHTMLScriptElement *iface, VARIANT_BOOL v)
230 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
231 HRESULT hr = S_OK;
232 nsresult nsres;
234 TRACE("(%p)->(%x)\n", This, v);
236 nsres = nsIDOMHTMLScriptElement_SetDefer(This->nsscript, v != VARIANT_FALSE);
237 if(NS_FAILED(nsres))
239 hr = E_FAIL;
242 return hr;
245 static HRESULT WINAPI HTMLScriptElement_get_defer(IHTMLScriptElement *iface, VARIANT_BOOL *p)
247 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
248 cpp_bool defer = FALSE;
249 nsresult nsres;
251 TRACE("(%p)->(%p)\n", This, p);
253 if(!p)
254 return E_INVALIDARG;
256 nsres = nsIDOMHTMLScriptElement_GetDefer(This->nsscript, &defer);
257 if(NS_FAILED(nsres)) {
258 ERR("GetSrc failed: %08x\n", nsres);
261 *p = defer ? VARIANT_TRUE : VARIANT_FALSE;
263 TRACE("*p = %d\n", *p);
264 return S_OK;
267 static HRESULT WINAPI HTMLScriptElement_get_readyState(IHTMLScriptElement *iface, BSTR *p)
269 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
271 TRACE("(%p)->(%p)\n", This, p);
273 return get_readystate_string(This->readystate, p);
276 static HRESULT WINAPI HTMLScriptElement_put_onerror(IHTMLScriptElement *iface, VARIANT v)
278 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
280 FIXME("(%p)->(%s) semi-stub\n", This, debugstr_variant(&v));
282 return set_node_event(&This->element.node, EVENTID_ERROR, &v);
285 static HRESULT WINAPI HTMLScriptElement_get_onerror(IHTMLScriptElement *iface, VARIANT *p)
287 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
289 TRACE("(%p)->(%p)\n", This, p);
291 return get_node_event(&This->element.node, EVENTID_ERROR, p);
294 static HRESULT WINAPI HTMLScriptElement_put_type(IHTMLScriptElement *iface, BSTR v)
296 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
297 nsAString nstype_str;
298 nsresult nsres;
300 TRACE("(%p)->(%s)\n", This, debugstr_w(v));
302 nsAString_Init(&nstype_str, v);
303 nsres = nsIDOMHTMLScriptElement_SetType(This->nsscript, &nstype_str);
304 if (NS_FAILED(nsres))
305 ERR("SetType failed: %08x\n", nsres);
306 nsAString_Finish (&nstype_str);
308 return S_OK;
311 static HRESULT WINAPI HTMLScriptElement_get_type(IHTMLScriptElement *iface, BSTR *p)
313 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
314 nsAString nstype_str;
315 nsresult nsres;
317 TRACE("(%p)->(%p)\n", This, p);
319 nsAString_Init(&nstype_str, NULL);
320 nsres = nsIDOMHTMLScriptElement_GetType(This->nsscript, &nstype_str);
321 return return_nsstr(nsres, &nstype_str, p);
324 static const IHTMLScriptElementVtbl HTMLScriptElementVtbl = {
325 HTMLScriptElement_QueryInterface,
326 HTMLScriptElement_AddRef,
327 HTMLScriptElement_Release,
328 HTMLScriptElement_GetTypeInfoCount,
329 HTMLScriptElement_GetTypeInfo,
330 HTMLScriptElement_GetIDsOfNames,
331 HTMLScriptElement_Invoke,
332 HTMLScriptElement_put_src,
333 HTMLScriptElement_get_src,
334 HTMLScriptElement_put_htmlFor,
335 HTMLScriptElement_get_htmlFor,
336 HTMLScriptElement_put_event,
337 HTMLScriptElement_get_event,
338 HTMLScriptElement_put_text,
339 HTMLScriptElement_get_text,
340 HTMLScriptElement_put_defer,
341 HTMLScriptElement_get_defer,
342 HTMLScriptElement_get_readyState,
343 HTMLScriptElement_put_onerror,
344 HTMLScriptElement_get_onerror,
345 HTMLScriptElement_put_type,
346 HTMLScriptElement_get_type
349 static inline HTMLScriptElement *impl_from_HTMLDOMNode(HTMLDOMNode *iface)
351 return CONTAINING_RECORD(iface, HTMLScriptElement, element.node);
354 static HRESULT HTMLScriptElement_QI(HTMLDOMNode *iface, REFIID riid, void **ppv)
356 HTMLScriptElement *This = impl_from_HTMLDOMNode(iface);
358 *ppv = NULL;
360 if(IsEqualGUID(&IID_IUnknown, riid)) {
361 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
362 *ppv = &This->IHTMLScriptElement_iface;
363 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
364 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
365 *ppv = &This->IHTMLScriptElement_iface;
366 }else if(IsEqualGUID(&IID_IHTMLScriptElement, riid)) {
367 TRACE("(%p)->(IID_IHTMLScriptElement %p)\n", This, ppv);
368 *ppv = &This->IHTMLScriptElement_iface;
371 if(*ppv) {
372 IUnknown_AddRef((IUnknown*)*ppv);
373 return S_OK;
376 return HTMLElement_QI(&This->element.node, riid, ppv);
379 static void HTMLScriptElement_destructor(HTMLDOMNode *iface)
381 HTMLScriptElement *This = impl_from_HTMLDOMNode(iface);
382 heap_free(This->src_text);
383 HTMLElement_destructor(&This->element.node);
386 static HRESULT HTMLScriptElement_get_readystate(HTMLDOMNode *iface, BSTR *p)
388 HTMLScriptElement *This = impl_from_HTMLDOMNode(iface);
390 return IHTMLScriptElement_get_readyState(&This->IHTMLScriptElement_iface, p);
393 static HRESULT HTMLScriptElement_bind_to_tree(HTMLDOMNode *iface)
395 HTMLScriptElement *This = impl_from_HTMLDOMNode(iface);
397 TRACE("(%p)\n", This);
399 if(!This->parse_on_bind)
400 return S_OK;
402 if(!This->element.node.doc || !This->element.node.doc->window) {
403 ERR("No window\n");
404 return E_UNEXPECTED;
407 This->parse_on_bind = FALSE;
408 doc_insert_script(This->element.node.doc->window, This, FALSE);
409 return S_OK;
412 static void HTMLScriptElement_traverse(HTMLDOMNode *iface, nsCycleCollectionTraversalCallback *cb)
414 HTMLScriptElement *This = impl_from_HTMLDOMNode(iface);
416 if(This->nsscript)
417 note_cc_edge((nsISupports*)This->nsscript, "This->nsscript", cb);
420 static void HTMLScriptElement_unlink(HTMLDOMNode *iface)
422 HTMLScriptElement *This = impl_from_HTMLDOMNode(iface);
424 if(This->nsscript) {
425 nsIDOMHTMLScriptElement *nsscript = This->nsscript;
427 This->nsscript = NULL;
428 nsIDOMHTMLScriptElement_Release(nsscript);
432 static const NodeImplVtbl HTMLScriptElementImplVtbl = {
433 &CLSID_HTMLScriptElement,
434 HTMLScriptElement_QI,
435 HTMLScriptElement_destructor,
436 HTMLElement_cpc,
437 HTMLElement_clone,
438 HTMLElement_handle_event,
439 HTMLElement_get_attr_col,
440 NULL,
441 NULL,
442 NULL,
443 NULL,
444 HTMLScriptElement_get_readystate,
445 NULL,
446 NULL,
447 HTMLScriptElement_bind_to_tree,
448 HTMLScriptElement_traverse,
449 HTMLScriptElement_unlink
452 HRESULT script_elem_from_nsscript(HTMLDocumentNode *doc, nsIDOMHTMLScriptElement *nsscript, HTMLScriptElement **ret)
454 nsIDOMNode *nsnode;
455 HTMLDOMNode *node;
456 nsresult nsres;
457 HRESULT hres;
459 nsres = nsIDOMHTMLScriptElement_QueryInterface(nsscript, &IID_nsIDOMNode, (void**)&nsnode);
460 assert(nsres == NS_OK);
462 hres = get_node(doc, nsnode, TRUE, &node);
463 nsIDOMNode_Release(nsnode);
464 if(FAILED(hres))
465 return hres;
467 assert(node->vtbl == &HTMLScriptElementImplVtbl);
468 *ret = impl_from_HTMLDOMNode(node);
469 return S_OK;
472 static const tid_t HTMLScriptElement_iface_tids[] = {
473 HTMLELEMENT_TIDS,
474 IHTMLScriptElement_tid,
478 static dispex_static_data_t HTMLScriptElement_dispex = {
479 NULL,
480 DispHTMLScriptElement_tid,
481 HTMLScriptElement_iface_tids,
482 HTMLElement_init_dispex_info
485 HRESULT HTMLScriptElement_Create(HTMLDocumentNode *doc, nsIDOMHTMLElement *nselem, HTMLElement **elem)
487 HTMLScriptElement *ret;
488 nsresult nsres;
490 ret = heap_alloc_zero(sizeof(HTMLScriptElement));
491 if(!ret)
492 return E_OUTOFMEMORY;
494 ret->IHTMLScriptElement_iface.lpVtbl = &HTMLScriptElementVtbl;
495 ret->element.node.vtbl = &HTMLScriptElementImplVtbl;
497 HTMLElement_Init(&ret->element, doc, nselem, &HTMLScriptElement_dispex);
499 nsres = nsIDOMHTMLElement_QueryInterface(nselem, &IID_nsIDOMHTMLScriptElement, (void**)&ret->nsscript);
500 assert(nsres == NS_OK);
502 *elem = &ret->element;
503 return S_OK;