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