jquery event: fixes #4989. blur and focus events now bubble and can be handled using...
[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(), no data", function() {
41         expect(1);
42         var handler = function(event) {
43                 ok ( !event.data, "Check that no data is added to the event object" );
44         };
45         jQuery("#firstp").bind("click", handler).trigger("click");
46 });
47
48 test("bind(), iframes", function() {
49         // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
50         var doc = jQuery("#loadediframe").contents();
51         
52         jQuery("div", doc).bind("click", function() {
53                 ok( true, "Binding to element inside iframe" );
54         }).click().unbind('click');
55 });
56
57 test("bind(), trigger change on select", function() {
58         expect(3);
59         var counter = 0;
60         function selectOnChange(event) {
61                 equals( event.data, counter++, "Event.data is not a global event object" );
62         };
63         jQuery("#form select").each(function(i){
64                 jQuery(this).bind('change', i, selectOnChange);
65         }).trigger('change');
66 });
67
68 test("bind(), namespaced events, cloned events", function() {
69         expect(6);
70
71         jQuery("#firstp").bind("custom.test",function(e){
72                 ok(true, "Custom event triggered");
73         });
74
75         jQuery("#firstp").bind("click",function(e){
76                 ok(true, "Normal click triggered");
77         });
78
79         jQuery("#firstp").bind("click.test",function(e){
80                 ok(true, "Namespaced click triggered");
81         });
82
83         // Trigger both bound fn (2)
84         jQuery("#firstp").trigger("click");
85
86         // Trigger one bound fn (1)
87         jQuery("#firstp").trigger("click.test");
88
89         // Remove only the one fn
90         jQuery("#firstp").unbind("click.test");
91
92         // Trigger the remaining fn (1)
93         jQuery("#firstp").trigger("click");
94
95         // Remove the remaining fn
96         jQuery("#firstp").unbind(".test");
97
98         // Trigger the remaining fn (0)
99         jQuery("#firstp").trigger("custom");
100
101         // using contents will get comments regular, text, and comment nodes
102         jQuery("#nonnodes").contents().bind("tester", function () {
103                 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
104         }).trigger("tester");
105
106         // Make sure events stick with appendTo'd elements (which are cloned) #2027
107         jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("p");
108         ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
109 });
110
111 test("bind(), multi-namespaced events", function() {
112         expect(6);
113         
114         var order = [
115                 "click.test.abc",
116                 "click.test.abc",
117                 "click.test",
118                 "click.test.abc",
119                 "click.test",
120                 "custom.test2"
121         ];
122         
123         function check(name, msg){
124                 same(name, order.shift(), msg);
125         }
126
127         jQuery("#firstp").bind("custom.test",function(e){
128                 check("custom.test", "Custom event triggered");
129         });
130
131         jQuery("#firstp").bind("custom.test2",function(e){
132                 check("custom.test2", "Custom event triggered");
133         });
134
135         jQuery("#firstp").bind("click.test",function(e){
136                 check("click.test", "Normal click triggered");
137         });
138
139         jQuery("#firstp").bind("click.test.abc",function(e){
140                 check("click.test.abc", "Namespaced click triggered");
141         });
142
143         // Trigger both bound fn (1)
144         jQuery("#firstp").trigger("click.test.abc");
145
146         // Trigger one bound fn (1)
147         jQuery("#firstp").trigger("click.abc");
148
149         // Trigger two bound fn (2)
150         jQuery("#firstp").trigger("click.test");
151
152         // Remove only the one fn
153         jQuery("#firstp").unbind("click.abc");
154
155         // Trigger the remaining fn (1)
156         jQuery("#firstp").trigger("click");
157
158         // Remove the remaining fn
159         jQuery("#firstp").unbind(".test");
160
161         // Trigger the remaining fn (1)
162         jQuery("#firstp").trigger("custom");
163 });
164
165 test("bind(), with different this object", function() {
166         expect(4);
167         var thisObject = { myThis: true },
168                 data = { myData: true },
169                 handler1 = function( event ) {
170                         equals( this, thisObject, "bind() with different this object" );
171                 },
172                 handler2 = function( event ) {
173                         equals( this, thisObject, "bind() with different this object and data" );
174                         equals( event.data, data, "bind() with different this object and data" );
175                 };
176         
177         jQuery("#firstp")
178                 .bind("click", handler1, thisObject).click().unbind("click", handler1)
179                 .bind("click", data, handler2, thisObject).click().unbind("click", handler2);
180
181         ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
182 });
183
184 test("unbind(type)", function() {
185         expect( 0 );
186         
187         var $elem = jQuery("#firstp"),
188                 message;
189
190         function error(){
191                 ok( false, message );
192         }
193         
194         message = "unbind passing function";
195         $elem.bind('error', error).unbind('error',error).triggerHandler('error');
196         
197         message = "unbind all from event";
198         $elem.bind('error', error).unbind('error').triggerHandler('error');
199         
200         message = "unbind all";
201         $elem.bind('error', error).unbind().triggerHandler('error');
202         
203         message = "unbind many with function";
204         $elem.bind('error error2',error)
205                  .unbind('error error2', error )
206                  .trigger('error').triggerHandler('error2');
207
208         message = "unbind many"; // #3538
209         $elem.bind('error error2',error)
210                  .unbind('error error2')
211                  .trigger('error').triggerHandler('error2');
212         
213         message = "unbind without a type or handler";
214         $elem.bind("error error2.test",error)
215                  .unbind()
216                  .trigger("error").triggerHandler("error2");
217 });
218
219 test("unbind(eventObject)", function() {
220         expect(4);
221         
222         var $elem = jQuery("#firstp"),
223                 num;
224
225         function assert( expected ){
226                 num = 0;
227                 $elem.trigger('foo').triggerHandler('bar');
228                 equals( num, expected, "Check the right handlers are triggered" );
229         }
230         
231         $elem
232                 // This handler shouldn't be unbound
233                 .bind('foo', function(){
234                         num += 1;
235                 })
236                 .bind('foo', function(e){
237                         $elem.unbind( e )
238                         num += 2;
239                 })
240                 // Neither this one
241                 .bind('bar', function(){
242                         num += 4;
243                 });
244                 
245         assert( 7 );
246         assert( 5 );
247         
248         $elem.unbind('bar');
249         assert( 1 );
250         
251         $elem.unbind(); 
252         assert( 0 );
253 });
254
255 test("hover()", function() {
256         var times = 0,
257                 handler1 = function( event ) { ++times; },
258                 handler2 = function( event ) { ++times; };
259
260         jQuery("#firstp")
261                 .hover(handler1, handler2)
262                 .mouseenter().mouseleave()
263                 .unbind("mouseenter", handler1)
264                 .unbind("mouseleave", handler2)
265                 .hover(handler1)
266                 .mouseenter().mouseleave()
267                 .unbind("mouseenter mouseleave", handler1)
268                 .mouseenter().mouseleave();
269
270         equals( times, 4, "hover handlers fired" );
271 });
272
273 test("trigger() shortcuts", function() {
274         expect(6);
275         jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL').find('a').bind('click', function() {
276                 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
277                 equals( close.length, 0, "Context element does not exist, length must be zero" );
278                 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
279                 return false;
280         }).click();
281         
282         jQuery("#check1").click(function() {
283                 ok( true, "click event handler for checkbox gets fired twice, see #815" );
284         }).click();
285         
286         var counter = 0;
287         jQuery('#firstp')[0].onclick = function(event) {
288                 counter++;
289         };
290         jQuery('#firstp').click();
291         equals( counter, 1, "Check that click, triggers onclick event handler also" );
292         
293         var clickCounter = 0;
294         jQuery('#simon1')[0].onclick = function(event) {
295                 clickCounter++;
296         };
297         jQuery('#simon1').click();
298         equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
299         
300         jQuery('<img />').load(function(){
301                 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
302         }).load();
303 });
304
305 test("trigger() bubbling", function() {
306         expect(14);
307
308         var doc = 0, html = 0, body = 0, main = 0, ap = 0;
309
310         jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
311         jQuery("html").bind("click", function(e){ html++; });
312         jQuery("body").bind("click", function(e){ body++; });
313         jQuery("#main").bind("click", function(e){ main++; });
314         jQuery("#ap").bind("click", function(){ ap++; return false; });
315
316         jQuery("html").trigger("click");
317         equals( doc, 1, "HTML bubble" );
318         equals( html, 1, "HTML bubble" );
319
320         jQuery("body").trigger("click");
321         equals( doc, 2, "Body bubble" );
322         equals( html, 2, "Body bubble" );
323         equals( body, 1, "Body bubble" );
324
325         jQuery("#main").trigger("click");
326         equals( doc, 3, "Main bubble" );
327         equals( html, 3, "Main bubble" );
328         equals( body, 2, "Main bubble" );
329         equals( main, 1, "Main bubble" );
330
331         jQuery("#ap").trigger("click");
332         equals( doc, 3, "ap bubble" );
333         equals( html, 3, "ap bubble" );
334         equals( body, 2, "ap bubble" );
335         equals( main, 1, "ap bubble" );
336         equals( ap, 1, "ap bubble" );
337 });
338
339 test("trigger(type, [data], [fn])", function() {
340         expect(12);
341
342         var handler = function(event, a, b, c) {
343                 equals( event.type, "click", "check passed data" );
344                 equals( a, 1, "check passed data" );
345                 equals( b, "2", "check passed data" );
346                 equals( c, "abc", "check passed data" );
347                 return "test";
348         };
349
350         var $elem = jQuery("#firstp");
351
352         // Simulate a "native" click
353         $elem[0].click = function(){
354                 ok( true, "Native call was triggered" );
355         };
356
357         // Triggers handlrs and native
358         // Trigger 5
359         $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
360
361         // Simulate a "native" click
362         $elem[0].click = function(){
363                 ok( false, "Native call was triggered" );
364         };
365
366         // Trigger only the handlers (no native)
367         // Triggers 5
368         equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
369
370         var pass = true;
371         try {
372                 jQuery('#form input:first').hide().trigger('focus');
373         } catch(e) {
374                 pass = false;
375         }
376         ok( pass, "Trigger focus on hidden element" );
377         
378         pass = true;
379         try {
380                 jQuery('table:first').bind('test:test', function(){}).trigger('test:test');
381         } catch (e) {
382                 pass = false;
383         }
384         ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
385 });
386
387 test("trigger(eventObject, [data], [fn])", function() {
388         expect(25);
389         
390         var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
391                 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
392         
393         var event = jQuery.Event("noNew");      
394         ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
395         equals( event.type, "noNew", "Verify its type" );
396         
397         equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
398         equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
399         equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
400         
401         event.preventDefault();
402         equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
403         event.stopPropagation();
404         equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
405         
406         event.isPropagationStopped = function(){ return false };
407         event.stopImmediatePropagation();
408         equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
409         equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
410         
411         $parent.bind('foo',function(e){
412                 // Tries bubbling
413                 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
414                 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
415                 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
416                 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
417         });
418         
419         // test with an event object
420         event = new jQuery.Event("foo");
421         event.secret = 'boo!';
422         $child.trigger(event);
423         
424         // test with a literal object
425         $child.trigger({type:'foo', secret:'boo!'});
426         
427         $parent.unbind();
428
429         function error(){
430                 ok( false, "This assertion shouldn't be reached");
431         }
432         
433         $parent.bind('foo', error );
434         
435         $child.bind('foo',function(e, a, b, c ){
436                 equals( arguments.length, 4, "Check arguments length");
437                 equals( a, 1, "Check first custom argument");
438                 equals( b, 2, "Check second custom argument");
439                 equals( c, 3, "Check third custom argument");
440                 
441                 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
442                 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
443                 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
444                 
445                 // Skips both errors
446                 e.stopImmediatePropagation();
447                 
448                 return "result";
449         });
450         
451         // We should add this back in when we want to test the order
452         // in which event handlers are iterated.
453         //$child.bind('foo', error );
454         
455         event = new jQuery.Event("foo");
456         $child.trigger( event, [1,2,3] ).unbind();
457         equals( event.result, "result", "Check event.result attribute");
458         
459         // Will error if it bubbles
460         $child.triggerHandler('foo');
461         
462         $child.unbind();
463         $parent.unbind().remove();
464 });
465
466 test("jQuery.Event.currentTarget", function(){
467         expect(1);
468         
469         var counter = 0,
470                 $elem = jQuery('<button>a</button>').click(function(e){
471                 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
472         });
473         
474         // Fake event
475         $elem.trigger('click');
476         
477         // Cleanup
478         $elem.unbind();
479 });
480
481 test("toggle(Function, Function, ...)", function() {
482         expect(11);
483         
484         var count = 0,
485                 fn1 = function(e) { count++; },
486                 fn2 = function(e) { count--; },
487                 preventDefault = function(e) { e.preventDefault() },
488                 link = jQuery('#mark');
489         link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
490         equals( count, 1, "Check for toggle(fn, fn)" );
491
492         jQuery("#firstp").toggle(function () {
493                 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
494         }, function() {}).trigger("click", [ 1, 2, 3 ]);
495
496         var first = 0;
497         jQuery("#simon1").one("click", function() {
498                 ok( true, "Execute event only once" );
499                 jQuery(this).toggle(function() {
500                         equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
501                 }, function() {
502                         equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
503                 });
504                 return false;
505         }).click().click().click();
506         
507         var turn = 0;
508         var fns = [
509                 function(){
510                         turn = 1;
511                 },
512                 function(){
513                         turn = 2;
514                 },
515                 function(){
516                         turn = 3;
517                 }
518         ];
519         
520         var $div = jQuery("<div>&nbsp;</div>").toggle( fns[0], fns[1], fns[2] );
521         $div.click();
522         equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
523         $div.click();
524         equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
525         $div.click();
526         equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
527         $div.click();
528         equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
529         $div.click();
530         equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
531         
532         $div.unbind('click',fns[0]);
533         var data = jQuery.data( $div[0], 'events' );
534         ok( !data, "Unbinding one function from toggle unbinds them all");
535 });
536
537 test(".live()/.die()", function() {
538         expect(58);
539
540         var submit = 0, div = 0, livea = 0, liveb = 0;
541
542         jQuery("div").live("submit", function(){ submit++; return false; });
543         jQuery("div").live("click", function(){ div++; });
544         jQuery("div#nothiddendiv").live("click", function(){ livea++; });
545         jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
546
547         // Nothing should trigger on the body
548         jQuery("body").trigger("click");
549         equals( submit, 0, "Click on body" );
550         equals( div, 0, "Click on body" );
551         equals( livea, 0, "Click on body" );
552         equals( liveb, 0, "Click on body" );
553
554         // This should trigger two events
555         jQuery("div#nothiddendiv").trigger("click");
556         equals( submit, 0, "Click on div" );
557         equals( div, 1, "Click on div" );
558         equals( livea, 1, "Click on div" );
559         equals( liveb, 0, "Click on div" );
560
561         // This should trigger three events (w/ bubbling)
562         jQuery("div#nothiddendivchild").trigger("click");
563         equals( submit, 0, "Click on inner div" );
564         equals( div, 2, "Click on inner div" );
565         equals( livea, 2, "Click on inner div" );
566         equals( liveb, 1, "Click on inner div" );
567
568         // This should trigger one submit
569         jQuery("div#nothiddendivchild").trigger("submit");
570         equals( submit, 1, "Submit on div" );
571         equals( div, 2, "Submit on div" );
572         equals( livea, 2, "Submit on div" );
573         equals( liveb, 1, "Submit on div" );
574
575         // Make sure no other events were removed in the process
576         jQuery("div#nothiddendivchild").trigger("click");
577         equals( submit, 1, "die Click on inner div" );
578         equals( div, 3, "die Click on inner div" );
579         equals( livea, 3, "die Click on inner div" );
580         equals( liveb, 2, "die Click on inner div" );
581
582         // Now make sure that the removal works
583         jQuery("div#nothiddendivchild").die("click");
584         jQuery("div#nothiddendivchild").trigger("click");
585         equals( submit, 1, "die Click on inner div" );
586         equals( div, 4, "die Click on inner div" );
587         equals( livea, 4, "die Click on inner div" );
588         equals( liveb, 2, "die Click on inner div" );
589
590         // Make sure that the click wasn't removed too early
591         jQuery("div#nothiddendiv").trigger("click");
592         equals( submit, 1, "die Click on inner div" );
593         equals( div, 5, "die Click on inner div" );
594         equals( livea, 5, "die Click on inner div" );
595         equals( liveb, 2, "die Click on inner div" );
596
597         // Make sure that stopPropgation doesn't stop live events
598         jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
599         jQuery("div#nothiddendivchild").trigger("click");
600         equals( submit, 1, "stopPropagation Click on inner div" );
601         equals( div, 6, "stopPropagation Click on inner div" );
602         equals( livea, 6, "stopPropagation Click on inner div" );
603         equals( liveb, 3, "stopPropagation Click on inner div" );
604
605         jQuery("div#nothiddendivchild").die("click");
606         jQuery("div#nothiddendiv").die("click");
607         jQuery("div").die("click");
608         jQuery("div").die("submit");
609
610         // Test binding with a different context
611         var clicked = 0, container = jQuery('#main')[0];
612         jQuery("#foo", container).live("click", function(e){ clicked++; });
613         jQuery("div").trigger('click');
614         jQuery("#foo").trigger('click');
615         jQuery("#main").trigger('click');
616         jQuery("body").trigger('click');
617         equals( clicked, 2, "live with a context" );
618
619         // Make sure the event is actually stored on the context
620         ok( jQuery.data(container, "events").live, "live with a context" );
621
622         // Test unbinding with a different context
623         jQuery("#foo", container).die("click");
624         jQuery("#foo").trigger('click');
625         equals( clicked, 2, "die with a context");
626
627         // Test binding with event data
628         jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
629         jQuery("#foo").trigger("click").die("click");
630
631         // Test binding with trigger data
632         jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
633         jQuery("#foo").trigger("click", true).die("click");
634
635         // Test binding with different this object
636         jQuery("#foo").live("click", function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" });
637         jQuery("#foo").trigger("click").die("click");
638
639         // Test binding with different this object, event data, and trigger data
640         jQuery("#foo").live("click", true, function(e, data){
641                 equals( e.data, true, "live with with different this object, event data, and trigger data" );
642                 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" ); 
643                 equals( data, true, "live with with different this object, event data, and trigger data")
644         }, { foo: "bar" });
645         jQuery("#foo").trigger("click", true).die("click");
646
647         // Verify that return false prevents default action
648         jQuery("#anchor2").live("click", function(){ return false; });
649         var hash = window.location.hash;
650         jQuery("#anchor2").trigger("click");
651         equals( window.location.hash, hash, "return false worked" );
652         jQuery("#anchor2").die("click");
653
654         // Verify that .preventDefault() prevents default action
655         jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
656         var hash = window.location.hash;
657         jQuery("#anchor2").trigger("click");
658         equals( window.location.hash, hash, "e.preventDefault() worked" );
659         jQuery("#anchor2").die("click");
660
661         // Test binding the same handler to multiple points
662         var called = 0;
663         function callback(){ called++; return false; }
664
665         jQuery("#nothiddendiv").live("click", callback);
666         jQuery("#anchor2").live("click", callback);
667
668         jQuery("#nothiddendiv").trigger("click");
669         equals( called, 1, "Verify that only one click occurred." );
670
671         jQuery("#anchor2").trigger("click");
672         equals( called, 2, "Verify that only one click occurred." );
673
674         // Make sure that only one callback is removed
675         jQuery("#anchor2").die("click", callback);
676
677         jQuery("#nothiddendiv").trigger("click");
678         equals( called, 3, "Verify that only one click occurred." );
679
680         jQuery("#anchor2").trigger("click");
681         equals( called, 3, "Verify that no click occurred." );
682
683         // Make sure that it still works if the selector is the same,
684         // but the event type is different
685         jQuery("#nothiddendiv").live("foo", callback);
686
687         // Cleanup
688         jQuery("#nothiddendiv").die("click", callback);
689
690         jQuery("#nothiddendiv").trigger("click");
691         equals( called, 3, "Verify that no click occurred." );
692
693         jQuery("#nothiddendiv").trigger("foo");
694         equals( called, 4, "Verify that one foo occurred." );
695
696         // Cleanup
697         jQuery("#nothiddendiv").die("foo", callback);
698         
699         // Make sure we don't loose the target by DOM modifications
700         // after the bubble already reached the liveHandler
701         var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
702         
703         jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
704         jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
705         
706         jQuery("#nothiddendiv span").click();
707         equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
708         equals( livec, 1, "Verify that second handler occurred even with nuked target." );
709         
710         // Cleanup
711         jQuery("#nothiddendivchild").die("click");
712
713         // Verify that .live() ocurs and cancel buble in the same order as
714         // we would expect .bind() and .click() without delegation
715         var lived = 0, livee = 0;
716         
717         // bind one pair in one order
718         jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
719         jQuery('span#liveSpan1').live('click', function(){ livee++; });
720
721         jQuery('span#liveSpan1 a').click();
722         equals( lived, 1, "Verify that only one first handler occurred." );
723         equals( livee, 0, "Verify that second handler don't." );
724
725         // and one pair in inverse
726         jQuery('#liveHandlerOrder span#liveSpan2').live('click', function(){ livee++; });
727         jQuery('#liveHandlerOrder span#liveSpan2 a').live('click', function(){ lived++; return false; });
728
729         jQuery('span#liveSpan2 a').click();
730         equals( lived, 2, "Verify that only one first handler occurred." );
731         equals( livee, 0, "Verify that second handler don't." );
732         
733         // Cleanup
734         jQuery("span#liveSpan1 a, span#liveSpan1, span#liveSpan2 a, span#liveSpan2").die("click");
735         
736         // Test this, target and currentTarget are correct
737         jQuery('span#liveSpan1').live('click', function(e){ 
738                 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
739                 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
740                 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
741         });
742         
743         jQuery('span#liveSpan1 a').click();
744         
745         jQuery('span#liveSpan1').die('click');
746 });
747
748 test("live with focus/blur", function(){
749         expect(2);
750
751         // Setup
752         jQuery("<input type='text' id='livefb' />").appendTo("body");
753         
754         var $child =  jQuery("#livefb"),
755                 child = $child[0],
756                 counter = 0;
757
758         function count(){
759                 counter++;
760         }
761         
762         // Test
763         $child.live("focus", count);
764         $child.live("blur", count);
765
766         child.focus();
767         equals(counter, 1, "Test live() with focus event");
768
769         child.blur();
770         equals(counter, 2, "Test live() with blur event");
771         
772         // Teardown
773         $child.die("focus", count);
774         $child.die("blur", count);
775         $child.remove();
776 });
777
778 test("Non DOM element events", function() {
779         expect(3);
780
781         jQuery({})
782                 .bind('nonelementglobal', function(e) {
783                         ok( true, "Global event on non-DOM annonymos object triggered" );
784                 });
785
786         var o = {};
787
788         jQuery(o)
789                 .bind('nonelementobj', function(e) {
790                         ok( true, "Event on non-DOM object triggered" );
791                 }).bind('nonelementglobal', function() {
792                         ok( true, "Global event on non-DOM object triggered" );
793                 });
794
795         jQuery(o).trigger('nonelementobj');
796         jQuery.event.trigger('nonelementglobal');
797 });
798
799 /*
800 test("jQuery(function($) {})", function() {
801         stop();
802         jQuery(function($) {
803                 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");
804                 start();
805         });
806 });
807
808 test("event properties", function() {
809         stop();
810         jQuery("#simon1").click(function(event) {
811                 ok( event.timeStamp, "assert event.timeStamp is present" );
812                 start();
813         }).click();
814 });
815 */