mshtml: Added noscript tag handling tests.
[wine] / dlls / mshtml / htmlscript.c
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 <stdarg.h>
20 #include <assert.h>
21
22 #define COBJMACROS
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "ole2.h"
28
29 #include "wine/debug.h"
30
31 #include "mshtml_private.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
34
35 static inline HTMLScriptElement *impl_from_IHTMLScriptElement(IHTMLScriptElement *iface)
36 {
37     return CONTAINING_RECORD(iface, HTMLScriptElement, IHTMLScriptElement_iface);
38 }
39
40 static HRESULT WINAPI HTMLScriptElement_QueryInterface(IHTMLScriptElement *iface,
41         REFIID riid, void **ppv)
42 {
43     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
44
45     return IHTMLDOMNode_QueryInterface(&This->element.node.IHTMLDOMNode_iface, riid, ppv);
46 }
47
48 static ULONG WINAPI HTMLScriptElement_AddRef(IHTMLScriptElement *iface)
49 {
50     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
51
52     return IHTMLDOMNode_AddRef(&This->element.node.IHTMLDOMNode_iface);
53 }
54
55 static ULONG WINAPI HTMLScriptElement_Release(IHTMLScriptElement *iface)
56 {
57     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
58
59     return IHTMLDOMNode_Release(&This->element.node.IHTMLDOMNode_iface);
60 }
61
62 static HRESULT WINAPI HTMLScriptElement_GetTypeInfoCount(IHTMLScriptElement *iface, UINT *pctinfo)
63 {
64     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
65     return IDispatchEx_GetTypeInfoCount(&This->element.node.dispex.IDispatchEx_iface, pctinfo);
66 }
67
68 static HRESULT WINAPI HTMLScriptElement_GetTypeInfo(IHTMLScriptElement *iface, UINT iTInfo,
69                                               LCID lcid, ITypeInfo **ppTInfo)
70 {
71     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
72     return IDispatchEx_GetTypeInfo(&This->element.node.dispex.IDispatchEx_iface, iTInfo, lcid,
73             ppTInfo);
74 }
75
76 static HRESULT WINAPI HTMLScriptElement_GetIDsOfNames(IHTMLScriptElement *iface, REFIID riid,
77                                                 LPOLESTR *rgszNames, UINT cNames,
78                                                 LCID lcid, DISPID *rgDispId)
79 {
80     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
81     return IDispatchEx_GetIDsOfNames(&This->element.node.dispex.IDispatchEx_iface, riid, rgszNames,
82             cNames, lcid, rgDispId);
83 }
84
85 static HRESULT WINAPI HTMLScriptElement_Invoke(IHTMLScriptElement *iface, DISPID dispIdMember,
86                             REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
87                             VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
88 {
89     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
90     return IDispatchEx_Invoke(&This->element.node.dispex.IDispatchEx_iface, dispIdMember, riid,
91             lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
92 }
93
94 static HRESULT WINAPI HTMLScriptElement_put_src(IHTMLScriptElement *iface, BSTR v)
95 {
96     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
97     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
98     return E_NOTIMPL;
99 }
100
101 static HRESULT WINAPI HTMLScriptElement_get_src(IHTMLScriptElement *iface, BSTR *p)
102 {
103     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
104     nsAString src_str;
105     nsresult nsres;
106
107     TRACE("(%p)->(%p)\n", This, p);
108
109     nsAString_Init(&src_str, NULL);
110     nsres = nsIDOMHTMLScriptElement_GetSrc(This->nsscript, &src_str);
111     return return_nsstr(nsres, &src_str, p);
112 }
113
114 static HRESULT WINAPI HTMLScriptElement_put_htmlFor(IHTMLScriptElement *iface, BSTR v)
115 {
116     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
117     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
118     return E_NOTIMPL;
119 }
120
121 static HRESULT WINAPI HTMLScriptElement_get_htmlFor(IHTMLScriptElement *iface, BSTR *p)
122 {
123     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
124     FIXME("(%p)->(%p)\n", This, p);
125     return E_NOTIMPL;
126 }
127
128 static HRESULT WINAPI HTMLScriptElement_put_event(IHTMLScriptElement *iface, BSTR v)
129 {
130     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
131     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
132     return E_NOTIMPL;
133 }
134
135 static HRESULT WINAPI HTMLScriptElement_get_event(IHTMLScriptElement *iface, BSTR *p)
136 {
137     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
138     FIXME("(%p)->(%p)\n", This, p);
139     return E_NOTIMPL;
140 }
141
142 static HRESULT WINAPI HTMLScriptElement_put_text(IHTMLScriptElement *iface, BSTR v)
143 {
144     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
145     FIXME("(%p)->(%s)\n", This, debugstr_w(v));
146     return E_NOTIMPL;
147 }
148
149 static HRESULT WINAPI HTMLScriptElement_get_text(IHTMLScriptElement *iface, BSTR *p)
150 {
151     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
152     FIXME("(%p)->(%p)\n", This, p);
153     return E_NOTIMPL;
154 }
155
156 static HRESULT WINAPI HTMLScriptElement_put_defer(IHTMLScriptElement *iface, VARIANT_BOOL v)
157 {
158     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
159     HRESULT hr = S_OK;
160     nsresult nsres;
161
162     TRACE("(%p)->(%x)\n", This, v);
163
164     nsres = nsIDOMHTMLScriptElement_SetDefer(This->nsscript, v != VARIANT_FALSE);
165     if(NS_FAILED(nsres))
166     {
167         hr = E_FAIL;
168     }
169
170     return hr;
171 }
172
173 static HRESULT WINAPI HTMLScriptElement_get_defer(IHTMLScriptElement *iface, VARIANT_BOOL *p)
174 {
175     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
176     cpp_bool defer = FALSE;
177     nsresult nsres;
178
179     TRACE("(%p)->(%p)\n", This, p);
180
181     if(!p)
182         return E_INVALIDARG;
183
184     nsres = nsIDOMHTMLScriptElement_GetDefer(This->nsscript, &defer);
185     if(NS_FAILED(nsres)) {
186         ERR("GetSrc failed: %08x\n", nsres);
187     }
188
189     *p = defer ? VARIANT_TRUE : VARIANT_FALSE;
190
191     TRACE("*p = %d\n", *p);
192     return S_OK;
193 }
194
195 static HRESULT WINAPI HTMLScriptElement_get_readyState(IHTMLScriptElement *iface, BSTR *p)
196 {
197     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
198     FIXME("(%p)->(%p)\n", This, p);
199     return E_NOTIMPL;
200 }
201
202 static HRESULT WINAPI HTMLScriptElement_put_onerror(IHTMLScriptElement *iface, VARIANT v)
203 {
204     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
205     FIXME("(%p)->(%s)\n", This, debugstr_variant(&v));
206     return E_NOTIMPL;
207 }
208
209 static HRESULT WINAPI HTMLScriptElement_get_onerror(IHTMLScriptElement *iface, VARIANT *p)
210 {
211     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
212     FIXME("(%p)->(%p)\n", This, p);
213     return E_NOTIMPL;
214 }
215
216 static HRESULT WINAPI HTMLScriptElement_put_type(IHTMLScriptElement *iface, BSTR v)
217 {
218     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
219     nsAString nstype_str;
220     nsresult nsres;
221
222     TRACE("(%p)->(%s)\n", This, debugstr_w(v));
223
224     nsAString_Init(&nstype_str, v);
225     nsres = nsIDOMHTMLScriptElement_SetType(This->nsscript, &nstype_str);
226     if (NS_FAILED(nsres))
227         ERR("SetType failed: %08x\n", nsres);
228     nsAString_Finish (&nstype_str);
229
230     return S_OK;
231 }
232
233 static HRESULT WINAPI HTMLScriptElement_get_type(IHTMLScriptElement *iface, BSTR *p)
234 {
235     HTMLScriptElement *This = impl_from_IHTMLScriptElement(iface);
236     nsAString nstype_str;
237     nsresult nsres;
238
239     TRACE("(%p)->(%p)\n", This, p);
240
241     nsAString_Init(&nstype_str, NULL);
242     nsres = nsIDOMHTMLScriptElement_GetType(This->nsscript, &nstype_str);
243     return return_nsstr(nsres, &nstype_str, p);
244 }
245
246 static const IHTMLScriptElementVtbl HTMLScriptElementVtbl = {
247     HTMLScriptElement_QueryInterface,
248     HTMLScriptElement_AddRef,
249     HTMLScriptElement_Release,
250     HTMLScriptElement_GetTypeInfoCount,
251     HTMLScriptElement_GetTypeInfo,
252     HTMLScriptElement_GetIDsOfNames,
253     HTMLScriptElement_Invoke,
254     HTMLScriptElement_put_src,
255     HTMLScriptElement_get_src,
256     HTMLScriptElement_put_htmlFor,
257     HTMLScriptElement_get_htmlFor,
258     HTMLScriptElement_put_event,
259     HTMLScriptElement_get_event,
260     HTMLScriptElement_put_text,
261     HTMLScriptElement_get_text,
262     HTMLScriptElement_put_defer,
263     HTMLScriptElement_get_defer,
264     HTMLScriptElement_get_readyState,
265     HTMLScriptElement_put_onerror,
266     HTMLScriptElement_get_onerror,
267     HTMLScriptElement_put_type,
268     HTMLScriptElement_get_type
269 };
270
271 static inline HTMLScriptElement *impl_from_HTMLDOMNode(HTMLDOMNode *iface)
272 {
273     return CONTAINING_RECORD(iface, HTMLScriptElement, element.node);
274 }
275
276 static HRESULT HTMLScriptElement_QI(HTMLDOMNode *iface, REFIID riid, void **ppv)
277 {
278     HTMLScriptElement *This = impl_from_HTMLDOMNode(iface);
279
280     *ppv = NULL;
281
282     if(IsEqualGUID(&IID_IUnknown, riid)) {
283         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
284         *ppv = &This->IHTMLScriptElement_iface;
285     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
286         TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
287         *ppv = &This->IHTMLScriptElement_iface;
288     }else if(IsEqualGUID(&IID_IHTMLScriptElement, riid)) {
289         TRACE("(%p)->(IID_IHTMLScriptElement %p)\n", This, ppv);
290         *ppv = &This->IHTMLScriptElement_iface;
291     }
292
293     if(*ppv) {
294         IUnknown_AddRef((IUnknown*)*ppv);
295         return S_OK;
296     }
297
298     return HTMLElement_QI(&This->element.node, riid, ppv);
299 }
300
301 static HRESULT HTMLScriptElement_get_readystate(HTMLDOMNode *iface, BSTR *p)
302 {
303     HTMLScriptElement *This = impl_from_HTMLDOMNode(iface);
304
305     return IHTMLScriptElement_get_readyState(&This->IHTMLScriptElement_iface, p);
306 }
307
308 static const NodeImplVtbl HTMLScriptElementImplVtbl = {
309     HTMLScriptElement_QI,
310     HTMLElement_destructor,
311     HTMLElement_clone,
312     HTMLElement_handle_event,
313     HTMLElement_get_attr_col,
314     NULL,
315     NULL,
316     NULL,
317     NULL,
318     NULL,
319     HTMLScriptElement_get_readystate
320 };
321
322 HRESULT script_elem_from_nsscript(HTMLDocumentNode *doc, nsIDOMHTMLScriptElement *nsscript, HTMLScriptElement **ret)
323 {
324     HTMLDOMNode *node;
325     HRESULT hres;
326
327     hres = get_node(doc, (nsIDOMNode*)nsscript, TRUE, &node);
328     if(FAILED(hres))
329         return hres;
330
331     assert(node->vtbl == &HTMLScriptElementImplVtbl);
332     *ret = impl_from_HTMLDOMNode(node);
333     return S_OK;
334 }
335
336 static const tid_t HTMLScriptElement_iface_tids[] = {
337     HTMLELEMENT_TIDS,
338     IHTMLScriptElement_tid,
339     0
340 };
341
342 static dispex_static_data_t HTMLScriptElement_dispex = {
343     NULL,
344     DispHTMLScriptElement_tid,
345     NULL,
346     HTMLScriptElement_iface_tids
347 };
348
349 HRESULT HTMLScriptElement_Create(HTMLDocumentNode *doc, nsIDOMHTMLElement *nselem, HTMLElement **elem)
350 {
351     HTMLScriptElement *ret;
352     nsresult nsres;
353
354     ret = heap_alloc_zero(sizeof(HTMLScriptElement));
355     if(!ret)
356         return E_OUTOFMEMORY;
357
358     ret->IHTMLScriptElement_iface.lpVtbl = &HTMLScriptElementVtbl;
359     ret->element.node.vtbl = &HTMLScriptElementImplVtbl;
360
361     HTMLElement_Init(&ret->element, doc, nselem, &HTMLScriptElement_dispex);
362
363     nsres = nsIDOMHTMLElement_QueryInterface(nselem, &IID_nsIDOMHTMLScriptElement, (void**)&ret->nsscript);
364
365     /* Share nsscript reference with nsnode */
366     assert(nsres == NS_OK && (nsIDOMNode*)ret->nsscript == ret->element.node.nsnode);
367     nsIDOMNode_Release(ret->element.node.nsnode);
368
369     *elem = &ret->element;
370     return S_OK;
371 }