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