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