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