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