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
);
160 FIXME("(%p)->(%p)\n", This
, p
);
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
));
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
);
178 static HRESULT WINAPI
HTMLScriptElement_put_text(IHTMLScriptElement
*iface
, BSTR v
)
180 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
181 HTMLInnerWindow
*window
;
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");
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
);
203 nsres
= nsIDOMElement_GetParentNode(This
->element
.dom_element
, &parent
);
204 if(NS_FAILED(nsres
) || !parent
) {
205 TRACE("No parent, not executing\n");
206 This
->parse_on_bind
= TRUE
;
210 nsIDOMNode_Release(parent
);
211 doc_insert_script(window
, This
, FALSE
);
215 static HRESULT WINAPI
HTMLScriptElement_get_text(IHTMLScriptElement
*iface
, BSTR
*p
)
217 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
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
);
234 TRACE("(%p)->(%x)\n", This
, v
);
236 nsres
= nsIDOMHTMLScriptElement_SetDefer(This
->nsscript
, v
!= VARIANT_FALSE
);
245 static HRESULT WINAPI
HTMLScriptElement_get_defer(IHTMLScriptElement
*iface
, VARIANT_BOOL
*p
)
247 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
248 cpp_bool defer
= FALSE
;
251 TRACE("(%p)->(%p)\n", This
, p
);
256 nsres
= nsIDOMHTMLScriptElement_GetDefer(This
->nsscript
, &defer
);
257 if(NS_FAILED(nsres
)) {
258 ERR("GetSrc failed: %08x\n", nsres
);
261 *p
= variant_bool(defer
);
265 static HRESULT WINAPI
HTMLScriptElement_get_readyState(IHTMLScriptElement
*iface
, BSTR
*p
)
267 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
269 TRACE("(%p)->(%p)\n", This
, p
);
271 return get_readystate_string(This
->readystate
, p
);
274 static HRESULT WINAPI
HTMLScriptElement_put_onerror(IHTMLScriptElement
*iface
, VARIANT v
)
276 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
278 FIXME("(%p)->(%s) semi-stub\n", This
, debugstr_variant(&v
));
280 return set_node_event(&This
->element
.node
, EVENTID_ERROR
, &v
);
283 static HRESULT WINAPI
HTMLScriptElement_get_onerror(IHTMLScriptElement
*iface
, VARIANT
*p
)
285 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
287 TRACE("(%p)->(%p)\n", This
, p
);
289 return get_node_event(&This
->element
.node
, EVENTID_ERROR
, p
);
292 static HRESULT WINAPI
HTMLScriptElement_put_type(IHTMLScriptElement
*iface
, BSTR v
)
294 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
295 nsAString nstype_str
;
298 TRACE("(%p)->(%s)\n", This
, debugstr_w(v
));
300 nsAString_Init(&nstype_str
, v
);
301 nsres
= nsIDOMHTMLScriptElement_SetType(This
->nsscript
, &nstype_str
);
302 if (NS_FAILED(nsres
))
303 ERR("SetType failed: %08x\n", nsres
);
304 nsAString_Finish (&nstype_str
);
309 static HRESULT WINAPI
HTMLScriptElement_get_type(IHTMLScriptElement
*iface
, BSTR
*p
)
311 HTMLScriptElement
*This
= impl_from_IHTMLScriptElement(iface
);
312 nsAString nstype_str
;
315 TRACE("(%p)->(%p)\n", This
, p
);
317 nsAString_Init(&nstype_str
, NULL
);
318 nsres
= nsIDOMHTMLScriptElement_GetType(This
->nsscript
, &nstype_str
);
319 return return_nsstr(nsres
, &nstype_str
, p
);
322 static const IHTMLScriptElementVtbl HTMLScriptElementVtbl
= {
323 HTMLScriptElement_QueryInterface
,
324 HTMLScriptElement_AddRef
,
325 HTMLScriptElement_Release
,
326 HTMLScriptElement_GetTypeInfoCount
,
327 HTMLScriptElement_GetTypeInfo
,
328 HTMLScriptElement_GetIDsOfNames
,
329 HTMLScriptElement_Invoke
,
330 HTMLScriptElement_put_src
,
331 HTMLScriptElement_get_src
,
332 HTMLScriptElement_put_htmlFor
,
333 HTMLScriptElement_get_htmlFor
,
334 HTMLScriptElement_put_event
,
335 HTMLScriptElement_get_event
,
336 HTMLScriptElement_put_text
,
337 HTMLScriptElement_get_text
,
338 HTMLScriptElement_put_defer
,
339 HTMLScriptElement_get_defer
,
340 HTMLScriptElement_get_readyState
,
341 HTMLScriptElement_put_onerror
,
342 HTMLScriptElement_get_onerror
,
343 HTMLScriptElement_put_type
,
344 HTMLScriptElement_get_type
347 static inline HTMLScriptElement
*impl_from_HTMLDOMNode(HTMLDOMNode
*iface
)
349 return CONTAINING_RECORD(iface
, HTMLScriptElement
, element
.node
);
352 static HRESULT
HTMLScriptElement_QI(HTMLDOMNode
*iface
, REFIID riid
, void **ppv
)
354 HTMLScriptElement
*This
= impl_from_HTMLDOMNode(iface
);
358 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
359 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
360 *ppv
= &This
->IHTMLScriptElement_iface
;
361 }else if(IsEqualGUID(&IID_IDispatch
, riid
)) {
362 TRACE("(%p)->(IID_IDispatch %p)\n", This
, ppv
);
363 *ppv
= &This
->IHTMLScriptElement_iface
;
364 }else if(IsEqualGUID(&IID_IHTMLScriptElement
, riid
)) {
365 TRACE("(%p)->(IID_IHTMLScriptElement %p)\n", This
, ppv
);
366 *ppv
= &This
->IHTMLScriptElement_iface
;
370 IUnknown_AddRef((IUnknown
*)*ppv
);
374 return HTMLElement_QI(&This
->element
.node
, riid
, ppv
);
377 static void HTMLScriptElement_destructor(HTMLDOMNode
*iface
)
379 HTMLScriptElement
*This
= impl_from_HTMLDOMNode(iface
);
380 heap_free(This
->src_text
);
381 HTMLElement_destructor(&This
->element
.node
);
384 static HRESULT
HTMLScriptElement_get_readystate(HTMLDOMNode
*iface
, BSTR
*p
)
386 HTMLScriptElement
*This
= impl_from_HTMLDOMNode(iface
);
388 return IHTMLScriptElement_get_readyState(&This
->IHTMLScriptElement_iface
, p
);
391 static HRESULT
HTMLScriptElement_bind_to_tree(HTMLDOMNode
*iface
)
393 HTMLScriptElement
*This
= impl_from_HTMLDOMNode(iface
);
395 TRACE("(%p)\n", This
);
397 if(!This
->parse_on_bind
)
400 if(!This
->element
.node
.doc
|| !This
->element
.node
.doc
->window
) {
405 This
->parse_on_bind
= FALSE
;
406 doc_insert_script(This
->element
.node
.doc
->window
, This
, FALSE
);
410 static void HTMLScriptElement_traverse(HTMLDOMNode
*iface
, nsCycleCollectionTraversalCallback
*cb
)
412 HTMLScriptElement
*This
= impl_from_HTMLDOMNode(iface
);
415 note_cc_edge((nsISupports
*)This
->nsscript
, "This->nsscript", cb
);
418 static void HTMLScriptElement_unlink(HTMLDOMNode
*iface
)
420 HTMLScriptElement
*This
= impl_from_HTMLDOMNode(iface
);
423 nsIDOMHTMLScriptElement
*nsscript
= This
->nsscript
;
425 This
->nsscript
= NULL
;
426 nsIDOMHTMLScriptElement_Release(nsscript
);
430 static const NodeImplVtbl HTMLScriptElementImplVtbl
= {
431 &CLSID_HTMLScriptElement
,
432 HTMLScriptElement_QI
,
433 HTMLScriptElement_destructor
,
436 HTMLElement_handle_event
,
437 HTMLElement_get_attr_col
,
442 HTMLScriptElement_get_readystate
,
445 HTMLScriptElement_bind_to_tree
,
446 HTMLScriptElement_traverse
,
447 HTMLScriptElement_unlink
450 HRESULT
script_elem_from_nsscript(nsIDOMHTMLScriptElement
*nsscript
, HTMLScriptElement
**ret
)
457 nsres
= nsIDOMHTMLScriptElement_QueryInterface(nsscript
, &IID_nsIDOMNode
, (void**)&nsnode
);
458 assert(nsres
== NS_OK
);
460 hres
= get_node(nsnode
, TRUE
, &node
);
461 nsIDOMNode_Release(nsnode
);
465 assert(node
->vtbl
== &HTMLScriptElementImplVtbl
);
466 *ret
= impl_from_HTMLDOMNode(node
);
470 static const tid_t HTMLScriptElement_iface_tids
[] = {
472 IHTMLScriptElement_tid
,
476 static dispex_static_data_t HTMLScriptElement_dispex
= {
478 DispHTMLScriptElement_tid
,
479 HTMLScriptElement_iface_tids
,
480 HTMLElement_init_dispex_info
483 HRESULT
HTMLScriptElement_Create(HTMLDocumentNode
*doc
, nsIDOMElement
*nselem
, HTMLElement
**elem
)
485 HTMLScriptElement
*ret
;
488 ret
= heap_alloc_zero(sizeof(HTMLScriptElement
));
490 return E_OUTOFMEMORY
;
492 ret
->IHTMLScriptElement_iface
.lpVtbl
= &HTMLScriptElementVtbl
;
493 ret
->element
.node
.vtbl
= &HTMLScriptElementImplVtbl
;
495 HTMLElement_Init(&ret
->element
, doc
, nselem
, &HTMLScriptElement_dispex
);
497 nsres
= nsIDOMElement_QueryInterface(nselem
, &IID_nsIDOMHTMLScriptElement
, (void**)&ret
->nsscript
);
498 assert(nsres
== NS_OK
);
500 *elem
= &ret
->element
;