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
32 #include "wine/debug.h"
34 #include "mshtml_private.h"
35 #include "htmlscript.h"
36 #include "pluginhost.h"
37 #include "htmlevent.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(mshtml
);
44 #define CTXARG_T DWORDLONG
45 #define IActiveScriptSiteDebugVtbl IActiveScriptSiteDebug64Vtbl
47 #define IActiveScriptParse_Release IActiveScriptParse64_Release
48 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
49 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
50 #define IActiveScriptParseProcedure2_Release IActiveScriptParseProcedure2_64_Release
51 #define IActiveScriptParseProcedure2_ParseProcedureText IActiveScriptParseProcedure2_64_ParseProcedureText
55 #define CTXARG_T DWORD
56 #define IActiveScriptSiteDebugVtbl IActiveScriptSiteDebug32Vtbl
58 #define IActiveScriptParse_Release IActiveScriptParse32_Release
59 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
60 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
61 #define IActiveScriptParseProcedure2_Release IActiveScriptParseProcedure2_32_Release
62 #define IActiveScriptParseProcedure2_ParseProcedureText IActiveScriptParseProcedure2_32_ParseProcedureText
66 /* See jscript.h in jscript.dll. */
67 #define SCRIPTLANGUAGEVERSION_HTML 0x400
68 #define SCRIPTLANGUAGEVERSION_ES5 0x102
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(ScriptHost
*script_host
, DWORD property
, VARIANT
*val
)
96 IActiveScriptProperty
*script_prop
;
99 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptProperty
,
100 (void**)&script_prop
);
102 WARN("Could not get IActiveScriptProperty iface: %08x\n", hres
);
106 hres
= IActiveScriptProperty_SetProperty(script_prop
, property
, NULL
, val
);
107 IActiveScriptProperty_Release(script_prop
);
109 WARN("SetProperty(%x) failed: %08x\n", property
, hres
);
116 static BOOL
init_script_engine(ScriptHost
*script_host
)
118 compat_mode_t compat_mode
;
119 IObjectSafety
*safety
;
121 DWORD supported_opts
=0, enabled_opts
=0, script_mode
;
125 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptParse
, (void**)&script_host
->parse
);
127 WARN("Could not get IActiveScriptParse: %08x\n", hres
);
131 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IObjectSafety
, (void**)&safety
);
133 FIXME("Could not get IObjectSafety: %08x\n", hres
);
137 hres
= IObjectSafety_GetInterfaceSafetyOptions(safety
, &IID_IActiveScriptParse
, &supported_opts
, &enabled_opts
);
139 FIXME("GetInterfaceSafetyOptions failed: %08x\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: %08x\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_IE9
)
158 script_mode
= SCRIPTLANGUAGEVERSION_ES5
;
159 script_mode
|= SCRIPTLANGUAGEVERSION_HTML
;
162 V_I4(&var
) = script_mode
;
163 if(!set_script_prop(script_host
, SCRIPTPROP_INVOKEVERSIONING
, &var
) && (script_mode
& SCRIPTLANGUAGEVERSION_HTML
)) {
164 /* If this failed, we're most likely using native jscript. */
165 WARN("Failed to set script mode to HTML version.\n");
166 V_I4(&var
) = compat_mode
< COMPAT_MODE_IE8
? SCRIPTLANGUAGEVERSION_5_7
: SCRIPTLANGUAGEVERSION_5_8
;
167 set_script_prop(script_host
, SCRIPTPROP_INVOKEVERSIONING
, &var
);
170 V_VT(&var
) = VT_BOOL
;
171 V_BOOL(&var
) = VARIANT_TRUE
;
172 set_script_prop(script_host
, SCRIPTPROP_HACK_TRIDENTEVENTSINK
, &var
);
174 hres
= IActiveScriptParse_InitNew(script_host
->parse
);
176 WARN("InitNew failed: %08x\n", hres
);
180 hres
= IActiveScript_SetScriptSite(script_host
->script
, &script_host
->IActiveScriptSite_iface
);
182 WARN("SetScriptSite failed: %08x\n", hres
);
183 IActiveScript_Close(script_host
->script
);
187 hres
= IActiveScript_GetScriptState(script_host
->script
, &state
);
189 WARN("GetScriptState failed: %08x\n", hres
);
190 else if(state
!= SCRIPTSTATE_INITIALIZED
)
191 FIXME("state = %x\n", state
);
193 hres
= IActiveScript_SetScriptState(script_host
->script
, SCRIPTSTATE_STARTED
);
195 WARN("Starting script failed: %08x\n", hres
);
199 hres
= IActiveScript_AddNamedItem(script_host
->script
, L
"window",
200 SCRIPTITEM_ISVISIBLE
|SCRIPTITEM_ISSOURCE
|SCRIPTITEM_GLOBALMEMBERS
);
201 if(SUCCEEDED(hres
)) {
202 V_VT(&var
) = VT_BOOL
;
203 V_BOOL(&var
) = VARIANT_TRUE
;
204 set_script_prop(script_host
, SCRIPTPROP_ABBREVIATE_GLOBALNAME_RESOLUTION
, &var
);
206 WARN("AddNamedItem failed: %08x\n", hres
);
209 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptParseProcedure2
,
210 (void**)&script_host
->parse_proc
);
212 /* FIXME: QI for IActiveScriptParseProcedure */
213 WARN("Could not get IActiveScriptParseProcedure iface: %08x\n", hres
);
219 static void release_script_engine(ScriptHost
*This
)
224 switch(This
->script_state
) {
225 case SCRIPTSTATE_CONNECTED
:
226 IActiveScript_SetScriptState(This
->script
, SCRIPTSTATE_DISCONNECTED
);
228 case SCRIPTSTATE_STARTED
:
229 case SCRIPTSTATE_DISCONNECTED
:
230 case SCRIPTSTATE_INITIALIZED
:
231 IActiveScript_Close(This
->script
);
234 if(This
->parse_proc
) {
235 IActiveScriptParseProcedure2_Release(This
->parse_proc
);
236 This
->parse_proc
= NULL
;
240 IActiveScriptParse_Release(This
->parse
);
245 IActiveScript_Release(This
->script
);
247 This
->script_state
= SCRIPTSTATE_UNINITIALIZED
;
250 void connect_scripts(HTMLInnerWindow
*window
)
254 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
255 if(iter
->script_state
== SCRIPTSTATE_STARTED
)
256 IActiveScript_SetScriptState(iter
->script
, SCRIPTSTATE_CONNECTED
);
260 static inline ScriptHost
*impl_from_IActiveScriptSite(IActiveScriptSite
*iface
)
262 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSite_iface
);
265 static HRESULT WINAPI
ActiveScriptSite_QueryInterface(IActiveScriptSite
*iface
, REFIID riid
, void **ppv
)
267 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
271 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
272 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
273 *ppv
= &This
->IActiveScriptSite_iface
;
274 }else if(IsEqualGUID(&IID_IActiveScriptSite
, riid
)) {
275 TRACE("(%p)->(IID_IActiveScriptSite %p)\n", This
, ppv
);
276 *ppv
= &This
->IActiveScriptSite_iface
;
277 }else if(IsEqualGUID(&IID_IActiveScriptSiteInterruptPoll
, riid
)) {
278 TRACE("(%p)->(IID_IActiveScriptSiteInterruprtPoll %p)\n", This
, ppv
);
279 *ppv
= &This
->IActiveScriptSiteInterruptPoll_iface
;
280 }else if(IsEqualGUID(&IID_IActiveScriptSiteWindow
, riid
)) {
281 TRACE("(%p)->(IID_IActiveScriptSiteWindow %p)\n", This
, ppv
);
282 *ppv
= &This
->IActiveScriptSiteWindow_iface
;
283 }else if(IsEqualGUID(&IID_IActiveScriptSiteUIControl
, riid
)) {
284 TRACE("(%p)->(IID_IActiveScriptSiteUIControl %p)\n", This
, ppv
);
285 *ppv
= &This
->IActiveScriptSiteUIControl_iface
;
286 }else if(IsEqualGUID(&IID_IActiveScriptSiteDebug
, riid
)) {
287 TRACE("(%p)->(IID_IActiveScriptSiteDebug %p)\n", This
, ppv
);
288 *ppv
= &This
->IActiveScriptSiteDebug_iface
;
289 }else if(IsEqualGUID(&IID_IServiceProvider
, riid
)) {
290 TRACE("(%p)->(IID_IServiceProvider %p)\n", This
, ppv
);
291 *ppv
= &This
->IServiceProvider_iface
;
292 }else if(IsEqualGUID(&IID_ICanHandleException
, riid
)) {
293 TRACE("(%p)->(IID_ICanHandleException not supported %p)\n", This
, ppv
);
294 return E_NOINTERFACE
;
296 FIXME("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
297 return E_NOINTERFACE
;
300 IUnknown_AddRef((IUnknown
*)*ppv
);
304 static ULONG WINAPI
ActiveScriptSite_AddRef(IActiveScriptSite
*iface
)
306 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
307 LONG ref
= InterlockedIncrement(&This
->ref
);
309 TRACE("(%p) ref=%d\n", This
, ref
);
314 static ULONG WINAPI
ActiveScriptSite_Release(IActiveScriptSite
*iface
)
316 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
317 LONG ref
= InterlockedDecrement(&This
->ref
);
319 TRACE("(%p) ref=%d\n", This
, ref
);
322 release_script_engine(This
);
324 list_remove(&This
->entry
);
331 static HRESULT WINAPI
ActiveScriptSite_GetLCID(IActiveScriptSite
*iface
, LCID
*plcid
)
333 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
335 TRACE("(%p)->(%p)\n", This
, plcid
);
337 *plcid
= GetUserDefaultLCID();
341 static HRESULT WINAPI
ActiveScriptSite_GetItemInfo(IActiveScriptSite
*iface
, LPCOLESTR pstrName
,
342 DWORD dwReturnMask
, IUnknown
**ppiunkItem
, ITypeInfo
**ppti
)
344 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
346 TRACE("(%p)->(%s %x %p %p)\n", This
, debugstr_w(pstrName
), dwReturnMask
, ppiunkItem
, ppti
);
348 if(dwReturnMask
!= SCRIPTINFO_IUNKNOWN
) {
349 FIXME("Unsupported mask %x\n", dwReturnMask
);
355 if(wcscmp(pstrName
, L
"window"))
356 return DISP_E_MEMBERNOTFOUND
;
361 /* FIXME: Return proxy object */
362 *ppiunkItem
= (IUnknown
*)&This
->window
->base
.IHTMLWindow2_iface
;
363 IUnknown_AddRef(*ppiunkItem
);
368 static HRESULT WINAPI
ActiveScriptSite_GetDocVersionString(IActiveScriptSite
*iface
, BSTR
*pbstrVersion
)
370 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
371 FIXME("(%p)->(%p)\n", This
, pbstrVersion
);
375 static HRESULT WINAPI
ActiveScriptSite_OnScriptTerminate(IActiveScriptSite
*iface
,
376 const VARIANT
*pvarResult
, const EXCEPINFO
*pexcepinfo
)
378 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
379 FIXME("(%p)->(%p %p)\n", This
, pvarResult
, pexcepinfo
);
383 static HRESULT WINAPI
ActiveScriptSite_OnStateChange(IActiveScriptSite
*iface
, SCRIPTSTATE ssScriptState
)
385 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
387 TRACE("(%p)->(%x)\n", This
, ssScriptState
);
389 This
->script_state
= ssScriptState
;
393 static HRESULT WINAPI
ActiveScriptSite_OnScriptError(IActiveScriptSite
*iface
, IActiveScriptError
*pscripterror
)
395 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
396 FIXME("(%p)->(%p)\n", This
, pscripterror
);
400 static HRESULT WINAPI
ActiveScriptSite_OnEnterScript(IActiveScriptSite
*iface
)
402 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
404 TRACE("(%p)->()\n", This
);
409 static HRESULT WINAPI
ActiveScriptSite_OnLeaveScript(IActiveScriptSite
*iface
)
411 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
413 TRACE("(%p)->()\n", This
);
418 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl
= {
419 ActiveScriptSite_QueryInterface
,
420 ActiveScriptSite_AddRef
,
421 ActiveScriptSite_Release
,
422 ActiveScriptSite_GetLCID
,
423 ActiveScriptSite_GetItemInfo
,
424 ActiveScriptSite_GetDocVersionString
,
425 ActiveScriptSite_OnScriptTerminate
,
426 ActiveScriptSite_OnStateChange
,
427 ActiveScriptSite_OnScriptError
,
428 ActiveScriptSite_OnEnterScript
,
429 ActiveScriptSite_OnLeaveScript
432 static inline ScriptHost
*impl_from_IActiveScriptSiteInterruptPoll(IActiveScriptSiteInterruptPoll
*iface
)
434 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteInterruptPoll_iface
);
437 static HRESULT WINAPI
ActiveScriptSiteInterruptPoll_QueryInterface(IActiveScriptSiteInterruptPoll
*iface
,
438 REFIID riid
, void **ppv
)
440 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
441 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
444 static ULONG WINAPI
ActiveScriptSiteInterruptPoll_AddRef(IActiveScriptSiteInterruptPoll
*iface
)
446 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
447 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
450 static ULONG WINAPI
ActiveScriptSiteInterruptPoll_Release(IActiveScriptSiteInterruptPoll
*iface
)
452 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
453 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
456 static HRESULT WINAPI
ActiveScriptSiteInterruptPoll_QueryContinue(IActiveScriptSiteInterruptPoll
*iface
)
458 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
460 TRACE("(%p)\n", This
);
465 static const IActiveScriptSiteInterruptPollVtbl ActiveScriptSiteInterruptPollVtbl
= {
466 ActiveScriptSiteInterruptPoll_QueryInterface
,
467 ActiveScriptSiteInterruptPoll_AddRef
,
468 ActiveScriptSiteInterruptPoll_Release
,
469 ActiveScriptSiteInterruptPoll_QueryContinue
472 static inline ScriptHost
*impl_from_IActiveScriptSiteWindow(IActiveScriptSiteWindow
*iface
)
474 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteWindow_iface
);
477 static HRESULT WINAPI
ActiveScriptSiteWindow_QueryInterface(IActiveScriptSiteWindow
*iface
,
478 REFIID riid
, void **ppv
)
480 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
481 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
484 static ULONG WINAPI
ActiveScriptSiteWindow_AddRef(IActiveScriptSiteWindow
*iface
)
486 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
487 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
490 static ULONG WINAPI
ActiveScriptSiteWindow_Release(IActiveScriptSiteWindow
*iface
)
492 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
493 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
496 static HRESULT WINAPI
ActiveScriptSiteWindow_GetWindow(IActiveScriptSiteWindow
*iface
, HWND
*phwnd
)
498 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
500 TRACE("(%p)->(%p)\n", This
, phwnd
);
502 if(!This
->window
|| !This
->window
->base
.outer_window
)
505 *phwnd
= This
->window
->base
.outer_window
->browser
->doc
->hwnd
;
509 static HRESULT WINAPI
ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow
*iface
, BOOL fEnable
)
511 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
512 FIXME("(%p)->(%x)\n", This
, fEnable
);
516 static const IActiveScriptSiteWindowVtbl ActiveScriptSiteWindowVtbl
= {
517 ActiveScriptSiteWindow_QueryInterface
,
518 ActiveScriptSiteWindow_AddRef
,
519 ActiveScriptSiteWindow_Release
,
520 ActiveScriptSiteWindow_GetWindow
,
521 ActiveScriptSiteWindow_EnableModeless
524 static inline ScriptHost
*impl_from_IActiveScriptSiteUIControl(IActiveScriptSiteUIControl
*iface
)
526 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteUIControl_iface
);
529 static HRESULT WINAPI
ActiveScriptSiteUIControl_QueryInterface(IActiveScriptSiteUIControl
*iface
, REFIID riid
, void **ppv
)
531 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
532 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
535 static ULONG WINAPI
ActiveScriptSiteUIControl_AddRef(IActiveScriptSiteUIControl
*iface
)
537 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
538 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
541 static ULONG WINAPI
ActiveScriptSiteUIControl_Release(IActiveScriptSiteUIControl
*iface
)
543 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
544 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
547 static HRESULT WINAPI
ActiveScriptSiteUIControl_GetUIBehavior(IActiveScriptSiteUIControl
*iface
, SCRIPTUICITEM UicItem
,
548 SCRIPTUICHANDLING
*pUicHandling
)
550 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
552 WARN("(%p)->(%d %p) semi-stub\n", This
, UicItem
, pUicHandling
);
554 *pUicHandling
= SCRIPTUICHANDLING_ALLOW
;
558 static const IActiveScriptSiteUIControlVtbl ActiveScriptSiteUIControlVtbl
= {
559 ActiveScriptSiteUIControl_QueryInterface
,
560 ActiveScriptSiteUIControl_AddRef
,
561 ActiveScriptSiteUIControl_Release
,
562 ActiveScriptSiteUIControl_GetUIBehavior
565 static inline ScriptHost
*impl_from_IActiveScriptSiteDebug(IActiveScriptSiteDebug
*iface
)
567 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteDebug_iface
);
570 static HRESULT WINAPI
ActiveScriptSiteDebug_QueryInterface(IActiveScriptSiteDebug
*iface
,
571 REFIID riid
, void **ppv
)
573 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
574 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
577 static ULONG WINAPI
ActiveScriptSiteDebug_AddRef(IActiveScriptSiteDebug
*iface
)
579 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
580 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
583 static ULONG WINAPI
ActiveScriptSiteDebug_Release(IActiveScriptSiteDebug
*iface
)
585 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
586 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
589 static HRESULT WINAPI
ActiveScriptSiteDebug_GetDocumentContextFromPosition(IActiveScriptSiteDebug
*iface
,
590 CTXARG_T dwSourceContext
, ULONG uCharacterOffset
, ULONG uNumChars
, IDebugDocumentContext
**ppsc
)
592 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
593 FIXME("(%p)->(%s %u %u %p)\n", This
, wine_dbgstr_longlong(dwSourceContext
), uCharacterOffset
,
598 static HRESULT WINAPI
ActiveScriptSiteDebug_GetApplication(IActiveScriptSiteDebug
*iface
, IDebugApplication
**ppda
)
600 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
601 FIXME("(%p)->(%p)\n", This
, ppda
);
605 static HRESULT WINAPI
ActiveScriptSiteDebug_GetRootApplicationNode(IActiveScriptSiteDebug
*iface
,
606 IDebugApplicationNode
**ppdanRoot
)
608 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
609 FIXME("(%p)->(%p)\n", This
, ppdanRoot
);
613 static HRESULT WINAPI
ActiveScriptSiteDebug_OnScriptErrorDebug(IActiveScriptSiteDebug
*iface
,
614 IActiveScriptErrorDebug
*pErrorDebug
, BOOL
*pfEnterDebugger
, BOOL
*pfCallOnScriptErrorWhenContinuing
)
616 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
617 FIXME("(%p)->(%p %p %p)\n", This
, pErrorDebug
, pfEnterDebugger
, pfCallOnScriptErrorWhenContinuing
);
621 static const IActiveScriptSiteDebugVtbl ActiveScriptSiteDebugVtbl
= {
622 ActiveScriptSiteDebug_QueryInterface
,
623 ActiveScriptSiteDebug_AddRef
,
624 ActiveScriptSiteDebug_Release
,
625 ActiveScriptSiteDebug_GetDocumentContextFromPosition
,
626 ActiveScriptSiteDebug_GetApplication
,
627 ActiveScriptSiteDebug_GetRootApplicationNode
,
628 ActiveScriptSiteDebug_OnScriptErrorDebug
631 static inline ScriptHost
*impl_from_IServiceProvider(IServiceProvider
*iface
)
633 return CONTAINING_RECORD(iface
, ScriptHost
, IServiceProvider_iface
);
636 static HRESULT WINAPI
ASServiceProvider_QueryInterface(IServiceProvider
*iface
, REFIID riid
, void **ppv
)
638 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
639 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
642 static ULONG WINAPI
ASServiceProvider_AddRef(IServiceProvider
*iface
)
644 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
645 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
648 static ULONG WINAPI
ASServiceProvider_Release(IServiceProvider
*iface
)
650 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
651 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
654 static HRESULT WINAPI
ASServiceProvider_QueryService(IServiceProvider
*iface
, REFGUID guidService
,
655 REFIID riid
, void **ppv
)
657 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
659 if(IsEqualGUID(&SID_SInternetHostSecurityManager
, guidService
)) {
660 TRACE("(%p)->(SID_SInternetHostSecurityManager)\n", This
);
662 if(!This
->window
|| !This
->window
->doc
)
663 return E_NOINTERFACE
;
665 return IInternetHostSecurityManager_QueryInterface(&This
->window
->doc
->IInternetHostSecurityManager_iface
,
669 if(IsEqualGUID(&SID_SContainerDispatch
, guidService
)) {
670 TRACE("(%p)->(SID_SContainerDispatch)\n", This
);
672 if(!This
->window
|| !This
->window
->doc
)
673 return E_NOINTERFACE
;
675 return IHTMLDocument2_QueryInterface(&This
->window
->doc
->basedoc
.IHTMLDocument2_iface
, riid
, ppv
);
678 FIXME("(%p)->(%s %s %p)\n", This
, debugstr_guid(guidService
), debugstr_guid(riid
), ppv
);
679 return E_NOINTERFACE
;
682 static const IServiceProviderVtbl ASServiceProviderVtbl
= {
683 ASServiceProvider_QueryInterface
,
684 ASServiceProvider_AddRef
,
685 ASServiceProvider_Release
,
686 ASServiceProvider_QueryService
689 static ScriptHost
*create_script_host(HTMLInnerWindow
*window
, const GUID
*guid
)
694 ret
= heap_alloc_zero(sizeof(*ret
));
698 ret
->IActiveScriptSite_iface
.lpVtbl
= &ActiveScriptSiteVtbl
;
699 ret
->IActiveScriptSiteInterruptPoll_iface
.lpVtbl
= &ActiveScriptSiteInterruptPollVtbl
;
700 ret
->IActiveScriptSiteWindow_iface
.lpVtbl
= &ActiveScriptSiteWindowVtbl
;
701 ret
->IActiveScriptSiteUIControl_iface
.lpVtbl
= &ActiveScriptSiteUIControlVtbl
;
702 ret
->IActiveScriptSiteDebug_iface
.lpVtbl
= &ActiveScriptSiteDebugVtbl
;
703 ret
->IServiceProvider_iface
.lpVtbl
= &ASServiceProviderVtbl
;
705 ret
->window
= window
;
706 ret
->script_state
= SCRIPTSTATE_UNINITIALIZED
;
709 list_add_tail(&window
->script_hosts
, &ret
->entry
);
711 hres
= CoCreateInstance(&ret
->guid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
712 &IID_IActiveScript
, (void**)&ret
->script
);
714 WARN("Could not load script engine: %08x\n", hres
);
715 else if(!init_script_engine(ret
))
716 release_script_engine(ret
);
721 static void dispatch_script_readystatechange_event(HTMLScriptElement
*script
)
726 if(script
->readystate
!= READYSTATE_LOADED
||
727 dispex_compat_mode(&script
->element
.node
.event_target
.dispex
) < COMPAT_MODE_IE10
) {
728 hres
= create_document_event(script
->element
.node
.doc
, EVENTID_READYSTATECHANGE
, &event
);
729 if(SUCCEEDED(hres
)) {
730 dispatch_event(&script
->element
.node
.event_target
, event
);
731 IDOMEvent_Release(&event
->IDOMEvent_iface
);
735 if(script
->readystate
== READYSTATE_LOADED
) {
736 hres
= create_document_event(script
->element
.node
.doc
, EVENTID_LOAD
, &event
);
737 if(SUCCEEDED(hres
)) {
738 dispatch_event(&script
->element
.node
.event_target
, event
);
739 IDOMEvent_Release(&event
->IDOMEvent_iface
);
746 HTMLScriptElement
*elem
;
747 } fire_readystatechange_task_t
;
749 static void fire_readystatechange_proc(task_t
*_task
)
751 fire_readystatechange_task_t
*task
= (fire_readystatechange_task_t
*)_task
;
753 if(!task
->elem
->pending_readystatechange_event
)
756 task
->elem
->pending_readystatechange_event
= FALSE
;
757 dispatch_script_readystatechange_event(task
->elem
);
760 static void fire_readystatechange_task_destr(task_t
*_task
)
762 fire_readystatechange_task_t
*task
= (fire_readystatechange_task_t
*)_task
;
764 IHTMLScriptElement_Release(&task
->elem
->IHTMLScriptElement_iface
);
767 static void set_script_elem_readystate(HTMLScriptElement
*script_elem
, READYSTATE readystate
)
769 script_elem
->readystate
= readystate
;
771 if(readystate
!= READYSTATE_LOADED
&&
772 dispex_compat_mode(&script_elem
->element
.node
.event_target
.dispex
) >= COMPAT_MODE_IE11
)
775 if(readystate
!= READYSTATE_INTERACTIVE
) {
776 if(!script_elem
->element
.node
.doc
->window
->parser_callback_cnt
) {
777 fire_readystatechange_task_t
*task
;
780 if(script_elem
->pending_readystatechange_event
)
783 task
= heap_alloc(sizeof(*task
));
787 IHTMLScriptElement_AddRef(&script_elem
->IHTMLScriptElement_iface
);
788 task
->elem
= script_elem
;
790 hres
= push_task(&task
->header
, fire_readystatechange_proc
, fire_readystatechange_task_destr
,
791 script_elem
->element
.node
.doc
->window
->task_magic
);
793 script_elem
->pending_readystatechange_event
= TRUE
;
795 script_elem
->pending_readystatechange_event
= FALSE
;
796 dispatch_script_readystatechange_event(script_elem
);
801 static void parse_elem_text(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
, LPCWSTR text
)
807 TRACE("%s\n", debugstr_w(text
));
810 memset(&excepinfo
, 0, sizeof(excepinfo
));
812 hres
= IActiveScriptParse_ParseScriptText(script_host
->parse
, text
, L
"window", NULL
, L
"</SCRIPT>",
813 0, 0, SCRIPTTEXT_ISVISIBLE
|SCRIPTTEXT_HOSTMANAGESSOURCE
,
818 WARN("<<< %08x\n", hres
);
824 HTMLScriptElement
*script_elem
;
825 nsILoadGroup
*load_group
;
833 static HRESULT
get_binding_text(ScriptBSC
*bsc
, WCHAR
**ret
)
839 text
= heap_alloc(sizeof(WCHAR
));
841 return E_OUTOFMEMORY
;
847 switch(bsc
->bsc
.bom
) {
849 if(bsc
->bsc
.read
% sizeof(WCHAR
)) {
850 FIXME("The buffer is not a valid utf16 string\n");
854 text
= heap_alloc(bsc
->bsc
.read
+sizeof(WCHAR
));
856 return E_OUTOFMEMORY
;
858 memcpy(text
, bsc
->buf
, bsc
->bsc
.read
);
859 text
[bsc
->bsc
.read
/sizeof(WCHAR
)] = 0;
863 /* FIXME: Try to use charset from HTTP headers first */
864 cp
= get_document_charset(bsc
->script_elem
->element
.node
.doc
);
869 len
= MultiByteToWideChar(cp
, 0, bsc
->buf
, bsc
->bsc
.read
, NULL
, 0);
870 text
= heap_alloc((len
+1)*sizeof(WCHAR
));
872 return E_OUTOFMEMORY
;
874 MultiByteToWideChar(cp
, 0, bsc
->buf
, bsc
->bsc
.read
, text
, len
);
883 static void script_file_available(ScriptBSC
*bsc
)
885 HTMLScriptElement
*script_elem
= bsc
->script_elem
;
886 HTMLInnerWindow
*window
= bsc
->bsc
.window
;
887 ScriptHost
*script_host
;
892 assert(window
!= NULL
);
894 hres
= get_binding_text(bsc
, &script_elem
->src_text
);
898 script_host
= get_elem_script_host(window
, script_elem
);
902 if(window
->parser_callback_cnt
) {
903 script_queue_entry_t
*queue
;
905 TRACE("Adding to queue\n");
907 queue
= heap_alloc(sizeof(*queue
));
911 IHTMLScriptElement_AddRef(&script_elem
->IHTMLScriptElement_iface
);
912 queue
->script
= script_elem
;
914 list_add_tail(&window
->script_queue
, &queue
->entry
);
918 nsres
= nsIDOMElement_GetParentNode(script_elem
->element
.dom_element
, &parent
);
919 if(NS_FAILED(nsres
) || !parent
) {
920 TRACE("No parent, not executing\n");
921 script_elem
->parse_on_bind
= TRUE
;
925 nsIDOMNode_Release(parent
);
927 script_host
= get_elem_script_host(window
, script_elem
);
931 script_elem
->parsed
= TRUE
;
932 if(script_host
->parse
)
933 parse_elem_text(script_host
, script_elem
, script_elem
->src_text
);
936 static inline ScriptBSC
*impl_from_BSCallback(BSCallback
*iface
)
938 return CONTAINING_RECORD(iface
, ScriptBSC
, bsc
);
941 static void ScriptBSC_destroy(BSCallback
*bsc
)
943 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
945 if(This
->script_elem
) {
946 IHTMLScriptElement_Release(&This
->script_elem
->IHTMLScriptElement_iface
);
947 This
->script_elem
= NULL
;
951 ERR("Unfinished request\n");
952 nsIRequest_Release(This
->request
);
955 nsILoadGroup_Release(This
->load_group
);
957 heap_free(This
->buf
);
961 static HRESULT
ScriptBSC_init_bindinfo(BSCallback
*bsc
)
966 static HRESULT
ScriptBSC_start_binding(BSCallback
*bsc
)
968 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
971 This
->script_elem
->binding
= &This
->bsc
;
973 if(This
->load_group
) {
974 nsres
= create_onload_blocker_request(&This
->request
);
975 if(NS_SUCCEEDED(nsres
)) {
976 nsres
= nsILoadGroup_AddRequest(This
->load_group
, This
->request
, NULL
);
978 ERR("AddRequest failed: %08x\n", nsres
);
982 /* FIXME: We should find a better to decide if 'loading' state is supposed to be used by the protocol. */
983 if(This
->scheme
== URL_SCHEME_HTTPS
|| This
->scheme
== URL_SCHEME_HTTP
)
984 set_script_elem_readystate(This
->script_elem
, READYSTATE_LOADING
);
989 static HRESULT
ScriptBSC_stop_binding(BSCallback
*bsc
, HRESULT result
)
991 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
994 if(SUCCEEDED(result
) && !This
->script_elem
)
995 result
= E_UNEXPECTED
;
997 assert(FAILED(result
) || This
->script_elem
->binding
== &This
->bsc
);
998 This
->script_elem
->binding
= NULL
;
1000 if(This
->script_elem
->readystate
== READYSTATE_LOADING
)
1001 set_script_elem_readystate(This
->script_elem
, READYSTATE_LOADED
);
1003 if(SUCCEEDED(result
)) {
1004 script_file_available(This
);
1006 FIXME("binding failed %08x\n", result
);
1007 heap_free(This
->buf
);
1013 nsres
= nsILoadGroup_RemoveRequest(This
->load_group
, This
->request
, NULL
, NS_OK
);
1014 if(NS_FAILED(nsres
))
1015 ERR("RemoveRequest failed: %08x\n", nsres
);
1016 nsIRequest_Release(This
->request
);
1017 This
->request
= NULL
;
1020 IHTMLScriptElement_Release(&This
->script_elem
->IHTMLScriptElement_iface
);
1021 This
->script_elem
= NULL
;
1025 static HRESULT
ScriptBSC_read_data(BSCallback
*bsc
, IStream
*stream
)
1027 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
1032 This
->buf
= heap_alloc(128);
1034 return E_OUTOFMEMORY
;
1039 if(This
->bsc
.read
>= This
->size
) {
1041 new_buf
= heap_realloc(This
->buf
, This
->size
<< 1);
1043 return E_OUTOFMEMORY
;
1045 This
->buf
= new_buf
;
1048 hres
= read_stream(&This
->bsc
, stream
, This
->buf
+This
->bsc
.read
, This
->size
-This
->bsc
.read
, &read
);
1049 }while(hres
== S_OK
);
1054 static HRESULT
ScriptBSC_on_progress(BSCallback
*bsc
, ULONG status_code
, LPCWSTR status_text
)
1059 static HRESULT
ScriptBSC_on_response(BSCallback
*bsc
, DWORD response_code
,
1060 LPCWSTR response_headers
)
1065 static HRESULT
ScriptBSC_beginning_transaction(BSCallback
*bsc
, WCHAR
**additional_headers
)
1070 static const BSCallbackVtbl ScriptBSCVtbl
= {
1072 ScriptBSC_init_bindinfo
,
1073 ScriptBSC_start_binding
,
1074 ScriptBSC_stop_binding
,
1075 ScriptBSC_read_data
,
1076 ScriptBSC_on_progress
,
1077 ScriptBSC_on_response
,
1078 ScriptBSC_beginning_transaction
1082 HRESULT
load_script(HTMLScriptElement
*script_elem
, const WCHAR
*src
, BOOL async
)
1084 HTMLInnerWindow
*window
;
1090 static const WCHAR wine_schemaW
[] = {'w','i','n','e',':'};
1092 if(lstrlenW(src
) > ARRAY_SIZE(wine_schemaW
) && !memcmp(src
, wine_schemaW
, sizeof(wine_schemaW
)))
1093 src
+= ARRAY_SIZE(wine_schemaW
);
1095 TRACE("(%p %s %x)\n", script_elem
, debugstr_w(src
), async
);
1097 if(!script_elem
->element
.node
.doc
|| !(window
= script_elem
->element
.node
.doc
->window
)) {
1099 return E_UNEXPECTED
;
1102 hres
= create_uri(src
, 0, &uri
);
1106 hres
= CreateURLMonikerEx2(NULL
, uri
, &mon
, URL_MK_UNIFORM
);
1112 bsc
= heap_alloc_zero(sizeof(*bsc
));
1114 IMoniker_Release(mon
);
1116 return E_OUTOFMEMORY
;
1119 init_bscallback(&bsc
->bsc
, &ScriptBSCVtbl
, mon
, async
? BINDF_ASYNCHRONOUS
| BINDF_ASYNCSTORAGE
| BINDF_PULLDATA
: 0);
1120 IMoniker_Release(mon
);
1122 hres
= IUri_GetScheme(uri
, &bsc
->scheme
);
1125 bsc
->scheme
= URL_SCHEME_UNKNOWN
;
1127 IHTMLScriptElement_AddRef(&script_elem
->IHTMLScriptElement_iface
);
1128 bsc
->script_elem
= script_elem
;
1130 if(window
->bscallback
&& window
->bscallback
->nschannel
&&
1131 window
->bscallback
->nschannel
->load_group
) {
1135 nsres
= nsIDOMNode_Contains(script_elem
->element
.node
.doc
->node
.nsnode
,
1136 script_elem
->element
.node
.nsnode
, &contains
);
1137 if(NS_SUCCEEDED(nsres
) && contains
) {
1138 TRACE("script %p will block load event\n", script_elem
);
1139 bsc
->load_group
= window
->bscallback
->nschannel
->load_group
;
1140 nsILoadGroup_AddRef(bsc
->load_group
);
1144 hres
= start_binding(window
, &bsc
->bsc
, NULL
);
1146 IBindStatusCallback_Release(&bsc
->bsc
.IBindStatusCallback_iface
);
1150 static void parse_inline_script(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
)
1152 const PRUnichar
*text
;
1156 nsAString_Init(&text_str
, NULL
);
1157 nsres
= nsIDOMHTMLScriptElement_GetText(script_elem
->nsscript
, &text_str
);
1158 nsAString_GetData(&text_str
, &text
);
1160 set_script_elem_readystate(script_elem
, READYSTATE_INTERACTIVE
);
1162 if(NS_FAILED(nsres
)) {
1163 ERR("GetText failed: %08x\n", nsres
);
1165 parse_elem_text(script_host
, script_elem
, text
);
1168 nsAString_Finish(&text_str
);
1171 static BOOL
parse_script_elem(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
)
1173 nsAString src_str
, event_str
;
1174 BOOL is_complete
= FALSE
;
1175 const PRUnichar
*src
;
1178 nsAString_Init(&event_str
, NULL
);
1179 nsres
= nsIDOMHTMLScriptElement_GetEvent(script_elem
->nsscript
, &event_str
);
1180 if(NS_SUCCEEDED(nsres
)) {
1181 const PRUnichar
*event
;
1183 nsAString_GetData(&event_str
, &event
);
1185 TRACE("deferring event %s script evaluation\n", debugstr_w(event
));
1186 nsAString_Finish(&event_str
);
1190 ERR("GetEvent failed: %08x\n", nsres
);
1192 nsAString_Finish(&event_str
);
1194 nsAString_Init(&src_str
, NULL
);
1195 nsres
= nsIDOMHTMLScriptElement_GetSrc(script_elem
->nsscript
, &src_str
);
1196 nsAString_GetData(&src_str
, &src
);
1198 if(NS_FAILED(nsres
)) {
1199 ERR("GetSrc failed: %08x\n", nsres
);
1201 load_script(script_elem
, src
, FALSE
);
1202 is_complete
= script_elem
->parsed
;
1204 parse_inline_script(script_host
, script_elem
);
1208 nsAString_Finish(&src_str
);
1213 static GUID
get_default_script_guid(HTMLInnerWindow
*window
)
1215 /* If not specified, we should use very first script host that was created for the page (or JScript if none) */
1216 return list_empty(&window
->script_hosts
)
1218 : LIST_ENTRY(list_head(&window
->script_hosts
), ScriptHost
, entry
)->guid
;
1221 static BOOL
get_guid_from_type(LPCWSTR type
, GUID
*guid
)
1223 /* FIXME: Handle more types */
1224 if(!wcsicmp(type
, L
"text/javascript") || !wcsicmp(type
, L
"text/jscript")) {
1225 *guid
= CLSID_JScript
;
1226 }else if(!wcsicmp(type
, L
"text/vbscript")) {
1227 *guid
= CLSID_VBScript
;
1229 FIXME("Unknown type %s\n", debugstr_w(type
));
1236 static BOOL
get_guid_from_language(LPCWSTR type
, GUID
*guid
)
1240 hres
= CLSIDFromProgID(type
, guid
);
1244 /* FIXME: Check CATID_ActiveScriptParse */
1249 static BOOL
get_script_guid(HTMLInnerWindow
*window
, nsIDOMHTMLScriptElement
*nsscript
, GUID
*guid
)
1251 nsIDOMElement
*nselem
;
1252 const PRUnichar
*language
;
1257 nsAString_Init(&val_str
, NULL
);
1259 nsres
= nsIDOMHTMLScriptElement_GetType(nsscript
, &val_str
);
1260 if(NS_SUCCEEDED(nsres
)) {
1261 const PRUnichar
*type
;
1263 nsAString_GetData(&val_str
, &type
);
1265 ret
= get_guid_from_type(type
, guid
);
1266 nsAString_Finish(&val_str
);
1270 ERR("GetType failed: %08x\n", nsres
);
1273 nsres
= nsIDOMHTMLScriptElement_QueryInterface(nsscript
, &IID_nsIDOMElement
, (void**)&nselem
);
1274 assert(nsres
== NS_OK
);
1276 nsres
= get_elem_attr_value(nselem
, L
"language", &val_str
, &language
);
1277 nsIDOMElement_Release(nselem
);
1278 if(NS_SUCCEEDED(nsres
)) {
1280 ret
= get_guid_from_language(language
, guid
);
1282 *guid
= get_default_script_guid(window
);
1285 nsAString_Finish(&val_str
);
1291 static ScriptHost
*get_script_host(HTMLInnerWindow
*window
, const GUID
*guid
)
1295 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
1296 if(IsEqualGUID(guid
, &iter
->guid
))
1300 return create_script_host(window
, guid
);
1303 static ScriptHost
*get_elem_script_host(HTMLInnerWindow
*window
, HTMLScriptElement
*script_elem
)
1307 if(!get_script_guid(window
, script_elem
->nsscript
, &guid
)) {
1308 WARN("Could not find script GUID\n");
1312 if(IsEqualGUID(&CLSID_JScript
, &guid
) && (!window
->doc
->browser
|| window
->doc
->browser
->script_mode
!= SCRIPTMODE_ACTIVESCRIPT
)) {
1313 TRACE("Ignoring JScript\n");
1317 return get_script_host(window
, &guid
);
1320 void doc_insert_script(HTMLInnerWindow
*window
, HTMLScriptElement
*script_elem
, BOOL from_parser
)
1322 ScriptHost
*script_host
;
1323 BOOL is_complete
= FALSE
;
1325 script_host
= get_elem_script_host(window
, script_elem
);
1329 if(script_host
->parse
) {
1330 if(script_elem
->src_text
) {
1332 set_script_elem_readystate(script_elem
, READYSTATE_INTERACTIVE
);
1333 script_elem
->parsed
= TRUE
;
1334 parse_elem_text(script_host
, script_elem
, script_elem
->src_text
);
1336 }else if(!script_elem
->binding
) {
1337 is_complete
= parse_script_elem(script_host
, script_elem
);
1342 set_script_elem_readystate(script_elem
, READYSTATE_COMPLETE
);
1345 IDispatch
*script_parse_event(HTMLInnerWindow
*window
, LPCWSTR text
)
1347 ScriptHost
*script_host
;
1353 TRACE("%s\n", debugstr_w(text
));
1355 for(ptr
= text
; iswalnum(*ptr
); ptr
++);
1360 language
= heap_alloc((ptr
-text
+1)*sizeof(WCHAR
));
1364 memcpy(language
, text
, (ptr
-text
)*sizeof(WCHAR
));
1365 language
[ptr
-text
] = 0;
1367 b
= get_guid_from_language(language
, &guid
);
1369 heap_free(language
);
1372 WARN("Could not find language\n");
1379 guid
= get_default_script_guid(window
);
1382 if(IsEqualGUID(&CLSID_JScript
, &guid
)
1383 && (!window
->doc
->browser
|| window
->doc
->browser
->script_mode
!= SCRIPTMODE_ACTIVESCRIPT
)) {
1384 TRACE("Ignoring JScript\n");
1388 script_host
= get_script_host(window
, &guid
);
1389 if(!script_host
|| !script_host
->parse_proc
)
1392 hres
= IActiveScriptParseProcedure2_ParseProcedureText(script_host
->parse_proc
, ptr
, NULL
, L
"",
1393 NULL
, NULL
, L
"\"", 0 /* FIXME */, 0,
1394 SCRIPTPROC_HOSTMANAGESSOURCE
|SCRIPTPROC_IMPLICIT_THIS
|SCRIPTPROC_IMPLICIT_PARENTS
, &disp
);
1396 WARN("ParseProcedureText failed: %08x\n", hres
);
1400 TRACE("ret %p\n", disp
);
1404 HRESULT
exec_script(HTMLInnerWindow
*window
, const WCHAR
*code
, const WCHAR
*lang
, VARIANT
*ret
)
1406 ScriptHost
*script_host
;
1411 if(!get_guid_from_language(lang
, &guid
)) {
1412 WARN("Could not find script GUID\n");
1413 return CO_E_CLASSSTRING
;
1416 script_host
= get_script_host(window
, &guid
);
1418 FIXME("No script host\n");
1422 if(!script_host
->parse
) {
1423 FIXME("script_host->parse == NULL\n");
1427 memset(&ei
, 0, sizeof(ei
));
1429 hres
= IActiveScriptParse_ParseScriptText(script_host
->parse
, code
, NULL
, NULL
, L
"\"", 0, 0, SCRIPTTEXT_ISVISIBLE
, ret
, &ei
);
1433 WARN("<<< %08x\n", hres
);
1438 IDispatch
*get_script_disp(ScriptHost
*script_host
)
1443 if(!script_host
->script
)
1446 hres
= IActiveScript_GetScriptDispatch(script_host
->script
, L
"window", &disp
);
1453 static EventTarget
*find_event_target(HTMLDocumentNode
*doc
, HTMLScriptElement
*script_elem
)
1455 EventTarget
*event_target
= NULL
;
1456 const PRUnichar
*target_id
;
1457 nsAString target_id_str
;
1461 nsAString_Init(&target_id_str
, NULL
);
1462 nsres
= nsIDOMHTMLScriptElement_GetHtmlFor(script_elem
->nsscript
, &target_id_str
);
1463 if(NS_FAILED(nsres
)) {
1464 ERR("GetScriptFor failed: %08x\n", nsres
);
1465 nsAString_Finish(&target_id_str
);
1469 nsAString_GetData(&target_id_str
, &target_id
);
1471 FIXME("Empty for attribute\n");
1472 }else if(!wcscmp(target_id
, L
"document")) {
1473 event_target
= &doc
->node
.event_target
;
1474 htmldoc_addref(&doc
->basedoc
);
1475 }else if(!wcscmp(target_id
, L
"window")) {
1477 event_target
= &doc
->window
->event_target
;
1478 IDispatchEx_AddRef(&event_target
->dispex
.IDispatchEx_iface
);
1481 HTMLElement
*target_elem
;
1483 hres
= get_doc_elem_by_id(doc
, target_id
, &target_elem
);
1484 if(SUCCEEDED(hres
) && target_elem
) {
1485 event_target
= &target_elem
->node
.event_target
;
1489 nsAString_Finish(&target_id_str
);
1490 return event_target
;
1493 static BOOL
parse_event_str(WCHAR
*event
, const WCHAR
**args
)
1497 TRACE("%s\n", debugstr_w(event
));
1499 for(ptr
= event
; iswalnum(*ptr
); ptr
++);
1510 while(iswalnum(*ptr
) || iswspace(*ptr
) || *ptr
== ',')
1520 static IDispatch
*parse_event_elem(HTMLDocumentNode
*doc
, HTMLScriptElement
*script_elem
, WCHAR
**ret_event
)
1522 ScriptHost
*script_host
;
1523 WCHAR
*event
= NULL
;
1530 if(script_elem
->parsed
)
1533 script_host
= get_elem_script_host(doc
->window
, script_elem
);
1534 if(!script_host
|| !script_host
->parse_proc
)
1537 nsAString_Init(&nsstr
, NULL
);
1538 nsres
= nsIDOMHTMLScriptElement_GetEvent(script_elem
->nsscript
, &nsstr
);
1539 if(NS_SUCCEEDED(nsres
)) {
1540 const PRUnichar
*event_val
;
1542 nsAString_GetData(&nsstr
, &event_val
);
1543 event
= heap_strdupW(event_val
);
1545 nsAString_Finish(&nsstr
);
1549 if(!parse_event_str(event
, &args
)) {
1550 WARN("parsing %s failed\n", debugstr_w(event
));
1555 nsAString_Init(&nsstr
, NULL
);
1556 nsres
= nsIDOMHTMLScriptElement_GetText(script_elem
->nsscript
, &nsstr
);
1557 if(NS_SUCCEEDED(nsres
)) {
1558 const PRUnichar
*text
;
1560 nsAString_GetData(&nsstr
, &text
);
1561 hres
= IActiveScriptParseProcedure2_ParseProcedureText(script_host
->parse_proc
, text
, args
,
1562 L
"", NULL
, NULL
, L
"</SCRIPT>", 0, 0,
1563 SCRIPTPROC_HOSTMANAGESSOURCE
|SCRIPTPROC_IMPLICIT_THIS
|SCRIPTPROC_IMPLICIT_PARENTS
, &disp
);
1567 ERR("GetText failed: %08x\n", nsres
);
1570 nsAString_Finish(&nsstr
);
1580 void bind_event_scripts(HTMLDocumentNode
*doc
)
1582 HTMLPluginContainer
*plugin_container
;
1583 nsIDOMHTMLScriptElement
*nsscript
;
1584 HTMLScriptElement
*script_elem
;
1585 EventTarget
*event_target
;
1586 nsIDOMNodeList
*node_list
;
1587 nsIDOMNode
*script_node
;
1588 nsAString selector_str
;
1589 IDispatch
*event_disp
;
1600 nsAString_InitDepend(&selector_str
, L
"script[event]");
1601 nsres
= nsIDOMHTMLDocument_QuerySelectorAll(doc
->nsdoc
, &selector_str
, &node_list
);
1602 nsAString_Finish(&selector_str
);
1603 if(NS_FAILED(nsres
)) {
1604 ERR("QuerySelectorAll failed: %08x\n", nsres
);
1611 nsres
= nsIDOMNodeList_GetLength(node_list
, &length
);
1612 assert(nsres
== NS_OK
);
1614 for(i
=0; i
< length
; i
++) {
1615 nsres
= nsIDOMNodeList_Item(node_list
, i
, &script_node
);
1616 if(NS_FAILED(nsres
) || !script_node
) {
1617 ERR("Item(%d) failed: %08x\n", i
, nsres
);
1621 nsres
= nsIDOMNode_QueryInterface(script_node
, &IID_nsIDOMHTMLScriptElement
, (void**)&nsscript
);
1622 assert(nsres
== NS_OK
);
1623 nsIDOMNode_Release(script_node
);
1625 hres
= script_elem_from_nsscript(nsscript
, &script_elem
);
1629 event_disp
= parse_event_elem(doc
, script_elem
, &event
);
1631 event_target
= find_event_target(doc
, script_elem
);
1633 hres
= IDispatchEx_QueryInterface(&event_target
->dispex
.IDispatchEx_iface
, &IID_HTMLPluginContainer
,
1634 (void**)&plugin_container
);
1636 bind_activex_event(doc
, plugin_container
, event
, event_disp
);
1638 bind_target_event(doc
, event_target
, event
, event_disp
);
1640 IDispatchEx_Release(&event_target
->dispex
.IDispatchEx_iface
);
1641 if(plugin_container
)
1642 node_release(&plugin_container
->element
.node
);
1646 IDispatch_Release(event_disp
);
1649 IHTMLScriptElement_Release(&script_elem
->IHTMLScriptElement_iface
);
1652 nsIDOMNodeList_Release(node_list
);
1655 BOOL
find_global_prop(HTMLInnerWindow
*window
, BSTR name
, DWORD flags
, ScriptHost
**ret_host
, DISPID
*ret_id
)
1657 IDispatchEx
*dispex
;
1662 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
1663 disp
= get_script_disp(iter
);
1667 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1668 if(SUCCEEDED(hres
)) {
1669 hres
= IDispatchEx_GetDispID(dispex
, name
, flags
& (~fdexNameEnsure
), ret_id
);
1670 IDispatchEx_Release(dispex
);
1672 FIXME("No IDispatchEx\n");
1676 IDispatch_Release(disp
);
1677 if(SUCCEEDED(hres
)) {
1686 static BOOL
is_jscript_available(void)
1688 static BOOL available
, checked
;
1692 HRESULT hres
= CoGetClassObject(&CLSID_JScript
, CLSCTX_INPROC_SERVER
, NULL
, &IID_IUnknown
, (void**)&unk
);
1694 if(SUCCEEDED(hres
)) {
1696 IUnknown_Release(unk
);
1706 static BOOL
use_gecko_script(IUri
*uri
)
1712 hres
= IUri_GetDisplayUri(uri
, &display_uri
);
1716 hres
= IInternetSecurityManager_MapUrlToZone(get_security_manager(), display_uri
, &zone
, 0);
1717 SysFreeString(display_uri
);
1719 WARN("Could not map %s to zone: %08x\n", debugstr_w(display_uri
), hres
);
1723 TRACE("zone %d\n", zone
);
1724 return zone
== URLZONE_UNTRUSTED
;
1727 void update_browser_script_mode(GeckoBrowser
*browser
, IUri
*uri
)
1729 nsIWebBrowserSetup
*setup
;
1732 if(!is_jscript_available()) {
1733 TRACE("jscript.dll not available\n");
1734 browser
->script_mode
= SCRIPTMODE_GECKO
;
1738 browser
->script_mode
= use_gecko_script(uri
) ? SCRIPTMODE_GECKO
: SCRIPTMODE_ACTIVESCRIPT
;
1740 nsres
= nsIWebBrowser_QueryInterface(browser
->webbrowser
, &IID_nsIWebBrowserSetup
, (void**)&setup
);
1741 if(NS_SUCCEEDED(nsres
)) {
1742 nsres
= nsIWebBrowserSetup_SetProperty(setup
, SETUP_ALLOW_JAVASCRIPT
,
1743 browser
->script_mode
== SCRIPTMODE_GECKO
);
1745 if(NS_SUCCEEDED(nsres
))
1746 nsres
= nsIWebBrowserSetup_SetProperty(setup
, SETUP_DISABLE_NOSCRIPT
, TRUE
);
1748 nsIWebBrowserSetup_Release(setup
);
1751 if(NS_FAILED(nsres
))
1752 ERR("JavaScript setup failed: %08x\n", nsres
);
1755 void release_script_hosts(HTMLInnerWindow
*window
)
1757 script_queue_entry_t
*queue_iter
;
1760 while(!list_empty(&window
->script_queue
)) {
1761 queue_iter
= LIST_ENTRY(list_head(&window
->script_queue
), script_queue_entry_t
, entry
);
1763 list_remove(&queue_iter
->entry
);
1764 IHTMLScriptElement_Release(&queue_iter
->script
->IHTMLScriptElement_iface
);
1765 heap_free(queue_iter
);
1768 while(!list_empty(&window
->script_hosts
)) {
1769 iter
= LIST_ENTRY(list_head(&window
->script_hosts
), ScriptHost
, entry
);
1771 release_script_engine(iter
);
1772 list_remove(&iter
->entry
);
1773 iter
->window
= NULL
;
1774 IActiveScriptSite_Release(&iter
->IActiveScriptSite_iface
);