When a native browser event is bubbling up the DOM, make sure that the correct isDefa...
[jquery.git] / test / unit / event.js
1 module("event");
2
3 test("null or undefined handler", function() {
4         expect(2);
5   // Supports Fixes bug #7229
6   try {
7   
8     jQuery("#firstp").click(null);
9   
10     ok(true, "Passing a null handler will not throw an exception");
11
12   } catch (e) {}  
13
14   try {
15   
16     jQuery("#firstp").click(undefined);
17   
18     ok(true, "Passing an undefined handler will not throw an exception");
19
20   } catch (e) {}  
21 });
22
23 test("bind(), with data", function() {
24         expect(3);
25         var handler = function(event) {
26                 ok( event.data, "bind() with data, check passed data exists" );
27                 equals( event.data.foo, "bar", "bind() with data, Check value of passed data" );
28         };
29         jQuery("#firstp").bind("click", {foo: "bar"}, handler).click().unbind("click", handler);
30
31         ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
32 });
33
34 test("click(), with data", function() {
35         expect(3);
36         var handler = function(event) {
37                 ok( event.data, "bind() with data, check passed data exists" );
38                 equals( event.data.foo, "bar", "bind() with data, Check value of passed data" );
39         };
40         jQuery("#firstp").click({foo: "bar"}, handler).click().unbind("click", handler);
41
42         ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
43 });
44
45 test("bind(), with data, trigger with data", function() {
46         expect(4);
47         var handler = function(event, data) {
48                 ok( event.data, "check passed data exists" );
49                 equals( event.data.foo, "bar", "Check value of passed data" );
50                 ok( data, "Check trigger data" );
51                 equals( data.bar, "foo", "Check value of trigger data" );
52         };
53         jQuery("#firstp").bind("click", {foo: "bar"}, handler).trigger("click", [{bar: "foo"}]).unbind("click", handler);
54 });
55
56 test("bind(), multiple events at once", function() {
57         expect(2);
58         var clickCounter = 0,
59                 mouseoverCounter = 0;
60         var handler = function(event) {
61                 if (event.type == "click")
62                         clickCounter += 1;
63                 else if (event.type == "mouseover")
64                         mouseoverCounter += 1;
65         };
66         jQuery("#firstp").bind("click mouseover", handler).trigger("click").trigger("mouseover");
67         equals( clickCounter, 1, "bind() with multiple events at once" );
68         equals( mouseoverCounter, 1, "bind() with multiple events at once" );
69 });
70
71 test("bind(), multiple events at once and namespaces", function() {
72         expect(7);
73
74         var cur, obj = {};
75
76         var div = jQuery("<div/>").bind("focusin.a", function(e) {
77                 equals( e.type, cur, "Verify right single event was fired." );
78         });
79
80         cur = "focusin";
81         div.trigger("focusin.a");
82
83         div = jQuery("<div/>").bind("click mouseover", obj, function(e) {
84                 equals( e.type, cur, "Verify right multi event was fired." );
85                 equals( e.data, obj, "Make sure the data came in correctly." );
86         });
87
88         cur = "click";
89         div.trigger("click");
90
91         cur = "mouseover";
92         div.trigger("mouseover");
93
94         div = jQuery("<div/>").bind("focusin.a focusout.b", function(e) {
95                 equals( e.type, cur, "Verify right multi event was fired." );
96         });
97
98         cur = "focusin";
99         div.trigger("focusin.a");
100
101         cur = "focusout";
102         div.trigger("focusout.b");
103 });
104
105 test("bind(), namespace with special add", function() {
106         expect(24);
107
108         var div = jQuery("<div/>").bind("test", function(e) {
109                 ok( true, "Test event fired." );
110         });
111
112         var i = 0;
113
114         jQuery.event.special.test = {
115                 _default: function(e) {
116                         equals( this, document, "Make sure we're at the top of the chain." );
117                         equals( e.type, "test", "And that we're still dealing with a test event." );
118                         equals( e.target, div[0], "And that the target is correct." );
119                 },
120                 setup: function(){},
121                 teardown: function(){
122                         ok(true, "Teardown called.");
123                 },
124                 add: function( handleObj ) {
125                         var handler = handleObj.handler;
126                         handleObj.handler = function(e) {
127                                 e.xyz = ++i;
128                                 handler.apply( this, arguments );
129                         };
130                 },
131                 remove: function() {
132                         ok(true, "Remove called.");
133                 }
134         };
135
136         div.bind("test.a", {x: 1}, function(e) {
137                 ok( !!e.xyz, "Make sure that the data is getting passed through." );
138                 equals( e.data.x, 1, "Make sure data is attached properly." );
139         });
140
141         div.bind("test.b", {x: 2}, function(e) {
142                 ok( !!e.xyz, "Make sure that the data is getting passed through." );
143                 equals( e.data.x, 2, "Make sure data is attached properly." );
144         });
145
146         // Should trigger 5
147         div.trigger("test");
148
149         // Should trigger 2
150         div.trigger("test.a");
151
152         // Should trigger 2
153         div.trigger("test.b");
154
155         // Should trigger 4
156         div.unbind("test");
157
158         div = jQuery("<div/>").bind("test", function(e) {
159                 ok( true, "Test event fired." );
160         });
161
162         // Should trigger 2
163         div.appendTo("#main").remove();
164
165         delete jQuery.event.special.test;
166 });
167
168 test("bind(), no data", function() {
169         expect(1);
170         var handler = function(event) {
171                 ok ( !event.data, "Check that no data is added to the event object" );
172         };
173         jQuery("#firstp").bind("click", handler).trigger("click");
174 });
175
176 test("bind/one/unbind(Object)", function(){
177         expect(6);
178         
179         var clickCounter = 0, mouseoverCounter = 0;
180         function handler(event) {
181                 if (event.type == "click")
182                         clickCounter++;
183                 else if (event.type == "mouseover")
184                         mouseoverCounter++;
185         };
186         
187         function handlerWithData(event) {
188                 if (event.type == "click")
189                         clickCounter += event.data;
190                 else if (event.type == "mouseover")
191                         mouseoverCounter += event.data;
192         };
193         
194         function trigger(){
195                 $elem.trigger("click").trigger("mouseover");
196         }
197         
198         var $elem = jQuery("#firstp")
199                 // Regular bind
200                 .bind({
201                         click:handler,
202                         mouseover:handler
203                 })
204                 // Bind with data
205                 .one({
206                         click:handlerWithData,
207                         mouseover:handlerWithData
208                 }, 2 );
209         
210         trigger();
211         
212         equals( clickCounter, 3, "bind(Object)" );
213         equals( mouseoverCounter, 3, "bind(Object)" );
214         
215         trigger();
216         equals( clickCounter, 4, "bind(Object)" );
217         equals( mouseoverCounter, 4, "bind(Object)" );
218         
219         jQuery("#firstp").unbind({
220                 click:handler,
221                 mouseover:handler
222         });
223
224         trigger();
225         equals( clickCounter, 4, "bind(Object)" );
226         equals( mouseoverCounter, 4, "bind(Object)" );
227 });
228
229 test("live/die(Object), delegate/undelegate(String, Object)", function() {
230         expect(6);
231         
232         var clickCounter = 0, mouseoverCounter = 0,
233                 $p = jQuery("#firstp"), $a = $p.find("a:first");
234         
235         var events = {
236                 click: function( event ) {
237                         clickCounter += ( event.data || 1 );
238                 },
239                 mouseover: function( event ) {
240                         mouseoverCounter += ( event.data || 1 );
241                 }
242         };
243         
244         function trigger() {
245                 $a.trigger("click").trigger("mouseover");
246         }
247         
248         $a.live( events );
249         $p.delegate( "a", events, 2 );
250         
251         trigger();
252         equals( clickCounter, 3, "live/delegate" );
253         equals( mouseoverCounter, 3, "live/delegate" );
254         
255         $p.undelegate( "a", events );
256         
257         trigger();
258         equals( clickCounter, 4, "undelegate" );
259         equals( mouseoverCounter, 4, "undelegate" );
260         
261         $a.die( events );
262         
263         trigger();
264         equals( clickCounter, 4, "die" );
265         equals( mouseoverCounter, 4, "die" );
266 });
267
268 test("live/delegate immediate propagation", function() {
269         expect(2);
270         
271         var $p = jQuery("#firstp"), $a = $p.find("a:first"), lastClick;
272         
273         lastClick = "";
274         $a.live( "click", function(e) { 
275                 lastClick = "click1"; 
276                 e.stopImmediatePropagation();
277         });
278         $a.live( "click", function(e) {
279                 lastClick = "click2";
280         });
281         $a.trigger( "click" );
282         equals( lastClick, "click1", "live stopImmediatePropagation" );
283         $a.die( "click" );
284         
285         lastClick = "";
286         $p.delegate( "a", "click", function(e) { 
287                 lastClick = "click1"; 
288                 e.stopImmediatePropagation();
289         });
290         $p.delegate( "a", "click", function(e) {
291                 lastClick = "click2";
292         });
293         $a.trigger( "click" );
294         equals( lastClick, "click1", "delegate stopImmediatePropagation" );
295         $p.undelegate( "click" );
296 });
297
298 test("bind/delegate bubbling, isDefaultPrevented", function() {
299         expect(2);
300         var $anchor2 = jQuery( "#anchor2" ),
301                 $main = jQuery( "#main" ),
302                 fakeClick = function($jq) {
303                         // Prefer a native click so we don't get jQuery simulated bubbling
304                         if ( $jq[0].click ) {
305                                 $jq[0].click(); // IE
306                         }
307                         else if ( document.createEvent ) {
308                                 var e = document.createEvent( 'MouseEvents' );
309                                 e.initEvent( "click", true, true ); 
310                                 $jq[0].dispatchEvent(e);
311                         }
312                         else {
313                                 $jq.click();
314                         }
315                 };
316         $anchor2.click(function(e) {
317                 e.preventDefault();
318         });
319         $main.delegate("#foo", "click", function(e) {
320                 equals( e.isDefaultPrevented(), true, "isDefaultPrevented true passed to bubbled event" );
321         });
322         fakeClick( $anchor2 );
323         $anchor2.unbind( "click" );
324         $main.undelegate( "click" );
325         $anchor2.click(function(e) {
326                 // Let the default action occur
327         });
328         $main.delegate("#foo", "click", function(e) {
329                 equals( e.isDefaultPrevented(), false, "isDefaultPrevented false passed to bubbled event" );
330         });
331         fakeClick( $anchor2 );
332         $anchor2.unbind( "click" );
333         $main.undelegate( "click" );
334 });
335
336 test("bind(), iframes", function() {
337         // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
338         var doc = jQuery("#loadediframe").contents();
339         
340         jQuery("div", doc).bind("click", function() {
341                 ok( true, "Binding to element inside iframe" );
342         }).click().unbind('click');
343 });
344
345 test("bind(), trigger change on select", function() {
346         expect(5);
347         var counter = 0;
348         function selectOnChange(event) {
349                 equals( event.data, counter++, "Event.data is not a global event object" );
350         };
351         jQuery("#form select").each(function(i){
352                 jQuery(this).bind('change', i, selectOnChange);
353         }).trigger('change');
354 });
355
356 test("bind(), namespaced events, cloned events", function() {
357         expect(6);
358
359         jQuery("#firstp").bind("custom.test",function(e){
360                 ok(true, "Custom event triggered");
361         });
362
363         jQuery("#firstp").bind("click",function(e){
364                 ok(true, "Normal click triggered");
365         });
366
367         jQuery("#firstp").bind("click.test",function(e){
368                 ok(true, "Namespaced click triggered");
369         });
370
371         // Trigger both bound fn (2)
372         jQuery("#firstp").trigger("click");
373
374         // Trigger one bound fn (1)
375         jQuery("#firstp").trigger("click.test");
376
377         // Remove only the one fn
378         jQuery("#firstp").unbind("click.test");
379
380         // Trigger the remaining fn (1)
381         jQuery("#firstp").trigger("click");
382
383         // Remove the remaining fn
384         jQuery("#firstp").unbind(".test");
385
386         // Trigger the remaining fn (0)
387         jQuery("#firstp").trigger("custom");
388
389         // using contents will get comments regular, text, and comment nodes
390         jQuery("#nonnodes").contents().bind("tester", function () {
391                 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
392         }).trigger("tester");
393
394         // Make sure events stick with appendTo'd elements (which are cloned) #2027
395         jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
396         ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
397 });
398
399 test("bind(), multi-namespaced events", function() {
400         expect(6);
401         
402         var order = [
403                 "click.test.abc",
404                 "click.test.abc",
405                 "click.test",
406                 "click.test.abc",
407                 "click.test",
408                 "custom.test2"
409         ];
410         
411         function check(name, msg){
412                 same(name, order.shift(), msg);
413         }
414
415         jQuery("#firstp").bind("custom.test",function(e){
416                 check("custom.test", "Custom event triggered");
417         });
418
419         jQuery("#firstp").bind("custom.test2",function(e){
420                 check("custom.test2", "Custom event triggered");
421         });
422
423         jQuery("#firstp").bind("click.test",function(e){
424                 check("click.test", "Normal click triggered");
425         });
426
427         jQuery("#firstp").bind("click.test.abc",function(e){
428                 check("click.test.abc", "Namespaced click triggered");
429         });
430         
431         // Those would not trigger/unbind (#5303)
432         jQuery("#firstp").trigger("click.a.test");
433         jQuery("#firstp").unbind("click.a.test");
434
435         // Trigger both bound fn (1)
436         jQuery("#firstp").trigger("click.test.abc");
437
438         // Trigger one bound fn (1)
439         jQuery("#firstp").trigger("click.abc");
440
441         // Trigger two bound fn (2)
442         jQuery("#firstp").trigger("click.test");
443
444         // Remove only the one fn
445         jQuery("#firstp").unbind("click.abc");
446
447         // Trigger the remaining fn (1)
448         jQuery("#firstp").trigger("click");
449
450         // Remove the remaining fn
451         jQuery("#firstp").unbind(".test");
452
453         // Trigger the remaining fn (1)
454         jQuery("#firstp").trigger("custom");
455 });
456
457 test("bind(), with same function", function() {
458         expect(2)
459
460         var count = 0 ,  func = function(){
461                 count++;
462         };
463
464         jQuery("#liveHandlerOrder").bind("foo.bar", func).bind("foo.zar", func);
465         jQuery("#liveHandlerOrder").trigger("foo.bar");
466
467         equals(count, 1, "Verify binding function with multiple namespaces." );
468
469         jQuery("#liveHandlerOrder").unbind("foo.bar", func).unbind("foo.zar", func);
470         jQuery("#liveHandlerOrder").trigger("foo.bar");
471
472         equals(count, 1, "Verify that removing events still work." );
473 });
474
475 test("bind(), make sure order is maintained", function() {
476         expect(1);
477
478         var elem = jQuery("#firstp"), log = [], check = [];
479
480         for ( var i = 0; i < 100; i++ ) (function(i){
481                 elem.bind( "click", function(){
482                         log.push( i );
483                 });
484
485                 check.push( i );
486         })(i);
487
488         elem.trigger("click");
489
490         equals( log.join(","), check.join(","), "Make sure order was maintained." );
491
492         elem.unbind("click");
493 });
494  
495 test("bind(), with different this object", function() {
496         expect(4);
497         var thisObject = { myThis: true },
498                 data = { myData: true },
499                 handler1 = function( event ) {
500                         equals( this, thisObject, "bind() with different this object" );
501                 },
502                 handler2 = function( event ) {
503                         equals( this, thisObject, "bind() with different this object and data" );
504                         equals( event.data, data, "bind() with different this object and data" );
505                 };
506         
507         jQuery("#firstp")
508                 .bind("click", jQuery.proxy(handler1, thisObject)).click().unbind("click", handler1)
509                 .bind("click", data, jQuery.proxy(handler2, thisObject)).click().unbind("click", handler2);
510
511         ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
512 });
513
514 test("bind(name, false), unbind(name, false)", function() {
515         expect(3);
516
517         var main = 0;
518         jQuery("#main").bind("click", function(e){ main++; });
519         jQuery("#ap").trigger("click");
520         equals( main, 1, "Verify that the trigger happened correctly." );
521
522         main = 0;
523         jQuery("#ap").bind("click", false);
524         jQuery("#ap").trigger("click");
525         equals( main, 0, "Verify that no bubble happened." );
526
527         main = 0;
528         jQuery("#ap").unbind("click", false);
529         jQuery("#ap").trigger("click");
530         equals( main, 1, "Verify that the trigger happened correctly." );
531 });
532
533 test("bind()/trigger()/unbind() on plain object", function() {
534         expect( 8 );
535
536         var obj = {};
537
538         // Make sure it doesn't complain when no events are found
539         jQuery(obj).trigger("test");
540
541         // Make sure it doesn't complain when no events are found
542         jQuery(obj).unbind("test");
543
544         jQuery(obj).bind({
545                 test: function() {
546                         ok( true, "Custom event run." );
547                 },
548                 submit: function() {
549                         ok( true, "Custom submit event run." );
550                 }
551         });
552
553         var events = jQuery(obj).data("__events__");
554         ok( events, "Object has events bound." );
555         equals( obj.events, undefined, "Events object on plain objects is not events" );
556         equals( typeof events, "function", "'events' expando is a function on plain objects." );
557         equals( obj.test, undefined, "Make sure that test event is not on the plain object." );
558         equals( obj.handle, undefined, "Make sure that the event handler is not on the plain object." );
559
560         // Should trigger 1
561         jQuery(obj).trigger("test");
562         jQuery(obj).trigger("submit");
563
564         jQuery(obj).unbind("test");
565         jQuery(obj).unbind("submit");
566
567         // Should trigger 0
568         jQuery(obj).trigger("test");
569
570         // Make sure it doesn't complain when no events are found
571         jQuery(obj).unbind("test");
572         
573         equals( obj.__events__, undefined, "Make sure events object is removed" );
574 });
575
576 test("unbind(type)", function() {
577         expect( 0 );
578         
579         var $elem = jQuery("#firstp"),
580                 message;
581
582         function error(){
583                 ok( false, message );
584         }
585         
586         message = "unbind passing function";
587         $elem.bind('error1', error).unbind('error1',error).triggerHandler('error1');
588         
589         message = "unbind all from event";
590         $elem.bind('error1', error).unbind('error1').triggerHandler('error1');
591         
592         message = "unbind all";
593         $elem.bind('error1', error).unbind().triggerHandler('error1');
594         
595         message = "unbind many with function";
596         $elem.bind('error1 error2',error)
597                  .unbind('error1 error2', error )
598                  .trigger('error1').triggerHandler('error2');
599
600         message = "unbind many"; // #3538
601         $elem.bind('error1 error2',error)
602                  .unbind('error1 error2')
603                  .trigger('error1').triggerHandler('error2');
604         
605         message = "unbind without a type or handler";
606         $elem.bind("error1 error2.test",error)
607                  .unbind()
608                  .trigger("error1").triggerHandler("error2");
609 });
610
611 test("unbind(eventObject)", function() {
612         expect(4);
613         
614         var $elem = jQuery("#firstp"),
615                 num;
616
617         function assert( expected ){
618                 num = 0;
619                 $elem.trigger('foo').triggerHandler('bar');
620                 equals( num, expected, "Check the right handlers are triggered" );
621         }
622         
623         $elem
624                 // This handler shouldn't be unbound
625                 .bind('foo', function(){
626                         num += 1;
627                 })
628                 .bind('foo', function(e){
629                         $elem.unbind( e )
630                         num += 2;
631                 })
632                 // Neither this one
633                 .bind('bar', function(){
634                         num += 4;
635                 });
636                 
637         assert( 7 );
638         assert( 5 );
639         
640         $elem.unbind('bar');
641         assert( 1 );
642         
643         $elem.unbind(); 
644         assert( 0 );
645 });
646
647 test("hover()", function() {
648         var times = 0,
649                 handler1 = function( event ) { ++times; },
650                 handler2 = function( event ) { ++times; };
651
652         jQuery("#firstp")
653                 .hover(handler1, handler2)
654                 .mouseenter().mouseleave()
655                 .unbind("mouseenter", handler1)
656                 .unbind("mouseleave", handler2)
657                 .hover(handler1)
658                 .mouseenter().mouseleave()
659                 .unbind("mouseenter mouseleave", handler1)
660                 .mouseenter().mouseleave();
661
662         equals( times, 4, "hover handlers fired" );
663 });
664
665 test("trigger() shortcuts", function() {
666         expect(6);
667         jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
668                 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
669                 equals( close.length, 0, "Context element does not exist, length must be zero" );
670                 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
671                 return false;
672         }).click();
673         
674         jQuery("#check1").click(function() {
675                 ok( true, "click event handler for checkbox gets fired twice, see #815" );
676         }).click();
677         
678         var counter = 0;
679         jQuery('#firstp')[0].onclick = function(event) {
680                 counter++;
681         };
682         jQuery('#firstp').click();
683         equals( counter, 1, "Check that click, triggers onclick event handler also" );
684         
685         var clickCounter = 0;
686         jQuery('#simon1')[0].onclick = function(event) {
687                 clickCounter++;
688         };
689         jQuery('#simon1').click();
690         equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
691         
692         jQuery('<img />').load(function(){
693                 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
694         }).load();
695 });
696
697 test("trigger() bubbling", function() {
698         expect(14);
699
700         var doc = 0, html = 0, body = 0, main = 0, ap = 0;
701
702         jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
703         jQuery("html").bind("click", function(e){ html++; });
704         jQuery("body").bind("click", function(e){ body++; });
705         jQuery("#main").bind("click", function(e){ main++; });
706         jQuery("#ap").bind("click", function(){ ap++; return false; });
707
708         jQuery("html").trigger("click");
709         equals( doc, 1, "HTML bubble" );
710         equals( html, 1, "HTML bubble" );
711
712         jQuery("body").trigger("click");
713         equals( doc, 2, "Body bubble" );
714         equals( html, 2, "Body bubble" );
715         equals( body, 1, "Body bubble" );
716
717         jQuery("#main").trigger("click");
718         equals( doc, 3, "Main bubble" );
719         equals( html, 3, "Main bubble" );
720         equals( body, 2, "Main bubble" );
721         equals( main, 1, "Main bubble" );
722
723         jQuery("#ap").trigger("click");
724         equals( doc, 3, "ap bubble" );
725         equals( html, 3, "ap bubble" );
726         equals( body, 2, "ap bubble" );
727         equals( main, 1, "ap bubble" );
728         equals( ap, 1, "ap bubble" );
729 });
730
731 test("trigger(type, [data], [fn])", function() {
732         expect(14);
733
734         var handler = function(event, a, b, c) {
735                 equals( event.type, "click", "check passed data" );
736                 equals( a, 1, "check passed data" );
737                 equals( b, "2", "check passed data" );
738                 equals( c, "abc", "check passed data" );
739                 return "test";
740         };
741
742         var $elem = jQuery("#firstp");
743
744         // Simulate a "native" click
745         $elem[0].click = function(){
746                 ok( true, "Native call was triggered" );
747         };
748
749         // Triggers handlrs and native
750         // Trigger 5
751         $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
752
753         // Simulate a "native" click
754         $elem[0].click = function(){
755                 ok( false, "Native call was triggered" );
756         };
757
758         // Trigger only the handlers (no native)
759         // Triggers 5
760         equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
761
762         var pass = true;
763         try {
764                 jQuery('#form input:first').hide().trigger('focus');
765         } catch(e) {
766                 pass = false;
767         }
768         ok( pass, "Trigger focus on hidden element" );
769         
770         pass = true;
771         try {
772                 jQuery('table:first').bind('test:test', function(){}).trigger('test:test');
773         } catch (e) {
774                 pass = false;
775         }
776         ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
777
778         var form = jQuery("<form action=''></form>").appendTo("body");
779
780         // Make sure it can be prevented locally
781         form.submit(function(){
782                 ok( true, "Local bind still works." );
783                 return false;
784         });
785
786         // Trigger 1
787         form.trigger("submit");
788
789         form.unbind("submit");
790
791         jQuery(document).submit(function(){
792                 ok( true, "Make sure bubble works up to document." );
793                 return false;
794         });
795
796         // Trigger 1
797         form.trigger("submit");
798
799         jQuery(document).unbind("submit");
800
801         form.remove();
802 });
803
804 test("jQuery.Event.currentTarget", function(){
805 });
806
807 test("trigger(eventObject, [data], [fn])", function() {
808         expect(25);
809         
810         var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
811                 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
812         
813         var event = jQuery.Event("noNew");      
814         ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
815         equals( event.type, "noNew", "Verify its type" );
816         
817         equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
818         equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
819         equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
820         
821         event.preventDefault();
822         equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
823         event.stopPropagation();
824         equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
825         
826         event.isPropagationStopped = function(){ return false };
827         event.stopImmediatePropagation();
828         equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
829         equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
830         
831         $parent.bind('foo',function(e){
832                 // Tries bubbling
833                 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
834                 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
835                 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
836                 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
837         });
838         
839         // test with an event object
840         event = new jQuery.Event("foo");
841         event.secret = 'boo!';
842         $child.trigger(event);
843         
844         // test with a literal object
845         $child.trigger({type:'foo', secret:'boo!'});
846         
847         $parent.unbind();
848
849         function error(){
850                 ok( false, "This assertion shouldn't be reached");
851         }
852         
853         $parent.bind('foo', error );
854         
855         $child.bind('foo',function(e, a, b, c ){
856                 equals( arguments.length, 4, "Check arguments length");
857                 equals( a, 1, "Check first custom argument");
858                 equals( b, 2, "Check second custom argument");
859                 equals( c, 3, "Check third custom argument");
860                 
861                 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
862                 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
863                 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
864                 
865                 // Skips both errors
866                 e.stopImmediatePropagation();
867                 
868                 return "result";
869         });
870         
871         // We should add this back in when we want to test the order
872         // in which event handlers are iterated.
873         //$child.bind('foo', error );
874         
875         event = new jQuery.Event("foo");
876         $child.trigger( event, [1,2,3] ).unbind();
877         equals( event.result, "result", "Check event.result attribute");
878         
879         // Will error if it bubbles
880         $child.triggerHandler('foo');
881         
882         $child.unbind();
883         $parent.unbind().remove();
884 });
885
886 test("jQuery.Event.currentTarget", function(){
887         expect(1);
888         
889         var counter = 0,
890                 $elem = jQuery('<button>a</button>').click(function(e){
891                 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
892         });
893         
894         // Fake event
895         $elem.trigger('click');
896         
897         // Cleanup
898         $elem.unbind();
899 });
900
901 test("toggle(Function, Function, ...)", function() {
902         expect(16);
903         
904         var count = 0,
905                 fn1 = function(e) { count++; },
906                 fn2 = function(e) { count--; },
907                 preventDefault = function(e) { e.preventDefault() },
908                 link = jQuery('#mark');
909         link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
910         equals( count, 1, "Check for toggle(fn, fn)" );
911
912         jQuery("#firstp").toggle(function () {
913                 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
914         }, function() {}).trigger("click", [ 1, 2, 3 ]);
915
916         var first = 0;
917         jQuery("#simon1").one("click", function() {
918                 ok( true, "Execute event only once" );
919                 jQuery(this).toggle(function() {
920                         equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
921                 }, function() {
922                         equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
923                 });
924                 return false;
925         }).click().click().click();
926         
927         var turn = 0;
928         var fns = [
929                 function(){
930                         turn = 1;
931                 },
932                 function(){
933                         turn = 2;
934                 },
935                 function(){
936                         turn = 3;
937                 }
938         ];
939         
940         var $div = jQuery("<div>&nbsp;</div>").toggle( fns[0], fns[1], fns[2] );
941         $div.click();
942         equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
943         $div.click();
944         equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
945         $div.click();
946         equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
947         $div.click();
948         equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
949         $div.click();
950         equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
951         
952         $div.unbind('click',fns[0]);
953         var data = jQuery.data( $div[0], 'events' );
954         ok( !data, "Unbinding one function from toggle unbinds them all");
955
956         // Test Multi-Toggles
957         var a = [], b = [];
958         $div = jQuery("<div/>");
959         $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
960         $div.click();
961         same( a, [1], "Check that a click worked." );
962
963         $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
964         $div.click();
965         same( a, [1,2], "Check that a click worked with a second toggle." );
966         same( b, [1], "Check that a click worked with a second toggle." );
967
968         $div.click();
969         same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
970         same( b, [1,2], "Check that a click worked with a second toggle, second click." );
971 });
972
973 test(".live()/.die()", function() {
974         expect(66);
975
976         var submit = 0, div = 0, livea = 0, liveb = 0;
977
978         jQuery("div").live("submit", function(){ submit++; return false; });
979         jQuery("div").live("click", function(){ div++; });
980         jQuery("div#nothiddendiv").live("click", function(){ livea++; });
981         jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
982
983         // Nothing should trigger on the body
984         jQuery("body").trigger("click");
985         equals( submit, 0, "Click on body" );
986         equals( div, 0, "Click on body" );
987         equals( livea, 0, "Click on body" );
988         equals( liveb, 0, "Click on body" );
989
990         // This should trigger two events
991         submit = 0, div = 0, livea = 0, liveb = 0;
992         jQuery("div#nothiddendiv").trigger("click");
993         equals( submit, 0, "Click on div" );
994         equals( div, 1, "Click on div" );
995         equals( livea, 1, "Click on div" );
996         equals( liveb, 0, "Click on div" );
997
998         // This should trigger three events (w/ bubbling)
999         submit = 0, div = 0, livea = 0, liveb = 0;
1000         jQuery("div#nothiddendivchild").trigger("click");
1001         equals( submit, 0, "Click on inner div" );
1002         equals( div, 2, "Click on inner div" );
1003         equals( livea, 1, "Click on inner div" );
1004         equals( liveb, 1, "Click on inner div" );
1005
1006         // This should trigger one submit
1007         submit = 0, div = 0, livea = 0, liveb = 0;
1008         jQuery("div#nothiddendivchild").trigger("submit");
1009         equals( submit, 1, "Submit on div" );
1010         equals( div, 0, "Submit on div" );
1011         equals( livea, 0, "Submit on div" );
1012         equals( liveb, 0, "Submit on div" );
1013
1014         // Make sure no other events were removed in the process
1015         submit = 0, div = 0, livea = 0, liveb = 0;
1016         jQuery("div#nothiddendivchild").trigger("click");
1017         equals( submit, 0, "die Click on inner div" );
1018         equals( div, 2, "die Click on inner div" );
1019         equals( livea, 1, "die Click on inner div" );
1020         equals( liveb, 1, "die Click on inner div" );
1021
1022         // Now make sure that the removal works
1023         submit = 0, div = 0, livea = 0, liveb = 0;
1024         jQuery("div#nothiddendivchild").die("click");
1025         jQuery("div#nothiddendivchild").trigger("click");
1026         equals( submit, 0, "die Click on inner div" );
1027         equals( div, 2, "die Click on inner div" );
1028         equals( livea, 1, "die Click on inner div" );
1029         equals( liveb, 0, "die Click on inner div" );
1030
1031         // Make sure that the click wasn't removed too early
1032         submit = 0, div = 0, livea = 0, liveb = 0;
1033         jQuery("div#nothiddendiv").trigger("click");
1034         equals( submit, 0, "die Click on inner div" );
1035         equals( div, 1, "die Click on inner div" );
1036         equals( livea, 1, "die Click on inner div" );
1037         equals( liveb, 0, "die Click on inner div" );
1038
1039         // Make sure that stopPropgation doesn't stop live events
1040         submit = 0, div = 0, livea = 0, liveb = 0;
1041         jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
1042         jQuery("div#nothiddendivchild").trigger("click");
1043         equals( submit, 0, "stopPropagation Click on inner div" );
1044         equals( div, 1, "stopPropagation Click on inner div" );
1045         equals( livea, 0, "stopPropagation Click on inner div" );
1046         equals( liveb, 1, "stopPropagation Click on inner div" );
1047
1048         // Make sure click events only fire with primary click
1049         submit = 0, div = 0, livea = 0, liveb = 0;
1050         var event = jQuery.Event("click");
1051         event.button = 1;
1052         jQuery("div#nothiddendiv").trigger(event);
1053
1054         equals( livea, 0, "live secondary click" );
1055
1056         jQuery("div#nothiddendivchild").die("click");
1057         jQuery("div#nothiddendiv").die("click");
1058         jQuery("div").die("click");
1059         jQuery("div").die("submit");
1060
1061         // Test binding with a different context
1062         var clicked = 0, container = jQuery('#main')[0];
1063         jQuery("#foo", container).live("click", function(e){ clicked++; });
1064         jQuery("div").trigger('click');
1065         jQuery("#foo").trigger('click');
1066         jQuery("#main").trigger('click');
1067         jQuery("body").trigger('click');
1068         equals( clicked, 2, "live with a context" );
1069
1070         // Make sure the event is actually stored on the context
1071         ok( jQuery.data(container, "events").live, "live with a context" );
1072
1073         // Test unbinding with a different context
1074         jQuery("#foo", container).die("click");
1075         jQuery("#foo").trigger('click');
1076         equals( clicked, 2, "die with a context");
1077
1078         // Test binding with event data
1079         jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
1080         jQuery("#foo").trigger("click").die("click");
1081
1082         // Test binding with trigger data
1083         jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
1084         jQuery("#foo").trigger("click", true).die("click");
1085
1086         // Test binding with different this object
1087         jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
1088         jQuery("#foo").trigger("click").die("click");
1089
1090         // Test binding with different this object, event data, and trigger data
1091         jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
1092                 equals( e.data, true, "live with with different this object, event data, and trigger data" );
1093                 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" ); 
1094                 equals( data, true, "live with with different this object, event data, and trigger data")
1095         }, { foo: "bar" }));
1096         jQuery("#foo").trigger("click", true).die("click");
1097
1098         // Verify that return false prevents default action
1099         jQuery("#anchor2").live("click", function(){ return false; });
1100         var hash = window.location.hash;
1101         jQuery("#anchor2").trigger("click");
1102         equals( window.location.hash, hash, "return false worked" );
1103         jQuery("#anchor2").die("click");
1104
1105         // Verify that .preventDefault() prevents default action
1106         jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
1107         var hash = window.location.hash;
1108         jQuery("#anchor2").trigger("click");
1109         equals( window.location.hash, hash, "e.preventDefault() worked" );
1110         jQuery("#anchor2").die("click");
1111
1112         // Test binding the same handler to multiple points
1113         var called = 0;
1114         function callback(){ called++; return false; }
1115
1116         jQuery("#nothiddendiv").live("click", callback);
1117         jQuery("#anchor2").live("click", callback);
1118
1119         jQuery("#nothiddendiv").trigger("click");
1120         equals( called, 1, "Verify that only one click occurred." );
1121
1122         called = 0;
1123         jQuery("#anchor2").trigger("click");
1124         equals( called, 1, "Verify that only one click occurred." );
1125
1126         // Make sure that only one callback is removed
1127         jQuery("#anchor2").die("click", callback);
1128
1129         called = 0;
1130         jQuery("#nothiddendiv").trigger("click");
1131         equals( called, 1, "Verify that only one click occurred." );
1132
1133         called = 0;
1134         jQuery("#anchor2").trigger("click");
1135         equals( called, 0, "Verify that no click occurred." );
1136
1137         // Make sure that it still works if the selector is the same,
1138         // but the event type is different
1139         jQuery("#nothiddendiv").live("foo", callback);
1140
1141         // Cleanup
1142         jQuery("#nothiddendiv").die("click", callback);
1143
1144         called = 0;
1145         jQuery("#nothiddendiv").trigger("click");
1146         equals( called, 0, "Verify that no click occurred." );
1147
1148         called = 0;
1149         jQuery("#nothiddendiv").trigger("foo");
1150         equals( called, 1, "Verify that one foo occurred." );
1151
1152         // Cleanup
1153         jQuery("#nothiddendiv").die("foo", callback);
1154         
1155         // Make sure we don't loose the target by DOM modifications
1156         // after the bubble already reached the liveHandler
1157         var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1158         
1159         jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
1160         jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
1161         
1162         jQuery("#nothiddendiv span").click();
1163         equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1164         equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1165         
1166         // Cleanup
1167         jQuery("#nothiddendivchild").die("click");
1168
1169         // Verify that .live() ocurs and cancel buble in the same order as
1170         // we would expect .bind() and .click() without delegation
1171         var lived = 0, livee = 0;
1172         
1173         // bind one pair in one order
1174         jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
1175         jQuery('span#liveSpan1').live('click', function(){ livee++; });
1176
1177         jQuery('span#liveSpan1 a').click();
1178         equals( lived, 1, "Verify that only one first handler occurred." );
1179         equals( livee, 0, "Verify that second handler doesn't." );
1180
1181         // and one pair in inverse
1182         jQuery('span#liveSpan2').live('click', function(){ livee++; });
1183         jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
1184
1185         lived = 0;
1186         livee = 0;
1187         jQuery('span#liveSpan2 a').click();
1188         equals( lived, 1, "Verify that only one first handler occurred." );
1189         equals( livee, 0, "Verify that second handler doesn't." );
1190         
1191         // Cleanup
1192         jQuery("span#liveSpan1 a").die("click")
1193         jQuery("span#liveSpan1").die("click");
1194         jQuery("span#liveSpan2 a").die("click");
1195         jQuery("span#liveSpan2").die("click");
1196         
1197         // Test this, target and currentTarget are correct
1198         jQuery('span#liveSpan1').live('click', function(e){ 
1199                 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
1200                 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
1201                 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
1202         });
1203         
1204         jQuery('span#liveSpan1 a').click();
1205         
1206         jQuery('span#liveSpan1').die('click');
1207
1208         // Work with deep selectors
1209         livee = 0;
1210
1211         function clickB(){ livee++; }
1212
1213         jQuery("#nothiddendiv div").live("click", function(){ livee++; });
1214         jQuery("#nothiddendiv div").live("click", clickB);
1215         jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
1216
1217         equals( livee, 0, "No clicks, deep selector." );
1218
1219         livee = 0;
1220         jQuery("#nothiddendivchild").trigger("click");
1221         equals( livee, 2, "Click, deep selector." );
1222
1223         livee = 0;
1224         jQuery("#nothiddendivchild").trigger("mouseover");
1225         equals( livee, 1, "Mouseover, deep selector." );
1226
1227         jQuery("#nothiddendiv div").die("mouseover");
1228
1229         livee = 0;
1230         jQuery("#nothiddendivchild").trigger("click");
1231         equals( livee, 2, "Click, deep selector." );
1232
1233         livee = 0;
1234         jQuery("#nothiddendivchild").trigger("mouseover");
1235         equals( livee, 0, "Mouseover, deep selector." );
1236
1237         jQuery("#nothiddendiv div").die("click", clickB);
1238
1239         livee = 0;
1240         jQuery("#nothiddendivchild").trigger("click");
1241         equals( livee, 1, "Click, deep selector." );
1242
1243         jQuery("#nothiddendiv div").die("click");
1244
1245         jQuery("#nothiddendiv div").live("blur", function(){
1246                 ok( true, "Live div trigger blur." );
1247         });
1248
1249         jQuery("#nothiddendiv div").trigger("blur");
1250
1251         jQuery("#nothiddendiv div").die("blur");
1252 });
1253
1254 test("die all bound events", function(){
1255         expect(1);
1256
1257         var count = 0;
1258         var div = jQuery("div#nothiddendivchild");
1259
1260         div.live("click submit", function(){ count++; });
1261         div.die();
1262
1263         div.trigger("click");
1264         div.trigger("submit");
1265
1266         equals( count, 0, "Make sure no events were triggered." );
1267 });
1268
1269 test("live with multiple events", function(){
1270         expect(1);
1271
1272         var count = 0;
1273         var div = jQuery("div#nothiddendivchild");
1274
1275         div.live("click submit", function(){ count++; });
1276
1277         div.trigger("click");
1278         div.trigger("submit");
1279
1280         equals( count, 2, "Make sure both the click and submit were triggered." );
1281 });
1282
1283 test("live with namespaces", function(){
1284         expect(12);
1285
1286         var count1 = 0, count2 = 0;
1287
1288         jQuery("#liveSpan1").live("foo.bar", function(e){
1289                 count1++;
1290         });
1291
1292         jQuery("#liveSpan1").live("foo.zed", function(e){
1293                 count2++;
1294         });
1295
1296         jQuery("#liveSpan1").trigger("foo.bar");
1297         equals( count1, 1, "Got live foo.bar" );
1298         equals( count2, 0, "Got live foo.bar" );
1299
1300         count1 = 0, count2 = 0;
1301
1302         jQuery("#liveSpan1").trigger("foo.zed");
1303         equals( count1, 0, "Got live foo.zed" );
1304         equals( count2, 1, "Got live foo.zed" );
1305
1306         //remove one
1307         count1 = 0, count2 = 0;
1308
1309         jQuery("#liveSpan1").die("foo.zed");
1310         jQuery("#liveSpan1").trigger("foo.bar");
1311
1312         equals( count1, 1, "Got live foo.bar after dieing foo.zed" );
1313         equals( count2, 0, "Got live foo.bar after dieing foo.zed" );
1314
1315         count1 = 0, count2 = 0;
1316
1317         jQuery("#liveSpan1").trigger("foo.zed");
1318         equals( count1, 0, "Got live foo.zed" );
1319         equals( count2, 0, "Got live foo.zed" );
1320
1321         //remove the other
1322         jQuery("#liveSpan1").die("foo.bar");
1323
1324         count1 = 0, count2 = 0;
1325
1326         jQuery("#liveSpan1").trigger("foo.bar");
1327         equals( count1, 0, "Did not respond to foo.bar after dieing it" );
1328         equals( count2, 0, "Did not respond to foo.bar after dieing it" );
1329
1330         jQuery("#liveSpan1").trigger("foo.zed");
1331         equals( count1, 0, "Did not trigger foo.zed again" );
1332         equals( count2, 0, "Did not trigger foo.zed again" );
1333 });
1334
1335 test("live with change", function(){
1336         expect(8);
1337
1338         var selectChange = 0, checkboxChange = 0;
1339         
1340         var select = jQuery("select[name='S1']")
1341         select.live("change", function() {
1342                 selectChange++;
1343         });
1344         
1345         var checkbox = jQuery("#check2"), 
1346                 checkboxFunction = function(){
1347                         checkboxChange++;
1348                 }
1349         checkbox.live("change", checkboxFunction);
1350         
1351         // test click on select
1352
1353         // second click that changed it
1354         selectChange = 0;
1355         select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1356         select.trigger("change");
1357         equals( selectChange, 1, "Change on click." );
1358         
1359         // test keys on select
1360         selectChange = 0;
1361         select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1362         select.trigger("change");
1363         equals( selectChange, 1, "Change on keyup." );
1364         
1365         // test click on checkbox
1366         checkbox.trigger("change");
1367         equals( checkboxChange, 1, "Change on checkbox." );
1368         
1369         // test blur/focus on text
1370         var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1371         text.live("change", function() {
1372                 textChange++;
1373         });
1374
1375         text.val(oldTextVal+"foo");
1376         text.trigger("change");
1377         equals( textChange, 1, "Change on text input." );
1378
1379         text.val(oldTextVal);
1380         text.die("change");
1381         
1382         // test blur/focus on password
1383         var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1384         password.live("change", function() {
1385                 passwordChange++;
1386         });
1387
1388         password.val(oldPasswordVal + "foo");
1389         password.trigger("change");
1390         equals( passwordChange, 1, "Change on password input." );
1391
1392         password.val(oldPasswordVal);
1393         password.die("change");
1394         
1395         // make sure die works
1396         
1397         // die all changes
1398         selectChange = 0;
1399         select.die("change");
1400         select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1401         select.trigger("change");
1402         equals( selectChange, 0, "Die on click works." );
1403
1404         selectChange = 0;
1405         select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1406         select.trigger("change");
1407         equals( selectChange, 0, "Die on keyup works." );
1408         
1409         // die specific checkbox
1410         checkbox.die("change", checkboxFunction);
1411         checkbox.trigger("change");
1412         equals( checkboxChange, 1, "Die on checkbox." );
1413 });
1414
1415 test("live with submit", function() {
1416         var count1 = 0, count2 = 0;
1417         
1418         jQuery("#testForm").live("submit", function(ev) {
1419                 count1++;
1420                 ev.preventDefault();
1421         });
1422
1423         jQuery("body").live("submit", function(ev) {
1424                 count2++;
1425                 ev.preventDefault();
1426         });
1427
1428         jQuery("#testForm input[name=sub1]").submit();
1429         equals( count1, 1, "Verify form submit." );
1430         equals( count2, 1, "Verify body submit." );
1431         
1432         jQuery("#testForm").die("submit");
1433         jQuery("body").die("submit");
1434 });
1435
1436 test("live with special events", function() {
1437         expect(13);
1438
1439         jQuery.event.special.foo = {
1440                 setup: function( data, namespaces, handler ) {
1441                         ok( true, "Setup run." );
1442                 },
1443                 teardown: function( namespaces ) {
1444                         ok( true, "Teardown run." );
1445                 },
1446                 add: function( handleObj ) {
1447                         ok( true, "Add run." );
1448                 },
1449                 remove: function( handleObj ) {
1450                         ok( true, "Remove run." );
1451                 },
1452                 _default: function( event ) {
1453                         ok( true, "Default run." );
1454                 }
1455         };
1456
1457         // Run: setup, add
1458         jQuery("#liveSpan1").live("foo.a", function(e){
1459                 ok( true, "Handler 1 run." );
1460         });
1461
1462         // Run: add
1463         jQuery("#liveSpan1").live("foo.b", function(e){
1464                 ok( true, "Handler 2 run." );
1465         });
1466
1467         // Run: Handler 1, Handler 2, Default
1468         jQuery("#liveSpan1").trigger("foo");
1469
1470         // Run: Handler 1, Default
1471         // TODO: Namespace doesn't trigger default (?)
1472         jQuery("#liveSpan1").trigger("foo.a");
1473
1474         // Run: remove
1475         jQuery("#liveSpan1").die("foo.a");
1476
1477         // Run: Handler 2, Default
1478         jQuery("#liveSpan1").trigger("foo");
1479
1480         // Run: remove, teardown
1481         jQuery("#liveSpan1").die("foo");
1482
1483         delete jQuery.event.special.foo;
1484 });
1485
1486 test(".delegate()/.undelegate()", function() {
1487         expect(65);
1488
1489         var submit = 0, div = 0, livea = 0, liveb = 0;
1490
1491         jQuery("#body").delegate("div", "submit", function(){ submit++; return false; });
1492         jQuery("#body").delegate("div", "click", function(){ div++; });
1493         jQuery("#body").delegate("div#nothiddendiv", "click", function(){ livea++; });
1494         jQuery("#body").delegate("div#nothiddendivchild", "click", function(){ liveb++; });
1495
1496         // Nothing should trigger on the body
1497         jQuery("body").trigger("click");
1498         equals( submit, 0, "Click on body" );
1499         equals( div, 0, "Click on body" );
1500         equals( livea, 0, "Click on body" );
1501         equals( liveb, 0, "Click on body" );
1502
1503         // This should trigger two events
1504         submit = 0, div = 0, livea = 0, liveb = 0;
1505         jQuery("div#nothiddendiv").trigger("click");
1506         equals( submit, 0, "Click on div" );
1507         equals( div, 1, "Click on div" );
1508         equals( livea, 1, "Click on div" );
1509         equals( liveb, 0, "Click on div" );
1510
1511         // This should trigger three events (w/ bubbling)
1512         submit = 0, div = 0, livea = 0, liveb = 0;
1513         jQuery("div#nothiddendivchild").trigger("click");
1514         equals( submit, 0, "Click on inner div" );
1515         equals( div, 2, "Click on inner div" );
1516         equals( livea, 1, "Click on inner div" );
1517         equals( liveb, 1, "Click on inner div" );
1518
1519         // This should trigger one submit
1520         submit = 0, div = 0, livea = 0, liveb = 0;
1521         jQuery("div#nothiddendivchild").trigger("submit");
1522         equals( submit, 1, "Submit on div" );
1523         equals( div, 0, "Submit on div" );
1524         equals( livea, 0, "Submit on div" );
1525         equals( liveb, 0, "Submit on div" );
1526
1527         // Make sure no other events were removed in the process
1528         submit = 0, div = 0, livea = 0, liveb = 0;
1529         jQuery("div#nothiddendivchild").trigger("click");
1530         equals( submit, 0, "undelegate Click on inner div" );
1531         equals( div, 2, "undelegate Click on inner div" );
1532         equals( livea, 1, "undelegate Click on inner div" );
1533         equals( liveb, 1, "undelegate Click on inner div" );
1534
1535         // Now make sure that the removal works
1536         submit = 0, div = 0, livea = 0, liveb = 0;
1537         jQuery("#body").undelegate("div#nothiddendivchild", "click");
1538         jQuery("div#nothiddendivchild").trigger("click");
1539         equals( submit, 0, "undelegate Click on inner div" );
1540         equals( div, 2, "undelegate Click on inner div" );
1541         equals( livea, 1, "undelegate Click on inner div" );
1542         equals( liveb, 0, "undelegate Click on inner div" );
1543
1544         // Make sure that the click wasn't removed too early
1545         submit = 0, div = 0, livea = 0, liveb = 0;
1546         jQuery("div#nothiddendiv").trigger("click");
1547         equals( submit, 0, "undelegate Click on inner div" );
1548         equals( div, 1, "undelegate Click on inner div" );
1549         equals( livea, 1, "undelegate Click on inner div" );
1550         equals( liveb, 0, "undelegate Click on inner div" );
1551
1552         // Make sure that stopPropgation doesn't stop live events
1553         submit = 0, div = 0, livea = 0, liveb = 0;
1554         jQuery("#body").delegate("div#nothiddendivchild", "click", function(e){ liveb++; e.stopPropagation(); });
1555         jQuery("div#nothiddendivchild").trigger("click");
1556         equals( submit, 0, "stopPropagation Click on inner div" );
1557         equals( div, 1, "stopPropagation Click on inner div" );
1558         equals( livea, 0, "stopPropagation Click on inner div" );
1559         equals( liveb, 1, "stopPropagation Click on inner div" );
1560
1561         // Make sure click events only fire with primary click
1562         submit = 0, div = 0, livea = 0, liveb = 0;
1563         var event = jQuery.Event("click");
1564         event.button = 1;
1565         jQuery("div#nothiddendiv").trigger(event);
1566
1567         equals( livea, 0, "delegate secondary click" );
1568
1569         jQuery("#body").undelegate("div#nothiddendivchild", "click");
1570         jQuery("#body").undelegate("div#nothiddendiv", "click");
1571         jQuery("#body").undelegate("div", "click");
1572         jQuery("#body").undelegate("div", "submit");
1573
1574         // Test binding with a different context
1575         var clicked = 0, container = jQuery('#main')[0];
1576         jQuery("#main").delegate("#foo", "click", function(e){ clicked++; });
1577         jQuery("div").trigger('click');
1578         jQuery("#foo").trigger('click');
1579         jQuery("#main").trigger('click');
1580         jQuery("body").trigger('click');
1581         equals( clicked, 2, "delegate with a context" );
1582
1583         // Make sure the event is actually stored on the context
1584         ok( jQuery.data(container, "events").live, "delegate with a context" );
1585
1586         // Test unbinding with a different context
1587         jQuery("#main").undelegate("#foo", "click");
1588         jQuery("#foo").trigger('click');
1589         equals( clicked, 2, "undelegate with a context");
1590
1591         // Test binding with event data
1592         jQuery("#body").delegate("#foo", "click", true, function(e){ equals( e.data, true, "delegate with event data" ); });
1593         jQuery("#foo").trigger("click");
1594         jQuery("#body").undelegate("#foo", "click");
1595
1596         // Test binding with trigger data
1597         jQuery("#body").delegate("#foo", "click", function(e, data){ equals( data, true, "delegate with trigger data" ); });
1598         jQuery("#foo").trigger("click", true);
1599         jQuery("#body").undelegate("#foo", "click");
1600
1601         // Test binding with different this object
1602         jQuery("#body").delegate("#foo", "click", jQuery.proxy(function(e){ equals( this.foo, "bar", "delegate with event scope" ); }, { foo: "bar" }));
1603         jQuery("#foo").trigger("click");
1604         jQuery("#body").undelegate("#foo", "click");
1605
1606         // Test binding with different this object, event data, and trigger data
1607         jQuery("#body").delegate("#foo", "click", true, jQuery.proxy(function(e, data){
1608                 equals( e.data, true, "delegate with with different this object, event data, and trigger data" );
1609                 equals( this.foo, "bar", "delegate with with different this object, event data, and trigger data" ); 
1610                 equals( data, true, "delegate with with different this object, event data, and trigger data")
1611         }, { foo: "bar" }));
1612         jQuery("#foo").trigger("click", true);
1613         jQuery("#body").undelegate("#foo", "click");
1614
1615         // Verify that return false prevents default action
1616         jQuery("#body").delegate("#anchor2", "click", function(){ return false; });
1617         var hash = window.location.hash;
1618         jQuery("#anchor2").trigger("click");
1619         equals( window.location.hash, hash, "return false worked" );
1620         jQuery("#body").undelegate("#anchor2", "click");
1621
1622         // Verify that .preventDefault() prevents default action
1623         jQuery("#body").delegate("#anchor2", "click", function(e){ e.preventDefault(); });
1624         var hash = window.location.hash;
1625         jQuery("#anchor2").trigger("click");
1626         equals( window.location.hash, hash, "e.preventDefault() worked" );
1627         jQuery("#body").undelegate("#anchor2", "click");
1628
1629         // Test binding the same handler to multiple points
1630         var called = 0;
1631         function callback(){ called++; return false; }
1632
1633         jQuery("#body").delegate("#nothiddendiv", "click", callback);
1634         jQuery("#body").delegate("#anchor2", "click", callback);
1635
1636         jQuery("#nothiddendiv").trigger("click");
1637         equals( called, 1, "Verify that only one click occurred." );
1638
1639         called = 0;
1640         jQuery("#anchor2").trigger("click");
1641         equals( called, 1, "Verify that only one click occurred." );
1642
1643         // Make sure that only one callback is removed
1644         jQuery("#body").undelegate("#anchor2", "click", callback);
1645
1646         called = 0;
1647         jQuery("#nothiddendiv").trigger("click");
1648         equals( called, 1, "Verify that only one click occurred." );
1649
1650         called = 0;
1651         jQuery("#anchor2").trigger("click");
1652         equals( called, 0, "Verify that no click occurred." );
1653
1654         // Make sure that it still works if the selector is the same,
1655         // but the event type is different
1656         jQuery("#body").delegate("#nothiddendiv", "foo", callback);
1657
1658         // Cleanup
1659         jQuery("#body").undelegate("#nothiddendiv", "click", callback);
1660
1661         called = 0;
1662         jQuery("#nothiddendiv").trigger("click");
1663         equals( called, 0, "Verify that no click occurred." );
1664
1665         called = 0;
1666         jQuery("#nothiddendiv").trigger("foo");
1667         equals( called, 1, "Verify that one foo occurred." );
1668
1669         // Cleanup
1670         jQuery("#body").undelegate("#nothiddendiv", "foo", callback);
1671         
1672         // Make sure we don't loose the target by DOM modifications
1673         // after the bubble already reached the liveHandler
1674         var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1675         
1676         jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ jQuery("#nothiddendivchild").html(''); });
1677         jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ if(e.target) {livec++;} });
1678         
1679         jQuery("#nothiddendiv span").click();
1680         equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1681         equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1682         
1683         // Cleanup
1684         jQuery("#body").undelegate("#nothiddendivchild", "click");
1685
1686         // Verify that .live() ocurs and cancel buble in the same order as
1687         // we would expect .bind() and .click() without delegation
1688         var lived = 0, livee = 0;
1689         
1690         // bind one pair in one order
1691         jQuery("#body").delegate('span#liveSpan1 a', 'click', function(){ lived++; return false; });
1692         jQuery("#body").delegate('span#liveSpan1', 'click', function(){ livee++; });
1693
1694         jQuery('span#liveSpan1 a').click();
1695         equals( lived, 1, "Verify that only one first handler occurred." );
1696         equals( livee, 0, "Verify that second handler doesn't." );
1697
1698         // and one pair in inverse
1699         jQuery("#body").delegate('span#liveSpan2', 'click', function(){ livee++; });
1700         jQuery("#body").delegate('span#liveSpan2 a', 'click', function(){ lived++; return false; });
1701
1702         lived = 0;
1703         livee = 0;
1704         jQuery('span#liveSpan2 a').click();
1705         equals( lived, 1, "Verify that only one first handler occurred." );
1706         equals( livee, 0, "Verify that second handler doesn't." );
1707         
1708         // Cleanup
1709         jQuery("#body").undelegate("click");
1710         
1711         // Test this, target and currentTarget are correct
1712         jQuery("#body").delegate('span#liveSpan1', 'click', function(e){ 
1713                 equals( this.id, 'liveSpan1', 'Check the this within a delegate handler' );
1714                 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a delegate handler' );
1715                 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a delegate handler' );
1716         });
1717         
1718         jQuery('span#liveSpan1 a').click();
1719         
1720         jQuery("#body").undelegate('span#liveSpan1', 'click');
1721
1722         // Work with deep selectors
1723         livee = 0;
1724
1725         function clickB(){ livee++; }
1726
1727         jQuery("#body").delegate("#nothiddendiv div", "click", function(){ livee++; });
1728         jQuery("#body").delegate("#nothiddendiv div", "click", clickB);
1729         jQuery("#body").delegate("#nothiddendiv div", "mouseover", function(){ livee++; });
1730
1731         equals( livee, 0, "No clicks, deep selector." );
1732
1733         livee = 0;
1734         jQuery("#nothiddendivchild").trigger("click");
1735         equals( livee, 2, "Click, deep selector." );
1736
1737         livee = 0;
1738         jQuery("#nothiddendivchild").trigger("mouseover");
1739         equals( livee, 1, "Mouseover, deep selector." );
1740
1741         jQuery("#body").undelegate("#nothiddendiv div", "mouseover");
1742
1743         livee = 0;
1744         jQuery("#nothiddendivchild").trigger("click");
1745         equals( livee, 2, "Click, deep selector." );
1746
1747         livee = 0;
1748         jQuery("#nothiddendivchild").trigger("mouseover");
1749         equals( livee, 0, "Mouseover, deep selector." );
1750
1751         jQuery("#body").undelegate("#nothiddendiv div", "click", clickB);
1752
1753         livee = 0;
1754         jQuery("#nothiddendivchild").trigger("click");
1755         equals( livee, 1, "Click, deep selector." );
1756
1757         jQuery("#body").undelegate("#nothiddendiv div", "click");
1758 });
1759
1760 test("undelegate all bound events", function(){
1761         expect(1);
1762
1763         var count = 0;
1764         var div = jQuery("#body");
1765
1766         div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1767         div.undelegate();
1768
1769         jQuery("div#nothiddendivchild").trigger("click");
1770         jQuery("div#nothiddendivchild").trigger("submit");
1771
1772         equals( count, 0, "Make sure no events were triggered." );
1773 });
1774
1775 test("delegate with multiple events", function(){
1776         expect(1);
1777
1778         var count = 0;
1779         var div = jQuery("#body");
1780
1781         div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1782
1783         jQuery("div#nothiddendivchild").trigger("click");
1784         jQuery("div#nothiddendivchild").trigger("submit");
1785
1786         equals( count, 2, "Make sure both the click and submit were triggered." );
1787
1788         jQuery("#body").undelegate();
1789 });
1790
1791 test("delegate with change", function(){
1792         expect(8);
1793
1794         var selectChange = 0, checkboxChange = 0;
1795         
1796         var select = jQuery("select[name='S1']");
1797         jQuery("#body").delegate("select[name='S1']", "change", function() {
1798                 selectChange++;
1799         });
1800         
1801         var checkbox = jQuery("#check2"), 
1802                 checkboxFunction = function(){
1803                         checkboxChange++;
1804                 }
1805         jQuery("#body").delegate("#check2", "change", checkboxFunction);
1806         
1807         // test click on select
1808
1809         // second click that changed it
1810         selectChange = 0;
1811         select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1812         select.trigger("change");
1813         equals( selectChange, 1, "Change on click." );
1814         
1815         // test keys on select
1816         selectChange = 0;
1817         select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1818         select.trigger("change");
1819         equals( selectChange, 1, "Change on keyup." );
1820         
1821         // test click on checkbox
1822         checkbox.trigger("change");
1823         equals( checkboxChange, 1, "Change on checkbox." );
1824         
1825         // test blur/focus on text
1826         var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1827         jQuery("#body").delegate("#name", "change", function() {
1828                 textChange++;
1829         });
1830
1831         text.val(oldTextVal+"foo");
1832         text.trigger("change");
1833         equals( textChange, 1, "Change on text input." );
1834
1835         text.val(oldTextVal);
1836         jQuery("#body").die("change");
1837         
1838         // test blur/focus on password
1839         var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1840         jQuery("#body").delegate("#name", "change", function() {
1841                 passwordChange++;
1842         });
1843
1844         password.val(oldPasswordVal + "foo");
1845         password.trigger("change");
1846         equals( passwordChange, 1, "Change on password input." );
1847
1848         password.val(oldPasswordVal);
1849         jQuery("#body").undelegate("#name", "change");
1850         
1851         // make sure die works
1852         
1853         // die all changes
1854         selectChange = 0;
1855         jQuery("#body").undelegate("select[name='S1']", "change");
1856         select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1857         select.trigger("change");
1858         equals( selectChange, 0, "Die on click works." );
1859
1860         selectChange = 0;
1861         select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1862         select.trigger("change");
1863         equals( selectChange, 0, "Die on keyup works." );
1864         
1865         // die specific checkbox
1866         jQuery("#body").undelegate("#check2", "change", checkboxFunction);
1867         checkbox.trigger("change");
1868         equals( checkboxChange, 1, "Die on checkbox." );
1869 });
1870
1871 test("delegate with submit", function() {
1872         var count1 = 0, count2 = 0;
1873         
1874         jQuery("#body").delegate("#testForm", "submit", function(ev) {
1875                 count1++;
1876                 ev.preventDefault();
1877         });
1878
1879         jQuery(document).delegate("body", "submit", function(ev) {
1880                 count2++;
1881                 ev.preventDefault();
1882         });
1883
1884         jQuery("#testForm input[name=sub1]").submit();
1885         equals( count1, 1, "Verify form submit." );
1886         equals( count2, 1, "Verify body submit." );
1887         
1888         jQuery("#body").undelegate();
1889         jQuery(document).undelegate();
1890 });
1891
1892 test("Non DOM element events", function() {
1893         expect(1);
1894
1895         var o = {};
1896
1897         jQuery(o).bind('nonelementobj', function(e) {
1898                 ok( true, "Event on non-DOM object triggered" );
1899         });
1900
1901         jQuery(o).trigger('nonelementobj');
1902 });
1903
1904 test("window resize", function() {
1905         expect(2);
1906
1907         jQuery(window).unbind();
1908
1909         jQuery(window).bind("resize", function(){
1910                 ok( true, "Resize event fired." );
1911         }).resize().unbind("resize");
1912
1913         ok( !jQuery(window).data("__events__"), "Make sure all the events are gone." );
1914 });
1915
1916 test("focusin bubbles", function() {
1917         //create an input and focusin on it
1918         var input = jQuery("<input/>"), order = 0;
1919
1920         input.prependTo("body");
1921
1922         jQuery("body").bind("focusin.focusinBubblesTest",function(){
1923                 equals(1,order++,"focusin on the body second")
1924         });
1925
1926         input.bind("focusin.focusinBubblesTest",function(){
1927                 equals(0,order++,"focusin on the element first")
1928         });
1929
1930         input[0].focus();
1931         input.remove();
1932
1933         jQuery("body").unbind("focusin.focusinBubblesTest");
1934 });
1935
1936 /*
1937 test("jQuery(function($) {})", function() {
1938         stop();
1939         jQuery(function($) {
1940                 equals(jQuery, $, "ready doesn't provide an event object, instead it provides a reference to the jQuery function, see http://docs.jquery.com/Events/ready#fn");
1941                 start();
1942         });
1943 });
1944
1945 test("event properties", function() {
1946         stop();
1947         jQuery("#simon1").click(function(event) {
1948                 ok( event.timeStamp, "assert event.timeStamp is present" );
1949                 start();
1950         }).click();
1951 });
1952 */