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
);
729 HTMLScriptElement
*elem
;
730 } fire_readystatechange_task_t
;
732 static void fire_readystatechange_proc(task_t
*_task
)
734 fire_readystatechange_task_t
*task
= (fire_readystatechange_task_t
*)_task
;
736 if(!task
->elem
->pending_readystatechange_event
)
739 task
->elem
->pending_readystatechange_event
= FALSE
;
740 fire_event(task
->elem
->element
.node
.doc
, EVENTID_READYSTATECHANGE
, FALSE
, &task
->elem
->element
.node
, NULL
, NULL
);
743 static void fire_readystatechange_task_destr(task_t
*_task
)
745 fire_readystatechange_task_t
*task
= (fire_readystatechange_task_t
*)_task
;
747 IHTMLScriptElement_Release(&task
->elem
->IHTMLScriptElement_iface
);
750 static void set_script_elem_readystate(HTMLScriptElement
*script_elem
, READYSTATE readystate
)
752 script_elem
->readystate
= readystate
;
754 if(readystate
!= READYSTATE_INTERACTIVE
) {
755 if(!script_elem
->element
.node
.doc
->window
->parser_callback_cnt
) {
756 fire_readystatechange_task_t
*task
;
759 if(script_elem
->pending_readystatechange_event
)
762 task
= heap_alloc(sizeof(*task
));
766 IHTMLScriptElement_AddRef(&script_elem
->IHTMLScriptElement_iface
);
767 task
->elem
= script_elem
;
769 hres
= push_task(&task
->header
, fire_readystatechange_proc
, fire_readystatechange_task_destr
,
770 script_elem
->element
.node
.doc
->window
->task_magic
);
772 script_elem
->pending_readystatechange_event
= TRUE
;
774 script_elem
->pending_readystatechange_event
= FALSE
;
775 fire_event(script_elem
->element
.node
.doc
, EVENTID_READYSTATECHANGE
, FALSE
,
776 &script_elem
->element
.node
, NULL
, NULL
);
781 static void parse_elem_text(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
, LPCWSTR text
)
787 TRACE("%s\n", debugstr_w(text
));
790 memset(&excepinfo
, 0, sizeof(excepinfo
));
792 hres
= IActiveScriptParse_ParseScriptText(script_host
->parse
, text
, windowW
, NULL
, script_endW
,
793 0, 0, SCRIPTTEXT_ISVISIBLE
|SCRIPTTEXT_HOSTMANAGESSOURCE
,
798 WARN("<<< %08x\n", hres
);
804 HTMLScriptElement
*script_elem
;
811 static HRESULT
get_binding_text(ScriptBSC
*bsc
, WCHAR
**ret
)
816 if(!bsc
->bsc
.readed
) {
817 text
= heap_alloc(sizeof(WCHAR
));
819 return E_OUTOFMEMORY
;
825 switch(bsc
->bsc
.bom
) {
827 if(bsc
->bsc
.readed
% sizeof(WCHAR
)) {
828 FIXME("The buffer is not a valid utf16 string\n");
832 text
= heap_alloc(bsc
->bsc
.readed
+sizeof(WCHAR
));
834 return E_OUTOFMEMORY
;
836 memcpy(text
, bsc
->buf
, bsc
->bsc
.readed
);
837 text
[bsc
->bsc
.readed
/sizeof(WCHAR
)] = 0;
841 /* FIXME: Try to use charset from HTTP headers first */
842 cp
= get_document_charset(bsc
->script_elem
->element
.node
.doc
);
847 len
= MultiByteToWideChar(cp
, 0, bsc
->buf
, bsc
->bsc
.readed
, NULL
, 0);
848 text
= heap_alloc((len
+1)*sizeof(WCHAR
));
850 return E_OUTOFMEMORY
;
852 MultiByteToWideChar(cp
, 0, bsc
->buf
, bsc
->bsc
.readed
, text
, len
);
861 static void script_file_available(ScriptBSC
*bsc
)
863 HTMLScriptElement
*script_elem
= bsc
->script_elem
;
864 HTMLInnerWindow
*window
= bsc
->bsc
.window
;
865 ScriptHost
*script_host
;
870 assert(window
!= NULL
);
872 hres
= get_binding_text(bsc
, &script_elem
->src_text
);
876 script_host
= get_elem_script_host(window
, script_elem
);
880 if(window
->parser_callback_cnt
) {
881 script_queue_entry_t
*queue
;
883 TRACE("Adding to queue\n");
885 queue
= heap_alloc(sizeof(*queue
));
889 IHTMLScriptElement_AddRef(&script_elem
->IHTMLScriptElement_iface
);
890 queue
->script
= script_elem
;
892 list_add_tail(&window
->script_queue
, &queue
->entry
);
896 nsres
= nsIDOMHTMLElement_GetParentNode(script_elem
->element
.nselem
, &parent
);
897 if(NS_FAILED(nsres
) || !parent
) {
898 TRACE("No parent, not executing\n");
899 script_elem
->parse_on_bind
= TRUE
;
903 nsIDOMNode_Release(parent
);
905 script_host
= get_elem_script_host(window
, script_elem
);
909 script_elem
->parsed
= TRUE
;
910 if(script_host
->parse
)
911 parse_elem_text(script_host
, script_elem
, script_elem
->src_text
);
914 static inline ScriptBSC
*impl_from_BSCallback(BSCallback
*iface
)
916 return CONTAINING_RECORD(iface
, ScriptBSC
, bsc
);
919 static void ScriptBSC_destroy(BSCallback
*bsc
)
921 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
923 if(This
->script_elem
) {
924 IHTMLScriptElement_Release(&This
->script_elem
->IHTMLScriptElement_iface
);
925 This
->script_elem
= NULL
;
928 heap_free(This
->buf
);
932 static HRESULT
ScriptBSC_init_bindinfo(BSCallback
*bsc
)
937 static HRESULT
ScriptBSC_start_binding(BSCallback
*bsc
)
939 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
941 This
->script_elem
->binding
= &This
->bsc
;
943 /* FIXME: We should find a better to decide if 'loading' state is supposed to be used by the protocol. */
944 if(This
->scheme
== URL_SCHEME_HTTPS
|| This
->scheme
== URL_SCHEME_HTTP
)
945 set_script_elem_readystate(This
->script_elem
, READYSTATE_LOADING
);
950 static HRESULT
ScriptBSC_stop_binding(BSCallback
*bsc
, HRESULT result
)
952 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
954 if(SUCCEEDED(result
) && !This
->script_elem
)
955 result
= E_UNEXPECTED
;
957 assert(FAILED(result
) || This
->script_elem
->binding
== &This
->bsc
);
958 This
->script_elem
->binding
= NULL
;
960 if(This
->script_elem
->readystate
== READYSTATE_LOADING
)
961 set_script_elem_readystate(This
->script_elem
, READYSTATE_LOADED
);
963 if(SUCCEEDED(result
)) {
964 script_file_available(This
);
966 FIXME("binding failed %08x\n", result
);
967 heap_free(This
->buf
);
972 IHTMLScriptElement_Release(&This
->script_elem
->IHTMLScriptElement_iface
);
973 This
->script_elem
= NULL
;
977 static HRESULT
ScriptBSC_read_data(BSCallback
*bsc
, IStream
*stream
)
979 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
984 This
->buf
= heap_alloc(128);
986 return E_OUTOFMEMORY
;
991 if(This
->bsc
.readed
>= This
->size
) {
993 new_buf
= heap_realloc(This
->buf
, This
->size
<< 1);
995 return E_OUTOFMEMORY
;
1000 hres
= read_stream(&This
->bsc
, stream
, This
->buf
+This
->bsc
.readed
, This
->size
-This
->bsc
.readed
, &readed
);
1001 }while(hres
== S_OK
);
1006 static HRESULT
ScriptBSC_on_progress(BSCallback
*bsc
, ULONG status_code
, LPCWSTR status_text
)
1011 static HRESULT
ScriptBSC_on_response(BSCallback
*bsc
, DWORD response_code
,
1012 LPCWSTR response_headers
)
1017 static HRESULT
ScriptBSC_beginning_transaction(BSCallback
*bsc
, WCHAR
**additional_headers
)
1022 static const BSCallbackVtbl ScriptBSCVtbl
= {
1024 ScriptBSC_init_bindinfo
,
1025 ScriptBSC_start_binding
,
1026 ScriptBSC_stop_binding
,
1027 ScriptBSC_read_data
,
1028 ScriptBSC_on_progress
,
1029 ScriptBSC_on_response
,
1030 ScriptBSC_beginning_transaction
1034 HRESULT
load_script(HTMLScriptElement
*script_elem
, const WCHAR
*src
, BOOL async
)
1036 HTMLInnerWindow
*window
;
1042 static const WCHAR wine_schemaW
[] = {'w','i','n','e',':'};
1044 if(strlenW(src
) > sizeof(wine_schemaW
)/sizeof(WCHAR
) && !memcmp(src
, wine_schemaW
, sizeof(wine_schemaW
)))
1045 src
+= sizeof(wine_schemaW
)/sizeof(WCHAR
);
1047 TRACE("(%p %s %x)\n", script_elem
, debugstr_w(src
), async
);
1049 if(!script_elem
->element
.node
.doc
|| !(window
= script_elem
->element
.node
.doc
->window
)) {
1051 return E_UNEXPECTED
;
1054 hres
= create_uri(src
, 0, &uri
);
1058 hres
= CreateURLMonikerEx2(NULL
, uri
, &mon
, URL_MK_UNIFORM
);
1062 bsc
= heap_alloc_zero(sizeof(*bsc
));
1064 IMoniker_Release(mon
);
1065 return E_OUTOFMEMORY
;
1068 init_bscallback(&bsc
->bsc
, &ScriptBSCVtbl
, mon
, async
? BINDF_ASYNCHRONOUS
| BINDF_ASYNCSTORAGE
| BINDF_PULLDATA
: 0);
1069 IMoniker_Release(mon
);
1071 hres
= IUri_GetScheme(uri
, &bsc
->scheme
);
1073 bsc
->scheme
= URL_SCHEME_UNKNOWN
;
1075 IHTMLScriptElement_AddRef(&script_elem
->IHTMLScriptElement_iface
);
1076 bsc
->script_elem
= script_elem
;
1078 hres
= start_binding(window
, &bsc
->bsc
, NULL
);
1080 IBindStatusCallback_Release(&bsc
->bsc
.IBindStatusCallback_iface
);
1084 static void parse_inline_script(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
)
1086 const PRUnichar
*text
;
1090 nsAString_Init(&text_str
, NULL
);
1091 nsres
= nsIDOMHTMLScriptElement_GetText(script_elem
->nsscript
, &text_str
);
1092 nsAString_GetData(&text_str
, &text
);
1094 set_script_elem_readystate(script_elem
, READYSTATE_INTERACTIVE
);
1096 if(NS_FAILED(nsres
)) {
1097 ERR("GetText failed: %08x\n", nsres
);
1099 parse_elem_text(script_host
, script_elem
, text
);
1102 nsAString_Finish(&text_str
);
1105 static BOOL
parse_script_elem(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
)
1107 nsAString src_str
, event_str
;
1108 BOOL is_complete
= FALSE
;
1109 const PRUnichar
*src
;
1112 nsAString_Init(&event_str
, NULL
);
1113 nsres
= nsIDOMHTMLScriptElement_GetEvent(script_elem
->nsscript
, &event_str
);
1114 if(NS_SUCCEEDED(nsres
)) {
1115 const PRUnichar
*event
;
1117 nsAString_GetData(&event_str
, &event
);
1119 TRACE("deferring event %s script evaluation\n", debugstr_w(event
));
1120 nsAString_Finish(&event_str
);
1124 ERR("GetEvent failed: %08x\n", nsres
);
1126 nsAString_Finish(&event_str
);
1128 nsAString_Init(&src_str
, NULL
);
1129 nsres
= nsIDOMHTMLScriptElement_GetSrc(script_elem
->nsscript
, &src_str
);
1130 nsAString_GetData(&src_str
, &src
);
1132 if(NS_FAILED(nsres
)) {
1133 ERR("GetSrc failed: %08x\n", nsres
);
1135 load_script(script_elem
, src
, FALSE
);
1136 is_complete
= script_elem
->parsed
;
1138 parse_inline_script(script_host
, script_elem
);
1142 nsAString_Finish(&src_str
);
1147 static GUID
get_default_script_guid(HTMLInnerWindow
*window
)
1149 /* If not specified, we should use very first script host that was created for the page (or JScript if none) */
1150 return list_empty(&window
->script_hosts
)
1152 : LIST_ENTRY(list_head(&window
->script_hosts
), ScriptHost
, entry
)->guid
;
1155 static BOOL
get_guid_from_type(LPCWSTR type
, GUID
*guid
)
1157 const WCHAR text_javascriptW
[] =
1158 {'t','e','x','t','/','j','a','v','a','s','c','r','i','p','t',0};
1159 const WCHAR text_jscriptW
[] =
1160 {'t','e','x','t','/','j','s','c','r','i','p','t',0};
1161 const WCHAR text_vbscriptW
[] =
1162 {'t','e','x','t','/','v','b','s','c','r','i','p','t',0};
1164 /* FIXME: Handle more types */
1165 if(!strcmpiW(type
, text_javascriptW
) || !strcmpiW(type
, text_jscriptW
)) {
1166 *guid
= CLSID_JScript
;
1167 }else if(!strcmpiW(type
, text_vbscriptW
)) {
1168 *guid
= CLSID_VBScript
;
1170 FIXME("Unknown type %s\n", debugstr_w(type
));
1177 static BOOL
get_guid_from_language(LPCWSTR type
, GUID
*guid
)
1181 hres
= CLSIDFromProgID(type
, guid
);
1185 /* FIXME: Check CATID_ActiveScriptParse */
1190 static BOOL
get_script_guid(HTMLInnerWindow
*window
, nsIDOMHTMLScriptElement
*nsscript
, GUID
*guid
)
1192 nsIDOMHTMLElement
*nselem
;
1193 const PRUnichar
*language
;
1198 static const PRUnichar languageW
[] = {'l','a','n','g','u','a','g','e',0};
1200 nsAString_Init(&val_str
, NULL
);
1202 nsres
= nsIDOMHTMLScriptElement_GetType(nsscript
, &val_str
);
1203 if(NS_SUCCEEDED(nsres
)) {
1204 const PRUnichar
*type
;
1206 nsAString_GetData(&val_str
, &type
);
1208 ret
= get_guid_from_type(type
, guid
);
1209 nsAString_Finish(&val_str
);
1213 ERR("GetType failed: %08x\n", nsres
);
1216 nsres
= nsIDOMHTMLScriptElement_QueryInterface(nsscript
, &IID_nsIDOMHTMLElement
, (void**)&nselem
);
1217 assert(nsres
== NS_OK
);
1219 nsres
= get_elem_attr_value(nselem
, languageW
, &val_str
, &language
);
1220 nsIDOMHTMLElement_Release(nselem
);
1221 if(NS_SUCCEEDED(nsres
)) {
1223 ret
= get_guid_from_language(language
, guid
);
1225 *guid
= get_default_script_guid(window
);
1228 nsAString_Finish(&val_str
);
1234 static ScriptHost
*get_script_host(HTMLInnerWindow
*window
, const GUID
*guid
)
1238 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
1239 if(IsEqualGUID(guid
, &iter
->guid
))
1243 return create_script_host(window
, guid
);
1246 static ScriptHost
*get_elem_script_host(HTMLInnerWindow
*window
, HTMLScriptElement
*script_elem
)
1250 if(!get_script_guid(window
, script_elem
->nsscript
, &guid
)) {
1251 WARN("Could not find script GUID\n");
1255 if(IsEqualGUID(&CLSID_JScript
, &guid
)
1256 && (!window
->base
.outer_window
|| window
->base
.outer_window
->scriptmode
!= SCRIPTMODE_ACTIVESCRIPT
)) {
1257 TRACE("Ignoring JScript\n");
1261 return get_script_host(window
, &guid
);
1264 void doc_insert_script(HTMLInnerWindow
*window
, HTMLScriptElement
*script_elem
, BOOL from_parser
)
1266 ScriptHost
*script_host
;
1267 BOOL is_complete
= FALSE
;
1269 script_host
= get_elem_script_host(window
, script_elem
);
1273 if(script_host
->parse
) {
1274 if(script_elem
->src_text
) {
1276 set_script_elem_readystate(script_elem
, READYSTATE_INTERACTIVE
);
1277 script_elem
->parsed
= TRUE
;
1278 parse_elem_text(script_host
, script_elem
, script_elem
->src_text
);
1280 }else if(!script_elem
->binding
) {
1281 is_complete
= parse_script_elem(script_host
, script_elem
);
1286 set_script_elem_readystate(script_elem
, READYSTATE_COMPLETE
);
1289 IDispatch
*script_parse_event(HTMLInnerWindow
*window
, LPCWSTR text
)
1291 ScriptHost
*script_host
;
1297 static const WCHAR delimiterW
[] = {'\"',0};
1299 TRACE("%s\n", debugstr_w(text
));
1301 for(ptr
= text
; isalnumW(*ptr
); ptr
++);
1306 language
= heap_alloc((ptr
-text
+1)*sizeof(WCHAR
));
1310 memcpy(language
, text
, (ptr
-text
)*sizeof(WCHAR
));
1311 language
[ptr
-text
] = 0;
1313 b
= get_guid_from_language(language
, &guid
);
1315 heap_free(language
);
1318 WARN("Could not find language\n");
1325 guid
= get_default_script_guid(window
);
1328 if(IsEqualGUID(&CLSID_JScript
, &guid
)
1329 && (!window
->base
.outer_window
|| window
->base
.outer_window
->scriptmode
!= SCRIPTMODE_ACTIVESCRIPT
)) {
1330 TRACE("Ignoring JScript\n");
1334 script_host
= get_script_host(window
, &guid
);
1335 if(!script_host
|| !script_host
->parse_proc
)
1338 hres
= IActiveScriptParseProcedure2_ParseProcedureText(script_host
->parse_proc
, ptr
, NULL
, emptyW
,
1339 NULL
, NULL
, delimiterW
, 0 /* FIXME */, 0,
1340 SCRIPTPROC_HOSTMANAGESSOURCE
|SCRIPTPROC_IMPLICIT_THIS
|SCRIPTPROC_IMPLICIT_PARENTS
, &disp
);
1342 WARN("ParseProcedureText failed: %08x\n", hres
);
1346 TRACE("ret %p\n", disp
);
1350 HRESULT
exec_script(HTMLInnerWindow
*window
, const WCHAR
*code
, const WCHAR
*lang
, VARIANT
*ret
)
1352 ScriptHost
*script_host
;
1357 static const WCHAR delimW
[] = {'"',0};
1359 if(!get_guid_from_language(lang
, &guid
)) {
1360 WARN("Could not find script GUID\n");
1361 return CO_E_CLASSSTRING
;
1364 script_host
= get_script_host(window
, &guid
);
1366 FIXME("No script host\n");
1370 if(!script_host
->parse
) {
1371 FIXME("script_host->parse == NULL\n");
1375 memset(&ei
, 0, sizeof(ei
));
1377 hres
= IActiveScriptParse_ParseScriptText(script_host
->parse
, code
, NULL
, NULL
, delimW
, 0, 0, SCRIPTTEXT_ISVISIBLE
, ret
, &ei
);
1381 WARN("<<< %08x\n", hres
);
1386 IDispatch
*get_script_disp(ScriptHost
*script_host
)
1391 if(!script_host
->script
)
1394 hres
= IActiveScript_GetScriptDispatch(script_host
->script
, windowW
, &disp
);
1401 static EventTarget
*find_event_target(HTMLDocumentNode
*doc
, HTMLScriptElement
*script_elem
)
1403 EventTarget
*event_target
= NULL
;
1404 const PRUnichar
*target_id
;
1405 nsAString target_id_str
;
1409 nsAString_Init(&target_id_str
, NULL
);
1410 nsres
= nsIDOMHTMLScriptElement_GetHtmlFor(script_elem
->nsscript
, &target_id_str
);
1411 if(NS_FAILED(nsres
)) {
1412 ERR("GetScriptFor failed: %08x\n", nsres
);
1413 nsAString_Finish(&target_id_str
);
1417 nsAString_GetData(&target_id_str
, &target_id
);
1419 FIXME("Empty for attribute\n");
1420 }else if(!strcmpW(target_id
, documentW
)) {
1421 event_target
= &doc
->node
.event_target
;
1422 htmldoc_addref(&doc
->basedoc
);
1423 }else if(!strcmpW(target_id
, windowW
)) {
1425 event_target
= &doc
->window
->event_target
;
1426 IDispatchEx_AddRef(&event_target
->dispex
.IDispatchEx_iface
);
1429 HTMLElement
*target_elem
;
1431 hres
= get_doc_elem_by_id(doc
, target_id
, &target_elem
);
1432 if(SUCCEEDED(hres
) && target_elem
) {
1433 event_target
= &target_elem
->node
.event_target
;
1437 nsAString_Finish(&target_id_str
);
1438 return event_target
;
1441 static BOOL
parse_event_str(WCHAR
*event
, const WCHAR
**args
)
1445 TRACE("%s\n", debugstr_w(event
));
1447 for(ptr
= event
; isalnumW(*ptr
); ptr
++);
1458 while(isalnumW(*ptr
) || isspaceW(*ptr
) || *ptr
== ',')
1468 static IDispatch
*parse_event_elem(HTMLDocumentNode
*doc
, HTMLScriptElement
*script_elem
, WCHAR
**ret_event
)
1470 ScriptHost
*script_host
;
1471 WCHAR
*event
= NULL
;
1478 if(script_elem
->parsed
)
1481 script_host
= get_elem_script_host(doc
->window
, script_elem
);
1482 if(!script_host
|| !script_host
->parse_proc
)
1485 nsAString_Init(&nsstr
, NULL
);
1486 nsres
= nsIDOMHTMLScriptElement_GetEvent(script_elem
->nsscript
, &nsstr
);
1487 if(NS_SUCCEEDED(nsres
)) {
1488 const PRUnichar
*event_val
;
1490 nsAString_GetData(&nsstr
, &event_val
);
1491 event
= heap_strdupW(event_val
);
1493 nsAString_Finish(&nsstr
);
1497 if(!parse_event_str(event
, &args
)) {
1498 WARN("parsing %s failed\n", debugstr_w(event
));
1503 nsAString_Init(&nsstr
, NULL
);
1504 nsres
= nsIDOMHTMLScriptElement_GetText(script_elem
->nsscript
, &nsstr
);
1505 if(NS_SUCCEEDED(nsres
)) {
1506 const PRUnichar
*text
;
1508 nsAString_GetData(&nsstr
, &text
);
1509 hres
= IActiveScriptParseProcedure2_ParseProcedureText(script_host
->parse_proc
, text
, args
,
1510 emptyW
, NULL
, NULL
, script_endW
, 0, 0,
1511 SCRIPTPROC_HOSTMANAGESSOURCE
|SCRIPTPROC_IMPLICIT_THIS
|SCRIPTPROC_IMPLICIT_PARENTS
, &disp
);
1515 ERR("GetText failed: %08x\n", nsres
);
1518 nsAString_Finish(&nsstr
);
1528 void bind_event_scripts(HTMLDocumentNode
*doc
)
1530 HTMLPluginContainer
*plugin_container
;
1531 nsIDOMHTMLScriptElement
*nsscript
;
1532 HTMLScriptElement
*script_elem
;
1533 EventTarget
*event_target
;
1534 nsIDOMNodeList
*node_list
;
1535 nsIDOMNode
*script_node
;
1536 nsAString selector_str
;
1537 IDispatch
*event_disp
;
1543 static const PRUnichar selectorW
[] = {'s','c','r','i','p','t','[','e','v','e','n','t',']',0};
1550 nsAString_InitDepend(&selector_str
, selectorW
);
1551 nsres
= nsIDOMHTMLDocument_QuerySelectorAll(doc
->nsdoc
, &selector_str
, &node_list
);
1552 nsAString_Finish(&selector_str
);
1553 if(NS_FAILED(nsres
)) {
1554 ERR("QuerySelectorAll failed: %08x\n", nsres
);
1561 nsres
= nsIDOMNodeList_GetLength(node_list
, &length
);
1562 assert(nsres
== NS_OK
);
1564 for(i
=0; i
< length
; i
++) {
1565 nsres
= nsIDOMNodeList_Item(node_list
, i
, &script_node
);
1566 if(NS_FAILED(nsres
) || !script_node
) {
1567 ERR("Item(%d) failed: %08x\n", i
, nsres
);
1571 nsres
= nsIDOMNode_QueryInterface(script_node
, &IID_nsIDOMHTMLScriptElement
, (void**)&nsscript
);
1572 assert(nsres
== NS_OK
);
1573 nsIDOMNode_Release(script_node
);
1575 hres
= script_elem_from_nsscript(doc
, nsscript
, &script_elem
);
1579 event_disp
= parse_event_elem(doc
, script_elem
, &event
);
1581 event_target
= find_event_target(doc
, script_elem
);
1583 hres
= IDispatchEx_QueryInterface(&event_target
->dispex
.IDispatchEx_iface
, &IID_HTMLPluginContainer
,
1584 (void**)&plugin_container
);
1586 bind_activex_event(doc
, plugin_container
, event
, event_disp
);
1588 bind_target_event(doc
, event_target
, event
, event_disp
);
1590 IDispatchEx_Release(&event_target
->dispex
.IDispatchEx_iface
);
1591 if(plugin_container
)
1592 node_release(&plugin_container
->element
.node
);
1596 IDispatch_Release(event_disp
);
1599 IHTMLScriptElement_Release(&script_elem
->IHTMLScriptElement_iface
);
1602 nsIDOMNodeList_Release(node_list
);
1605 BOOL
find_global_prop(HTMLInnerWindow
*window
, BSTR name
, DWORD flags
, ScriptHost
**ret_host
, DISPID
*ret_id
)
1607 IDispatchEx
*dispex
;
1612 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
1613 disp
= get_script_disp(iter
);
1617 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1618 if(SUCCEEDED(hres
)) {
1619 hres
= IDispatchEx_GetDispID(dispex
, name
, flags
& (~fdexNameEnsure
), ret_id
);
1620 IDispatchEx_Release(dispex
);
1622 FIXME("No IDispatchEx\n");
1626 IDispatch_Release(disp
);
1627 if(SUCCEEDED(hres
)) {
1636 static BOOL
is_jscript_available(void)
1638 static BOOL available
, checked
;
1642 HRESULT hres
= CoGetClassObject(&CLSID_JScript
, CLSCTX_INPROC_SERVER
, NULL
, &IID_IUnknown
, (void**)&unk
);
1644 if(SUCCEEDED(hres
)) {
1646 IUnknown_Release(unk
);
1656 void set_script_mode(HTMLOuterWindow
*window
, SCRIPTMODE mode
)
1658 nsIWebBrowserSetup
*setup
;
1661 if(mode
== SCRIPTMODE_ACTIVESCRIPT
&& !is_jscript_available()) {
1662 TRACE("jscript.dll not available\n");
1663 window
->scriptmode
= SCRIPTMODE_GECKO
;
1667 window
->scriptmode
= mode
;
1669 if(!window
->doc_obj
->nscontainer
|| !window
->doc_obj
->nscontainer
->webbrowser
)
1672 nsres
= nsIWebBrowser_QueryInterface(window
->doc_obj
->nscontainer
->webbrowser
,
1673 &IID_nsIWebBrowserSetup
, (void**)&setup
);
1674 if(NS_SUCCEEDED(nsres
)) {
1675 nsres
= nsIWebBrowserSetup_SetProperty(setup
, SETUP_ALLOW_JAVASCRIPT
,
1676 window
->scriptmode
== SCRIPTMODE_GECKO
);
1678 if(NS_SUCCEEDED(nsres
))
1679 nsres
= nsIWebBrowserSetup_SetProperty(setup
, SETUP_DISABLE_NOSCRIPT
, TRUE
);
1681 nsIWebBrowserSetup_Release(setup
);
1684 if(NS_FAILED(nsres
))
1685 ERR("JavaScript setup failed: %08x\n", nsres
);
1688 void release_script_hosts(HTMLInnerWindow
*window
)
1690 script_queue_entry_t
*queue_iter
;
1693 while(!list_empty(&window
->script_queue
)) {
1694 queue_iter
= LIST_ENTRY(list_head(&window
->script_queue
), script_queue_entry_t
, entry
);
1696 list_remove(&queue_iter
->entry
);
1697 IHTMLScriptElement_Release(&queue_iter
->script
->IHTMLScriptElement_iface
);
1698 heap_free(queue_iter
);
1701 while(!list_empty(&window
->script_hosts
)) {
1702 iter
= LIST_ENTRY(list_head(&window
->script_hosts
), ScriptHost
, entry
);
1704 release_script_engine(iter
);
1705 list_remove(&iter
->entry
);
1706 iter
->window
= NULL
;
1707 IActiveScriptSite_Release(&iter
->IActiveScriptSite_iface
);