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 static const WCHAR documentW
[] = {'d','o','c','u','m','e','n','t',0};
69 static const WCHAR windowW
[] = {'w','i','n','d','o','w',0};
70 static const WCHAR script_endW
[] = {'<','/','S','C','R','I','P','T','>',0};
71 static const WCHAR emptyW
[] = {0};
74 IActiveScriptSite IActiveScriptSite_iface
;
75 IActiveScriptSiteInterruptPoll IActiveScriptSiteInterruptPoll_iface
;
76 IActiveScriptSiteWindow IActiveScriptSiteWindow_iface
;
77 IActiveScriptSiteUIControl IActiveScriptSiteUIControl_iface
;
78 IActiveScriptSiteDebug IActiveScriptSiteDebug_iface
;
79 IServiceProvider IServiceProvider_iface
;
83 IActiveScript
*script
;
84 IActiveScriptParse
*parse
;
85 IActiveScriptParseProcedure2
*parse_proc
;
87 SCRIPTSTATE script_state
;
89 HTMLInnerWindow
*window
;
95 static void set_script_prop(ScriptHost
*script_host
, DWORD property
, VARIANT
*val
)
97 IActiveScriptProperty
*script_prop
;
100 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptProperty
,
101 (void**)&script_prop
);
103 WARN("Could not get IActiveScriptProperty iface: %08x\n", hres
);
107 hres
= IActiveScriptProperty_SetProperty(script_prop
, property
, NULL
, val
);
108 IActiveScriptProperty_Release(script_prop
);
110 WARN("SetProperty(%x) failed: %08x\n", property
, hres
);
113 static BOOL
init_script_engine(ScriptHost
*script_host
)
115 IObjectSafety
*safety
;
117 DWORD supported_opts
=0, enabled_opts
=0;
121 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptParse
, (void**)&script_host
->parse
);
123 WARN("Could not get IActiveScriptHost: %08x\n", hres
);
127 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IObjectSafety
, (void**)&safety
);
129 FIXME("Could not get IObjectSafety: %08x\n", hres
);
133 hres
= IObjectSafety_GetInterfaceSafetyOptions(safety
, &IID_IActiveScriptParse
, &supported_opts
, &enabled_opts
);
135 FIXME("GetInterfaceSafetyOptions failed: %08x\n", hres
);
136 }else if(!(supported_opts
& INTERFACE_USES_DISPEX
)) {
137 FIXME("INTERFACE_USES_DISPEX is not supported\n");
139 hres
= IObjectSafety_SetInterfaceSafetyOptions(safety
, &IID_IActiveScriptParse
,
140 INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
,
141 INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
);
143 FIXME("SetInterfaceSafetyOptions failed: %08x\n", hres
);
146 IObjectSafety_Release(safety
);
152 set_script_prop(script_host
, SCRIPTPROP_INVOKEVERSIONING
, &var
);
154 V_VT(&var
) = VT_BOOL
;
155 V_BOOL(&var
) = VARIANT_TRUE
;
156 set_script_prop(script_host
, SCRIPTPROP_HACK_TRIDENTEVENTSINK
, &var
);
158 hres
= IActiveScriptParse_InitNew(script_host
->parse
);
160 WARN("InitNew failed: %08x\n", hres
);
164 hres
= IActiveScript_SetScriptSite(script_host
->script
, &script_host
->IActiveScriptSite_iface
);
166 WARN("SetScriptSite failed: %08x\n", hres
);
167 IActiveScript_Close(script_host
->script
);
171 hres
= IActiveScript_GetScriptState(script_host
->script
, &state
);
173 WARN("GetScriptState failed: %08x\n", hres
);
174 else if(state
!= SCRIPTSTATE_INITIALIZED
)
175 FIXME("state = %x\n", state
);
177 hres
= IActiveScript_SetScriptState(script_host
->script
, SCRIPTSTATE_STARTED
);
179 WARN("Starting script failed: %08x\n", hres
);
183 hres
= IActiveScript_AddNamedItem(script_host
->script
, windowW
,
184 SCRIPTITEM_ISVISIBLE
|SCRIPTITEM_ISSOURCE
|SCRIPTITEM_GLOBALMEMBERS
);
185 if(SUCCEEDED(hres
)) {
186 V_VT(&var
) = VT_BOOL
;
187 V_BOOL(&var
) = VARIANT_TRUE
;
188 set_script_prop(script_host
, SCRIPTPROP_ABBREVIATE_GLOBALNAME_RESOLUTION
, &var
);
190 WARN("AddNamedItem failed: %08x\n", hres
);
193 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptParseProcedure2
,
194 (void**)&script_host
->parse_proc
);
196 /* FIXME: QI for IActiveScriptParseProcedure */
197 WARN("Could not get IActiveScriptParseProcedure iface: %08x\n", hres
);
203 static void release_script_engine(ScriptHost
*This
)
208 switch(This
->script_state
) {
209 case SCRIPTSTATE_CONNECTED
:
210 IActiveScript_SetScriptState(This
->script
, SCRIPTSTATE_DISCONNECTED
);
212 case SCRIPTSTATE_STARTED
:
213 case SCRIPTSTATE_DISCONNECTED
:
214 case SCRIPTSTATE_INITIALIZED
:
215 IActiveScript_Close(This
->script
);
218 if(This
->parse_proc
) {
219 IActiveScriptParseProcedure2_Release(This
->parse_proc
);
220 This
->parse_proc
= NULL
;
224 IActiveScriptParse_Release(This
->parse
);
229 IActiveScript_Release(This
->script
);
231 This
->script_state
= SCRIPTSTATE_UNINITIALIZED
;
234 void connect_scripts(HTMLInnerWindow
*window
)
238 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
239 if(iter
->script_state
== SCRIPTSTATE_STARTED
)
240 IActiveScript_SetScriptState(iter
->script
, SCRIPTSTATE_CONNECTED
);
244 static inline ScriptHost
*impl_from_IActiveScriptSite(IActiveScriptSite
*iface
)
246 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSite_iface
);
249 static HRESULT WINAPI
ActiveScriptSite_QueryInterface(IActiveScriptSite
*iface
, REFIID riid
, void **ppv
)
251 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
255 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
256 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
257 *ppv
= &This
->IActiveScriptSite_iface
;
258 }else if(IsEqualGUID(&IID_IActiveScriptSite
, riid
)) {
259 TRACE("(%p)->(IID_IActiveScriptSite %p)\n", This
, ppv
);
260 *ppv
= &This
->IActiveScriptSite_iface
;
261 }else if(IsEqualGUID(&IID_IActiveScriptSiteInterruptPoll
, riid
)) {
262 TRACE("(%p)->(IID_IActiveScriptSiteInterruprtPoll %p)\n", This
, ppv
);
263 *ppv
= &This
->IActiveScriptSiteInterruptPoll_iface
;
264 }else if(IsEqualGUID(&IID_IActiveScriptSiteWindow
, riid
)) {
265 TRACE("(%p)->(IID_IActiveScriptSiteWindow %p)\n", This
, ppv
);
266 *ppv
= &This
->IActiveScriptSiteWindow_iface
;
267 }else if(IsEqualGUID(&IID_IActiveScriptSiteUIControl
, riid
)) {
268 TRACE("(%p)->(IID_IActiveScriptSiteUIControl %p)\n", This
, ppv
);
269 *ppv
= &This
->IActiveScriptSiteUIControl_iface
;
270 }else if(IsEqualGUID(&IID_IActiveScriptSiteDebug
, riid
)) {
271 TRACE("(%p)->(IID_IActiveScriptSiteDebug %p)\n", This
, ppv
);
272 *ppv
= &This
->IActiveScriptSiteDebug_iface
;
273 }else if(IsEqualGUID(&IID_IServiceProvider
, riid
)) {
274 TRACE("(%p)->(IID_IServiceProvider %p)\n", This
, ppv
);
275 *ppv
= &This
->IServiceProvider_iface
;
276 }else if(IsEqualGUID(&IID_ICanHandleException
, riid
)) {
277 TRACE("(%p)->(IID_ICanHandleException not supported %p)\n", This
, ppv
);
278 return E_NOINTERFACE
;
280 FIXME("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
281 return E_NOINTERFACE
;
284 IUnknown_AddRef((IUnknown
*)*ppv
);
288 static ULONG WINAPI
ActiveScriptSite_AddRef(IActiveScriptSite
*iface
)
290 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
291 LONG ref
= InterlockedIncrement(&This
->ref
);
293 TRACE("(%p) ref=%d\n", This
, ref
);
298 static ULONG WINAPI
ActiveScriptSite_Release(IActiveScriptSite
*iface
)
300 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
301 LONG ref
= InterlockedDecrement(&This
->ref
);
303 TRACE("(%p) ref=%d\n", This
, ref
);
306 release_script_engine(This
);
308 list_remove(&This
->entry
);
315 static HRESULT WINAPI
ActiveScriptSite_GetLCID(IActiveScriptSite
*iface
, LCID
*plcid
)
317 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
319 TRACE("(%p)->(%p)\n", This
, plcid
);
321 *plcid
= GetUserDefaultLCID();
325 static HRESULT WINAPI
ActiveScriptSite_GetItemInfo(IActiveScriptSite
*iface
, LPCOLESTR pstrName
,
326 DWORD dwReturnMask
, IUnknown
**ppiunkItem
, ITypeInfo
**ppti
)
328 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
330 TRACE("(%p)->(%s %x %p %p)\n", This
, debugstr_w(pstrName
), dwReturnMask
, ppiunkItem
, ppti
);
332 if(dwReturnMask
!= SCRIPTINFO_IUNKNOWN
) {
333 FIXME("Unsupported mask %x\n", dwReturnMask
);
339 if(strcmpW(pstrName
, windowW
))
340 return DISP_E_MEMBERNOTFOUND
;
345 /* FIXME: Return proxy object */
346 *ppiunkItem
= (IUnknown
*)&This
->window
->base
.IHTMLWindow2_iface
;
347 IUnknown_AddRef(*ppiunkItem
);
352 static HRESULT WINAPI
ActiveScriptSite_GetDocVersionString(IActiveScriptSite
*iface
, BSTR
*pbstrVersion
)
354 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
355 FIXME("(%p)->(%p)\n", This
, pbstrVersion
);
359 static HRESULT WINAPI
ActiveScriptSite_OnScriptTerminate(IActiveScriptSite
*iface
,
360 const VARIANT
*pvarResult
, const EXCEPINFO
*pexcepinfo
)
362 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
363 FIXME("(%p)->(%p %p)\n", This
, pvarResult
, pexcepinfo
);
367 static HRESULT WINAPI
ActiveScriptSite_OnStateChange(IActiveScriptSite
*iface
, SCRIPTSTATE ssScriptState
)
369 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
371 TRACE("(%p)->(%x)\n", This
, ssScriptState
);
373 This
->script_state
= ssScriptState
;
377 static HRESULT WINAPI
ActiveScriptSite_OnScriptError(IActiveScriptSite
*iface
, IActiveScriptError
*pscripterror
)
379 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
380 FIXME("(%p)->(%p)\n", This
, pscripterror
);
384 static HRESULT WINAPI
ActiveScriptSite_OnEnterScript(IActiveScriptSite
*iface
)
386 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
388 TRACE("(%p)->()\n", This
);
393 static HRESULT WINAPI
ActiveScriptSite_OnLeaveScript(IActiveScriptSite
*iface
)
395 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
397 TRACE("(%p)->()\n", This
);
402 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl
= {
403 ActiveScriptSite_QueryInterface
,
404 ActiveScriptSite_AddRef
,
405 ActiveScriptSite_Release
,
406 ActiveScriptSite_GetLCID
,
407 ActiveScriptSite_GetItemInfo
,
408 ActiveScriptSite_GetDocVersionString
,
409 ActiveScriptSite_OnScriptTerminate
,
410 ActiveScriptSite_OnStateChange
,
411 ActiveScriptSite_OnScriptError
,
412 ActiveScriptSite_OnEnterScript
,
413 ActiveScriptSite_OnLeaveScript
416 static inline ScriptHost
*impl_from_IActiveScriptSiteInterruptPoll(IActiveScriptSiteInterruptPoll
*iface
)
418 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteInterruptPoll_iface
);
421 static HRESULT WINAPI
ActiveScriptSiteInterruptPoll_QueryInterface(IActiveScriptSiteInterruptPoll
*iface
,
422 REFIID riid
, void **ppv
)
424 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
425 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
428 static ULONG WINAPI
ActiveScriptSiteInterruptPoll_AddRef(IActiveScriptSiteInterruptPoll
*iface
)
430 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
431 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
434 static ULONG WINAPI
ActiveScriptSiteInterruptPoll_Release(IActiveScriptSiteInterruptPoll
*iface
)
436 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
437 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
440 static HRESULT WINAPI
ActiveScriptSiteInterruptPoll_QueryContinue(IActiveScriptSiteInterruptPoll
*iface
)
442 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
444 TRACE("(%p)\n", This
);
449 static const IActiveScriptSiteInterruptPollVtbl ActiveScriptSiteInterruptPollVtbl
= {
450 ActiveScriptSiteInterruptPoll_QueryInterface
,
451 ActiveScriptSiteInterruptPoll_AddRef
,
452 ActiveScriptSiteInterruptPoll_Release
,
453 ActiveScriptSiteInterruptPoll_QueryContinue
456 static inline ScriptHost
*impl_from_IActiveScriptSiteWindow(IActiveScriptSiteWindow
*iface
)
458 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteWindow_iface
);
461 static HRESULT WINAPI
ActiveScriptSiteWindow_QueryInterface(IActiveScriptSiteWindow
*iface
,
462 REFIID riid
, void **ppv
)
464 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
465 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
468 static ULONG WINAPI
ActiveScriptSiteWindow_AddRef(IActiveScriptSiteWindow
*iface
)
470 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
471 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
474 static ULONG WINAPI
ActiveScriptSiteWindow_Release(IActiveScriptSiteWindow
*iface
)
476 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
477 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
480 static HRESULT WINAPI
ActiveScriptSiteWindow_GetWindow(IActiveScriptSiteWindow
*iface
, HWND
*phwnd
)
482 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
484 TRACE("(%p)->(%p)\n", This
, phwnd
);
486 if(!This
->window
|| !This
->window
->base
.outer_window
|| !This
->window
->base
.outer_window
->doc_obj
)
489 *phwnd
= This
->window
->base
.outer_window
->doc_obj
->hwnd
;
493 static HRESULT WINAPI
ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow
*iface
, BOOL fEnable
)
495 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
496 FIXME("(%p)->(%x)\n", This
, fEnable
);
500 static const IActiveScriptSiteWindowVtbl ActiveScriptSiteWindowVtbl
= {
501 ActiveScriptSiteWindow_QueryInterface
,
502 ActiveScriptSiteWindow_AddRef
,
503 ActiveScriptSiteWindow_Release
,
504 ActiveScriptSiteWindow_GetWindow
,
505 ActiveScriptSiteWindow_EnableModeless
508 static inline ScriptHost
*impl_from_IActiveScriptSiteUIControl(IActiveScriptSiteUIControl
*iface
)
510 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteUIControl_iface
);
513 static HRESULT WINAPI
ActiveScriptSiteUIControl_QueryInterface(IActiveScriptSiteUIControl
*iface
, REFIID riid
, void **ppv
)
515 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
516 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
519 static ULONG WINAPI
ActiveScriptSiteUIControl_AddRef(IActiveScriptSiteUIControl
*iface
)
521 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
522 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
525 static ULONG WINAPI
ActiveScriptSiteUIControl_Release(IActiveScriptSiteUIControl
*iface
)
527 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
528 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
531 static HRESULT WINAPI
ActiveScriptSiteUIControl_GetUIBehavior(IActiveScriptSiteUIControl
*iface
, SCRIPTUICITEM UicItem
,
532 SCRIPTUICHANDLING
*pUicHandling
)
534 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
536 WARN("(%p)->(%d %p) semi-stub\n", This
, UicItem
, pUicHandling
);
538 *pUicHandling
= SCRIPTUICHANDLING_ALLOW
;
542 static const IActiveScriptSiteUIControlVtbl ActiveScriptSiteUIControlVtbl
= {
543 ActiveScriptSiteUIControl_QueryInterface
,
544 ActiveScriptSiteUIControl_AddRef
,
545 ActiveScriptSiteUIControl_Release
,
546 ActiveScriptSiteUIControl_GetUIBehavior
549 static inline ScriptHost
*impl_from_IActiveScriptSiteDebug(IActiveScriptSiteDebug
*iface
)
551 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteDebug_iface
);
554 static HRESULT WINAPI
ActiveScriptSiteDebug_QueryInterface(IActiveScriptSiteDebug
*iface
,
555 REFIID riid
, void **ppv
)
557 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
558 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
561 static ULONG WINAPI
ActiveScriptSiteDebug_AddRef(IActiveScriptSiteDebug
*iface
)
563 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
564 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
567 static ULONG WINAPI
ActiveScriptSiteDebug_Release(IActiveScriptSiteDebug
*iface
)
569 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
570 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
573 static HRESULT WINAPI
ActiveScriptSiteDebug_GetDocumentContextFromPosition(IActiveScriptSiteDebug
*iface
,
574 CTXARG_T dwSourceContext
, ULONG uCharacterOffset
, ULONG uNumChars
, IDebugDocumentContext
**ppsc
)
576 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
577 FIXME("(%p)->(%s %u %u %p)\n", This
, wine_dbgstr_longlong(dwSourceContext
), uCharacterOffset
,
582 static HRESULT WINAPI
ActiveScriptSiteDebug_GetApplication(IActiveScriptSiteDebug
*iface
, IDebugApplication
**ppda
)
584 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
585 FIXME("(%p)->(%p)\n", This
, ppda
);
589 static HRESULT WINAPI
ActiveScriptSiteDebug_GetRootApplicationNode(IActiveScriptSiteDebug
*iface
,
590 IDebugApplicationNode
**ppdanRoot
)
592 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
593 FIXME("(%p)->(%p)\n", This
, ppdanRoot
);
597 static HRESULT WINAPI
ActiveScriptSiteDebug_OnScriptErrorDebug(IActiveScriptSiteDebug
*iface
,
598 IActiveScriptErrorDebug
*pErrorDebug
, BOOL
*pfEnterDebugger
, BOOL
*pfCallOnScriptErrorWhenContinuing
)
600 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
601 FIXME("(%p)->(%p %p %p)\n", This
, pErrorDebug
, pfEnterDebugger
, pfCallOnScriptErrorWhenContinuing
);
605 static const IActiveScriptSiteDebugVtbl ActiveScriptSiteDebugVtbl
= {
606 ActiveScriptSiteDebug_QueryInterface
,
607 ActiveScriptSiteDebug_AddRef
,
608 ActiveScriptSiteDebug_Release
,
609 ActiveScriptSiteDebug_GetDocumentContextFromPosition
,
610 ActiveScriptSiteDebug_GetApplication
,
611 ActiveScriptSiteDebug_GetRootApplicationNode
,
612 ActiveScriptSiteDebug_OnScriptErrorDebug
615 static inline ScriptHost
*impl_from_IServiceProvider(IServiceProvider
*iface
)
617 return CONTAINING_RECORD(iface
, ScriptHost
, IServiceProvider_iface
);
620 static HRESULT WINAPI
ASServiceProvider_QueryInterface(IServiceProvider
*iface
, REFIID riid
, void **ppv
)
622 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
623 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
626 static ULONG WINAPI
ASServiceProvider_AddRef(IServiceProvider
*iface
)
628 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
629 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
632 static ULONG WINAPI
ASServiceProvider_Release(IServiceProvider
*iface
)
634 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
635 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
638 static HRESULT WINAPI
ASServiceProvider_QueryService(IServiceProvider
*iface
, REFGUID guidService
,
639 REFIID riid
, void **ppv
)
641 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
643 if(IsEqualGUID(&SID_SInternetHostSecurityManager
, guidService
)) {
644 TRACE("(%p)->(SID_SInternetHostSecurityManager)\n", This
);
646 if(!This
->window
|| !This
->window
->doc
)
647 return E_NOINTERFACE
;
649 return IInternetHostSecurityManager_QueryInterface(&This
->window
->doc
->IInternetHostSecurityManager_iface
,
653 if(IsEqualGUID(&SID_SContainerDispatch
, guidService
)) {
654 TRACE("(%p)->(SID_SContainerDispatch)\n", This
);
656 if(!This
->window
|| !This
->window
->doc
)
657 return E_NOINTERFACE
;
659 return IHTMLDocument2_QueryInterface(&This
->window
->doc
->basedoc
.IHTMLDocument2_iface
, riid
, ppv
);
662 FIXME("(%p)->(%s %s %p)\n", This
, debugstr_guid(guidService
), debugstr_guid(riid
), ppv
);
663 return E_NOINTERFACE
;
666 static const IServiceProviderVtbl ASServiceProviderVtbl
= {
667 ASServiceProvider_QueryInterface
,
668 ASServiceProvider_AddRef
,
669 ASServiceProvider_Release
,
670 ASServiceProvider_QueryService
673 static ScriptHost
*create_script_host(HTMLInnerWindow
*window
, const GUID
*guid
)
678 ret
= heap_alloc_zero(sizeof(*ret
));
682 ret
->IActiveScriptSite_iface
.lpVtbl
= &ActiveScriptSiteVtbl
;
683 ret
->IActiveScriptSiteInterruptPoll_iface
.lpVtbl
= &ActiveScriptSiteInterruptPollVtbl
;
684 ret
->IActiveScriptSiteWindow_iface
.lpVtbl
= &ActiveScriptSiteWindowVtbl
;
685 ret
->IActiveScriptSiteUIControl_iface
.lpVtbl
= &ActiveScriptSiteUIControlVtbl
;
686 ret
->IActiveScriptSiteDebug_iface
.lpVtbl
= &ActiveScriptSiteDebugVtbl
;
687 ret
->IServiceProvider_iface
.lpVtbl
= &ASServiceProviderVtbl
;
689 ret
->window
= window
;
690 ret
->script_state
= SCRIPTSTATE_UNINITIALIZED
;
693 list_add_tail(&window
->script_hosts
, &ret
->entry
);
695 hres
= CoCreateInstance(&ret
->guid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
696 &IID_IActiveScript
, (void**)&ret
->script
);
698 WARN("Could not load script engine: %08x\n", hres
);
699 else if(!init_script_engine(ret
))
700 release_script_engine(ret
);
707 HTMLScriptElement
*elem
;
708 } fire_readystatechange_task_t
;
710 static void fire_readystatechange_proc(task_t
*_task
)
712 fire_readystatechange_task_t
*task
= (fire_readystatechange_task_t
*)_task
;
714 if(!task
->elem
->pending_readystatechange_event
)
717 task
->elem
->pending_readystatechange_event
= FALSE
;
718 fire_event(task
->elem
->element
.node
.doc
, EVENTID_READYSTATECHANGE
, FALSE
, task
->elem
->element
.node
.nsnode
, NULL
, NULL
);
721 static void fire_readystatechange_task_destr(task_t
*_task
)
723 fire_readystatechange_task_t
*task
= (fire_readystatechange_task_t
*)_task
;
725 IHTMLScriptElement_Release(&task
->elem
->IHTMLScriptElement_iface
);
728 static void set_script_elem_readystate(HTMLScriptElement
*script_elem
, READYSTATE readystate
)
730 script_elem
->readystate
= readystate
;
732 if(readystate
!= READYSTATE_INTERACTIVE
) {
733 if(!script_elem
->element
.node
.doc
->window
->parser_callback_cnt
) {
734 fire_readystatechange_task_t
*task
;
737 if(script_elem
->pending_readystatechange_event
)
740 task
= heap_alloc(sizeof(*task
));
744 IHTMLScriptElement_AddRef(&script_elem
->IHTMLScriptElement_iface
);
745 task
->elem
= script_elem
;
747 hres
= push_task(&task
->header
, fire_readystatechange_proc
, fire_readystatechange_task_destr
,
748 script_elem
->element
.node
.doc
->window
->task_magic
);
750 script_elem
->pending_readystatechange_event
= TRUE
;
752 script_elem
->pending_readystatechange_event
= FALSE
;
753 fire_event(script_elem
->element
.node
.doc
, EVENTID_READYSTATECHANGE
, FALSE
,
754 script_elem
->element
.node
.nsnode
, NULL
, NULL
);
759 static void parse_elem_text(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
, LPCWSTR text
)
765 TRACE("%s\n", debugstr_w(text
));
767 set_script_elem_readystate(script_elem
, READYSTATE_INTERACTIVE
);
770 memset(&excepinfo
, 0, sizeof(excepinfo
));
772 hres
= IActiveScriptParse_ParseScriptText(script_host
->parse
, text
, windowW
, NULL
, script_endW
,
773 0, 0, SCRIPTTEXT_ISVISIBLE
|SCRIPTTEXT_HOSTMANAGESSOURCE
,
778 WARN("<<< %08x\n", hres
);
785 HTMLScriptElement
*script_elem
;
793 static inline ScriptBSC
*impl_from_BSCallback(BSCallback
*iface
)
795 return CONTAINING_RECORD(iface
, ScriptBSC
, bsc
);
798 static void ScriptBSC_destroy(BSCallback
*bsc
)
800 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
802 if(This
->script_elem
) {
803 IHTMLScriptElement_Release(&This
->script_elem
->IHTMLScriptElement_iface
);
804 This
->script_elem
= NULL
;
807 heap_free(This
->buf
);
811 static HRESULT
ScriptBSC_init_bindinfo(BSCallback
*bsc
)
816 static HRESULT
ScriptBSC_start_binding(BSCallback
*bsc
)
818 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
820 /* FIXME: We should find a better to decide if 'loading' state is supposed to be used by the protocol. */
821 if(This
->scheme
== URL_SCHEME_HTTPS
|| This
->scheme
== URL_SCHEME_HTTP
)
822 set_script_elem_readystate(This
->script_elem
, READYSTATE_LOADING
);
827 static HRESULT
ScriptBSC_stop_binding(BSCallback
*bsc
, HRESULT result
)
829 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
833 if(SUCCEEDED(result
)) {
834 if(This
->script_elem
->readystate
== READYSTATE_LOADING
)
835 set_script_elem_readystate(This
->script_elem
, READYSTATE_LOADED
);
837 FIXME("binding failed %08x\n", result
);
838 heap_free(This
->buf
);
843 IHTMLScriptElement_Release(&This
->script_elem
->IHTMLScriptElement_iface
);
844 This
->script_elem
= NULL
;
848 static HRESULT
ScriptBSC_read_data(BSCallback
*bsc
, IStream
*stream
)
850 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
855 This
->buf
= heap_alloc(128);
857 return E_OUTOFMEMORY
;
862 if(This
->bsc
.readed
>= This
->size
) {
864 new_buf
= heap_realloc(This
->buf
, This
->size
<< 1);
866 return E_OUTOFMEMORY
;
871 hres
= read_stream(&This
->bsc
, stream
, This
->buf
+This
->bsc
.readed
, This
->size
-This
->bsc
.readed
, &readed
);
872 }while(hres
== S_OK
);
877 static HRESULT
ScriptBSC_on_progress(BSCallback
*bsc
, ULONG status_code
, LPCWSTR status_text
)
882 static HRESULT
ScriptBSC_on_response(BSCallback
*bsc
, DWORD response_code
,
883 LPCWSTR response_headers
)
888 static HRESULT
ScriptBSC_beginning_transaction(BSCallback
*bsc
, WCHAR
**additional_headers
)
893 static const BSCallbackVtbl ScriptBSCVtbl
= {
895 ScriptBSC_init_bindinfo
,
896 ScriptBSC_start_binding
,
897 ScriptBSC_stop_binding
,
899 ScriptBSC_on_progress
,
900 ScriptBSC_on_response
,
901 ScriptBSC_beginning_transaction
905 static HRESULT
bind_script_to_text(HTMLInnerWindow
*window
, IUri
*uri
, HTMLScriptElement
*script_elem
, WCHAR
**ret
)
913 hres
= CreateURLMonikerEx2(NULL
, uri
, &mon
, URL_MK_UNIFORM
);
917 bsc
= heap_alloc_zero(sizeof(*bsc
));
919 IMoniker_Release(mon
);
920 return E_OUTOFMEMORY
;
923 init_bscallback(&bsc
->bsc
, &ScriptBSCVtbl
, mon
, 0);
924 IMoniker_Release(mon
);
927 hres
= IUri_GetScheme(uri
, &bsc
->scheme
);
929 bsc
->scheme
= URL_SCHEME_UNKNOWN
;
931 IHTMLScriptElement_AddRef(&script_elem
->IHTMLScriptElement_iface
);
932 bsc
->script_elem
= script_elem
;
934 hres
= start_binding(window
, &bsc
->bsc
, NULL
);
938 IBindStatusCallback_Release(&bsc
->bsc
.IBindStatusCallback_iface
);
942 if(!bsc
->bsc
.readed
) {
947 switch(bsc
->bsc
.bom
) {
949 if(bsc
->bsc
.readed
% sizeof(WCHAR
)) {
950 FIXME("The buffer is not a valid utf16 string\n");
955 text
= heap_alloc(bsc
->bsc
.readed
+sizeof(WCHAR
));
957 hres
= E_OUTOFMEMORY
;
961 memcpy(text
, bsc
->buf
, bsc
->bsc
.readed
);
962 text
[bsc
->bsc
.readed
/sizeof(WCHAR
)] = 0;
966 /* FIXME: Try to use charset from HTTP headers first */
967 cp
= get_document_charset(window
->doc
);
972 len
= MultiByteToWideChar(cp
, 0, bsc
->buf
, bsc
->bsc
.readed
, NULL
, 0);
973 text
= heap_alloc((len
+1)*sizeof(WCHAR
));
975 hres
= E_OUTOFMEMORY
;
979 MultiByteToWideChar(cp
, 0, bsc
->buf
, bsc
->bsc
.readed
, text
, len
);
984 IBindStatusCallback_Release(&bsc
->bsc
.IBindStatusCallback_iface
);
992 static void parse_extern_script(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
, LPCWSTR src
)
998 static const WCHAR wine_schemaW
[] = {'w','i','n','e',':'};
1000 if(strlenW(src
) > sizeof(wine_schemaW
)/sizeof(WCHAR
) && !memcmp(src
, wine_schemaW
, sizeof(wine_schemaW
)))
1001 src
+= sizeof(wine_schemaW
)/sizeof(WCHAR
);
1003 hres
= create_uri(src
, 0, &uri
);
1007 hres
= bind_script_to_text(script_host
->window
, uri
, script_elem
, &text
);
1009 if(FAILED(hres
) || !text
)
1012 parse_elem_text(script_host
, script_elem
, text
);
1017 static void parse_inline_script(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
)
1019 const PRUnichar
*text
;
1023 nsAString_Init(&text_str
, NULL
);
1024 nsres
= nsIDOMHTMLScriptElement_GetText(script_elem
->nsscript
, &text_str
);
1025 nsAString_GetData(&text_str
, &text
);
1027 if(NS_FAILED(nsres
)) {
1028 ERR("GetText failed: %08x\n", nsres
);
1030 parse_elem_text(script_host
, script_elem
, text
);
1033 nsAString_Finish(&text_str
);
1036 static void parse_script_elem(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
)
1038 nsAString src_str
, event_str
;
1039 const PRUnichar
*src
;
1042 nsAString_Init(&event_str
, NULL
);
1043 nsres
= nsIDOMHTMLScriptElement_GetEvent(script_elem
->nsscript
, &event_str
);
1044 if(NS_SUCCEEDED(nsres
)) {
1045 const PRUnichar
*event
;
1047 nsAString_GetData(&event_str
, &event
);
1049 TRACE("deferring event %s script evaluation\n", debugstr_w(event
));
1050 nsAString_Finish(&event_str
);
1054 ERR("GetEvent failed: %08x\n", nsres
);
1056 nsAString_Finish(&event_str
);
1058 nsAString_Init(&src_str
, NULL
);
1059 nsres
= nsIDOMHTMLScriptElement_GetSrc(script_elem
->nsscript
, &src_str
);
1060 nsAString_GetData(&src_str
, &src
);
1062 if(NS_FAILED(nsres
)) {
1063 ERR("GetSrc failed: %08x\n", nsres
);
1065 script_elem
->parsed
= TRUE
;
1066 parse_extern_script(script_host
, script_elem
, src
);
1068 parse_inline_script(script_host
, script_elem
);
1071 nsAString_Finish(&src_str
);
1073 set_script_elem_readystate(script_elem
, READYSTATE_COMPLETE
);
1076 static GUID
get_default_script_guid(HTMLInnerWindow
*window
)
1078 /* If not specified, we should use very first script host that was created for the page (or JScript if none) */
1079 return list_empty(&window
->script_hosts
)
1081 : LIST_ENTRY(list_head(&window
->script_hosts
), ScriptHost
, entry
)->guid
;
1084 static BOOL
get_guid_from_type(LPCWSTR type
, GUID
*guid
)
1086 const WCHAR text_javascriptW
[] =
1087 {'t','e','x','t','/','j','a','v','a','s','c','r','i','p','t',0};
1088 const WCHAR text_jscriptW
[] =
1089 {'t','e','x','t','/','j','s','c','r','i','p','t',0};
1090 const WCHAR text_vbscriptW
[] =
1091 {'t','e','x','t','/','v','b','s','c','r','i','p','t',0};
1093 /* FIXME: Handle more types */
1094 if(!strcmpiW(type
, text_javascriptW
) || !strcmpiW(type
, text_jscriptW
)) {
1095 *guid
= CLSID_JScript
;
1096 }else if(!strcmpiW(type
, text_vbscriptW
)) {
1097 *guid
= CLSID_VBScript
;
1099 FIXME("Unknown type %s\n", debugstr_w(type
));
1106 static BOOL
get_guid_from_language(LPCWSTR type
, GUID
*guid
)
1110 hres
= CLSIDFromProgID(type
, guid
);
1114 /* FIXME: Check CATID_ActiveScriptParse */
1119 static BOOL
get_script_guid(HTMLInnerWindow
*window
, nsIDOMHTMLScriptElement
*nsscript
, GUID
*guid
)
1121 nsIDOMHTMLElement
*nselem
;
1122 const PRUnichar
*language
;
1127 static const PRUnichar languageW
[] = {'l','a','n','g','u','a','g','e',0};
1129 nsAString_Init(&val_str
, NULL
);
1131 nsres
= nsIDOMHTMLScriptElement_GetType(nsscript
, &val_str
);
1132 if(NS_SUCCEEDED(nsres
)) {
1133 const PRUnichar
*type
;
1135 nsAString_GetData(&val_str
, &type
);
1137 ret
= get_guid_from_type(type
, guid
);
1138 nsAString_Finish(&val_str
);
1142 ERR("GetType failed: %08x\n", nsres
);
1145 nsres
= nsIDOMHTMLScriptElement_QueryInterface(nsscript
, &IID_nsIDOMHTMLElement
, (void**)&nselem
);
1146 assert(nsres
== NS_OK
);
1148 nsres
= get_elem_attr_value(nselem
, languageW
, &val_str
, &language
);
1149 nsIDOMHTMLElement_Release(nselem
);
1150 if(NS_SUCCEEDED(nsres
)) {
1152 ret
= get_guid_from_language(language
, guid
);
1154 *guid
= get_default_script_guid(window
);
1157 nsAString_Finish(&val_str
);
1163 static ScriptHost
*get_script_host(HTMLInnerWindow
*window
, const GUID
*guid
)
1167 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
1168 if(IsEqualGUID(guid
, &iter
->guid
))
1172 return create_script_host(window
, guid
);
1175 static ScriptHost
*get_elem_script_host(HTMLInnerWindow
*window
, HTMLScriptElement
*script_elem
)
1179 if(!get_script_guid(window
, script_elem
->nsscript
, &guid
)) {
1180 WARN("Could not find script GUID\n");
1184 if(IsEqualGUID(&CLSID_JScript
, &guid
)
1185 && (!window
->base
.outer_window
|| window
->base
.outer_window
->scriptmode
!= SCRIPTMODE_ACTIVESCRIPT
)) {
1186 TRACE("Ignoring JScript\n");
1190 return get_script_host(window
, &guid
);
1193 void doc_insert_script(HTMLInnerWindow
*window
, HTMLScriptElement
*script_elem
)
1195 ScriptHost
*script_host
;
1197 script_host
= get_elem_script_host(window
, script_elem
);
1201 if(script_host
->parse
)
1202 parse_script_elem(script_host
, script_elem
);
1205 IDispatch
*script_parse_event(HTMLInnerWindow
*window
, LPCWSTR text
)
1207 ScriptHost
*script_host
;
1213 static const WCHAR delimiterW
[] = {'\"',0};
1215 TRACE("%s\n", debugstr_w(text
));
1217 for(ptr
= text
; isalnumW(*ptr
); ptr
++);
1222 language
= heap_alloc((ptr
-text
+1)*sizeof(WCHAR
));
1226 memcpy(language
, text
, (ptr
-text
)*sizeof(WCHAR
));
1227 language
[ptr
-text
] = 0;
1229 b
= get_guid_from_language(language
, &guid
);
1231 heap_free(language
);
1234 WARN("Could not find language\n");
1241 guid
= get_default_script_guid(window
);
1244 if(IsEqualGUID(&CLSID_JScript
, &guid
)
1245 && (!window
->base
.outer_window
|| window
->base
.outer_window
->scriptmode
!= SCRIPTMODE_ACTIVESCRIPT
)) {
1246 TRACE("Ignoring JScript\n");
1250 script_host
= get_script_host(window
, &guid
);
1251 if(!script_host
|| !script_host
->parse_proc
)
1254 hres
= IActiveScriptParseProcedure2_ParseProcedureText(script_host
->parse_proc
, ptr
, NULL
, emptyW
,
1255 NULL
, NULL
, delimiterW
, 0 /* FIXME */, 0,
1256 SCRIPTPROC_HOSTMANAGESSOURCE
|SCRIPTPROC_IMPLICIT_THIS
|SCRIPTPROC_IMPLICIT_PARENTS
, &disp
);
1258 WARN("ParseProcedureText failed: %08x\n", hres
);
1262 TRACE("ret %p\n", disp
);
1266 HRESULT
exec_script(HTMLInnerWindow
*window
, const WCHAR
*code
, const WCHAR
*lang
, VARIANT
*ret
)
1268 ScriptHost
*script_host
;
1273 static const WCHAR delimW
[] = {'"',0};
1275 if(!get_guid_from_language(lang
, &guid
)) {
1276 WARN("Could not find script GUID\n");
1277 return CO_E_CLASSSTRING
;
1280 script_host
= get_script_host(window
, &guid
);
1282 FIXME("No script host\n");
1286 if(!script_host
->parse
) {
1287 FIXME("script_host->parse == NULL\n");
1291 memset(&ei
, 0, sizeof(ei
));
1293 hres
= IActiveScriptParse_ParseScriptText(script_host
->parse
, code
, NULL
, NULL
, delimW
, 0, 0, SCRIPTTEXT_ISVISIBLE
, ret
, &ei
);
1297 WARN("<<< %08x\n", hres
);
1302 IDispatch
*get_script_disp(ScriptHost
*script_host
)
1307 if(!script_host
->script
)
1310 hres
= IActiveScript_GetScriptDispatch(script_host
->script
, windowW
, &disp
);
1317 static EventTarget
*find_event_target(HTMLDocumentNode
*doc
, HTMLScriptElement
*script_elem
)
1319 EventTarget
*event_target
= NULL
;
1320 const PRUnichar
*target_id
;
1321 nsAString target_id_str
;
1325 nsAString_Init(&target_id_str
, NULL
);
1326 nsres
= nsIDOMHTMLScriptElement_GetHtmlFor(script_elem
->nsscript
, &target_id_str
);
1327 if(NS_FAILED(nsres
)) {
1328 ERR("GetScriptFor failed: %08x\n", nsres
);
1329 nsAString_Finish(&target_id_str
);
1333 nsAString_GetData(&target_id_str
, &target_id
);
1335 FIXME("Empty for attribute\n");
1336 }else if(!strcmpW(target_id
, documentW
)) {
1337 event_target
= &doc
->node
.event_target
;
1338 htmldoc_addref(&doc
->basedoc
);
1339 }else if(!strcmpW(target_id
, windowW
)) {
1341 event_target
= &doc
->window
->event_target
;
1342 IDispatchEx_AddRef(&event_target
->dispex
.IDispatchEx_iface
);
1345 HTMLElement
*target_elem
;
1347 hres
= get_doc_elem_by_id(doc
, target_id
, &target_elem
);
1348 if(SUCCEEDED(hres
) && target_elem
) {
1349 event_target
= &target_elem
->node
.event_target
;
1353 nsAString_Finish(&target_id_str
);
1354 return event_target
;
1357 static BOOL
parse_event_str(WCHAR
*event
, const WCHAR
**args
)
1361 TRACE("%s\n", debugstr_w(event
));
1363 for(ptr
= event
; isalnumW(*ptr
); ptr
++);
1374 while(isalnumW(*ptr
) || isspaceW(*ptr
) || *ptr
== ',')
1384 static IDispatch
*parse_event_elem(HTMLDocumentNode
*doc
, HTMLScriptElement
*script_elem
, WCHAR
**ret_event
)
1386 ScriptHost
*script_host
;
1387 WCHAR
*event
= NULL
;
1394 if(script_elem
->parsed
)
1397 script_host
= get_elem_script_host(doc
->window
, script_elem
);
1398 if(!script_host
|| !script_host
->parse_proc
)
1401 nsAString_Init(&nsstr
, NULL
);
1402 nsres
= nsIDOMHTMLScriptElement_GetEvent(script_elem
->nsscript
, &nsstr
);
1403 if(NS_SUCCEEDED(nsres
)) {
1404 const PRUnichar
*event_val
;
1406 nsAString_GetData(&nsstr
, &event_val
);
1407 event
= heap_strdupW(event_val
);
1409 nsAString_Finish(&nsstr
);
1413 if(!parse_event_str(event
, &args
)) {
1414 WARN("parsing %s failed\n", debugstr_w(event
));
1419 nsAString_Init(&nsstr
, NULL
);
1420 nsres
= nsIDOMHTMLScriptElement_GetText(script_elem
->nsscript
, &nsstr
);
1421 if(NS_SUCCEEDED(nsres
)) {
1422 const PRUnichar
*text
;
1424 nsAString_GetData(&nsstr
, &text
);
1425 hres
= IActiveScriptParseProcedure2_ParseProcedureText(script_host
->parse_proc
, text
, args
,
1426 emptyW
, NULL
, NULL
, script_endW
, 0, 0,
1427 SCRIPTPROC_HOSTMANAGESSOURCE
|SCRIPTPROC_IMPLICIT_THIS
|SCRIPTPROC_IMPLICIT_PARENTS
, &disp
);
1431 ERR("GetText failed: %08x\n", nsres
);
1434 nsAString_Finish(&nsstr
);
1444 void bind_event_scripts(HTMLDocumentNode
*doc
)
1446 HTMLPluginContainer
*plugin_container
;
1447 nsIDOMHTMLScriptElement
*nsscript
;
1448 HTMLScriptElement
*script_elem
;
1449 EventTarget
*event_target
;
1450 nsIDOMNodeList
*node_list
;
1451 nsIDOMNode
*script_node
;
1452 nsAString selector_str
;
1453 IDispatch
*event_disp
;
1459 static const PRUnichar selectorW
[] = {'s','c','r','i','p','t','[','e','v','e','n','t',']',0};
1466 nsAString_InitDepend(&selector_str
, selectorW
);
1467 nsres
= nsIDOMHTMLDocument_QuerySelectorAll(doc
->nsdoc
, &selector_str
, &node_list
);
1468 nsAString_Finish(&selector_str
);
1469 if(NS_FAILED(nsres
)) {
1470 ERR("QuerySelectorAll failed: %08x\n", nsres
);
1477 nsres
= nsIDOMNodeList_GetLength(node_list
, &length
);
1478 assert(nsres
== NS_OK
);
1480 for(i
=0; i
< length
; i
++) {
1481 nsres
= nsIDOMNodeList_Item(node_list
, i
, &script_node
);
1482 if(NS_FAILED(nsres
) || !script_node
) {
1483 ERR("Item(%d) failed: %08x\n", i
, nsres
);
1487 nsres
= nsIDOMNode_QueryInterface(script_node
, &IID_nsIDOMHTMLScriptElement
, (void**)&nsscript
);
1488 assert(nsres
== NS_OK
);
1489 nsIDOMNode_Release(script_node
);
1491 hres
= script_elem_from_nsscript(doc
, nsscript
, &script_elem
);
1495 event_disp
= parse_event_elem(doc
, script_elem
, &event
);
1497 event_target
= find_event_target(doc
, script_elem
);
1499 hres
= IDispatchEx_QueryInterface(&event_target
->dispex
.IDispatchEx_iface
, &IID_HTMLPluginContainer
,
1500 (void**)&plugin_container
);
1502 bind_activex_event(doc
, plugin_container
, event
, event_disp
);
1504 bind_target_event(doc
, event_target
, event
, event_disp
);
1506 IDispatchEx_Release(&event_target
->dispex
.IDispatchEx_iface
);
1507 if(plugin_container
)
1508 node_release(&plugin_container
->element
.node
);
1512 IDispatch_Release(event_disp
);
1515 IHTMLScriptElement_Release(&script_elem
->IHTMLScriptElement_iface
);
1518 nsIDOMNodeList_Release(node_list
);
1521 BOOL
find_global_prop(HTMLInnerWindow
*window
, BSTR name
, DWORD flags
, ScriptHost
**ret_host
, DISPID
*ret_id
)
1523 IDispatchEx
*dispex
;
1528 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
1529 disp
= get_script_disp(iter
);
1533 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1534 if(SUCCEEDED(hres
)) {
1535 hres
= IDispatchEx_GetDispID(dispex
, name
, flags
& (~fdexNameEnsure
), ret_id
);
1536 IDispatchEx_Release(dispex
);
1538 FIXME("No IDispatchEx\n");
1542 IDispatch_Release(disp
);
1543 if(SUCCEEDED(hres
)) {
1552 static BOOL
is_jscript_available(void)
1554 static BOOL available
, checked
;
1558 HRESULT hres
= CoGetClassObject(&CLSID_JScript
, CLSCTX_INPROC_SERVER
, NULL
, &IID_IUnknown
, (void**)&unk
);
1560 if(SUCCEEDED(hres
)) {
1562 IUnknown_Release(unk
);
1572 void set_script_mode(HTMLOuterWindow
*window
, SCRIPTMODE mode
)
1574 nsIWebBrowserSetup
*setup
;
1577 if(mode
== SCRIPTMODE_ACTIVESCRIPT
&& !is_jscript_available()) {
1578 TRACE("jscript.dll not available\n");
1579 window
->scriptmode
= SCRIPTMODE_GECKO
;
1583 window
->scriptmode
= mode
;
1585 if(!window
->doc_obj
->nscontainer
|| !window
->doc_obj
->nscontainer
->webbrowser
)
1588 nsres
= nsIWebBrowser_QueryInterface(window
->doc_obj
->nscontainer
->webbrowser
,
1589 &IID_nsIWebBrowserSetup
, (void**)&setup
);
1590 if(NS_SUCCEEDED(nsres
)) {
1591 nsres
= nsIWebBrowserSetup_SetProperty(setup
, SETUP_ALLOW_JAVASCRIPT
,
1592 window
->scriptmode
== SCRIPTMODE_GECKO
);
1594 if(NS_SUCCEEDED(nsres
))
1595 nsres
= nsIWebBrowserSetup_SetProperty(setup
, SETUP_DISABLE_NOSCRIPT
, TRUE
);
1597 nsIWebBrowserSetup_Release(setup
);
1600 if(NS_FAILED(nsres
))
1601 ERR("JavaScript setup failed: %08x\n", nsres
);
1604 void release_script_hosts(HTMLInnerWindow
*window
)
1606 script_queue_entry_t
*queue_iter
;
1609 while(!list_empty(&window
->script_queue
)) {
1610 queue_iter
= LIST_ENTRY(list_head(&window
->script_queue
), script_queue_entry_t
, entry
);
1612 list_remove(&queue_iter
->entry
);
1613 IHTMLScriptElement_Release(&queue_iter
->script
->IHTMLScriptElement_iface
);
1614 heap_free(queue_iter
);
1617 while(!list_empty(&window
->script_hosts
)) {
1618 iter
= LIST_ENTRY(list_head(&window
->script_hosts
), ScriptHost
, entry
);
1620 release_script_engine(iter
);
1621 list_remove(&iter
->entry
);
1622 iter
->window
= NULL
;
1623 IActiveScriptSite_Release(&iter
->IActiveScriptSite_iface
);