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
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
,
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 HTMLInnerWindow
*window
;
104 TRACE("(%p)->(%s)\n", This
, debugstr_w(v
));
106 if(!This
->element
.node
.doc
|| !This
->element
.node
.doc
->window
) {
107 WARN("no windoow\n");
111 window
= This
->element
.node
.doc
->window
;
113 nsAString_InitDepend(&src_str
, v
);
114 nsres
= nsIDOMHTMLScriptElement_SetSrc(This
->nsscript
, &src_str
);
115 nsAString_Finish(&src_str
);
116 if(NS_FAILED(nsres
)) {
117 ERR("SetSrc failed: %08x\n", nsres
);
122 WARN("already parsed\n");
126 if(window
->parser_callback_cnt
) {
127 script_queue_entry_t
*queue
;
129 queue
= heap_alloc(sizeof(*queue
));
131 return E_OUTOFMEMORY
;
133 IHTMLScriptElement_AddRef(&This
->IHTMLScriptElement_iface
);
134 queue
->script
= This
;
136 list_add_tail(&window
->script_queue
, &queue
->entry
);
140 nsres
= nsIDOMHTMLElement_GetParentNode(This
->element
.nselem
, &parent
);
141 if(NS_FAILED(nsres
) || !parent
) {
142 TRACE("No parent, not executing\n");
143 This
->parse_on_bind
= TRUE
;
147 nsIDOMNode_Release(parent
);
148 doc_insert_script(window
, This
);
152 static HRESULT WINAPI
HTMLScriptElement_get_src(IHTMLScriptElement
*iface
, BSTR
*p
)
154 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
158 TRACE("(%p)->(%p)\n", This
, p
);
160 nsAString_Init(&src_str
, NULL
);
161 nsres
= nsIDOMHTMLScriptElement_GetSrc(This
->nsscript
, &src_str
);
162 return return_nsstr(nsres
, &src_str
, p
);
165 static HRESULT WINAPI
HTMLScriptElement_put_htmlFor(IHTMLScriptElement
*iface
, BSTR v
)
167 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
168 FIXME("(%p)->(%s)\n", This
, debugstr_w(v
));
172 static HRESULT WINAPI
HTMLScriptElement_get_htmlFor(IHTMLScriptElement
*iface
, BSTR
*p
)
174 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
175 FIXME("(%p)->(%p)\n", This
, p
);
179 static HRESULT WINAPI
HTMLScriptElement_put_event(IHTMLScriptElement
*iface
, BSTR v
)
181 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
182 FIXME("(%p)->(%s)\n", This
, debugstr_w(v
));
186 static HRESULT WINAPI
HTMLScriptElement_get_event(IHTMLScriptElement
*iface
, BSTR
*p
)
188 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
189 FIXME("(%p)->(%p)\n", This
, p
);
193 static HRESULT WINAPI
HTMLScriptElement_put_text(IHTMLScriptElement
*iface
, BSTR v
)
195 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
196 HTMLInnerWindow
*window
;
201 TRACE("(%p)->(%s)\n", This
, debugstr_w(v
));
203 if(!This
->element
.node
.doc
|| !This
->element
.node
.doc
->window
) {
204 WARN("no windoow\n");
208 window
= This
->element
.node
.doc
->window
;
210 nsAString_InitDepend(&text_str
, v
);
211 nsres
= nsIDOMHTMLScriptElement_SetText(This
->nsscript
, &text_str
);
212 nsAString_Finish(&text_str
);
213 if(NS_FAILED(nsres
)) {
214 ERR("SetSrc failed: %08x\n", nsres
);
218 nsres
= nsIDOMHTMLElement_GetParentNode(This
->element
.nselem
, &parent
);
219 if(NS_FAILED(nsres
) || !parent
) {
220 TRACE("No parent, not executing\n");
221 This
->parse_on_bind
= TRUE
;
225 nsIDOMNode_Release(parent
);
226 doc_insert_script(window
, This
);
230 static HRESULT WINAPI
HTMLScriptElement_get_text(IHTMLScriptElement
*iface
, BSTR
*p
)
232 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
236 TRACE("(%p)->(%p)\n", This
, p
);
238 nsAString_Init(&nsstr
, NULL
);
239 nsres
= nsIDOMHTMLScriptElement_GetText(This
->nsscript
, &nsstr
);
240 return return_nsstr(nsres
, &nsstr
, p
);
243 static HRESULT WINAPI
HTMLScriptElement_put_defer(IHTMLScriptElement
*iface
, VARIANT_BOOL v
)
245 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
249 TRACE("(%p)->(%x)\n", This
, v
);
251 nsres
= nsIDOMHTMLScriptElement_SetDefer(This
->nsscript
, v
!= VARIANT_FALSE
);
260 static HRESULT WINAPI
HTMLScriptElement_get_defer(IHTMLScriptElement
*iface
, VARIANT_BOOL
*p
)
262 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
263 cpp_bool defer
= FALSE
;
266 TRACE("(%p)->(%p)\n", This
, p
);
271 nsres
= nsIDOMHTMLScriptElement_GetDefer(This
->nsscript
, &defer
);
272 if(NS_FAILED(nsres
)) {
273 ERR("GetSrc failed: %08x\n", nsres
);
276 *p
= defer
? VARIANT_TRUE
: VARIANT_FALSE
;
278 TRACE("*p = %d\n", *p
);
282 static HRESULT WINAPI
HTMLScriptElement_get_readyState(IHTMLScriptElement
*iface
, BSTR
*p
)
284 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
286 TRACE("(%p)->(%p)\n", This
, p
);
288 return get_readystate_string(This
->readystate
, p
);
291 static HRESULT WINAPI
HTMLScriptElement_put_onerror(IHTMLScriptElement
*iface
, VARIANT v
)
293 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
295 FIXME("(%p)->(%s) semi-stub\n", This
, debugstr_variant(&v
));
297 return set_node_event(&This
->element
.node
, EVENTID_ERROR
, &v
);
300 static HRESULT WINAPI
HTMLScriptElement_get_onerror(IHTMLScriptElement
*iface
, VARIANT
*p
)
302 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
304 TRACE("(%p)->(%p)\n", This
, p
);
306 return get_node_event(&This
->element
.node
, EVENTID_ERROR
, p
);
309 static HRESULT WINAPI
HTMLScriptElement_put_type(IHTMLScriptElement
*iface
, BSTR v
)
311 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
312 nsAString nstype_str
;
315 TRACE("(%p)->(%s)\n", This
, debugstr_w(v
));
317 nsAString_Init(&nstype_str
, v
);
318 nsres
= nsIDOMHTMLScriptElement_SetType(This
->nsscript
, &nstype_str
);
319 if (NS_FAILED(nsres
))
320 ERR("SetType failed: %08x\n", nsres
);
321 nsAString_Finish (&nstype_str
);
326 static HRESULT WINAPI
HTMLScriptElement_get_type(IHTMLScriptElement
*iface
, BSTR
*p
)
328 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
329 nsAString nstype_str
;
332 TRACE("(%p)->(%p)\n", This
, p
);
334 nsAString_Init(&nstype_str
, NULL
);
335 nsres
= nsIDOMHTMLScriptElement_GetType(This
->nsscript
, &nstype_str
);
336 return return_nsstr(nsres
, &nstype_str
, p
);
339 static const IHTMLScriptElementVtbl HTMLScriptElementVtbl
= {
340 HTMLScriptElement_QueryInterface
,
341 HTMLScriptElement_AddRef
,
342 HTMLScriptElement_Release
,
343 HTMLScriptElement_GetTypeInfoCount
,
344 HTMLScriptElement_GetTypeInfo
,
345 HTMLScriptElement_GetIDsOfNames
,
346 HTMLScriptElement_Invoke
,
347 HTMLScriptElement_put_src
,
348 HTMLScriptElement_get_src
,
349 HTMLScriptElement_put_htmlFor
,
350 HTMLScriptElement_get_htmlFor
,
351 HTMLScriptElement_put_event
,
352 HTMLScriptElement_get_event
,
353 HTMLScriptElement_put_text
,
354 HTMLScriptElement_get_text
,
355 HTMLScriptElement_put_defer
,
356 HTMLScriptElement_get_defer
,
357 HTMLScriptElement_get_readyState
,
358 HTMLScriptElement_put_onerror
,
359 HTMLScriptElement_get_onerror
,
360 HTMLScriptElement_put_type
,
361 HTMLScriptElement_get_type
364 static inline HTMLScriptElement
*impl_from_HTMLDOMNode(HTMLDOMNode
*iface
)
366 return CONTAINING_RECORD(iface
, HTMLScriptElement
, element
.node
);
369 static HRESULT
HTMLScriptElement_QI(HTMLDOMNode
*iface
, REFIID riid
, void **ppv
)
371 HTMLScriptElement
*This
= impl_from_HTMLDOMNode(iface
);
375 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
376 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
377 *ppv
= &This
->IHTMLScriptElement_iface
;
378 }else if(IsEqualGUID(&IID_IDispatch
, riid
)) {
379 TRACE("(%p)->(IID_IDispatch %p)\n", This
, ppv
);
380 *ppv
= &This
->IHTMLScriptElement_iface
;
381 }else if(IsEqualGUID(&IID_IHTMLScriptElement
, riid
)) {
382 TRACE("(%p)->(IID_IHTMLScriptElement %p)\n", This
, ppv
);
383 *ppv
= &This
->IHTMLScriptElement_iface
;
387 IUnknown_AddRef((IUnknown
*)*ppv
);
391 return HTMLElement_QI(&This
->element
.node
, riid
, ppv
);
394 static HRESULT
HTMLScriptElement_get_readystate(HTMLDOMNode
*iface
, BSTR
*p
)
396 HTMLScriptElement
*This
= impl_from_HTMLDOMNode(iface
);
398 return IHTMLScriptElement_get_readyState(&This
->IHTMLScriptElement_iface
, p
);
401 static void HTMLScriptElement_traverse(HTMLDOMNode
*iface
, nsCycleCollectionTraversalCallback
*cb
)
403 HTMLScriptElement
*This
= impl_from_HTMLDOMNode(iface
);
406 note_cc_edge((nsISupports
*)This
->nsscript
, "This->nsscript", cb
);
409 static void HTMLScriptElement_unlink(HTMLDOMNode
*iface
)
411 HTMLScriptElement
*This
= impl_from_HTMLDOMNode(iface
);
414 nsIDOMHTMLScriptElement
*nsscript
= This
->nsscript
;
416 This
->nsscript
= NULL
;
417 nsIDOMHTMLScriptElement_Release(nsscript
);
421 static const NodeImplVtbl HTMLScriptElementImplVtbl
= {
422 HTMLScriptElement_QI
,
423 HTMLElement_destructor
,
426 HTMLElement_handle_event
,
427 HTMLElement_get_attr_col
,
433 HTMLScriptElement_get_readystate
,
437 HTMLScriptElement_traverse
,
438 HTMLScriptElement_unlink
441 HRESULT
script_elem_from_nsscript(HTMLDocumentNode
*doc
, nsIDOMHTMLScriptElement
*nsscript
, HTMLScriptElement
**ret
)
448 nsres
= nsIDOMHTMLScriptElement_QueryInterface(nsscript
, &IID_nsIDOMNode
, (void**)&nsnode
);
449 assert(nsres
== NS_OK
);
451 hres
= get_node(doc
, nsnode
, TRUE
, &node
);
452 nsIDOMNode_Release(nsnode
);
456 assert(node
->vtbl
== &HTMLScriptElementImplVtbl
);
457 *ret
= impl_from_HTMLDOMNode(node
);
461 static const tid_t HTMLScriptElement_iface_tids
[] = {
463 IHTMLScriptElement_tid
,
467 static dispex_static_data_t HTMLScriptElement_dispex
= {
469 DispHTMLScriptElement_tid
,
471 HTMLScriptElement_iface_tids
474 HRESULT
HTMLScriptElement_Create(HTMLDocumentNode
*doc
, nsIDOMHTMLElement
*nselem
, HTMLElement
**elem
)
476 HTMLScriptElement
*ret
;
479 ret
= heap_alloc_zero(sizeof(HTMLScriptElement
));
481 return E_OUTOFMEMORY
;
483 ret
->IHTMLScriptElement_iface
.lpVtbl
= &HTMLScriptElementVtbl
;
484 ret
->element
.node
.vtbl
= &HTMLScriptElementImplVtbl
;
486 HTMLElement_Init(&ret
->element
, doc
, nselem
, &HTMLScriptElement_dispex
);
488 nsres
= nsIDOMHTMLElement_QueryInterface(nselem
, &IID_nsIDOMHTMLScriptElement
, (void**)&ret
->nsscript
);
489 assert(nsres
== NS_OK
);
491 *elem
= &ret
->element
;