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