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