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