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