1 /*
2 * Copyright 2008 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 "mshtml_private.h"
20
impl_from_IHTMLScriptElement(IHTMLScriptElement * iface)21 static inline HTMLScriptElement *impl_from_IHTMLScriptElement(IHTMLScriptElement *iface)
22 {
23 return CONTAINING_RECORD(iface, HTMLScriptElement, IHTMLScriptElement_iface);
24 }
25
HTMLScriptElement_QueryInterface(IHTMLScriptElement * iface,REFIID riid,void ** ppv)26 static HRESULT WINAPI HTMLScriptElement_QueryInterface(IHTMLScriptElement *iface,
27 REFIID riid, void **ppv)
28 {
29 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
30
31 return IHTMLDOMNode_QueryInterface(&This->element.node.IHTMLDOMNode_iface, riid, ppv);
32 }
33
HTMLScriptElement_AddRef(IHTMLScriptElement * iface)34 static ULONG WINAPI HTMLScriptElement_AddRef(IHTMLScriptElement *iface)
35 {
36 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
37
38 return IHTMLDOMNode_AddRef(&This->element.node.IHTMLDOMNode_iface);
39 }
40
HTMLScriptElement_Release(IHTMLScriptElement * iface)41 static ULONG WINAPI HTMLScriptElement_Release(IHTMLScriptElement *iface)
42 {
43 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
44
45 return IHTMLDOMNode_Release(&This->element.node.IHTMLDOMNode_iface);
46 }
47
HTMLScriptElement_GetTypeInfoCount(IHTMLScriptElement * iface,UINT * pctinfo)48 static HRESULT WINAPI HTMLScriptElement_GetTypeInfoCount(IHTMLScriptElement *iface, UINT *pctinfo)
49 {
50 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
51 return IDispatchEx_GetTypeInfoCount(&This->element.node.event_target.dispex.IDispatchEx_iface, pctinfo);
52 }
53
HTMLScriptElement_GetTypeInfo(IHTMLScriptElement * iface,UINT iTInfo,LCID lcid,ITypeInfo ** ppTInfo)54 static HRESULT WINAPI HTMLScriptElement_GetTypeInfo(IHTMLScriptElement *iface, UINT iTInfo,
55 LCID lcid, ITypeInfo **ppTInfo)
56 {
57 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
58 return IDispatchEx_GetTypeInfo(&This->element.node.event_target.dispex.IDispatchEx_iface, iTInfo, lcid,
59 ppTInfo);
60 }
61
HTMLScriptElement_GetIDsOfNames(IHTMLScriptElement * iface,REFIID riid,LPOLESTR * rgszNames,UINT cNames,LCID lcid,DISPID * rgDispId)62 static HRESULT WINAPI HTMLScriptElement_GetIDsOfNames(IHTMLScriptElement *iface, REFIID riid,
63 LPOLESTR *rgszNames, UINT cNames,
64 LCID lcid, DISPID *rgDispId)
65 {
66 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
67 return IDispatchEx_GetIDsOfNames(&This->element.node.event_target.dispex.IDispatchEx_iface, riid, rgszNames,
68 cNames, lcid, rgDispId);
69 }
70
HTMLScriptElement_Invoke(IHTMLScriptElement * iface,DISPID dispIdMember,REFIID riid,LCID lcid,WORD wFlags,DISPPARAMS * pDispParams,VARIANT * pVarResult,EXCEPINFO * pExcepInfo,UINT * puArgErr)71 static HRESULT WINAPI HTMLScriptElement_Invoke(IHTMLScriptElement *iface, DISPID dispIdMember,
72 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
73 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
74 {
75 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
76 return IDispatchEx_Invoke(&This->element.node.event_target.dispex.IDispatchEx_iface, dispIdMember, riid,
77 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
78 }
79
HTMLScriptElement_put_src(IHTMLScriptElement * iface,BSTR v)80 static HRESULT WINAPI HTMLScriptElement_put_src(IHTMLScriptElement *iface, BSTR v)
81 {
82 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
83 HTMLInnerWindow *window;
84 nsIDOMNode *parent;
85 nsAString src_str;
86 nsresult nsres;
87
88 TRACE("(%p)->(%s)\n", This, debugstr_w(v));
89
90 if(!This->element.node.doc || !This->element.node.doc->window) {
91 WARN("no windoow\n");
92 return E_UNEXPECTED;
93 }
94
95 window = This->element.node.doc->window;
96
97 nsAString_InitDepend(&src_str, v);
98 nsres = nsIDOMHTMLScriptElement_SetSrc(This->nsscript, &src_str);
99 nsAString_Finish(&src_str);
100 if(NS_FAILED(nsres)) {
101 ERR("SetSrc failed: %08x\n", nsres);
102 return E_FAIL;
103 }
104
105 if(This->parsed) {
106 WARN("already parsed\n");
107 return S_OK;
108 }
109
110 if(window->parser_callback_cnt) {
111 script_queue_entry_t *queue;
112
113 queue = heap_alloc(sizeof(*queue));
114 if(!queue)
115 return E_OUTOFMEMORY;
116
117 IHTMLScriptElement_AddRef(&This->IHTMLScriptElement_iface);
118 queue->script = This;
119
120 list_add_tail(&window->script_queue, &queue->entry);
121 return S_OK;
122 }
123
124 nsres = nsIDOMHTMLElement_GetParentNode(This->element.nselem, &parent);
125 if(NS_FAILED(nsres) || !parent) {
126 TRACE("No parent, not executing\n");
127 This->parse_on_bind = TRUE;
128 return S_OK;
129 }
130
131 nsIDOMNode_Release(parent);
132 doc_insert_script(window, This);
133 return S_OK;
134 }
135
HTMLScriptElement_get_src(IHTMLScriptElement * iface,BSTR * p)136 static HRESULT WINAPI HTMLScriptElement_get_src(IHTMLScriptElement *iface, BSTR *p)
137 {
138 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
139 nsAString src_str;
140 nsresult nsres;
141
142 TRACE("(%p)->(%p)\n", This, p);
143
144 nsAString_Init(&src_str, NULL);
145 nsres = nsIDOMHTMLScriptElement_GetSrc(This->nsscript, &src_str);
146 return return_nsstr(nsres, &src_str, p);
147 }
148
HTMLScriptElement_put_htmlFor(IHTMLScriptElement * iface,BSTR v)149 static HRESULT WINAPI HTMLScriptElement_put_htmlFor(IHTMLScriptElement *iface, BSTR v)
150 {
151 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
152 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
153 return E_NOTIMPL;
154 }
155
HTMLScriptElement_get_htmlFor(IHTMLScriptElement * iface,BSTR * p)156 static HRESULT WINAPI HTMLScriptElement_get_htmlFor(IHTMLScriptElement *iface, BSTR *p)
157 {
158 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
159 FIXME("(%p)->(%p)\n", This, p);
160 return E_NOTIMPL;
161 }
162
HTMLScriptElement_put_event(IHTMLScriptElement * iface,BSTR v)163 static HRESULT WINAPI HTMLScriptElement_put_event(IHTMLScriptElement *iface, BSTR v)
164 {
165 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
166 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
167 return E_NOTIMPL;
168 }
169
HTMLScriptElement_get_event(IHTMLScriptElement * iface,BSTR * p)170 static HRESULT WINAPI HTMLScriptElement_get_event(IHTMLScriptElement *iface, BSTR *p)
171 {
172 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
173 FIXME("(%p)->(%p)\n", This, p);
174 return E_NOTIMPL;
175 }
176
HTMLScriptElement_put_text(IHTMLScriptElement * iface,BSTR v)177 static HRESULT WINAPI HTMLScriptElement_put_text(IHTMLScriptElement *iface, BSTR v)
178 {
179 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
180 HTMLInnerWindow *window;
181 nsIDOMNode *parent;
182 nsAString text_str;
183 nsresult nsres;
184
185 TRACE("(%p)->(%s)\n", This, debugstr_w(v));
186
187 if(!This->element.node.doc || !This->element.node.doc->window) {
188 WARN("no windoow\n");
189 return E_UNEXPECTED;
190 }
191
192 window = This->element.node.doc->window;
193
194 nsAString_InitDepend(&text_str, v);
195 nsres = nsIDOMHTMLScriptElement_SetText(This->nsscript, &text_str);
196 nsAString_Finish(&text_str);
197 if(NS_FAILED(nsres)) {
198 ERR("SetSrc failed: %08x\n", nsres);
199 return E_FAIL;
200 }
201
202 nsres = nsIDOMHTMLElement_GetParentNode(This->element.nselem, &parent);
203 if(NS_FAILED(nsres) || !parent) {
204 TRACE("No parent, not executing\n");
205 This->parse_on_bind = TRUE;
206 return S_OK;
207 }
208
209 nsIDOMNode_Release(parent);
210 doc_insert_script(window, This);
211 return S_OK;
212 }
213
HTMLScriptElement_get_text(IHTMLScriptElement * iface,BSTR * p)214 static HRESULT WINAPI HTMLScriptElement_get_text(IHTMLScriptElement *iface, BSTR *p)
215 {
216 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
217 nsAString nsstr;
218 nsresult nsres;
219
220 TRACE("(%p)->(%p)\n", This, p);
221
222 nsAString_Init(&nsstr, NULL);
223 nsres = nsIDOMHTMLScriptElement_GetText(This->nsscript, &nsstr);
224 return return_nsstr(nsres, &nsstr, p);
225 }
226
HTMLScriptElement_put_defer(IHTMLScriptElement * iface,VARIANT_BOOL v)227 static HRESULT WINAPI HTMLScriptElement_put_defer(IHTMLScriptElement *iface, VARIANT_BOOL v)
228 {
229 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
230 HRESULT hr = S_OK;
231 nsresult nsres;
232
233 TRACE("(%p)->(%x)\n", This, v);
234
235 nsres = nsIDOMHTMLScriptElement_SetDefer(This->nsscript, v != VARIANT_FALSE);
236 if(NS_FAILED(nsres))
237 {
238 hr = E_FAIL;
239 }
240
241 return hr;
242 }
243
HTMLScriptElement_get_defer(IHTMLScriptElement * iface,VARIANT_BOOL * p)244 static HRESULT WINAPI HTMLScriptElement_get_defer(IHTMLScriptElement *iface, VARIANT_BOOL *p)
245 {
246 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
247 cpp_bool defer = FALSE;
248 nsresult nsres;
249
250 TRACE("(%p)->(%p)\n", This, p);
251
252 if(!p)
253 return E_INVALIDARG;
254
255 nsres = nsIDOMHTMLScriptElement_GetDefer(This->nsscript, &defer);
256 if(NS_FAILED(nsres)) {
257 ERR("GetSrc failed: %08x\n", nsres);
258 }
259
260 *p = defer ? VARIANT_TRUE : VARIANT_FALSE;
261
262 TRACE("*p = %d\n", *p);
263 return S_OK;
264 }
265
HTMLScriptElement_get_readyState(IHTMLScriptElement * iface,BSTR * p)266 static HRESULT WINAPI HTMLScriptElement_get_readyState(IHTMLScriptElement *iface, BSTR *p)
267 {
268 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
269
270 TRACE("(%p)->(%p)\n", This, p);
271
272 return get_readystate_string(This->readystate, p);
273 }
274
HTMLScriptElement_put_onerror(IHTMLScriptElement * iface,VARIANT v)275 static HRESULT WINAPI HTMLScriptElement_put_onerror(IHTMLScriptElement *iface, VARIANT v)
276 {
277 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
278
279 FIXME("(%p)->(%s) semi-stub\n", This, debugstr_variant(&v));
280
281 return set_node_event(&This->element.node, EVENTID_ERROR, &v);
282 }
283
HTMLScriptElement_get_onerror(IHTMLScriptElement * iface,VARIANT * p)284 static HRESULT WINAPI HTMLScriptElement_get_onerror(IHTMLScriptElement *iface, VARIANT *p)
285 {
286 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
287
288 TRACE("(%p)->(%p)\n", This, p);
289
290 return get_node_event(&This->element.node, EVENTID_ERROR, p);
291 }
292
HTMLScriptElement_put_type(IHTMLScriptElement * iface,BSTR v)293 static HRESULT WINAPI HTMLScriptElement_put_type(IHTMLScriptElement *iface, BSTR v)
294 {
295 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
296 nsAString nstype_str;
297 nsresult nsres;
298
299 TRACE("(%p)->(%s)\n", This, debugstr_w(v));
300
301 nsAString_Init(&nstype_str, v);
302 nsres = nsIDOMHTMLScriptElement_SetType(This->nsscript, &nstype_str);
303 if (NS_FAILED(nsres))
304 ERR("SetType failed: %08x\n", nsres);
305 nsAString_Finish (&nstype_str);
306
307 return S_OK;
308 }
309
HTMLScriptElement_get_type(IHTMLScriptElement * iface,BSTR * p)310 static HRESULT WINAPI HTMLScriptElement_get_type(IHTMLScriptElement *iface, BSTR *p)
311 {
312 HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
313 nsAString nstype_str;
314 nsresult nsres;
315
316 TRACE("(%p)->(%p)\n", This, p);
317
318 nsAString_Init(&nstype_str, NULL);
319 nsres = nsIDOMHTMLScriptElement_GetType(This->nsscript, &nstype_str);
320 return return_nsstr(nsres, &nstype_str, p);
321 }
322
323 static const IHTMLScriptElementVtbl HTMLScriptElementVtbl = {
324 HTMLScriptElement_QueryInterface,
325 HTMLScriptElement_AddRef,
326 HTMLScriptElement_Release,
327 HTMLScriptElement_GetTypeInfoCount,
328 HTMLScriptElement_GetTypeInfo,
329 HTMLScriptElement_GetIDsOfNames,
330 HTMLScriptElement_Invoke,
331 HTMLScriptElement_put_src,
332 HTMLScriptElement_get_src,
333 HTMLScriptElement_put_htmlFor,
334 HTMLScriptElement_get_htmlFor,
335 HTMLScriptElement_put_event,
336 HTMLScriptElement_get_event,
337 HTMLScriptElement_put_text,
338 HTMLScriptElement_get_text,
339 HTMLScriptElement_put_defer,
340 HTMLScriptElement_get_defer,
341 HTMLScriptElement_get_readyState,
342 HTMLScriptElement_put_onerror,
343 HTMLScriptElement_get_onerror,
344 HTMLScriptElement_put_type,
345 HTMLScriptElement_get_type
346 };
347
impl_from_HTMLDOMNode(HTMLDOMNode * iface)348 static inline HTMLScriptElement *impl_from_HTMLDOMNode(HTMLDOMNode *iface)
349 {
350 return CONTAINING_RECORD(iface, HTMLScriptElement, element.node);
351 }
352
HTMLScriptElement_QI(HTMLDOMNode * iface,REFIID riid,void ** ppv)353 static HRESULT HTMLScriptElement_QI(HTMLDOMNode *iface, REFIID riid, void **ppv)
354 {
355 HTMLScriptElement *This = impl_from_HTMLDOMNode(iface);
356
357 *ppv = NULL;
358
359 if(IsEqualGUID(&IID_IUnknown, riid)) {
360 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
361 *ppv = &This->IHTMLScriptElement_iface;
362 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
363 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
364 *ppv = &This->IHTMLScriptElement_iface;
365 }else if(IsEqualGUID(&IID_IHTMLScriptElement, riid)) {
366 TRACE("(%p)->(IID_IHTMLScriptElement %p)\n", This, ppv);
367 *ppv = &This->IHTMLScriptElement_iface;
368 }
369
370 if(*ppv) {
371 IUnknown_AddRef((IUnknown*)*ppv);
372 return S_OK;
373 }
374
375 return HTMLElement_QI(&This->element.node, riid, ppv);
376 }
377
HTMLScriptElement_get_readystate(HTMLDOMNode * iface,BSTR * p)378 static HRESULT HTMLScriptElement_get_readystate(HTMLDOMNode *iface, BSTR *p)
379 {
380 HTMLScriptElement *This = impl_from_HTMLDOMNode(iface);
381
382 return IHTMLScriptElement_get_readyState(&This->IHTMLScriptElement_iface, p);
383 }
384
HTMLScriptElement_traverse(HTMLDOMNode * iface,nsCycleCollectionTraversalCallback * cb)385 static void HTMLScriptElement_traverse(HTMLDOMNode *iface, nsCycleCollectionTraversalCallback *cb)
386 {
387 HTMLScriptElement *This = impl_from_HTMLDOMNode(iface);
388
389 if(This->nsscript)
390 note_cc_edge((nsISupports*)This->nsscript, "This->nsscript", cb);
391 }
392
HTMLScriptElement_unlink(HTMLDOMNode * iface)393 static void HTMLScriptElement_unlink(HTMLDOMNode *iface)
394 {
395 HTMLScriptElement *This = impl_from_HTMLDOMNode(iface);
396
397 if(This->nsscript) {
398 nsIDOMHTMLScriptElement *nsscript = This->nsscript;
399
400 This->nsscript = NULL;
401 nsIDOMHTMLScriptElement_Release(nsscript);
402 }
403 }
404
405 static const NodeImplVtbl HTMLScriptElementImplVtbl = {
406 HTMLScriptElement_QI,
407 HTMLElement_destructor,
408 HTMLElement_cpc,
409 HTMLElement_clone,
410 HTMLElement_handle_event,
411 HTMLElement_get_attr_col,
412 NULL,
413 NULL,
414 NULL,
415 NULL,
416 NULL,
417 HTMLScriptElement_get_readystate,
418 NULL,
419 NULL,
420 NULL,
421 HTMLScriptElement_traverse,
422 HTMLScriptElement_unlink
423 };
424
script_elem_from_nsscript(HTMLDocumentNode * doc,nsIDOMHTMLScriptElement * nsscript,HTMLScriptElement ** ret)425 HRESULT script_elem_from_nsscript(HTMLDocumentNode *doc, nsIDOMHTMLScriptElement *nsscript, HTMLScriptElement **ret)
426 {
427 nsIDOMNode *nsnode;
428 HTMLDOMNode *node;
429 nsresult nsres;
430 HRESULT hres;
431
432 nsres = nsIDOMHTMLScriptElement_QueryInterface(nsscript, &IID_nsIDOMNode, (void**)&nsnode);
433 assert(nsres == NS_OK);
434
435 hres = get_node(doc, nsnode, TRUE, &node);
436 nsIDOMNode_Release(nsnode);
437 if(FAILED(hres))
438 return hres;
439
440 assert(node->vtbl == &HTMLScriptElementImplVtbl);
441 *ret = impl_from_HTMLDOMNode(node);
442 return S_OK;
443 }
444
445 static const tid_t HTMLScriptElement_iface_tids[] = {
446 HTMLELEMENT_TIDS,
447 IHTMLScriptElement_tid,
448 0
449 };
450
451 static dispex_static_data_t HTMLScriptElement_dispex = {
452 NULL,
453 DispHTMLScriptElement_tid,
454 NULL,
455 HTMLScriptElement_iface_tids
456 };
457
HTMLScriptElement_Create(HTMLDocumentNode * doc,nsIDOMHTMLElement * nselem,HTMLElement ** elem)458 HRESULT HTMLScriptElement_Create(HTMLDocumentNode *doc, nsIDOMHTMLElement *nselem, HTMLElement **elem)
459 {
460 HTMLScriptElement *ret;
461 nsresult nsres;
462
463 ret = heap_alloc_zero(sizeof(HTMLScriptElement));
464 if(!ret)
465 return E_OUTOFMEMORY;
466
467 ret->IHTMLScriptElement_iface.lpVtbl = &HTMLScriptElementVtbl;
468 ret->element.node.vtbl = &HTMLScriptElementImplVtbl;
469
470 HTMLElement_Init(&ret->element, doc, nselem, &HTMLScriptElement_dispex);
471
472 nsres = nsIDOMHTMLElement_QueryInterface(nselem, &IID_nsIDOMHTMLScriptElement, (void**)&ret->nsscript);
473 assert(nsres == NS_OK);
474
475 *elem = &ret->element;
476 return S_OK;
477 }
478