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
70 static const WCHAR documentW
[] = {'d','o','c','u','m','e','n','t',0};
71 static const WCHAR windowW
[] = {'w','i','n','d','o','w',0};
72 static const WCHAR script_endW
[] = {'<','/','S','C','R','I','P','T','>',0};
73 static const WCHAR emptyW
[] = {0};
76 IActiveScriptSite IActiveScriptSite_iface
;
77 IActiveScriptSiteInterruptPoll IActiveScriptSiteInterruptPoll_iface
;
78 IActiveScriptSiteWindow IActiveScriptSiteWindow_iface
;
79 IActiveScriptSiteUIControl IActiveScriptSiteUIControl_iface
;
80 IActiveScriptSiteDebug IActiveScriptSiteDebug_iface
;
81 IServiceProvider IServiceProvider_iface
;
85 IActiveScript
*script
;
86 IActiveScriptParse
*parse
;
87 IActiveScriptParseProcedure2
*parse_proc
;
89 SCRIPTSTATE script_state
;
91 HTMLInnerWindow
*window
;
97 static ScriptHost
*get_elem_script_host(HTMLInnerWindow
*,HTMLScriptElement
*);
99 static BOOL
set_script_prop(ScriptHost
*script_host
, DWORD property
, VARIANT
*val
)
101 IActiveScriptProperty
*script_prop
;
104 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptProperty
,
105 (void**)&script_prop
);
107 WARN("Could not get IActiveScriptProperty iface: %08x\n", hres
);
111 hres
= IActiveScriptProperty_SetProperty(script_prop
, property
, NULL
, val
);
112 IActiveScriptProperty_Release(script_prop
);
114 WARN("SetProperty(%x) failed: %08x\n", property
, hres
);
121 static BOOL
init_script_engine(ScriptHost
*script_host
)
123 compat_mode_t compat_mode
;
124 IObjectSafety
*safety
;
126 DWORD supported_opts
=0, enabled_opts
=0, script_mode
;
130 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptParse
, (void**)&script_host
->parse
);
132 WARN("Could not get IActiveScriptParse: %08x\n", hres
);
136 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IObjectSafety
, (void**)&safety
);
138 FIXME("Could not get IObjectSafety: %08x\n", hres
);
142 hres
= IObjectSafety_GetInterfaceSafetyOptions(safety
, &IID_IActiveScriptParse
, &supported_opts
, &enabled_opts
);
144 FIXME("GetInterfaceSafetyOptions failed: %08x\n", hres
);
145 }else if(!(supported_opts
& INTERFACE_USES_DISPEX
)) {
146 FIXME("INTERFACE_USES_DISPEX is not supported\n");
148 hres
= IObjectSafety_SetInterfaceSafetyOptions(safety
, &IID_IActiveScriptParse
,
149 INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
,
150 INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
);
152 FIXME("SetInterfaceSafetyOptions failed: %08x\n", hres
);
155 IObjectSafety_Release(safety
);
159 compat_mode
= lock_document_mode(script_host
->window
->doc
);
160 script_mode
= compat_mode
< COMPAT_MODE_IE8
? SCRIPTLANGUAGEVERSION_5_7
: SCRIPTLANGUAGEVERSION_5_8
;
161 if(IsEqualGUID(&script_host
->guid
, &CLSID_JScript
)) {
162 if(compat_mode
>= COMPAT_MODE_IE9
)
163 script_mode
= SCRIPTLANGUAGEVERSION_ES5
;
164 script_mode
|= SCRIPTLANGUAGEVERSION_HTML
;
167 V_I4(&var
) = script_mode
;
168 if(!set_script_prop(script_host
, SCRIPTPROP_INVOKEVERSIONING
, &var
) && (script_mode
& SCRIPTLANGUAGEVERSION_HTML
)) {
169 /* If this failed, we're most likely using native jscript. */
170 WARN("Failed to set script mode to HTML version.\n");
171 V_I4(&var
) = compat_mode
< COMPAT_MODE_IE8
? SCRIPTLANGUAGEVERSION_5_7
: SCRIPTLANGUAGEVERSION_5_8
;
172 set_script_prop(script_host
, SCRIPTPROP_INVOKEVERSIONING
, &var
);
175 V_VT(&var
) = VT_BOOL
;
176 V_BOOL(&var
) = VARIANT_TRUE
;
177 set_script_prop(script_host
, SCRIPTPROP_HACK_TRIDENTEVENTSINK
, &var
);
179 hres
= IActiveScriptParse_InitNew(script_host
->parse
);
181 WARN("InitNew failed: %08x\n", hres
);
185 hres
= IActiveScript_SetScriptSite(script_host
->script
, &script_host
->IActiveScriptSite_iface
);
187 WARN("SetScriptSite failed: %08x\n", hres
);
188 IActiveScript_Close(script_host
->script
);
192 hres
= IActiveScript_GetScriptState(script_host
->script
, &state
);
194 WARN("GetScriptState failed: %08x\n", hres
);
195 else if(state
!= SCRIPTSTATE_INITIALIZED
)
196 FIXME("state = %x\n", state
);
198 hres
= IActiveScript_SetScriptState(script_host
->script
, SCRIPTSTATE_STARTED
);
200 WARN("Starting script failed: %08x\n", hres
);
204 hres
= IActiveScript_AddNamedItem(script_host
->script
, windowW
,
205 SCRIPTITEM_ISVISIBLE
|SCRIPTITEM_ISSOURCE
|SCRIPTITEM_GLOBALMEMBERS
);
206 if(SUCCEEDED(hres
)) {
207 V_VT(&var
) = VT_BOOL
;
208 V_BOOL(&var
) = VARIANT_TRUE
;
209 set_script_prop(script_host
, SCRIPTPROP_ABBREVIATE_GLOBALNAME_RESOLUTION
, &var
);
211 WARN("AddNamedItem failed: %08x\n", hres
);
214 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptParseProcedure2
,
215 (void**)&script_host
->parse_proc
);
217 /* FIXME: QI for IActiveScriptParseProcedure */
218 WARN("Could not get IActiveScriptParseProcedure iface: %08x\n", hres
);
224 static void release_script_engine(ScriptHost
*This
)
229 switch(This
->script_state
) {
230 case SCRIPTSTATE_CONNECTED
:
231 IActiveScript_SetScriptState(This
->script
, SCRIPTSTATE_DISCONNECTED
);
233 case SCRIPTSTATE_STARTED
:
234 case SCRIPTSTATE_DISCONNECTED
:
235 case SCRIPTSTATE_INITIALIZED
:
236 IActiveScript_Close(This
->script
);
239 if(This
->parse_proc
) {
240 IActiveScriptParseProcedure2_Release(This
->parse_proc
);
241 This
->parse_proc
= NULL
;
245 IActiveScriptParse_Release(This
->parse
);
250 IActiveScript_Release(This
->script
);
252 This
->script_state
= SCRIPTSTATE_UNINITIALIZED
;
255 void connect_scripts(HTMLInnerWindow
*window
)
259 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
260 if(iter
->script_state
== SCRIPTSTATE_STARTED
)
261 IActiveScript_SetScriptState(iter
->script
, SCRIPTSTATE_CONNECTED
);
265 static inline ScriptHost
*impl_from_IActiveScriptSite(IActiveScriptSite
*iface
)
267 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSite_iface
);
270 static HRESULT WINAPI
ActiveScriptSite_QueryInterface(IActiveScriptSite
*iface
, REFIID riid
, void **ppv
)
272 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
276 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
277 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
278 *ppv
= &This
->IActiveScriptSite_iface
;
279 }else if(IsEqualGUID(&IID_IActiveScriptSite
, riid
)) {
280 TRACE("(%p)->(IID_IActiveScriptSite %p)\n", This
, ppv
);
281 *ppv
= &This
->IActiveScriptSite_iface
;
282 }else if(IsEqualGUID(&IID_IActiveScriptSiteInterruptPoll
, riid
)) {
283 TRACE("(%p)->(IID_IActiveScriptSiteInterruprtPoll %p)\n", This
, ppv
);
284 *ppv
= &This
->IActiveScriptSiteInterruptPoll_iface
;
285 }else if(IsEqualGUID(&IID_IActiveScriptSiteWindow
, riid
)) {
286 TRACE("(%p)->(IID_IActiveScriptSiteWindow %p)\n", This
, ppv
);
287 *ppv
= &This
->IActiveScriptSiteWindow_iface
;
288 }else if(IsEqualGUID(&IID_IActiveScriptSiteUIControl
, riid
)) {
289 TRACE("(%p)->(IID_IActiveScriptSiteUIControl %p)\n", This
, ppv
);
290 *ppv
= &This
->IActiveScriptSiteUIControl_iface
;
291 }else if(IsEqualGUID(&IID_IActiveScriptSiteDebug
, riid
)) {
292 TRACE("(%p)->(IID_IActiveScriptSiteDebug %p)\n", This
, ppv
);
293 *ppv
= &This
->IActiveScriptSiteDebug_iface
;
294 }else if(IsEqualGUID(&IID_IServiceProvider
, riid
)) {
295 TRACE("(%p)->(IID_IServiceProvider %p)\n", This
, ppv
);
296 *ppv
= &This
->IServiceProvider_iface
;
297 }else if(IsEqualGUID(&IID_ICanHandleException
, riid
)) {
298 TRACE("(%p)->(IID_ICanHandleException not supported %p)\n", This
, ppv
);
299 return E_NOINTERFACE
;
301 FIXME("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
302 return E_NOINTERFACE
;
305 IUnknown_AddRef((IUnknown
*)*ppv
);
309 static ULONG WINAPI
ActiveScriptSite_AddRef(IActiveScriptSite
*iface
)
311 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
312 LONG ref
= InterlockedIncrement(&This
->ref
);
314 TRACE("(%p) ref=%d\n", This
, ref
);
319 static ULONG WINAPI
ActiveScriptSite_Release(IActiveScriptSite
*iface
)
321 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
322 LONG ref
= InterlockedDecrement(&This
->ref
);
324 TRACE("(%p) ref=%d\n", This
, ref
);
327 release_script_engine(This
);
329 list_remove(&This
->entry
);
336 static HRESULT WINAPI
ActiveScriptSite_GetLCID(IActiveScriptSite
*iface
, LCID
*plcid
)
338 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
340 TRACE("(%p)->(%p)\n", This
, plcid
);
342 *plcid
= GetUserDefaultLCID();
346 static HRESULT WINAPI
ActiveScriptSite_GetItemInfo(IActiveScriptSite
*iface
, LPCOLESTR pstrName
,
347 DWORD dwReturnMask
, IUnknown
**ppiunkItem
, ITypeInfo
**ppti
)
349 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
351 TRACE("(%p)->(%s %x %p %p)\n", This
, debugstr_w(pstrName
), dwReturnMask
, ppiunkItem
, ppti
);
353 if(dwReturnMask
!= SCRIPTINFO_IUNKNOWN
) {
354 FIXME("Unsupported mask %x\n", dwReturnMask
);
360 if(wcscmp(pstrName
, windowW
))
361 return DISP_E_MEMBERNOTFOUND
;
366 /* FIXME: Return proxy object */
367 *ppiunkItem
= (IUnknown
*)&This
->window
->base
.IHTMLWindow2_iface
;
368 IUnknown_AddRef(*ppiunkItem
);
373 static HRESULT WINAPI
ActiveScriptSite_GetDocVersionString(IActiveScriptSite
*iface
, BSTR
*pbstrVersion
)
375 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
376 FIXME("(%p)->(%p)\n", This
, pbstrVersion
);
380 static HRESULT WINAPI
ActiveScriptSite_OnScriptTerminate(IActiveScriptSite
*iface
,
381 const VARIANT
*pvarResult
, const EXCEPINFO
*pexcepinfo
)
383 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
384 FIXME("(%p)->(%p %p)\n", This
, pvarResult
, pexcepinfo
);
388 static HRESULT WINAPI
ActiveScriptSite_OnStateChange(IActiveScriptSite
*iface
, SCRIPTSTATE ssScriptState
)
390 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
392 TRACE("(%p)->(%x)\n", This
, ssScriptState
);
394 This
->script_state
= ssScriptState
;
398 static HRESULT WINAPI
ActiveScriptSite_OnScriptError(IActiveScriptSite
*iface
, IActiveScriptError
*pscripterror
)
400 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
401 FIXME("(%p)->(%p)\n", This
, pscripterror
);
405 static HRESULT WINAPI
ActiveScriptSite_OnEnterScript(IActiveScriptSite
*iface
)
407 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
409 TRACE("(%p)->()\n", This
);
414 static HRESULT WINAPI
ActiveScriptSite_OnLeaveScript(IActiveScriptSite
*iface
)
416 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
418 TRACE("(%p)->()\n", This
);
423 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl
= {
424 ActiveScriptSite_QueryInterface
,
425 ActiveScriptSite_AddRef
,
426 ActiveScriptSite_Release
,
427 ActiveScriptSite_GetLCID
,
428 ActiveScriptSite_GetItemInfo
,
429 ActiveScriptSite_GetDocVersionString
,
430 ActiveScriptSite_OnScriptTerminate
,
431 ActiveScriptSite_OnStateChange
,
432 ActiveScriptSite_OnScriptError
,
433 ActiveScriptSite_OnEnterScript
,
434 ActiveScriptSite_OnLeaveScript
437 static inline ScriptHost
*impl_from_IActiveScriptSiteInterruptPoll(IActiveScriptSiteInterruptPoll
*iface
)
439 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteInterruptPoll_iface
);
442 static HRESULT WINAPI
ActiveScriptSiteInterruptPoll_QueryInterface(IActiveScriptSiteInterruptPoll
*iface
,
443 REFIID riid
, void **ppv
)
445 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
446 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
449 static ULONG WINAPI
ActiveScriptSiteInterruptPoll_AddRef(IActiveScriptSiteInterruptPoll
*iface
)
451 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
452 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
455 static ULONG WINAPI
ActiveScriptSiteInterruptPoll_Release(IActiveScriptSiteInterruptPoll
*iface
)
457 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
458 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
461 static HRESULT WINAPI
ActiveScriptSiteInterruptPoll_QueryContinue(IActiveScriptSiteInterruptPoll
*iface
)
463 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
465 TRACE("(%p)\n", This
);
470 static const IActiveScriptSiteInterruptPollVtbl ActiveScriptSiteInterruptPollVtbl
= {
471 ActiveScriptSiteInterruptPoll_QueryInterface
,
472 ActiveScriptSiteInterruptPoll_AddRef
,
473 ActiveScriptSiteInterruptPoll_Release
,
474 ActiveScriptSiteInterruptPoll_QueryContinue
477 static inline ScriptHost
*impl_from_IActiveScriptSiteWindow(IActiveScriptSiteWindow
*iface
)
479 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteWindow_iface
);
482 static HRESULT WINAPI
ActiveScriptSiteWindow_QueryInterface(IActiveScriptSiteWindow
*iface
,
483 REFIID riid
, void **ppv
)
485 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
486 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
489 static ULONG WINAPI
ActiveScriptSiteWindow_AddRef(IActiveScriptSiteWindow
*iface
)
491 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
492 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
495 static ULONG WINAPI
ActiveScriptSiteWindow_Release(IActiveScriptSiteWindow
*iface
)
497 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
498 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
501 static HRESULT WINAPI
ActiveScriptSiteWindow_GetWindow(IActiveScriptSiteWindow
*iface
, HWND
*phwnd
)
503 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
505 TRACE("(%p)->(%p)\n", This
, phwnd
);
507 if(!This
->window
|| !This
->window
->base
.outer_window
)
510 *phwnd
= This
->window
->base
.outer_window
->browser
->doc
->hwnd
;
514 static HRESULT WINAPI
ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow
*iface
, BOOL fEnable
)
516 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
517 FIXME("(%p)->(%x)\n", This
, fEnable
);
521 static const IActiveScriptSiteWindowVtbl ActiveScriptSiteWindowVtbl
= {
522 ActiveScriptSiteWindow_QueryInterface
,
523 ActiveScriptSiteWindow_AddRef
,
524 ActiveScriptSiteWindow_Release
,
525 ActiveScriptSiteWindow_GetWindow
,
526 ActiveScriptSiteWindow_EnableModeless
529 static inline ScriptHost
*impl_from_IActiveScriptSiteUIControl(IActiveScriptSiteUIControl
*iface
)
531 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteUIControl_iface
);
534 static HRESULT WINAPI
ActiveScriptSiteUIControl_QueryInterface(IActiveScriptSiteUIControl
*iface
, REFIID riid
, void **ppv
)
536 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
537 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
540 static ULONG WINAPI
ActiveScriptSiteUIControl_AddRef(IActiveScriptSiteUIControl
*iface
)
542 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
543 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
546 static ULONG WINAPI
ActiveScriptSiteUIControl_Release(IActiveScriptSiteUIControl
*iface
)
548 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
549 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
552 static HRESULT WINAPI
ActiveScriptSiteUIControl_GetUIBehavior(IActiveScriptSiteUIControl
*iface
, SCRIPTUICITEM UicItem
,
553 SCRIPTUICHANDLING
*pUicHandling
)
555 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
557 WARN("(%p)->(%d %p) semi-stub\n", This
, UicItem
, pUicHandling
);
559 *pUicHandling
= SCRIPTUICHANDLING_ALLOW
;
563 static const IActiveScriptSiteUIControlVtbl ActiveScriptSiteUIControlVtbl
= {
564 ActiveScriptSiteUIControl_QueryInterface
,
565 ActiveScriptSiteUIControl_AddRef
,
566 ActiveScriptSiteUIControl_Release
,
567 ActiveScriptSiteUIControl_GetUIBehavior
570 static inline ScriptHost
*impl_from_IActiveScriptSiteDebug(IActiveScriptSiteDebug
*iface
)
572 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteDebug_iface
);
575 static HRESULT WINAPI
ActiveScriptSiteDebug_QueryInterface(IActiveScriptSiteDebug
*iface
,
576 REFIID riid
, void **ppv
)
578 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
579 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
582 static ULONG WINAPI
ActiveScriptSiteDebug_AddRef(IActiveScriptSiteDebug
*iface
)
584 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
585 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
588 static ULONG WINAPI
ActiveScriptSiteDebug_Release(IActiveScriptSiteDebug
*iface
)
590 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
591 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
594 static HRESULT WINAPI
ActiveScriptSiteDebug_GetDocumentContextFromPosition(IActiveScriptSiteDebug
*iface
,
595 CTXARG_T dwSourceContext
, ULONG uCharacterOffset
, ULONG uNumChars
, IDebugDocumentContext
**ppsc
)
597 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
598 FIXME("(%p)->(%s %u %u %p)\n", This
, wine_dbgstr_longlong(dwSourceContext
), uCharacterOffset
,
603 static HRESULT WINAPI
ActiveScriptSiteDebug_GetApplication(IActiveScriptSiteDebug
*iface
, IDebugApplication
**ppda
)
605 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
606 FIXME("(%p)->(%p)\n", This
, ppda
);
610 static HRESULT WINAPI
ActiveScriptSiteDebug_GetRootApplicationNode(IActiveScriptSiteDebug
*iface
,
611 IDebugApplicationNode
**ppdanRoot
)
613 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
614 FIXME("(%p)->(%p)\n", This
, ppdanRoot
);
618 static HRESULT WINAPI
ActiveScriptSiteDebug_OnScriptErrorDebug(IActiveScriptSiteDebug
*iface
,
619 IActiveScriptErrorDebug
*pErrorDebug
, BOOL
*pfEnterDebugger
, BOOL
*pfCallOnScriptErrorWhenContinuing
)
621 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
622 FIXME("(%p)->(%p %p %p)\n", This
, pErrorDebug
, pfEnterDebugger
, pfCallOnScriptErrorWhenContinuing
);
626 static const IActiveScriptSiteDebugVtbl ActiveScriptSiteDebugVtbl
= {
627 ActiveScriptSiteDebug_QueryInterface
,
628 ActiveScriptSiteDebug_AddRef
,
629 ActiveScriptSiteDebug_Release
,
630 ActiveScriptSiteDebug_GetDocumentContextFromPosition
,
631 ActiveScriptSiteDebug_GetApplication
,
632 ActiveScriptSiteDebug_GetRootApplicationNode
,
633 ActiveScriptSiteDebug_OnScriptErrorDebug
636 static inline ScriptHost
*impl_from_IServiceProvider(IServiceProvider
*iface
)
638 return CONTAINING_RECORD(iface
, ScriptHost
, IServiceProvider_iface
);
641 static HRESULT WINAPI
ASServiceProvider_QueryInterface(IServiceProvider
*iface
, REFIID riid
, void **ppv
)
643 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
644 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
647 static ULONG WINAPI
ASServiceProvider_AddRef(IServiceProvider
*iface
)
649 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
650 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
653 static ULONG WINAPI
ASServiceProvider_Release(IServiceProvider
*iface
)
655 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
656 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
659 static HRESULT WINAPI
ASServiceProvider_QueryService(IServiceProvider
*iface
, REFGUID guidService
,
660 REFIID riid
, void **ppv
)
662 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
664 if(IsEqualGUID(&SID_SInternetHostSecurityManager
, guidService
)) {
665 TRACE("(%p)->(SID_SInternetHostSecurityManager)\n", This
);
667 if(!This
->window
|| !This
->window
->doc
)
668 return E_NOINTERFACE
;
670 return IInternetHostSecurityManager_QueryInterface(&This
->window
->doc
->IInternetHostSecurityManager_iface
,
674 if(IsEqualGUID(&SID_SContainerDispatch
, guidService
)) {
675 TRACE("(%p)->(SID_SContainerDispatch)\n", This
);
677 if(!This
->window
|| !This
->window
->doc
)
678 return E_NOINTERFACE
;
680 return IHTMLDocument2_QueryInterface(&This
->window
->doc
->basedoc
.IHTMLDocument2_iface
, riid
, ppv
);
683 FIXME("(%p)->(%s %s %p)\n", This
, debugstr_guid(guidService
), debugstr_guid(riid
), ppv
);
684 return E_NOINTERFACE
;
687 static const IServiceProviderVtbl ASServiceProviderVtbl
= {
688 ASServiceProvider_QueryInterface
,
689 ASServiceProvider_AddRef
,
690 ASServiceProvider_Release
,
691 ASServiceProvider_QueryService
694 static ScriptHost
*create_script_host(HTMLInnerWindow
*window
, const GUID
*guid
)
699 ret
= heap_alloc_zero(sizeof(*ret
));
703 ret
->IActiveScriptSite_iface
.lpVtbl
= &ActiveScriptSiteVtbl
;
704 ret
->IActiveScriptSiteInterruptPoll_iface
.lpVtbl
= &ActiveScriptSiteInterruptPollVtbl
;
705 ret
->IActiveScriptSiteWindow_iface
.lpVtbl
= &ActiveScriptSiteWindowVtbl
;
706 ret
->IActiveScriptSiteUIControl_iface
.lpVtbl
= &ActiveScriptSiteUIControlVtbl
;
707 ret
->IActiveScriptSiteDebug_iface
.lpVtbl
= &ActiveScriptSiteDebugVtbl
;
708 ret
->IServiceProvider_iface
.lpVtbl
= &ASServiceProviderVtbl
;
710 ret
->window
= window
;
711 ret
->script_state
= SCRIPTSTATE_UNINITIALIZED
;
714 list_add_tail(&window
->script_hosts
, &ret
->entry
);
716 hres
= CoCreateInstance(&ret
->guid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
717 &IID_IActiveScript
, (void**)&ret
->script
);
719 WARN("Could not load script engine: %08x\n", hres
);
720 else if(!init_script_engine(ret
))
721 release_script_engine(ret
);
726 static void dispatch_script_readystatechange_event(HTMLScriptElement
*script
)
731 hres
= create_document_event(script
->element
.node
.doc
, EVENTID_READYSTATECHANGE
, &event
);
735 dispatch_event(&script
->element
.node
.event_target
, event
);
736 IDOMEvent_Release(&event
->IDOMEvent_iface
);
741 HTMLScriptElement
*elem
;
742 } fire_readystatechange_task_t
;
744 static void fire_readystatechange_proc(task_t
*_task
)
746 fire_readystatechange_task_t
*task
= (fire_readystatechange_task_t
*)_task
;
748 if(!task
->elem
->pending_readystatechange_event
)
751 task
->elem
->pending_readystatechange_event
= FALSE
;
752 dispatch_script_readystatechange_event(task
->elem
);
755 static void fire_readystatechange_task_destr(task_t
*_task
)
757 fire_readystatechange_task_t
*task
= (fire_readystatechange_task_t
*)_task
;
759 IHTMLScriptElement_Release(&task
->elem
->IHTMLScriptElement_iface
);
762 static void set_script_elem_readystate(HTMLScriptElement
*script_elem
, READYSTATE readystate
)
764 script_elem
->readystate
= readystate
;
766 if(readystate
!= READYSTATE_INTERACTIVE
) {
767 if(!script_elem
->element
.node
.doc
->window
->parser_callback_cnt
) {
768 fire_readystatechange_task_t
*task
;
771 if(script_elem
->pending_readystatechange_event
)
774 task
= heap_alloc(sizeof(*task
));
778 IHTMLScriptElement_AddRef(&script_elem
->IHTMLScriptElement_iface
);
779 task
->elem
= script_elem
;
781 hres
= push_task(&task
->header
, fire_readystatechange_proc
, fire_readystatechange_task_destr
,
782 script_elem
->element
.node
.doc
->window
->task_magic
);
784 script_elem
->pending_readystatechange_event
= TRUE
;
786 script_elem
->pending_readystatechange_event
= FALSE
;
787 dispatch_script_readystatechange_event(script_elem
);
792 static void parse_elem_text(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
, LPCWSTR text
)
798 TRACE("%s\n", debugstr_w(text
));
801 memset(&excepinfo
, 0, sizeof(excepinfo
));
803 hres
= IActiveScriptParse_ParseScriptText(script_host
->parse
, text
, windowW
, NULL
, script_endW
,
804 0, 0, SCRIPTTEXT_ISVISIBLE
|SCRIPTTEXT_HOSTMANAGESSOURCE
,
809 WARN("<<< %08x\n", hres
);
815 HTMLScriptElement
*script_elem
;
822 static HRESULT
get_binding_text(ScriptBSC
*bsc
, WCHAR
**ret
)
828 text
= heap_alloc(sizeof(WCHAR
));
830 return E_OUTOFMEMORY
;
836 switch(bsc
->bsc
.bom
) {
838 if(bsc
->bsc
.read
% sizeof(WCHAR
)) {
839 FIXME("The buffer is not a valid utf16 string\n");
843 text
= heap_alloc(bsc
->bsc
.read
+sizeof(WCHAR
));
845 return E_OUTOFMEMORY
;
847 memcpy(text
, bsc
->buf
, bsc
->bsc
.read
);
848 text
[bsc
->bsc
.read
/sizeof(WCHAR
)] = 0;
852 /* FIXME: Try to use charset from HTTP headers first */
853 cp
= get_document_charset(bsc
->script_elem
->element
.node
.doc
);
858 len
= MultiByteToWideChar(cp
, 0, bsc
->buf
, bsc
->bsc
.read
, NULL
, 0);
859 text
= heap_alloc((len
+1)*sizeof(WCHAR
));
861 return E_OUTOFMEMORY
;
863 MultiByteToWideChar(cp
, 0, bsc
->buf
, bsc
->bsc
.read
, text
, len
);
872 static void script_file_available(ScriptBSC
*bsc
)
874 HTMLScriptElement
*script_elem
= bsc
->script_elem
;
875 HTMLInnerWindow
*window
= bsc
->bsc
.window
;
876 ScriptHost
*script_host
;
881 assert(window
!= NULL
);
883 hres
= get_binding_text(bsc
, &script_elem
->src_text
);
887 script_host
= get_elem_script_host(window
, script_elem
);
891 if(window
->parser_callback_cnt
) {
892 script_queue_entry_t
*queue
;
894 TRACE("Adding to queue\n");
896 queue
= heap_alloc(sizeof(*queue
));
900 IHTMLScriptElement_AddRef(&script_elem
->IHTMLScriptElement_iface
);
901 queue
->script
= script_elem
;
903 list_add_tail(&window
->script_queue
, &queue
->entry
);
907 nsres
= nsIDOMElement_GetParentNode(script_elem
->element
.dom_element
, &parent
);
908 if(NS_FAILED(nsres
) || !parent
) {
909 TRACE("No parent, not executing\n");
910 script_elem
->parse_on_bind
= TRUE
;
914 nsIDOMNode_Release(parent
);
916 script_host
= get_elem_script_host(window
, script_elem
);
920 script_elem
->parsed
= TRUE
;
921 if(script_host
->parse
)
922 parse_elem_text(script_host
, script_elem
, script_elem
->src_text
);
925 static inline ScriptBSC
*impl_from_BSCallback(BSCallback
*iface
)
927 return CONTAINING_RECORD(iface
, ScriptBSC
, bsc
);
930 static void ScriptBSC_destroy(BSCallback
*bsc
)
932 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
934 if(This
->script_elem
) {
935 IHTMLScriptElement_Release(&This
->script_elem
->IHTMLScriptElement_iface
);
936 This
->script_elem
= NULL
;
939 heap_free(This
->buf
);
943 static HRESULT
ScriptBSC_init_bindinfo(BSCallback
*bsc
)
948 static HRESULT
ScriptBSC_start_binding(BSCallback
*bsc
)
950 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
952 This
->script_elem
->binding
= &This
->bsc
;
954 /* FIXME: We should find a better to decide if 'loading' state is supposed to be used by the protocol. */
955 if(This
->scheme
== URL_SCHEME_HTTPS
|| This
->scheme
== URL_SCHEME_HTTP
)
956 set_script_elem_readystate(This
->script_elem
, READYSTATE_LOADING
);
961 static HRESULT
ScriptBSC_stop_binding(BSCallback
*bsc
, HRESULT result
)
963 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
965 if(SUCCEEDED(result
) && !This
->script_elem
)
966 result
= E_UNEXPECTED
;
968 assert(FAILED(result
) || This
->script_elem
->binding
== &This
->bsc
);
969 This
->script_elem
->binding
= NULL
;
971 if(This
->script_elem
->readystate
== READYSTATE_LOADING
)
972 set_script_elem_readystate(This
->script_elem
, READYSTATE_LOADED
);
974 if(SUCCEEDED(result
)) {
975 script_file_available(This
);
977 FIXME("binding failed %08x\n", result
);
978 heap_free(This
->buf
);
983 IHTMLScriptElement_Release(&This
->script_elem
->IHTMLScriptElement_iface
);
984 This
->script_elem
= NULL
;
988 static HRESULT
ScriptBSC_read_data(BSCallback
*bsc
, IStream
*stream
)
990 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
995 This
->buf
= heap_alloc(128);
997 return E_OUTOFMEMORY
;
1002 if(This
->bsc
.read
>= This
->size
) {
1004 new_buf
= heap_realloc(This
->buf
, This
->size
<< 1);
1006 return E_OUTOFMEMORY
;
1008 This
->buf
= new_buf
;
1011 hres
= read_stream(&This
->bsc
, stream
, This
->buf
+This
->bsc
.read
, This
->size
-This
->bsc
.read
, &read
);
1012 }while(hres
== S_OK
);
1017 static HRESULT
ScriptBSC_on_progress(BSCallback
*bsc
, ULONG status_code
, LPCWSTR status_text
)
1022 static HRESULT
ScriptBSC_on_response(BSCallback
*bsc
, DWORD response_code
,
1023 LPCWSTR response_headers
)
1028 static HRESULT
ScriptBSC_beginning_transaction(BSCallback
*bsc
, WCHAR
**additional_headers
)
1033 static const BSCallbackVtbl ScriptBSCVtbl
= {
1035 ScriptBSC_init_bindinfo
,
1036 ScriptBSC_start_binding
,
1037 ScriptBSC_stop_binding
,
1038 ScriptBSC_read_data
,
1039 ScriptBSC_on_progress
,
1040 ScriptBSC_on_response
,
1041 ScriptBSC_beginning_transaction
1045 HRESULT
load_script(HTMLScriptElement
*script_elem
, const WCHAR
*src
, BOOL async
)
1047 HTMLInnerWindow
*window
;
1053 static const WCHAR wine_schemaW
[] = {'w','i','n','e',':'};
1055 if(lstrlenW(src
) > ARRAY_SIZE(wine_schemaW
) && !memcmp(src
, wine_schemaW
, sizeof(wine_schemaW
)))
1056 src
+= ARRAY_SIZE(wine_schemaW
);
1058 TRACE("(%p %s %x)\n", script_elem
, debugstr_w(src
), async
);
1060 if(!script_elem
->element
.node
.doc
|| !(window
= script_elem
->element
.node
.doc
->window
)) {
1062 return E_UNEXPECTED
;
1065 hres
= create_uri(src
, 0, &uri
);
1069 hres
= CreateURLMonikerEx2(NULL
, uri
, &mon
, URL_MK_UNIFORM
);
1075 bsc
= heap_alloc_zero(sizeof(*bsc
));
1077 IMoniker_Release(mon
);
1079 return E_OUTOFMEMORY
;
1082 init_bscallback(&bsc
->bsc
, &ScriptBSCVtbl
, mon
, async
? BINDF_ASYNCHRONOUS
| BINDF_ASYNCSTORAGE
| BINDF_PULLDATA
: 0);
1083 IMoniker_Release(mon
);
1085 hres
= IUri_GetScheme(uri
, &bsc
->scheme
);
1088 bsc
->scheme
= URL_SCHEME_UNKNOWN
;
1090 IHTMLScriptElement_AddRef(&script_elem
->IHTMLScriptElement_iface
);
1091 bsc
->script_elem
= script_elem
;
1093 hres
= start_binding(window
, &bsc
->bsc
, NULL
);
1095 IBindStatusCallback_Release(&bsc
->bsc
.IBindStatusCallback_iface
);
1099 static void parse_inline_script(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
)
1101 const PRUnichar
*text
;
1105 nsAString_Init(&text_str
, NULL
);
1106 nsres
= nsIDOMHTMLScriptElement_GetText(script_elem
->nsscript
, &text_str
);
1107 nsAString_GetData(&text_str
, &text
);
1109 set_script_elem_readystate(script_elem
, READYSTATE_INTERACTIVE
);
1111 if(NS_FAILED(nsres
)) {
1112 ERR("GetText failed: %08x\n", nsres
);
1114 parse_elem_text(script_host
, script_elem
, text
);
1117 nsAString_Finish(&text_str
);
1120 static BOOL
parse_script_elem(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
)
1122 nsAString src_str
, event_str
;
1123 BOOL is_complete
= FALSE
;
1124 const PRUnichar
*src
;
1127 nsAString_Init(&event_str
, NULL
);
1128 nsres
= nsIDOMHTMLScriptElement_GetEvent(script_elem
->nsscript
, &event_str
);
1129 if(NS_SUCCEEDED(nsres
)) {
1130 const PRUnichar
*event
;
1132 nsAString_GetData(&event_str
, &event
);
1134 TRACE("deferring event %s script evaluation\n", debugstr_w(event
));
1135 nsAString_Finish(&event_str
);
1139 ERR("GetEvent failed: %08x\n", nsres
);
1141 nsAString_Finish(&event_str
);
1143 nsAString_Init(&src_str
, NULL
);
1144 nsres
= nsIDOMHTMLScriptElement_GetSrc(script_elem
->nsscript
, &src_str
);
1145 nsAString_GetData(&src_str
, &src
);
1147 if(NS_FAILED(nsres
)) {
1148 ERR("GetSrc failed: %08x\n", nsres
);
1150 load_script(script_elem
, src
, FALSE
);
1151 is_complete
= script_elem
->parsed
;
1153 parse_inline_script(script_host
, script_elem
);
1157 nsAString_Finish(&src_str
);
1162 static GUID
get_default_script_guid(HTMLInnerWindow
*window
)
1164 /* If not specified, we should use very first script host that was created for the page (or JScript if none) */
1165 return list_empty(&window
->script_hosts
)
1167 : LIST_ENTRY(list_head(&window
->script_hosts
), ScriptHost
, entry
)->guid
;
1170 static BOOL
get_guid_from_type(LPCWSTR type
, GUID
*guid
)
1172 static const WCHAR text_javascriptW
[] =
1173 {'t','e','x','t','/','j','a','v','a','s','c','r','i','p','t',0};
1174 static const WCHAR text_jscriptW
[] =
1175 {'t','e','x','t','/','j','s','c','r','i','p','t',0};
1176 static const WCHAR text_vbscriptW
[] =
1177 {'t','e','x','t','/','v','b','s','c','r','i','p','t',0};
1179 /* FIXME: Handle more types */
1180 if(!wcsicmp(type
, text_javascriptW
) || !wcsicmp(type
, text_jscriptW
)) {
1181 *guid
= CLSID_JScript
;
1182 }else if(!wcsicmp(type
, text_vbscriptW
)) {
1183 *guid
= CLSID_VBScript
;
1185 FIXME("Unknown type %s\n", debugstr_w(type
));
1192 static BOOL
get_guid_from_language(LPCWSTR type
, GUID
*guid
)
1196 hres
= CLSIDFromProgID(type
, guid
);
1200 /* FIXME: Check CATID_ActiveScriptParse */
1205 static BOOL
get_script_guid(HTMLInnerWindow
*window
, nsIDOMHTMLScriptElement
*nsscript
, GUID
*guid
)
1207 nsIDOMElement
*nselem
;
1208 const PRUnichar
*language
;
1213 static const PRUnichar languageW
[] = {'l','a','n','g','u','a','g','e',0};
1215 nsAString_Init(&val_str
, NULL
);
1217 nsres
= nsIDOMHTMLScriptElement_GetType(nsscript
, &val_str
);
1218 if(NS_SUCCEEDED(nsres
)) {
1219 const PRUnichar
*type
;
1221 nsAString_GetData(&val_str
, &type
);
1223 ret
= get_guid_from_type(type
, guid
);
1224 nsAString_Finish(&val_str
);
1228 ERR("GetType failed: %08x\n", nsres
);
1231 nsres
= nsIDOMHTMLScriptElement_QueryInterface(nsscript
, &IID_nsIDOMElement
, (void**)&nselem
);
1232 assert(nsres
== NS_OK
);
1234 nsres
= get_elem_attr_value(nselem
, languageW
, &val_str
, &language
);
1235 nsIDOMElement_Release(nselem
);
1236 if(NS_SUCCEEDED(nsres
)) {
1238 ret
= get_guid_from_language(language
, guid
);
1240 *guid
= get_default_script_guid(window
);
1243 nsAString_Finish(&val_str
);
1249 static ScriptHost
*get_script_host(HTMLInnerWindow
*window
, const GUID
*guid
)
1253 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
1254 if(IsEqualGUID(guid
, &iter
->guid
))
1258 return create_script_host(window
, guid
);
1261 static ScriptHost
*get_elem_script_host(HTMLInnerWindow
*window
, HTMLScriptElement
*script_elem
)
1265 if(!get_script_guid(window
, script_elem
->nsscript
, &guid
)) {
1266 WARN("Could not find script GUID\n");
1270 if(IsEqualGUID(&CLSID_JScript
, &guid
) && (!window
->doc
->browser
|| window
->doc
->browser
->script_mode
!= SCRIPTMODE_ACTIVESCRIPT
)) {
1271 TRACE("Ignoring JScript\n");
1275 return get_script_host(window
, &guid
);
1278 void doc_insert_script(HTMLInnerWindow
*window
, HTMLScriptElement
*script_elem
, BOOL from_parser
)
1280 ScriptHost
*script_host
;
1281 BOOL is_complete
= FALSE
;
1283 script_host
= get_elem_script_host(window
, script_elem
);
1287 if(script_host
->parse
) {
1288 if(script_elem
->src_text
) {
1290 set_script_elem_readystate(script_elem
, READYSTATE_INTERACTIVE
);
1291 script_elem
->parsed
= TRUE
;
1292 parse_elem_text(script_host
, script_elem
, script_elem
->src_text
);
1294 }else if(!script_elem
->binding
) {
1295 is_complete
= parse_script_elem(script_host
, script_elem
);
1300 set_script_elem_readystate(script_elem
, READYSTATE_COMPLETE
);
1303 IDispatch
*script_parse_event(HTMLInnerWindow
*window
, LPCWSTR text
)
1305 ScriptHost
*script_host
;
1311 static const WCHAR delimiterW
[] = {'\"',0};
1313 TRACE("%s\n", debugstr_w(text
));
1315 for(ptr
= text
; iswalnum(*ptr
); ptr
++);
1320 language
= heap_alloc((ptr
-text
+1)*sizeof(WCHAR
));
1324 memcpy(language
, text
, (ptr
-text
)*sizeof(WCHAR
));
1325 language
[ptr
-text
] = 0;
1327 b
= get_guid_from_language(language
, &guid
);
1329 heap_free(language
);
1332 WARN("Could not find language\n");
1339 guid
= get_default_script_guid(window
);
1342 if(IsEqualGUID(&CLSID_JScript
, &guid
)
1343 && (!window
->doc
->browser
|| window
->doc
->browser
->script_mode
!= SCRIPTMODE_ACTIVESCRIPT
)) {
1344 TRACE("Ignoring JScript\n");
1348 script_host
= get_script_host(window
, &guid
);
1349 if(!script_host
|| !script_host
->parse_proc
)
1352 hres
= IActiveScriptParseProcedure2_ParseProcedureText(script_host
->parse_proc
, ptr
, NULL
, emptyW
,
1353 NULL
, NULL
, delimiterW
, 0 /* FIXME */, 0,
1354 SCRIPTPROC_HOSTMANAGESSOURCE
|SCRIPTPROC_IMPLICIT_THIS
|SCRIPTPROC_IMPLICIT_PARENTS
, &disp
);
1356 WARN("ParseProcedureText failed: %08x\n", hres
);
1360 TRACE("ret %p\n", disp
);
1364 HRESULT
exec_script(HTMLInnerWindow
*window
, const WCHAR
*code
, const WCHAR
*lang
, VARIANT
*ret
)
1366 ScriptHost
*script_host
;
1371 static const WCHAR delimW
[] = {'"',0};
1373 if(!get_guid_from_language(lang
, &guid
)) {
1374 WARN("Could not find script GUID\n");
1375 return CO_E_CLASSSTRING
;
1378 script_host
= get_script_host(window
, &guid
);
1380 FIXME("No script host\n");
1384 if(!script_host
->parse
) {
1385 FIXME("script_host->parse == NULL\n");
1389 memset(&ei
, 0, sizeof(ei
));
1391 hres
= IActiveScriptParse_ParseScriptText(script_host
->parse
, code
, NULL
, NULL
, delimW
, 0, 0, SCRIPTTEXT_ISVISIBLE
, ret
, &ei
);
1395 WARN("<<< %08x\n", hres
);
1400 IDispatch
*get_script_disp(ScriptHost
*script_host
)
1405 if(!script_host
->script
)
1408 hres
= IActiveScript_GetScriptDispatch(script_host
->script
, windowW
, &disp
);
1415 static EventTarget
*find_event_target(HTMLDocumentNode
*doc
, HTMLScriptElement
*script_elem
)
1417 EventTarget
*event_target
= NULL
;
1418 const PRUnichar
*target_id
;
1419 nsAString target_id_str
;
1423 nsAString_Init(&target_id_str
, NULL
);
1424 nsres
= nsIDOMHTMLScriptElement_GetHtmlFor(script_elem
->nsscript
, &target_id_str
);
1425 if(NS_FAILED(nsres
)) {
1426 ERR("GetScriptFor failed: %08x\n", nsres
);
1427 nsAString_Finish(&target_id_str
);
1431 nsAString_GetData(&target_id_str
, &target_id
);
1433 FIXME("Empty for attribute\n");
1434 }else if(!wcscmp(target_id
, documentW
)) {
1435 event_target
= &doc
->node
.event_target
;
1436 htmldoc_addref(&doc
->basedoc
);
1437 }else if(!wcscmp(target_id
, windowW
)) {
1439 event_target
= &doc
->window
->event_target
;
1440 IDispatchEx_AddRef(&event_target
->dispex
.IDispatchEx_iface
);
1443 HTMLElement
*target_elem
;
1445 hres
= get_doc_elem_by_id(doc
, target_id
, &target_elem
);
1446 if(SUCCEEDED(hres
) && target_elem
) {
1447 event_target
= &target_elem
->node
.event_target
;
1451 nsAString_Finish(&target_id_str
);
1452 return event_target
;
1455 static BOOL
parse_event_str(WCHAR
*event
, const WCHAR
**args
)
1459 TRACE("%s\n", debugstr_w(event
));
1461 for(ptr
= event
; iswalnum(*ptr
); ptr
++);
1472 while(iswalnum(*ptr
) || iswspace(*ptr
) || *ptr
== ',')
1482 static IDispatch
*parse_event_elem(HTMLDocumentNode
*doc
, HTMLScriptElement
*script_elem
, WCHAR
**ret_event
)
1484 ScriptHost
*script_host
;
1485 WCHAR
*event
= NULL
;
1492 if(script_elem
->parsed
)
1495 script_host
= get_elem_script_host(doc
->window
, script_elem
);
1496 if(!script_host
|| !script_host
->parse_proc
)
1499 nsAString_Init(&nsstr
, NULL
);
1500 nsres
= nsIDOMHTMLScriptElement_GetEvent(script_elem
->nsscript
, &nsstr
);
1501 if(NS_SUCCEEDED(nsres
)) {
1502 const PRUnichar
*event_val
;
1504 nsAString_GetData(&nsstr
, &event_val
);
1505 event
= heap_strdupW(event_val
);
1507 nsAString_Finish(&nsstr
);
1511 if(!parse_event_str(event
, &args
)) {
1512 WARN("parsing %s failed\n", debugstr_w(event
));
1517 nsAString_Init(&nsstr
, NULL
);
1518 nsres
= nsIDOMHTMLScriptElement_GetText(script_elem
->nsscript
, &nsstr
);
1519 if(NS_SUCCEEDED(nsres
)) {
1520 const PRUnichar
*text
;
1522 nsAString_GetData(&nsstr
, &text
);
1523 hres
= IActiveScriptParseProcedure2_ParseProcedureText(script_host
->parse_proc
, text
, args
,
1524 emptyW
, NULL
, NULL
, script_endW
, 0, 0,
1525 SCRIPTPROC_HOSTMANAGESSOURCE
|SCRIPTPROC_IMPLICIT_THIS
|SCRIPTPROC_IMPLICIT_PARENTS
, &disp
);
1529 ERR("GetText failed: %08x\n", nsres
);
1532 nsAString_Finish(&nsstr
);
1542 void bind_event_scripts(HTMLDocumentNode
*doc
)
1544 HTMLPluginContainer
*plugin_container
;
1545 nsIDOMHTMLScriptElement
*nsscript
;
1546 HTMLScriptElement
*script_elem
;
1547 EventTarget
*event_target
;
1548 nsIDOMNodeList
*node_list
;
1549 nsIDOMNode
*script_node
;
1550 nsAString selector_str
;
1551 IDispatch
*event_disp
;
1557 static const PRUnichar selectorW
[] = {'s','c','r','i','p','t','[','e','v','e','n','t',']',0};
1564 nsAString_InitDepend(&selector_str
, selectorW
);
1565 nsres
= nsIDOMHTMLDocument_QuerySelectorAll(doc
->nsdoc
, &selector_str
, &node_list
);
1566 nsAString_Finish(&selector_str
);
1567 if(NS_FAILED(nsres
)) {
1568 ERR("QuerySelectorAll failed: %08x\n", nsres
);
1575 nsres
= nsIDOMNodeList_GetLength(node_list
, &length
);
1576 assert(nsres
== NS_OK
);
1578 for(i
=0; i
< length
; i
++) {
1579 nsres
= nsIDOMNodeList_Item(node_list
, i
, &script_node
);
1580 if(NS_FAILED(nsres
) || !script_node
) {
1581 ERR("Item(%d) failed: %08x\n", i
, nsres
);
1585 nsres
= nsIDOMNode_QueryInterface(script_node
, &IID_nsIDOMHTMLScriptElement
, (void**)&nsscript
);
1586 assert(nsres
== NS_OK
);
1587 nsIDOMNode_Release(script_node
);
1589 hres
= script_elem_from_nsscript(nsscript
, &script_elem
);
1593 event_disp
= parse_event_elem(doc
, script_elem
, &event
);
1595 event_target
= find_event_target(doc
, script_elem
);
1597 hres
= IDispatchEx_QueryInterface(&event_target
->dispex
.IDispatchEx_iface
, &IID_HTMLPluginContainer
,
1598 (void**)&plugin_container
);
1600 bind_activex_event(doc
, plugin_container
, event
, event_disp
);
1602 bind_target_event(doc
, event_target
, event
, event_disp
);
1604 IDispatchEx_Release(&event_target
->dispex
.IDispatchEx_iface
);
1605 if(plugin_container
)
1606 node_release(&plugin_container
->element
.node
);
1610 IDispatch_Release(event_disp
);
1613 IHTMLScriptElement_Release(&script_elem
->IHTMLScriptElement_iface
);
1616 nsIDOMNodeList_Release(node_list
);
1619 BOOL
find_global_prop(HTMLInnerWindow
*window
, BSTR name
, DWORD flags
, ScriptHost
**ret_host
, DISPID
*ret_id
)
1621 IDispatchEx
*dispex
;
1626 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
1627 disp
= get_script_disp(iter
);
1631 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1632 if(SUCCEEDED(hres
)) {
1633 hres
= IDispatchEx_GetDispID(dispex
, name
, flags
& (~fdexNameEnsure
), ret_id
);
1634 IDispatchEx_Release(dispex
);
1636 FIXME("No IDispatchEx\n");
1640 IDispatch_Release(disp
);
1641 if(SUCCEEDED(hres
)) {
1650 static BOOL
is_jscript_available(void)
1652 static BOOL available
, checked
;
1656 HRESULT hres
= CoGetClassObject(&CLSID_JScript
, CLSCTX_INPROC_SERVER
, NULL
, &IID_IUnknown
, (void**)&unk
);
1658 if(SUCCEEDED(hres
)) {
1660 IUnknown_Release(unk
);
1670 static BOOL
use_gecko_script(IUri
*uri
)
1676 hres
= IUri_GetDisplayUri(uri
, &display_uri
);
1680 hres
= IInternetSecurityManager_MapUrlToZone(get_security_manager(), display_uri
, &zone
, 0);
1681 SysFreeString(display_uri
);
1683 WARN("Could not map %s to zone: %08x\n", debugstr_w(display_uri
), hres
);
1687 TRACE("zone %d\n", zone
);
1688 return zone
== URLZONE_UNTRUSTED
;
1691 void update_browser_script_mode(GeckoBrowser
*browser
, IUri
*uri
)
1693 nsIWebBrowserSetup
*setup
;
1696 if(!is_jscript_available()) {
1697 TRACE("jscript.dll not available\n");
1698 browser
->script_mode
= SCRIPTMODE_GECKO
;
1702 browser
->script_mode
= use_gecko_script(uri
) ? SCRIPTMODE_GECKO
: SCRIPTMODE_ACTIVESCRIPT
;
1704 nsres
= nsIWebBrowser_QueryInterface(browser
->webbrowser
, &IID_nsIWebBrowserSetup
, (void**)&setup
);
1705 if(NS_SUCCEEDED(nsres
)) {
1706 nsres
= nsIWebBrowserSetup_SetProperty(setup
, SETUP_ALLOW_JAVASCRIPT
,
1707 browser
->script_mode
== SCRIPTMODE_GECKO
);
1709 if(NS_SUCCEEDED(nsres
))
1710 nsres
= nsIWebBrowserSetup_SetProperty(setup
, SETUP_DISABLE_NOSCRIPT
, TRUE
);
1712 nsIWebBrowserSetup_Release(setup
);
1715 if(NS_FAILED(nsres
))
1716 ERR("JavaScript setup failed: %08x\n", nsres
);
1719 void release_script_hosts(HTMLInnerWindow
*window
)
1721 script_queue_entry_t
*queue_iter
;
1724 while(!list_empty(&window
->script_queue
)) {
1725 queue_iter
= LIST_ENTRY(list_head(&window
->script_queue
), script_queue_entry_t
, entry
);
1727 list_remove(&queue_iter
->entry
);
1728 IHTMLScriptElement_Release(&queue_iter
->script
->IHTMLScriptElement_iface
);
1729 heap_free(queue_iter
);
1732 while(!list_empty(&window
->script_hosts
)) {
1733 iter
= LIST_ENTRY(list_head(&window
->script_hosts
), ScriptHost
, entry
);
1735 release_script_engine(iter
);
1736 list_remove(&iter
->entry
);
1737 iter
->window
= NULL
;
1738 IActiveScriptSite_Release(&iter
->IActiveScriptSite_iface
);