Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / jquery / resources / test / unit / event.js
blobb7b26046214df7645e6d71836fd489e2eed010fc
1 module("event", { teardown: moduleTeardown });
3 test("null or undefined handler", function() {
4 expect(2);
5 // Supports Fixes bug #7229
6 try {
7 jQuery("#firstp").click(null);
8 ok(true, "Passing a null handler will not throw an exception");
9 } catch (e) {}
11 try {
12 jQuery("#firstp").click(undefined);
13 ok(true, "Passing an undefined handler will not throw an exception");
14 } catch (e) {}
15 });
17 test("bind(), with data", function() {
18 expect(3);
19 var handler = function(event) {
20 ok( event.data, "bind() with data, check passed data exists" );
21 equals( event.data.foo, "bar", "bind() with data, Check value of passed data" );
23 jQuery("#firstp").bind("click", {foo: "bar"}, handler).click().unbind("click", handler);
25 ok( !jQuery._data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
26 });
28 test("click(), with data", function() {
29 expect(3);
30 var handler = function(event) {
31 ok( event.data, "bind() with data, check passed data exists" );
32 equals( event.data.foo, "bar", "bind() with data, Check value of passed data" );
34 jQuery("#firstp").click({foo: "bar"}, handler).click().unbind("click", handler);
36 ok( !jQuery._data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
37 });
39 test("bind(), with data, trigger with data", function() {
40 expect(4);
41 var handler = function(event, data) {
42 ok( event.data, "check passed data exists" );
43 equals( event.data.foo, "bar", "Check value of passed data" );
44 ok( data, "Check trigger data" );
45 equals( data.bar, "foo", "Check value of trigger data" );
47 jQuery("#firstp").bind("click", {foo: "bar"}, handler).trigger("click", [{bar: "foo"}]).unbind("click", handler);
48 });
50 test("bind(), multiple events at once", function() {
51 expect(2);
52 var clickCounter = 0,
53 mouseoverCounter = 0;
54 var handler = function(event) {
55 if (event.type == "click")
56 clickCounter += 1;
57 else if (event.type == "mouseover")
58 mouseoverCounter += 1;
60 jQuery("#firstp").bind("click mouseover", handler).trigger("click").trigger("mouseover");
61 equals( clickCounter, 1, "bind() with multiple events at once" );
62 equals( mouseoverCounter, 1, "bind() with multiple events at once" );
63 });
65 test("bind(), multiple events at once and namespaces", function() {
66 expect(7);
68 var cur, obj = {};
70 var div = jQuery("<div/>").bind("focusin.a", function(e) {
71 equals( e.type, cur, "Verify right single event was fired." );
72 });
74 cur = "focusin";
75 div.trigger("focusin.a");
77 // manually clean up detached elements
78 div.remove();
80 div = jQuery("<div/>").bind("click mouseover", obj, function(e) {
81 equals( e.type, cur, "Verify right multi event was fired." );
82 equals( e.data, obj, "Make sure the data came in correctly." );
83 });
85 cur = "click";
86 div.trigger("click");
88 cur = "mouseover";
89 div.trigger("mouseover");
91 // manually clean up detached elements
92 div.remove();
94 div = jQuery("<div/>").bind("focusin.a focusout.b", function(e) {
95 equals( e.type, cur, "Verify right multi event was fired." );
96 });
98 cur = "focusin";
99 div.trigger("focusin.a");
101 cur = "focusout";
102 div.trigger("focusout.b");
104 // manually clean up detached elements
105 div.remove();
108 test("bind(), namespace with special add", function() {
109 expect(24);
111 var div = jQuery("<div/>").bind("test", function(e) {
112 ok( true, "Test event fired." );
115 var i = 0;
117 jQuery.event.special.test = {
118 _default: function(e) {
119 equals( this, document, "Make sure we're at the top of the chain." );
120 equals( e.type, "test", "And that we're still dealing with a test event." );
121 equals( e.target, div[0], "And that the target is correct." );
123 setup: function(){},
124 teardown: function(){
125 ok(true, "Teardown called.");
127 add: function( handleObj ) {
128 var handler = handleObj.handler;
129 handleObj.handler = function(e) {
130 e.xyz = ++i;
131 handler.apply( this, arguments );
134 remove: function() {
135 ok(true, "Remove called.");
139 div.bind("test.a", {x: 1}, function(e) {
140 ok( !!e.xyz, "Make sure that the data is getting passed through." );
141 equals( e.data.x, 1, "Make sure data is attached properly." );
144 div.bind("test.b", {x: 2}, function(e) {
145 ok( !!e.xyz, "Make sure that the data is getting passed through." );
146 equals( e.data.x, 2, "Make sure data is attached properly." );
149 // Should trigger 5
150 div.trigger("test");
152 // Should trigger 2
153 div.trigger("test.a");
155 // Should trigger 2
156 div.trigger("test.b");
158 // Should trigger 4
159 div.unbind("test");
161 div = jQuery("<div/>").bind("test", function(e) {
162 ok( true, "Test event fired." );
165 // Should trigger 2
166 div.appendTo("#main").remove();
168 delete jQuery.event.special.test;
171 test("bind(), no data", function() {
172 expect(1);
173 var handler = function(event) {
174 ok ( !event.data, "Check that no data is added to the event object" );
176 jQuery("#firstp").bind("click", handler).trigger("click");
179 test("bind/one/unbind(Object)", function(){
180 expect(6);
182 var clickCounter = 0, mouseoverCounter = 0;
183 function handler(event) {
184 if (event.type == "click")
185 clickCounter++;
186 else if (event.type == "mouseover")
187 mouseoverCounter++;
190 function handlerWithData(event) {
191 if (event.type == "click")
192 clickCounter += event.data;
193 else if (event.type == "mouseover")
194 mouseoverCounter += event.data;
197 function trigger(){
198 $elem.trigger("click").trigger("mouseover");
201 var $elem = jQuery("#firstp")
202 // Regular bind
203 .bind({
204 click:handler,
205 mouseover:handler
207 // Bind with data
208 .one({
209 click:handlerWithData,
210 mouseover:handlerWithData
211 }, 2 );
213 trigger();
215 equals( clickCounter, 3, "bind(Object)" );
216 equals( mouseoverCounter, 3, "bind(Object)" );
218 trigger();
219 equals( clickCounter, 4, "bind(Object)" );
220 equals( mouseoverCounter, 4, "bind(Object)" );
222 jQuery("#firstp").unbind({
223 click:handler,
224 mouseover:handler
227 trigger();
228 equals( clickCounter, 4, "bind(Object)" );
229 equals( mouseoverCounter, 4, "bind(Object)" );
232 test("live/die(Object), delegate/undelegate(String, Object)", function() {
233 expect(6);
235 var clickCounter = 0, mouseoverCounter = 0,
236 $p = jQuery("#firstp"), $a = $p.find("a:first");
238 var events = {
239 click: function( event ) {
240 clickCounter += ( event.data || 1 );
242 mouseover: function( event ) {
243 mouseoverCounter += ( event.data || 1 );
247 function trigger() {
248 $a.trigger("click").trigger("mouseover");
251 $a.live( events );
252 $p.delegate( "a", events, 2 );
254 trigger();
255 equals( clickCounter, 3, "live/delegate" );
256 equals( mouseoverCounter, 3, "live/delegate" );
258 $p.undelegate( "a", events );
260 trigger();
261 equals( clickCounter, 4, "undelegate" );
262 equals( mouseoverCounter, 4, "undelegate" );
264 $a.die( events );
266 trigger();
267 equals( clickCounter, 4, "die" );
268 equals( mouseoverCounter, 4, "die" );
271 test("live/delegate immediate propagation", function() {
272 expect(2);
274 var $p = jQuery("#firstp"), $a = $p.find("a:first"), lastClick;
276 lastClick = "";
277 $a.live( "click", function(e) {
278 lastClick = "click1";
279 e.stopImmediatePropagation();
281 $a.live( "click", function(e) {
282 lastClick = "click2";
284 $a.trigger( "click" );
285 equals( lastClick, "click1", "live stopImmediatePropagation" );
286 $a.die( "click" );
288 lastClick = "";
289 $p.delegate( "a", "click", function(e) {
290 lastClick = "click1";
291 e.stopImmediatePropagation();
293 $p.delegate( "a", "click", function(e) {
294 lastClick = "click2";
296 $a.trigger( "click" );
297 equals( lastClick, "click1", "delegate stopImmediatePropagation" );
298 $p.undelegate( "click" );
301 test("bind/delegate bubbling, isDefaultPrevented", function() {
302 expect(2);
303 var $anchor2 = jQuery( "#anchor2" ),
304 $main = jQuery( "#main" ),
305 fakeClick = function($jq) {
306 // Use a native click so we don't get jQuery simulated bubbling
307 if ( document.createEvent ) {
308 var e = document.createEvent( 'MouseEvents' );
309 e.initEvent( "click", true, true );
310 $jq[0].dispatchEvent(e);
312 else if ( $jq[0].click ) {
313 $jq[0].click(); // IE
316 $anchor2.click(function(e) {
317 e.preventDefault();
319 $main.delegate("#foo", "click", function(e) {
320 var orig = e.originalEvent;
322 if ( typeof(orig.defaultPrevented) === "boolean" || typeof(orig.returnValue) === "boolean" || orig.getPreventDefault ) {
323 equals( e.isDefaultPrevented(), true, "isDefaultPrevented true passed to bubbled event" );
325 } else {
326 // Opera < 11 doesn't implement any interface we can use, so give it a pass
327 ok( true, "isDefaultPrevented not supported by this browser, test skipped" );
330 fakeClick( $anchor2 );
331 $anchor2.unbind( "click" );
332 $main.undelegate( "click" );
333 $anchor2.click(function(e) {
334 // Let the default action occur
336 $main.delegate("#foo", "click", function(e) {
337 equals( e.isDefaultPrevented(), false, "isDefaultPrevented false passed to bubbled event" );
339 fakeClick( $anchor2 );
340 $anchor2.unbind( "click" );
341 $main.undelegate( "click" );
344 test("bind(), iframes", function() {
345 // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
346 var doc = jQuery("#loadediframe").contents();
348 jQuery("div", doc).bind("click", function() {
349 ok( true, "Binding to element inside iframe" );
350 }).click().unbind('click');
353 test("bind(), trigger change on select", function() {
354 expect(5);
355 var counter = 0;
356 function selectOnChange(event) {
357 equals( event.data, counter++, "Event.data is not a global event object" );
359 jQuery("#form select").each(function(i){
360 jQuery(this).bind('change', i, selectOnChange);
361 }).trigger('change');
364 test("bind(), namespaced events, cloned events", 18, function() {
365 var firstp = jQuery( "#firstp" );
367 firstp.bind("custom.test",function(e){
368 ok(false, "Custom event triggered");
371 firstp.bind("click",function(e){
372 ok(true, "Normal click triggered");
373 equal( e.type + e.namespace, "click", "Check that only click events trigger this fn" );
376 firstp.bind("click.test",function(e){
377 var check = "click";
378 ok( true, "Namespaced click triggered" );
379 if ( e.namespace ) {
380 check += "test";
382 equal( e.type + e.namespace, check, "Check that only click/click.test events trigger this fn" );
385 //clone(true) element to verify events are cloned correctly
386 firstp = firstp.add( firstp.clone( true ).attr( "id", "firstp2" ).insertBefore( firstp ) );
388 // Trigger both bound fn (8)
389 firstp.trigger("click");
391 // Trigger one bound fn (4)
392 firstp.trigger("click.test");
394 // Remove only the one fn
395 firstp.unbind("click.test");
397 // Trigger the remaining fn (4)
398 firstp.trigger("click");
400 // Remove the remaining namespaced fn
401 firstp.unbind(".test");
403 // Try triggering the custom event (0)
404 firstp.trigger("custom");
406 // using contents will get comments regular, text, and comment nodes
407 jQuery("#nonnodes").contents().bind("tester", function () {
408 equals(this.nodeType, 1, "Check node,textnode,comment bind just does real nodes" );
409 }).trigger("tester");
411 // Make sure events stick with appendTo'd elements (which are cloned) #2027
412 jQuery("<a href='#fail' class='test'>test</a>").click(function(){ return false; }).appendTo("#main");
413 ok( jQuery("a.test:first").triggerHandler("click") === false, "Handler is bound to appendTo'd elements" );
416 test("bind(), multi-namespaced events", function() {
417 expect(6);
419 var order = [
420 "click.test.abc",
421 "click.test.abc",
422 "click.test",
423 "click.test.abc",
424 "click.test",
425 "custom.test2"
428 function check(name, msg){
429 same(name, order.shift(), msg);
432 jQuery("#firstp").bind("custom.test",function(e){
433 check("custom.test", "Custom event triggered");
436 jQuery("#firstp").bind("custom.test2",function(e){
437 check("custom.test2", "Custom event triggered");
440 jQuery("#firstp").bind("click.test",function(e){
441 check("click.test", "Normal click triggered");
444 jQuery("#firstp").bind("click.test.abc",function(e){
445 check("click.test.abc", "Namespaced click triggered");
448 // Those would not trigger/unbind (#5303)
449 jQuery("#firstp").trigger("click.a.test");
450 jQuery("#firstp").unbind("click.a.test");
452 // Trigger both bound fn (1)
453 jQuery("#firstp").trigger("click.test.abc");
455 // Trigger one bound fn (1)
456 jQuery("#firstp").trigger("click.abc");
458 // Trigger two bound fn (2)
459 jQuery("#firstp").trigger("click.test");
461 // Remove only the one fn
462 jQuery("#firstp").unbind("click.abc");
464 // Trigger the remaining fn (1)
465 jQuery("#firstp").trigger("click");
467 // Remove the remaining fn
468 jQuery("#firstp").unbind(".test");
470 // Trigger the remaining fn (1)
471 jQuery("#firstp").trigger("custom");
474 test("bind(), with same function", function() {
475 expect(2)
477 var count = 0, func = function(){
478 count++;
481 jQuery("#liveHandlerOrder").bind("foo.bar", func).bind("foo.zar", func);
482 jQuery("#liveHandlerOrder").trigger("foo.bar");
484 equals(count, 1, "Verify binding function with multiple namespaces." );
486 jQuery("#liveHandlerOrder").unbind("foo.bar", func).unbind("foo.zar", func);
487 jQuery("#liveHandlerOrder").trigger("foo.bar");
489 equals(count, 1, "Verify that removing events still work." );
492 test("bind(), make sure order is maintained", function() {
493 expect(1);
495 var elem = jQuery("#firstp"), log = [], check = [];
497 for ( var i = 0; i < 100; i++ ) (function(i){
498 elem.bind( "click", function(){
499 log.push( i );
502 check.push( i );
503 })(i);
505 elem.trigger("click");
507 equals( log.join(","), check.join(","), "Make sure order was maintained." );
509 elem.unbind("click");
512 test("bind(), with different this object", function() {
513 expect(4);
514 var thisObject = { myThis: true },
515 data = { myData: true },
516 handler1 = function( event ) {
517 equals( this, thisObject, "bind() with different this object" );
519 handler2 = function( event ) {
520 equals( this, thisObject, "bind() with different this object and data" );
521 equals( event.data, data, "bind() with different this object and data" );
524 jQuery("#firstp")
525 .bind("click", jQuery.proxy(handler1, thisObject)).click().unbind("click", handler1)
526 .bind("click", data, jQuery.proxy(handler2, thisObject)).click().unbind("click", handler2);
528 ok( !jQuery._data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
531 test("bind(name, false), unbind(name, false)", function() {
532 expect(3);
534 var main = 0;
535 jQuery("#main").bind("click", function(e){ main++; });
536 jQuery("#ap").trigger("click");
537 equals( main, 1, "Verify that the trigger happened correctly." );
539 main = 0;
540 jQuery("#ap").bind("click", false);
541 jQuery("#ap").trigger("click");
542 equals( main, 0, "Verify that no bubble happened." );
544 main = 0;
545 jQuery("#ap").unbind("click", false);
546 jQuery("#ap").trigger("click");
547 equals( main, 1, "Verify that the trigger happened correctly." );
549 // manually clean up events from elements outside the fixture
550 jQuery("#main").unbind("click");
553 test("bind()/trigger()/unbind() on plain object", function() {
554 expect( 7 );
556 var obj = {};
558 // Make sure it doesn't complain when no events are found
559 jQuery(obj).trigger("test");
561 // Make sure it doesn't complain when no events are found
562 jQuery(obj).unbind("test");
564 jQuery(obj).bind({
565 test: function() {
566 ok( true, "Custom event run." );
568 submit: function() {
569 ok( true, "Custom submit event run." );
573 var events = jQuery._data(obj, "events");
574 ok( events, "Object has events bound." );
575 equals( obj.events, undefined, "Events object on plain objects is not events" );
576 equals( obj.test, undefined, "Make sure that test event is not on the plain object." );
577 equals( obj.handle, undefined, "Make sure that the event handler is not on the plain object." );
579 // Should trigger 1
580 jQuery(obj).trigger("test");
581 jQuery(obj).trigger("submit");
583 jQuery(obj).unbind("test");
584 jQuery(obj).unbind("submit");
586 // Should trigger 0
587 jQuery(obj).trigger("test");
589 // Make sure it doesn't complain when no events are found
590 jQuery(obj).unbind("test");
592 equals( obj && obj[ jQuery.expando ] &&
593 obj[ jQuery.expando ][ jQuery.expando ] &&
594 obj[ jQuery.expando ][ jQuery.expando ].events, undefined, "Make sure events object is removed" );
597 test("unbind(type)", function() {
598 expect( 0 );
600 var $elem = jQuery("#firstp"),
601 message;
603 function error(){
604 ok( false, message );
607 message = "unbind passing function";
608 $elem.bind('error1', error).unbind('error1',error).triggerHandler('error1');
610 message = "unbind all from event";
611 $elem.bind('error1', error).unbind('error1').triggerHandler('error1');
613 message = "unbind all";
614 $elem.bind('error1', error).unbind().triggerHandler('error1');
616 message = "unbind many with function";
617 $elem.bind('error1 error2',error)
618 .unbind('error1 error2', error )
619 .trigger('error1').triggerHandler('error2');
621 message = "unbind many"; // #3538
622 $elem.bind('error1 error2',error)
623 .unbind('error1 error2')
624 .trigger('error1').triggerHandler('error2');
626 message = "unbind without a type or handler";
627 $elem.bind("error1 error2.test",error)
628 .unbind()
629 .trigger("error1").triggerHandler("error2");
632 test("unbind(eventObject)", function() {
633 expect(4);
635 var $elem = jQuery("#firstp"),
636 num;
638 function assert( expected ){
639 num = 0;
640 $elem.trigger('foo').triggerHandler('bar');
641 equals( num, expected, "Check the right handlers are triggered" );
644 $elem
645 // This handler shouldn't be unbound
646 .bind('foo', function(){
647 num += 1;
649 .bind('foo', function(e){
650 $elem.unbind( e )
651 num += 2;
653 // Neither this one
654 .bind('bar', function(){
655 num += 4;
658 assert( 7 );
659 assert( 5 );
661 $elem.unbind('bar');
662 assert( 1 );
664 $elem.unbind();
665 assert( 0 );
668 test("hover()", function() {
669 var times = 0,
670 handler1 = function( event ) { ++times; },
671 handler2 = function( event ) { ++times; };
673 jQuery("#firstp")
674 .hover(handler1, handler2)
675 .mouseenter().mouseleave()
676 .unbind("mouseenter", handler1)
677 .unbind("mouseleave", handler2)
678 .hover(handler1)
679 .mouseenter().mouseleave()
680 .unbind("mouseenter mouseleave", handler1)
681 .mouseenter().mouseleave();
683 equals( times, 4, "hover handlers fired" );
686 test("trigger() shortcuts", function() {
687 expect(6);
689 var elem = jQuery('<li><a href="#">Change location</a></li>').prependTo('#firstUL');
690 elem.find('a').bind('click', function() {
691 var close = jQuery('spanx', this); // same with jQuery(this).find('span');
692 equals( close.length, 0, "Context element does not exist, length must be zero" );
693 ok( !close[0], "Context element does not exist, direct access to element must return undefined" );
694 return false;
695 }).click();
697 // manually clean up detached elements
698 elem.remove();
700 jQuery("#check1").click(function() {
701 ok( true, "click event handler for checkbox gets fired twice, see #815" );
702 }).click();
704 var counter = 0;
705 jQuery('#firstp')[0].onclick = function(event) {
706 counter++;
708 jQuery('#firstp').click();
709 equals( counter, 1, "Check that click, triggers onclick event handler also" );
711 var clickCounter = 0;
712 jQuery('#simon1')[0].onclick = function(event) {
713 clickCounter++;
715 jQuery('#simon1').click();
716 equals( clickCounter, 1, "Check that click, triggers onclick event handler on an a tag also" );
718 elem = jQuery('<img />').load(function(){
719 ok( true, "Trigger the load event, using the shortcut .load() (#2819)");
720 }).load();
722 // manually clean up detached elements
723 elem.remove();
726 test("trigger() bubbling", function() {
727 expect(14);
729 var doc = 0, html = 0, body = 0, main = 0, ap = 0;
731 jQuery(document).bind("click", function(e){ if ( e.target !== document) { doc++; } });
732 jQuery("html").bind("click", function(e){ html++; });
733 jQuery("body").bind("click", function(e){ body++; });
734 jQuery("#main").bind("click", function(e){ main++; });
735 jQuery("#ap").bind("click", function(){ ap++; return false; });
737 jQuery("html").trigger("click");
738 equals( doc, 1, "HTML bubble" );
739 equals( html, 1, "HTML bubble" );
741 jQuery("body").trigger("click");
742 equals( doc, 2, "Body bubble" );
743 equals( html, 2, "Body bubble" );
744 equals( body, 1, "Body bubble" );
746 jQuery("#main").trigger("click");
747 equals( doc, 3, "Main bubble" );
748 equals( html, 3, "Main bubble" );
749 equals( body, 2, "Main bubble" );
750 equals( main, 1, "Main bubble" );
752 jQuery("#ap").trigger("click");
753 equals( doc, 3, "ap bubble" );
754 equals( html, 3, "ap bubble" );
755 equals( body, 2, "ap bubble" );
756 equals( main, 1, "ap bubble" );
757 equals( ap, 1, "ap bubble" );
759 // manually clean up events from elements outside the fixture
760 jQuery(document).unbind("click");
761 jQuery("html, body, #main").unbind("click");
764 test("trigger(type, [data], [fn])", function() {
765 expect(14);
767 var handler = function(event, a, b, c) {
768 equals( event.type, "click", "check passed data" );
769 equals( a, 1, "check passed data" );
770 equals( b, "2", "check passed data" );
771 equals( c, "abc", "check passed data" );
772 return "test";
775 var $elem = jQuery("#firstp");
777 // Simulate a "native" click
778 $elem[0].click = function(){
779 ok( true, "Native call was triggered" );
782 // Triggers handlrs and native
783 // Trigger 5
784 $elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
786 // Simulate a "native" click
787 $elem[0].click = function(){
788 ok( false, "Native call was triggered" );
791 // Trigger only the handlers (no native)
792 // Triggers 5
793 equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
795 var pass = true;
796 try {
797 jQuery('#form input:first').hide().trigger('focus');
798 } catch(e) {
799 pass = false;
801 ok( pass, "Trigger focus on hidden element" );
803 pass = true;
804 try {
805 jQuery('#main table:first').bind('test:test', function(){}).trigger('test:test');
806 } catch (e) {
807 pass = false;
809 ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
811 var form = jQuery("<form action=''></form>").appendTo("body");
813 // Make sure it can be prevented locally
814 form.submit(function(){
815 ok( true, "Local bind still works." );
816 return false;
819 // Trigger 1
820 form.trigger("submit");
822 form.unbind("submit");
824 jQuery(document).submit(function(){
825 ok( true, "Make sure bubble works up to document." );
826 return false;
829 // Trigger 1
830 form.trigger("submit");
832 jQuery(document).unbind("submit");
834 form.remove();
837 test("jQuery.Event.currentTarget", function(){
840 test("trigger(eventObject, [data], [fn])", function() {
841 expect(25);
843 var $parent = jQuery('<div id="par" />').hide().appendTo('body'),
844 $child = jQuery('<p id="child">foo</p>').appendTo( $parent );
846 var event = jQuery.Event("noNew");
847 ok( event != window, "Instantiate jQuery.Event without the 'new' keyword" );
848 equals( event.type, "noNew", "Verify its type" );
850 equals( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
851 equals( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
852 equals( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
854 event.preventDefault();
855 equals( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
856 event.stopPropagation();
857 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
859 event.isPropagationStopped = function(){ return false };
860 event.stopImmediatePropagation();
861 equals( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
862 equals( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
864 $parent.bind('foo',function(e){
865 // Tries bubbling
866 equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
867 equals( e.target.id, 'child', 'Verify event.target when passed passing an event object' );
868 equals( e.currentTarget.id, 'par', 'Verify event.target when passed passing an event object' );
869 equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
872 // test with an event object
873 event = new jQuery.Event("foo");
874 event.secret = 'boo!';
875 $child.trigger(event);
877 // test with a literal object
878 $child.trigger({type:'foo', secret:'boo!'});
880 $parent.unbind();
882 function error(){
883 ok( false, "This assertion shouldn't be reached");
886 $parent.bind('foo', error );
888 $child.bind('foo',function(e, a, b, c ){
889 equals( arguments.length, 4, "Check arguments length");
890 equals( a, 1, "Check first custom argument");
891 equals( b, 2, "Check second custom argument");
892 equals( c, 3, "Check third custom argument");
894 equals( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
895 equals( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
896 equals( e.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
898 // Skips both errors
899 e.stopImmediatePropagation();
901 return "result";
904 // We should add this back in when we want to test the order
905 // in which event handlers are iterated.
906 //$child.bind('foo', error );
908 event = new jQuery.Event("foo");
909 $child.trigger( event, [1,2,3] ).unbind();
910 equals( event.result, "result", "Check event.result attribute");
912 // Will error if it bubbles
913 $child.triggerHandler('foo');
915 $child.unbind();
916 $parent.unbind().remove();
919 test("jQuery.Event.currentTarget", function(){
920 expect(1);
922 var counter = 0,
923 $elem = jQuery('<button>a</button>').click(function(e){
924 equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
927 // Fake event
928 $elem.trigger('click');
930 // Cleanup
931 $elem.unbind();
934 test("toggle(Function, Function, ...)", function() {
935 expect(16);
937 var count = 0,
938 fn1 = function(e) { count++; },
939 fn2 = function(e) { count--; },
940 preventDefault = function(e) { e.preventDefault() },
941 link = jQuery('#mark');
942 link.click(preventDefault).click().toggle(fn1, fn2).click().click().click().click().click();
943 equals( count, 1, "Check for toggle(fn, fn)" );
945 jQuery("#firstp").toggle(function () {
946 equals(arguments.length, 4, "toggle correctly passes through additional triggered arguments, see #1701" )
947 }, function() {}).trigger("click", [ 1, 2, 3 ]);
949 var first = 0;
950 jQuery("#simon1").one("click", function() {
951 ok( true, "Execute event only once" );
952 jQuery(this).toggle(function() {
953 equals( first++, 0, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
954 }, function() {
955 equals( first, 1, "toggle(Function,Function) assigned from within one('xxx'), see #1054" );
957 return false;
958 }).click().click().click();
960 var turn = 0;
961 var fns = [
962 function(){
963 turn = 1;
965 function(){
966 turn = 2;
968 function(){
969 turn = 3;
973 var $div = jQuery("<div>&nbsp;</div>").toggle( fns[0], fns[1], fns[2] );
974 $div.click();
975 equals( turn, 1, "Trying toggle with 3 functions, attempt 1 yields 1");
976 $div.click();
977 equals( turn, 2, "Trying toggle with 3 functions, attempt 2 yields 2");
978 $div.click();
979 equals( turn, 3, "Trying toggle with 3 functions, attempt 3 yields 3");
980 $div.click();
981 equals( turn, 1, "Trying toggle with 3 functions, attempt 4 yields 1");
982 $div.click();
983 equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
985 $div.unbind('click',fns[0]);
986 var data = jQuery._data( $div[0], 'events' );
987 ok( !data, "Unbinding one function from toggle unbinds them all");
989 // manually clean up detached elements
990 $div.remove();
992 // Test Multi-Toggles
993 var a = [], b = [];
994 $div = jQuery("<div/>");
995 $div.toggle(function(){ a.push(1); }, function(){ a.push(2); });
996 $div.click();
997 same( a, [1], "Check that a click worked." );
999 $div.toggle(function(){ b.push(1); }, function(){ b.push(2); });
1000 $div.click();
1001 same( a, [1,2], "Check that a click worked with a second toggle." );
1002 same( b, [1], "Check that a click worked with a second toggle." );
1004 $div.click();
1005 same( a, [1,2,1], "Check that a click worked with a second toggle, second click." );
1006 same( b, [1,2], "Check that a click worked with a second toggle, second click." );
1008 // manually clean up detached elements
1009 $div.remove();
1012 test(".live()/.die()", function() {
1013 expect(66);
1015 var submit = 0, div = 0, livea = 0, liveb = 0;
1017 jQuery("div").live("submit", function(){ submit++; return false; });
1018 jQuery("div").live("click", function(){ div++; });
1019 jQuery("div#nothiddendiv").live("click", function(){ livea++; });
1020 jQuery("div#nothiddendivchild").live("click", function(){ liveb++; });
1022 // Nothing should trigger on the body
1023 jQuery("body").trigger("click");
1024 equals( submit, 0, "Click on body" );
1025 equals( div, 0, "Click on body" );
1026 equals( livea, 0, "Click on body" );
1027 equals( liveb, 0, "Click on body" );
1029 // This should trigger two events
1030 submit = 0, div = 0, livea = 0, liveb = 0;
1031 jQuery("div#nothiddendiv").trigger("click");
1032 equals( submit, 0, "Click on div" );
1033 equals( div, 1, "Click on div" );
1034 equals( livea, 1, "Click on div" );
1035 equals( liveb, 0, "Click on div" );
1037 // This should trigger three events (w/ bubbling)
1038 submit = 0, div = 0, livea = 0, liveb = 0;
1039 jQuery("div#nothiddendivchild").trigger("click");
1040 equals( submit, 0, "Click on inner div" );
1041 equals( div, 2, "Click on inner div" );
1042 equals( livea, 1, "Click on inner div" );
1043 equals( liveb, 1, "Click on inner div" );
1045 // This should trigger one submit
1046 submit = 0, div = 0, livea = 0, liveb = 0;
1047 jQuery("div#nothiddendivchild").trigger("submit");
1048 equals( submit, 1, "Submit on div" );
1049 equals( div, 0, "Submit on div" );
1050 equals( livea, 0, "Submit on div" );
1051 equals( liveb, 0, "Submit on div" );
1053 // Make sure no other events were removed in the process
1054 submit = 0, div = 0, livea = 0, liveb = 0;
1055 jQuery("div#nothiddendivchild").trigger("click");
1056 equals( submit, 0, "die Click on inner div" );
1057 equals( div, 2, "die Click on inner div" );
1058 equals( livea, 1, "die Click on inner div" );
1059 equals( liveb, 1, "die Click on inner div" );
1061 // Now make sure that the removal works
1062 submit = 0, div = 0, livea = 0, liveb = 0;
1063 jQuery("div#nothiddendivchild").die("click");
1064 jQuery("div#nothiddendivchild").trigger("click");
1065 equals( submit, 0, "die Click on inner div" );
1066 equals( div, 2, "die Click on inner div" );
1067 equals( livea, 1, "die Click on inner div" );
1068 equals( liveb, 0, "die Click on inner div" );
1070 // Make sure that the click wasn't removed too early
1071 submit = 0, div = 0, livea = 0, liveb = 0;
1072 jQuery("div#nothiddendiv").trigger("click");
1073 equals( submit, 0, "die Click on inner div" );
1074 equals( div, 1, "die Click on inner div" );
1075 equals( livea, 1, "die Click on inner div" );
1076 equals( liveb, 0, "die Click on inner div" );
1078 // Make sure that stopPropgation doesn't stop live events
1079 submit = 0, div = 0, livea = 0, liveb = 0;
1080 jQuery("div#nothiddendivchild").live("click", function(e){ liveb++; e.stopPropagation(); });
1081 jQuery("div#nothiddendivchild").trigger("click");
1082 equals( submit, 0, "stopPropagation Click on inner div" );
1083 equals( div, 1, "stopPropagation Click on inner div" );
1084 equals( livea, 0, "stopPropagation Click on inner div" );
1085 equals( liveb, 1, "stopPropagation Click on inner div" );
1087 // Make sure click events only fire with primary click
1088 submit = 0, div = 0, livea = 0, liveb = 0;
1089 var event = jQuery.Event("click");
1090 event.button = 1;
1091 jQuery("div#nothiddendiv").trigger(event);
1093 equals( livea, 0, "live secondary click" );
1095 jQuery("div#nothiddendivchild").die("click");
1096 jQuery("div#nothiddendiv").die("click");
1097 jQuery("div").die("click");
1098 jQuery("div").die("submit");
1100 // Test binding with a different context
1101 var clicked = 0, container = jQuery('#main')[0];
1102 jQuery("#foo", container).live("click", function(e){ clicked++; });
1103 jQuery("div").trigger('click');
1104 jQuery("#foo").trigger('click');
1105 jQuery("#main").trigger('click');
1106 jQuery("body").trigger('click');
1107 equals( clicked, 2, "live with a context" );
1109 // Make sure the event is actually stored on the context
1110 ok( jQuery._data(container, "events").live, "live with a context" );
1112 // Test unbinding with a different context
1113 jQuery("#foo", container).die("click");
1114 jQuery("#foo").trigger('click');
1115 equals( clicked, 2, "die with a context");
1117 // Test binding with event data
1118 jQuery("#foo").live("click", true, function(e){ equals( e.data, true, "live with event data" ); });
1119 jQuery("#foo").trigger("click").die("click");
1121 // Test binding with trigger data
1122 jQuery("#foo").live("click", function(e, data){ equals( data, true, "live with trigger data" ); });
1123 jQuery("#foo").trigger("click", true).die("click");
1125 // Test binding with different this object
1126 jQuery("#foo").live("click", jQuery.proxy(function(e){ equals( this.foo, "bar", "live with event scope" ); }, { foo: "bar" }));
1127 jQuery("#foo").trigger("click").die("click");
1129 // Test binding with different this object, event data, and trigger data
1130 jQuery("#foo").live("click", true, jQuery.proxy(function(e, data){
1131 equals( e.data, true, "live with with different this object, event data, and trigger data" );
1132 equals( this.foo, "bar", "live with with different this object, event data, and trigger data" );
1133 equals( data, true, "live with with different this object, event data, and trigger data")
1134 }, { foo: "bar" }));
1135 jQuery("#foo").trigger("click", true).die("click");
1137 // Verify that return false prevents default action
1138 jQuery("#anchor2").live("click", function(){ return false; });
1139 var hash = window.location.hash;
1140 jQuery("#anchor2").trigger("click");
1141 equals( window.location.hash, hash, "return false worked" );
1142 jQuery("#anchor2").die("click");
1144 // Verify that .preventDefault() prevents default action
1145 jQuery("#anchor2").live("click", function(e){ e.preventDefault(); });
1146 var hash = window.location.hash;
1147 jQuery("#anchor2").trigger("click");
1148 equals( window.location.hash, hash, "e.preventDefault() worked" );
1149 jQuery("#anchor2").die("click");
1151 // Test binding the same handler to multiple points
1152 var called = 0;
1153 function callback(){ called++; return false; }
1155 jQuery("#nothiddendiv").live("click", callback);
1156 jQuery("#anchor2").live("click", callback);
1158 jQuery("#nothiddendiv").trigger("click");
1159 equals( called, 1, "Verify that only one click occurred." );
1161 called = 0;
1162 jQuery("#anchor2").trigger("click");
1163 equals( called, 1, "Verify that only one click occurred." );
1165 // Make sure that only one callback is removed
1166 jQuery("#anchor2").die("click", callback);
1168 called = 0;
1169 jQuery("#nothiddendiv").trigger("click");
1170 equals( called, 1, "Verify that only one click occurred." );
1172 called = 0;
1173 jQuery("#anchor2").trigger("click");
1174 equals( called, 0, "Verify that no click occurred." );
1176 // Make sure that it still works if the selector is the same,
1177 // but the event type is different
1178 jQuery("#nothiddendiv").live("foo", callback);
1180 // Cleanup
1181 jQuery("#nothiddendiv").die("click", callback);
1183 called = 0;
1184 jQuery("#nothiddendiv").trigger("click");
1185 equals( called, 0, "Verify that no click occurred." );
1187 called = 0;
1188 jQuery("#nothiddendiv").trigger("foo");
1189 equals( called, 1, "Verify that one foo occurred." );
1191 // Cleanup
1192 jQuery("#nothiddendiv").die("foo", callback);
1194 // Make sure we don't loose the target by DOM modifications
1195 // after the bubble already reached the liveHandler
1196 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1198 jQuery("#nothiddendivchild").live("click", function(e){ jQuery("#nothiddendivchild").html(''); });
1199 jQuery("#nothiddendivchild").live("click", function(e){ if(e.target) {livec++;} });
1201 jQuery("#nothiddendiv span").click();
1202 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1203 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1205 // Cleanup
1206 jQuery("#nothiddendivchild").die("click");
1208 // Verify that .live() ocurs and cancel buble in the same order as
1209 // we would expect .bind() and .click() without delegation
1210 var lived = 0, livee = 0;
1212 // bind one pair in one order
1213 jQuery('span#liveSpan1 a').live('click', function(){ lived++; return false; });
1214 jQuery('span#liveSpan1').live('click', function(){ livee++; });
1216 jQuery('span#liveSpan1 a').click();
1217 equals( lived, 1, "Verify that only one first handler occurred." );
1218 equals( livee, 0, "Verify that second handler doesn't." );
1220 // and one pair in inverse
1221 jQuery('span#liveSpan2').live('click', function(){ livee++; });
1222 jQuery('span#liveSpan2 a').live('click', function(){ lived++; return false; });
1224 lived = 0;
1225 livee = 0;
1226 jQuery('span#liveSpan2 a').click();
1227 equals( lived, 1, "Verify that only one first handler occurred." );
1228 equals( livee, 0, "Verify that second handler doesn't." );
1230 // Cleanup
1231 jQuery("span#liveSpan1 a").die("click")
1232 jQuery("span#liveSpan1").die("click");
1233 jQuery("span#liveSpan2 a").die("click");
1234 jQuery("span#liveSpan2").die("click");
1236 // Test this, target and currentTarget are correct
1237 jQuery('span#liveSpan1').live('click', function(e){
1238 equals( this.id, 'liveSpan1', 'Check the this within a live handler' );
1239 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a live handler' );
1240 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a live handler' );
1243 jQuery('span#liveSpan1 a').click();
1245 jQuery('span#liveSpan1').die('click');
1247 // Work with deep selectors
1248 livee = 0;
1250 function clickB(){ livee++; }
1252 jQuery("#nothiddendiv div").live("click", function(){ livee++; });
1253 jQuery("#nothiddendiv div").live("click", clickB);
1254 jQuery("#nothiddendiv div").live("mouseover", function(){ livee++; });
1256 equals( livee, 0, "No clicks, deep selector." );
1258 livee = 0;
1259 jQuery("#nothiddendivchild").trigger("click");
1260 equals( livee, 2, "Click, deep selector." );
1262 livee = 0;
1263 jQuery("#nothiddendivchild").trigger("mouseover");
1264 equals( livee, 1, "Mouseover, deep selector." );
1266 jQuery("#nothiddendiv div").die("mouseover");
1268 livee = 0;
1269 jQuery("#nothiddendivchild").trigger("click");
1270 equals( livee, 2, "Click, deep selector." );
1272 livee = 0;
1273 jQuery("#nothiddendivchild").trigger("mouseover");
1274 equals( livee, 0, "Mouseover, deep selector." );
1276 jQuery("#nothiddendiv div").die("click", clickB);
1278 livee = 0;
1279 jQuery("#nothiddendivchild").trigger("click");
1280 equals( livee, 1, "Click, deep selector." );
1282 jQuery("#nothiddendiv div").die("click");
1284 jQuery("#nothiddendiv div").live("blur", function(){
1285 ok( true, "Live div trigger blur." );
1288 jQuery("#nothiddendiv div").trigger("blur");
1290 jQuery("#nothiddendiv div").die("blur");
1293 test("die all bound events", function(){
1294 expect(1);
1296 var count = 0;
1297 var div = jQuery("div#nothiddendivchild");
1299 div.live("click submit", function(){ count++; });
1300 div.die();
1302 div.trigger("click");
1303 div.trigger("submit");
1305 equals( count, 0, "Make sure no events were triggered." );
1308 test("live with multiple events", function(){
1309 expect(1);
1311 var count = 0;
1312 var div = jQuery("div#nothiddendivchild");
1314 div.live("click submit", function(){ count++; });
1316 div.trigger("click");
1317 div.trigger("submit");
1319 equals( count, 2, "Make sure both the click and submit were triggered." );
1321 // manually clean up events from elements outside the fixture
1322 div.die();
1325 test("live with namespaces", function(){
1326 expect(12);
1328 var count1 = 0, count2 = 0;
1330 jQuery("#liveSpan1").live("foo.bar", function(e){
1331 count1++;
1334 jQuery("#liveSpan1").live("foo.zed", function(e){
1335 count2++;
1338 jQuery("#liveSpan1").trigger("foo.bar");
1339 equals( count1, 1, "Got live foo.bar" );
1340 equals( count2, 0, "Got live foo.bar" );
1342 count1 = 0, count2 = 0;
1344 jQuery("#liveSpan1").trigger("foo.zed");
1345 equals( count1, 0, "Got live foo.zed" );
1346 equals( count2, 1, "Got live foo.zed" );
1348 //remove one
1349 count1 = 0, count2 = 0;
1351 jQuery("#liveSpan1").die("foo.zed");
1352 jQuery("#liveSpan1").trigger("foo.bar");
1354 equals( count1, 1, "Got live foo.bar after dieing foo.zed" );
1355 equals( count2, 0, "Got live foo.bar after dieing foo.zed" );
1357 count1 = 0, count2 = 0;
1359 jQuery("#liveSpan1").trigger("foo.zed");
1360 equals( count1, 0, "Got live foo.zed" );
1361 equals( count2, 0, "Got live foo.zed" );
1363 //remove the other
1364 jQuery("#liveSpan1").die("foo.bar");
1366 count1 = 0, count2 = 0;
1368 jQuery("#liveSpan1").trigger("foo.bar");
1369 equals( count1, 0, "Did not respond to foo.bar after dieing it" );
1370 equals( count2, 0, "Did not respond to foo.bar after dieing it" );
1372 jQuery("#liveSpan1").trigger("foo.zed");
1373 equals( count1, 0, "Did not trigger foo.zed again" );
1374 equals( count2, 0, "Did not trigger foo.zed again" );
1377 test("live with change", function(){
1378 expect(8);
1380 var selectChange = 0, checkboxChange = 0;
1382 var select = jQuery("select[name='S1']")
1383 select.live("change", function() {
1384 selectChange++;
1387 var checkbox = jQuery("#check2"),
1388 checkboxFunction = function(){
1389 checkboxChange++;
1391 checkbox.live("change", checkboxFunction);
1393 // test click on select
1395 // second click that changed it
1396 selectChange = 0;
1397 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1398 select.trigger("change");
1399 equals( selectChange, 1, "Change on click." );
1401 // test keys on select
1402 selectChange = 0;
1403 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1404 select.trigger("change");
1405 equals( selectChange, 1, "Change on keyup." );
1407 // test click on checkbox
1408 checkbox.trigger("change");
1409 equals( checkboxChange, 1, "Change on checkbox." );
1411 // test blur/focus on text
1412 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1413 text.live("change", function() {
1414 textChange++;
1417 text.val(oldTextVal+"foo");
1418 text.trigger("change");
1419 equals( textChange, 1, "Change on text input." );
1421 text.val(oldTextVal);
1422 text.die("change");
1424 // test blur/focus on password
1425 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1426 password.live("change", function() {
1427 passwordChange++;
1430 password.val(oldPasswordVal + "foo");
1431 password.trigger("change");
1432 equals( passwordChange, 1, "Change on password input." );
1434 password.val(oldPasswordVal);
1435 password.die("change");
1437 // make sure die works
1439 // die all changes
1440 selectChange = 0;
1441 select.die("change");
1442 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1443 select.trigger("change");
1444 equals( selectChange, 0, "Die on click works." );
1446 selectChange = 0;
1447 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1448 select.trigger("change");
1449 equals( selectChange, 0, "Die on keyup works." );
1451 // die specific checkbox
1452 checkbox.die("change", checkboxFunction);
1453 checkbox.trigger("change");
1454 equals( checkboxChange, 1, "Die on checkbox." );
1457 test("live with submit", function() {
1458 expect(5);
1460 var count1 = 0, count2 = 0;
1462 jQuery("#testForm").live("submit", function(ev) {
1463 count1++;
1464 ev.preventDefault();
1467 jQuery("body").live("submit", function(ev) {
1468 count2++;
1469 ev.preventDefault();
1472 jQuery("#testForm input[name=sub1]").submit();
1473 equals( count1, 1, "Verify form submit." );
1474 equals( count2, 1, "Verify body submit." );
1476 jQuery("#testForm input[name=sub1]").live("click", function(ev) {
1477 ok( true, "cancelling submit still calls click handler" );
1480 jQuery("#testForm input[name=sub1]")[0].click();
1481 equals( count1, 2, "Verify form submit." );
1482 equals( count2, 2, "Verify body submit." );
1484 jQuery("#testForm").die("submit");
1485 jQuery("#testForm input[name=sub1]").die("click");
1486 jQuery("body").die("submit");
1489 test("live with special events", function() {
1490 expect(13);
1492 jQuery.event.special.foo = {
1493 setup: function( data, namespaces, handler ) {
1494 ok( true, "Setup run." );
1496 teardown: function( namespaces ) {
1497 ok( true, "Teardown run." );
1499 add: function( handleObj ) {
1500 ok( true, "Add run." );
1502 remove: function( handleObj ) {
1503 ok( true, "Remove run." );
1505 _default: function( event ) {
1506 ok( true, "Default run." );
1510 // Run: setup, add
1511 jQuery("#liveSpan1").live("foo.a", function(e){
1512 ok( true, "Handler 1 run." );
1515 // Run: add
1516 jQuery("#liveSpan1").live("foo.b", function(e){
1517 ok( true, "Handler 2 run." );
1520 // Run: Handler 1, Handler 2, Default
1521 jQuery("#liveSpan1").trigger("foo");
1523 // Run: Handler 1, Default
1524 // TODO: Namespace doesn't trigger default (?)
1525 jQuery("#liveSpan1").trigger("foo.a");
1527 // Run: remove
1528 jQuery("#liveSpan1").die("foo.a");
1530 // Run: Handler 2, Default
1531 jQuery("#liveSpan1").trigger("foo");
1533 // Run: remove, teardown
1534 jQuery("#liveSpan1").die("foo");
1536 delete jQuery.event.special.foo;
1539 test(".delegate()/.undelegate()", function() {
1540 expect(65);
1542 var submit = 0, div = 0, livea = 0, liveb = 0;
1544 jQuery("#body").delegate("div", "submit", function(){ submit++; return false; });
1545 jQuery("#body").delegate("div", "click", function(){ div++; });
1546 jQuery("#body").delegate("div#nothiddendiv", "click", function(){ livea++; });
1547 jQuery("#body").delegate("div#nothiddendivchild", "click", function(){ liveb++; });
1549 // Nothing should trigger on the body
1550 jQuery("body").trigger("click");
1551 equals( submit, 0, "Click on body" );
1552 equals( div, 0, "Click on body" );
1553 equals( livea, 0, "Click on body" );
1554 equals( liveb, 0, "Click on body" );
1556 // This should trigger two events
1557 submit = 0, div = 0, livea = 0, liveb = 0;
1558 jQuery("div#nothiddendiv").trigger("click");
1559 equals( submit, 0, "Click on div" );
1560 equals( div, 1, "Click on div" );
1561 equals( livea, 1, "Click on div" );
1562 equals( liveb, 0, "Click on div" );
1564 // This should trigger three events (w/ bubbling)
1565 submit = 0, div = 0, livea = 0, liveb = 0;
1566 jQuery("div#nothiddendivchild").trigger("click");
1567 equals( submit, 0, "Click on inner div" );
1568 equals( div, 2, "Click on inner div" );
1569 equals( livea, 1, "Click on inner div" );
1570 equals( liveb, 1, "Click on inner div" );
1572 // This should trigger one submit
1573 submit = 0, div = 0, livea = 0, liveb = 0;
1574 jQuery("div#nothiddendivchild").trigger("submit");
1575 equals( submit, 1, "Submit on div" );
1576 equals( div, 0, "Submit on div" );
1577 equals( livea, 0, "Submit on div" );
1578 equals( liveb, 0, "Submit on div" );
1580 // Make sure no other events were removed in the process
1581 submit = 0, div = 0, livea = 0, liveb = 0;
1582 jQuery("div#nothiddendivchild").trigger("click");
1583 equals( submit, 0, "undelegate Click on inner div" );
1584 equals( div, 2, "undelegate Click on inner div" );
1585 equals( livea, 1, "undelegate Click on inner div" );
1586 equals( liveb, 1, "undelegate Click on inner div" );
1588 // Now make sure that the removal works
1589 submit = 0, div = 0, livea = 0, liveb = 0;
1590 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1591 jQuery("div#nothiddendivchild").trigger("click");
1592 equals( submit, 0, "undelegate Click on inner div" );
1593 equals( div, 2, "undelegate Click on inner div" );
1594 equals( livea, 1, "undelegate Click on inner div" );
1595 equals( liveb, 0, "undelegate Click on inner div" );
1597 // Make sure that the click wasn't removed too early
1598 submit = 0, div = 0, livea = 0, liveb = 0;
1599 jQuery("div#nothiddendiv").trigger("click");
1600 equals( submit, 0, "undelegate Click on inner div" );
1601 equals( div, 1, "undelegate Click on inner div" );
1602 equals( livea, 1, "undelegate Click on inner div" );
1603 equals( liveb, 0, "undelegate Click on inner div" );
1605 // Make sure that stopPropgation doesn't stop live events
1606 submit = 0, div = 0, livea = 0, liveb = 0;
1607 jQuery("#body").delegate("div#nothiddendivchild", "click", function(e){ liveb++; e.stopPropagation(); });
1608 jQuery("div#nothiddendivchild").trigger("click");
1609 equals( submit, 0, "stopPropagation Click on inner div" );
1610 equals( div, 1, "stopPropagation Click on inner div" );
1611 equals( livea, 0, "stopPropagation Click on inner div" );
1612 equals( liveb, 1, "stopPropagation Click on inner div" );
1614 // Make sure click events only fire with primary click
1615 submit = 0, div = 0, livea = 0, liveb = 0;
1616 var event = jQuery.Event("click");
1617 event.button = 1;
1618 jQuery("div#nothiddendiv").trigger(event);
1620 equals( livea, 0, "delegate secondary click" );
1622 jQuery("#body").undelegate("div#nothiddendivchild", "click");
1623 jQuery("#body").undelegate("div#nothiddendiv", "click");
1624 jQuery("#body").undelegate("div", "click");
1625 jQuery("#body").undelegate("div", "submit");
1627 // Test binding with a different context
1628 var clicked = 0, container = jQuery('#main')[0];
1629 jQuery("#main").delegate("#foo", "click", function(e){ clicked++; });
1630 jQuery("div").trigger('click');
1631 jQuery("#foo").trigger('click');
1632 jQuery("#main").trigger('click');
1633 jQuery("body").trigger('click');
1634 equals( clicked, 2, "delegate with a context" );
1636 // Make sure the event is actually stored on the context
1637 ok( jQuery._data(container, "events").live, "delegate with a context" );
1639 // Test unbinding with a different context
1640 jQuery("#main").undelegate("#foo", "click");
1641 jQuery("#foo").trigger('click');
1642 equals( clicked, 2, "undelegate with a context");
1644 // Test binding with event data
1645 jQuery("#body").delegate("#foo", "click", true, function(e){ equals( e.data, true, "delegate with event data" ); });
1646 jQuery("#foo").trigger("click");
1647 jQuery("#body").undelegate("#foo", "click");
1649 // Test binding with trigger data
1650 jQuery("#body").delegate("#foo", "click", function(e, data){ equals( data, true, "delegate with trigger data" ); });
1651 jQuery("#foo").trigger("click", true);
1652 jQuery("#body").undelegate("#foo", "click");
1654 // Test binding with different this object
1655 jQuery("#body").delegate("#foo", "click", jQuery.proxy(function(e){ equals( this.foo, "bar", "delegate with event scope" ); }, { foo: "bar" }));
1656 jQuery("#foo").trigger("click");
1657 jQuery("#body").undelegate("#foo", "click");
1659 // Test binding with different this object, event data, and trigger data
1660 jQuery("#body").delegate("#foo", "click", true, jQuery.proxy(function(e, data){
1661 equals( e.data, true, "delegate with with different this object, event data, and trigger data" );
1662 equals( this.foo, "bar", "delegate with with different this object, event data, and trigger data" );
1663 equals( data, true, "delegate with with different this object, event data, and trigger data")
1664 }, { foo: "bar" }));
1665 jQuery("#foo").trigger("click", true);
1666 jQuery("#body").undelegate("#foo", "click");
1668 // Verify that return false prevents default action
1669 jQuery("#body").delegate("#anchor2", "click", function(){ return false; });
1670 var hash = window.location.hash;
1671 jQuery("#anchor2").trigger("click");
1672 equals( window.location.hash, hash, "return false worked" );
1673 jQuery("#body").undelegate("#anchor2", "click");
1675 // Verify that .preventDefault() prevents default action
1676 jQuery("#body").delegate("#anchor2", "click", function(e){ e.preventDefault(); });
1677 var hash = window.location.hash;
1678 jQuery("#anchor2").trigger("click");
1679 equals( window.location.hash, hash, "e.preventDefault() worked" );
1680 jQuery("#body").undelegate("#anchor2", "click");
1682 // Test binding the same handler to multiple points
1683 var called = 0;
1684 function callback(){ called++; return false; }
1686 jQuery("#body").delegate("#nothiddendiv", "click", callback);
1687 jQuery("#body").delegate("#anchor2", "click", callback);
1689 jQuery("#nothiddendiv").trigger("click");
1690 equals( called, 1, "Verify that only one click occurred." );
1692 called = 0;
1693 jQuery("#anchor2").trigger("click");
1694 equals( called, 1, "Verify that only one click occurred." );
1696 // Make sure that only one callback is removed
1697 jQuery("#body").undelegate("#anchor2", "click", callback);
1699 called = 0;
1700 jQuery("#nothiddendiv").trigger("click");
1701 equals( called, 1, "Verify that only one click occurred." );
1703 called = 0;
1704 jQuery("#anchor2").trigger("click");
1705 equals( called, 0, "Verify that no click occurred." );
1707 // Make sure that it still works if the selector is the same,
1708 // but the event type is different
1709 jQuery("#body").delegate("#nothiddendiv", "foo", callback);
1711 // Cleanup
1712 jQuery("#body").undelegate("#nothiddendiv", "click", callback);
1714 called = 0;
1715 jQuery("#nothiddendiv").trigger("click");
1716 equals( called, 0, "Verify that no click occurred." );
1718 called = 0;
1719 jQuery("#nothiddendiv").trigger("foo");
1720 equals( called, 1, "Verify that one foo occurred." );
1722 // Cleanup
1723 jQuery("#body").undelegate("#nothiddendiv", "foo", callback);
1725 // Make sure we don't loose the target by DOM modifications
1726 // after the bubble already reached the liveHandler
1727 var livec = 0, elemDiv = jQuery("#nothiddendivchild").html('<span></span>').get(0);
1729 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ jQuery("#nothiddendivchild").html(''); });
1730 jQuery("#body").delegate("#nothiddendivchild", "click", function(e){ if(e.target) {livec++;} });
1732 jQuery("#nothiddendiv span").click();
1733 equals( jQuery("#nothiddendiv span").length, 0, "Verify that first handler occurred and modified the DOM." );
1734 equals( livec, 1, "Verify that second handler occurred even with nuked target." );
1736 // Cleanup
1737 jQuery("#body").undelegate("#nothiddendivchild", "click");
1739 // Verify that .live() ocurs and cancel buble in the same order as
1740 // we would expect .bind() and .click() without delegation
1741 var lived = 0, livee = 0;
1743 // bind one pair in one order
1744 jQuery("#body").delegate('span#liveSpan1 a', 'click', function(){ lived++; return false; });
1745 jQuery("#body").delegate('span#liveSpan1', 'click', function(){ livee++; });
1747 jQuery('span#liveSpan1 a').click();
1748 equals( lived, 1, "Verify that only one first handler occurred." );
1749 equals( livee, 0, "Verify that second handler doesn't." );
1751 // and one pair in inverse
1752 jQuery("#body").delegate('span#liveSpan2', 'click', function(){ livee++; });
1753 jQuery("#body").delegate('span#liveSpan2 a', 'click', function(){ lived++; return false; });
1755 lived = 0;
1756 livee = 0;
1757 jQuery('span#liveSpan2 a').click();
1758 equals( lived, 1, "Verify that only one first handler occurred." );
1759 equals( livee, 0, "Verify that second handler doesn't." );
1761 // Cleanup
1762 jQuery("#body").undelegate("click");
1764 // Test this, target and currentTarget are correct
1765 jQuery("#body").delegate('span#liveSpan1', 'click', function(e){
1766 equals( this.id, 'liveSpan1', 'Check the this within a delegate handler' );
1767 equals( e.currentTarget.id, 'liveSpan1', 'Check the event.currentTarget within a delegate handler' );
1768 equals( e.target.nodeName.toUpperCase(), 'A', 'Check the event.target within a delegate handler' );
1771 jQuery('span#liveSpan1 a').click();
1773 jQuery("#body").undelegate('span#liveSpan1', 'click');
1775 // Work with deep selectors
1776 livee = 0;
1778 function clickB(){ livee++; }
1780 jQuery("#body").delegate("#nothiddendiv div", "click", function(){ livee++; });
1781 jQuery("#body").delegate("#nothiddendiv div", "click", clickB);
1782 jQuery("#body").delegate("#nothiddendiv div", "mouseover", function(){ livee++; });
1784 equals( livee, 0, "No clicks, deep selector." );
1786 livee = 0;
1787 jQuery("#nothiddendivchild").trigger("click");
1788 equals( livee, 2, "Click, deep selector." );
1790 livee = 0;
1791 jQuery("#nothiddendivchild").trigger("mouseover");
1792 equals( livee, 1, "Mouseover, deep selector." );
1794 jQuery("#body").undelegate("#nothiddendiv div", "mouseover");
1796 livee = 0;
1797 jQuery("#nothiddendivchild").trigger("click");
1798 equals( livee, 2, "Click, deep selector." );
1800 livee = 0;
1801 jQuery("#nothiddendivchild").trigger("mouseover");
1802 equals( livee, 0, "Mouseover, deep selector." );
1804 jQuery("#body").undelegate("#nothiddendiv div", "click", clickB);
1806 livee = 0;
1807 jQuery("#nothiddendivchild").trigger("click");
1808 equals( livee, 1, "Click, deep selector." );
1810 jQuery("#body").undelegate("#nothiddendiv div", "click");
1813 test("undelegate all bound events", function(){
1814 expect(1);
1816 var count = 0;
1817 var div = jQuery("#body");
1819 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1820 div.undelegate();
1822 jQuery("div#nothiddendivchild").trigger("click");
1823 jQuery("div#nothiddendivchild").trigger("submit");
1825 equals( count, 0, "Make sure no events were triggered." );
1828 test("delegate with multiple events", function(){
1829 expect(1);
1831 var count = 0;
1832 var div = jQuery("#body");
1834 div.delegate("div#nothiddendivchild", "click submit", function(){ count++; });
1836 jQuery("div#nothiddendivchild").trigger("click");
1837 jQuery("div#nothiddendivchild").trigger("submit");
1839 equals( count, 2, "Make sure both the click and submit were triggered." );
1841 jQuery("#body").undelegate();
1844 test("delegate with change", function(){
1845 expect(8);
1847 var selectChange = 0, checkboxChange = 0;
1849 var select = jQuery("select[name='S1']");
1850 jQuery("#body").delegate("select[name='S1']", "change", function() {
1851 selectChange++;
1854 var checkbox = jQuery("#check2"),
1855 checkboxFunction = function(){
1856 checkboxChange++;
1858 jQuery("#body").delegate("#check2", "change", checkboxFunction);
1860 // test click on select
1862 // second click that changed it
1863 selectChange = 0;
1864 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1865 select.trigger("change");
1866 equals( selectChange, 1, "Change on click." );
1868 // test keys on select
1869 selectChange = 0;
1870 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1871 select.trigger("change");
1872 equals( selectChange, 1, "Change on keyup." );
1874 // test click on checkbox
1875 checkbox.trigger("change");
1876 equals( checkboxChange, 1, "Change on checkbox." );
1878 // test blur/focus on text
1879 var text = jQuery("#name"), textChange = 0, oldTextVal = text.val();
1880 jQuery("#body").delegate("#name", "change", function() {
1881 textChange++;
1884 text.val(oldTextVal+"foo");
1885 text.trigger("change");
1886 equals( textChange, 1, "Change on text input." );
1888 text.val(oldTextVal);
1889 jQuery("#body").die("change");
1891 // test blur/focus on password
1892 var password = jQuery("#name"), passwordChange = 0, oldPasswordVal = password.val();
1893 jQuery("#body").delegate("#name", "change", function() {
1894 passwordChange++;
1897 password.val(oldPasswordVal + "foo");
1898 password.trigger("change");
1899 equals( passwordChange, 1, "Change on password input." );
1901 password.val(oldPasswordVal);
1902 jQuery("#body").undelegate("#name", "change");
1904 // make sure die works
1906 // die all changes
1907 selectChange = 0;
1908 jQuery("#body").undelegate("select[name='S1']", "change");
1909 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1910 select.trigger("change");
1911 equals( selectChange, 0, "Die on click works." );
1913 selectChange = 0;
1914 select[0].selectedIndex = select[0].selectedIndex ? 0 : 1;
1915 select.trigger("change");
1916 equals( selectChange, 0, "Die on keyup works." );
1918 // die specific checkbox
1919 jQuery("#body").undelegate("#check2", "change", checkboxFunction);
1920 checkbox.trigger("change");
1921 equals( checkboxChange, 1, "Die on checkbox." );
1924 test("delegate with submit", function() {
1925 var count1 = 0, count2 = 0;
1927 jQuery("#body").delegate("#testForm", "submit", function(ev) {
1928 count1++;
1929 ev.preventDefault();
1932 jQuery(document).delegate("body", "submit", function(ev) {
1933 count2++;
1934 ev.preventDefault();
1937 jQuery("#testForm input[name=sub1]").submit();
1938 equals( count1, 1, "Verify form submit." );
1939 equals( count2, 1, "Verify body submit." );
1941 jQuery("#body").undelegate();
1942 jQuery(document).undelegate();
1945 test("Non DOM element events", function() {
1946 expect(1);
1948 var o = {};
1950 jQuery(o).bind('nonelementobj', function(e) {
1951 ok( true, "Event on non-DOM object triggered" );
1954 jQuery(o).trigger('nonelementobj');
1957 test("window resize", function() {
1958 expect(2);
1960 jQuery(window).unbind();
1962 jQuery(window).bind("resize", function(){
1963 ok( true, "Resize event fired." );
1964 }).resize().unbind("resize");
1966 ok( !jQuery._data(window, "__events__"), "Make sure all the events are gone." );
1970 test("jQuery(function($) {})", function() {
1971 stop();
1972 jQuery(function($) {
1973 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");
1974 start();
1978 test("event properties", function() {
1979 stop();
1980 jQuery("#simon1").click(function(event) {
1981 ok( event.timeStamp, "assert event.timeStamp is present" );
1982 start();
1983 }).click();