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