2 * Copyright 2008 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
34 #include "wine/debug.h"
36 #include "mshtml_private.h"
37 #include "htmlscript.h"
38 #include "pluginhost.h"
39 #include "htmlevent.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(mshtml
);
46 #define CTXARG_T DWORDLONG
47 #define IActiveScriptSiteDebugVtbl IActiveScriptSiteDebug64Vtbl
49 #define IActiveScriptParse_Release IActiveScriptParse64_Release
50 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
51 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
52 #define IActiveScriptParseProcedure2_Release IActiveScriptParseProcedure2_64_Release
53 #define IActiveScriptParseProcedure2_ParseProcedureText IActiveScriptParseProcedure2_64_ParseProcedureText
57 #define CTXARG_T DWORD
58 #define IActiveScriptSiteDebugVtbl IActiveScriptSiteDebug32Vtbl
60 #define IActiveScriptParse_Release IActiveScriptParse32_Release
61 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
62 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
63 #define IActiveScriptParseProcedure2_Release IActiveScriptParseProcedure2_32_Release
64 #define IActiveScriptParseProcedure2_ParseProcedureText IActiveScriptParseProcedure2_32_ParseProcedureText
68 static const WCHAR documentW
[] = {'d','o','c','u','m','e','n','t',0};
69 static const WCHAR windowW
[] = {'w','i','n','d','o','w',0};
70 static const WCHAR script_endW
[] = {'<','/','S','C','R','I','P','T','>',0};
71 static const WCHAR emptyW
[] = {0};
74 IActiveScriptSite IActiveScriptSite_iface
;
75 IActiveScriptSiteInterruptPoll IActiveScriptSiteInterruptPoll_iface
;
76 IActiveScriptSiteWindow IActiveScriptSiteWindow_iface
;
77 IActiveScriptSiteUIControl IActiveScriptSiteUIControl_iface
;
78 IActiveScriptSiteDebug IActiveScriptSiteDebug_iface
;
79 IServiceProvider IServiceProvider_iface
;
83 IActiveScript
*script
;
84 IActiveScriptParse
*parse
;
85 IActiveScriptParseProcedure2
*parse_proc
;
87 SCRIPTSTATE script_state
;
89 HTMLInnerWindow
*window
;
95 static void set_script_prop(ScriptHost
*script_host
, DWORD property
, VARIANT
*val
)
97 IActiveScriptProperty
*script_prop
;
100 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptProperty
,
101 (void**)&script_prop
);
103 WARN("Could not get IActiveScriptProperty iface: %08x\n", hres
);
107 hres
= IActiveScriptProperty_SetProperty(script_prop
, property
, NULL
, val
);
108 IActiveScriptProperty_Release(script_prop
);
110 WARN("SetProperty(%x) failed: %08x\n", property
, hres
);
113 static BOOL
is_quirks_mode(HTMLDocumentNode
*doc
)
115 const WCHAR
*compat_mode
;
120 static const WCHAR BackCompatW
[] = {'B','a','c','k','C','o','m','p','a','t',0};
122 nsAString_Init(&nsstr
, NULL
);
123 nsres
= nsIDOMHTMLDocument_GetCompatMode(doc
->nsdoc
, &nsstr
);
124 if(NS_SUCCEEDED(nsres
)) {
125 nsAString_GetData(&nsstr
, &compat_mode
);
126 if(!strcmpW(compat_mode
, BackCompatW
))
129 nsAString_Finish(&nsstr
);
133 static BOOL
init_script_engine(ScriptHost
*script_host
)
135 IObjectSafety
*safety
;
137 DWORD supported_opts
=0, enabled_opts
=0;
141 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptParse
, (void**)&script_host
->parse
);
143 WARN("Could not get IActiveScriptHost: %08x\n", hres
);
147 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IObjectSafety
, (void**)&safety
);
149 FIXME("Could not get IObjectSafety: %08x\n", hres
);
153 hres
= IObjectSafety_GetInterfaceSafetyOptions(safety
, &IID_IActiveScriptParse
, &supported_opts
, &enabled_opts
);
155 FIXME("GetInterfaceSafetyOptions failed: %08x\n", hres
);
156 }else if(!(supported_opts
& INTERFACE_USES_DISPEX
)) {
157 FIXME("INTERFACE_USES_DISPEX is not supported\n");
159 hres
= IObjectSafety_SetInterfaceSafetyOptions(safety
, &IID_IActiveScriptParse
,
160 INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
,
161 INTERFACESAFE_FOR_UNTRUSTED_DATA
|INTERFACE_USES_DISPEX
|INTERFACE_USES_SECURITY_MANAGER
);
163 FIXME("SetInterfaceSafetyOptions failed: %08x\n", hres
);
166 IObjectSafety_Release(safety
);
171 V_I4(&var
) = is_quirks_mode(script_host
->window
->doc
) ? 1 : 2;
172 set_script_prop(script_host
, SCRIPTPROP_INVOKEVERSIONING
, &var
);
174 V_VT(&var
) = VT_BOOL
;
175 V_BOOL(&var
) = VARIANT_TRUE
;
176 set_script_prop(script_host
, SCRIPTPROP_HACK_TRIDENTEVENTSINK
, &var
);
178 hres
= IActiveScriptParse_InitNew(script_host
->parse
);
180 WARN("InitNew failed: %08x\n", hres
);
184 hres
= IActiveScript_SetScriptSite(script_host
->script
, &script_host
->IActiveScriptSite_iface
);
186 WARN("SetScriptSite failed: %08x\n", hres
);
187 IActiveScript_Close(script_host
->script
);
191 hres
= IActiveScript_GetScriptState(script_host
->script
, &state
);
193 WARN("GetScriptState failed: %08x\n", hres
);
194 else if(state
!= SCRIPTSTATE_INITIALIZED
)
195 FIXME("state = %x\n", state
);
197 hres
= IActiveScript_SetScriptState(script_host
->script
, SCRIPTSTATE_STARTED
);
199 WARN("Starting script failed: %08x\n", hres
);
203 hres
= IActiveScript_AddNamedItem(script_host
->script
, windowW
,
204 SCRIPTITEM_ISVISIBLE
|SCRIPTITEM_ISSOURCE
|SCRIPTITEM_GLOBALMEMBERS
);
205 if(SUCCEEDED(hres
)) {
206 V_VT(&var
) = VT_BOOL
;
207 V_BOOL(&var
) = VARIANT_TRUE
;
208 set_script_prop(script_host
, SCRIPTPROP_ABBREVIATE_GLOBALNAME_RESOLUTION
, &var
);
210 WARN("AddNamedItem failed: %08x\n", hres
);
213 hres
= IActiveScript_QueryInterface(script_host
->script
, &IID_IActiveScriptParseProcedure2
,
214 (void**)&script_host
->parse_proc
);
216 /* FIXME: QI for IActiveScriptParseProcedure */
217 WARN("Could not get IActiveScriptParseProcedure iface: %08x\n", hres
);
223 static void release_script_engine(ScriptHost
*This
)
228 switch(This
->script_state
) {
229 case SCRIPTSTATE_CONNECTED
:
230 IActiveScript_SetScriptState(This
->script
, SCRIPTSTATE_DISCONNECTED
);
232 case SCRIPTSTATE_STARTED
:
233 case SCRIPTSTATE_DISCONNECTED
:
234 case SCRIPTSTATE_INITIALIZED
:
235 IActiveScript_Close(This
->script
);
238 if(This
->parse_proc
) {
239 IActiveScriptParseProcedure2_Release(This
->parse_proc
);
240 This
->parse_proc
= NULL
;
244 IActiveScriptParse_Release(This
->parse
);
249 IActiveScript_Release(This
->script
);
251 This
->script_state
= SCRIPTSTATE_UNINITIALIZED
;
254 void connect_scripts(HTMLInnerWindow
*window
)
258 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
259 if(iter
->script_state
== SCRIPTSTATE_STARTED
)
260 IActiveScript_SetScriptState(iter
->script
, SCRIPTSTATE_CONNECTED
);
264 static inline ScriptHost
*impl_from_IActiveScriptSite(IActiveScriptSite
*iface
)
266 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSite_iface
);
269 static HRESULT WINAPI
ActiveScriptSite_QueryInterface(IActiveScriptSite
*iface
, REFIID riid
, void **ppv
)
271 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
275 if(IsEqualGUID(&IID_IUnknown
, riid
)) {
276 TRACE("(%p)->(IID_IUnknown %p)\n", This
, ppv
);
277 *ppv
= &This
->IActiveScriptSite_iface
;
278 }else if(IsEqualGUID(&IID_IActiveScriptSite
, riid
)) {
279 TRACE("(%p)->(IID_IActiveScriptSite %p)\n", This
, ppv
);
280 *ppv
= &This
->IActiveScriptSite_iface
;
281 }else if(IsEqualGUID(&IID_IActiveScriptSiteInterruptPoll
, riid
)) {
282 TRACE("(%p)->(IID_IActiveScriptSiteInterruprtPoll %p)\n", This
, ppv
);
283 *ppv
= &This
->IActiveScriptSiteInterruptPoll_iface
;
284 }else if(IsEqualGUID(&IID_IActiveScriptSiteWindow
, riid
)) {
285 TRACE("(%p)->(IID_IActiveScriptSiteWindow %p)\n", This
, ppv
);
286 *ppv
= &This
->IActiveScriptSiteWindow_iface
;
287 }else if(IsEqualGUID(&IID_IActiveScriptSiteUIControl
, riid
)) {
288 TRACE("(%p)->(IID_IActiveScriptSiteUIControl %p)\n", This
, ppv
);
289 *ppv
= &This
->IActiveScriptSiteUIControl_iface
;
290 }else if(IsEqualGUID(&IID_IActiveScriptSiteDebug
, riid
)) {
291 TRACE("(%p)->(IID_IActiveScriptSiteDebug %p)\n", This
, ppv
);
292 *ppv
= &This
->IActiveScriptSiteDebug_iface
;
293 }else if(IsEqualGUID(&IID_IServiceProvider
, riid
)) {
294 TRACE("(%p)->(IID_IServiceProvider %p)\n", This
, ppv
);
295 *ppv
= &This
->IServiceProvider_iface
;
296 }else if(IsEqualGUID(&IID_ICanHandleException
, riid
)) {
297 TRACE("(%p)->(IID_ICanHandleException not supported %p)\n", This
, ppv
);
298 return E_NOINTERFACE
;
300 FIXME("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), ppv
);
301 return E_NOINTERFACE
;
304 IUnknown_AddRef((IUnknown
*)*ppv
);
308 static ULONG WINAPI
ActiveScriptSite_AddRef(IActiveScriptSite
*iface
)
310 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
311 LONG ref
= InterlockedIncrement(&This
->ref
);
313 TRACE("(%p) ref=%d\n", This
, ref
);
318 static ULONG WINAPI
ActiveScriptSite_Release(IActiveScriptSite
*iface
)
320 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
321 LONG ref
= InterlockedDecrement(&This
->ref
);
323 TRACE("(%p) ref=%d\n", This
, ref
);
326 release_script_engine(This
);
328 list_remove(&This
->entry
);
335 static HRESULT WINAPI
ActiveScriptSite_GetLCID(IActiveScriptSite
*iface
, LCID
*plcid
)
337 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
339 TRACE("(%p)->(%p)\n", This
, plcid
);
341 *plcid
= GetUserDefaultLCID();
345 static HRESULT WINAPI
ActiveScriptSite_GetItemInfo(IActiveScriptSite
*iface
, LPCOLESTR pstrName
,
346 DWORD dwReturnMask
, IUnknown
**ppiunkItem
, ITypeInfo
**ppti
)
348 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
350 TRACE("(%p)->(%s %x %p %p)\n", This
, debugstr_w(pstrName
), dwReturnMask
, ppiunkItem
, ppti
);
352 if(dwReturnMask
!= SCRIPTINFO_IUNKNOWN
) {
353 FIXME("Unsupported mask %x\n", dwReturnMask
);
359 if(strcmpW(pstrName
, windowW
))
360 return DISP_E_MEMBERNOTFOUND
;
365 /* FIXME: Return proxy object */
366 *ppiunkItem
= (IUnknown
*)&This
->window
->base
.IHTMLWindow2_iface
;
367 IUnknown_AddRef(*ppiunkItem
);
372 static HRESULT WINAPI
ActiveScriptSite_GetDocVersionString(IActiveScriptSite
*iface
, BSTR
*pbstrVersion
)
374 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
375 FIXME("(%p)->(%p)\n", This
, pbstrVersion
);
379 static HRESULT WINAPI
ActiveScriptSite_OnScriptTerminate(IActiveScriptSite
*iface
,
380 const VARIANT
*pvarResult
, const EXCEPINFO
*pexcepinfo
)
382 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
383 FIXME("(%p)->(%p %p)\n", This
, pvarResult
, pexcepinfo
);
387 static HRESULT WINAPI
ActiveScriptSite_OnStateChange(IActiveScriptSite
*iface
, SCRIPTSTATE ssScriptState
)
389 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
391 TRACE("(%p)->(%x)\n", This
, ssScriptState
);
393 This
->script_state
= ssScriptState
;
397 static HRESULT WINAPI
ActiveScriptSite_OnScriptError(IActiveScriptSite
*iface
, IActiveScriptError
*pscripterror
)
399 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
400 FIXME("(%p)->(%p)\n", This
, pscripterror
);
404 static HRESULT WINAPI
ActiveScriptSite_OnEnterScript(IActiveScriptSite
*iface
)
406 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
408 TRACE("(%p)->()\n", This
);
413 static HRESULT WINAPI
ActiveScriptSite_OnLeaveScript(IActiveScriptSite
*iface
)
415 ScriptHost
*This
= impl_from_IActiveScriptSite(iface
);
417 TRACE("(%p)->()\n", This
);
422 static const IActiveScriptSiteVtbl ActiveScriptSiteVtbl
= {
423 ActiveScriptSite_QueryInterface
,
424 ActiveScriptSite_AddRef
,
425 ActiveScriptSite_Release
,
426 ActiveScriptSite_GetLCID
,
427 ActiveScriptSite_GetItemInfo
,
428 ActiveScriptSite_GetDocVersionString
,
429 ActiveScriptSite_OnScriptTerminate
,
430 ActiveScriptSite_OnStateChange
,
431 ActiveScriptSite_OnScriptError
,
432 ActiveScriptSite_OnEnterScript
,
433 ActiveScriptSite_OnLeaveScript
436 static inline ScriptHost
*impl_from_IActiveScriptSiteInterruptPoll(IActiveScriptSiteInterruptPoll
*iface
)
438 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteInterruptPoll_iface
);
441 static HRESULT WINAPI
ActiveScriptSiteInterruptPoll_QueryInterface(IActiveScriptSiteInterruptPoll
*iface
,
442 REFIID riid
, void **ppv
)
444 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
445 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
448 static ULONG WINAPI
ActiveScriptSiteInterruptPoll_AddRef(IActiveScriptSiteInterruptPoll
*iface
)
450 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
451 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
454 static ULONG WINAPI
ActiveScriptSiteInterruptPoll_Release(IActiveScriptSiteInterruptPoll
*iface
)
456 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
457 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
460 static HRESULT WINAPI
ActiveScriptSiteInterruptPoll_QueryContinue(IActiveScriptSiteInterruptPoll
*iface
)
462 ScriptHost
*This
= impl_from_IActiveScriptSiteInterruptPoll(iface
);
464 TRACE("(%p)\n", This
);
469 static const IActiveScriptSiteInterruptPollVtbl ActiveScriptSiteInterruptPollVtbl
= {
470 ActiveScriptSiteInterruptPoll_QueryInterface
,
471 ActiveScriptSiteInterruptPoll_AddRef
,
472 ActiveScriptSiteInterruptPoll_Release
,
473 ActiveScriptSiteInterruptPoll_QueryContinue
476 static inline ScriptHost
*impl_from_IActiveScriptSiteWindow(IActiveScriptSiteWindow
*iface
)
478 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteWindow_iface
);
481 static HRESULT WINAPI
ActiveScriptSiteWindow_QueryInterface(IActiveScriptSiteWindow
*iface
,
482 REFIID riid
, void **ppv
)
484 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
485 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
488 static ULONG WINAPI
ActiveScriptSiteWindow_AddRef(IActiveScriptSiteWindow
*iface
)
490 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
491 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
494 static ULONG WINAPI
ActiveScriptSiteWindow_Release(IActiveScriptSiteWindow
*iface
)
496 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
497 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
500 static HRESULT WINAPI
ActiveScriptSiteWindow_GetWindow(IActiveScriptSiteWindow
*iface
, HWND
*phwnd
)
502 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
504 TRACE("(%p)->(%p)\n", This
, phwnd
);
506 if(!This
->window
|| !This
->window
->base
.outer_window
|| !This
->window
->base
.outer_window
->doc_obj
)
509 *phwnd
= This
->window
->base
.outer_window
->doc_obj
->hwnd
;
513 static HRESULT WINAPI
ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow
*iface
, BOOL fEnable
)
515 ScriptHost
*This
= impl_from_IActiveScriptSiteWindow(iface
);
516 FIXME("(%p)->(%x)\n", This
, fEnable
);
520 static const IActiveScriptSiteWindowVtbl ActiveScriptSiteWindowVtbl
= {
521 ActiveScriptSiteWindow_QueryInterface
,
522 ActiveScriptSiteWindow_AddRef
,
523 ActiveScriptSiteWindow_Release
,
524 ActiveScriptSiteWindow_GetWindow
,
525 ActiveScriptSiteWindow_EnableModeless
528 static inline ScriptHost
*impl_from_IActiveScriptSiteUIControl(IActiveScriptSiteUIControl
*iface
)
530 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteUIControl_iface
);
533 static HRESULT WINAPI
ActiveScriptSiteUIControl_QueryInterface(IActiveScriptSiteUIControl
*iface
, REFIID riid
, void **ppv
)
535 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
536 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
539 static ULONG WINAPI
ActiveScriptSiteUIControl_AddRef(IActiveScriptSiteUIControl
*iface
)
541 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
542 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
545 static ULONG WINAPI
ActiveScriptSiteUIControl_Release(IActiveScriptSiteUIControl
*iface
)
547 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
548 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
551 static HRESULT WINAPI
ActiveScriptSiteUIControl_GetUIBehavior(IActiveScriptSiteUIControl
*iface
, SCRIPTUICITEM UicItem
,
552 SCRIPTUICHANDLING
*pUicHandling
)
554 ScriptHost
*This
= impl_from_IActiveScriptSiteUIControl(iface
);
556 WARN("(%p)->(%d %p) semi-stub\n", This
, UicItem
, pUicHandling
);
558 *pUicHandling
= SCRIPTUICHANDLING_ALLOW
;
562 static const IActiveScriptSiteUIControlVtbl ActiveScriptSiteUIControlVtbl
= {
563 ActiveScriptSiteUIControl_QueryInterface
,
564 ActiveScriptSiteUIControl_AddRef
,
565 ActiveScriptSiteUIControl_Release
,
566 ActiveScriptSiteUIControl_GetUIBehavior
569 static inline ScriptHost
*impl_from_IActiveScriptSiteDebug(IActiveScriptSiteDebug
*iface
)
571 return CONTAINING_RECORD(iface
, ScriptHost
, IActiveScriptSiteDebug_iface
);
574 static HRESULT WINAPI
ActiveScriptSiteDebug_QueryInterface(IActiveScriptSiteDebug
*iface
,
575 REFIID riid
, void **ppv
)
577 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
578 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
581 static ULONG WINAPI
ActiveScriptSiteDebug_AddRef(IActiveScriptSiteDebug
*iface
)
583 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
584 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
587 static ULONG WINAPI
ActiveScriptSiteDebug_Release(IActiveScriptSiteDebug
*iface
)
589 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
590 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
593 static HRESULT WINAPI
ActiveScriptSiteDebug_GetDocumentContextFromPosition(IActiveScriptSiteDebug
*iface
,
594 CTXARG_T dwSourceContext
, ULONG uCharacterOffset
, ULONG uNumChars
, IDebugDocumentContext
**ppsc
)
596 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
597 FIXME("(%p)->(%s %u %u %p)\n", This
, wine_dbgstr_longlong(dwSourceContext
), uCharacterOffset
,
602 static HRESULT WINAPI
ActiveScriptSiteDebug_GetApplication(IActiveScriptSiteDebug
*iface
, IDebugApplication
**ppda
)
604 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
605 FIXME("(%p)->(%p)\n", This
, ppda
);
609 static HRESULT WINAPI
ActiveScriptSiteDebug_GetRootApplicationNode(IActiveScriptSiteDebug
*iface
,
610 IDebugApplicationNode
**ppdanRoot
)
612 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
613 FIXME("(%p)->(%p)\n", This
, ppdanRoot
);
617 static HRESULT WINAPI
ActiveScriptSiteDebug_OnScriptErrorDebug(IActiveScriptSiteDebug
*iface
,
618 IActiveScriptErrorDebug
*pErrorDebug
, BOOL
*pfEnterDebugger
, BOOL
*pfCallOnScriptErrorWhenContinuing
)
620 ScriptHost
*This
= impl_from_IActiveScriptSiteDebug(iface
);
621 FIXME("(%p)->(%p %p %p)\n", This
, pErrorDebug
, pfEnterDebugger
, pfCallOnScriptErrorWhenContinuing
);
625 static const IActiveScriptSiteDebugVtbl ActiveScriptSiteDebugVtbl
= {
626 ActiveScriptSiteDebug_QueryInterface
,
627 ActiveScriptSiteDebug_AddRef
,
628 ActiveScriptSiteDebug_Release
,
629 ActiveScriptSiteDebug_GetDocumentContextFromPosition
,
630 ActiveScriptSiteDebug_GetApplication
,
631 ActiveScriptSiteDebug_GetRootApplicationNode
,
632 ActiveScriptSiteDebug_OnScriptErrorDebug
635 static inline ScriptHost
*impl_from_IServiceProvider(IServiceProvider
*iface
)
637 return CONTAINING_RECORD(iface
, ScriptHost
, IServiceProvider_iface
);
640 static HRESULT WINAPI
ASServiceProvider_QueryInterface(IServiceProvider
*iface
, REFIID riid
, void **ppv
)
642 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
643 return IActiveScriptSite_QueryInterface(&This
->IActiveScriptSite_iface
, riid
, ppv
);
646 static ULONG WINAPI
ASServiceProvider_AddRef(IServiceProvider
*iface
)
648 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
649 return IActiveScriptSite_AddRef(&This
->IActiveScriptSite_iface
);
652 static ULONG WINAPI
ASServiceProvider_Release(IServiceProvider
*iface
)
654 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
655 return IActiveScriptSite_Release(&This
->IActiveScriptSite_iface
);
658 static HRESULT WINAPI
ASServiceProvider_QueryService(IServiceProvider
*iface
, REFGUID guidService
,
659 REFIID riid
, void **ppv
)
661 ScriptHost
*This
= impl_from_IServiceProvider(iface
);
663 if(IsEqualGUID(&SID_SInternetHostSecurityManager
, guidService
)) {
664 TRACE("(%p)->(SID_SInternetHostSecurityManager)\n", This
);
666 if(!This
->window
|| !This
->window
->doc
)
667 return E_NOINTERFACE
;
669 return IInternetHostSecurityManager_QueryInterface(&This
->window
->doc
->IInternetHostSecurityManager_iface
,
673 if(IsEqualGUID(&SID_SContainerDispatch
, guidService
)) {
674 TRACE("(%p)->(SID_SContainerDispatch)\n", This
);
676 if(!This
->window
|| !This
->window
->doc
)
677 return E_NOINTERFACE
;
679 return IHTMLDocument2_QueryInterface(&This
->window
->doc
->basedoc
.IHTMLDocument2_iface
, riid
, ppv
);
682 FIXME("(%p)->(%s %s %p)\n", This
, debugstr_guid(guidService
), debugstr_guid(riid
), ppv
);
683 return E_NOINTERFACE
;
686 static const IServiceProviderVtbl ASServiceProviderVtbl
= {
687 ASServiceProvider_QueryInterface
,
688 ASServiceProvider_AddRef
,
689 ASServiceProvider_Release
,
690 ASServiceProvider_QueryService
693 static ScriptHost
*create_script_host(HTMLInnerWindow
*window
, const GUID
*guid
)
698 ret
= heap_alloc_zero(sizeof(*ret
));
702 ret
->IActiveScriptSite_iface
.lpVtbl
= &ActiveScriptSiteVtbl
;
703 ret
->IActiveScriptSiteInterruptPoll_iface
.lpVtbl
= &ActiveScriptSiteInterruptPollVtbl
;
704 ret
->IActiveScriptSiteWindow_iface
.lpVtbl
= &ActiveScriptSiteWindowVtbl
;
705 ret
->IActiveScriptSiteUIControl_iface
.lpVtbl
= &ActiveScriptSiteUIControlVtbl
;
706 ret
->IActiveScriptSiteDebug_iface
.lpVtbl
= &ActiveScriptSiteDebugVtbl
;
707 ret
->IServiceProvider_iface
.lpVtbl
= &ASServiceProviderVtbl
;
709 ret
->window
= window
;
710 ret
->script_state
= SCRIPTSTATE_UNINITIALIZED
;
713 list_add_tail(&window
->script_hosts
, &ret
->entry
);
715 hres
= CoCreateInstance(&ret
->guid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
716 &IID_IActiveScript
, (void**)&ret
->script
);
718 WARN("Could not load script engine: %08x\n", hres
);
719 else if(!init_script_engine(ret
))
720 release_script_engine(ret
);
727 HTMLScriptElement
*elem
;
728 } fire_readystatechange_task_t
;
730 static void fire_readystatechange_proc(task_t
*_task
)
732 fire_readystatechange_task_t
*task
= (fire_readystatechange_task_t
*)_task
;
734 if(!task
->elem
->pending_readystatechange_event
)
737 task
->elem
->pending_readystatechange_event
= FALSE
;
738 fire_event(task
->elem
->element
.node
.doc
, EVENTID_READYSTATECHANGE
, FALSE
, task
->elem
->element
.node
.nsnode
, NULL
, NULL
);
741 static void fire_readystatechange_task_destr(task_t
*_task
)
743 fire_readystatechange_task_t
*task
= (fire_readystatechange_task_t
*)_task
;
745 IHTMLScriptElement_Release(&task
->elem
->IHTMLScriptElement_iface
);
748 static void set_script_elem_readystate(HTMLScriptElement
*script_elem
, READYSTATE readystate
)
750 script_elem
->readystate
= readystate
;
752 if(readystate
!= READYSTATE_INTERACTIVE
) {
753 if(!script_elem
->element
.node
.doc
->window
->parser_callback_cnt
) {
754 fire_readystatechange_task_t
*task
;
757 if(script_elem
->pending_readystatechange_event
)
760 task
= heap_alloc(sizeof(*task
));
764 IHTMLScriptElement_AddRef(&script_elem
->IHTMLScriptElement_iface
);
765 task
->elem
= script_elem
;
767 hres
= push_task(&task
->header
, fire_readystatechange_proc
, fire_readystatechange_task_destr
,
768 script_elem
->element
.node
.doc
->window
->task_magic
);
770 script_elem
->pending_readystatechange_event
= TRUE
;
772 script_elem
->pending_readystatechange_event
= FALSE
;
773 fire_event(script_elem
->element
.node
.doc
, EVENTID_READYSTATECHANGE
, FALSE
,
774 script_elem
->element
.node
.nsnode
, NULL
, NULL
);
779 static void parse_elem_text(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
, LPCWSTR text
)
785 TRACE("%s\n", debugstr_w(text
));
787 set_script_elem_readystate(script_elem
, READYSTATE_INTERACTIVE
);
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
);
805 HTMLScriptElement
*script_elem
;
813 static inline ScriptBSC
*impl_from_BSCallback(BSCallback
*iface
)
815 return CONTAINING_RECORD(iface
, ScriptBSC
, bsc
);
818 static void ScriptBSC_destroy(BSCallback
*bsc
)
820 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
822 if(This
->script_elem
) {
823 IHTMLScriptElement_Release(&This
->script_elem
->IHTMLScriptElement_iface
);
824 This
->script_elem
= NULL
;
827 heap_free(This
->buf
);
831 static HRESULT
ScriptBSC_init_bindinfo(BSCallback
*bsc
)
836 static HRESULT
ScriptBSC_start_binding(BSCallback
*bsc
)
838 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
840 /* FIXME: We should find a better to decide if 'loading' state is supposed to be used by the protocol. */
841 if(This
->scheme
== URL_SCHEME_HTTPS
|| This
->scheme
== URL_SCHEME_HTTP
)
842 set_script_elem_readystate(This
->script_elem
, READYSTATE_LOADING
);
847 static HRESULT
ScriptBSC_stop_binding(BSCallback
*bsc
, HRESULT result
)
849 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
853 if(SUCCEEDED(result
)) {
854 if(This
->script_elem
->readystate
== READYSTATE_LOADING
)
855 set_script_elem_readystate(This
->script_elem
, READYSTATE_LOADED
);
857 FIXME("binding failed %08x\n", result
);
858 heap_free(This
->buf
);
863 IHTMLScriptElement_Release(&This
->script_elem
->IHTMLScriptElement_iface
);
864 This
->script_elem
= NULL
;
868 static HRESULT
ScriptBSC_read_data(BSCallback
*bsc
, IStream
*stream
)
870 ScriptBSC
*This
= impl_from_BSCallback(bsc
);
875 This
->buf
= heap_alloc(128);
877 return E_OUTOFMEMORY
;
882 if(This
->bsc
.readed
>= This
->size
) {
884 new_buf
= heap_realloc(This
->buf
, This
->size
<< 1);
886 return E_OUTOFMEMORY
;
891 hres
= read_stream(&This
->bsc
, stream
, This
->buf
+This
->bsc
.readed
, This
->size
-This
->bsc
.readed
, &readed
);
892 }while(hres
== S_OK
);
897 static HRESULT
ScriptBSC_on_progress(BSCallback
*bsc
, ULONG status_code
, LPCWSTR status_text
)
902 static HRESULT
ScriptBSC_on_response(BSCallback
*bsc
, DWORD response_code
,
903 LPCWSTR response_headers
)
908 static HRESULT
ScriptBSC_beginning_transaction(BSCallback
*bsc
, WCHAR
**additional_headers
)
913 static const BSCallbackVtbl ScriptBSCVtbl
= {
915 ScriptBSC_init_bindinfo
,
916 ScriptBSC_start_binding
,
917 ScriptBSC_stop_binding
,
919 ScriptBSC_on_progress
,
920 ScriptBSC_on_response
,
921 ScriptBSC_beginning_transaction
925 static HRESULT
bind_script_to_text(HTMLInnerWindow
*window
, IUri
*uri
, HTMLScriptElement
*script_elem
, WCHAR
**ret
)
933 hres
= CreateURLMonikerEx2(NULL
, uri
, &mon
, URL_MK_UNIFORM
);
937 bsc
= heap_alloc_zero(sizeof(*bsc
));
939 IMoniker_Release(mon
);
940 return E_OUTOFMEMORY
;
943 init_bscallback(&bsc
->bsc
, &ScriptBSCVtbl
, mon
, 0);
944 IMoniker_Release(mon
);
947 hres
= IUri_GetScheme(uri
, &bsc
->scheme
);
949 bsc
->scheme
= URL_SCHEME_UNKNOWN
;
951 IHTMLScriptElement_AddRef(&script_elem
->IHTMLScriptElement_iface
);
952 bsc
->script_elem
= script_elem
;
954 hres
= start_binding(window
, &bsc
->bsc
, NULL
);
958 IBindStatusCallback_Release(&bsc
->bsc
.IBindStatusCallback_iface
);
962 if(!bsc
->bsc
.readed
) {
967 switch(bsc
->bsc
.bom
) {
969 if(bsc
->bsc
.readed
% sizeof(WCHAR
)) {
970 FIXME("The buffer is not a valid utf16 string\n");
975 text
= heap_alloc(bsc
->bsc
.readed
+sizeof(WCHAR
));
977 hres
= E_OUTOFMEMORY
;
981 memcpy(text
, bsc
->buf
, bsc
->bsc
.readed
);
982 text
[bsc
->bsc
.readed
/sizeof(WCHAR
)] = 0;
986 /* FIXME: Try to use charset from HTTP headers first */
987 cp
= get_document_charset(window
->doc
);
992 len
= MultiByteToWideChar(cp
, 0, bsc
->buf
, bsc
->bsc
.readed
, NULL
, 0);
993 text
= heap_alloc((len
+1)*sizeof(WCHAR
));
995 hres
= E_OUTOFMEMORY
;
999 MultiByteToWideChar(cp
, 0, bsc
->buf
, bsc
->bsc
.readed
, text
, len
);
1004 IBindStatusCallback_Release(&bsc
->bsc
.IBindStatusCallback_iface
);
1012 static void parse_extern_script(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
, LPCWSTR src
)
1018 static const WCHAR wine_schemaW
[] = {'w','i','n','e',':'};
1020 if(strlenW(src
) > sizeof(wine_schemaW
)/sizeof(WCHAR
) && !memcmp(src
, wine_schemaW
, sizeof(wine_schemaW
)))
1021 src
+= sizeof(wine_schemaW
)/sizeof(WCHAR
);
1023 hres
= create_uri(src
, 0, &uri
);
1027 hres
= bind_script_to_text(script_host
->window
, uri
, script_elem
, &text
);
1029 if(FAILED(hres
) || !text
)
1032 parse_elem_text(script_host
, script_elem
, text
);
1037 static void parse_inline_script(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
)
1039 const PRUnichar
*text
;
1043 nsAString_Init(&text_str
, NULL
);
1044 nsres
= nsIDOMHTMLScriptElement_GetText(script_elem
->nsscript
, &text_str
);
1045 nsAString_GetData(&text_str
, &text
);
1047 if(NS_FAILED(nsres
)) {
1048 ERR("GetText failed: %08x\n", nsres
);
1050 parse_elem_text(script_host
, script_elem
, text
);
1053 nsAString_Finish(&text_str
);
1056 static void parse_script_elem(ScriptHost
*script_host
, HTMLScriptElement
*script_elem
)
1058 nsAString src_str
, event_str
;
1059 const PRUnichar
*src
;
1062 nsAString_Init(&event_str
, NULL
);
1063 nsres
= nsIDOMHTMLScriptElement_GetEvent(script_elem
->nsscript
, &event_str
);
1064 if(NS_SUCCEEDED(nsres
)) {
1065 const PRUnichar
*event
;
1067 nsAString_GetData(&event_str
, &event
);
1069 TRACE("deferring event %s script evaluation\n", debugstr_w(event
));
1070 nsAString_Finish(&event_str
);
1074 ERR("GetEvent failed: %08x\n", nsres
);
1076 nsAString_Finish(&event_str
);
1078 nsAString_Init(&src_str
, NULL
);
1079 nsres
= nsIDOMHTMLScriptElement_GetSrc(script_elem
->nsscript
, &src_str
);
1080 nsAString_GetData(&src_str
, &src
);
1082 if(NS_FAILED(nsres
)) {
1083 ERR("GetSrc failed: %08x\n", nsres
);
1085 script_elem
->parsed
= TRUE
;
1086 parse_extern_script(script_host
, script_elem
, src
);
1088 parse_inline_script(script_host
, script_elem
);
1091 nsAString_Finish(&src_str
);
1093 set_script_elem_readystate(script_elem
, READYSTATE_COMPLETE
);
1096 static GUID
get_default_script_guid(HTMLInnerWindow
*window
)
1098 /* If not specified, we should use very first script host that was created for the page (or JScript if none) */
1099 return list_empty(&window
->script_hosts
)
1101 : LIST_ENTRY(list_head(&window
->script_hosts
), ScriptHost
, entry
)->guid
;
1104 static BOOL
get_guid_from_type(LPCWSTR type
, GUID
*guid
)
1106 const WCHAR text_javascriptW
[] =
1107 {'t','e','x','t','/','j','a','v','a','s','c','r','i','p','t',0};
1108 const WCHAR text_jscriptW
[] =
1109 {'t','e','x','t','/','j','s','c','r','i','p','t',0};
1110 const WCHAR text_vbscriptW
[] =
1111 {'t','e','x','t','/','v','b','s','c','r','i','p','t',0};
1113 /* FIXME: Handle more types */
1114 if(!strcmpiW(type
, text_javascriptW
) || !strcmpiW(type
, text_jscriptW
)) {
1115 *guid
= CLSID_JScript
;
1116 }else if(!strcmpiW(type
, text_vbscriptW
)) {
1117 *guid
= CLSID_VBScript
;
1119 FIXME("Unknown type %s\n", debugstr_w(type
));
1126 static BOOL
get_guid_from_language(LPCWSTR type
, GUID
*guid
)
1130 hres
= CLSIDFromProgID(type
, guid
);
1134 /* FIXME: Check CATID_ActiveScriptParse */
1139 static BOOL
get_script_guid(HTMLInnerWindow
*window
, nsIDOMHTMLScriptElement
*nsscript
, GUID
*guid
)
1141 nsIDOMHTMLElement
*nselem
;
1142 const PRUnichar
*language
;
1147 static const PRUnichar languageW
[] = {'l','a','n','g','u','a','g','e',0};
1149 nsAString_Init(&val_str
, NULL
);
1151 nsres
= nsIDOMHTMLScriptElement_GetType(nsscript
, &val_str
);
1152 if(NS_SUCCEEDED(nsres
)) {
1153 const PRUnichar
*type
;
1155 nsAString_GetData(&val_str
, &type
);
1157 ret
= get_guid_from_type(type
, guid
);
1158 nsAString_Finish(&val_str
);
1162 ERR("GetType failed: %08x\n", nsres
);
1165 nsres
= nsIDOMHTMLScriptElement_QueryInterface(nsscript
, &IID_nsIDOMHTMLElement
, (void**)&nselem
);
1166 assert(nsres
== NS_OK
);
1168 nsres
= get_elem_attr_value(nselem
, languageW
, &val_str
, &language
);
1169 nsIDOMHTMLElement_Release(nselem
);
1170 if(NS_SUCCEEDED(nsres
)) {
1172 ret
= get_guid_from_language(language
, guid
);
1174 *guid
= get_default_script_guid(window
);
1177 nsAString_Finish(&val_str
);
1183 static ScriptHost
*get_script_host(HTMLInnerWindow
*window
, const GUID
*guid
)
1187 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
1188 if(IsEqualGUID(guid
, &iter
->guid
))
1192 return create_script_host(window
, guid
);
1195 static ScriptHost
*get_elem_script_host(HTMLInnerWindow
*window
, HTMLScriptElement
*script_elem
)
1199 if(!get_script_guid(window
, script_elem
->nsscript
, &guid
)) {
1200 WARN("Could not find script GUID\n");
1204 if(IsEqualGUID(&CLSID_JScript
, &guid
)
1205 && (!window
->base
.outer_window
|| window
->base
.outer_window
->scriptmode
!= SCRIPTMODE_ACTIVESCRIPT
)) {
1206 TRACE("Ignoring JScript\n");
1210 return get_script_host(window
, &guid
);
1213 void doc_insert_script(HTMLInnerWindow
*window
, HTMLScriptElement
*script_elem
)
1215 ScriptHost
*script_host
;
1217 script_host
= get_elem_script_host(window
, script_elem
);
1221 if(script_host
->parse
)
1222 parse_script_elem(script_host
, script_elem
);
1225 IDispatch
*script_parse_event(HTMLInnerWindow
*window
, LPCWSTR text
)
1227 ScriptHost
*script_host
;
1233 static const WCHAR delimiterW
[] = {'\"',0};
1235 TRACE("%s\n", debugstr_w(text
));
1237 for(ptr
= text
; isalnumW(*ptr
); ptr
++);
1242 language
= heap_alloc((ptr
-text
+1)*sizeof(WCHAR
));
1246 memcpy(language
, text
, (ptr
-text
)*sizeof(WCHAR
));
1247 language
[ptr
-text
] = 0;
1249 b
= get_guid_from_language(language
, &guid
);
1251 heap_free(language
);
1254 WARN("Could not find language\n");
1261 guid
= get_default_script_guid(window
);
1264 if(IsEqualGUID(&CLSID_JScript
, &guid
)
1265 && (!window
->base
.outer_window
|| window
->base
.outer_window
->scriptmode
!= SCRIPTMODE_ACTIVESCRIPT
)) {
1266 TRACE("Ignoring JScript\n");
1270 script_host
= get_script_host(window
, &guid
);
1271 if(!script_host
|| !script_host
->parse_proc
)
1274 hres
= IActiveScriptParseProcedure2_ParseProcedureText(script_host
->parse_proc
, ptr
, NULL
, emptyW
,
1275 NULL
, NULL
, delimiterW
, 0 /* FIXME */, 0,
1276 SCRIPTPROC_HOSTMANAGESSOURCE
|SCRIPTPROC_IMPLICIT_THIS
|SCRIPTPROC_IMPLICIT_PARENTS
, &disp
);
1278 WARN("ParseProcedureText failed: %08x\n", hres
);
1282 TRACE("ret %p\n", disp
);
1286 HRESULT
exec_script(HTMLInnerWindow
*window
, const WCHAR
*code
, const WCHAR
*lang
, VARIANT
*ret
)
1288 ScriptHost
*script_host
;
1293 static const WCHAR delimW
[] = {'"',0};
1295 if(!get_guid_from_language(lang
, &guid
)) {
1296 WARN("Could not find script GUID\n");
1297 return CO_E_CLASSSTRING
;
1300 script_host
= get_script_host(window
, &guid
);
1302 FIXME("No script host\n");
1306 if(!script_host
->parse
) {
1307 FIXME("script_host->parse == NULL\n");
1311 memset(&ei
, 0, sizeof(ei
));
1313 hres
= IActiveScriptParse_ParseScriptText(script_host
->parse
, code
, NULL
, NULL
, delimW
, 0, 0, SCRIPTTEXT_ISVISIBLE
, ret
, &ei
);
1317 WARN("<<< %08x\n", hres
);
1322 IDispatch
*get_script_disp(ScriptHost
*script_host
)
1327 if(!script_host
->script
)
1330 hres
= IActiveScript_GetScriptDispatch(script_host
->script
, windowW
, &disp
);
1337 static EventTarget
*find_event_target(HTMLDocumentNode
*doc
, HTMLScriptElement
*script_elem
)
1339 EventTarget
*event_target
= NULL
;
1340 const PRUnichar
*target_id
;
1341 nsAString target_id_str
;
1345 nsAString_Init(&target_id_str
, NULL
);
1346 nsres
= nsIDOMHTMLScriptElement_GetHtmlFor(script_elem
->nsscript
, &target_id_str
);
1347 if(NS_FAILED(nsres
)) {
1348 ERR("GetScriptFor failed: %08x\n", nsres
);
1349 nsAString_Finish(&target_id_str
);
1353 nsAString_GetData(&target_id_str
, &target_id
);
1355 FIXME("Empty for attribute\n");
1356 }else if(!strcmpW(target_id
, documentW
)) {
1357 event_target
= &doc
->node
.event_target
;
1358 htmldoc_addref(&doc
->basedoc
);
1359 }else if(!strcmpW(target_id
, windowW
)) {
1361 event_target
= &doc
->window
->event_target
;
1362 IDispatchEx_AddRef(&event_target
->dispex
.IDispatchEx_iface
);
1365 HTMLElement
*target_elem
;
1367 hres
= get_doc_elem_by_id(doc
, target_id
, &target_elem
);
1368 if(SUCCEEDED(hres
) && target_elem
) {
1369 event_target
= &target_elem
->node
.event_target
;
1373 nsAString_Finish(&target_id_str
);
1374 return event_target
;
1377 static BOOL
parse_event_str(WCHAR
*event
, const WCHAR
**args
)
1381 TRACE("%s\n", debugstr_w(event
));
1383 for(ptr
= event
; isalnumW(*ptr
); ptr
++);
1394 while(isalnumW(*ptr
) || isspaceW(*ptr
) || *ptr
== ',')
1404 static IDispatch
*parse_event_elem(HTMLDocumentNode
*doc
, HTMLScriptElement
*script_elem
, WCHAR
**ret_event
)
1406 ScriptHost
*script_host
;
1407 WCHAR
*event
= NULL
;
1414 if(script_elem
->parsed
)
1417 script_host
= get_elem_script_host(doc
->window
, script_elem
);
1418 if(!script_host
|| !script_host
->parse_proc
)
1421 nsAString_Init(&nsstr
, NULL
);
1422 nsres
= nsIDOMHTMLScriptElement_GetEvent(script_elem
->nsscript
, &nsstr
);
1423 if(NS_SUCCEEDED(nsres
)) {
1424 const PRUnichar
*event_val
;
1426 nsAString_GetData(&nsstr
, &event_val
);
1427 event
= heap_strdupW(event_val
);
1429 nsAString_Finish(&nsstr
);
1433 if(!parse_event_str(event
, &args
)) {
1434 WARN("parsing %s failed\n", debugstr_w(event
));
1439 nsAString_Init(&nsstr
, NULL
);
1440 nsres
= nsIDOMHTMLScriptElement_GetText(script_elem
->nsscript
, &nsstr
);
1441 if(NS_SUCCEEDED(nsres
)) {
1442 const PRUnichar
*text
;
1444 nsAString_GetData(&nsstr
, &text
);
1445 hres
= IActiveScriptParseProcedure2_ParseProcedureText(script_host
->parse_proc
, text
, args
,
1446 emptyW
, NULL
, NULL
, script_endW
, 0, 0,
1447 SCRIPTPROC_HOSTMANAGESSOURCE
|SCRIPTPROC_IMPLICIT_THIS
|SCRIPTPROC_IMPLICIT_PARENTS
, &disp
);
1451 ERR("GetText failed: %08x\n", nsres
);
1454 nsAString_Finish(&nsstr
);
1464 void bind_event_scripts(HTMLDocumentNode
*doc
)
1466 HTMLPluginContainer
*plugin_container
;
1467 nsIDOMHTMLScriptElement
*nsscript
;
1468 HTMLScriptElement
*script_elem
;
1469 EventTarget
*event_target
;
1470 nsIDOMNodeList
*node_list
;
1471 nsIDOMNode
*script_node
;
1472 nsAString selector_str
;
1473 IDispatch
*event_disp
;
1479 static const PRUnichar selectorW
[] = {'s','c','r','i','p','t','[','e','v','e','n','t',']',0};
1486 nsAString_InitDepend(&selector_str
, selectorW
);
1487 nsres
= nsIDOMHTMLDocument_QuerySelectorAll(doc
->nsdoc
, &selector_str
, &node_list
);
1488 nsAString_Finish(&selector_str
);
1489 if(NS_FAILED(nsres
)) {
1490 ERR("QuerySelectorAll failed: %08x\n", nsres
);
1497 nsres
= nsIDOMNodeList_GetLength(node_list
, &length
);
1498 assert(nsres
== NS_OK
);
1500 for(i
=0; i
< length
; i
++) {
1501 nsres
= nsIDOMNodeList_Item(node_list
, i
, &script_node
);
1502 if(NS_FAILED(nsres
) || !script_node
) {
1503 ERR("Item(%d) failed: %08x\n", i
, nsres
);
1507 nsres
= nsIDOMNode_QueryInterface(script_node
, &IID_nsIDOMHTMLScriptElement
, (void**)&nsscript
);
1508 assert(nsres
== NS_OK
);
1509 nsIDOMNode_Release(script_node
);
1511 hres
= script_elem_from_nsscript(doc
, nsscript
, &script_elem
);
1515 event_disp
= parse_event_elem(doc
, script_elem
, &event
);
1517 event_target
= find_event_target(doc
, script_elem
);
1519 hres
= IDispatchEx_QueryInterface(&event_target
->dispex
.IDispatchEx_iface
, &IID_HTMLPluginContainer
,
1520 (void**)&plugin_container
);
1522 bind_activex_event(doc
, plugin_container
, event
, event_disp
);
1524 bind_target_event(doc
, event_target
, event
, event_disp
);
1526 IDispatchEx_Release(&event_target
->dispex
.IDispatchEx_iface
);
1527 if(plugin_container
)
1528 node_release(&plugin_container
->element
.node
);
1532 IDispatch_Release(event_disp
);
1535 IHTMLScriptElement_Release(&script_elem
->IHTMLScriptElement_iface
);
1538 nsIDOMNodeList_Release(node_list
);
1541 BOOL
find_global_prop(HTMLInnerWindow
*window
, BSTR name
, DWORD flags
, ScriptHost
**ret_host
, DISPID
*ret_id
)
1543 IDispatchEx
*dispex
;
1548 LIST_FOR_EACH_ENTRY(iter
, &window
->script_hosts
, ScriptHost
, entry
) {
1549 disp
= get_script_disp(iter
);
1553 hres
= IDispatch_QueryInterface(disp
, &IID_IDispatchEx
, (void**)&dispex
);
1554 if(SUCCEEDED(hres
)) {
1555 hres
= IDispatchEx_GetDispID(dispex
, name
, flags
& (~fdexNameEnsure
), ret_id
);
1556 IDispatchEx_Release(dispex
);
1558 FIXME("No IDispatchEx\n");
1562 IDispatch_Release(disp
);
1563 if(SUCCEEDED(hres
)) {
1572 static BOOL
is_jscript_available(void)
1574 static BOOL available
, checked
;
1578 HRESULT hres
= CoGetClassObject(&CLSID_JScript
, CLSCTX_INPROC_SERVER
, NULL
, &IID_IUnknown
, (void**)&unk
);
1580 if(SUCCEEDED(hres
)) {
1582 IUnknown_Release(unk
);
1592 void set_script_mode(HTMLOuterWindow
*window
, SCRIPTMODE mode
)
1594 nsIWebBrowserSetup
*setup
;
1597 if(mode
== SCRIPTMODE_ACTIVESCRIPT
&& !is_jscript_available()) {
1598 TRACE("jscript.dll not available\n");
1599 window
->scriptmode
= SCRIPTMODE_GECKO
;
1603 window
->scriptmode
= mode
;
1605 if(!window
->doc_obj
->nscontainer
|| !window
->doc_obj
->nscontainer
->webbrowser
)
1608 nsres
= nsIWebBrowser_QueryInterface(window
->doc_obj
->nscontainer
->webbrowser
,
1609 &IID_nsIWebBrowserSetup
, (void**)&setup
);
1610 if(NS_SUCCEEDED(nsres
)) {
1611 nsres
= nsIWebBrowserSetup_SetProperty(setup
, SETUP_ALLOW_JAVASCRIPT
,
1612 window
->scriptmode
== SCRIPTMODE_GECKO
);
1614 if(NS_SUCCEEDED(nsres
))
1615 nsres
= nsIWebBrowserSetup_SetProperty(setup
, SETUP_DISABLE_NOSCRIPT
, TRUE
);
1617 nsIWebBrowserSetup_Release(setup
);
1620 if(NS_FAILED(nsres
))
1621 ERR("JavaScript setup failed: %08x\n", nsres
);
1624 void release_script_hosts(HTMLInnerWindow
*window
)
1626 script_queue_entry_t
*queue_iter
;
1629 while(!list_empty(&window
->script_queue
)) {
1630 queue_iter
= LIST_ENTRY(list_head(&window
->script_queue
), script_queue_entry_t
, entry
);
1632 list_remove(&queue_iter
->entry
);
1633 IHTMLScriptElement_Release(&queue_iter
->script
->IHTMLScriptElement_iface
);
1634 heap_free(queue_iter
);
1637 while(!list_empty(&window
->script_hosts
)) {
1638 iter
= LIST_ENTRY(list_head(&window
->script_hosts
), ScriptHost
, entry
);
1640 release_script_engine(iter
);
1641 list_remove(&iter
->entry
);
1642 iter
->window
= NULL
;
1643 IActiveScriptSite_Release(&iter
->IActiveScriptSite_iface
);