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 ScriptHost
*get_elem_script_host(HTMLInnerWindow
*,HTMLScriptElement
*);
97 static void set_script_prop(ScriptHost
*script_host
, DWORD property
, VARIANT
*val
)
99 IActiveScriptProperty
*script_prop
;
102 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptProperty
,
103 (void**)&script_prop
);
105 WARN("Could not get IActiveScriptProperty iface: %08x\n", hres
);
109 hres
= IActiveScriptProperty_SetProperty(script_prop
, property
, NULL
, val
);
110 IActiveScriptProperty_Release(script_prop
);
112 WARN("SetProperty(%x) failed: %08x\n", property
, hres
);
115 static BOOL
is_quirks_mode(HTMLDocumentNode
*doc
)
117 const WCHAR
*compat_mode
;
122 static const WCHAR BackCompatW
[] = {'B','a','c','k','C','o','m','p','a','t',0};
124 nsAString_Init(&nsstr
, NULL
);
125 nsres
= nsIDOMHTMLDocument_GetCompatMode(doc
->nsdoc
, &nsstr
);
126 if(NS_SUCCEEDED(nsres
)) {
127 nsAString_GetData(&nsstr
, &compat_mode
);
128 if(!strcmpW(compat_mode
, BackCompatW
))
131 nsAString_Finish(&nsstr
);
135 static BOOL
init_script_engine(ScriptHost
*script_host
)
137 IObjectSafety
*safety
;
139 DWORD supported_opts
=0, enabled_opts
=0;
143 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptParse
, (void**)&script_host
->parse
);
145 WARN("Could not get IActiveScriptParse: %08x\n", hres
);
149 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IObjectSafety
, (void**)&safety
);
151 FIXME("Could not get IObjectSafety: %08x\n", hres
);
155 hres
= IObjectSafety_GetInterfaceSafetyOptions(safety
, &IID_IActiveScriptParse
, &supported_opts
, &enabled_opts
);
157 FIXME("GetInterfaceSafetyOptions failed: %08x\n", hres
);
158 }else if(!(supported_opts
& INTERFACE_USES_DISPEX
)) {
159 FIXME("INTERFACE_USES_DISPEX is not supported\n");
161 hres
= IObjectSafety_SetInterfaceSafetyOptions(safety
, &IID_IActiveScriptParse
,
162 INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
,
163 INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
);
165 FIXME("SetInterfaceSafetyOptions failed: %08x\n", hres
);
168 IObjectSafety_Release(safety
);
173 V_I4(&var
) = is_quirks_mode(script_host
->window
->doc
) ? 1 : 2;
174 set_script_prop(script_host
, SCRIPTPROP_INVOKEVERSIONING
, &var
);
176 V_VT(&var
) = VT_BOOL
;
177 V_BOOL(&var
) = VARIANT_TRUE
;
178 set_script_prop(script_host
, SCRIPTPROP_HACK_TRIDENTEVENTSINK
, &var
);
180 hres
= IActiveScriptParse_InitNew(script_host
->parse
);
182 WARN("InitNew failed: %08x\n", hres
);
186 hres
= IActiveScript_SetScriptSite(script_host
->script
, &script_host
->IActiveScriptSite_iface
);
188 WARN("SetScriptSite failed: %08x\n", hres
);
189 IActiveScript_Close(script_host
->script
);
193 hres
= IActiveScript_GetScriptState(script_host
->script
, &state
);
195 WARN("GetScriptState failed: %08x\n", hres
);
196 else if(state
!= SCRIPTSTATE_INITIALIZED
)
197 FIXME("state = %x\n", state
);
199 hres
= IActiveScript_SetScriptState(script_host
->script
, SCRIPTSTATE_STARTED
);
201 WARN("Starting script failed: %08x\n", hres
);
205 hres
= IActiveScript_AddNamedItem(script_host
->script
, windowW
,
206 SCRIPTITEM_ISVISIBLE
|SCRIPTITEM_ISSOURCE
|SCRIPTITEM_GLOBALMEMBERS
);
207 if(SUCCEEDED(hres
)) {
208 V_VT(&var
) = VT_BOOL
;
209 V_BOOL(&var
) = VARIANT_TRUE
;
210 set_script_prop(script_host
, SCRIPTPROP_ABBREVIATE_GLOBALNAME_RESOLUTION
, &var
);
212 WARN("AddNamedItem failed: %08x\n", hres
);
215 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptParseProcedure2
,
216 (void**)&script_host
->parse_proc
);
218 /* FIXME: QI for IActiveScriptParseProcedure */
219 WARN("Could not get IActiveScriptParseProcedure iface: %08x\n", hres
);
225 static void release_script_engine(ScriptHost
*This
)
230 switch(This
->script_state
) {
231 case SCRIPTSTATE_CONNECTED
:
232 IActiveScript_SetScriptState(This
->script
, SCRIPTSTATE_DISCONNECTED
);
234 case SCRIPTSTATE_STARTED
:
235 case SCRIPTSTATE_DISCONNECTED
:
236 case SCRIPTSTATE_INITIALIZED
:
237 IActiveScript_Close(This
->script
);
240 if(This
->parse_proc
) {
241 IActiveScriptParseProcedure2_Release(This
->parse_proc
);
242 This
->parse_proc
= NULL
;
246 IActiveScriptParse_Release(This
->parse
);
251 IActiveScript_Release(This
->script
);
253 This
->script_state
= SCRIPTSTATE_UNINITIALIZED
;
256 void connect_scripts(HTMLInnerWindow
*window
)
260 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
261 if(iter
->script_state
== SCRIPTSTATE_STARTED
)
262 IActiveScript_SetScriptState(iter
->script
, SCRIPTSTATE_CONNECTED
);
266 static inline ScriptHost
*impl_from_IActiveScriptSite(IActiveScriptSite
*iface
)
268 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSite_iface
);
271 static HRESULT WINAPI
ActiveScriptSite_QueryInterface(IActiveScriptSite
*iface
, REFIID riid
, void **ppv
)
273 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
277 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
278 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
279 *ppv
= &This
->IActiveScriptSite_iface
;
280 }else if(IsEqualGUID(&IID_IActiveScriptSite
, riid
)) {
281 TRACE("(%p)->(IID_IActiveScriptSite %p)\n", This
, ppv
);
282 *ppv
= &This
->IActiveScriptSite_iface
;
283 }else if(IsEqualGUID(&IID_IActiveScriptSiteInterruptPoll
, riid
)) {
284 TRACE("(%p)->(IID_IActiveScriptSiteInterruprtPoll %p)\n", This
, ppv
);
285 *ppv
= &This
->IActiveScriptSiteInterruptPoll_iface
;
286 }else if(IsEqualGUID(&IID_IActiveScriptSiteWindow
, riid
)) {
287 TRACE("(%p)->(IID_IActiveScriptSiteWindow %p)\n", This
, ppv
);
288 *ppv
= &This
->IActiveScriptSiteWindow_iface
;
289 }else if(IsEqualGUID(&IID_IActiveScriptSiteUIControl
, riid
)) {
290 TRACE("(%p)->(IID_IActiveScriptSiteUIControl %p)\n", This
, ppv
);
291 *ppv
= &This
->IActiveScriptSiteUIControl_iface
;
292 }else if(IsEqualGUID(&IID_IActiveScriptSiteDebug
, riid
)) {
293 TRACE("(%p)->(IID_IActiveScriptSiteDebug %p)\n", This
, ppv
);
294 *ppv
= &This
->IActiveScriptSiteDebug_iface
;
295 }else if(IsEqualGUID(&IID_IServiceProvider
, riid
)) {
296 TRACE("(%p)->(IID_IServiceProvider %p)\n", This
, ppv
);
297 *ppv
= &This
->IServiceProvider_iface
;
298 }else if(IsEqualGUID(&IID_ICanHandleException
, riid
)) {
299 TRACE("(%p)->(IID_ICanHandleException not supported %p)\n", This
, ppv
);
300 return E_NOINTERFACE
;
302 FIXME("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
303 return E_NOINTERFACE
;
306 IUnknown_AddRef((IUnknown
*)*ppv
);
310 static ULONG WINAPI
ActiveScriptSite_AddRef(IActiveScriptSite
*iface
)
312 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
313 LONG ref
= InterlockedIncrement(&This
->ref
);
315 TRACE("(%p) ref=%d\n", This
, ref
);
320 static ULONG WINAPI
ActiveScriptSite_Release(IActiveScriptSite
*iface
)
322 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
323 LONG ref
= InterlockedDecrement(&This
->ref
);
325 TRACE("(%p) ref=%d\n", This
, ref
);
328 release_script_engine(This
);
330 list_remove(&This
->entry
);
337 static HRESULT WINAPI
ActiveScriptSite_GetLCID(IActiveScriptSite
*iface
, LCID
*plcid
)
339 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
341 TRACE("(%p)->(%p)\n", This
, plcid
);
343 *plcid
= GetUserDefaultLCID();
347 static HRESULT WINAPI
ActiveScriptSite_GetItemInfo(IActiveScriptSite
*iface
, LPCOLESTR pstrName
,
348 DWORD dwReturnMask
, IUnknown
**ppiunkItem
, ITypeInfo
**ppti
)
350 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
352 TRACE("(%p)->(%s %x %p %p)\n", This
, debugstr_w(pstrName
), dwReturnMask
, ppiunkItem
, ppti
);
354 if(dwReturnMask
!= SCRIPTINFO_IUNKNOWN
) {
355 FIXME("Unsupported mask %x\n", dwReturnMask
);
361 if(strcmpW(pstrName
, windowW
))
362 return DISP_E_MEMBERNOTFOUND
;
367 /* FIXME: Return proxy object */
368 *ppiunkItem
= (IUnknown
*)&This
->window
->base
.IHTMLWindow2_iface
;
369 IUnknown_AddRef(*ppiunkItem
);
374 static HRESULT WINAPI
ActiveScriptSite_GetDocVersionString(IActiveScriptSite
*iface
, BSTR
*pbstrVersion
)
376 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
377 FIXME("(%p)->(%p)\n", This
, pbstrVersion
);
381 static HRESULT WINAPI
ActiveScriptSite_OnScriptTerminate(IActiveScriptSite
*iface
,
382 const VARIANT
*pvarResult
, const EXCEPINFO
*pexcepinfo
)
384 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
385 FIXME("(%p)->(%p %p)\n", This
, pvarResult
, pexcepinfo
);
389 static HRESULT WINAPI
ActiveScriptSite_OnStateChange(IActiveScriptSite
*iface
, SCRIPTSTATE ssScriptState
)
391 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
393 TRACE("(%p)->(%x)\n", This
, ssScriptState
);
395 This
->script_state
= ssScriptState
;
399 static HRESULT WINAPI
ActiveScriptSite_OnScriptError(IActiveScriptSite
*iface
, IActiveScriptError
*pscripterror
)
401 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
402 FIXME("(%p)->(%p)\n", This
, pscripterror
);
406 static HRESULT WINAPI
ActiveScriptSite_OnEnterScript(IActiveScriptSite
*iface
)
408 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
410 TRACE("(%p)->()\n", This
);
415 static HRESULT WINAPI
ActiveScriptSite_OnLeaveScript(IActiveScriptSite
*iface
)
417 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
419 TRACE("(%p)->()\n", This
);
424 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl
= {
425 ActiveScriptSite_QueryInterface
,
426 ActiveScriptSite_AddRef
,
427 ActiveScriptSite_Release
,
428 ActiveScriptSite_GetLCID
,
429 ActiveScriptSite_GetItemInfo
,
430 ActiveScriptSite_GetDocVersionString
,
431 ActiveScriptSite_OnScriptTerminate
,
432 ActiveScriptSite_OnStateChange
,
433 ActiveScriptSite_OnScriptError
,
434 ActiveScriptSite_OnEnterScript
,
435 ActiveScriptSite_OnLeaveScript
438 static inline ScriptHost
*impl_from_IActiveScriptSiteInterruptPoll(IActiveScriptSiteInterruptPoll
*iface
)
440 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteInterruptPoll_iface
);
443 static HRESULT WINAPI
ActiveScriptSiteInterruptPoll_QueryInterface(IActiveScriptSiteInterruptPoll
*iface
,
444 REFIID riid
, void **ppv
)
446 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
447 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
450 static ULONG WINAPI
ActiveScriptSiteInterruptPoll_AddRef(IActiveScriptSiteInterruptPoll
*iface
)
452 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
453 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
456 static ULONG WINAPI
ActiveScriptSiteInterruptPoll_Release(IActiveScriptSiteInterruptPoll
*iface
)
458 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
459 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
462 static HRESULT WINAPI
ActiveScriptSiteInterruptPoll_QueryContinue(IActiveScriptSiteInterruptPoll
*iface
)
464 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
466 TRACE("(%p)\n", This
);
471 static const IActiveScriptSiteInterruptPollVtbl ActiveScriptSiteInterruptPollVtbl
= {
472 ActiveScriptSiteInterruptPoll_QueryInterface
,
473 ActiveScriptSiteInterruptPoll_AddRef
,
474 ActiveScriptSiteInterruptPoll_Release
,
475 ActiveScriptSiteInterruptPoll_QueryContinue
478 static inline ScriptHost
*impl_from_IActiveScriptSiteWindow(IActiveScriptSiteWindow
*iface
)
480 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteWindow_iface
);
483 static HRESULT WINAPI
ActiveScriptSiteWindow_QueryInterface(IActiveScriptSiteWindow
*iface
,
484 REFIID riid
, void **ppv
)
486 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
487 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
490 static ULONG WINAPI
ActiveScriptSiteWindow_AddRef(IActiveScriptSiteWindow
*iface
)
492 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
493 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
496 static ULONG WINAPI
ActiveScriptSiteWindow_Release(IActiveScriptSiteWindow
*iface
)
498 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
499 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
502 static HRESULT WINAPI
ActiveScriptSiteWindow_GetWindow(IActiveScriptSiteWindow
*iface
, HWND
*phwnd
)
504 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
506 TRACE("(%p)->(%p)\n", This
, phwnd
);
508 if(!This
->window
|| !This
->window
->base
.outer_window
|| !This
->window
->base
.outer_window
->doc_obj
)
511 *phwnd
= This
->window
->base
.outer_window
->doc_obj
->hwnd
;
515 static HRESULT WINAPI
ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow
*iface
, BOOL fEnable
)
517 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
518 FIXME("(%p)->(%x)\n", This
, fEnable
);
522 static const IActiveScriptSiteWindowVtbl ActiveScriptSiteWindowVtbl
= {
523 ActiveScriptSiteWindow_QueryInterface
,
524 ActiveScriptSiteWindow_AddRef
,
525 ActiveScriptSiteWindow_Release
,
526 ActiveScriptSiteWindow_GetWindow
,
527 ActiveScriptSiteWindow_EnableModeless
530 static inline ScriptHost
*impl_from_IActiveScriptSiteUIControl(IActiveScriptSiteUIControl
*iface
)
532 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteUIControl_iface
);
535 static HRESULT WINAPI
ActiveScriptSiteUIControl_QueryInterface(IActiveScriptSiteUIControl
*iface
, REFIID riid
, void **ppv
)
537 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
538 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
541 static ULONG WINAPI
ActiveScriptSiteUIControl_AddRef(IActiveScriptSiteUIControl
*iface
)
543 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
544 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
547 static ULONG WINAPI
ActiveScriptSiteUIControl_Release(IActiveScriptSiteUIControl
*iface
)
549 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
550 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
553 static HRESULT WINAPI
ActiveScriptSiteUIControl_GetUIBehavior(IActiveScriptSiteUIControl
*iface
, SCRIPTUICITEM UicItem
,
554 SCRIPTUICHANDLING
*pUicHandling
)
556 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
558 WARN("(%p)->(%d %p) semi-stub\n", This
, UicItem
, pUicHandling
);
560 *pUicHandling
= SCRIPTUICHANDLING_ALLOW
;
564 static const IActiveScriptSiteUIControlVtbl ActiveScriptSiteUIControlVtbl
= {
565 ActiveScriptSiteUIControl_QueryInterface
,
566 ActiveScriptSiteUIControl_AddRef
,
567 ActiveScriptSiteUIControl_Release
,
568 ActiveScriptSiteUIControl_GetUIBehavior
571 static inline ScriptHost
*impl_from_IActiveScriptSiteDebug(IActiveScriptSiteDebug
*iface
)
573 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteDebug_iface
);
576 static HRESULT WINAPI
ActiveScriptSiteDebug_QueryInterface(IActiveScriptSiteDebug
*iface
,
577 REFIID riid
, void **ppv
)
579 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
580 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
583 static ULONG WINAPI
ActiveScriptSiteDebug_AddRef(IActiveScriptSiteDebug
*iface
)
585 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
586 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
589 static ULONG WINAPI
ActiveScriptSiteDebug_Release(IActiveScriptSiteDebug
*iface
)
591 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
592 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
595 static HRESULT WINAPI
ActiveScriptSiteDebug_GetDocumentContextFromPosition(IActiveScriptSiteDebug
*iface
,
596 CTXARG_T dwSourceContext
, ULONG uCharacterOffset
, ULONG uNumChars
, IDebugDocumentContext
**ppsc
)
598 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
599 FIXME("(%p)->(%s %u %u %p)\n", This
, wine_dbgstr_longlong(dwSourceContext
), uCharacterOffset
,
604 static HRESULT WINAPI
ActiveScriptSiteDebug_GetApplication(IActiveScriptSiteDebug
*iface
, IDebugApplication
**ppda
)
606 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
607 FIXME("(%p)->(%p)\n", This
, ppda
);
611 static HRESULT WINAPI
ActiveScriptSiteDebug_GetRootApplicationNode(IActiveScriptSiteDebug
*iface
,
612 IDebugApplicationNode
**ppdanRoot
)
614 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
615 FIXME("(%p)->(%p)\n", This
, ppdanRoot
);
619 static HRESULT WINAPI
ActiveScriptSiteDebug_OnScriptErrorDebug(IActiveScriptSiteDebug
*iface
,
620 IActiveScriptErrorDebug
*pErrorDebug
, BOOL
*pfEnterDebugger
, BOOL
*pfCallOnScriptErrorWhenContinuing
)
622 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
623 FIXME("(%p)->(%p %p %p)\n", This
, pErrorDebug
, pfEnterDebugger
, pfCallOnScriptErrorWhenContinuing
);
627 static const IActiveScriptSiteDebugVtbl ActiveScriptSiteDebugVtbl
= {
628 ActiveScriptSiteDebug_QueryInterface
,
629 ActiveScriptSiteDebug_AddRef
,
630 ActiveScriptSiteDebug_Release
,
631 ActiveScriptSiteDebug_GetDocumentContextFromPosition
,
632 ActiveScriptSiteDebug_GetApplication
,
633 ActiveScriptSiteDebug_GetRootApplicationNode
,
634 ActiveScriptSiteDebug_OnScriptErrorDebug
637 static inline ScriptHost
*impl_from_IServiceProvider(IServiceProvider
*iface
)
639 return CONTAINING_RECORD(iface
, ScriptHost
, IServiceProvider_iface
);
642 static HRESULT WINAPI
ASServiceProvider_QueryInterface(IServiceProvider
*iface
, REFIID riid
, void **ppv
)
644 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
645 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
648 static ULONG WINAPI
ASServiceProvider_AddRef(IServiceProvider
*iface
)
650 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
651 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
654 static ULONG WINAPI
ASServiceProvider_Release(IServiceProvider
*iface
)
656 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
657 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
660 static HRESULT WINAPI
ASServiceProvider_QueryService(IServiceProvider
*iface
, REFGUID guidService
,
661 REFIID riid
, void **ppv
)
663 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
665 if(IsEqualGUID(&SID_SInternetHostSecurityManager
, guidService
)) {
666 TRACE("(%p)->(SID_SInternetHostSecurityManager)\n", This
);
668 if(!This
->window
|| !This
->window
->doc
)
669 return E_NOINTERFACE
;
671 return IInternetHostSecurityManager_QueryInterface(&This
->window
->doc
->IInternetHostSecurityManager_iface
,
675 if(IsEqualGUID(&SID_SContainerDispatch
, guidService
)) {
676 TRACE("(%p)->(SID_SContainerDispatch)\n", This
);
678 if(!This
->window
|| !This
->window
->doc
)
679 return E_NOINTERFACE
;
681 return IHTMLDocument2_QueryInterface(&This
->window
->doc
->basedoc
.IHTMLDocument2_iface
, riid
, ppv
);
684 FIXME("(%p)->(%s %s %p)\n", This
, debugstr_guid(guidService
), debugstr_guid(riid
), ppv
);
685 return E_NOINTERFACE
;
688 static const IServiceProviderVtbl ASServiceProviderVtbl
= {
689 ASServiceProvider_QueryInterface
,
690 ASServiceProvider_AddRef
,
691 ASServiceProvider_Release
,
692 ASServiceProvider_QueryService
695 static ScriptHost
*create_script_host(HTMLInnerWindow
*window
, const GUID
*guid
)
700 ret
= heap_alloc_zero(sizeof(*ret
));
704 ret
->IActiveScriptSite_iface
.lpVtbl
= &ActiveScriptSiteVtbl
;
705 ret
->IActiveScriptSiteInterruptPoll_iface
.lpVtbl
= &ActiveScriptSiteInterruptPollVtbl
;
706 ret
->IActiveScriptSiteWindow_iface
.lpVtbl
= &ActiveScriptSiteWindowVtbl
;
707 ret
->IActiveScriptSiteUIControl_iface
.lpVtbl
= &ActiveScriptSiteUIControlVtbl
;
708 ret
->IActiveScriptSiteDebug_iface
.lpVtbl
= &ActiveScriptSiteDebugVtbl
;
709 ret
->IServiceProvider_iface
.lpVtbl
= &ASServiceProviderVtbl
;
711 ret
->window
= window
;
712 ret
->script_state
= SCRIPTSTATE_UNINITIALIZED
;
715 list_add_tail(&window
->script_hosts
, &ret
->entry
);
717 hres
= CoCreateInstance(&ret
->guid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
718 &IID_IActiveScript
, (void**)&ret
->script
);
720 WARN("Could not load script engine: %08x\n", hres
);
721 else if(!init_script_engine(ret
))
722 release_script_engine(ret
);
727 static void dispatch_script_readystatechange_event(HTMLScriptElement
*script
)
732 hres
= create_document_event(script
->element
.node
.doc
, EVENTID_READYSTATECHANGE
, &event
);
736 dispatch_event(&script
->element
.node
.event_target
, event
);
737 IDOMEvent_Release(&event
->IDOMEvent_iface
);
742 HTMLScriptElement
*elem
;
743 } fire_readystatechange_task_t
;
745 static void fire_readystatechange_proc(task_t
*_task
)
747 fire_readystatechange_task_t
*task
= (fire_readystatechange_task_t
*)_task
;
749 if(!task
->elem
->pending_readystatechange_event
)
752 task
->elem
->pending_readystatechange_event
= FALSE
;
753 dispatch_script_readystatechange_event(task
->elem
);
756 static void fire_readystatechange_task_destr(task_t
*_task
)
758 fire_readystatechange_task_t
*task
= (fire_readystatechange_task_t
*)_task
;
760 IHTMLScriptElement_Release(&task
->elem
->IHTMLScriptElement_iface
);
763 static void set_script_elem_readystate(HTMLScriptElement
*script_elem
, READYSTATE readystate
)
765 script_elem
->readystate
= readystate
;
767 if(readystate
!= READYSTATE_INTERACTIVE
) {
768 if(!script_elem
->element
.node
.doc
->window
->parser_callback_cnt
) {
769 fire_readystatechange_task_t
*task
;
772 if(script_elem
->pending_readystatechange_event
)
775 task
= heap_alloc(sizeof(*task
));
779 IHTMLScriptElement_AddRef(&script_elem
->IHTMLScriptElement_iface
);
780 task
->elem
= script_elem
;
782 hres
= push_task(&task
->header
, fire_readystatechange_proc
, fire_readystatechange_task_destr
,
783 script_elem
->element
.node
.doc
->window
->task_magic
);
785 script_elem
->pending_readystatechange_event
= TRUE
;
787 script_elem
->pending_readystatechange_event
= FALSE
;
788 dispatch_script_readystatechange_event(script_elem
);
793 static void parse_elem_text(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
, LPCWSTR text
)
799 TRACE("%s\n", debugstr_w(text
));
802 memset(&excepinfo
, 0, sizeof(excepinfo
));
804 hres
= IActiveScriptParse_ParseScriptText(script_host
->parse
, text
, windowW
, NULL
, script_endW
,
805 0, 0, SCRIPTTEXT_ISVISIBLE
|SCRIPTTEXT_HOSTMANAGESSOURCE
,
810 WARN("<<< %08x\n", hres
);
816 HTMLScriptElement
*script_elem
;
823 static HRESULT
get_binding_text(ScriptBSC
*bsc
, WCHAR
**ret
)
828 if(!bsc
->bsc
.readed
) {
829 text
= heap_alloc(sizeof(WCHAR
));
831 return E_OUTOFMEMORY
;
837 switch(bsc
->bsc
.bom
) {
839 if(bsc
->bsc
.readed
% sizeof(WCHAR
)) {
840 FIXME("The buffer is not a valid utf16 string\n");
844 text
= heap_alloc(bsc
->bsc
.readed
+sizeof(WCHAR
));
846 return E_OUTOFMEMORY
;
848 memcpy(text
, bsc
->buf
, bsc
->bsc
.readed
);
849 text
[bsc
->bsc
.readed
/sizeof(WCHAR
)] = 0;
853 /* FIXME: Try to use charset from HTTP headers first */
854 cp
= get_document_charset(bsc
->script_elem
->element
.node
.doc
);
859 len
= MultiByteToWideChar(cp
, 0, bsc
->buf
, bsc
->bsc
.readed
, NULL
, 0);
860 text
= heap_alloc((len
+1)*sizeof(WCHAR
));
862 return E_OUTOFMEMORY
;
864 MultiByteToWideChar(cp
, 0, bsc
->buf
, bsc
->bsc
.readed
, text
, len
);
873 static void script_file_available(ScriptBSC
*bsc
)
875 HTMLScriptElement
*script_elem
= bsc
->script_elem
;
876 HTMLInnerWindow
*window
= bsc
->bsc
.window
;
877 ScriptHost
*script_host
;
882 assert(window
!= NULL
);
884 hres
= get_binding_text(bsc
, &script_elem
->src_text
);
888 script_host
= get_elem_script_host(window
, script_elem
);
892 if(window
->parser_callback_cnt
) {
893 script_queue_entry_t
*queue
;
895 TRACE("Adding to queue\n");
897 queue
= heap_alloc(sizeof(*queue
));
901 IHTMLScriptElement_AddRef(&script_elem
->IHTMLScriptElement_iface
);
902 queue
->script
= script_elem
;
904 list_add_tail(&window
->script_queue
, &queue
->entry
);
908 nsres
= nsIDOMHTMLElement_GetParentNode(script_elem
->element
.nselem
, &parent
);
909 if(NS_FAILED(nsres
) || !parent
) {
910 TRACE("No parent, not executing\n");
911 script_elem
->parse_on_bind
= TRUE
;
915 nsIDOMNode_Release(parent
);
917 script_host
= get_elem_script_host(window
, script_elem
);
921 script_elem
->parsed
= TRUE
;
922 if(script_host
->parse
)
923 parse_elem_text(script_host
, script_elem
, script_elem
->src_text
);
926 static inline ScriptBSC
*impl_from_BSCallback(BSCallback
*iface
)
928 return CONTAINING_RECORD(iface
, ScriptBSC
, bsc
);
931 static void ScriptBSC_destroy(BSCallback
*bsc
)
933 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
935 if(This
->script_elem
) {
936 IHTMLScriptElement_Release(&This
->script_elem
->IHTMLScriptElement_iface
);
937 This
->script_elem
= NULL
;
940 heap_free(This
->buf
);
944 static HRESULT
ScriptBSC_init_bindinfo(BSCallback
*bsc
)
949 static HRESULT
ScriptBSC_start_binding(BSCallback
*bsc
)
951 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
953 This
->script_elem
->binding
= &This
->bsc
;
955 /* FIXME: We should find a better to decide if 'loading' state is supposed to be used by the protocol. */
956 if(This
->scheme
== URL_SCHEME_HTTPS
|| This
->scheme
== URL_SCHEME_HTTP
)
957 set_script_elem_readystate(This
->script_elem
, READYSTATE_LOADING
);
962 static HRESULT
ScriptBSC_stop_binding(BSCallback
*bsc
, HRESULT result
)
964 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
966 if(SUCCEEDED(result
) && !This
->script_elem
)
967 result
= E_UNEXPECTED
;
969 assert(FAILED(result
) || This
->script_elem
->binding
== &This
->bsc
);
970 This
->script_elem
->binding
= NULL
;
972 if(This
->script_elem
->readystate
== READYSTATE_LOADING
)
973 set_script_elem_readystate(This
->script_elem
, READYSTATE_LOADED
);
975 if(SUCCEEDED(result
)) {
976 script_file_available(This
);
978 FIXME("binding failed %08x\n", result
);
979 heap_free(This
->buf
);
984 IHTMLScriptElement_Release(&This
->script_elem
->IHTMLScriptElement_iface
);
985 This
->script_elem
= NULL
;
989 static HRESULT
ScriptBSC_read_data(BSCallback
*bsc
, IStream
*stream
)
991 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
996 This
->buf
= heap_alloc(128);
998 return E_OUTOFMEMORY
;
1003 if(This
->bsc
.readed
>= This
->size
) {
1005 new_buf
= heap_realloc(This
->buf
, This
->size
<< 1);
1007 return E_OUTOFMEMORY
;
1009 This
->buf
= new_buf
;
1012 hres
= read_stream(&This
->bsc
, stream
, This
->buf
+This
->bsc
.readed
, This
->size
-This
->bsc
.readed
, &readed
);
1013 }while(hres
== S_OK
);
1018 static HRESULT
ScriptBSC_on_progress(BSCallback
*bsc
, ULONG status_code
, LPCWSTR status_text
)
1023 static HRESULT
ScriptBSC_on_response(BSCallback
*bsc
, DWORD response_code
,
1024 LPCWSTR response_headers
)
1029 static HRESULT
ScriptBSC_beginning_transaction(BSCallback
*bsc
, WCHAR
**additional_headers
)
1034 static const BSCallbackVtbl ScriptBSCVtbl
= {
1036 ScriptBSC_init_bindinfo
,
1037 ScriptBSC_start_binding
,
1038 ScriptBSC_stop_binding
,
1039 ScriptBSC_read_data
,
1040 ScriptBSC_on_progress
,
1041 ScriptBSC_on_response
,
1042 ScriptBSC_beginning_transaction
1046 HRESULT
load_script(HTMLScriptElement
*script_elem
, const WCHAR
*src
, BOOL async
)
1048 HTMLInnerWindow
*window
;
1054 static const WCHAR wine_schemaW
[] = {'w','i','n','e',':'};
1056 if(strlenW(src
) > sizeof(wine_schemaW
)/sizeof(WCHAR
) && !memcmp(src
, wine_schemaW
, sizeof(wine_schemaW
)))
1057 src
+= sizeof(wine_schemaW
)/sizeof(WCHAR
);
1059 TRACE("(%p %s %x)\n", script_elem
, debugstr_w(src
), async
);
1061 if(!script_elem
->element
.node
.doc
|| !(window
= script_elem
->element
.node
.doc
->window
)) {
1063 return E_UNEXPECTED
;
1066 hres
= create_uri(src
, 0, &uri
);
1070 hres
= CreateURLMonikerEx2(NULL
, uri
, &mon
, URL_MK_UNIFORM
);
1076 bsc
= heap_alloc_zero(sizeof(*bsc
));
1078 IMoniker_Release(mon
);
1080 return E_OUTOFMEMORY
;
1083 init_bscallback(&bsc
->bsc
, &ScriptBSCVtbl
, mon
, async
? BINDF_ASYNCHRONOUS
| BINDF_ASYNCSTORAGE
| BINDF_PULLDATA
: 0);
1084 IMoniker_Release(mon
);
1086 hres
= IUri_GetScheme(uri
, &bsc
->scheme
);
1089 bsc
->scheme
= URL_SCHEME_UNKNOWN
;
1091 IHTMLScriptElement_AddRef(&script_elem
->IHTMLScriptElement_iface
);
1092 bsc
->script_elem
= script_elem
;
1094 hres
= start_binding(window
, &bsc
->bsc
, NULL
);
1096 IBindStatusCallback_Release(&bsc
->bsc
.IBindStatusCallback_iface
);
1100 static void parse_inline_script(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
)
1102 const PRUnichar
*text
;
1106 nsAString_Init(&text_str
, NULL
);
1107 nsres
= nsIDOMHTMLScriptElement_GetText(script_elem
->nsscript
, &text_str
);
1108 nsAString_GetData(&text_str
, &text
);
1110 set_script_elem_readystate(script_elem
, READYSTATE_INTERACTIVE
);
1112 if(NS_FAILED(nsres
)) {
1113 ERR("GetText failed: %08x\n", nsres
);
1115 parse_elem_text(script_host
, script_elem
, text
);
1118 nsAString_Finish(&text_str
);
1121 static BOOL
parse_script_elem(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
)
1123 nsAString src_str
, event_str
;
1124 BOOL is_complete
= FALSE
;
1125 const PRUnichar
*src
;
1128 nsAString_Init(&event_str
, NULL
);
1129 nsres
= nsIDOMHTMLScriptElement_GetEvent(script_elem
->nsscript
, &event_str
);
1130 if(NS_SUCCEEDED(nsres
)) {
1131 const PRUnichar
*event
;
1133 nsAString_GetData(&event_str
, &event
);
1135 TRACE("deferring event %s script evaluation\n", debugstr_w(event
));
1136 nsAString_Finish(&event_str
);
1140 ERR("GetEvent failed: %08x\n", nsres
);
1142 nsAString_Finish(&event_str
);
1144 nsAString_Init(&src_str
, NULL
);
1145 nsres
= nsIDOMHTMLScriptElement_GetSrc(script_elem
->nsscript
, &src_str
);
1146 nsAString_GetData(&src_str
, &src
);
1148 if(NS_FAILED(nsres
)) {
1149 ERR("GetSrc failed: %08x\n", nsres
);
1151 load_script(script_elem
, src
, FALSE
);
1152 is_complete
= script_elem
->parsed
;
1154 parse_inline_script(script_host
, script_elem
);
1158 nsAString_Finish(&src_str
);
1163 static GUID
get_default_script_guid(HTMLInnerWindow
*window
)
1165 /* If not specified, we should use very first script host that was created for the page (or JScript if none) */
1166 return list_empty(&window
->script_hosts
)
1168 : LIST_ENTRY(list_head(&window
->script_hosts
), ScriptHost
, entry
)->guid
;
1171 static BOOL
get_guid_from_type(LPCWSTR type
, GUID
*guid
)
1173 const WCHAR text_javascriptW
[] =
1174 {'t','e','x','t','/','j','a','v','a','s','c','r','i','p','t',0};
1175 const WCHAR text_jscriptW
[] =
1176 {'t','e','x','t','/','j','s','c','r','i','p','t',0};
1177 const WCHAR text_vbscriptW
[] =
1178 {'t','e','x','t','/','v','b','s','c','r','i','p','t',0};
1180 /* FIXME: Handle more types */
1181 if(!strcmpiW(type
, text_javascriptW
) || !strcmpiW(type
, text_jscriptW
)) {
1182 *guid
= CLSID_JScript
;
1183 }else if(!strcmpiW(type
, text_vbscriptW
)) {
1184 *guid
= CLSID_VBScript
;
1186 FIXME("Unknown type %s\n", debugstr_w(type
));
1193 static BOOL
get_guid_from_language(LPCWSTR type
, GUID
*guid
)
1197 hres
= CLSIDFromProgID(type
, guid
);
1201 /* FIXME: Check CATID_ActiveScriptParse */
1206 static BOOL
get_script_guid(HTMLInnerWindow
*window
, nsIDOMHTMLScriptElement
*nsscript
, GUID
*guid
)
1208 nsIDOMElement
*nselem
;
1209 const PRUnichar
*language
;
1214 static const PRUnichar languageW
[] = {'l','a','n','g','u','a','g','e',0};
1216 nsAString_Init(&val_str
, NULL
);
1218 nsres
= nsIDOMHTMLScriptElement_GetType(nsscript
, &val_str
);
1219 if(NS_SUCCEEDED(nsres
)) {
1220 const PRUnichar
*type
;
1222 nsAString_GetData(&val_str
, &type
);
1224 ret
= get_guid_from_type(type
, guid
);
1225 nsAString_Finish(&val_str
);
1229 ERR("GetType failed: %08x\n", nsres
);
1232 nsres
= nsIDOMHTMLScriptElement_QueryInterface(nsscript
, &IID_nsIDOMElement
, (void**)&nselem
);
1233 assert(nsres
== NS_OK
);
1235 nsres
= get_elem_attr_value(nselem
, languageW
, &val_str
, &language
);
1236 nsIDOMElement_Release(nselem
);
1237 if(NS_SUCCEEDED(nsres
)) {
1239 ret
= get_guid_from_language(language
, guid
);
1241 *guid
= get_default_script_guid(window
);
1244 nsAString_Finish(&val_str
);
1250 static ScriptHost
*get_script_host(HTMLInnerWindow
*window
, const GUID
*guid
)
1254 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
1255 if(IsEqualGUID(guid
, &iter
->guid
))
1259 return create_script_host(window
, guid
);
1262 static ScriptHost
*get_elem_script_host(HTMLInnerWindow
*window
, HTMLScriptElement
*script_elem
)
1266 if(!get_script_guid(window
, script_elem
->nsscript
, &guid
)) {
1267 WARN("Could not find script GUID\n");
1271 if(IsEqualGUID(&CLSID_JScript
, &guid
)
1272 && (!window
->base
.outer_window
|| window
->base
.outer_window
->scriptmode
!= SCRIPTMODE_ACTIVESCRIPT
)) {
1273 TRACE("Ignoring JScript\n");
1277 return get_script_host(window
, &guid
);
1280 void doc_insert_script(HTMLInnerWindow
*window
, HTMLScriptElement
*script_elem
, BOOL from_parser
)
1282 ScriptHost
*script_host
;
1283 BOOL is_complete
= FALSE
;
1285 script_host
= get_elem_script_host(window
, script_elem
);
1289 if(script_host
->parse
) {
1290 if(script_elem
->src_text
) {
1292 set_script_elem_readystate(script_elem
, READYSTATE_INTERACTIVE
);
1293 script_elem
->parsed
= TRUE
;
1294 parse_elem_text(script_host
, script_elem
, script_elem
->src_text
);
1296 }else if(!script_elem
->binding
) {
1297 is_complete
= parse_script_elem(script_host
, script_elem
);
1302 set_script_elem_readystate(script_elem
, READYSTATE_COMPLETE
);
1305 IDispatch
*script_parse_event(HTMLInnerWindow
*window
, LPCWSTR text
)
1307 ScriptHost
*script_host
;
1313 static const WCHAR delimiterW
[] = {'\"',0};
1315 TRACE("%s\n", debugstr_w(text
));
1317 for(ptr
= text
; isalnumW(*ptr
); ptr
++);
1322 language
= heap_alloc((ptr
-text
+1)*sizeof(WCHAR
));
1326 memcpy(language
, text
, (ptr
-text
)*sizeof(WCHAR
));
1327 language
[ptr
-text
] = 0;
1329 b
= get_guid_from_language(language
, &guid
);
1331 heap_free(language
);
1334 WARN("Could not find language\n");
1341 guid
= get_default_script_guid(window
);
1344 if(IsEqualGUID(&CLSID_JScript
, &guid
)
1345 && (!window
->base
.outer_window
|| window
->base
.outer_window
->scriptmode
!= SCRIPTMODE_ACTIVESCRIPT
)) {
1346 TRACE("Ignoring JScript\n");
1350 script_host
= get_script_host(window
, &guid
);
1351 if(!script_host
|| !script_host
->parse_proc
)
1354 hres
= IActiveScriptParseProcedure2_ParseProcedureText(script_host
->parse_proc
, ptr
, NULL
, emptyW
,
1355 NULL
, NULL
, delimiterW
, 0 /* FIXME */, 0,
1356 SCRIPTPROC_HOSTMANAGESSOURCE
|SCRIPTPROC_IMPLICIT_THIS
|SCRIPTPROC_IMPLICIT_PARENTS
, &disp
);
1358 WARN("ParseProcedureText failed: %08x\n", hres
);
1362 TRACE("ret %p\n", disp
);
1366 HRESULT
exec_script(HTMLInnerWindow
*window
, const WCHAR
*code
, const WCHAR
*lang
, VARIANT
*ret
)
1368 ScriptHost
*script_host
;
1373 static const WCHAR delimW
[] = {'"',0};
1375 if(!get_guid_from_language(lang
, &guid
)) {
1376 WARN("Could not find script GUID\n");
1377 return CO_E_CLASSSTRING
;
1380 script_host
= get_script_host(window
, &guid
);
1382 FIXME("No script host\n");
1386 if(!script_host
->parse
) {
1387 FIXME("script_host->parse == NULL\n");
1391 memset(&ei
, 0, sizeof(ei
));
1393 hres
= IActiveScriptParse_ParseScriptText(script_host
->parse
, code
, NULL
, NULL
, delimW
, 0, 0, SCRIPTTEXT_ISVISIBLE
, ret
, &ei
);
1397 WARN("<<< %08x\n", hres
);
1402 IDispatch
*get_script_disp(ScriptHost
*script_host
)
1407 if(!script_host
->script
)
1410 hres
= IActiveScript_GetScriptDispatch(script_host
->script
, windowW
, &disp
);
1417 static EventTarget
*find_event_target(HTMLDocumentNode
*doc
, HTMLScriptElement
*script_elem
)
1419 EventTarget
*event_target
= NULL
;
1420 const PRUnichar
*target_id
;
1421 nsAString target_id_str
;
1425 nsAString_Init(&target_id_str
, NULL
);
1426 nsres
= nsIDOMHTMLScriptElement_GetHtmlFor(script_elem
->nsscript
, &target_id_str
);
1427 if(NS_FAILED(nsres
)) {
1428 ERR("GetScriptFor failed: %08x\n", nsres
);
1429 nsAString_Finish(&target_id_str
);
1433 nsAString_GetData(&target_id_str
, &target_id
);
1435 FIXME("Empty for attribute\n");
1436 }else if(!strcmpW(target_id
, documentW
)) {
1437 event_target
= &doc
->node
.event_target
;
1438 htmldoc_addref(&doc
->basedoc
);
1439 }else if(!strcmpW(target_id
, windowW
)) {
1441 event_target
= &doc
->window
->event_target
;
1442 IDispatchEx_AddRef(&event_target
->dispex
.IDispatchEx_iface
);
1445 HTMLElement
*target_elem
;
1447 hres
= get_doc_elem_by_id(doc
, target_id
, &target_elem
);
1448 if(SUCCEEDED(hres
) && target_elem
) {
1449 event_target
= &target_elem
->node
.event_target
;
1453 nsAString_Finish(&target_id_str
);
1454 return event_target
;
1457 static BOOL
parse_event_str(WCHAR
*event
, const WCHAR
**args
)
1461 TRACE("%s\n", debugstr_w(event
));
1463 for(ptr
= event
; isalnumW(*ptr
); ptr
++);
1474 while(isalnumW(*ptr
) || isspaceW(*ptr
) || *ptr
== ',')
1484 static IDispatch
*parse_event_elem(HTMLDocumentNode
*doc
, HTMLScriptElement
*script_elem
, WCHAR
**ret_event
)
1486 ScriptHost
*script_host
;
1487 WCHAR
*event
= NULL
;
1494 if(script_elem
->parsed
)
1497 script_host
= get_elem_script_host(doc
->window
, script_elem
);
1498 if(!script_host
|| !script_host
->parse_proc
)
1501 nsAString_Init(&nsstr
, NULL
);
1502 nsres
= nsIDOMHTMLScriptElement_GetEvent(script_elem
->nsscript
, &nsstr
);
1503 if(NS_SUCCEEDED(nsres
)) {
1504 const PRUnichar
*event_val
;
1506 nsAString_GetData(&nsstr
, &event_val
);
1507 event
= heap_strdupW(event_val
);
1509 nsAString_Finish(&nsstr
);
1513 if(!parse_event_str(event
, &args
)) {
1514 WARN("parsing %s failed\n", debugstr_w(event
));
1519 nsAString_Init(&nsstr
, NULL
);
1520 nsres
= nsIDOMHTMLScriptElement_GetText(script_elem
->nsscript
, &nsstr
);
1521 if(NS_SUCCEEDED(nsres
)) {
1522 const PRUnichar
*text
;
1524 nsAString_GetData(&nsstr
, &text
);
1525 hres
= IActiveScriptParseProcedure2_ParseProcedureText(script_host
->parse_proc
, text
, args
,
1526 emptyW
, NULL
, NULL
, script_endW
, 0, 0,
1527 SCRIPTPROC_HOSTMANAGESSOURCE
|SCRIPTPROC_IMPLICIT_THIS
|SCRIPTPROC_IMPLICIT_PARENTS
, &disp
);
1531 ERR("GetText failed: %08x\n", nsres
);
1534 nsAString_Finish(&nsstr
);
1544 void bind_event_scripts(HTMLDocumentNode
*doc
)
1546 HTMLPluginContainer
*plugin_container
;
1547 nsIDOMHTMLScriptElement
*nsscript
;
1548 HTMLScriptElement
*script_elem
;
1549 EventTarget
*event_target
;
1550 nsIDOMNodeList
*node_list
;
1551 nsIDOMNode
*script_node
;
1552 nsAString selector_str
;
1553 IDispatch
*event_disp
;
1559 static const PRUnichar selectorW
[] = {'s','c','r','i','p','t','[','e','v','e','n','t',']',0};
1566 nsAString_InitDepend(&selector_str
, selectorW
);
1567 nsres
= nsIDOMHTMLDocument_QuerySelectorAll(doc
->nsdoc
, &selector_str
, &node_list
);
1568 nsAString_Finish(&selector_str
);
1569 if(NS_FAILED(nsres
)) {
1570 ERR("QuerySelectorAll failed: %08x\n", nsres
);
1577 nsres
= nsIDOMNodeList_GetLength(node_list
, &length
);
1578 assert(nsres
== NS_OK
);
1580 for(i
=0; i
< length
; i
++) {
1581 nsres
= nsIDOMNodeList_Item(node_list
, i
, &script_node
);
1582 if(NS_FAILED(nsres
) || !script_node
) {
1583 ERR("Item(%d) failed: %08x\n", i
, nsres
);
1587 nsres
= nsIDOMNode_QueryInterface(script_node
, &IID_nsIDOMHTMLScriptElement
, (void**)&nsscript
);
1588 assert(nsres
== NS_OK
);
1589 nsIDOMNode_Release(script_node
);
1591 hres
= script_elem_from_nsscript(doc
, nsscript
, &script_elem
);
1595 event_disp
= parse_event_elem(doc
, script_elem
, &event
);
1597 event_target
= find_event_target(doc
, script_elem
);
1599 hres
= IDispatchEx_QueryInterface(&event_target
->dispex
.IDispatchEx_iface
, &IID_HTMLPluginContainer
,
1600 (void**)&plugin_container
);
1602 bind_activex_event(doc
, plugin_container
, event
, event_disp
);
1604 bind_target_event(doc
, event_target
, event
, event_disp
);
1606 IDispatchEx_Release(&event_target
->dispex
.IDispatchEx_iface
);
1607 if(plugin_container
)
1608 node_release(&plugin_container
->element
.node
);
1612 IDispatch_Release(event_disp
);
1615 IHTMLScriptElement_Release(&script_elem
->IHTMLScriptElement_iface
);
1618 nsIDOMNodeList_Release(node_list
);
1621 BOOL
find_global_prop(HTMLInnerWindow
*window
, BSTR name
, DWORD flags
, ScriptHost
**ret_host
, DISPID
*ret_id
)
1623 IDispatchEx
*dispex
;
1628 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
1629 disp
= get_script_disp(iter
);
1633 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1634 if(SUCCEEDED(hres
)) {
1635 hres
= IDispatchEx_GetDispID(dispex
, name
, flags
& (~fdexNameEnsure
), ret_id
);
1636 IDispatchEx_Release(dispex
);
1638 FIXME("No IDispatchEx\n");
1642 IDispatch_Release(disp
);
1643 if(SUCCEEDED(hres
)) {
1652 static BOOL
is_jscript_available(void)
1654 static BOOL available
, checked
;
1658 HRESULT hres
= CoGetClassObject(&CLSID_JScript
, CLSCTX_INPROC_SERVER
, NULL
, &IID_IUnknown
, (void**)&unk
);
1660 if(SUCCEEDED(hres
)) {
1662 IUnknown_Release(unk
);
1672 void set_script_mode(HTMLOuterWindow
*window
, SCRIPTMODE mode
)
1674 nsIWebBrowserSetup
*setup
;
1677 if(mode
== SCRIPTMODE_ACTIVESCRIPT
&& !is_jscript_available()) {
1678 TRACE("jscript.dll not available\n");
1679 window
->scriptmode
= SCRIPTMODE_GECKO
;
1683 window
->scriptmode
= mode
;
1685 if(!window
->doc_obj
->nscontainer
|| !window
->doc_obj
->nscontainer
->webbrowser
)
1688 nsres
= nsIWebBrowser_QueryInterface(window
->doc_obj
->nscontainer
->webbrowser
,
1689 &IID_nsIWebBrowserSetup
, (void**)&setup
);
1690 if(NS_SUCCEEDED(nsres
)) {
1691 nsres
= nsIWebBrowserSetup_SetProperty(setup
, SETUP_ALLOW_JAVASCRIPT
,
1692 window
->scriptmode
== SCRIPTMODE_GECKO
);
1694 if(NS_SUCCEEDED(nsres
))
1695 nsres
= nsIWebBrowserSetup_SetProperty(setup
, SETUP_DISABLE_NOSCRIPT
, TRUE
);
1697 nsIWebBrowserSetup_Release(setup
);
1700 if(NS_FAILED(nsres
))
1701 ERR("JavaScript setup failed: %08x\n", nsres
);
1704 void release_script_hosts(HTMLInnerWindow
*window
)
1706 script_queue_entry_t
*queue_iter
;
1709 while(!list_empty(&window
->script_queue
)) {
1710 queue_iter
= LIST_ENTRY(list_head(&window
->script_queue
), script_queue_entry_t
, entry
);
1712 list_remove(&queue_iter
->entry
);
1713 IHTMLScriptElement_Release(&queue_iter
->script
->IHTMLScriptElement_iface
);
1714 heap_free(queue_iter
);
1717 while(!list_empty(&window
->script_hosts
)) {
1718 iter
= LIST_ENTRY(list_head(&window
->script_hosts
), ScriptHost
, entry
);
1720 release_script_engine(iter
);
1721 list_remove(&iter
->entry
);
1722 iter
->window
= NULL
;
1723 IActiveScriptSite_Release(&iter
->IActiveScriptSite_iface
);