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