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