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
);
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
);
114 WARN("already parsed\n");
119 FIXME("binding in progress\n");
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
);
130 ERR("SetSrc failed: %08x\n", nsres
);
133 nsAString_Finish(&src_str
);
137 static HRESULT WINAPI
HTMLScriptElement_get_src(IHTMLScriptElement
*iface
, BSTR
*p
)
139 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
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
));
157 static HRESULT WINAPI
HTMLScriptElement_get_htmlFor(IHTMLScriptElement
*iface
, BSTR
*p
)
159 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
163 TRACE("(%p)->(%p)\n", This
, p
);
165 nsAString_Init(&html_str
, NULL
);
166 nsres
= nsIDOMHTMLScriptElement_GetHtmlFor(This
->nsscript
, &html_str
);
167 return return_nsstr(nsres
, &html_str
, p
);
170 static HRESULT WINAPI
HTMLScriptElement_put_event(IHTMLScriptElement
*iface
, BSTR v
)
172 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
173 FIXME("(%p)->(%s)\n", This
, debugstr_w(v
));
177 static HRESULT WINAPI
HTMLScriptElement_get_event(IHTMLScriptElement
*iface
, BSTR
*p
)
179 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
180 FIXME("(%p)->(%p)\n", This
, p
);
184 static HRESULT WINAPI
HTMLScriptElement_put_text(IHTMLScriptElement
*iface
, BSTR v
)
186 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
187 HTMLInnerWindow
*window
;
192 TRACE("(%p)->(%s)\n", This
, debugstr_w(v
));
194 if(!This
->element
.node
.doc
|| !This
->element
.node
.doc
->window
) {
195 WARN("no windoow\n");
199 window
= This
->element
.node
.doc
->window
;
201 nsAString_InitDepend(&text_str
, v
);
202 nsres
= nsIDOMHTMLScriptElement_SetText(This
->nsscript
, &text_str
);
203 nsAString_Finish(&text_str
);
204 if(NS_FAILED(nsres
)) {
205 ERR("SetSrc failed: %08x\n", nsres
);
209 nsres
= nsIDOMElement_GetParentNode(This
->element
.dom_element
, &parent
);
210 if(NS_FAILED(nsres
) || !parent
) {
211 TRACE("No parent, not executing\n");
212 This
->parse_on_bind
= TRUE
;
216 nsIDOMNode_Release(parent
);
217 doc_insert_script(window
, This
, FALSE
);
221 static HRESULT WINAPI
HTMLScriptElement_get_text(IHTMLScriptElement
*iface
, BSTR
*p
)
223 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
227 TRACE("(%p)->(%p)\n", This
, p
);
229 nsAString_Init(&nsstr
, NULL
);
230 nsres
= nsIDOMHTMLScriptElement_GetText(This
->nsscript
, &nsstr
);
231 return return_nsstr(nsres
, &nsstr
, p
);
234 static HRESULT WINAPI
HTMLScriptElement_put_defer(IHTMLScriptElement
*iface
, VARIANT_BOOL v
)
236 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
240 TRACE("(%p)->(%x)\n", This
, v
);
242 nsres
= nsIDOMHTMLScriptElement_SetDefer(This
->nsscript
, v
!= VARIANT_FALSE
);
251 static HRESULT WINAPI
HTMLScriptElement_get_defer(IHTMLScriptElement
*iface
, VARIANT_BOOL
*p
)
253 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
254 cpp_bool defer
= FALSE
;
257 TRACE("(%p)->(%p)\n", This
, p
);
262 nsres
= nsIDOMHTMLScriptElement_GetDefer(This
->nsscript
, &defer
);
263 if(NS_FAILED(nsres
)) {
264 ERR("GetSrc failed: %08x\n", nsres
);
267 *p
= variant_bool(defer
);
271 static HRESULT WINAPI
HTMLScriptElement_get_readyState(IHTMLScriptElement
*iface
, BSTR
*p
)
273 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
275 TRACE("(%p)->(%p)\n", This
, p
);
277 return get_readystate_string(This
->readystate
, p
);
280 static HRESULT WINAPI
HTMLScriptElement_put_onerror(IHTMLScriptElement
*iface
, VARIANT v
)
282 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
284 FIXME("(%p)->(%s) semi-stub\n", This
, debugstr_variant(&v
));
286 return set_node_event(&This
->element
.node
, EVENTID_ERROR
, &v
);
289 static HRESULT WINAPI
HTMLScriptElement_get_onerror(IHTMLScriptElement
*iface
, VARIANT
*p
)
291 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
293 TRACE("(%p)->(%p)\n", This
, p
);
295 return get_node_event(&This
->element
.node
, EVENTID_ERROR
, p
);
298 static HRESULT WINAPI
HTMLScriptElement_put_type(IHTMLScriptElement
*iface
, BSTR v
)
300 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
301 nsAString nstype_str
;
304 TRACE("(%p)->(%s)\n", This
, debugstr_w(v
));
306 nsAString_Init(&nstype_str
, v
);
307 nsres
= nsIDOMHTMLScriptElement_SetType(This
->nsscript
, &nstype_str
);
308 if (NS_FAILED(nsres
))
309 ERR("SetType failed: %08x\n", nsres
);
310 nsAString_Finish (&nstype_str
);
315 static HRESULT WINAPI
HTMLScriptElement_get_type(IHTMLScriptElement
*iface
, BSTR
*p
)
317 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
318 nsAString nstype_str
;
321 TRACE("(%p)->(%p)\n", This
, p
);
323 nsAString_Init(&nstype_str
, NULL
);
324 nsres
= nsIDOMHTMLScriptElement_GetType(This
->nsscript
, &nstype_str
);
325 return return_nsstr(nsres
, &nstype_str
, p
);
328 static const IHTMLScriptElementVtbl HTMLScriptElementVtbl
= {
329 HTMLScriptElement_QueryInterface
,
330 HTMLScriptElement_AddRef
,
331 HTMLScriptElement_Release
,
332 HTMLScriptElement_GetTypeInfoCount
,
333 HTMLScriptElement_GetTypeInfo
,
334 HTMLScriptElement_GetIDsOfNames
,
335 HTMLScriptElement_Invoke
,
336 HTMLScriptElement_put_src
,
337 HTMLScriptElement_get_src
,
338 HTMLScriptElement_put_htmlFor
,
339 HTMLScriptElement_get_htmlFor
,
340 HTMLScriptElement_put_event
,
341 HTMLScriptElement_get_event
,
342 HTMLScriptElement_put_text
,
343 HTMLScriptElement_get_text
,
344 HTMLScriptElement_put_defer
,
345 HTMLScriptElement_get_defer
,
346 HTMLScriptElement_get_readyState
,
347 HTMLScriptElement_put_onerror
,
348 HTMLScriptElement_get_onerror
,
349 HTMLScriptElement_put_type
,
350 HTMLScriptElement_get_type
353 static inline HTMLScriptElement
*impl_from_HTMLDOMNode(HTMLDOMNode
*iface
)
355 return CONTAINING_RECORD(iface
, HTMLScriptElement
, element
.node
);
358 static HRESULT
HTMLScriptElement_QI(HTMLDOMNode
*iface
, REFIID riid
, void **ppv
)
360 HTMLScriptElement
*This
= impl_from_HTMLDOMNode(iface
);
364 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
365 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
366 *ppv
= &This
->IHTMLScriptElement_iface
;
367 }else if(IsEqualGUID(&IID_IDispatch
, riid
)) {
368 TRACE("(%p)->(IID_IDispatch %p)\n", This
, ppv
);
369 *ppv
= &This
->IHTMLScriptElement_iface
;
370 }else if(IsEqualGUID(&IID_IHTMLScriptElement
, riid
)) {
371 TRACE("(%p)->(IID_IHTMLScriptElement %p)\n", This
, ppv
);
372 *ppv
= &This
->IHTMLScriptElement_iface
;
376 IUnknown_AddRef((IUnknown
*)*ppv
);
380 return HTMLElement_QI(&This
->element
.node
, riid
, ppv
);
383 static void HTMLScriptElement_destructor(HTMLDOMNode
*iface
)
385 HTMLScriptElement
*This
= impl_from_HTMLDOMNode(iface
);
386 heap_free(This
->src_text
);
387 HTMLElement_destructor(&This
->element
.node
);
390 static HRESULT
HTMLScriptElement_get_readystate(HTMLDOMNode
*iface
, BSTR
*p
)
392 HTMLScriptElement
*This
= impl_from_HTMLDOMNode(iface
);
394 return IHTMLScriptElement_get_readyState(&This
->IHTMLScriptElement_iface
, p
);
397 static HRESULT
HTMLScriptElement_bind_to_tree(HTMLDOMNode
*iface
)
399 HTMLScriptElement
*This
= impl_from_HTMLDOMNode(iface
);
401 TRACE("(%p)\n", This
);
403 if(!This
->parse_on_bind
)
406 if(!This
->element
.node
.doc
|| !This
->element
.node
.doc
->window
) {
411 This
->parse_on_bind
= FALSE
;
412 doc_insert_script(This
->element
.node
.doc
->window
, This
, FALSE
);
416 static void HTMLScriptElement_traverse(HTMLDOMNode
*iface
, nsCycleCollectionTraversalCallback
*cb
)
418 HTMLScriptElement
*This
= impl_from_HTMLDOMNode(iface
);
421 note_cc_edge((nsISupports
*)This
->nsscript
, "This->nsscript", cb
);
424 static void HTMLScriptElement_unlink(HTMLDOMNode
*iface
)
426 HTMLScriptElement
*This
= impl_from_HTMLDOMNode(iface
);
429 nsIDOMHTMLScriptElement
*nsscript
= This
->nsscript
;
431 This
->nsscript
= NULL
;
432 nsIDOMHTMLScriptElement_Release(nsscript
);
436 static const NodeImplVtbl HTMLScriptElementImplVtbl
= {
437 &CLSID_HTMLScriptElement
,
438 HTMLScriptElement_QI
,
439 HTMLScriptElement_destructor
,
442 HTMLElement_handle_event
,
443 HTMLElement_get_attr_col
,
448 HTMLScriptElement_get_readystate
,
451 HTMLScriptElement_bind_to_tree
,
452 HTMLScriptElement_traverse
,
453 HTMLScriptElement_unlink
456 HRESULT
script_elem_from_nsscript(nsIDOMHTMLScriptElement
*nsscript
, HTMLScriptElement
**ret
)
463 nsres
= nsIDOMHTMLScriptElement_QueryInterface(nsscript
, &IID_nsIDOMNode
, (void**)&nsnode
);
464 assert(nsres
== NS_OK
);
466 hres
= get_node(nsnode
, TRUE
, &node
);
467 nsIDOMNode_Release(nsnode
);
471 assert(node
->vtbl
== &HTMLScriptElementImplVtbl
);
472 *ret
= impl_from_HTMLDOMNode(node
);
476 static const tid_t HTMLScriptElement_iface_tids
[] = {
478 IHTMLScriptElement_tid
,
482 static dispex_static_data_t HTMLScriptElement_dispex
= {
484 DispHTMLScriptElement_tid
,
485 HTMLScriptElement_iface_tids
,
486 HTMLElement_init_dispex_info
489 HRESULT
HTMLScriptElement_Create(HTMLDocumentNode
*doc
, nsIDOMElement
*nselem
, HTMLElement
**elem
)
491 HTMLScriptElement
*ret
;
494 ret
= heap_alloc_zero(sizeof(HTMLScriptElement
));
496 return E_OUTOFMEMORY
;
498 ret
->IHTMLScriptElement_iface
.lpVtbl
= &HTMLScriptElementVtbl
;
499 ret
->element
.node
.vtbl
= &HTMLScriptElementImplVtbl
;
501 HTMLElement_Init(&ret
->element
, doc
, nselem
, &HTMLScriptElement_dispex
);
503 nsres
= nsIDOMElement_QueryInterface(nselem
, &IID_nsIDOMHTMLScriptElement
, (void**)&ret
->nsscript
);
504 assert(nsres
== NS_OK
);
506 *elem
= &ret
->element
;