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