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
31 #include "wine/debug.h"
33 #include "mshtml_private.h"
34 #include "htmlscript.h"
35 #include "pluginhost.h"
36 #include "htmlevent.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(mshtml
);
43 #define CTXARG_T DWORDLONG
44 #define IActiveScriptSiteDebugVtbl IActiveScriptSiteDebug64Vtbl
46 #define IActiveScriptParse_Release IActiveScriptParse64_Release
47 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
48 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
49 #define IActiveScriptParseProcedure2_Release IActiveScriptParseProcedure2_64_Release
50 #define IActiveScriptParseProcedure2_ParseProcedureText IActiveScriptParseProcedure2_64_ParseProcedureText
54 #define CTXARG_T DWORD
55 #define IActiveScriptSiteDebugVtbl IActiveScriptSiteDebug32Vtbl
57 #define IActiveScriptParse_Release IActiveScriptParse32_Release
58 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
59 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
60 #define IActiveScriptParseProcedure2_Release IActiveScriptParseProcedure2_32_Release
61 #define IActiveScriptParseProcedure2_ParseProcedureText IActiveScriptParseProcedure2_32_ParseProcedureText
65 /* See jscript.h in jscript.dll. */
66 #define SCRIPTLANGUAGEVERSION_HTML 0x400
67 #define SCRIPTLANGUAGEVERSION_ES5 0x102
68 #define SCRIPTLANGUAGEVERSION_ES6 0x103
71 IActiveScriptSite IActiveScriptSite_iface
;
72 IActiveScriptSiteInterruptPoll IActiveScriptSiteInterruptPoll_iface
;
73 IActiveScriptSiteWindow IActiveScriptSiteWindow_iface
;
74 IActiveScriptSiteUIControl IActiveScriptSiteUIControl_iface
;
75 IActiveScriptSiteDebug IActiveScriptSiteDebug_iface
;
76 IServiceProvider IServiceProvider_iface
;
80 IActiveScript
*script
;
81 IActiveScriptParse
*parse
;
82 IActiveScriptParseProcedure2
*parse_proc
;
84 SCRIPTSTATE script_state
;
86 HTMLInnerWindow
*window
;
92 static ScriptHost
*get_elem_script_host(HTMLInnerWindow
*,HTMLScriptElement
*);
94 static BOOL
set_script_prop(IActiveScript
*script
, DWORD property
, VARIANT
*val
)
96 IActiveScriptProperty
*script_prop
;
99 hres
= IActiveScript_QueryInterface(script
, &IID_IActiveScriptProperty
,
100 (void**)&script_prop
);
102 WARN("Could not get IActiveScriptProperty iface: %08lx\n", hres
);
106 hres
= IActiveScriptProperty_SetProperty(script_prop
, property
, NULL
, val
);
107 IActiveScriptProperty_Release(script_prop
);
109 WARN("SetProperty(%lx) failed: %08lx\n", property
, hres
);
116 static BOOL
init_script_engine(ScriptHost
*script_host
, IActiveScript
*script
)
118 compat_mode_t compat_mode
;
119 IObjectSafety
*safety
;
121 DWORD supported_opts
=0, enabled_opts
=0, script_mode
;
125 hres
= IActiveScript_QueryInterface(script
, &IID_IActiveScriptParse
, (void**)&script_host
->parse
);
127 WARN("Could not get IActiveScriptParse: %08lx\n", hres
);
131 hres
= IActiveScript_QueryInterface(script
, &IID_IObjectSafety
, (void**)&safety
);
133 FIXME("Could not get IObjectSafety: %08lx\n", hres
);
137 hres
= IObjectSafety_GetInterfaceSafetyOptions(safety
, &IID_IActiveScriptParse
, &supported_opts
, &enabled_opts
);
139 FIXME("GetInterfaceSafetyOptions failed: %08lx\n", hres
);
140 }else if(!(supported_opts
& INTERFACE_USES_DISPEX
)) {
141 FIXME("INTERFACE_USES_DISPEX is not supported\n");
143 hres
= IObjectSafety_SetInterfaceSafetyOptions(safety
, &IID_IActiveScriptParse
,
144 INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
,
145 INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
);
147 FIXME("SetInterfaceSafetyOptions failed: %08lx\n", hres
);
150 IObjectSafety_Release(safety
);
154 compat_mode
= lock_document_mode(script_host
->window
->doc
);
155 script_mode
= compat_mode
< COMPAT_MODE_IE8
? SCRIPTLANGUAGEVERSION_5_7
: SCRIPTLANGUAGEVERSION_5_8
;
156 if(IsEqualGUID(&script_host
->guid
, &CLSID_JScript
)) {
157 if(compat_mode
>= COMPAT_MODE_IE11
)
158 script_mode
= SCRIPTLANGUAGEVERSION_ES6
;
159 else if(compat_mode
>= COMPAT_MODE_IE9
)
160 script_mode
= SCRIPTLANGUAGEVERSION_ES5
;
161 script_mode
|= SCRIPTLANGUAGEVERSION_HTML
;
164 V_I4(&var
) = script_mode
;
165 if(!set_script_prop(script
, SCRIPTPROP_INVOKEVERSIONING
, &var
) && (script_mode
& SCRIPTLANGUAGEVERSION_HTML
)) {
166 /* If this failed, we're most likely using native jscript. */
167 WARN("Failed to set script mode to HTML version.\n");
168 V_I4(&var
) = compat_mode
< COMPAT_MODE_IE8
? SCRIPTLANGUAGEVERSION_5_7
: SCRIPTLANGUAGEVERSION_5_8
;
169 set_script_prop(script
, SCRIPTPROP_INVOKEVERSIONING
, &var
);
172 V_VT(&var
) = VT_BOOL
;
173 V_BOOL(&var
) = VARIANT_TRUE
;
174 set_script_prop(script
, SCRIPTPROP_HACK_TRIDENTEVENTSINK
, &var
);
176 hres
= IActiveScriptParse_InitNew(script_host
->parse
);
178 WARN("InitNew failed: %08lx\n", hres
);
182 hres
= IActiveScript_SetScriptSite(script
, &script_host
->IActiveScriptSite_iface
);
184 WARN("SetScriptSite failed: %08lx\n", hres
);
185 IActiveScript_Close(script
);
189 hres
= IActiveScript_GetScriptState(script
, &state
);
191 WARN("GetScriptState failed: %08lx\n", hres
);
192 else if(state
!= SCRIPTSTATE_INITIALIZED
)
193 FIXME("state = %x\n", state
);
195 hres
= IActiveScript_SetScriptState(script
, SCRIPTSTATE_STARTED
);
197 WARN("Starting script failed: %08lx\n", hres
);
201 hres
= IActiveScript_AddNamedItem(script
, L
"window",
202 SCRIPTITEM_ISVISIBLE
|SCRIPTITEM_ISSOURCE
|SCRIPTITEM_GLOBALMEMBERS
);
203 if(SUCCEEDED(hres
)) {
204 ScriptHost
*first_host
;
206 V_VT(&var
) = VT_BOOL
;
207 V_BOOL(&var
) = VARIANT_TRUE
;
209 LIST_FOR_EACH_ENTRY(first_host
, &script_host
->window
->script_hosts
, ScriptHost
, entry
) {
210 if(first_host
->script
) {
211 V_BOOL(&var
) = VARIANT_FALSE
;
215 set_script_prop(script
, SCRIPTPROP_ABBREVIATE_GLOBALNAME_RESOLUTION
, &var
);
217 /* if this was second engine initialized, also set it to first engine, since it used to be TRUE */
219 struct list
*iter
= &first_host
->entry
;
220 BOOL is_second_init
= TRUE
;
222 while((iter
= list_next(&script_host
->window
->script_hosts
, iter
))) {
223 if(LIST_ENTRY(iter
, ScriptHost
, entry
)->script
) {
224 is_second_init
= FALSE
;
229 set_script_prop(first_host
->script
, SCRIPTPROP_ABBREVIATE_GLOBALNAME_RESOLUTION
, &var
);
232 WARN("AddNamedItem failed: %08lx\n", hres
);
235 hres
= IActiveScript_QueryInterface(script
, &IID_IActiveScriptParseProcedure2
,
236 (void**)&script_host
->parse_proc
);
238 /* FIXME: QI for IActiveScriptParseProcedure */
239 WARN("Could not get IActiveScriptParseProcedure iface: %08lx\n", hres
);
245 static void release_script_engine(ScriptHost
*This
)
250 switch(This
->script_state
) {
251 case SCRIPTSTATE_CONNECTED
:
252 IActiveScript_SetScriptState(This
->script
, SCRIPTSTATE_DISCONNECTED
);
254 case SCRIPTSTATE_STARTED
:
255 case SCRIPTSTATE_DISCONNECTED
:
256 case SCRIPTSTATE_INITIALIZED
:
257 IActiveScript_Close(This
->script
);
260 if(This
->parse_proc
) {
261 IActiveScriptParseProcedure2_Release(This
->parse_proc
);
262 This
->parse_proc
= NULL
;
266 IActiveScriptParse_Release(This
->parse
);
271 IActiveScript_Release(This
->script
);
273 This
->script_state
= SCRIPTSTATE_UNINITIALIZED
;
276 void connect_scripts(HTMLInnerWindow
*window
)
280 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
281 if(iter
->script_state
== SCRIPTSTATE_STARTED
)
282 IActiveScript_SetScriptState(iter
->script
, SCRIPTSTATE_CONNECTED
);
286 static inline ScriptHost
*impl_from_IActiveScriptSite(IActiveScriptSite
*iface
)
288 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSite_iface
);
291 static HRESULT WINAPI
ActiveScriptSite_QueryInterface(IActiveScriptSite
*iface
, REFIID riid
, void **ppv
)
293 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
297 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
298 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
299 *ppv
= &This
->IActiveScriptSite_iface
;
300 }else if(IsEqualGUID(&IID_IActiveScriptSite
, riid
)) {
301 TRACE("(%p)->(IID_IActiveScriptSite %p)\n", This
, ppv
);
302 *ppv
= &This
->IActiveScriptSite_iface
;
303 }else if(IsEqualGUID(&IID_IActiveScriptSiteInterruptPoll
, riid
)) {
304 TRACE("(%p)->(IID_IActiveScriptSiteInterruprtPoll %p)\n", This
, ppv
);
305 *ppv
= &This
->IActiveScriptSiteInterruptPoll_iface
;
306 }else if(IsEqualGUID(&IID_IActiveScriptSiteWindow
, riid
)) {
307 TRACE("(%p)->(IID_IActiveScriptSiteWindow %p)\n", This
, ppv
);
308 *ppv
= &This
->IActiveScriptSiteWindow_iface
;
309 }else if(IsEqualGUID(&IID_IActiveScriptSiteUIControl
, riid
)) {
310 TRACE("(%p)->(IID_IActiveScriptSiteUIControl %p)\n", This
, ppv
);
311 *ppv
= &This
->IActiveScriptSiteUIControl_iface
;
312 }else if(IsEqualGUID(&IID_IActiveScriptSiteDebug
, riid
)) {
313 TRACE("(%p)->(IID_IActiveScriptSiteDebug %p)\n", This
, ppv
);
314 *ppv
= &This
->IActiveScriptSiteDebug_iface
;
315 }else if(IsEqualGUID(&IID_IServiceProvider
, riid
)) {
316 TRACE("(%p)->(IID_IServiceProvider %p)\n", This
, ppv
);
317 *ppv
= &This
->IServiceProvider_iface
;
318 }else if(IsEqualGUID(&IID_ICanHandleException
, riid
)) {
319 TRACE("(%p)->(IID_ICanHandleException not supported %p)\n", This
, ppv
);
320 return E_NOINTERFACE
;
322 FIXME("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
323 return E_NOINTERFACE
;
326 IUnknown_AddRef((IUnknown
*)*ppv
);
330 static ULONG WINAPI
ActiveScriptSite_AddRef(IActiveScriptSite
*iface
)
332 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
333 LONG ref
= InterlockedIncrement(&This
->ref
);
335 TRACE("(%p) ref=%ld\n", This
, ref
);
340 static ULONG WINAPI
ActiveScriptSite_Release(IActiveScriptSite
*iface
)
342 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
343 LONG ref
= InterlockedDecrement(&This
->ref
);
345 TRACE("(%p) ref=%ld\n", This
, ref
);
348 release_script_engine(This
);
350 list_remove(&This
->entry
);
357 static HRESULT WINAPI
ActiveScriptSite_GetLCID(IActiveScriptSite
*iface
, LCID
*plcid
)
359 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
361 TRACE("(%p)->(%p)\n", This
, plcid
);
363 *plcid
= GetUserDefaultLCID();
367 static HRESULT WINAPI
ActiveScriptSite_GetItemInfo(IActiveScriptSite
*iface
, LPCOLESTR pstrName
,
368 DWORD dwReturnMask
, IUnknown
**ppiunkItem
, ITypeInfo
**ppti
)
370 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
372 TRACE("(%p)->(%s %lx %p %p)\n", This
, debugstr_w(pstrName
), dwReturnMask
, ppiunkItem
, ppti
);
374 if(dwReturnMask
!= SCRIPTINFO_IUNKNOWN
) {
375 FIXME("Unsupported mask %lx\n", dwReturnMask
);
381 if(wcscmp(pstrName
, L
"window"))
382 return DISP_E_MEMBERNOTFOUND
;
387 /* FIXME: Return proxy object */
388 *ppiunkItem
= (IUnknown
*)&This
->window
->base
.IHTMLWindow2_iface
;
389 IUnknown_AddRef(*ppiunkItem
);
394 static HRESULT WINAPI
ActiveScriptSite_GetDocVersionString(IActiveScriptSite
*iface
, BSTR
*pbstrVersion
)
396 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
397 FIXME("(%p)->(%p)\n", This
, pbstrVersion
);
401 static HRESULT WINAPI
ActiveScriptSite_OnScriptTerminate(IActiveScriptSite
*iface
,
402 const VARIANT
*pvarResult
, const EXCEPINFO
*pexcepinfo
)
404 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
405 FIXME("(%p)->(%p %p)\n", This
, pvarResult
, pexcepinfo
);
409 static HRESULT WINAPI
ActiveScriptSite_OnStateChange(IActiveScriptSite
*iface
, SCRIPTSTATE ssScriptState
)
411 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
413 TRACE("(%p)->(%x)\n", This
, ssScriptState
);
415 This
->script_state
= ssScriptState
;
419 static HRESULT WINAPI
ActiveScriptSite_OnScriptError(IActiveScriptSite
*iface
, IActiveScriptError
*pscripterror
)
421 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
422 FIXME("(%p)->(%p)\n", This
, pscripterror
);
426 static HRESULT WINAPI
ActiveScriptSite_OnEnterScript(IActiveScriptSite
*iface
)
428 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
430 TRACE("(%p)->()\n", This
);
435 static HRESULT WINAPI
ActiveScriptSite_OnLeaveScript(IActiveScriptSite
*iface
)
437 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
439 TRACE("(%p)->()\n", This
);
444 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl
= {
445 ActiveScriptSite_QueryInterface
,
446 ActiveScriptSite_AddRef
,
447 ActiveScriptSite_Release
,
448 ActiveScriptSite_GetLCID
,
449 ActiveScriptSite_GetItemInfo
,
450 ActiveScriptSite_GetDocVersionString
,
451 ActiveScriptSite_OnScriptTerminate
,
452 ActiveScriptSite_OnStateChange
,
453 ActiveScriptSite_OnScriptError
,
454 ActiveScriptSite_OnEnterScript
,
455 ActiveScriptSite_OnLeaveScript
458 static inline ScriptHost
*impl_from_IActiveScriptSiteInterruptPoll(IActiveScriptSiteInterruptPoll
*iface
)
460 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteInterruptPoll_iface
);
463 static HRESULT WINAPI
ActiveScriptSiteInterruptPoll_QueryInterface(IActiveScriptSiteInterruptPoll
*iface
,
464 REFIID riid
, void **ppv
)
466 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
467 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
470 static ULONG WINAPI
ActiveScriptSiteInterruptPoll_AddRef(IActiveScriptSiteInterruptPoll
*iface
)
472 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
473 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
476 static ULONG WINAPI
ActiveScriptSiteInterruptPoll_Release(IActiveScriptSiteInterruptPoll
*iface
)
478 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
479 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
482 static HRESULT WINAPI
ActiveScriptSiteInterruptPoll_QueryContinue(IActiveScriptSiteInterruptPoll
*iface
)
484 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
486 TRACE("(%p)\n", This
);
491 static const IActiveScriptSiteInterruptPollVtbl ActiveScriptSiteInterruptPollVtbl
= {
492 ActiveScriptSiteInterruptPoll_QueryInterface
,
493 ActiveScriptSiteInterruptPoll_AddRef
,
494 ActiveScriptSiteInterruptPoll_Release
,
495 ActiveScriptSiteInterruptPoll_QueryContinue
498 static inline ScriptHost
*impl_from_IActiveScriptSiteWindow(IActiveScriptSiteWindow
*iface
)
500 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteWindow_iface
);
503 static HRESULT WINAPI
ActiveScriptSiteWindow_QueryInterface(IActiveScriptSiteWindow
*iface
,
504 REFIID riid
, void **ppv
)
506 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
507 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
510 static ULONG WINAPI
ActiveScriptSiteWindow_AddRef(IActiveScriptSiteWindow
*iface
)
512 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
513 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
516 static ULONG WINAPI
ActiveScriptSiteWindow_Release(IActiveScriptSiteWindow
*iface
)
518 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
519 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
522 static HRESULT WINAPI
ActiveScriptSiteWindow_GetWindow(IActiveScriptSiteWindow
*iface
, HWND
*phwnd
)
524 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
526 TRACE("(%p)->(%p)\n", This
, phwnd
);
528 if(!This
->window
|| !This
->window
->base
.outer_window
)
531 *phwnd
= This
->window
->base
.outer_window
->browser
->doc
->hwnd
;
535 static HRESULT WINAPI
ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow
*iface
, BOOL fEnable
)
537 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
538 FIXME("(%p)->(%x)\n", This
, fEnable
);
542 static const IActiveScriptSiteWindowVtbl ActiveScriptSiteWindowVtbl
= {
543 ActiveScriptSiteWindow_QueryInterface
,
544 ActiveScriptSiteWindow_AddRef
,
545 ActiveScriptSiteWindow_Release
,
546 ActiveScriptSiteWindow_GetWindow
,
547 ActiveScriptSiteWindow_EnableModeless
550 static inline ScriptHost
*impl_from_IActiveScriptSiteUIControl(IActiveScriptSiteUIControl
*iface
)
552 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteUIControl_iface
);
555 static HRESULT WINAPI
ActiveScriptSiteUIControl_QueryInterface(IActiveScriptSiteUIControl
*iface
, REFIID riid
, void **ppv
)
557 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
558 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
561 static ULONG WINAPI
ActiveScriptSiteUIControl_AddRef(IActiveScriptSiteUIControl
*iface
)
563 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
564 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
567 static ULONG WINAPI
ActiveScriptSiteUIControl_Release(IActiveScriptSiteUIControl
*iface
)
569 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
570 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
573 static HRESULT WINAPI
ActiveScriptSiteUIControl_GetUIBehavior(IActiveScriptSiteUIControl
*iface
, SCRIPTUICITEM UicItem
,
574 SCRIPTUICHANDLING
*pUicHandling
)
576 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
578 WARN("(%p)->(%d %p) semi-stub\n", This
, UicItem
, pUicHandling
);
580 *pUicHandling
= SCRIPTUICHANDLING_ALLOW
;
584 static const IActiveScriptSiteUIControlVtbl ActiveScriptSiteUIControlVtbl
= {
585 ActiveScriptSiteUIControl_QueryInterface
,
586 ActiveScriptSiteUIControl_AddRef
,
587 ActiveScriptSiteUIControl_Release
,
588 ActiveScriptSiteUIControl_GetUIBehavior
591 static inline ScriptHost
*impl_from_IActiveScriptSiteDebug(IActiveScriptSiteDebug
*iface
)
593 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteDebug_iface
);
596 static HRESULT WINAPI
ActiveScriptSiteDebug_QueryInterface(IActiveScriptSiteDebug
*iface
,
597 REFIID riid
, void **ppv
)
599 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
600 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
603 static ULONG WINAPI
ActiveScriptSiteDebug_AddRef(IActiveScriptSiteDebug
*iface
)
605 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
606 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
609 static ULONG WINAPI
ActiveScriptSiteDebug_Release(IActiveScriptSiteDebug
*iface
)
611 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
612 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
615 static HRESULT WINAPI
ActiveScriptSiteDebug_GetDocumentContextFromPosition(IActiveScriptSiteDebug
*iface
,
616 CTXARG_T dwSourceContext
, ULONG uCharacterOffset
, ULONG uNumChars
, IDebugDocumentContext
**ppsc
)
618 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
619 FIXME("(%p)->(%s %lu %lu %p)\n", This
, wine_dbgstr_longlong(dwSourceContext
), uCharacterOffset
,
624 static HRESULT WINAPI
ActiveScriptSiteDebug_GetApplication(IActiveScriptSiteDebug
*iface
, IDebugApplication
**ppda
)
626 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
627 FIXME("(%p)->(%p)\n", This
, ppda
);
631 static HRESULT WINAPI
ActiveScriptSiteDebug_GetRootApplicationNode(IActiveScriptSiteDebug
*iface
,
632 IDebugApplicationNode
**ppdanRoot
)
634 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
635 FIXME("(%p)->(%p)\n", This
, ppdanRoot
);
639 static HRESULT WINAPI
ActiveScriptSiteDebug_OnScriptErrorDebug(IActiveScriptSiteDebug
*iface
,
640 IActiveScriptErrorDebug
*pErrorDebug
, BOOL
*pfEnterDebugger
, BOOL
*pfCallOnScriptErrorWhenContinuing
)
642 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
643 FIXME("(%p)->(%p %p %p)\n", This
, pErrorDebug
, pfEnterDebugger
, pfCallOnScriptErrorWhenContinuing
);
647 static const IActiveScriptSiteDebugVtbl ActiveScriptSiteDebugVtbl
= {
648 ActiveScriptSiteDebug_QueryInterface
,
649 ActiveScriptSiteDebug_AddRef
,
650 ActiveScriptSiteDebug_Release
,
651 ActiveScriptSiteDebug_GetDocumentContextFromPosition
,
652 ActiveScriptSiteDebug_GetApplication
,
653 ActiveScriptSiteDebug_GetRootApplicationNode
,
654 ActiveScriptSiteDebug_OnScriptErrorDebug
657 static inline ScriptHost
*impl_from_IServiceProvider(IServiceProvider
*iface
)
659 return CONTAINING_RECORD(iface
, ScriptHost
, IServiceProvider_iface
);
662 static HRESULT WINAPI
ASServiceProvider_QueryInterface(IServiceProvider
*iface
, REFIID riid
, void **ppv
)
664 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
665 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
668 static ULONG WINAPI
ASServiceProvider_AddRef(IServiceProvider
*iface
)
670 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
671 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
674 static ULONG WINAPI
ASServiceProvider_Release(IServiceProvider
*iface
)
676 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
677 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
680 static HRESULT WINAPI
ASServiceProvider_QueryService(IServiceProvider
*iface
, REFGUID guidService
,
681 REFIID riid
, void **ppv
)
683 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
685 if(IsEqualGUID(&SID_SInternetHostSecurityManager
, guidService
)) {
686 TRACE("(%p)->(SID_SInternetHostSecurityManager)\n", This
);
688 if(!This
->window
|| !This
->window
->doc
)
689 return E_NOINTERFACE
;
691 return IInternetHostSecurityManager_QueryInterface(&This
->window
->doc
->IInternetHostSecurityManager_iface
,
695 if(IsEqualGUID(&SID_SContainerDispatch
, guidService
)) {
696 TRACE("(%p)->(SID_SContainerDispatch)\n", This
);
698 if(!This
->window
|| !This
->window
->doc
)
699 return E_NOINTERFACE
;
701 return IHTMLDocument2_QueryInterface(&This
->window
->doc
->IHTMLDocument2_iface
, riid
, ppv
);
704 FIXME("(%p)->(%s %s %p)\n", This
, debugstr_guid(guidService
), debugstr_guid(riid
), ppv
);
705 return E_NOINTERFACE
;
708 static const IServiceProviderVtbl ASServiceProviderVtbl
= {
709 ASServiceProvider_QueryInterface
,
710 ASServiceProvider_AddRef
,
711 ASServiceProvider_Release
,
712 ASServiceProvider_QueryService
715 static ScriptHost
*create_script_host(HTMLInnerWindow
*window
, const GUID
*guid
)
717 IActiveScript
*script
;
721 ret
= calloc(1, sizeof(*ret
));
725 ret
->IActiveScriptSite_iface
.lpVtbl
= &ActiveScriptSiteVtbl
;
726 ret
->IActiveScriptSiteInterruptPoll_iface
.lpVtbl
= &ActiveScriptSiteInterruptPollVtbl
;
727 ret
->IActiveScriptSiteWindow_iface
.lpVtbl
= &ActiveScriptSiteWindowVtbl
;
728 ret
->IActiveScriptSiteUIControl_iface
.lpVtbl
= &ActiveScriptSiteUIControlVtbl
;
729 ret
->IActiveScriptSiteDebug_iface
.lpVtbl
= &ActiveScriptSiteDebugVtbl
;
730 ret
->IServiceProvider_iface
.lpVtbl
= &ASServiceProviderVtbl
;
732 ret
->window
= window
;
733 ret
->script_state
= SCRIPTSTATE_UNINITIALIZED
;
736 list_add_tail(&window
->script_hosts
, &ret
->entry
);
738 hres
= CoCreateInstance(&ret
->guid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
739 &IID_IActiveScript
, (void**)&script
);
741 WARN("Could not load script engine: %08lx\n", hres
);
743 BOOL succeeded
= init_script_engine(ret
, script
);
744 ret
->script
= script
;
746 release_script_engine(ret
);
752 static void dispatch_script_readystatechange_event(HTMLScriptElement
*script
)
757 if(script
->readystate
!= READYSTATE_LOADED
||
758 dispex_compat_mode(&script
->element
.node
.event_target
.dispex
) < COMPAT_MODE_IE10
) {
759 hres
= create_document_event(script
->element
.node
.doc
, EVENTID_READYSTATECHANGE
, &event
);
760 if(SUCCEEDED(hres
)) {
761 dispatch_event(&script
->element
.node
.event_target
, event
);
762 IDOMEvent_Release(&event
->IDOMEvent_iface
);
766 if(script
->readystate
== READYSTATE_LOADED
) {
767 hres
= create_document_event(script
->element
.node
.doc
, EVENTID_LOAD
, &event
);
768 if(SUCCEEDED(hres
)) {
769 dispatch_event(&script
->element
.node
.event_target
, event
);
770 IDOMEvent_Release(&event
->IDOMEvent_iface
);
777 HTMLScriptElement
*elem
;
778 } fire_readystatechange_task_t
;
780 static void fire_readystatechange_proc(event_task_t
*_task
)
782 fire_readystatechange_task_t
*task
= (fire_readystatechange_task_t
*)_task
;
784 if(!task
->elem
->pending_readystatechange_event
)
787 task
->elem
->pending_readystatechange_event
= FALSE
;
788 dispatch_script_readystatechange_event(task
->elem
);
791 static void fire_readystatechange_task_destr(event_task_t
*_task
)
793 fire_readystatechange_task_t
*task
= (fire_readystatechange_task_t
*)_task
;
795 IHTMLScriptElement_Release(&task
->elem
->IHTMLScriptElement_iface
);
798 static void set_script_elem_readystate(HTMLScriptElement
*script_elem
, READYSTATE readystate
)
800 script_elem
->readystate
= readystate
;
802 if(readystate
!= READYSTATE_LOADED
&&
803 dispex_compat_mode(&script_elem
->element
.node
.event_target
.dispex
) >= COMPAT_MODE_IE11
)
806 if(readystate
!= READYSTATE_INTERACTIVE
) {
807 if(!script_elem
->element
.node
.doc
->window
->parser_callback_cnt
) {
808 fire_readystatechange_task_t
*task
;
811 if(script_elem
->pending_readystatechange_event
)
814 task
= malloc(sizeof(*task
));
818 IHTMLScriptElement_AddRef(&script_elem
->IHTMLScriptElement_iface
);
819 task
->elem
= script_elem
;
821 hres
= push_event_task(&task
->header
, script_elem
->element
.node
.doc
->window
, fire_readystatechange_proc
,
822 fire_readystatechange_task_destr
, script_elem
->element
.node
.doc
->window
->task_magic
);
824 script_elem
->pending_readystatechange_event
= TRUE
;
826 script_elem
->pending_readystatechange_event
= FALSE
;
827 dispatch_script_readystatechange_event(script_elem
);
832 static void parse_elem_text(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
, LPCWSTR text
)
838 TRACE("%s\n", debugstr_w(text
));
841 memset(&excepinfo
, 0, sizeof(excepinfo
));
843 hres
= IActiveScriptParse_ParseScriptText(script_host
->parse
, text
, L
"window", NULL
, L
"</SCRIPT>",
844 0, 0, SCRIPTTEXT_ISVISIBLE
|SCRIPTTEXT_HOSTMANAGESSOURCE
,
849 WARN("<<< %08lx\n", hres
);
855 HTMLScriptElement
*script_elem
;
856 nsILoadGroup
*load_group
;
864 static HRESULT
get_binding_text(ScriptBSC
*bsc
, WCHAR
**ret
)
870 text
= malloc(sizeof(WCHAR
));
872 return E_OUTOFMEMORY
;
878 switch(bsc
->bsc
.bom
) {
880 if(bsc
->bsc
.read
% sizeof(WCHAR
)) {
881 FIXME("The buffer is not a valid utf16 string\n");
885 text
= malloc(bsc
->bsc
.read
+ sizeof(WCHAR
));
887 return E_OUTOFMEMORY
;
889 memcpy(text
, bsc
->buf
, bsc
->bsc
.read
);
890 text
[bsc
->bsc
.read
/sizeof(WCHAR
)] = 0;
894 /* FIXME: Try to use charset from HTTP headers first */
895 cp
= get_document_charset(bsc
->script_elem
->element
.node
.doc
);
900 len
= MultiByteToWideChar(cp
, 0, bsc
->buf
, bsc
->bsc
.read
, NULL
, 0);
901 text
= malloc((len
+ 1) * sizeof(WCHAR
));
903 return E_OUTOFMEMORY
;
905 MultiByteToWideChar(cp
, 0, bsc
->buf
, bsc
->bsc
.read
, text
, len
);
914 static void script_file_available(ScriptBSC
*bsc
)
916 HTMLScriptElement
*script_elem
= bsc
->script_elem
;
917 HTMLInnerWindow
*window
= bsc
->bsc
.window
;
918 ScriptHost
*script_host
;
923 assert(window
!= NULL
);
925 hres
= get_binding_text(bsc
, &script_elem
->src_text
);
929 script_host
= get_elem_script_host(window
, script_elem
);
933 if(window
->parser_callback_cnt
) {
934 script_queue_entry_t
*queue
;
936 TRACE("Adding to queue\n");
938 queue
= malloc(sizeof(*queue
));
942 IHTMLScriptElement_AddRef(&script_elem
->IHTMLScriptElement_iface
);
943 queue
->script
= script_elem
;
945 list_add_tail(&window
->script_queue
, &queue
->entry
);
949 nsres
= nsIDOMElement_GetParentNode(script_elem
->element
.dom_element
, &parent
);
950 if(NS_FAILED(nsres
) || !parent
) {
951 TRACE("No parent, not executing\n");
952 script_elem
->parse_on_bind
= TRUE
;
956 nsIDOMNode_Release(parent
);
958 script_host
= get_elem_script_host(window
, script_elem
);
962 script_elem
->parsed
= TRUE
;
963 if(script_host
->parse
)
964 parse_elem_text(script_host
, script_elem
, script_elem
->src_text
);
967 static inline ScriptBSC
*impl_from_BSCallback(BSCallback
*iface
)
969 return CONTAINING_RECORD(iface
, ScriptBSC
, bsc
);
972 static void ScriptBSC_destroy(BSCallback
*bsc
)
974 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
976 if(This
->script_elem
) {
977 IHTMLScriptElement_Release(&This
->script_elem
->IHTMLScriptElement_iface
);
978 This
->script_elem
= NULL
;
982 ERR("Unfinished request\n");
983 nsIRequest_Release(This
->request
);
986 nsILoadGroup_Release(This
->load_group
);
992 static HRESULT
ScriptBSC_init_bindinfo(BSCallback
*bsc
)
997 static HRESULT
ScriptBSC_start_binding(BSCallback
*bsc
)
999 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
1002 This
->script_elem
->binding
= &This
->bsc
;
1004 if(This
->load_group
) {
1005 nsres
= create_onload_blocker_request(&This
->request
);
1006 if(NS_SUCCEEDED(nsres
)) {
1007 nsres
= nsILoadGroup_AddRequest(This
->load_group
, This
->request
, NULL
);
1008 if(NS_FAILED(nsres
))
1009 ERR("AddRequest failed: %08lx\n", nsres
);
1013 /* FIXME: We should find a better to decide if 'loading' state is supposed to be used by the protocol. */
1014 if(This
->scheme
== URL_SCHEME_HTTPS
|| This
->scheme
== URL_SCHEME_HTTP
)
1015 set_script_elem_readystate(This
->script_elem
, READYSTATE_LOADING
);
1020 static HRESULT
ScriptBSC_stop_binding(BSCallback
*bsc
, HRESULT result
)
1022 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
1025 if(SUCCEEDED(result
) && !This
->script_elem
)
1026 result
= E_UNEXPECTED
;
1028 assert(FAILED(result
) || This
->script_elem
->binding
== &This
->bsc
);
1029 This
->script_elem
->binding
= NULL
;
1031 if(This
->script_elem
->readystate
== READYSTATE_LOADING
)
1032 set_script_elem_readystate(This
->script_elem
, READYSTATE_LOADED
);
1034 if(SUCCEEDED(result
)) {
1035 script_file_available(This
);
1037 FIXME("binding failed %08lx\n", result
);
1044 nsres
= nsILoadGroup_RemoveRequest(This
->load_group
, This
->request
, NULL
, NS_OK
);
1045 if(NS_FAILED(nsres
))
1046 ERR("RemoveRequest failed: %08lx\n", nsres
);
1047 nsIRequest_Release(This
->request
);
1048 This
->request
= NULL
;
1051 IHTMLScriptElement_Release(&This
->script_elem
->IHTMLScriptElement_iface
);
1052 This
->script_elem
= NULL
;
1056 static HRESULT
ScriptBSC_read_data(BSCallback
*bsc
, IStream
*stream
)
1058 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
1063 This
->buf
= malloc(128);
1065 return E_OUTOFMEMORY
;
1070 if(This
->bsc
.read
>= This
->size
) {
1072 new_buf
= realloc(This
->buf
, This
->size
<< 1);
1074 return E_OUTOFMEMORY
;
1076 This
->buf
= new_buf
;
1079 hres
= read_stream(&This
->bsc
, stream
, This
->buf
+This
->bsc
.read
, This
->size
-This
->bsc
.read
, &read
);
1080 }while(hres
== S_OK
);
1085 static HRESULT
ScriptBSC_on_progress(BSCallback
*bsc
, ULONG progress
, ULONG total
, ULONG status_code
, LPCWSTR status_text
)
1090 static HRESULT
ScriptBSC_on_response(BSCallback
*bsc
, DWORD response_code
,
1091 LPCWSTR response_headers
)
1096 static HRESULT
ScriptBSC_beginning_transaction(BSCallback
*bsc
, WCHAR
**additional_headers
)
1101 static const BSCallbackVtbl ScriptBSCVtbl
= {
1103 ScriptBSC_init_bindinfo
,
1104 ScriptBSC_start_binding
,
1105 ScriptBSC_stop_binding
,
1106 ScriptBSC_read_data
,
1107 ScriptBSC_on_progress
,
1108 ScriptBSC_on_response
,
1109 ScriptBSC_beginning_transaction
1113 HRESULT
load_script(HTMLScriptElement
*script_elem
, const WCHAR
*src
, BOOL async
)
1115 HTMLInnerWindow
*window
;
1121 static const WCHAR wine_schemaW
[] = {'w','i','n','e',':'};
1123 if(lstrlenW(src
) > ARRAY_SIZE(wine_schemaW
) && !memcmp(src
, wine_schemaW
, sizeof(wine_schemaW
)))
1124 src
+= ARRAY_SIZE(wine_schemaW
);
1126 TRACE("(%p %s %x)\n", script_elem
, debugstr_w(src
), async
);
1128 if(!script_elem
->element
.node
.doc
|| !(window
= script_elem
->element
.node
.doc
->window
)) {
1130 return E_UNEXPECTED
;
1133 hres
= create_uri(src
, 0, &uri
);
1137 hres
= CreateURLMonikerEx2(NULL
, uri
, &mon
, URL_MK_UNIFORM
);
1143 bsc
= calloc(1, sizeof(*bsc
));
1145 IMoniker_Release(mon
);
1147 return E_OUTOFMEMORY
;
1150 init_bscallback(&bsc
->bsc
, &ScriptBSCVtbl
, mon
, async
? BINDF_ASYNCHRONOUS
| BINDF_ASYNCSTORAGE
| BINDF_PULLDATA
: 0);
1151 IMoniker_Release(mon
);
1153 hres
= IUri_GetScheme(uri
, &bsc
->scheme
);
1156 bsc
->scheme
= URL_SCHEME_UNKNOWN
;
1158 IHTMLScriptElement_AddRef(&script_elem
->IHTMLScriptElement_iface
);
1159 bsc
->script_elem
= script_elem
;
1161 if(window
->bscallback
&& window
->bscallback
->nschannel
&&
1162 window
->bscallback
->nschannel
->load_group
) {
1166 nsres
= nsIDOMNode_Contains(script_elem
->element
.node
.doc
->node
.nsnode
,
1167 script_elem
->element
.node
.nsnode
, &contains
);
1168 if(NS_SUCCEEDED(nsres
) && contains
) {
1169 TRACE("script %p will block load event\n", script_elem
);
1170 bsc
->load_group
= window
->bscallback
->nschannel
->load_group
;
1171 nsILoadGroup_AddRef(bsc
->load_group
);
1175 hres
= start_binding(window
, &bsc
->bsc
, NULL
);
1177 IBindStatusCallback_Release(&bsc
->bsc
.IBindStatusCallback_iface
);
1181 static void parse_inline_script(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
)
1183 const PRUnichar
*text
;
1187 nsAString_Init(&text_str
, NULL
);
1188 nsres
= nsIDOMHTMLScriptElement_GetText(script_elem
->nsscript
, &text_str
);
1189 nsAString_GetData(&text_str
, &text
);
1191 set_script_elem_readystate(script_elem
, READYSTATE_INTERACTIVE
);
1193 if(NS_FAILED(nsres
)) {
1194 ERR("GetText failed: %08lx\n", nsres
);
1196 parse_elem_text(script_host
, script_elem
, text
);
1199 nsAString_Finish(&text_str
);
1202 static BOOL
parse_script_elem(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
)
1204 nsAString src_str
, event_str
;
1205 BOOL is_complete
= FALSE
;
1206 const PRUnichar
*src
;
1209 nsAString_Init(&event_str
, NULL
);
1210 nsres
= nsIDOMHTMLScriptElement_GetEvent(script_elem
->nsscript
, &event_str
);
1211 if(NS_SUCCEEDED(nsres
)) {
1212 const PRUnichar
*event
;
1214 nsAString_GetData(&event_str
, &event
);
1216 TRACE("deferring event %s script evaluation\n", debugstr_w(event
));
1217 nsAString_Finish(&event_str
);
1221 ERR("GetEvent failed: %08lx\n", nsres
);
1223 nsAString_Finish(&event_str
);
1225 nsAString_Init(&src_str
, NULL
);
1226 nsres
= nsIDOMHTMLScriptElement_GetSrc(script_elem
->nsscript
, &src_str
);
1227 nsAString_GetData(&src_str
, &src
);
1229 if(NS_FAILED(nsres
)) {
1230 ERR("GetSrc failed: %08lx\n", nsres
);
1232 load_script(script_elem
, src
, FALSE
);
1233 is_complete
= script_elem
->parsed
;
1235 parse_inline_script(script_host
, script_elem
);
1239 nsAString_Finish(&src_str
);
1244 static GUID
get_default_script_guid(HTMLInnerWindow
*window
)
1246 /* If not specified, we should use very first script host that was created for the page (or JScript if none) */
1247 return list_empty(&window
->script_hosts
)
1249 : LIST_ENTRY(list_head(&window
->script_hosts
), ScriptHost
, entry
)->guid
;
1252 static BOOL
get_guid_from_type(LPCWSTR type
, GUID
*guid
)
1254 /* FIXME: Handle more types */
1255 if(!wcsicmp(type
, L
"text/javascript") || !wcsicmp(type
, L
"text/jscript")) {
1256 *guid
= CLSID_JScript
;
1257 }else if(!wcsicmp(type
, L
"text/vbscript")) {
1258 *guid
= CLSID_VBScript
;
1260 FIXME("Unknown type %s\n", debugstr_w(type
));
1267 static BOOL
get_guid_from_language(LPCWSTR type
, GUID
*guid
)
1271 hres
= CLSIDFromProgID(type
, guid
);
1275 /* FIXME: Check CATID_ActiveScriptParse */
1280 static BOOL
get_script_guid(HTMLInnerWindow
*window
, nsIDOMHTMLScriptElement
*nsscript
, GUID
*guid
)
1282 nsIDOMElement
*nselem
;
1283 const PRUnichar
*language
;
1288 nsAString_Init(&val_str
, NULL
);
1290 nsres
= nsIDOMHTMLScriptElement_GetType(nsscript
, &val_str
);
1291 if(NS_SUCCEEDED(nsres
)) {
1292 const PRUnichar
*type
;
1294 nsAString_GetData(&val_str
, &type
);
1296 ret
= get_guid_from_type(type
, guid
);
1297 nsAString_Finish(&val_str
);
1301 ERR("GetType failed: %08lx\n", nsres
);
1304 nsres
= nsIDOMHTMLScriptElement_QueryInterface(nsscript
, &IID_nsIDOMElement
, (void**)&nselem
);
1305 assert(nsres
== NS_OK
);
1307 nsres
= get_elem_attr_value(nselem
, L
"language", &val_str
, &language
);
1308 nsIDOMElement_Release(nselem
);
1309 if(NS_SUCCEEDED(nsres
)) {
1311 ret
= get_guid_from_language(language
, guid
);
1313 *guid
= get_default_script_guid(window
);
1316 nsAString_Finish(&val_str
);
1322 static ScriptHost
*get_script_host(HTMLInnerWindow
*window
, const GUID
*guid
)
1326 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
1327 if(IsEqualGUID(guid
, &iter
->guid
))
1331 return create_script_host(window
, guid
);
1334 static ScriptHost
*get_elem_script_host(HTMLInnerWindow
*window
, HTMLScriptElement
*script_elem
)
1338 if(!get_script_guid(window
, script_elem
->nsscript
, &guid
)) {
1339 WARN("Could not find script GUID\n");
1343 if(IsEqualGUID(&CLSID_JScript
, &guid
) && (!window
->doc
->browser
|| window
->doc
->browser
->script_mode
!= SCRIPTMODE_ACTIVESCRIPT
)) {
1344 TRACE("Ignoring JScript\n");
1348 return get_script_host(window
, &guid
);
1351 void doc_insert_script(HTMLInnerWindow
*window
, HTMLScriptElement
*script_elem
, BOOL from_parser
)
1353 ScriptHost
*script_host
;
1354 BOOL is_complete
= FALSE
;
1356 script_host
= get_elem_script_host(window
, script_elem
);
1360 if(script_host
->parse
) {
1361 if(script_elem
->src_text
) {
1363 set_script_elem_readystate(script_elem
, READYSTATE_INTERACTIVE
);
1364 script_elem
->parsed
= TRUE
;
1365 parse_elem_text(script_host
, script_elem
, script_elem
->src_text
);
1367 }else if(!script_elem
->binding
) {
1368 is_complete
= parse_script_elem(script_host
, script_elem
);
1373 set_script_elem_readystate(script_elem
, READYSTATE_COMPLETE
);
1376 IDispatch
*script_parse_event(HTMLInnerWindow
*window
, LPCWSTR text
)
1378 ScriptHost
*script_host
;
1384 TRACE("%s\n", debugstr_w(text
));
1386 for(ptr
= text
; iswalnum(*ptr
); ptr
++);
1391 language
= malloc((ptr
- text
+ 1) * sizeof(WCHAR
));
1395 memcpy(language
, text
, (ptr
-text
)*sizeof(WCHAR
));
1396 language
[ptr
-text
] = 0;
1398 b
= get_guid_from_language(language
, &guid
);
1403 WARN("Could not find language\n");
1410 guid
= get_default_script_guid(window
);
1413 if(IsEqualGUID(&CLSID_JScript
, &guid
)
1414 && (!window
->doc
->browser
|| window
->doc
->browser
->script_mode
!= SCRIPTMODE_ACTIVESCRIPT
)) {
1415 TRACE("Ignoring JScript\n");
1419 script_host
= get_script_host(window
, &guid
);
1420 if(!script_host
|| !script_host
->parse_proc
)
1423 hres
= IActiveScriptParseProcedure2_ParseProcedureText(script_host
->parse_proc
, ptr
, NULL
, L
"",
1424 NULL
, NULL
, L
"\"", 0 /* FIXME */, 0,
1425 SCRIPTPROC_HOSTMANAGESSOURCE
|SCRIPTPROC_IMPLICIT_THIS
|SCRIPTPROC_IMPLICIT_PARENTS
, &disp
);
1427 WARN("ParseProcedureText failed: %08lx\n", hres
);
1431 TRACE("ret %p\n", disp
);
1435 HRESULT
exec_script(HTMLInnerWindow
*window
, const WCHAR
*code
, const WCHAR
*lang
, VARIANT
*ret
)
1437 ScriptHost
*script_host
;
1442 if(!get_guid_from_language(lang
, &guid
)) {
1443 WARN("Could not find script GUID\n");
1444 return CO_E_CLASSSTRING
;
1447 script_host
= get_script_host(window
, &guid
);
1449 FIXME("No script host\n");
1453 if(!script_host
->parse
) {
1454 FIXME("script_host->parse == NULL\n");
1458 memset(&ei
, 0, sizeof(ei
));
1460 hres
= IActiveScriptParse_ParseScriptText(script_host
->parse
, code
, NULL
, NULL
, L
"\"", 0, 0, SCRIPTTEXT_ISVISIBLE
, ret
, &ei
);
1464 WARN("<<< %08lx\n", hres
);
1469 IDispatch
*get_script_disp(ScriptHost
*script_host
)
1474 if(!script_host
->script
)
1477 hres
= IActiveScript_GetScriptDispatch(script_host
->script
, L
"window", &disp
);
1484 static EventTarget
*find_event_target(HTMLDocumentNode
*doc
, HTMLScriptElement
*script_elem
)
1486 EventTarget
*event_target
= NULL
;
1487 const PRUnichar
*target_id
;
1488 nsAString target_id_str
;
1492 nsAString_Init(&target_id_str
, NULL
);
1493 nsres
= nsIDOMHTMLScriptElement_GetHtmlFor(script_elem
->nsscript
, &target_id_str
);
1494 if(NS_FAILED(nsres
)) {
1495 ERR("GetScriptFor failed: %08lx\n", nsres
);
1496 nsAString_Finish(&target_id_str
);
1500 nsAString_GetData(&target_id_str
, &target_id
);
1502 FIXME("Empty for attribute\n");
1503 }else if(!wcscmp(target_id
, L
"document")) {
1504 event_target
= &doc
->node
.event_target
;
1505 IHTMLDOMNode_AddRef(&doc
->node
.IHTMLDOMNode_iface
);
1506 }else if(!wcscmp(target_id
, L
"window")) {
1508 event_target
= &doc
->window
->event_target
;
1509 IDispatchEx_AddRef(&event_target
->dispex
.IDispatchEx_iface
);
1512 HTMLElement
*target_elem
;
1514 hres
= get_doc_elem_by_id(doc
, target_id
, &target_elem
);
1515 if(SUCCEEDED(hres
) && target_elem
) {
1516 event_target
= &target_elem
->node
.event_target
;
1520 nsAString_Finish(&target_id_str
);
1521 return event_target
;
1524 static BOOL
parse_event_str(WCHAR
*event
, const WCHAR
**args
)
1528 TRACE("%s\n", debugstr_w(event
));
1530 for(ptr
= event
; iswalnum(*ptr
); ptr
++);
1541 while(iswalnum(*ptr
) || iswspace(*ptr
) || *ptr
== ',')
1551 static IDispatch
*parse_event_elem(HTMLDocumentNode
*doc
, HTMLScriptElement
*script_elem
, WCHAR
**ret_event
)
1553 ScriptHost
*script_host
;
1554 WCHAR
*event
= NULL
;
1561 if(script_elem
->parsed
)
1564 script_host
= get_elem_script_host(doc
->window
, script_elem
);
1565 if(!script_host
|| !script_host
->parse_proc
)
1568 nsAString_Init(&nsstr
, NULL
);
1569 nsres
= nsIDOMHTMLScriptElement_GetEvent(script_elem
->nsscript
, &nsstr
);
1570 if(NS_SUCCEEDED(nsres
)) {
1571 const PRUnichar
*event_val
;
1573 nsAString_GetData(&nsstr
, &event_val
);
1574 event
= wcsdup(event_val
);
1576 nsAString_Finish(&nsstr
);
1580 if(!parse_event_str(event
, &args
)) {
1581 WARN("parsing %s failed\n", debugstr_w(event
));
1586 nsAString_Init(&nsstr
, NULL
);
1587 nsres
= nsIDOMHTMLScriptElement_GetText(script_elem
->nsscript
, &nsstr
);
1588 if(NS_SUCCEEDED(nsres
)) {
1589 const PRUnichar
*text
;
1591 nsAString_GetData(&nsstr
, &text
);
1592 hres
= IActiveScriptParseProcedure2_ParseProcedureText(script_host
->parse_proc
, text
, args
,
1593 L
"", NULL
, NULL
, L
"</SCRIPT>", 0, 0,
1594 SCRIPTPROC_HOSTMANAGESSOURCE
|SCRIPTPROC_IMPLICIT_THIS
|SCRIPTPROC_IMPLICIT_PARENTS
, &disp
);
1598 ERR("GetText failed: %08lx\n", nsres
);
1601 nsAString_Finish(&nsstr
);
1611 void bind_event_scripts(HTMLDocumentNode
*doc
)
1613 HTMLPluginContainer
*plugin_container
;
1614 nsIDOMHTMLScriptElement
*nsscript
;
1615 HTMLScriptElement
*script_elem
;
1616 EventTarget
*event_target
;
1617 nsIDOMNodeList
*node_list
;
1618 nsIDOMNode
*script_node
;
1619 nsAString selector_str
;
1620 IDispatch
*event_disp
;
1628 if(!doc
->dom_document
)
1631 nsAString_InitDepend(&selector_str
, L
"script[event]");
1632 nsres
= nsIDOMDocument_QuerySelectorAll(doc
->dom_document
, &selector_str
, &node_list
);
1633 nsAString_Finish(&selector_str
);
1634 if(NS_FAILED(nsres
)) {
1635 ERR("QuerySelectorAll failed: %08lx\n", nsres
);
1642 nsres
= nsIDOMNodeList_GetLength(node_list
, &length
);
1643 assert(nsres
== NS_OK
);
1645 for(i
=0; i
< length
; i
++) {
1646 nsres
= nsIDOMNodeList_Item(node_list
, i
, &script_node
);
1647 if(NS_FAILED(nsres
) || !script_node
) {
1648 ERR("Item(%d) failed: %08lx\n", i
, nsres
);
1652 nsres
= nsIDOMNode_QueryInterface(script_node
, &IID_nsIDOMHTMLScriptElement
, (void**)&nsscript
);
1653 assert(nsres
== NS_OK
);
1654 nsIDOMNode_Release(script_node
);
1656 hres
= script_elem_from_nsscript(nsscript
, &script_elem
);
1657 nsIDOMHTMLScriptElement_Release(nsscript
);
1661 event_disp
= parse_event_elem(doc
, script_elem
, &event
);
1663 event_target
= find_event_target(doc
, script_elem
);
1665 hres
= IDispatchEx_QueryInterface(&event_target
->dispex
.IDispatchEx_iface
, &IID_HTMLPluginContainer
,
1666 (void**)&plugin_container
);
1668 bind_activex_event(doc
, plugin_container
, event
, event_disp
);
1670 bind_target_event(doc
, event_target
, event
, event_disp
);
1672 IDispatchEx_Release(&event_target
->dispex
.IDispatchEx_iface
);
1673 if(plugin_container
)
1674 node_release(&plugin_container
->element
.node
);
1678 IDispatch_Release(event_disp
);
1681 IHTMLScriptElement_Release(&script_elem
->IHTMLScriptElement_iface
);
1684 nsIDOMNodeList_Release(node_list
);
1687 BOOL
find_global_prop(HTMLInnerWindow
*window
, BSTR name
, DWORD flags
, ScriptHost
**ret_host
, DISPID
*ret_id
)
1689 IDispatchEx
*dispex
;
1694 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
1695 disp
= get_script_disp(iter
);
1699 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1700 if(SUCCEEDED(hres
)) {
1701 hres
= IDispatchEx_GetDispID(dispex
, name
, flags
& (~fdexNameEnsure
), ret_id
);
1702 IDispatchEx_Release(dispex
);
1704 FIXME("No IDispatchEx\n");
1708 IDispatch_Release(disp
);
1709 if(SUCCEEDED(hres
)) {
1718 static BOOL
is_jscript_available(void)
1720 static BOOL available
, checked
;
1724 HRESULT hres
= CoGetClassObject(&CLSID_JScript
, CLSCTX_INPROC_SERVER
, NULL
, &IID_IUnknown
, (void**)&unk
);
1726 if(SUCCEEDED(hres
)) {
1728 IUnknown_Release(unk
);
1738 static BOOL
use_gecko_script(IUri
*uri
)
1744 hres
= IUri_GetDisplayUri(uri
, &display_uri
);
1748 hres
= IInternetSecurityManager_MapUrlToZone(get_security_manager(), display_uri
, &zone
, 0);
1750 WARN("Could not map %s to zone: %08lx\n", debugstr_w(display_uri
), hres
);
1751 SysFreeString(display_uri
);
1755 SysFreeString(display_uri
);
1756 TRACE("zone %ld\n", zone
);
1757 return zone
== URLZONE_UNTRUSTED
;
1760 void update_browser_script_mode(GeckoBrowser
*browser
, IUri
*uri
)
1762 nsIWebBrowserSetup
*setup
;
1765 if(!is_jscript_available()) {
1766 TRACE("jscript.dll not available\n");
1767 browser
->script_mode
= SCRIPTMODE_GECKO
;
1771 browser
->script_mode
= use_gecko_script(uri
) ? SCRIPTMODE_GECKO
: SCRIPTMODE_ACTIVESCRIPT
;
1773 nsres
= nsIWebBrowser_QueryInterface(browser
->webbrowser
, &IID_nsIWebBrowserSetup
, (void**)&setup
);
1774 if(NS_SUCCEEDED(nsres
)) {
1775 nsres
= nsIWebBrowserSetup_SetProperty(setup
, SETUP_ALLOW_JAVASCRIPT
,
1776 browser
->script_mode
== SCRIPTMODE_GECKO
);
1778 if(NS_SUCCEEDED(nsres
))
1779 nsres
= nsIWebBrowserSetup_SetProperty(setup
, SETUP_DISABLE_NOSCRIPT
, TRUE
);
1781 nsIWebBrowserSetup_Release(setup
);
1784 if(NS_FAILED(nsres
))
1785 ERR("JavaScript setup failed: %08lx\n", nsres
);
1788 void release_script_hosts(HTMLInnerWindow
*window
)
1790 script_queue_entry_t
*queue_iter
;
1793 while(!list_empty(&window
->script_queue
)) {
1794 queue_iter
= LIST_ENTRY(list_head(&window
->script_queue
), script_queue_entry_t
, entry
);
1796 list_remove(&queue_iter
->entry
);
1797 IHTMLScriptElement_Release(&queue_iter
->script
->IHTMLScriptElement_iface
);
1801 while(!list_empty(&window
->script_hosts
)) {
1802 iter
= LIST_ENTRY(list_head(&window
->script_hosts
), ScriptHost
, entry
);
1804 release_script_engine(iter
);
1805 list_remove(&iter
->entry
);
1806 iter
->window
= NULL
;
1807 IActiveScriptSite_Release(&iter
->IActiveScriptSite_iface
);