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