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