Make sure that special.add actually copies over event namespaces and data. Fixes...
[jquery.git] / test / unit / event.js
1 module("event");
2
3 test("bind(), with data", function() {
4         expect(3);
5         var handler = function(event) {
6                 ok( event.data, "bind() with data, check passed data exists" );
7                 equals( event.data.foo, "bar", "bind() with data, Check value of passed data" );
8         };
9         jQuery("#firstp").bind("click", {foo: "bar"}, handler).click().unbind("click", handler);
10
11         ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
12 });
13
14 test("bind(), with data, trigger with data", function() {
15         expect(4);
16         var handler = function(event, data) {
17                 ok( event.data, "check passed data exists" );
18                 equals( event.data.foo, "bar", "Check value of passed data" );
19                 ok( data, "Check trigger data" );
20                 equals( data.bar, "foo", "Check value of trigger data" );
21         };
22         jQuery("#firstp").bind("click", {foo: "bar"}, handler).trigger("click", [{bar: "foo"}]).unbind("click", handler);
23 });
24
25 test("bind(), multiple events at once", function() {
26         expect(2);
27         var clickCounter = 0,
28                 mouseoverCounter = 0;
29         var handler = function(event) {
30                 if (event.type == "click")
31                         clickCounter += 1;
32                 else if (event.type == "mouseover")
33                         mouseoverCounter += 1;
34         };
35         jQuery("#firstp").bind("click mouseover", handler).trigger("click").trigger("mouseover");
36         equals( clickCounter, 1, "bind() with multiple events at once" );
37         equals( mouseoverCounter, 1, "bind() with multiple events at once" );
38 });
39
40 test("bind(), multiple events at once and namespaces", function() {
41         expect(7);
42
43         var cur, obj = {};
44
45         var div = jQuery("<div/>").bind("focusin.a", function(e) {
46                 equals( e.type, cur, "Verify right single event was fired." );
47         });
48
49         cur = "focusin";
50         div.trigger("focusin.a");
51
52         div = jQuery("<div/>").bind("click mouseover", obj, function(e) {
53                 equals( e.type, cur, "Verify right multi event was fired." );
54                 equals( e.data, obj, "Make sure the data came in correctly." );
55         });
56
57         cur = "click";
58         div.trigger("click");
59
60         cur = "mouseover";
61         div.trigger("mouseover");
62
63         div = jQuery("<div/>").bind("focusin.a focusout.b", function(e) {
64                 equals( e.type, cur, "Verify right multi event was fired." );
65         });
66
67         cur = "focusin";
68         div.trigger("focusin.a");
69
70         cur = "focusout";
71         div.trigger("focusout.b");
72 });
73
74 test("bind(), namespace with special add", function() {
75         expect(9);
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                 setup: function(){},
85                 teardown: function(){},
86                 add: function( handler, data, namespaces ) {
87                         return function(e) {
88                                 e.xyz = ++i;
89                                 handler.apply( this, arguments );
90                         };
91                 },
92                 remove: function() {}
93         };
94
95         div.bind("test.a", {x: 1}, function(e) {
96                 ok( !!e.xyz, "Make sure that the data is getting passed through." );
97                 equals( e.data.x, 1, "Make sure data is attached properly." );
98         });
99
100         div.bind("test.b", {x: 2}, function(e) {
101                 ok( !!e.xyz, "Make sure that the data is getting passed through." );
102                 equals( e.data.x, 2, "Make sure data is attached properly." );
103         });
104
105         // Should trigger 5
106         div.trigger("test");
107
108         // Should trigger 2
109         div.trigger("test.a");
110
111         // Should trigger 2
112         div.trigger("test.b");
113 });
114
115 test("bind(), no data", function() {
116         expect(1);
117         var handler = function(event) {
118                 ok ( !event.data, "Check that no data is added to the event object" );
119         };
120         jQuery("#firstp").bind("click", handler).trigger("click");
121 });
122
123 test("bind/one/unbind(Object)", function(){
124         expect(6);
125         
126         var clickCounter = 0, mouseoverCounter = 0;
127         function handler(event) {
128                 if (event.type == "click")
129                         clickCounter++;
130                 else if (event.type == "mouseover")
131                         mouseoverCounter++;
132         };
133         
134         function handlerWithData(event) {
135                 if (event.type == "click")
136                         clickCounter += event.data;
137                 else if (event.type == "mouseover")
138                         mouseoverCounter += event.data;
139         };
140         
141         function trigger(){
142                 $elem.trigger("click").trigger("mouseover");
143         }
144         
145         var $elem = jQuery("#firstp")
146                 // Regular bind
147                 .bind({
148                         click:handler,
149                         mouseover:handler
150                 })
151                 // Bind with data
152                 .one({
153                         click:handlerWithData,
154                         mouseover:handlerWithData
155                 }, 2 );
156         
157         trigger();
158         
159         equals( clickCounter, 3, "bind(Object)" );
160         equals( mouseoverCounter, 3, "bind(Object)" );
161         
162         trigger();
163         equals( clickCounter, 4, "bind(Object)" );
164         equals( mouseoverCounter, 4, "bind(Object)" );
165         
166         jQuery("#firstp").unbind({
167                 click:handler,
168                 mouseover:handler
169         });
170
171         trigger();
172         equals( clickCounter, 4, "bind(Object)" );
173         equals( mouseoverCounter, 4, "bind(Object)" );
174 });
175
176 test("bind(), iframes", function() {
177         // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
178         var doc = jQuery("#loadediframe").contents();
179         
180         jQuery("div", doc).bind("click", function() {
181                 ok( true, "Binding to element inside iframe" );
182         }).click().unbind('click');
183 });
184
185 test("bind(), trigger change on select", function() {
186         expect(3);
187         var counter = 0;
188         function selectOnChange(event) {
189                 equals( event.data, counter++, "Event.data is not a global event object" );
190         };
191         jQuery("#form select").each(function(i){
192                 jQuery(this).bind('change', i, selectOnChange);
193         }).trigger('change');
194 });
195
196 test("bind(), namespaced events, cloned events", function() {
197         expect(6);
198
199         jQuery("#firstp").bind("custom.test",function(e){
200                 ok(true, "Custom event triggered");
201         });
202
203         jQuery("#firstp").bind("click",function(e){
204                 ok(true, "Normal click triggered");
205         });
206
207         jQuery("#firstp").bind("click.test",function(e){
208                 ok(true, "Namespaced click triggered");
209         });
210
211         // Trigger both bound fn (2)
212         jQuery("#firstp").trigger("click");
213
214         // Trigger one bound fn (1)
215         jQuery("#firstp").trigger("click.test");
216
217         // Remove only the one fn
218         jQuery("#firstp").unbind("click.test");
219
220         // Trigger the remaining fn (1)
221         jQuery("#firstp").trigger("click");
222
223         // Remove the remaining fn
224         jQuery("#firstp").unbind(".test");
225
226         // Trigger the remaining fn (0)
227         jQuery("#firstp").trigger("custom");
228
229         // using contents will get comments regular, text, and comment nodes
230         jQuery("#nonnodes").contents().bind("tester", function () {
231                 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
232         }).trigger("tester");
233
234         // Make sure events stick with appendTo'd elements (which are cloned) #2027
235         jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
236         ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
237 });
238
239 test("bind(), multi-namespaced events", function() {
240         expect(6);
241         
242         var order = [
243                 "click.test.abc",
244                 "click.test.abc",
245                 "click.test",
246                 "click.test.abc",
247                 "click.test",
248                 "custom.test2"
249         ];
250         
251         function check(name, msg){
252                 same(name, order.shift(), msg);
253         }
254
255         jQuery("#firstp").bind("custom.test",function(e){
256                 check("custom.test", "Custom event triggered");
257         });
258
259         jQuery("#firstp").bind("custom.test2",function(e){
260                 check("custom.test2", "Custom event triggered");
261         });
262
263         jQuery("#firstp").bind("click.test",function(e){
264                 check("click.test", "Normal click triggered");
265         });
266
267         jQuery("#firstp").bind("click.test.abc",function(e){
268                 check("click.test.abc", "Namespaced click triggered");
269         });
270         
271         // Those would not trigger/unbind (#5303)
272         jQuery("#firstp").trigger("click.a.test");
273         jQuery("#firstp").unbind("click.a.test");
274
275         // Trigger both bound fn (1)
276         jQuery("#firstp").trigger("click.test.abc");
277
278         // Trigger one bound fn (1)
279         jQuery("#firstp").trigger("click.abc");
280
281         // Trigger two bound fn (2)
282         jQuery("#firstp").trigger("click.test");
283
284         // Remove only the one fn
285         jQuery("#firstp").unbind("click.abc");
286
287         // Trigger the remaining fn (1)
288         jQuery("#firstp").trigger("click");
289
290         // Remove the remaining fn
291         jQuery("#firstp").unbind(".test");
292
293         // Trigger the remaining fn (1)
294         jQuery("#firstp").trigger("custom");
295 });
296
297 test("bind(), with different this object", function() {
298         expect(4);
299         var thisObject = { myThis: true },
300                 data = { myData: true },
301                 handler1 = function( event ) {
302                         equals( this, thisObject, "bind() with different this object" );
303                 },
304                 handler2 = function( event ) {
305                         equals( this, thisObject, "bind() with different this object and data" );
306                         equals( event.data, data, "bind() with different this object and data" );
307                 };
308         
309         jQuery("#firstp")
310                 .bind("click", jQuery.proxy(handler1, thisObject)).click().unbind("click", handler1)
311                 .bind("click", data, jQuery.proxy(handler2, thisObject)).click().unbind("click", handler2);
312
313         ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
314 });
315
316 test("unbind(type)", function() {
317         expect( 0 );
318         
319         var $elem = jQuery("#firstp"),
320                 message;
321
322         function error(){
323                 ok( false, message );
324         }
325         
326         message = "unbind passing function";
327         $elem.bind('error', error).unbind('error',error).triggerHandler('error');
328         
329         message = "unbind all from event";
330         $elem.bind('error', error).unbind('error').triggerHandler('error');
331         
332         message = "unbind all";
333         $elem.bind('error', error).unbind().triggerHandler('error');
334         
335         message = "unbind many with function";
336         $elem.bind('error error2',error)
337                  .unbind('error error2', error )
338                  .trigger('error').triggerHandler('error2');
339
340         message = "unbind many"; // #3538
341         $elem.bind('error error2',error)
342                  .unbind('error error2')
343                  .trigger('error').triggerHandler('error2');
344         
345         message = "unbind without a type or handler";
346         $elem.bind("error error2.test",error)
347                  .unbind()
348                  .trigger("error").triggerHandler("error2");
349 });
350
351 test("unbind(eventObject)", function() {
352         expect(4);
353         
354         var $elem = jQuery("#firstp"),
355                 num;
356
357         function assert( expected ){
358                 num = 0;
359                 $elem.trigger('foo').triggerHandler('bar');
360                 equals( num, expected, "Check the right handlers are triggered" );
361         }
362         
363         $elem
364                 // This handler shouldn't be unbound
365                 .bind('foo', function(){
366                         num += 1;
367                 })
368                 .bind('foo', function(e){
369                         $elem.unbind( e )
370                         num += 2;
371                 })
372                 // Neither this one
373                 .bind('bar', function(){
374                         num += 4;
375                 });
376                 
377         assert( 7 );
378         assert( 5 );
379         
380         $elem.unbind('bar');
381         assert( 1 );
382         
383         $elem.unbind(); 
384         assert( 0 );
385 });
386
387 test("hover()", function() {
388         var times = 0,
389                 handler1 = function( event ) { ++times; },
390                 handler2 = function( event ) { ++times; };
391
392         jQuery("#firstp")
393                 .hover(handler1, handler2)
394                 .mouseenter().mouseleave()
395                 .unbind("mouseenter", handler1)
396                 .unbind("mouseleave", handler2)
397                 .hover(handler1)
398                 .mouseenter().mouseleave()
399                 .unbind("mouseenter mouseleave", handler1)
400                 .mouseenter().mouseleave();
401
402         equals( times, 4, "hover handlers fired" );
403 });
404
405 test("trigger() shortcuts", function() {
406         expect(6);
407         jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
408                 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
409                 equals( close.length, 0, "Context element does not exist, length must be zero" );
410                 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
411                 return false;
412         }).click();
413         
414         jQuery("#check1").click(function() {
415                 ok( true, "click event handler for checkbox gets fired twice, see #815" );
416         }).click();
417         
418         var counter = 0;
419         jQuery('#firstp')[0].onclick = function(event) {
420                 counter++;
421         };
422         jQuery('#firstp').click();
423         equals( counter, 1, "Check that click, triggers onclick event handler also" );
424         
425         var clickCounter = 0;
426         jQuery('#simon1')[0].onclick = function(event) {
427                 clickCounter++;
428         };
429         jQuery('#simon1').click();
430         equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
431         
432         jQuery('<img />').load(function(){
433                 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
434         }).load();
435 });
436
437 test("trigger() bubbling", function() {
438         expect(14);
439
440         var doc = 0, html = 0, body = 0, main = 0, ap = 0;
441
442         jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
443         jQuery("html").bind("click", function(e){ html++; });
444         jQuery("body").bind("click", function(e){ body++; });
445         jQuery("#main").bind("click", function(e){ main++; });
446         jQuery("#ap").bind("click", function(){ ap++; return false; });
447
448         jQuery("html").trigger("click");
449         equals( doc, 1, "HTML bubble" );
450         equals( html, 1, "HTML bubble" );
451
452         jQuery("body").trigger("click");
453         equals( doc, 2, "Body bubble" );
454         equals( html, 2, "Body bubble" );
455         equals( body, 1, "Body bubble" );
456
457         jQuery("#main").trigger("click");
458         equals( doc, 3, "Main bubble" );
459         equals( html, 3, "Main bubble" );
460         equals( body, 2, "Main bubble" );
461         equals( main, 1, "Main bubble" );
462
463         jQuery("#ap").trigger("click");
464         equals( doc, 3, "ap bubble" );
465         equals( html, 3, "ap bubble" );
466         equals( body, 2, "ap bubble" );
467         equals( main, 1, "ap bubble" );
468         equals( ap, 1, "ap bubble" );
469 });
470
471 test("trigger(type, [data], [fn])", function() {
472         expect(14);
473
474         var handler = function(event, a, b, c) {
475                 equals( event.type, "click", "check passed data" );
476                 equals( a, 1, "check passed data" );
477                 equals( b, "2", "check passed data" );
478                 equals( c, "abc", "check passed data" );
479                 return "test";
480         };
481
482         var $elem = jQuery("#firstp");
483
484         // Simulate a "native" click
485         $elem[0].click = function(){
486                 ok( true, "Native call was triggered" );
487         };
488
489         // Triggers handlrs and native
490         // Trigger 5
491         $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
492
493         // Simulate a "native" click
494         $elem[0].click = function(){
495                 ok( false, "Native call was triggered" );
496         };
497
498         // Trigger only the handlers (no native)
499         // Triggers 5
500         equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
501
502         var pass = true;
503         try {
504                 jQuery('#form input:first').hide().trigger('focus');
505         } catch(e) {
506                 pass = false;
507         }
508         ok( pass, "Trigger focus on hidden element" );
509         
510         pass = true;
511         try {
512                 jQuery('table:first').bind('test:test', function(){}).trigger('test:test');
513         } catch (e) {
514                 pass = false;
515         }
516         ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
517
518         var form = jQuery("<form action=''></form>").appendTo("body");
519
520         // Make sure it can be prevented locally
521         form.submit(function(){
522                 ok( true, "Local bind still works." );
523                 return false;
524         });
525
526         // Trigger 1
527         form.trigger("submit");
528
529         form.unbind("submit");
530
531         jQuery(document).submit(function(){
532                 ok( true, "Make sure bubble works up to document." );
533                 return false;
534         });
535
536         // Trigger 1
537         form.trigger("submit");
538
539         jQuery(document).unbind("submit");
540
541         form.remove();
542 });
543
544 test("jQuery.Event.currentTarget", function(){
545 });
546
547 test("trigger(eventObject, [data], [fn])", function() {
548         expect(25);
549         
550         var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
551                 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
552         
553         var event = jQuery.Event("noNew");      
554         ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
555         equals( event.type, "noNew", "Verify its type" );
556         
557         equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
558         equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
559         equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
560         
561         event.preventDefault();
562         equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
563         event.stopPropagation();
564         equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
565         
566         event.isPropagationStopped = function(){ return false };
567         event.stopImmediatePropagation();
568         equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
569         equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
570         
571         $parent.bind('foo',function(e){
572                 // Tries bubbling
573                 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
574                 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
575                 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
576                 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
577         });
578         
579         // test with an event object
580         event = new jQuery.Event("foo");
581         event.secret = 'boo!';
582         $child.trigger(event);
583         
584         // test with a literal object
585         $child.trigger({type:'foo', secret:'boo!'});
586         
587         $parent.unbind();
588
589         function error(){
590                 ok( false, "This assertion shouldn't be reached");
591         }
592         
593         $parent.bind('foo', error );
594         
595         $child.bind('foo',function(e, a, b, c ){
596                 equals( arguments.length, 4, "Check arguments length");
597                 equals( a, 1, "Check first custom argument");
598                 equals( b, 2, "Check second custom argument");
599                 equals( c, 3, "Check third custom argument");
600                 
601                 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
602                 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
603                 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
604                 
605                 // Skips both errors
606                 e.stopImmediatePropagation();
607                 
608                 return "result";
609         });
610         
611         // We should add this back in when we want to test the order
612         // in which event handlers are iterated.
613         //$child.bind('foo', error );
614         
615         event = new jQuery.Event("foo");
616         $child.trigger( event, [1,2,3] ).unbind();
617         equals( event.result, "result", "Check event.result attribute");
618         
619         // Will error if it bubbles
620         $child.triggerHandler('foo');
621         
622         $child.unbind();
623         $parent.unbind().remove();
624 });
625
626 test("jQuery.Event.currentTarget", function(){
627         expect(1);
628         
629         var counter = 0,
630                 $elem = jQuery('<button>a</button>').click(function(e){
631                 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
632         });
633         
634         // Fake event
635         $elem.trigger('click');
636         
637         // Cleanup
638         $elem.unbind();
639 });
640
641 test("toggle(Function, Function, ...)", function() {
642         expect(16);
643         
644         var count = 0,
645                 fn1 = function(e) { count++; },
646                 fn2 = function(e) { count--; },
647                 preventDefault = function(e) { e.preventDefault() },
648                 link = jQuery('#mark');
649         link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
650         equals( count, 1, "Check for toggle(fn, fn)" );
651
652         jQuery("#firstp").toggle(function () {
653                 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
654         }, function() {}).trigger("click", [ 1, 2, 3 ]);
655
656         var first = 0;
657         jQuery("#simon1").one("click", function() {
658                 ok( true, "Execute event only once" );
659                 jQuery(this).toggle(function() {
660                         equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
661                 }, function() {
662                         equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
663                 });
664                 return false;
665         }).click().click().click();
666         
667         var turn = 0;
668         var fns = [
669                 function(){
670                         turn = 1;
671                 },
672                 function(){
673                         turn = 2;
674                 },
675                 function(){
676                         turn = 3;
677                 }
678         ];
679         
680         var $div = jQuery("<div>&nbsp;</div>").toggle( fns[0], fns[1], fns[2] );
681         $div.click();
682         equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
683         $div.click();
684         equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
685         $div.click();
686         equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
687         $div.click();
688         equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
689         $div.click();
690         equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
691         
692         $div.unbind('click',fns[0]);
693         var data = jQuery.data( $div[0], 'events' );
694         ok( !data, "Unbinding one function from toggle unbinds them all");
695
696         // Test Multi-Toggles
697         var a = [], b = [];
698         $div = jQuery("<div/>");
699         $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
700         $div.click();
701         same( a, [1], "Check that a click worked." );
702
703         $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
704         $div.click();
705         same( a, [1,2], "Check that a click worked with a second toggle." );
706         same( b, [1], "Check that a click worked with a second toggle." );
707
708         $div.click();
709         same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
710         same( b, [1,2], "Check that a click worked with a second toggle, second click." );
711 });
712
713 test(".live()/.die()", function() {
714         expect(62);
715
716         var submit = 0, div = 0, livea = 0, liveb = 0;
717
718         jQuery("div").live("submit", function(){ submit++; return false; });
719         jQuery("div").live("click", function(){ div++; });
720         jQuery("div#nothiddendiv").live("click", function(){ livea++; });
721         jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
722
723         // Nothing should trigger on the body
724         jQuery("body").trigger("click");
725         equals( submit, 0, "Click on body" );
726         equals( div, 0, "Click on body" );
727         equals( livea, 0, "Click on body" );
728         equals( liveb, 0, "Click on body" );
729
730         // This should trigger two events
731         jQuery("div#nothiddendiv").trigger("click");
732         equals( submit, 0, "Click on div" );
733         equals( div, 1, "Click on div" );
734         equals( livea, 1, "Click on div" );
735         equals( liveb, 0, "Click on div" );
736
737         // This should trigger three events (w/ bubbling)
738         jQuery("div#nothiddendivchild").trigger("click");
739         equals( submit, 0, "Click on inner div" );
740         equals( div, 2, "Click on inner div" );
741         equals( livea, 2, "Click on inner div" );
742         equals( liveb, 1, "Click on inner div" );
743
744         // This should trigger one submit
745         jQuery("div#nothiddendivchild").trigger("submit");
746         equals( submit, 1, "Submit on div" );
747         equals( div, 2, "Submit on div" );
748         equals( livea, 2, "Submit on div" );
749         equals( liveb, 1, "Submit on div" );
750
751         // Make sure no other events were removed in the process
752         jQuery("div#nothiddendivchild").trigger("click");
753         equals( submit, 1, "die Click on inner div" );
754         equals( div, 3, "die Click on inner div" );
755         equals( livea, 3, "die Click on inner div" );
756         equals( liveb, 2, "die Click on inner div" );
757
758         // Now make sure that the removal works
759         jQuery("div#nothiddendivchild").die("click");
760         jQuery("div#nothiddendivchild").trigger("click");
761         equals( submit, 1, "die Click on inner div" );
762         equals( div, 4, "die Click on inner div" );
763         equals( livea, 4, "die Click on inner div" );
764         equals( liveb, 2, "die Click on inner div" );
765
766         // Make sure that the click wasn't removed too early
767         jQuery("div#nothiddendiv").trigger("click");
768         equals( submit, 1, "die Click on inner div" );
769         equals( div, 5, "die Click on inner div" );
770         equals( livea, 5, "die Click on inner div" );
771         equals( liveb, 2, "die Click on inner div" );
772
773         // Make sure that stopPropgation doesn't stop live events
774         jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
775         jQuery("div#nothiddendivchild").trigger("click");
776         equals( submit, 1, "stopPropagation Click on inner div" );
777         equals( div, 6, "stopPropagation Click on inner div" );
778         equals( livea, 6, "stopPropagation Click on inner div" );
779         equals( liveb, 3, "stopPropagation Click on inner div" );
780
781         // Make sure click events only fire with primary click
782         var event = jQuery.Event("click");
783         event.button = 1;
784         jQuery("div#nothiddendiv").trigger(event);
785
786         equals( livea, 6, "live secondary click" );
787
788         jQuery("div#nothiddendivchild").die("click");
789         jQuery("div#nothiddendiv").die("click");
790         jQuery("div").die("click");
791         jQuery("div").die("submit");
792
793         // Test binding with a different context
794         var clicked = 0, container = jQuery('#main')[0];
795         jQuery("#foo", container).live("click", function(e){ clicked++; });
796         jQuery("div").trigger('click');
797         jQuery("#foo").trigger('click');
798         jQuery("#main").trigger('click');
799         jQuery("body").trigger('click');
800         equals( clicked, 2, "live with a context" );
801
802         // Make sure the event is actually stored on the context
803         ok( jQuery.data(container, "events").live, "live with a context" );
804
805         // Test unbinding with a different context
806         jQuery("#foo", container).die("click");
807         jQuery("#foo").trigger('click');
808         equals( clicked, 2, "die with a context");
809
810         // Test binding with event data
811         jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
812         jQuery("#foo").trigger("click").die("click");
813
814         // Test binding with trigger data
815         jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
816         jQuery("#foo").trigger("click", true).die("click");
817
818         // Test binding with different this object
819         jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
820         jQuery("#foo").trigger("click").die("click");
821
822         // Test binding with different this object, event data, and trigger data
823         jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
824                 equals( e.data, true, "live with with different this object, event data, and trigger data" );
825                 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" ); 
826                 equals( data, true, "live with with different this object, event data, and trigger data")
827         }, { foo: "bar" }));
828         jQuery("#foo").trigger("click", true).die("click");
829
830         // Verify that return false prevents default action
831         jQuery("#anchor2").live("click", function(){ return false; });
832         var hash = window.location.hash;
833         jQuery("#anchor2").trigger("click");
834         equals( window.location.hash, hash, "return false worked" );
835         jQuery("#anchor2").die("click");
836
837         // Verify that .preventDefault() prevents default action
838         jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
839         var hash = window.location.hash;
840         jQuery("#anchor2").trigger("click");
841         equals( window.location.hash, hash, "e.preventDefault() worked" );
842         jQuery("#anchor2").die("click");
843
844         // Test binding the same handler to multiple points
845         var called = 0;
846         function callback(){ called++; return false; }
847
848         jQuery("#nothiddendiv").live("click", callback);
849         jQuery("#anchor2").live("click", callback);
850
851         jQuery("#nothiddendiv").trigger("click");
852         equals( called, 1, "Verify that only one click occurred." );
853
854         jQuery("#anchor2").trigger("click");
855         equals( called, 2, "Verify that only one click occurred." );
856
857         // Make sure that only one callback is removed
858         jQuery("#anchor2").die("click", callback);
859
860         jQuery("#nothiddendiv").trigger("click");
861         equals( called, 3, "Verify that only one click occurred." );
862
863         jQuery("#anchor2").trigger("click");
864         equals( called, 3, "Verify that no click occurred." );
865
866         // Make sure that it still works if the selector is the same,
867         // but the event type is different
868         jQuery("#nothiddendiv").live("foo", callback);
869
870         // Cleanup
871         jQuery("#nothiddendiv").die("click", callback);
872
873         jQuery("#nothiddendiv").trigger("click");
874         equals( called, 3, "Verify that no click occurred." );
875
876         jQuery("#nothiddendiv").trigger("foo");
877         equals( called, 4, "Verify that one foo occurred." );
878
879         // Cleanup
880         jQuery("#nothiddendiv").die("foo", callback);
881         
882         // Make sure we don't loose the target by DOM modifications
883         // after the bubble already reached the liveHandler
884         var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
885         
886         jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
887         jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
888         
889         jQuery("#nothiddendiv span").click();
890         equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
891         equals( livec, 1, "Verify that second handler occurred even with nuked target." );
892         
893         // Cleanup
894         jQuery("#nothiddendivchild").die("click");
895
896         // Verify that .live() ocurs and cancel buble in the same order as
897         // we would expect .bind() and .click() without delegation
898         var lived = 0, livee = 0;
899         
900         // bind one pair in one order
901         jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
902         jQuery('span#liveSpan1').live('click', function(){ livee++; });
903
904         jQuery('span#liveSpan1 a').click();
905         equals( lived, 1, "Verify that only one first handler occurred." );
906         equals( livee, 0, "Verify that second handler doesn't." );
907
908         // and one pair in inverse
909         jQuery('span#liveSpan2').live('click', function(){ livee++; });
910         jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
911
912         lived = 0;
913         livee = 0;
914         jQuery('span#liveSpan2 a').click();
915         equals( lived, 1, "Verify that only one first handler occurred." );
916         equals( livee, 0, "Verify that second handler doesn't." );
917         
918         // Cleanup
919         jQuery("span#liveSpan1 a, span#liveSpan1, span#liveSpan2 a, span#liveSpan2").die("click");
920         
921         // Test this, target and currentTarget are correct
922         jQuery('span#liveSpan1').live('click', function(e){ 
923                 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
924                 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
925                 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
926         });
927         
928         jQuery('span#liveSpan1 a').click();
929         
930         jQuery('span#liveSpan1').die('click');
931
932         // Work with deep selectors
933         livee = 0;
934
935         function clickB(){ livee++; }
936
937         jQuery("#nothiddendiv div").live("click", function(){ livee++; });
938         jQuery("#nothiddendiv div").live("click", clickB);
939         jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
940
941         equals( livee, 0, "No clicks, deep selector." );
942
943         livee = 0;
944         jQuery("#nothiddendivchild").trigger("click");
945         equals( livee, 2, "Click, deep selector." );
946
947         livee = 0;
948         jQuery("#nothiddendivchild").trigger("mouseover");
949         equals( livee, 1, "Mouseover, deep selector." );
950
951         jQuery("#nothiddendiv div").die("mouseover");
952
953         livee = 0;
954         jQuery("#nothiddendivchild").trigger("click");
955         equals( livee, 2, "Click, deep selector." );
956
957         livee = 0;
958         jQuery("#nothiddendivchild").trigger("mouseover");
959         equals( livee, 0, "Mouseover, deep selector." );
960
961         jQuery("#nothiddendiv div").die("click", clickB);
962
963         livee = 0;
964         jQuery("#nothiddendivchild").trigger("click");
965         equals( livee, 1, "Click, deep selector." );
966
967         jQuery("#nothiddendiv div").die("click");
968 });
969
970 test("die all bound events", function(){
971         expect(1);
972
973         var count = 0;
974         var div = jQuery("div#nothiddendivchild");
975
976         div.live("click submit", function(){ count++; });
977         div.die();
978
979         div.trigger("click");
980         div.trigger("submit");
981
982         equals( count, 0, "Make sure no events were triggered." );
983 });
984
985 test("live with multiple events", function(){
986         expect(1);
987
988         var count = 0;
989         var div = jQuery("div#nothiddendivchild");
990
991         div.live("click submit", function(){ count++; });
992
993         div.trigger("click");
994         div.trigger("submit");
995
996         equals( count, 2, "Make sure both the click and submit were triggered." );
997 });
998
999 test("live with change", function(){
1000         var selectChange = 0, checkboxChange = 0;
1001         
1002         var select = jQuery("select[name='S1']")
1003         select.live("change", function() {
1004                 selectChange++;
1005         });
1006         
1007         var checkbox = jQuery("#check2"), 
1008                 checkboxFunction = function(){
1009                         checkboxChange++;
1010                 }
1011         checkbox.live("change", checkboxFunction);
1012         
1013         // test click on select
1014
1015         // second click that changed it
1016         selectChange = 0;
1017         select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1018         select.trigger("change");
1019         equals( selectChange, 1, "Change on click." );
1020         
1021         // test keys on select
1022         selectChange = 0;
1023         select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1024         select.trigger("change");
1025         equals( selectChange, 1, "Change on keyup." );
1026         
1027         // test click on checkbox
1028         checkbox.trigger("change");
1029         equals( checkboxChange, 1, "Change on checkbox." );
1030         
1031         // test before activate on radio
1032         
1033         // test blur/focus on textarea
1034         var textarea = jQuery("#area1"), textareaChange = 0, oldVal = textarea.val();
1035         textarea.live("change", function() {
1036                 textareaChange++;
1037         });
1038
1039         textarea.val(oldVal + "foo");
1040         textarea.trigger("change");
1041         equals( textareaChange, 1, "Change on textarea." );
1042
1043         textarea.val(oldVal);
1044         textarea.die("change");
1045         
1046         // test blur/focus on text
1047         var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1048         text.live("change", function() {
1049                 textChange++;
1050         });
1051
1052         text.val(oldVal+"foo");
1053         text.trigger("change");
1054         equals( textChange, 1, "Change on text input." );
1055
1056         text.val(oldTextVal);
1057         text.die("change");
1058         
1059         // test blur/focus on password
1060         var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1061         password.live("change", function() {
1062                 passwordChange++;
1063         });
1064
1065         password.val(oldPasswordVal + "foo");
1066         password.trigger("change");
1067         equals( passwordChange, 1, "Change on password input." );
1068
1069         password.val(oldPasswordVal);
1070         password.die("change");
1071         
1072         // make sure die works
1073         
1074         // die all changes
1075         selectChange = 0;
1076         select.die("change");
1077         select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1078         select.trigger("change");
1079         equals( selectChange, 0, "Die on click works." );
1080
1081         selectChange = 0;
1082         select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1083         select.trigger("change");
1084         equals( selectChange, 0, "Die on keyup works." );
1085         
1086         // die specific checkbox
1087         checkbox.die("change", checkboxFunction);
1088         checkbox.trigger("change");
1089         equals( checkboxChange, 1, "Die on checkbox." );
1090 });
1091
1092 test("live with submit", function() {
1093         var count1 = 0, count2 = 0;
1094         
1095         jQuery("#testForm").live("submit", function(ev) {
1096                 count1++;
1097                 ev.preventDefault();
1098         });
1099
1100         jQuery("body").live("submit", function(ev) {
1101                 count2++;
1102                 ev.preventDefault();
1103         });
1104
1105         if ( jQuery.support.submitBubbles ) {
1106                 jQuery("#testForm input[name=sub1]")[0].click();
1107                 equals(count1,1 );
1108                 equals(count2,1);
1109         } else {
1110                 jQuery("#testForm input[name=sub1]")[0].click();
1111                 jQuery("#testForm input[name=T1]").trigger({type: "keypress", keyCode: 13});
1112                 equals(count1,2);
1113                 equals(count2,2);
1114         }
1115         
1116         jQuery("#testForm").die("submit");
1117         jQuery("body").die("submit");
1118 });
1119
1120 test("live with focus/blur", function(){
1121         expect(2);
1122
1123         // Setup
1124         jQuery("<input type='text' id='livefb' />").appendTo("body");
1125         
1126         var $child =  jQuery("#livefb"),
1127                 child = $child[0],
1128                 pass = {};
1129
1130         function worked(e){
1131                 pass[e.type] = true;
1132         }
1133         
1134         $child.live("focus", worked);
1135         $child.live("blur", worked);
1136         
1137         // Test
1138         child.focus();
1139         if (pass.focus)
1140                 ok(true, "Test live() with focus event");
1141         else
1142                 ok(true, "Cannot test focus because the window isn't focused");
1143
1144         child.blur();
1145         if (pass.blur)
1146                 ok( true, "Test live() with blur event");
1147         else
1148                 ok(true, "Cannot test blur because the window isn't focused");
1149         
1150         // Teardown
1151         $child.die("focus", worked);
1152         $child.die("blur", worked);
1153         $child.remove();
1154         window.scrollTo(0,0);
1155 });
1156
1157 test("Non DOM element events", function() {
1158         expect(3);
1159
1160         jQuery({})
1161                 .bind('nonelementglobal', function(e) {
1162                         ok( true, "Global event on non-DOM annonymos object triggered" );
1163                 });
1164
1165         var o = {};
1166
1167         jQuery(o)
1168                 .bind('nonelementobj', function(e) {
1169                         ok( true, "Event on non-DOM object triggered" );
1170                 }).bind('nonelementglobal', function() {
1171                         ok( true, "Global event on non-DOM object triggered" );
1172                 });
1173
1174         jQuery(o).trigger('nonelementobj');
1175         jQuery.event.trigger('nonelementglobal');
1176 });
1177
1178 /*
1179 test("jQuery(function($) {})", function() {
1180         stop();
1181         jQuery(function($) {
1182                 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");
1183                 start();
1184         });
1185 });
1186
1187 test("event properties", function() {
1188         stop();
1189         jQuery("#simon1").click(function(event) {
1190                 ok( event.timeStamp, "assert event.timeStamp is present" );
1191                 start();
1192         }).click();
1193 });
1194 */