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
34 #include "wine/debug.h"
36 #include "mshtml_private.h"
37 #include "htmlscript.h"
38 #include "pluginhost.h"
39 #include "htmlevent.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(mshtml
);
46 #define CTXARG_T DWORDLONG
47 #define IActiveScriptSiteDebugVtbl IActiveScriptSiteDebug64Vtbl
49 #define IActiveScriptParse_Release IActiveScriptParse64_Release
50 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
51 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
52 #define IActiveScriptParseProcedure2_Release IActiveScriptParseProcedure2_64_Release
53 #define IActiveScriptParseProcedure2_ParseProcedureText IActiveScriptParseProcedure2_64_ParseProcedureText
57 #define CTXARG_T DWORD
58 #define IActiveScriptSiteDebugVtbl IActiveScriptSiteDebug32Vtbl
60 #define IActiveScriptParse_Release IActiveScriptParse32_Release
61 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
62 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
63 #define IActiveScriptParseProcedure2_Release IActiveScriptParseProcedure2_32_Release
64 #define IActiveScriptParseProcedure2_ParseProcedureText IActiveScriptParseProcedure2_32_ParseProcedureText
68 /* See jscript.h in jscript.dll. */
69 #define SCRIPTLANGUAGEVERSION_HTML 0x400
70 #define SCRIPTLANGUAGEVERSION_ES5 0x102
72 static const WCHAR documentW
[] = {'d','o','c','u','m','e','n','t',0};
73 static const WCHAR windowW
[] = {'w','i','n','d','o','w',0};
74 static const WCHAR script_endW
[] = {'<','/','S','C','R','I','P','T','>',0};
75 static const WCHAR emptyW
[] = {0};
78 IActiveScriptSite IActiveScriptSite_iface
;
79 IActiveScriptSiteInterruptPoll IActiveScriptSiteInterruptPoll_iface
;
80 IActiveScriptSiteWindow IActiveScriptSiteWindow_iface
;
81 IActiveScriptSiteUIControl IActiveScriptSiteUIControl_iface
;
82 IActiveScriptSiteDebug IActiveScriptSiteDebug_iface
;
83 IServiceProvider IServiceProvider_iface
;
87 IActiveScript
*script
;
88 IActiveScriptParse
*parse
;
89 IActiveScriptParseProcedure2
*parse_proc
;
91 SCRIPTSTATE script_state
;
93 HTMLInnerWindow
*window
;
99 static ScriptHost
*get_elem_script_host(HTMLInnerWindow
*,HTMLScriptElement
*);
101 static BOOL
set_script_prop(ScriptHost
*script_host
, DWORD property
, VARIANT
*val
)
103 IActiveScriptProperty
*script_prop
;
106 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptProperty
,
107 (void**)&script_prop
);
109 WARN("Could not get IActiveScriptProperty iface: %08x\n", hres
);
113 hres
= IActiveScriptProperty_SetProperty(script_prop
, property
, NULL
, val
);
114 IActiveScriptProperty_Release(script_prop
);
116 WARN("SetProperty(%x) failed: %08x\n", property
, hres
);
123 static BOOL
init_script_engine(ScriptHost
*script_host
)
125 compat_mode_t compat_mode
;
126 IObjectSafety
*safety
;
128 DWORD supported_opts
=0, enabled_opts
=0, script_mode
;
132 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptParse
, (void**)&script_host
->parse
);
134 WARN("Could not get IActiveScriptParse: %08x\n", hres
);
138 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IObjectSafety
, (void**)&safety
);
140 FIXME("Could not get IObjectSafety: %08x\n", hres
);
144 hres
= IObjectSafety_GetInterfaceSafetyOptions(safety
, &IID_IActiveScriptParse
, &supported_opts
, &enabled_opts
);
146 FIXME("GetInterfaceSafetyOptions failed: %08x\n", hres
);
147 }else if(!(supported_opts
& INTERFACE_USES_DISPEX
)) {
148 FIXME("INTERFACE_USES_DISPEX is not supported\n");
150 hres
= IObjectSafety_SetInterfaceSafetyOptions(safety
, &IID_IActiveScriptParse
,
151 INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
,
152 INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
);
154 FIXME("SetInterfaceSafetyOptions failed: %08x\n", hres
);
157 IObjectSafety_Release(safety
);
161 compat_mode
= lock_document_mode(script_host
->window
->doc
);
162 script_mode
= compat_mode
< COMPAT_MODE_IE8
? SCRIPTLANGUAGEVERSION_5_7
: SCRIPTLANGUAGEVERSION_5_8
;
163 if(IsEqualGUID(&script_host
->guid
, &CLSID_JScript
)) {
164 if(compat_mode
>= COMPAT_MODE_IE9
)
165 script_mode
= SCRIPTLANGUAGEVERSION_ES5
;
166 script_mode
|= SCRIPTLANGUAGEVERSION_HTML
;
169 V_I4(&var
) = script_mode
;
170 if(!set_script_prop(script_host
, SCRIPTPROP_INVOKEVERSIONING
, &var
) && (script_mode
& SCRIPTLANGUAGEVERSION_HTML
)) {
171 /* If this failed, we're most likely using native jscript. */
172 WARN("Failed to set script mode to HTML version.\n");
173 V_I4(&var
) = compat_mode
< COMPAT_MODE_IE8
? SCRIPTLANGUAGEVERSION_5_7
: SCRIPTLANGUAGEVERSION_5_8
;
174 set_script_prop(script_host
, SCRIPTPROP_INVOKEVERSIONING
, &var
);
177 V_VT(&var
) = VT_BOOL
;
178 V_BOOL(&var
) = VARIANT_TRUE
;
179 set_script_prop(script_host
, SCRIPTPROP_HACK_TRIDENTEVENTSINK
, &var
);
181 hres
= IActiveScriptParse_InitNew(script_host
->parse
);
183 WARN("InitNew failed: %08x\n", hres
);
187 hres
= IActiveScript_SetScriptSite(script_host
->script
, &script_host
->IActiveScriptSite_iface
);
189 WARN("SetScriptSite failed: %08x\n", hres
);
190 IActiveScript_Close(script_host
->script
);
194 hres
= IActiveScript_GetScriptState(script_host
->script
, &state
);
196 WARN("GetScriptState failed: %08x\n", hres
);
197 else if(state
!= SCRIPTSTATE_INITIALIZED
)
198 FIXME("state = %x\n", state
);
200 hres
= IActiveScript_SetScriptState(script_host
->script
, SCRIPTSTATE_STARTED
);
202 WARN("Starting script failed: %08x\n", hres
);
206 hres
= IActiveScript_AddNamedItem(script_host
->script
, windowW
,
207 SCRIPTITEM_ISVISIBLE
|SCRIPTITEM_ISSOURCE
|SCRIPTITEM_GLOBALMEMBERS
);
208 if(SUCCEEDED(hres
)) {
209 V_VT(&var
) = VT_BOOL
;
210 V_BOOL(&var
) = VARIANT_TRUE
;
211 set_script_prop(script_host
, SCRIPTPROP_ABBREVIATE_GLOBALNAME_RESOLUTION
, &var
);
213 WARN("AddNamedItem failed: %08x\n", hres
);
216 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptParseProcedure2
,
217 (void**)&script_host
->parse_proc
);
219 /* FIXME: QI for IActiveScriptParseProcedure */
220 WARN("Could not get IActiveScriptParseProcedure iface: %08x\n", hres
);
226 static void release_script_engine(ScriptHost
*This
)
231 switch(This
->script_state
) {
232 case SCRIPTSTATE_CONNECTED
:
233 IActiveScript_SetScriptState(This
->script
, SCRIPTSTATE_DISCONNECTED
);
235 case SCRIPTSTATE_STARTED
:
236 case SCRIPTSTATE_DISCONNECTED
:
237 case SCRIPTSTATE_INITIALIZED
:
238 IActiveScript_Close(This
->script
);
241 if(This
->parse_proc
) {
242 IActiveScriptParseProcedure2_Release(This
->parse_proc
);
243 This
->parse_proc
= NULL
;
247 IActiveScriptParse_Release(This
->parse
);
252 IActiveScript_Release(This
->script
);
254 This
->script_state
= SCRIPTSTATE_UNINITIALIZED
;
257 void connect_scripts(HTMLInnerWindow
*window
)
261 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
262 if(iter
->script_state
== SCRIPTSTATE_STARTED
)
263 IActiveScript_SetScriptState(iter
->script
, SCRIPTSTATE_CONNECTED
);
267 static inline ScriptHost
*impl_from_IActiveScriptSite(IActiveScriptSite
*iface
)
269 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSite_iface
);
272 static HRESULT WINAPI
ActiveScriptSite_QueryInterface(IActiveScriptSite
*iface
, REFIID riid
, void **ppv
)
274 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
278 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
279 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
280 *ppv
= &This
->IActiveScriptSite_iface
;
281 }else if(IsEqualGUID(&IID_IActiveScriptSite
, riid
)) {
282 TRACE("(%p)->(IID_IActiveScriptSite %p)\n", This
, ppv
);
283 *ppv
= &This
->IActiveScriptSite_iface
;
284 }else if(IsEqualGUID(&IID_IActiveScriptSiteInterruptPoll
, riid
)) {
285 TRACE("(%p)->(IID_IActiveScriptSiteInterruprtPoll %p)\n", This
, ppv
);
286 *ppv
= &This
->IActiveScriptSiteInterruptPoll_iface
;
287 }else if(IsEqualGUID(&IID_IActiveScriptSiteWindow
, riid
)) {
288 TRACE("(%p)->(IID_IActiveScriptSiteWindow %p)\n", This
, ppv
);
289 *ppv
= &This
->IActiveScriptSiteWindow_iface
;
290 }else if(IsEqualGUID(&IID_IActiveScriptSiteUIControl
, riid
)) {
291 TRACE("(%p)->(IID_IActiveScriptSiteUIControl %p)\n", This
, ppv
);
292 *ppv
= &This
->IActiveScriptSiteUIControl_iface
;
293 }else if(IsEqualGUID(&IID_IActiveScriptSiteDebug
, riid
)) {
294 TRACE("(%p)->(IID_IActiveScriptSiteDebug %p)\n", This
, ppv
);
295 *ppv
= &This
->IActiveScriptSiteDebug_iface
;
296 }else if(IsEqualGUID(&IID_IServiceProvider
, riid
)) {
297 TRACE("(%p)->(IID_IServiceProvider %p)\n", This
, ppv
);
298 *ppv
= &This
->IServiceProvider_iface
;
299 }else if(IsEqualGUID(&IID_ICanHandleException
, riid
)) {
300 TRACE("(%p)->(IID_ICanHandleException not supported %p)\n", This
, ppv
);
301 return E_NOINTERFACE
;
303 FIXME("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
304 return E_NOINTERFACE
;
307 IUnknown_AddRef((IUnknown
*)*ppv
);
311 static ULONG WINAPI
ActiveScriptSite_AddRef(IActiveScriptSite
*iface
)
313 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
314 LONG ref
= InterlockedIncrement(&This
->ref
);
316 TRACE("(%p) ref=%d\n", This
, ref
);
321 static ULONG WINAPI
ActiveScriptSite_Release(IActiveScriptSite
*iface
)
323 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
324 LONG ref
= InterlockedDecrement(&This
->ref
);
326 TRACE("(%p) ref=%d\n", This
, ref
);
329 release_script_engine(This
);
331 list_remove(&This
->entry
);
338 static HRESULT WINAPI
ActiveScriptSite_GetLCID(IActiveScriptSite
*iface
, LCID
*plcid
)
340 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
342 TRACE("(%p)->(%p)\n", This
, plcid
);
344 *plcid
= GetUserDefaultLCID();
348 static HRESULT WINAPI
ActiveScriptSite_GetItemInfo(IActiveScriptSite
*iface
, LPCOLESTR pstrName
,
349 DWORD dwReturnMask
, IUnknown
**ppiunkItem
, ITypeInfo
**ppti
)
351 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
353 TRACE("(%p)->(%s %x %p %p)\n", This
, debugstr_w(pstrName
), dwReturnMask
, ppiunkItem
, ppti
);
355 if(dwReturnMask
!= SCRIPTINFO_IUNKNOWN
) {
356 FIXME("Unsupported mask %x\n", dwReturnMask
);
362 if(strcmpW(pstrName
, windowW
))
363 return DISP_E_MEMBERNOTFOUND
;
368 /* FIXME: Return proxy object */
369 *ppiunkItem
= (IUnknown
*)&This
->window
->base
.IHTMLWindow2_iface
;
370 IUnknown_AddRef(*ppiunkItem
);
375 static HRESULT WINAPI
ActiveScriptSite_GetDocVersionString(IActiveScriptSite
*iface
, BSTR
*pbstrVersion
)
377 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
378 FIXME("(%p)->(%p)\n", This
, pbstrVersion
);
382 static HRESULT WINAPI
ActiveScriptSite_OnScriptTerminate(IActiveScriptSite
*iface
,
383 const VARIANT
*pvarResult
, const EXCEPINFO
*pexcepinfo
)
385 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
386 FIXME("(%p)->(%p %p)\n", This
, pvarResult
, pexcepinfo
);
390 static HRESULT WINAPI
ActiveScriptSite_OnStateChange(IActiveScriptSite
*iface
, SCRIPTSTATE ssScriptState
)
392 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
394 TRACE("(%p)->(%x)\n", This
, ssScriptState
);
396 This
->script_state
= ssScriptState
;
400 static HRESULT WINAPI
ActiveScriptSite_OnScriptError(IActiveScriptSite
*iface
, IActiveScriptError
*pscripterror
)
402 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
403 FIXME("(%p)->(%p)\n", This
, pscripterror
);
407 static HRESULT WINAPI
ActiveScriptSite_OnEnterScript(IActiveScriptSite
*iface
)
409 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
411 TRACE("(%p)->()\n", This
);
416 static HRESULT WINAPI
ActiveScriptSite_OnLeaveScript(IActiveScriptSite
*iface
)
418 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
420 TRACE("(%p)->()\n", This
);
425 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl
= {
426 ActiveScriptSite_QueryInterface
,
427 ActiveScriptSite_AddRef
,
428 ActiveScriptSite_Release
,
429 ActiveScriptSite_GetLCID
,
430 ActiveScriptSite_GetItemInfo
,
431 ActiveScriptSite_GetDocVersionString
,
432 ActiveScriptSite_OnScriptTerminate
,
433 ActiveScriptSite_OnStateChange
,
434 ActiveScriptSite_OnScriptError
,
435 ActiveScriptSite_OnEnterScript
,
436 ActiveScriptSite_OnLeaveScript
439 static inline ScriptHost
*impl_from_IActiveScriptSiteInterruptPoll(IActiveScriptSiteInterruptPoll
*iface
)
441 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteInterruptPoll_iface
);
444 static HRESULT WINAPI
ActiveScriptSiteInterruptPoll_QueryInterface(IActiveScriptSiteInterruptPoll
*iface
,
445 REFIID riid
, void **ppv
)
447 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
448 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
451 static ULONG WINAPI
ActiveScriptSiteInterruptPoll_AddRef(IActiveScriptSiteInterruptPoll
*iface
)
453 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
454 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
457 static ULONG WINAPI
ActiveScriptSiteInterruptPoll_Release(IActiveScriptSiteInterruptPoll
*iface
)
459 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
460 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
463 static HRESULT WINAPI
ActiveScriptSiteInterruptPoll_QueryContinue(IActiveScriptSiteInterruptPoll
*iface
)
465 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
467 TRACE("(%p)\n", This
);
472 static const IActiveScriptSiteInterruptPollVtbl ActiveScriptSiteInterruptPollVtbl
= {
473 ActiveScriptSiteInterruptPoll_QueryInterface
,
474 ActiveScriptSiteInterruptPoll_AddRef
,
475 ActiveScriptSiteInterruptPoll_Release
,
476 ActiveScriptSiteInterruptPoll_QueryContinue
479 static inline ScriptHost
*impl_from_IActiveScriptSiteWindow(IActiveScriptSiteWindow
*iface
)
481 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteWindow_iface
);
484 static HRESULT WINAPI
ActiveScriptSiteWindow_QueryInterface(IActiveScriptSiteWindow
*iface
,
485 REFIID riid
, void **ppv
)
487 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
488 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
491 static ULONG WINAPI
ActiveScriptSiteWindow_AddRef(IActiveScriptSiteWindow
*iface
)
493 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
494 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
497 static ULONG WINAPI
ActiveScriptSiteWindow_Release(IActiveScriptSiteWindow
*iface
)
499 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
500 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
503 static HRESULT WINAPI
ActiveScriptSiteWindow_GetWindow(IActiveScriptSiteWindow
*iface
, HWND
*phwnd
)
505 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
507 TRACE("(%p)->(%p)\n", This
, phwnd
);
509 if(!This
->window
|| !This
->window
->base
.outer_window
|| !This
->window
->base
.outer_window
->doc_obj
)
512 *phwnd
= This
->window
->base
.outer_window
->doc_obj
->hwnd
;
516 static HRESULT WINAPI
ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow
*iface
, BOOL fEnable
)
518 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
519 FIXME("(%p)->(%x)\n", This
, fEnable
);
523 static const IActiveScriptSiteWindowVtbl ActiveScriptSiteWindowVtbl
= {
524 ActiveScriptSiteWindow_QueryInterface
,
525 ActiveScriptSiteWindow_AddRef
,
526 ActiveScriptSiteWindow_Release
,
527 ActiveScriptSiteWindow_GetWindow
,
528 ActiveScriptSiteWindow_EnableModeless
531 static inline ScriptHost
*impl_from_IActiveScriptSiteUIControl(IActiveScriptSiteUIControl
*iface
)
533 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteUIControl_iface
);
536 static HRESULT WINAPI
ActiveScriptSiteUIControl_QueryInterface(IActiveScriptSiteUIControl
*iface
, REFIID riid
, void **ppv
)
538 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
539 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
542 static ULONG WINAPI
ActiveScriptSiteUIControl_AddRef(IActiveScriptSiteUIControl
*iface
)
544 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
545 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
548 static ULONG WINAPI
ActiveScriptSiteUIControl_Release(IActiveScriptSiteUIControl
*iface
)
550 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
551 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
554 static HRESULT WINAPI
ActiveScriptSiteUIControl_GetUIBehavior(IActiveScriptSiteUIControl
*iface
, SCRIPTUICITEM UicItem
,
555 SCRIPTUICHANDLING
*pUicHandling
)
557 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
559 WARN("(%p)->(%d %p) semi-stub\n", This
, UicItem
, pUicHandling
);
561 *pUicHandling
= SCRIPTUICHANDLING_ALLOW
;
565 static const IActiveScriptSiteUIControlVtbl ActiveScriptSiteUIControlVtbl
= {
566 ActiveScriptSiteUIControl_QueryInterface
,
567 ActiveScriptSiteUIControl_AddRef
,
568 ActiveScriptSiteUIControl_Release
,
569 ActiveScriptSiteUIControl_GetUIBehavior
572 static inline ScriptHost
*impl_from_IActiveScriptSiteDebug(IActiveScriptSiteDebug
*iface
)
574 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteDebug_iface
);
577 static HRESULT WINAPI
ActiveScriptSiteDebug_QueryInterface(IActiveScriptSiteDebug
*iface
,
578 REFIID riid
, void **ppv
)
580 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
581 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
584 static ULONG WINAPI
ActiveScriptSiteDebug_AddRef(IActiveScriptSiteDebug
*iface
)
586 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
587 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
590 static ULONG WINAPI
ActiveScriptSiteDebug_Release(IActiveScriptSiteDebug
*iface
)
592 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
593 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
596 static HRESULT WINAPI
ActiveScriptSiteDebug_GetDocumentContextFromPosition(IActiveScriptSiteDebug
*iface
,
597 CTXARG_T dwSourceContext
, ULONG uCharacterOffset
, ULONG uNumChars
, IDebugDocumentContext
**ppsc
)
599 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
600 FIXME("(%p)->(%s %u %u %p)\n", This
, wine_dbgstr_longlong(dwSourceContext
), uCharacterOffset
,
605 static HRESULT WINAPI
ActiveScriptSiteDebug_GetApplication(IActiveScriptSiteDebug
*iface
, IDebugApplication
**ppda
)
607 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
608 FIXME("(%p)->(%p)\n", This
, ppda
);
612 static HRESULT WINAPI
ActiveScriptSiteDebug_GetRootApplicationNode(IActiveScriptSiteDebug
*iface
,
613 IDebugApplicationNode
**ppdanRoot
)
615 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
616 FIXME("(%p)->(%p)\n", This
, ppdanRoot
);
620 static HRESULT WINAPI
ActiveScriptSiteDebug_OnScriptErrorDebug(IActiveScriptSiteDebug
*iface
,
621 IActiveScriptErrorDebug
*pErrorDebug
, BOOL
*pfEnterDebugger
, BOOL
*pfCallOnScriptErrorWhenContinuing
)
623 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
624 FIXME("(%p)->(%p %p %p)\n", This
, pErrorDebug
, pfEnterDebugger
, pfCallOnScriptErrorWhenContinuing
);
628 static const IActiveScriptSiteDebugVtbl ActiveScriptSiteDebugVtbl
= {
629 ActiveScriptSiteDebug_QueryInterface
,
630 ActiveScriptSiteDebug_AddRef
,
631 ActiveScriptSiteDebug_Release
,
632 ActiveScriptSiteDebug_GetDocumentContextFromPosition
,
633 ActiveScriptSiteDebug_GetApplication
,
634 ActiveScriptSiteDebug_GetRootApplicationNode
,
635 ActiveScriptSiteDebug_OnScriptErrorDebug
638 static inline ScriptHost
*impl_from_IServiceProvider(IServiceProvider
*iface
)
640 return CONTAINING_RECORD(iface
, ScriptHost
, IServiceProvider_iface
);
643 static HRESULT WINAPI
ASServiceProvider_QueryInterface(IServiceProvider
*iface
, REFIID riid
, void **ppv
)
645 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
646 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
649 static ULONG WINAPI
ASServiceProvider_AddRef(IServiceProvider
*iface
)
651 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
652 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
655 static ULONG WINAPI
ASServiceProvider_Release(IServiceProvider
*iface
)
657 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
658 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
661 static HRESULT WINAPI
ASServiceProvider_QueryService(IServiceProvider
*iface
, REFGUID guidService
,
662 REFIID riid
, void **ppv
)
664 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
666 if(IsEqualGUID(&SID_SInternetHostSecurityManager
, guidService
)) {
667 TRACE("(%p)->(SID_SInternetHostSecurityManager)\n", This
);
669 if(!This
->window
|| !This
->window
->doc
)
670 return E_NOINTERFACE
;
672 return IInternetHostSecurityManager_QueryInterface(&This
->window
->doc
->IInternetHostSecurityManager_iface
,
676 if(IsEqualGUID(&SID_SContainerDispatch
, guidService
)) {
677 TRACE("(%p)->(SID_SContainerDispatch)\n", This
);
679 if(!This
->window
|| !This
->window
->doc
)
680 return E_NOINTERFACE
;
682 return IHTMLDocument2_QueryInterface(&This
->window
->doc
->basedoc
.IHTMLDocument2_iface
, riid
, ppv
);
685 FIXME("(%p)->(%s %s %p)\n", This
, debugstr_guid(guidService
), debugstr_guid(riid
), ppv
);
686 return E_NOINTERFACE
;
689 static const IServiceProviderVtbl ASServiceProviderVtbl
= {
690 ASServiceProvider_QueryInterface
,
691 ASServiceProvider_AddRef
,
692 ASServiceProvider_Release
,
693 ASServiceProvider_QueryService
696 static ScriptHost
*create_script_host(HTMLInnerWindow
*window
, const GUID
*guid
)
701 ret
= heap_alloc_zero(sizeof(*ret
));
705 ret
->IActiveScriptSite_iface
.lpVtbl
= &ActiveScriptSiteVtbl
;
706 ret
->IActiveScriptSiteInterruptPoll_iface
.lpVtbl
= &ActiveScriptSiteInterruptPollVtbl
;
707 ret
->IActiveScriptSiteWindow_iface
.lpVtbl
= &ActiveScriptSiteWindowVtbl
;
708 ret
->IActiveScriptSiteUIControl_iface
.lpVtbl
= &ActiveScriptSiteUIControlVtbl
;
709 ret
->IActiveScriptSiteDebug_iface
.lpVtbl
= &ActiveScriptSiteDebugVtbl
;
710 ret
->IServiceProvider_iface
.lpVtbl
= &ASServiceProviderVtbl
;
712 ret
->window
= window
;
713 ret
->script_state
= SCRIPTSTATE_UNINITIALIZED
;
716 list_add_tail(&window
->script_hosts
, &ret
->entry
);
718 hres
= CoCreateInstance(&ret
->guid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
719 &IID_IActiveScript
, (void**)&ret
->script
);
721 WARN("Could not load script engine: %08x\n", hres
);
722 else if(!init_script_engine(ret
))
723 release_script_engine(ret
);
728 static void dispatch_script_readystatechange_event(HTMLScriptElement
*script
)
733 hres
= create_document_event(script
->element
.node
.doc
, EVENTID_READYSTATECHANGE
, &event
);
737 dispatch_event(&script
->element
.node
.event_target
, event
);
738 IDOMEvent_Release(&event
->IDOMEvent_iface
);
743 HTMLScriptElement
*elem
;
744 } fire_readystatechange_task_t
;
746 static void fire_readystatechange_proc(task_t
*_task
)
748 fire_readystatechange_task_t
*task
= (fire_readystatechange_task_t
*)_task
;
750 if(!task
->elem
->pending_readystatechange_event
)
753 task
->elem
->pending_readystatechange_event
= FALSE
;
754 dispatch_script_readystatechange_event(task
->elem
);
757 static void fire_readystatechange_task_destr(task_t
*_task
)
759 fire_readystatechange_task_t
*task
= (fire_readystatechange_task_t
*)_task
;
761 IHTMLScriptElement_Release(&task
->elem
->IHTMLScriptElement_iface
);
764 static void set_script_elem_readystate(HTMLScriptElement
*script_elem
, READYSTATE readystate
)
766 script_elem
->readystate
= readystate
;
768 if(readystate
!= READYSTATE_INTERACTIVE
) {
769 if(!script_elem
->element
.node
.doc
->window
->parser_callback_cnt
) {
770 fire_readystatechange_task_t
*task
;
773 if(script_elem
->pending_readystatechange_event
)
776 task
= heap_alloc(sizeof(*task
));
780 IHTMLScriptElement_AddRef(&script_elem
->IHTMLScriptElement_iface
);
781 task
->elem
= script_elem
;
783 hres
= push_task(&task
->header
, fire_readystatechange_proc
, fire_readystatechange_task_destr
,
784 script_elem
->element
.node
.doc
->window
->task_magic
);
786 script_elem
->pending_readystatechange_event
= TRUE
;
788 script_elem
->pending_readystatechange_event
= FALSE
;
789 dispatch_script_readystatechange_event(script_elem
);
794 static void parse_elem_text(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
, LPCWSTR text
)
800 TRACE("%s\n", debugstr_w(text
));
803 memset(&excepinfo
, 0, sizeof(excepinfo
));
805 hres
= IActiveScriptParse_ParseScriptText(script_host
->parse
, text
, windowW
, NULL
, script_endW
,
806 0, 0, SCRIPTTEXT_ISVISIBLE
|SCRIPTTEXT_HOSTMANAGESSOURCE
,
811 WARN("<<< %08x\n", hres
);
817 HTMLScriptElement
*script_elem
;
824 static HRESULT
get_binding_text(ScriptBSC
*bsc
, WCHAR
**ret
)
829 if(!bsc
->bsc
.readed
) {
830 text
= heap_alloc(sizeof(WCHAR
));
832 return E_OUTOFMEMORY
;
838 switch(bsc
->bsc
.bom
) {
840 if(bsc
->bsc
.readed
% sizeof(WCHAR
)) {
841 FIXME("The buffer is not a valid utf16 string\n");
845 text
= heap_alloc(bsc
->bsc
.readed
+sizeof(WCHAR
));
847 return E_OUTOFMEMORY
;
849 memcpy(text
, bsc
->buf
, bsc
->bsc
.readed
);
850 text
[bsc
->bsc
.readed
/sizeof(WCHAR
)] = 0;
854 /* FIXME: Try to use charset from HTTP headers first */
855 cp
= get_document_charset(bsc
->script_elem
->element
.node
.doc
);
860 len
= MultiByteToWideChar(cp
, 0, bsc
->buf
, bsc
->bsc
.readed
, NULL
, 0);
861 text
= heap_alloc((len
+1)*sizeof(WCHAR
));
863 return E_OUTOFMEMORY
;
865 MultiByteToWideChar(cp
, 0, bsc
->buf
, bsc
->bsc
.readed
, text
, len
);
874 static void script_file_available(ScriptBSC
*bsc
)
876 HTMLScriptElement
*script_elem
= bsc
->script_elem
;
877 HTMLInnerWindow
*window
= bsc
->bsc
.window
;
878 ScriptHost
*script_host
;
883 assert(window
!= NULL
);
885 hres
= get_binding_text(bsc
, &script_elem
->src_text
);
889 script_host
= get_elem_script_host(window
, script_elem
);
893 if(window
->parser_callback_cnt
) {
894 script_queue_entry_t
*queue
;
896 TRACE("Adding to queue\n");
898 queue
= heap_alloc(sizeof(*queue
));
902 IHTMLScriptElement_AddRef(&script_elem
->IHTMLScriptElement_iface
);
903 queue
->script
= script_elem
;
905 list_add_tail(&window
->script_queue
, &queue
->entry
);
909 nsres
= nsIDOMElement_GetParentNode(script_elem
->element
.dom_element
, &parent
);
910 if(NS_FAILED(nsres
) || !parent
) {
911 TRACE("No parent, not executing\n");
912 script_elem
->parse_on_bind
= TRUE
;
916 nsIDOMNode_Release(parent
);
918 script_host
= get_elem_script_host(window
, script_elem
);
922 script_elem
->parsed
= TRUE
;
923 if(script_host
->parse
)
924 parse_elem_text(script_host
, script_elem
, script_elem
->src_text
);
927 static inline ScriptBSC
*impl_from_BSCallback(BSCallback
*iface
)
929 return CONTAINING_RECORD(iface
, ScriptBSC
, bsc
);
932 static void ScriptBSC_destroy(BSCallback
*bsc
)
934 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
936 if(This
->script_elem
) {
937 IHTMLScriptElement_Release(&This
->script_elem
->IHTMLScriptElement_iface
);
938 This
->script_elem
= NULL
;
941 heap_free(This
->buf
);
945 static HRESULT
ScriptBSC_init_bindinfo(BSCallback
*bsc
)
950 static HRESULT
ScriptBSC_start_binding(BSCallback
*bsc
)
952 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
954 This
->script_elem
->binding
= &This
->bsc
;
956 /* FIXME: We should find a better to decide if 'loading' state is supposed to be used by the protocol. */
957 if(This
->scheme
== URL_SCHEME_HTTPS
|| This
->scheme
== URL_SCHEME_HTTP
)
958 set_script_elem_readystate(This
->script_elem
, READYSTATE_LOADING
);
963 static HRESULT
ScriptBSC_stop_binding(BSCallback
*bsc
, HRESULT result
)
965 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
967 if(SUCCEEDED(result
) && !This
->script_elem
)
968 result
= E_UNEXPECTED
;
970 assert(FAILED(result
) || This
->script_elem
->binding
== &This
->bsc
);
971 This
->script_elem
->binding
= NULL
;
973 if(This
->script_elem
->readystate
== READYSTATE_LOADING
)
974 set_script_elem_readystate(This
->script_elem
, READYSTATE_LOADED
);
976 if(SUCCEEDED(result
)) {
977 script_file_available(This
);
979 FIXME("binding failed %08x\n", result
);
980 heap_free(This
->buf
);
985 IHTMLScriptElement_Release(&This
->script_elem
->IHTMLScriptElement_iface
);
986 This
->script_elem
= NULL
;
990 static HRESULT
ScriptBSC_read_data(BSCallback
*bsc
, IStream
*stream
)
992 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
997 This
->buf
= heap_alloc(128);
999 return E_OUTOFMEMORY
;
1004 if(This
->bsc
.readed
>= This
->size
) {
1006 new_buf
= heap_realloc(This
->buf
, This
->size
<< 1);
1008 return E_OUTOFMEMORY
;
1010 This
->buf
= new_buf
;
1013 hres
= read_stream(&This
->bsc
, stream
, This
->buf
+This
->bsc
.readed
, This
->size
-This
->bsc
.readed
, &readed
);
1014 }while(hres
== S_OK
);
1019 static HRESULT
ScriptBSC_on_progress(BSCallback
*bsc
, ULONG status_code
, LPCWSTR status_text
)
1024 static HRESULT
ScriptBSC_on_response(BSCallback
*bsc
, DWORD response_code
,
1025 LPCWSTR response_headers
)
1030 static HRESULT
ScriptBSC_beginning_transaction(BSCallback
*bsc
, WCHAR
**additional_headers
)
1035 static const BSCallbackVtbl ScriptBSCVtbl
= {
1037 ScriptBSC_init_bindinfo
,
1038 ScriptBSC_start_binding
,
1039 ScriptBSC_stop_binding
,
1040 ScriptBSC_read_data
,
1041 ScriptBSC_on_progress
,
1042 ScriptBSC_on_response
,
1043 ScriptBSC_beginning_transaction
1047 HRESULT
load_script(HTMLScriptElement
*script_elem
, const WCHAR
*src
, BOOL async
)
1049 HTMLInnerWindow
*window
;
1055 static const WCHAR wine_schemaW
[] = {'w','i','n','e',':'};
1057 if(strlenW(src
) > ARRAY_SIZE(wine_schemaW
) && !memcmp(src
, wine_schemaW
, sizeof(wine_schemaW
)))
1058 src
+= ARRAY_SIZE(wine_schemaW
);
1060 TRACE("(%p %s %x)\n", script_elem
, debugstr_w(src
), async
);
1062 if(!script_elem
->element
.node
.doc
|| !(window
= script_elem
->element
.node
.doc
->window
)) {
1064 return E_UNEXPECTED
;
1067 hres
= create_uri(src
, 0, &uri
);
1071 hres
= CreateURLMonikerEx2(NULL
, uri
, &mon
, URL_MK_UNIFORM
);
1077 bsc
= heap_alloc_zero(sizeof(*bsc
));
1079 IMoniker_Release(mon
);
1081 return E_OUTOFMEMORY
;
1084 init_bscallback(&bsc
->bsc
, &ScriptBSCVtbl
, mon
, async
? BINDF_ASYNCHRONOUS
| BINDF_ASYNCSTORAGE
| BINDF_PULLDATA
: 0);
1085 IMoniker_Release(mon
);
1087 hres
= IUri_GetScheme(uri
, &bsc
->scheme
);
1090 bsc
->scheme
= URL_SCHEME_UNKNOWN
;
1092 IHTMLScriptElement_AddRef(&script_elem
->IHTMLScriptElement_iface
);
1093 bsc
->script_elem
= script_elem
;
1095 hres
= start_binding(window
, &bsc
->bsc
, NULL
);
1097 IBindStatusCallback_Release(&bsc
->bsc
.IBindStatusCallback_iface
);
1101 static void parse_inline_script(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
)
1103 const PRUnichar
*text
;
1107 nsAString_Init(&text_str
, NULL
);
1108 nsres
= nsIDOMHTMLScriptElement_GetText(script_elem
->nsscript
, &text_str
);
1109 nsAString_GetData(&text_str
, &text
);
1111 set_script_elem_readystate(script_elem
, READYSTATE_INTERACTIVE
);
1113 if(NS_FAILED(nsres
)) {
1114 ERR("GetText failed: %08x\n", nsres
);
1116 parse_elem_text(script_host
, script_elem
, text
);
1119 nsAString_Finish(&text_str
);
1122 static BOOL
parse_script_elem(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
)
1124 nsAString src_str
, event_str
;
1125 BOOL is_complete
= FALSE
;
1126 const PRUnichar
*src
;
1129 nsAString_Init(&event_str
, NULL
);
1130 nsres
= nsIDOMHTMLScriptElement_GetEvent(script_elem
->nsscript
, &event_str
);
1131 if(NS_SUCCEEDED(nsres
)) {
1132 const PRUnichar
*event
;
1134 nsAString_GetData(&event_str
, &event
);
1136 TRACE("deferring event %s script evaluation\n", debugstr_w(event
));
1137 nsAString_Finish(&event_str
);
1141 ERR("GetEvent failed: %08x\n", nsres
);
1143 nsAString_Finish(&event_str
);
1145 nsAString_Init(&src_str
, NULL
);
1146 nsres
= nsIDOMHTMLScriptElement_GetSrc(script_elem
->nsscript
, &src_str
);
1147 nsAString_GetData(&src_str
, &src
);
1149 if(NS_FAILED(nsres
)) {
1150 ERR("GetSrc failed: %08x\n", nsres
);
1152 load_script(script_elem
, src
, FALSE
);
1153 is_complete
= script_elem
->parsed
;
1155 parse_inline_script(script_host
, script_elem
);
1159 nsAString_Finish(&src_str
);
1164 static GUID
get_default_script_guid(HTMLInnerWindow
*window
)
1166 /* If not specified, we should use very first script host that was created for the page (or JScript if none) */
1167 return list_empty(&window
->script_hosts
)
1169 : LIST_ENTRY(list_head(&window
->script_hosts
), ScriptHost
, entry
)->guid
;
1172 static BOOL
get_guid_from_type(LPCWSTR type
, GUID
*guid
)
1174 static const WCHAR text_javascriptW
[] =
1175 {'t','e','x','t','/','j','a','v','a','s','c','r','i','p','t',0};
1176 static const WCHAR text_jscriptW
[] =
1177 {'t','e','x','t','/','j','s','c','r','i','p','t',0};
1178 static const WCHAR text_vbscriptW
[] =
1179 {'t','e','x','t','/','v','b','s','c','r','i','p','t',0};
1181 /* FIXME: Handle more types */
1182 if(!strcmpiW(type
, text_javascriptW
) || !strcmpiW(type
, text_jscriptW
)) {
1183 *guid
= CLSID_JScript
;
1184 }else if(!strcmpiW(type
, text_vbscriptW
)) {
1185 *guid
= CLSID_VBScript
;
1187 FIXME("Unknown type %s\n", debugstr_w(type
));
1194 static BOOL
get_guid_from_language(LPCWSTR type
, GUID
*guid
)
1198 hres
= CLSIDFromProgID(type
, guid
);
1202 /* FIXME: Check CATID_ActiveScriptParse */
1207 static BOOL
get_script_guid(HTMLInnerWindow
*window
, nsIDOMHTMLScriptElement
*nsscript
, GUID
*guid
)
1209 nsIDOMElement
*nselem
;
1210 const PRUnichar
*language
;
1215 static const PRUnichar languageW
[] = {'l','a','n','g','u','a','g','e',0};
1217 nsAString_Init(&val_str
, NULL
);
1219 nsres
= nsIDOMHTMLScriptElement_GetType(nsscript
, &val_str
);
1220 if(NS_SUCCEEDED(nsres
)) {
1221 const PRUnichar
*type
;
1223 nsAString_GetData(&val_str
, &type
);
1225 ret
= get_guid_from_type(type
, guid
);
1226 nsAString_Finish(&val_str
);
1230 ERR("GetType failed: %08x\n", nsres
);
1233 nsres
= nsIDOMHTMLScriptElement_QueryInterface(nsscript
, &IID_nsIDOMElement
, (void**)&nselem
);
1234 assert(nsres
== NS_OK
);
1236 nsres
= get_elem_attr_value(nselem
, languageW
, &val_str
, &language
);
1237 nsIDOMElement_Release(nselem
);
1238 if(NS_SUCCEEDED(nsres
)) {
1240 ret
= get_guid_from_language(language
, guid
);
1242 *guid
= get_default_script_guid(window
);
1245 nsAString_Finish(&val_str
);
1251 static ScriptHost
*get_script_host(HTMLInnerWindow
*window
, const GUID
*guid
)
1255 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
1256 if(IsEqualGUID(guid
, &iter
->guid
))
1260 return create_script_host(window
, guid
);
1263 static ScriptHost
*get_elem_script_host(HTMLInnerWindow
*window
, HTMLScriptElement
*script_elem
)
1267 if(!get_script_guid(window
, script_elem
->nsscript
, &guid
)) {
1268 WARN("Could not find script GUID\n");
1272 if(IsEqualGUID(&CLSID_JScript
, &guid
)
1273 && (!window
->base
.outer_window
|| window
->base
.outer_window
->scriptmode
!= SCRIPTMODE_ACTIVESCRIPT
)) {
1274 TRACE("Ignoring JScript\n");
1278 return get_script_host(window
, &guid
);
1281 void doc_insert_script(HTMLInnerWindow
*window
, HTMLScriptElement
*script_elem
, BOOL from_parser
)
1283 ScriptHost
*script_host
;
1284 BOOL is_complete
= FALSE
;
1286 script_host
= get_elem_script_host(window
, script_elem
);
1290 if(script_host
->parse
) {
1291 if(script_elem
->src_text
) {
1293 set_script_elem_readystate(script_elem
, READYSTATE_INTERACTIVE
);
1294 script_elem
->parsed
= TRUE
;
1295 parse_elem_text(script_host
, script_elem
, script_elem
->src_text
);
1297 }else if(!script_elem
->binding
) {
1298 is_complete
= parse_script_elem(script_host
, script_elem
);
1303 set_script_elem_readystate(script_elem
, READYSTATE_COMPLETE
);
1306 IDispatch
*script_parse_event(HTMLInnerWindow
*window
, LPCWSTR text
)
1308 ScriptHost
*script_host
;
1314 static const WCHAR delimiterW
[] = {'\"',0};
1316 TRACE("%s\n", debugstr_w(text
));
1318 for(ptr
= text
; isalnumW(*ptr
); ptr
++);
1323 language
= heap_alloc((ptr
-text
+1)*sizeof(WCHAR
));
1327 memcpy(language
, text
, (ptr
-text
)*sizeof(WCHAR
));
1328 language
[ptr
-text
] = 0;
1330 b
= get_guid_from_language(language
, &guid
);
1332 heap_free(language
);
1335 WARN("Could not find language\n");
1342 guid
= get_default_script_guid(window
);
1345 if(IsEqualGUID(&CLSID_JScript
, &guid
)
1346 && (!window
->base
.outer_window
|| window
->base
.outer_window
->scriptmode
!= SCRIPTMODE_ACTIVESCRIPT
)) {
1347 TRACE("Ignoring JScript\n");
1351 script_host
= get_script_host(window
, &guid
);
1352 if(!script_host
|| !script_host
->parse_proc
)
1355 hres
= IActiveScriptParseProcedure2_ParseProcedureText(script_host
->parse_proc
, ptr
, NULL
, emptyW
,
1356 NULL
, NULL
, delimiterW
, 0 /* FIXME */, 0,
1357 SCRIPTPROC_HOSTMANAGESSOURCE
|SCRIPTPROC_IMPLICIT_THIS
|SCRIPTPROC_IMPLICIT_PARENTS
, &disp
);
1359 WARN("ParseProcedureText failed: %08x\n", hres
);
1363 TRACE("ret %p\n", disp
);
1367 HRESULT
exec_script(HTMLInnerWindow
*window
, const WCHAR
*code
, const WCHAR
*lang
, VARIANT
*ret
)
1369 ScriptHost
*script_host
;
1374 static const WCHAR delimW
[] = {'"',0};
1376 if(!get_guid_from_language(lang
, &guid
)) {
1377 WARN("Could not find script GUID\n");
1378 return CO_E_CLASSSTRING
;
1381 script_host
= get_script_host(window
, &guid
);
1383 FIXME("No script host\n");
1387 if(!script_host
->parse
) {
1388 FIXME("script_host->parse == NULL\n");
1392 memset(&ei
, 0, sizeof(ei
));
1394 hres
= IActiveScriptParse_ParseScriptText(script_host
->parse
, code
, NULL
, NULL
, delimW
, 0, 0, SCRIPTTEXT_ISVISIBLE
, ret
, &ei
);
1398 WARN("<<< %08x\n", hres
);
1403 IDispatch
*get_script_disp(ScriptHost
*script_host
)
1408 if(!script_host
->script
)
1411 hres
= IActiveScript_GetScriptDispatch(script_host
->script
, windowW
, &disp
);
1418 static EventTarget
*find_event_target(HTMLDocumentNode
*doc
, HTMLScriptElement
*script_elem
)
1420 EventTarget
*event_target
= NULL
;
1421 const PRUnichar
*target_id
;
1422 nsAString target_id_str
;
1426 nsAString_Init(&target_id_str
, NULL
);
1427 nsres
= nsIDOMHTMLScriptElement_GetHtmlFor(script_elem
->nsscript
, &target_id_str
);
1428 if(NS_FAILED(nsres
)) {
1429 ERR("GetScriptFor failed: %08x\n", nsres
);
1430 nsAString_Finish(&target_id_str
);
1434 nsAString_GetData(&target_id_str
, &target_id
);
1436 FIXME("Empty for attribute\n");
1437 }else if(!strcmpW(target_id
, documentW
)) {
1438 event_target
= &doc
->node
.event_target
;
1439 htmldoc_addref(&doc
->basedoc
);
1440 }else if(!strcmpW(target_id
, windowW
)) {
1442 event_target
= &doc
->window
->event_target
;
1443 IDispatchEx_AddRef(&event_target
->dispex
.IDispatchEx_iface
);
1446 HTMLElement
*target_elem
;
1448 hres
= get_doc_elem_by_id(doc
, target_id
, &target_elem
);
1449 if(SUCCEEDED(hres
) && target_elem
) {
1450 event_target
= &target_elem
->node
.event_target
;
1454 nsAString_Finish(&target_id_str
);
1455 return event_target
;
1458 static BOOL
parse_event_str(WCHAR
*event
, const WCHAR
**args
)
1462 TRACE("%s\n", debugstr_w(event
));
1464 for(ptr
= event
; isalnumW(*ptr
); ptr
++);
1475 while(isalnumW(*ptr
) || isspaceW(*ptr
) || *ptr
== ',')
1485 static IDispatch
*parse_event_elem(HTMLDocumentNode
*doc
, HTMLScriptElement
*script_elem
, WCHAR
**ret_event
)
1487 ScriptHost
*script_host
;
1488 WCHAR
*event
= NULL
;
1495 if(script_elem
->parsed
)
1498 script_host
= get_elem_script_host(doc
->window
, script_elem
);
1499 if(!script_host
|| !script_host
->parse_proc
)
1502 nsAString_Init(&nsstr
, NULL
);
1503 nsres
= nsIDOMHTMLScriptElement_GetEvent(script_elem
->nsscript
, &nsstr
);
1504 if(NS_SUCCEEDED(nsres
)) {
1505 const PRUnichar
*event_val
;
1507 nsAString_GetData(&nsstr
, &event_val
);
1508 event
= heap_strdupW(event_val
);
1510 nsAString_Finish(&nsstr
);
1514 if(!parse_event_str(event
, &args
)) {
1515 WARN("parsing %s failed\n", debugstr_w(event
));
1520 nsAString_Init(&nsstr
, NULL
);
1521 nsres
= nsIDOMHTMLScriptElement_GetText(script_elem
->nsscript
, &nsstr
);
1522 if(NS_SUCCEEDED(nsres
)) {
1523 const PRUnichar
*text
;
1525 nsAString_GetData(&nsstr
, &text
);
1526 hres
= IActiveScriptParseProcedure2_ParseProcedureText(script_host
->parse_proc
, text
, args
,
1527 emptyW
, NULL
, NULL
, script_endW
, 0, 0,
1528 SCRIPTPROC_HOSTMANAGESSOURCE
|SCRIPTPROC_IMPLICIT_THIS
|SCRIPTPROC_IMPLICIT_PARENTS
, &disp
);
1532 ERR("GetText failed: %08x\n", nsres
);
1535 nsAString_Finish(&nsstr
);
1545 void bind_event_scripts(HTMLDocumentNode
*doc
)
1547 HTMLPluginContainer
*plugin_container
;
1548 nsIDOMHTMLScriptElement
*nsscript
;
1549 HTMLScriptElement
*script_elem
;
1550 EventTarget
*event_target
;
1551 nsIDOMNodeList
*node_list
;
1552 nsIDOMNode
*script_node
;
1553 nsAString selector_str
;
1554 IDispatch
*event_disp
;
1560 static const PRUnichar selectorW
[] = {'s','c','r','i','p','t','[','e','v','e','n','t',']',0};
1567 nsAString_InitDepend(&selector_str
, selectorW
);
1568 nsres
= nsIDOMHTMLDocument_QuerySelectorAll(doc
->nsdoc
, &selector_str
, &node_list
);
1569 nsAString_Finish(&selector_str
);
1570 if(NS_FAILED(nsres
)) {
1571 ERR("QuerySelectorAll failed: %08x\n", nsres
);
1578 nsres
= nsIDOMNodeList_GetLength(node_list
, &length
);
1579 assert(nsres
== NS_OK
);
1581 for(i
=0; i
< length
; i
++) {
1582 nsres
= nsIDOMNodeList_Item(node_list
, i
, &script_node
);
1583 if(NS_FAILED(nsres
) || !script_node
) {
1584 ERR("Item(%d) failed: %08x\n", i
, nsres
);
1588 nsres
= nsIDOMNode_QueryInterface(script_node
, &IID_nsIDOMHTMLScriptElement
, (void**)&nsscript
);
1589 assert(nsres
== NS_OK
);
1590 nsIDOMNode_Release(script_node
);
1592 hres
= script_elem_from_nsscript(nsscript
, &script_elem
);
1596 event_disp
= parse_event_elem(doc
, script_elem
, &event
);
1598 event_target
= find_event_target(doc
, script_elem
);
1600 hres
= IDispatchEx_QueryInterface(&event_target
->dispex
.IDispatchEx_iface
, &IID_HTMLPluginContainer
,
1601 (void**)&plugin_container
);
1603 bind_activex_event(doc
, plugin_container
, event
, event_disp
);
1605 bind_target_event(doc
, event_target
, event
, event_disp
);
1607 IDispatchEx_Release(&event_target
->dispex
.IDispatchEx_iface
);
1608 if(plugin_container
)
1609 node_release(&plugin_container
->element
.node
);
1613 IDispatch_Release(event_disp
);
1616 IHTMLScriptElement_Release(&script_elem
->IHTMLScriptElement_iface
);
1619 nsIDOMNodeList_Release(node_list
);
1622 BOOL
find_global_prop(HTMLInnerWindow
*window
, BSTR name
, DWORD flags
, ScriptHost
**ret_host
, DISPID
*ret_id
)
1624 IDispatchEx
*dispex
;
1629 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
1630 disp
= get_script_disp(iter
);
1634 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1635 if(SUCCEEDED(hres
)) {
1636 hres
= IDispatchEx_GetDispID(dispex
, name
, flags
& (~fdexNameEnsure
), ret_id
);
1637 IDispatchEx_Release(dispex
);
1639 FIXME("No IDispatchEx\n");
1643 IDispatch_Release(disp
);
1644 if(SUCCEEDED(hres
)) {
1653 static BOOL
is_jscript_available(void)
1655 static BOOL available
, checked
;
1659 HRESULT hres
= CoGetClassObject(&CLSID_JScript
, CLSCTX_INPROC_SERVER
, NULL
, &IID_IUnknown
, (void**)&unk
);
1661 if(SUCCEEDED(hres
)) {
1663 IUnknown_Release(unk
);
1673 void set_script_mode(HTMLOuterWindow
*window
, SCRIPTMODE mode
)
1675 nsIWebBrowserSetup
*setup
;
1678 if(mode
== SCRIPTMODE_ACTIVESCRIPT
&& !is_jscript_available()) {
1679 TRACE("jscript.dll not available\n");
1680 window
->scriptmode
= SCRIPTMODE_GECKO
;
1684 window
->scriptmode
= mode
;
1686 if(!window
->doc_obj
->nscontainer
|| !window
->doc_obj
->nscontainer
->webbrowser
)
1689 nsres
= nsIWebBrowser_QueryInterface(window
->doc_obj
->nscontainer
->webbrowser
,
1690 &IID_nsIWebBrowserSetup
, (void**)&setup
);
1691 if(NS_SUCCEEDED(nsres
)) {
1692 nsres
= nsIWebBrowserSetup_SetProperty(setup
, SETUP_ALLOW_JAVASCRIPT
,
1693 window
->scriptmode
== SCRIPTMODE_GECKO
);
1695 if(NS_SUCCEEDED(nsres
))
1696 nsres
= nsIWebBrowserSetup_SetProperty(setup
, SETUP_DISABLE_NOSCRIPT
, TRUE
);
1698 nsIWebBrowserSetup_Release(setup
);
1701 if(NS_FAILED(nsres
))
1702 ERR("JavaScript setup failed: %08x\n", nsres
);
1705 void release_script_hosts(HTMLInnerWindow
*window
)
1707 script_queue_entry_t
*queue_iter
;
1710 while(!list_empty(&window
->script_queue
)) {
1711 queue_iter
= LIST_ENTRY(list_head(&window
->script_queue
), script_queue_entry_t
, entry
);
1713 list_remove(&queue_iter
->entry
);
1714 IHTMLScriptElement_Release(&queue_iter
->script
->IHTMLScriptElement_iface
);
1715 heap_free(queue_iter
);
1718 while(!list_empty(&window
->script_hosts
)) {
1719 iter
= LIST_ENTRY(list_head(&window
->script_hosts
), ScriptHost
, entry
);
1721 release_script_engine(iter
);
1722 list_remove(&iter
->entry
);
1723 iter
->window
= NULL
;
1724 IActiveScriptSite_Release(&iter
->IActiveScriptSite_iface
);