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