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