2 * Copyright 2007-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
30 #include "wine/debug.h"
31 #include "wine/unicode.h"
33 #include "mshtml_private.h"
34 #include "htmlevent.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml
);
38 #define NSEVENTLIST_THIS(iface) DEFINE_THIS(nsEventListener, DOMEventListener, iface)
40 static nsresult NSAPI
nsDOMEventListener_QueryInterface(nsIDOMEventListener
*iface
,
41 nsIIDRef riid
, nsQIResult result
)
43 nsEventListener
*This
= NSEVENTLIST_THIS(iface
);
47 if(IsEqualGUID(&IID_nsISupports
, riid
)) {
48 TRACE("(%p)->(IID_nsISupports, %p)\n", This
, result
);
49 *result
= NSEVENTLIST(This
);
50 }else if(IsEqualGUID(&IID_nsIDOMEventListener
, riid
)) {
51 TRACE("(%p)->(IID_nsIDOMEventListener %p)\n", This
, result
);
52 *result
= NSEVENTLIST(This
);
56 nsIWebBrowserChrome_AddRef(NSEVENTLIST(This
));
60 TRACE("(%p)->(%s %p)\n", This
, debugstr_guid(riid
), result
);
61 return NS_NOINTERFACE
;
64 static nsrefcnt NSAPI
nsDOMEventListener_AddRef(nsIDOMEventListener
*iface
)
66 NSContainer
*This
= NSEVENTLIST_THIS(iface
)->This
;
67 return nsIWebBrowserChrome_AddRef(NSWBCHROME(This
));
70 static nsrefcnt NSAPI
nsDOMEventListener_Release(nsIDOMEventListener
*iface
)
72 NSContainer
*This
= NSEVENTLIST_THIS(iface
)->This
;
73 return nsIWebBrowserChrome_Release(NSWBCHROME(This
));
76 static BOOL
is_doc_child_focus(NSContainer
*This
)
83 for(hwnd
= GetFocus(); hwnd
&& hwnd
!= This
->doc
->hwnd
; hwnd
= GetParent(hwnd
));
85 return hwnd
== This
->doc
->hwnd
;
88 static nsresult NSAPI
handle_blur(nsIDOMEventListener
*iface
, nsIDOMEvent
*event
)
90 NSContainer
*This
= NSEVENTLIST_THIS(iface
)->This
;
92 TRACE("(%p)\n", This
);
94 if(!This
->reset_focus
&& This
->doc
&& This
->doc
->focus
&& !is_doc_child_focus(This
)) {
95 This
->doc
->focus
= FALSE
;
96 notif_focus(This
->doc
);
102 static nsresult NSAPI
handle_focus(nsIDOMEventListener
*iface
, nsIDOMEvent
*event
)
104 NSContainer
*This
= NSEVENTLIST_THIS(iface
)->This
;
106 TRACE("(%p)\n", This
);
108 if(!This
->reset_focus
&& This
->doc
&& !This
->doc
->focus
) {
109 This
->doc
->focus
= TRUE
;
110 notif_focus(This
->doc
);
116 static nsresult NSAPI
handle_keypress(nsIDOMEventListener
*iface
,
119 NSContainer
*This
= NSEVENTLIST_THIS(iface
)->This
;
121 TRACE("(%p)->(%p)\n", This
, event
);
123 update_doc(This
->doc
, UPDATE_UI
);
124 if(This
->doc
->usermode
== EDITMODE
)
125 handle_edit_event(This
->doc
, event
);
130 static nsresult NSAPI
handle_load(nsIDOMEventListener
*iface
, nsIDOMEvent
*event
)
132 NSContainer
*This
= NSEVENTLIST_THIS(iface
)->This
;
133 nsIDOMHTMLElement
*nsbody
= NULL
;
136 TRACE("(%p)\n", This
);
141 update_nsdocument(This
->doc
);
142 connect_scripts(This
->doc
);
144 if(This
->editor_controller
) {
145 nsIController_Release(This
->editor_controller
);
146 This
->editor_controller
= NULL
;
149 if(This
->doc
->usermode
== EDITMODE
)
150 handle_edit_load(This
->doc
);
152 task
= heap_alloc(sizeof(task_t
));
154 task
->doc
= This
->doc
;
155 task
->task_id
= TASK_PARSECOMPLETE
;
159 * This should be done in the worker thread that parses HTML,
160 * but we don't have such thread (Gecko parses HTML for us).
164 if(!This
->doc
->nsdoc
) {
166 return NS_ERROR_FAILURE
;
169 nsIDOMHTMLDocument_GetBody(This
->doc
->nsdoc
, &nsbody
);
171 fire_event(This
->doc
, EVENTID_LOAD
, (nsIDOMNode
*)nsbody
);
172 nsIDOMHTMLElement_Release(nsbody
);
178 #define IE_MAJOR_VERSION 7
179 #define IE_MINOR_VERSION 0
181 static BOOL
handle_insert_comment(HTMLDocument
*doc
, const PRUnichar
*comment
)
184 int majorv
= 0, minorv
= 0;
185 const PRUnichar
*ptr
, *end
;
198 static const PRUnichar endifW
[] = {'<','!','[','e','n','d','i','f',']'};
200 if(comment
[0] != '[' || comment
[1] != 'i' || comment
[2] != 'f')
204 while(isspaceW(*ptr
))
207 if(ptr
[0] == 'l' && ptr
[1] == 't') {
215 }else if(ptr
[0] == 'g' && ptr
[1] == 't') {
225 if(!isspaceW(*ptr
++))
227 while(isspaceW(*ptr
))
230 if(ptr
[0] != 'I' || ptr
[1] != 'E')
234 if(!isspaceW(*ptr
++))
236 while(isspaceW(*ptr
))
241 while(isdigitW(*ptr
))
242 majorv
= majorv
*10 + (*ptr
++ - '0');
247 while(isdigitW(*ptr
))
248 minorv
= minorv
*10 + (*ptr
++ - '0');
251 while(isspaceW(*ptr
))
253 if(ptr
[0] != ']' || ptr
[1] != '>')
258 if(len
< sizeof(endifW
)/sizeof(WCHAR
))
261 end
= ptr
+ len
-sizeof(endifW
)/sizeof(WCHAR
);
262 if(memcmp(end
, endifW
, sizeof(endifW
)))
267 if(majorv
== IE_MAJOR_VERSION
&& minorv
== IE_MINOR_VERSION
)
271 if(majorv
> IE_MAJOR_VERSION
)
273 if(majorv
== IE_MAJOR_VERSION
&& minorv
> IE_MINOR_VERSION
)
277 if(majorv
> IE_MAJOR_VERSION
)
279 if(majorv
== IE_MAJOR_VERSION
&& minorv
>= IE_MINOR_VERSION
)
283 if(majorv
< IE_MAJOR_VERSION
)
285 if(majorv
== IE_MAJOR_VERSION
&& minorv
< IE_MINOR_VERSION
)
289 if(majorv
< IE_MAJOR_VERSION
)
291 if(majorv
== IE_MAJOR_VERSION
&& minorv
<= IE_MINOR_VERSION
)
296 buf
= heap_alloc((end
-ptr
+1)*sizeof(WCHAR
));
300 memcpy(buf
, ptr
, (end
-ptr
)*sizeof(WCHAR
));
302 nsAString_Init(&nsstr
, buf
);
305 /* FIXME: Find better way to insert HTML to document. */
306 nsres
= nsIDOMHTMLDocument_Write(doc
->nsdoc
, &nsstr
);
307 nsAString_Finish(&nsstr
);
308 if(NS_FAILED(nsres
)) {
309 ERR("Write failed: %08x\n", nsres
);
316 static nsresult NSAPI
handle_node_insert(nsIDOMEventListener
*iface
, nsIDOMEvent
*event
)
318 NSContainer
*This
= NSEVENTLIST_THIS(iface
)->This
;
319 nsIDOMEventTarget
*target
;
320 nsIDOMComment
*nscomment
;
324 TRACE("(%p %p)\n", This
, event
);
326 nsres
= nsIDOMEvent_GetTarget(event
, &target
);
327 if(NS_FAILED(nsres
)) {
328 ERR("GetTarget failed: %08x\n", nsres
);
332 nsres
= nsIDOMEventTarget_QueryInterface(target
, &IID_nsIDOMElement
, (void**)&elem
);
333 if(NS_SUCCEEDED(nsres
)) {
334 nsIDOMHTMLScriptElement
*script
;
336 nsres
= nsIDOMElement_QueryInterface(elem
, &IID_nsIDOMHTMLScriptElement
, (void**)&script
);
337 if(NS_SUCCEEDED(nsres
)) {
338 doc_insert_script(This
->doc
, script
);
339 nsIDOMHTMLScriptElement_Release(script
);
342 check_event_attr(This
->doc
, elem
);
344 nsIDOMEventTarget_Release(target
);
345 nsIDOMNode_Release(elem
);
349 nsres
= nsIDOMEventTarget_QueryInterface(target
, &IID_nsIDOMComment
, (void**)&nscomment
);
350 if(NS_SUCCEEDED(nsres
)) {
351 nsAString comment_str
;
352 BOOL remove_comment
= FALSE
;
354 nsAString_Init(&comment_str
, NULL
);
355 nsres
= nsIDOMComment_GetData(nscomment
, &comment_str
);
356 if(NS_SUCCEEDED(nsres
)) {
357 const PRUnichar
*comment
;
359 nsAString_GetData(&comment_str
, &comment
);
360 remove_comment
= handle_insert_comment(This
->doc
, comment
);
363 nsAString_Finish(&comment_str
);
366 nsIDOMNode
*nsparent
, *tmp
;
368 nsIDOMComment_GetParentNode(nscomment
, &nsparent
);
369 nsIDOMNode_RemoveChild(nsparent
, (nsIDOMNode
*)nscomment
, &tmp
);
370 nsIDOMNode_Release(nsparent
);
371 nsIDOMNode_Release(tmp
);
374 nsIDOMComment_Release(nscomment
);
380 static nsresult NSAPI
handle_htmlevent(nsIDOMEventListener
*iface
, nsIDOMEvent
*event
)
382 NSContainer
*This
= NSEVENTLIST_THIS(iface
)->This
;
383 const PRUnichar
*type
;
384 nsIDOMEventTarget
*event_target
;
390 nsAString_Init(&type_str
, NULL
);
391 nsIDOMEvent_GetType(event
, &type_str
);
392 nsAString_GetData(&type_str
, &type
);
393 eid
= str_to_eid(type
);
394 nsAString_Finish(&type_str
);
396 nsres
= nsIDOMEvent_GetTarget(event
, &event_target
);
397 if(NS_FAILED(nsres
) || !event_target
) {
398 ERR("GetEventTarget failed: %08x\n", nsres
);
402 nsres
= nsIDOMEventTarget_QueryInterface(event_target
, &IID_nsIDOMNode
, (void**)&nsnode
);
403 nsIDOMEventTarget_Release(event_target
);
404 if(NS_FAILED(nsres
)) {
405 ERR("Could not get nsIDOMNode: %08x\n", nsres
);
409 fire_event(This
->doc
, eid
, nsnode
);
411 nsIDOMNode_Release(nsnode
);
416 #undef NSEVENTLIST_THIS
418 #define EVENTLISTENER_VTBL(handler) \
420 nsDOMEventListener_QueryInterface, \
421 nsDOMEventListener_AddRef, \
422 nsDOMEventListener_Release, \
426 static const nsIDOMEventListenerVtbl blur_vtbl
= EVENTLISTENER_VTBL(handle_blur
);
427 static const nsIDOMEventListenerVtbl focus_vtbl
= EVENTLISTENER_VTBL(handle_focus
);
428 static const nsIDOMEventListenerVtbl keypress_vtbl
= EVENTLISTENER_VTBL(handle_keypress
);
429 static const nsIDOMEventListenerVtbl load_vtbl
= EVENTLISTENER_VTBL(handle_load
);
430 static const nsIDOMEventListenerVtbl node_insert_vtbl
= EVENTLISTENER_VTBL(handle_node_insert
);
431 static const nsIDOMEventListenerVtbl htmlevent_vtbl
= EVENTLISTENER_VTBL(handle_htmlevent
);
433 static void init_event(nsIDOMEventTarget
*target
, const PRUnichar
*type
,
434 nsIDOMEventListener
*listener
, BOOL capture
)
439 nsAString_Init(&type_str
, type
);
440 nsres
= nsIDOMEventTarget_AddEventListener(target
, &type_str
, listener
, capture
);
441 nsAString_Finish(&type_str
);
443 ERR("AddEventTarget failed: %08x\n", nsres
);
447 static void init_listener(nsEventListener
*This
, NSContainer
*container
,
448 const nsIDOMEventListenerVtbl
*vtbl
)
450 This
->lpDOMEventListenerVtbl
= vtbl
;
451 This
->This
= container
;
454 void add_nsevent_listener(NSContainer
*container
, LPCWSTR type
)
456 nsIDOMWindow
*dom_window
;
457 nsIDOMEventTarget
*target
;
460 nsres
= nsIWebBrowser_GetContentDOMWindow(container
->webbrowser
, &dom_window
);
461 if(NS_FAILED(nsres
)) {
462 ERR("GetContentDOMWindow failed: %08x\n", nsres
);
466 nsres
= nsIDOMWindow_QueryInterface(dom_window
, &IID_nsIDOMEventTarget
, (void**)&target
);
467 nsIDOMWindow_Release(dom_window
);
468 if(NS_FAILED(nsres
)) {
469 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres
);
473 init_event(target
, type
, NSEVENTLIST(&container
->htmlevent_listener
), TRUE
);
474 nsIDOMEventTarget_Release(target
);
477 void init_nsevents(NSContainer
*This
)
479 nsIDOMWindow
*dom_window
;
480 nsIDOMEventTarget
*target
;
483 static const PRUnichar wsz_blur
[] = {'b','l','u','r',0};
484 static const PRUnichar wsz_focus
[] = {'f','o','c','u','s',0};
485 static const PRUnichar wsz_keypress
[] = {'k','e','y','p','r','e','s','s',0};
486 static const PRUnichar wsz_load
[] = {'l','o','a','d',0};
487 static const PRUnichar DOMNodeInsertedW
[] =
488 {'D','O','M','N','o','d','e','I','n','s','e','r','t','e','d',0};
490 init_listener(&This
->blur_listener
, This
, &blur_vtbl
);
491 init_listener(&This
->focus_listener
, This
, &focus_vtbl
);
492 init_listener(&This
->keypress_listener
, This
, &keypress_vtbl
);
493 init_listener(&This
->load_listener
, This
, &load_vtbl
);
494 init_listener(&This
->node_insert_listener
, This
, &node_insert_vtbl
);
495 init_listener(&This
->htmlevent_listener
, This
, &htmlevent_vtbl
);
497 nsres
= nsIWebBrowser_GetContentDOMWindow(This
->webbrowser
, &dom_window
);
498 if(NS_FAILED(nsres
)) {
499 ERR("GetContentDOMWindow failed: %08x\n", nsres
);
503 nsres
= nsIDOMWindow_QueryInterface(dom_window
, &IID_nsIDOMEventTarget
, (void**)&target
);
504 nsIDOMWindow_Release(dom_window
);
505 if(NS_FAILED(nsres
)) {
506 ERR("Could not get nsIDOMEventTarget interface: %08x\n", nsres
);
510 init_event(target
, wsz_blur
, NSEVENTLIST(&This
->blur_listener
), TRUE
);
511 init_event(target
, wsz_focus
, NSEVENTLIST(&This
->focus_listener
), TRUE
);
512 init_event(target
, wsz_keypress
, NSEVENTLIST(&This
->keypress_listener
), FALSE
);
513 init_event(target
, wsz_load
, NSEVENTLIST(&This
->load_listener
), TRUE
);
514 init_event(target
, DOMNodeInsertedW
,NSEVENTLIST(&This
->node_insert_listener
),TRUE
);
516 nsIDOMEventTarget_Release(target
);