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