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