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