2011-01-09 21:58:47 +00:00
module ( "event" , { teardown : moduleTeardown } ) ;
2006-11-18 13:37:01 +00:00
2010-10-24 16:18:33 +00:00
test ( "null or undefined handler" , function ( ) {
expect ( 2 ) ;
2011-02-10 02:15:32 +00:00
// Supports Fixes bug #7229
try {
jQuery ( "#firstp" ) . click ( null ) ;
ok ( true , "Passing a null handler will not throw an exception" ) ;
} catch ( e ) { }
try {
jQuery ( "#firstp" ) . click ( undefined ) ;
ok ( true , "Passing an undefined handler will not throw an exception" ) ;
} catch ( e ) { }
2010-10-24 16:18:33 +00:00
} ) ;
2011-05-16 14:38:36 +00:00
test ( "bind(),live(),delegate() with non-null,defined data" , function ( ) {
expect ( 3 ) ;
var handler = function ( event , data ) {
2011-10-28 18:17:14 +00:00
equal ( data , 0 , "non-null, defined data (zero) is correctly passed" ) ;
} ;
2011-05-16 14:38:36 +00:00
jQuery ( "#foo" ) . bind ( "foo" , handler ) ;
jQuery ( "#foo" ) . live ( "foo" , handler ) ;
jQuery ( "div" ) . delegate ( "#foo" , "foo" , handler ) ;
jQuery ( "#foo" ) . trigger ( "foo" , 0 ) ;
jQuery ( "#foo" ) . unbind ( "foo" , handler ) ;
jQuery ( "#foo" ) . die ( "foo" , handler ) ;
jQuery ( "div" ) . undelegate ( "#foo" , "foo" ) ;
} ) ;
2011-09-08 01:02:13 +00:00
test ( "Handler changes and .trigger() order" , function ( ) {
2011-10-28 18:17:14 +00:00
expect ( 1 ) ;
2011-09-08 01:02:13 +00:00
2011-10-28 18:17:14 +00:00
var markup = jQuery (
2012-07-02 15:30:22 +00:00
"<div><div><p><span><b class=\"a\">b</b></span></p></div></div>"
2011-10-28 18:17:14 +00:00
) ,
path = "" ;
2011-09-08 01:02:13 +00:00
2011-10-28 18:17:14 +00:00
markup
2011-10-13 20:30:40 +00:00
. find ( "*" ) . andSelf ( ) . on ( "click" , function ( e ) {
path += this . nodeName . toLowerCase ( ) + " " ;
} )
. filter ( "b" ) . on ( "click" , function ( e ) {
// Removing span should not stop propagation to original parents
if ( e . target === this ) {
jQuery ( this ) . parent ( ) . remove ( ) ;
}
} ) ;
2011-09-08 01:02:13 +00:00
2011-10-28 18:17:14 +00:00
markup . find ( "b" ) . trigger ( "click" ) ;
2011-09-08 01:02:13 +00:00
2011-11-06 20:27:42 +00:00
equal ( path , "b p div div " , "Delivered all events" ) ;
2011-09-20 16:44:49 +00:00
2011-10-28 18:17:14 +00:00
markup . remove ( ) ;
2011-09-08 01:02:13 +00:00
} ) ;
2008-01-14 09:33:08 +00:00
test ( "bind(), with data" , function ( ) {
2011-04-17 00:39:30 +00:00
expect ( 4 ) ;
2007-03-25 18:06:18 +00:00
var handler = function ( event ) {
ok ( event . data , "bind() with data, check passed data exists" ) ;
2012-07-05 19:52:13 +00:00
equal ( event . data [ "foo" ] , "bar" , "bind() with data, Check value of passed data" ) ;
2007-04-24 21:48:52 +00:00
} ;
2012-07-05 19:52:13 +00:00
jQuery ( "#firstp" ) . bind ( "click" , { "foo" : "bar" } , handler ) . click ( ) . unbind ( "click" , handler ) ;
2007-09-08 23:31:23 +00:00
2011-01-09 21:52:33 +00:00
ok ( ! jQuery . _data ( jQuery ( "#firstp" ) [ 0 ] , "events" ) , "Event handler unbound when using data." ) ;
2011-04-17 00:39:30 +00:00
var test = function ( ) { } ;
var handler2 = function ( event ) {
2011-11-06 20:27:42 +00:00
equal ( event . data , test , "bind() with function data, Check value of passed data" ) ;
2011-04-17 00:39:30 +00:00
} ;
jQuery ( "#firstp" ) . bind ( "click" , test , handler2 ) . click ( ) . unbind ( "click" , handler2 ) ;
2010-02-27 01:01:19 +00:00
} ) ;
test ( "click(), with data" , function ( ) {
expect ( 3 ) ;
var handler = function ( event ) {
ok ( event . data , "bind() with data, check passed data exists" ) ;
2012-07-05 19:52:13 +00:00
equal ( event . data [ "foo" ] , "bar" , "bind() with data, Check value of passed data" ) ;
2010-02-27 01:01:19 +00:00
} ;
2012-07-05 19:52:13 +00:00
jQuery ( "#firstp" ) . click ( { "foo" : "bar" } , handler ) . click ( ) . unbind ( "click" , handler ) ;
2010-02-27 01:01:19 +00:00
2011-01-09 21:52:33 +00:00
ok ( ! jQuery . _data ( jQuery ( "#firstp" ) [ 0 ] , "events" ) , "Event handler unbound when using data." ) ;
2008-01-14 09:33:08 +00:00
} ) ;
test ( "bind(), with data, trigger with data" , function ( ) {
expect ( 4 ) ;
2007-03-25 18:06:18 +00:00
var handler = function ( event , data ) {
ok ( event . data , "check passed data exists" ) ;
2011-11-06 20:27:42 +00:00
equal ( event . data . foo , "bar" , "Check value of passed data" ) ;
2007-03-25 18:06:18 +00:00
ok ( data , "Check trigger data" ) ;
2011-11-06 20:27:42 +00:00
equal ( data . bar , "foo" , "Check value of trigger data" ) ;
2007-04-24 21:48:52 +00:00
} ;
2008-05-28 23:18:25 +00:00
jQuery ( "#firstp" ) . bind ( "click" , { foo : "bar" } , handler ) . trigger ( "click" , [ { bar : "foo" } ] ) . unbind ( "click" , handler ) ;
2008-01-14 09:33:08 +00:00
} ) ;
test ( "bind(), multiple events at once" , function ( ) {
expect ( 2 ) ;
var clickCounter = 0 ,
mouseoverCounter = 0 ;
2007-12-15 05:55:33 +00:00
var handler = function ( event ) {
2012-06-21 19:30:24 +00:00
if ( event . type == "click" ) {
2007-12-15 05:55:33 +00:00
clickCounter += 1 ;
2012-06-21 19:30:24 +00:00
}
else if ( event . type == "mouseover" ) {
2007-12-15 05:55:33 +00:00
mouseoverCounter += 1 ;
2012-06-21 19:30:24 +00:00
}
2007-12-15 05:55:33 +00:00
} ;
2008-05-28 23:18:25 +00:00
jQuery ( "#firstp" ) . bind ( "click mouseover" , handler ) . trigger ( "click" ) . trigger ( "mouseover" ) ;
2011-11-06 20:27:42 +00:00
equal ( clickCounter , 1 , "bind() with multiple events at once" ) ;
equal ( mouseoverCounter , 1 , "bind() with multiple events at once" ) ;
2008-01-14 09:33:08 +00:00
} ) ;
2011-04-29 01:14:12 +00:00
test ( "bind(), five events at once" , function ( ) {
expect ( 1 ) ;
var count = 0 ,
2011-10-28 18:17:14 +00:00
handler = function ( event ) {
count ++ ;
} ;
2011-04-29 01:14:12 +00:00
jQuery ( "#firstp" ) . bind ( "click mouseover foo bar baz" , handler )
2011-10-28 18:17:14 +00:00
. trigger ( "click" ) . trigger ( "mouseover" )
. trigger ( "foo" ) . trigger ( "bar" )
. trigger ( "baz" ) ;
2011-04-29 01:14:12 +00:00
2011-11-06 20:27:42 +00:00
equal ( count , 5 , "bind() five events at once" ) ;
2011-04-29 01:14:12 +00:00
} ) ;
2010-01-25 21:45:39 +00:00
test ( "bind(), multiple events at once and namespaces" , function ( ) {
expect ( 7 ) ;
var cur , obj = { } ;
var div = jQuery ( "<div/>" ) . bind ( "focusin.a" , function ( e ) {
2011-11-06 20:27:42 +00:00
equal ( e . type , cur , "Verify right single event was fired." ) ;
2010-01-25 21:45:39 +00:00
} ) ;
cur = "focusin" ;
div . trigger ( "focusin.a" ) ;
2011-01-09 21:58:47 +00:00
// manually clean up detached elements
div . remove ( ) ;
2010-01-25 21:45:39 +00:00
div = jQuery ( "<div/>" ) . bind ( "click mouseover" , obj , function ( e ) {
2011-11-06 20:27:42 +00:00
equal ( e . type , cur , "Verify right multi event was fired." ) ;
equal ( e . data , obj , "Make sure the data came in correctly." ) ;
2010-01-25 21:45:39 +00:00
} ) ;
cur = "click" ;
div . trigger ( "click" ) ;
cur = "mouseover" ;
div . trigger ( "mouseover" ) ;
2011-01-09 21:58:47 +00:00
// manually clean up detached elements
div . remove ( ) ;
2010-01-25 21:45:39 +00:00
div = jQuery ( "<div/>" ) . bind ( "focusin.a focusout.b" , function ( e ) {
2011-11-06 20:27:42 +00:00
equal ( e . type , cur , "Verify right multi event was fired." ) ;
2010-01-25 21:45:39 +00:00
} ) ;
cur = "focusin" ;
div . trigger ( "focusin.a" ) ;
cur = "focusout" ;
div . trigger ( "focusout.b" ) ;
2011-01-09 21:58:47 +00:00
// manually clean up detached elements
div . remove ( ) ;
2010-01-25 21:45:39 +00:00
} ) ;
2010-01-25 22:01:07 +00:00
test ( "bind(), namespace with special add" , function ( ) {
2011-08-03 02:50:58 +00:00
expect ( 27 ) ;
2010-01-25 22:01:07 +00:00
var div = jQuery ( "<div/>" ) . bind ( "test" , function ( e ) {
ok ( true , "Test event fired." ) ;
} ) ;
var i = 0 ;
2012-07-05 19:52:13 +00:00
jQuery . event . special [ "test" ] = {
2011-08-03 02:50:58 +00:00
_default : function ( e , data ) {
2011-11-06 20:27:42 +00:00
equal ( this , document , "Make sure we're at the top of the chain." ) ;
equal ( e . type , "test" , "And that we're still dealing with a test event." ) ;
equal ( e . target , div [ 0 ] , "And that the target is correct." ) ;
2011-08-03 02:50:58 +00:00
ok ( data !== undefined , "And that trigger data was passed." ) ;
2010-01-28 19:34:09 +00:00
} ,
2010-01-25 22:01:07 +00:00
setup : function ( ) { } ,
2010-02-11 06:42:51 +00:00
teardown : function ( ) {
ok ( true , "Teardown called." ) ;
} ,
2010-02-04 05:20:52 +00:00
add : function ( handleObj ) {
var handler = handleObj . handler ;
handleObj . handler = function ( e ) {
2010-01-25 22:01:07 +00:00
e . xyz = ++ i ;
handler . apply ( this , arguments ) ;
} ;
} ,
2010-02-13 11:10:43 +00:00
remove : function ( ) {
ok ( true , "Remove called." ) ;
}
2010-01-25 22:01:07 +00:00
} ;
2012-07-05 19:52:13 +00:00
div . bind ( "test.a" , { "x" : 1 } , function ( e ) {
2010-01-25 22:01:07 +00:00
ok ( ! ! e . xyz , "Make sure that the data is getting passed through." ) ;
2012-07-05 19:52:13 +00:00
equal ( e . data [ "x" ] , 1 , "Make sure data is attached properly." ) ;
2010-01-25 22:01:07 +00:00
} ) ;
2012-07-05 19:52:13 +00:00
div . bind ( "test.b" , { "x" : 2 } , function ( e ) {
2010-01-25 22:01:07 +00:00
ok ( ! ! e . xyz , "Make sure that the data is getting passed through." ) ;
2012-07-05 19:52:13 +00:00
equal ( e . data [ "x" ] , 2 , "Make sure data is attached properly." ) ;
2010-01-25 22:01:07 +00:00
} ) ;
// Should trigger 5
2011-08-03 02:50:58 +00:00
div . trigger ( "test" , 33.33 ) ;
2010-01-25 22:01:07 +00:00
// Should trigger 2
2011-08-03 02:50:58 +00:00
div . trigger ( "test.a" , "George Harrison" ) ;
2010-01-25 22:01:07 +00:00
// Should trigger 2
2011-08-03 02:50:58 +00:00
div . trigger ( "test.b" , { year : 1982 } ) ;
2010-02-11 06:42:51 +00:00
2010-02-13 11:10:43 +00:00
// Should trigger 4
2010-02-11 06:42:51 +00:00
div . unbind ( "test" ) ;
2010-02-13 11:10:43 +00:00
div = jQuery ( "<div/>" ) . bind ( "test" , function ( e ) {
ok ( true , "Test event fired." ) ;
} ) ;
// Should trigger 2
2011-04-17 06:43:57 +00:00
div . appendTo ( "#qunit-fixture" ) . remove ( ) ;
2010-02-13 11:10:43 +00:00
2012-07-05 19:52:13 +00:00
delete jQuery . event . special [ "test" ] ;
2010-01-25 22:01:07 +00:00
} ) ;
2008-01-14 09:33:08 +00:00
test ( "bind(), no data" , function ( ) {
expect ( 1 ) ;
2007-04-24 21:48:52 +00:00
var handler = function ( event ) {
ok ( ! event . data , "Check that no data is added to the event object" ) ;
} ;
2008-05-28 23:18:25 +00:00
jQuery ( "#firstp" ) . bind ( "click" , handler ) . trigger ( "click" ) ;
2008-01-14 09:33:08 +00:00
} ) ;
2009-09-16 02:19:18 +00:00
test ( "bind/one/unbind(Object)" , function ( ) {
expect ( 6 ) ;
2010-12-30 06:34:48 +00:00
2009-09-16 02:19:18 +00:00
var clickCounter = 0 , mouseoverCounter = 0 ;
function handler ( event ) {
2012-06-21 19:30:24 +00:00
if ( event . type == "click" ) {
2009-09-16 02:19:18 +00:00
clickCounter ++ ;
2012-06-21 19:30:24 +00:00
}
else if ( event . type == "mouseover" ) {
2009-09-16 02:19:18 +00:00
mouseoverCounter ++ ;
2012-06-21 19:30:24 +00:00
}
}
2010-12-30 06:34:48 +00:00
2009-09-16 02:19:18 +00:00
function handlerWithData ( event ) {
2012-06-21 19:30:24 +00:00
if ( event . type == "click" ) {
2009-09-16 02:19:18 +00:00
clickCounter += event . data ;
2012-06-21 19:30:24 +00:00
}
else if ( event . type == "mouseover" ) {
2009-09-16 02:19:18 +00:00
mouseoverCounter += event . data ;
2012-06-21 19:30:24 +00:00
}
}
2010-12-30 06:34:48 +00:00
2009-09-16 02:19:18 +00:00
function trigger ( ) {
$elem . trigger ( "click" ) . trigger ( "mouseover" ) ;
}
2010-12-30 06:34:48 +00:00
2009-09-16 02:19:18 +00:00
var $elem = jQuery ( "#firstp" )
// Regular bind
. bind ( {
2012-07-05 19:52:13 +00:00
"click" : handler ,
"mouseover" : handler
2009-09-16 02:19:18 +00:00
} )
// Bind with data
. one ( {
2012-07-05 19:52:13 +00:00
"click" : handlerWithData ,
"mouseover" : handlerWithData
2009-09-16 02:19:18 +00:00
} , 2 ) ;
2010-12-30 06:34:48 +00:00
2009-09-16 02:19:18 +00:00
trigger ( ) ;
2010-12-30 06:34:48 +00:00
2011-11-06 20:27:42 +00:00
equal ( clickCounter , 3 , "bind(Object)" ) ;
equal ( mouseoverCounter , 3 , "bind(Object)" ) ;
2010-12-30 06:34:48 +00:00
2009-09-16 02:19:18 +00:00
trigger ( ) ;
2011-11-06 20:27:42 +00:00
equal ( clickCounter , 4 , "bind(Object)" ) ;
equal ( mouseoverCounter , 4 , "bind(Object)" ) ;
2010-12-30 06:34:48 +00:00
2009-09-16 02:19:18 +00:00
jQuery ( "#firstp" ) . unbind ( {
2012-07-05 19:52:13 +00:00
"click" : handler ,
"mouseover" : handler
2009-09-16 02:19:18 +00:00
} ) ;
trigger ( ) ;
2011-11-06 20:27:42 +00:00
equal ( clickCounter , 4 , "bind(Object)" ) ;
equal ( mouseoverCounter , 4 , "bind(Object)" ) ;
2009-09-16 02:19:18 +00:00
} ) ;
2010-03-14 11:34:32 +00:00
test ( "live/die(Object), delegate/undelegate(String, Object)" , function ( ) {
expect ( 6 ) ;
2010-12-30 06:34:48 +00:00
2010-03-14 11:34:32 +00:00
var clickCounter = 0 , mouseoverCounter = 0 ,
2010-04-13 13:11:56 +00:00
$p = jQuery ( "#firstp" ) , $a = $p . find ( "a:first" ) ;
2010-12-30 06:34:48 +00:00
2010-03-14 11:34:32 +00:00
var events = {
2012-07-05 19:52:13 +00:00
"click" : function ( event ) {
2010-03-14 11:34:32 +00:00
clickCounter += ( event . data || 1 ) ;
} ,
2012-07-05 19:52:13 +00:00
"mouseover" : function ( event ) {
2010-03-14 11:34:32 +00:00
mouseoverCounter += ( event . data || 1 ) ;
}
} ;
2010-12-30 06:34:48 +00:00
2010-03-14 11:34:32 +00:00
function trigger ( ) {
$a . trigger ( "click" ) . trigger ( "mouseover" ) ;
}
2010-12-30 06:34:48 +00:00
2010-03-14 11:34:32 +00:00
$a . live ( events ) ;
$p . delegate ( "a" , events , 2 ) ;
2010-12-30 06:34:48 +00:00
2010-03-14 11:34:32 +00:00
trigger ( ) ;
2011-11-06 20:27:42 +00:00
equal ( clickCounter , 3 , "live/delegate" ) ;
equal ( mouseoverCounter , 3 , "live/delegate" ) ;
2010-12-30 06:34:48 +00:00
2010-03-14 11:34:32 +00:00
$p . undelegate ( "a" , events ) ;
2010-12-30 06:34:48 +00:00
2010-03-14 11:34:32 +00:00
trigger ( ) ;
2011-11-06 20:27:42 +00:00
equal ( clickCounter , 4 , "undelegate" ) ;
equal ( mouseoverCounter , 4 , "undelegate" ) ;
2010-12-30 06:34:48 +00:00
2010-03-14 11:34:32 +00:00
$a . die ( events ) ;
2010-12-30 06:34:48 +00:00
2010-03-14 11:34:32 +00:00
trigger ( ) ;
2011-11-06 20:27:42 +00:00
equal ( clickCounter , 4 , "die" ) ;
equal ( mouseoverCounter , 4 , "die" ) ;
2010-03-14 11:34:32 +00:00
} ) ;
2010-10-23 18:36:24 +00:00
test ( "live/delegate immediate propagation" , function ( ) {
expect ( 2 ) ;
2010-12-30 06:34:48 +00:00
2010-10-23 18:36:24 +00:00
var $p = jQuery ( "#firstp" ) , $a = $p . find ( "a:first" ) , lastClick ;
2010-12-30 06:34:48 +00:00
2010-10-23 18:36:24 +00:00
lastClick = "" ;
2010-12-30 06:34:48 +00:00
$a . live ( "click" , function ( e ) {
lastClick = "click1" ;
2010-10-23 18:36:24 +00:00
e . stopImmediatePropagation ( ) ;
} ) ;
$a . live ( "click" , function ( e ) {
lastClick = "click2" ;
} ) ;
$a . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( lastClick , "click1" , "live stopImmediatePropagation" ) ;
2010-10-23 18:36:24 +00:00
$a . die ( "click" ) ;
2010-12-30 06:34:48 +00:00
2010-10-23 18:36:24 +00:00
lastClick = "" ;
2010-12-30 06:34:48 +00:00
$p . delegate ( "a" , "click" , function ( e ) {
lastClick = "click1" ;
2010-10-23 18:36:24 +00:00
e . stopImmediatePropagation ( ) ;
} ) ;
$p . delegate ( "a" , "click" , function ( e ) {
lastClick = "click2" ;
} ) ;
$a . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( lastClick , "click1" , "delegate stopImmediatePropagation" ) ;
2010-10-23 18:36:24 +00:00
$p . undelegate ( "click" ) ;
} ) ;
2010-12-23 21:21:14 +00:00
test ( "bind/delegate bubbling, isDefaultPrevented" , function ( ) {
2010-12-27 19:30:05 +00:00
expect ( 2 ) ;
var $anchor2 = jQuery ( "#anchor2" ) ,
2011-04-17 06:43:57 +00:00
$main = jQuery ( "#qunit-fixture" ) ,
2010-12-27 19:30:05 +00:00
fakeClick = function ( $jq ) {
// Use a native click so we don't get jQuery simulated bubbling
if ( document . createEvent ) {
2011-04-12 08:47:46 +00:00
var e = document . createEvent ( "MouseEvents" ) ;
2011-02-07 16:48:38 +00:00
e . initEvent ( "click" , true , true ) ;
2010-12-27 19:30:05 +00:00
$jq [ 0 ] . dispatchEvent ( e ) ;
}
else if ( $jq [ 0 ] . click ) {
$jq [ 0 ] . click ( ) ; // IE
}
} ;
$anchor2 . click ( function ( e ) {
e . preventDefault ( ) ;
} ) ;
$main . delegate ( "#foo" , "click" , function ( e ) {
2011-01-15 15:24:13 +00:00
var orig = e . originalEvent ;
2011-01-17 20:45:07 +00:00
2012-07-05 19:52:13 +00:00
if ( typeof ( orig . defaultPrevented ) === "boolean" || typeof ( orig . returnValue ) === "boolean" || orig [ "getPreventDefault" ] ) {
2011-11-06 20:27:42 +00:00
equal ( e . isDefaultPrevented ( ) , true , "isDefaultPrevented true passed to bubbled event" ) ;
2011-01-17 20:45:07 +00:00
} else {
2011-01-15 15:24:13 +00:00
// Opera < 11 doesn't implement any interface we can use, so give it a pass
ok ( true , "isDefaultPrevented not supported by this browser, test skipped" ) ;
}
2010-12-27 19:30:05 +00:00
} ) ;
fakeClick ( $anchor2 ) ;
$anchor2 . unbind ( "click" ) ;
2011-07-29 00:43:23 +00:00
$main . undelegate ( "click" ) ;
2010-12-27 19:30:05 +00:00
$anchor2 . click ( function ( e ) {
// Let the default action occur
} ) ;
$main . delegate ( "#foo" , "click" , function ( e ) {
2011-11-06 20:27:42 +00:00
equal ( e . isDefaultPrevented ( ) , false , "isDefaultPrevented false passed to bubbled event" ) ;
2010-12-27 19:30:05 +00:00
} ) ;
fakeClick ( $anchor2 ) ;
$anchor2 . unbind ( "click" ) ;
2011-07-29 00:43:23 +00:00
$main . undelegate ( "click" ) ;
2010-12-27 19:30:05 +00:00
} ) ;
2008-01-14 09:33:08 +00:00
test ( "bind(), iframes" , function ( ) {
2012-10-15 16:16:49 +00:00
expect ( 1 ) ;
2007-07-20 21:53:37 +00:00
// events don't work with iframes, see #939 - this test fails in IE because of contentDocument
2009-04-29 22:04:41 +00:00
var doc = jQuery ( "#loadediframe" ) . contents ( ) ;
2010-12-30 06:34:48 +00:00
2009-04-29 22:04:41 +00:00
jQuery ( "div" , doc ) . bind ( "click" , function ( ) {
ok ( true , "Binding to element inside iframe" ) ;
2011-04-12 08:47:46 +00:00
} ) . click ( ) . unbind ( "click" ) ;
2008-01-14 09:33:08 +00:00
} ) ;
test ( "bind(), trigger change on select" , function ( ) {
2010-09-29 12:41:27 +00:00
expect ( 5 ) ;
2007-03-25 18:06:18 +00:00
var counter = 0 ;
function selectOnChange ( event ) {
2011-11-06 20:27:42 +00:00
equal ( event . data , counter ++ , "Event.data is not a global event object" ) ;
2012-06-21 19:30:24 +00:00
}
2008-05-28 23:18:25 +00:00
jQuery ( "#form select" ) . each ( function ( i ) {
2011-04-12 08:47:46 +00:00
jQuery ( this ) . bind ( "change" , i , selectOnChange ) ;
} ) . trigger ( "change" ) ;
2008-01-14 09:33:08 +00:00
} ) ;
2007-09-03 14:53:09 +00:00
2011-02-10 02:18:11 +00:00
test ( "bind(), namespaced events, cloned events" , 18 , function ( ) {
2011-02-10 02:15:32 +00:00
var firstp = jQuery ( "#firstp" ) ;
firstp . bind ( "custom.test" , function ( e ) {
ok ( false , "Custom event triggered" ) ;
2008-02-03 04:33:11 +00:00
} ) ;
2011-02-10 02:15:32 +00:00
firstp . bind ( "click" , function ( e ) {
2007-09-03 14:53:09 +00:00
ok ( true , "Normal click triggered" ) ;
2011-02-10 02:18:11 +00:00
equal ( e . type + e . namespace , "click" , "Check that only click events trigger this fn" ) ;
2007-12-03 21:41:10 +00:00
} ) ;
2007-09-03 14:53:09 +00:00
2011-02-10 02:15:32 +00:00
firstp . bind ( "click.test" , function ( e ) {
2011-02-10 02:18:11 +00:00
var check = "click" ;
2011-02-10 02:15:32 +00:00
ok ( true , "Namespaced click triggered" ) ;
2011-02-10 02:18:11 +00:00
if ( e . namespace ) {
check += "test" ;
}
equal ( e . type + e . namespace , check , "Check that only click/click.test events trigger this fn" ) ;
2007-12-03 21:41:10 +00:00
} ) ;
2007-09-03 14:53:09 +00:00
2011-02-10 02:18:11 +00:00
//clone(true) element to verify events are cloned correctly
firstp = firstp . add ( firstp . clone ( true ) . attr ( "id" , "firstp2" ) . insertBefore ( firstp ) ) ;
// Trigger both bound fn (8)
2011-02-10 02:15:32 +00:00
firstp . trigger ( "click" ) ;
2007-09-03 14:53:09 +00:00
2011-02-10 02:18:11 +00:00
// Trigger one bound fn (4)
2011-02-10 02:15:32 +00:00
firstp . trigger ( "click.test" ) ;
2007-09-03 14:53:09 +00:00
// Remove only the one fn
2011-02-10 02:15:32 +00:00
firstp . unbind ( "click.test" ) ;
2007-09-03 14:53:09 +00:00
2011-02-10 02:18:11 +00:00
// Trigger the remaining fn (4)
2011-02-10 02:15:32 +00:00
firstp . trigger ( "click" ) ;
2007-12-07 01:52:21 +00:00
2011-02-10 02:15:32 +00:00
// Remove the remaining namespaced fn
firstp . unbind ( ".test" ) ;
2008-02-03 04:33:11 +00:00
2011-02-10 02:15:32 +00:00
// Try triggering the custom event (0)
firstp . trigger ( "custom" ) ;
2008-02-03 04:33:11 +00:00
2007-12-07 01:52:21 +00:00
// using contents will get comments regular, text, and comment nodes
2008-05-28 23:18:25 +00:00
jQuery ( "#nonnodes" ) . contents ( ) . bind ( "tester" , function ( ) {
2011-11-06 20:27:42 +00:00
equal ( this . nodeType , 1 , "Check node,textnode,comment bind just does real nodes" ) ;
2007-12-07 01:52:21 +00:00
} ) . trigger ( "tester" ) ;
2007-12-20 13:36:56 +00:00
// Make sure events stick with appendTo'd elements (which are cloned) #2027
2011-04-17 06:43:57 +00:00
jQuery ( "<a href='#fail' class='test'>test</a>" ) . click ( function ( ) { return false ; } ) . appendTo ( "#qunit-fixture" ) ;
2008-05-28 23:18:25 +00:00
ok ( jQuery ( "a.test:first" ) . triggerHandler ( "click" ) === false , "Handler is bound to appendTo'd elements" ) ;
2007-03-25 18:06:18 +00:00
} ) ;
2008-12-19 04:34:12 +00:00
test ( "bind(), multi-namespaced events" , function ( ) {
expect ( 6 ) ;
2010-12-30 06:34:48 +00:00
2008-12-19 04:34:12 +00:00
var order = [
"click.test.abc" ,
"click.test.abc" ,
"click.test" ,
"click.test.abc" ,
"click.test" ,
"custom.test2"
] ;
2010-12-30 06:34:48 +00:00
2008-12-19 04:34:12 +00:00
function check ( name , msg ) {
2011-11-06 20:27:42 +00:00
deepEqual ( name , order . shift ( ) , msg ) ;
2008-12-19 04:34:12 +00:00
}
jQuery ( "#firstp" ) . bind ( "custom.test" , function ( e ) {
check ( "custom.test" , "Custom event triggered" ) ;
} ) ;
jQuery ( "#firstp" ) . bind ( "custom.test2" , function ( e ) {
check ( "custom.test2" , "Custom event triggered" ) ;
} ) ;
jQuery ( "#firstp" ) . bind ( "click.test" , function ( e ) {
check ( "click.test" , "Normal click triggered" ) ;
} ) ;
jQuery ( "#firstp" ) . bind ( "click.test.abc" , function ( e ) {
check ( "click.test.abc" , "Namespaced click triggered" ) ;
} ) ;
2010-12-30 06:34:48 +00:00
2009-11-19 05:11:29 +00:00
// Those would not trigger/unbind (#5303)
jQuery ( "#firstp" ) . trigger ( "click.a.test" ) ;
jQuery ( "#firstp" ) . unbind ( "click.a.test" ) ;
2008-12-19 04:34:12 +00:00
// Trigger both bound fn (1)
jQuery ( "#firstp" ) . trigger ( "click.test.abc" ) ;
// Trigger one bound fn (1)
jQuery ( "#firstp" ) . trigger ( "click.abc" ) ;
// Trigger two bound fn (2)
jQuery ( "#firstp" ) . trigger ( "click.test" ) ;
// Remove only the one fn
jQuery ( "#firstp" ) . unbind ( "click.abc" ) ;
// Trigger the remaining fn (1)
jQuery ( "#firstp" ) . trigger ( "click" ) ;
// Remove the remaining fn
jQuery ( "#firstp" ) . unbind ( ".test" ) ;
// Trigger the remaining fn (1)
jQuery ( "#firstp" ) . trigger ( "custom" ) ;
} ) ;
2010-02-04 05:49:46 +00:00
test ( "bind(), with same function" , function ( ) {
2012-06-21 19:30:24 +00:00
expect ( 2 ) ;
2010-02-04 05:49:46 +00:00
2011-02-10 02:15:32 +00:00
var count = 0 , func = function ( ) {
2010-02-04 05:49:46 +00:00
count ++ ;
} ;
jQuery ( "#liveHandlerOrder" ) . bind ( "foo.bar" , func ) . bind ( "foo.zar" , func ) ;
jQuery ( "#liveHandlerOrder" ) . trigger ( "foo.bar" ) ;
2011-11-06 20:27:42 +00:00
equal ( count , 1 , "Verify binding function with multiple namespaces." ) ;
2010-02-04 05:49:46 +00:00
jQuery ( "#liveHandlerOrder" ) . unbind ( "foo.bar" , func ) . unbind ( "foo.zar" , func ) ;
jQuery ( "#liveHandlerOrder" ) . trigger ( "foo.bar" ) ;
2011-11-06 20:27:42 +00:00
equal ( count , 1 , "Verify that removing events still work." ) ;
2010-02-04 05:49:46 +00:00
} ) ;
2010-02-04 14:23:50 +00:00
test ( "bind(), make sure order is maintained" , function ( ) {
expect ( 1 ) ;
var elem = jQuery ( "#firstp" ) , log = [ ] , check = [ ] ;
2012-06-21 19:30:24 +00:00
jQuery . each ( new Array ( 100 ) , function ( i ) {
2010-02-04 14:23:50 +00:00
elem . bind ( "click" , function ( ) {
log . push ( i ) ;
} ) ;
check . push ( i ) ;
2012-06-21 19:30:24 +00:00
} ) ;
2010-02-04 14:23:50 +00:00
elem . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( log . join ( "," ) , check . join ( "," ) , "Make sure order was maintained." ) ;
2010-02-04 14:23:50 +00:00
elem . unbind ( "click" ) ;
} ) ;
2010-12-30 06:34:48 +00:00
2009-05-07 00:50:28 +00:00
test ( "bind(), with different this object" , function ( ) {
expect ( 4 ) ;
var thisObject = { myThis : true } ,
data = { myData : true } ,
handler1 = function ( event ) {
2011-11-06 20:27:42 +00:00
equal ( this , thisObject , "bind() with different this object" ) ;
2009-05-07 00:50:28 +00:00
} ,
handler2 = function ( event ) {
2011-11-06 20:27:42 +00:00
equal ( this , thisObject , "bind() with different this object and data" ) ;
equal ( event . data , data , "bind() with different this object and data" ) ;
2009-05-07 00:50:28 +00:00
} ;
2010-12-30 06:34:48 +00:00
2009-05-07 00:50:28 +00:00
jQuery ( "#firstp" )
2009-12-31 05:37:23 +00:00
. bind ( "click" , jQuery . proxy ( handler1 , thisObject ) ) . click ( ) . unbind ( "click" , handler1 )
. bind ( "click" , data , jQuery . proxy ( handler2 , thisObject ) ) . click ( ) . unbind ( "click" , handler2 ) ;
2009-05-07 00:50:28 +00:00
2011-01-09 21:52:33 +00:00
ok ( ! jQuery . _data ( jQuery ( "#firstp" ) [ 0 ] , "events" ) , "Event handler unbound when using different this object and data." ) ;
2009-05-07 00:50:28 +00:00
} ) ;
2010-02-27 14:02:13 +00:00
test ( "bind(name, false), unbind(name, false)" , function ( ) {
expect ( 3 ) ;
var main = 0 ;
2011-04-17 06:43:57 +00:00
jQuery ( "#qunit-fixture" ) . bind ( "click" , function ( e ) { main ++ ; } ) ;
2010-02-27 14:02:13 +00:00
jQuery ( "#ap" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( main , 1 , "Verify that the trigger happened correctly." ) ;
2010-02-27 14:02:13 +00:00
main = 0 ;
jQuery ( "#ap" ) . bind ( "click" , false ) ;
jQuery ( "#ap" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( main , 0 , "Verify that no bubble happened." ) ;
2010-02-27 14:02:13 +00:00
main = 0 ;
jQuery ( "#ap" ) . unbind ( "click" , false ) ;
jQuery ( "#ap" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( main , 1 , "Verify that the trigger happened correctly." ) ;
2011-01-09 21:58:47 +00:00
// manually clean up events from elements outside the fixture
2011-04-17 06:43:57 +00:00
jQuery ( "#qunit-fixture" ) . unbind ( "click" ) ;
2010-02-27 14:02:13 +00:00
} ) ;
2011-01-01 18:49:59 +00:00
test ( "live(name, false), die(name, false)" , function ( ) {
expect ( 3 ) ;
var main = 0 ;
2011-04-17 06:43:57 +00:00
jQuery ( "#qunit-fixture" ) . live ( "click" , function ( e ) { main ++ ; } ) ;
2011-01-01 18:49:59 +00:00
jQuery ( "#ap" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( main , 1 , "Verify that the trigger happened correctly." ) ;
2011-01-01 18:49:59 +00:00
main = 0 ;
jQuery ( "#ap" ) . live ( "click" , false ) ;
jQuery ( "#ap" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( main , 0 , "Verify that no bubble happened." ) ;
2011-01-01 18:49:59 +00:00
main = 0 ;
jQuery ( "#ap" ) . die ( "click" , false ) ;
jQuery ( "#ap" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( main , 1 , "Verify that the trigger happened correctly." ) ;
2011-04-17 06:43:57 +00:00
jQuery ( "#qunit-fixture" ) . die ( "click" ) ;
2011-01-01 18:49:59 +00:00
} ) ;
test ( "delegate(selector, name, false), undelegate(selector, name, false)" , function ( ) {
expect ( 3 ) ;
var main = 0 ;
2011-04-17 06:43:57 +00:00
jQuery ( "#qunit-fixture" ) . delegate ( "#ap" , "click" , function ( e ) { main ++ ; } ) ;
2011-01-01 18:49:59 +00:00
jQuery ( "#ap" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( main , 1 , "Verify that the trigger happened correctly." ) ;
2011-01-01 18:49:59 +00:00
main = 0 ;
jQuery ( "#ap" ) . delegate ( "#groups" , "click" , false ) ;
jQuery ( "#groups" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( main , 0 , "Verify that no bubble happened." ) ;
2011-01-01 18:49:59 +00:00
main = 0 ;
jQuery ( "#ap" ) . undelegate ( "#groups" , "click" , false ) ;
jQuery ( "#groups" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( main , 1 , "Verify that the trigger happened correctly." ) ;
2011-04-17 06:43:57 +00:00
jQuery ( "#qunit-fixture" ) . undelegate ( "#ap" , "click" ) ;
2011-01-01 18:49:59 +00:00
} ) ;
2010-02-26 16:32:12 +00:00
test ( "bind()/trigger()/unbind() on plain object" , function ( ) {
2011-02-07 16:48:38 +00:00
expect ( 7 ) ;
2010-02-26 16:32:12 +00:00
var obj = { } ;
// Make sure it doesn't complain when no events are found
jQuery ( obj ) . trigger ( "test" ) ;
// Make sure it doesn't complain when no events are found
jQuery ( obj ) . unbind ( "test" ) ;
2010-11-21 03:31:04 +00:00
jQuery ( obj ) . bind ( {
2012-07-05 19:52:13 +00:00
"test" : function ( ) {
2010-11-21 03:31:04 +00:00
ok ( true , "Custom event run." ) ;
} ,
2012-07-05 19:52:13 +00:00
"submit" : function ( ) {
2010-11-21 03:31:04 +00:00
ok ( true , "Custom submit event run." ) ;
}
2010-02-26 16:32:12 +00:00
} ) ;
2011-01-09 21:52:33 +00:00
var events = jQuery . _data ( obj , "events" ) ;
2010-09-29 13:46:25 +00:00
ok ( events , "Object has events bound." ) ;
2012-07-05 19:52:13 +00:00
equal ( obj [ "events" ] , undefined , "Events object on plain objects is not events" ) ;
equal ( obj [ "test" ] , undefined , "Make sure that test event is not on the plain object." ) ;
equal ( obj [ "handle" ] , undefined , "Make sure that the event handler is not on the plain object." ) ;
2010-02-26 16:32:12 +00:00
// Should trigger 1
jQuery ( obj ) . trigger ( "test" ) ;
2010-11-21 03:31:04 +00:00
jQuery ( obj ) . trigger ( "submit" ) ;
2010-02-26 16:32:12 +00:00
jQuery ( obj ) . unbind ( "test" ) ;
2010-11-21 03:31:04 +00:00
jQuery ( obj ) . unbind ( "submit" ) ;
2010-02-26 16:32:12 +00:00
// Should trigger 0
jQuery ( obj ) . trigger ( "test" ) ;
// Make sure it doesn't complain when no events are found
jQuery ( obj ) . unbind ( "test" ) ;
2010-12-30 06:34:48 +00:00
2011-11-06 20:27:42 +00:00
equal ( obj && obj [ jQuery . expando ] &&
2011-02-10 02:15:32 +00:00
obj [ jQuery . expando ] [ jQuery . expando ] &&
2012-07-05 19:52:13 +00:00
obj [ jQuery . expando ] [ jQuery . expando ] [ "events" ] , undefined , "Make sure events object is removed" ) ;
2010-02-26 16:32:12 +00:00
} ) ;
2009-01-08 22:22:33 +00:00
test ( "unbind(type)" , function ( ) {
2011-10-12 00:30:07 +00:00
expect ( 1 ) ;
2010-12-30 06:34:48 +00:00
2009-01-08 22:22:33 +00:00
var $elem = jQuery ( "#firstp" ) ,
message ;
function error ( ) {
ok ( false , message ) ;
}
2010-12-30 06:34:48 +00:00
2009-01-08 22:22:33 +00:00
message = "unbind passing function" ;
2011-04-12 08:47:46 +00:00
$elem . bind ( "error1" , error ) . unbind ( "error1" , error ) . triggerHandler ( "error1" ) ;
2010-12-30 06:34:48 +00:00
2009-01-08 22:22:33 +00:00
message = "unbind all from event" ;
2011-04-12 08:47:46 +00:00
$elem . bind ( "error1" , error ) . unbind ( "error1" ) . triggerHandler ( "error1" ) ;
2010-12-30 06:34:48 +00:00
2009-01-08 22:22:33 +00:00
message = "unbind all" ;
2011-04-12 08:47:46 +00:00
$elem . bind ( "error1" , error ) . unbind ( ) . triggerHandler ( "error1" ) ;
2010-12-30 06:34:48 +00:00
2009-01-08 22:22:33 +00:00
message = "unbind many with function" ;
2011-04-12 08:47:46 +00:00
$elem . bind ( "error1 error2" , error )
. unbind ( "error1 error2" , error )
. trigger ( "error1" ) . triggerHandler ( "error2" ) ;
2009-01-08 22:22:33 +00:00
message = "unbind many" ; // #3538
2011-04-12 08:47:46 +00:00
$elem . bind ( "error1 error2" , error )
. unbind ( "error1 error2" )
. trigger ( "error1" ) . triggerHandler ( "error2" ) ;
2010-12-30 06:34:48 +00:00
2009-04-29 21:45:58 +00:00
message = "unbind without a type or handler" ;
2010-03-24 19:39:58 +00:00
$elem . bind ( "error1 error2.test" , error )
2011-02-15 21:03:23 +00:00
. unbind ( )
. trigger ( "error1" ) . triggerHandler ( "error2" ) ;
2011-10-12 00:30:07 +00:00
// Should only unbind the specified function
jQuery ( document ) . bind ( "click" , function ( ) {
ok ( true , "called handler after selective removal" ) ;
} ) ;
var func = function ( ) { } ;
jQuery ( document )
. bind ( "click" , func )
. unbind ( "click" , func )
. click ( )
. unbind ( "click" ) ;
2009-01-08 22:22:33 +00:00
} ) ;
test ( "unbind(eventObject)" , function ( ) {
expect ( 4 ) ;
2010-12-30 06:34:48 +00:00
2009-01-08 22:22:33 +00:00
var $elem = jQuery ( "#firstp" ) ,
num ;
function assert ( expected ) {
num = 0 ;
2011-04-12 08:47:46 +00:00
$elem . trigger ( "foo" ) . triggerHandler ( "bar" ) ;
2011-11-06 20:27:42 +00:00
equal ( num , expected , "Check the right handlers are triggered" ) ;
2009-01-08 22:22:33 +00:00
}
2010-12-30 06:34:48 +00:00
2009-01-08 22:22:33 +00:00
$elem
// This handler shouldn't be unbound
2011-04-12 08:47:46 +00:00
. bind ( "foo" , function ( ) {
2009-01-08 22:22:33 +00:00
num += 1 ;
} )
2011-04-12 08:47:46 +00:00
. bind ( "foo" , function ( e ) {
2012-06-21 19:30:24 +00:00
$elem . unbind ( e ) ;
2009-01-08 22:22:33 +00:00
num += 2 ;
} )
// Neither this one
2011-04-12 08:47:46 +00:00
. bind ( "bar" , function ( ) {
2009-01-08 22:22:33 +00:00
num += 4 ;
} ) ;
2010-12-30 06:34:48 +00:00
2009-01-08 22:22:33 +00:00
assert ( 7 ) ;
assert ( 5 ) ;
2010-12-30 06:34:48 +00:00
2011-04-12 08:47:46 +00:00
$elem . unbind ( "bar" ) ;
2009-01-08 22:22:33 +00:00
assert ( 1 ) ;
2010-12-30 06:34:48 +00:00
$elem . unbind ( ) ;
2009-01-08 22:22:33 +00:00
assert ( 0 ) ;
} ) ;
2012-10-16 18:36:47 +00:00
test ( "hover() mouseenter mouseleave" , function ( ) {
expect ( 1 ) ;
2011-10-27 21:11:40 +00:00
2009-05-06 02:17:24 +00:00
var times = 0 ,
handler1 = function ( event ) { ++ times ; } ,
handler2 = function ( event ) { ++ times ; } ;
jQuery ( "#firstp" )
. hover ( handler1 , handler2 )
. mouseenter ( ) . mouseleave ( )
. unbind ( "mouseenter" , handler1 )
. unbind ( "mouseleave" , handler2 )
. hover ( handler1 )
. mouseenter ( ) . mouseleave ( )
. unbind ( "mouseenter mouseleave" , handler1 )
. mouseenter ( ) . mouseleave ( ) ;
2011-11-06 20:27:42 +00:00
equal ( times , 4 , "hover handlers fired" ) ;
2011-10-27 21:11:40 +00:00
2009-05-06 02:17:24 +00:00
} ) ;
2011-03-10 03:38:26 +00:00
test ( "mouseover triggers mouseenter" , function ( ) {
expect ( 1 ) ;
2011-03-31 15:37:48 +00:00
2011-03-10 03:38:26 +00:00
var count = 0 ,
elem = jQuery ( "<a />" ) ;
elem . mouseenter ( function ( ) {
2012-06-21 19:30:24 +00:00
count ++ ;
2011-03-10 03:38:26 +00:00
} ) ;
2011-04-12 08:47:46 +00:00
elem . trigger ( "mouseover" ) ;
2011-11-06 20:27:42 +00:00
equal ( count , 1 , "make sure mouseover triggers a mouseenter" ) ;
2011-03-31 15:37:48 +00:00
2011-03-10 03:38:26 +00:00
elem . remove ( ) ;
} ) ;
2011-06-14 19:38:46 +00:00
test ( "withinElement implemented with jQuery.contains()" , function ( ) {
expect ( 1 ) ;
2012-10-16 14:17:14 +00:00
jQuery ( "#qunit-fixture" ) . append ( "<div id='jc-outer'><div id='jc-inner'></div></div>" ) ;
2011-06-14 19:38:46 +00:00
jQuery ( "#jc-outer" ) . bind ( "mouseenter mouseleave" , function ( event ) {
equal ( this . id , "jc-outer" , this . id + " " + event . type ) ;
} ) . trigger ( "mouseenter" ) ;
jQuery ( "#jc-inner" ) . trigger ( "mousenter" ) ;
jQuery ( "#jc-outer" ) . unbind ( "mouseenter mouseleave" ) . remove ( ) ;
jQuery ( "#jc-inner" ) . remove ( ) ;
} ) ;
test ( "mouseenter, mouseleave don't catch exceptions" , function ( ) {
expect ( 2 ) ;
var elem = jQuery ( "#firstp" ) . hover ( function ( ) { throw "an Exception" ; } ) ;
try {
elem . mouseenter ( ) ;
} catch ( e ) {
2011-11-06 20:27:42 +00:00
equal ( e , "an Exception" , "mouseenter doesn't catch exceptions" ) ;
2011-06-14 19:38:46 +00:00
}
try {
elem . mouseleave ( ) ;
} catch ( e ) {
2011-11-06 20:27:42 +00:00
equal ( e , "an Exception" , "mouseleave doesn't catch exceptions" ) ;
2011-06-14 19:38:46 +00:00
}
} ) ;
2008-05-08 16:25:12 +00:00
test ( "trigger() shortcuts" , function ( ) {
expect ( 6 ) ;
2011-01-09 21:58:47 +00:00
2011-04-12 08:47:46 +00:00
var elem = jQuery ( "<li><a href='#'>Change location</a></li>" ) . prependTo ( "#firstUL" ) ;
elem . find ( "a" ) . bind ( "click" , function ( ) {
var close = jQuery ( "spanx" , this ) ; // same with jQuery(this).find("span");
2011-11-06 20:27:42 +00:00
equal ( close . length , 0 , "Context element does not exist, length must be zero" ) ;
2007-12-03 21:41:10 +00:00
ok ( ! close [ 0 ] , "Context element does not exist, direct access to element must return undefined" ) ;
return false ;
2007-03-25 18:06:18 +00:00
} ) . click ( ) ;
2010-12-30 06:34:48 +00:00
2011-01-09 21:58:47 +00:00
// manually clean up detached elements
elem . remove ( ) ;
2008-05-28 23:18:25 +00:00
jQuery ( "#check1" ) . click ( function ( ) {
2007-03-25 18:06:18 +00:00
ok ( true , "click event handler for checkbox gets fired twice, see #815" ) ;
2007-04-22 03:16:53 +00:00
} ) . click ( ) ;
2010-12-30 06:34:48 +00:00
2007-07-21 01:04:59 +00:00
var counter = 0 ;
2011-04-12 08:47:46 +00:00
jQuery ( "#firstp" ) [ 0 ] . onclick = function ( event ) {
2007-07-21 01:04:59 +00:00
counter ++ ;
} ;
2011-04-12 08:47:46 +00:00
jQuery ( "#firstp" ) . click ( ) ;
2011-11-06 20:27:42 +00:00
equal ( counter , 1 , "Check that click, triggers onclick event handler also" ) ;
2010-12-30 06:34:48 +00:00
2008-04-21 20:39:17 +00:00
var clickCounter = 0 ;
2011-04-12 08:47:46 +00:00
jQuery ( "#simon1" ) [ 0 ] . onclick = function ( event ) {
2008-04-21 20:39:17 +00:00
clickCounter ++ ;
} ;
2011-04-12 08:47:46 +00:00
jQuery ( "#simon1" ) . click ( ) ;
2011-11-06 20:27:42 +00:00
equal ( clickCounter , 1 , "Check that click, triggers onclick event handler on an a tag also" ) ;
2010-12-30 06:34:48 +00:00
2011-04-12 08:47:46 +00:00
elem = jQuery ( "<img />" ) . load ( function ( ) {
2008-05-08 16:25:12 +00:00
ok ( true , "Trigger the load event, using the shortcut .load() (#2819)" ) ;
} ) . load ( ) ;
2011-01-09 21:58:47 +00:00
// manually clean up detached elements
elem . remove ( ) ;
2011-04-12 20:48:22 +00:00
// test that special handlers do not blow up with VML elements (#7071)
2012-10-16 14:17:14 +00:00
jQuery ( "<xml:namespace ns='urn:schemas-microsoft-com:vml' prefix='v' />" ) . appendTo ( "head" ) ;
jQuery ( "<v:oval id='oval' style='width:100pt;height:75pt;' fillcolor='red'> </v:oval>" ) . appendTo ( "#form" ) ;
2011-04-12 20:48:22 +00:00
jQuery ( "#oval" ) . click ( ) . keydown ( ) ;
2006-12-28 11:37:07 +00:00
} ) ;
2008-12-22 01:57:06 +00:00
test ( "trigger() bubbling" , function ( ) {
2012-05-12 17:38:33 +00:00
expect ( 18 ) ;
2008-12-22 01:57:06 +00:00
2011-04-07 02:11:58 +00:00
var win = 0 , doc = 0 , html = 0 , body = 0 , main = 0 , ap = 0 ;
2008-12-22 01:57:06 +00:00
2011-04-07 02:11:58 +00:00
jQuery ( window ) . bind ( "click" , function ( e ) { win ++ ; } ) ;
2008-12-22 02:02:05 +00:00
jQuery ( document ) . bind ( "click" , function ( e ) { if ( e . target !== document ) { doc ++ ; } } ) ;
jQuery ( "html" ) . bind ( "click" , function ( e ) { html ++ ; } ) ;
jQuery ( "body" ) . bind ( "click" , function ( e ) { body ++ ; } ) ;
2011-04-17 06:43:57 +00:00
jQuery ( "#qunit-fixture" ) . bind ( "click" , function ( e ) { main ++ ; } ) ;
2008-12-22 01:57:06 +00:00
jQuery ( "#ap" ) . bind ( "click" , function ( ) { ap ++ ; return false ; } ) ;
jQuery ( "html" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( win , 1 , "HTML bubble" ) ;
equal ( doc , 1 , "HTML bubble" ) ;
equal ( html , 1 , "HTML bubble" ) ;
2008-12-22 01:57:06 +00:00
jQuery ( "body" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( win , 2 , "Body bubble" ) ;
equal ( doc , 2 , "Body bubble" ) ;
equal ( html , 2 , "Body bubble" ) ;
equal ( body , 1 , "Body bubble" ) ;
2008-12-22 01:57:06 +00:00
2011-04-17 06:43:57 +00:00
jQuery ( "#qunit-fixture" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( win , 3 , "Main bubble" ) ;
equal ( doc , 3 , "Main bubble" ) ;
equal ( html , 3 , "Main bubble" ) ;
equal ( body , 2 , "Main bubble" ) ;
equal ( main , 1 , "Main bubble" ) ;
2008-12-22 01:57:06 +00:00
jQuery ( "#ap" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( doc , 3 , "ap bubble" ) ;
equal ( html , 3 , "ap bubble" ) ;
equal ( body , 2 , "ap bubble" ) ;
equal ( main , 1 , "ap bubble" ) ;
equal ( ap , 1 , "ap bubble" ) ;
2011-01-09 21:58:47 +00:00
2012-05-12 17:38:33 +00:00
jQuery ( document ) . trigger ( "click" ) ;
equal ( win , 4 , "doc bubble" ) ;
2012-06-21 19:30:24 +00:00
2011-01-09 21:58:47 +00:00
// manually clean up events from elements outside the fixture
jQuery ( document ) . unbind ( "click" ) ;
2011-04-17 06:43:57 +00:00
jQuery ( "html, body, #qunit-fixture" ) . unbind ( "click" ) ;
2008-12-22 01:57:06 +00:00
} ) ;
2008-12-31 02:58:13 +00:00
test ( "trigger(type, [data], [fn])" , function ( ) {
2011-04-17 18:58:20 +00:00
expect ( 16 ) ;
2007-08-30 05:51:11 +00:00
2006-12-28 11:37:07 +00:00
var handler = function ( event , a , b , c ) {
2011-11-06 20:27:42 +00:00
equal ( event . type , "click" , "check passed data" ) ;
equal ( a , 1 , "check passed data" ) ;
equal ( b , "2" , "check passed data" ) ;
equal ( c , "abc" , "check passed data" ) ;
2007-08-30 05:51:11 +00:00
return "test" ;
} ;
2008-12-25 21:44:54 +00:00
var $elem = jQuery ( "#firstp" ) ;
2007-08-30 16:34:34 +00:00
2007-08-30 05:51:11 +00:00
// Simulate a "native" click
2008-12-25 21:44:54 +00:00
$elem [ 0 ] . click = function ( ) {
2007-08-30 05:51:11 +00:00
ok ( true , "Native call was triggered" ) ;
2007-04-24 21:48:52 +00:00
} ;
2007-08-30 05:51:11 +00:00
2011-07-26 15:49:23 +00:00
2012-10-16 14:17:14 +00:00
$elem . live ( "mouseenter" , function ( ) {
ok ( true , "Trigger mouseenter bound by live" ) ;
2011-04-17 18:58:20 +00:00
} ) ;
2012-10-16 14:17:14 +00:00
$elem . live ( "mouseleave" , function ( ) {
ok ( true , "Trigger mouseleave bound by live" ) ;
2011-04-17 18:58:20 +00:00
} ) ;
2012-10-16 14:17:14 +00:00
$elem . trigger ( "mouseenter" ) ;
2011-04-17 18:58:20 +00:00
2012-10-16 14:17:14 +00:00
$elem . trigger ( "mouseleave" ) ;
2011-04-17 18:58:20 +00:00
2012-10-16 14:17:14 +00:00
$elem . die ( "mouseenter" ) ;
2011-04-17 18:58:20 +00:00
2012-10-16 14:17:14 +00:00
$elem . die ( "mouseleave" ) ;
2011-07-26 15:49:23 +00:00
2011-10-28 18:17:14 +00:00
// Triggers handlrs and native
2007-08-30 05:51:11 +00:00
// Trigger 5
2008-12-25 21:44:54 +00:00
$elem . bind ( "click" , handler ) . trigger ( "click" , [ 1 , "2" , "abc" ] ) ;
2007-08-30 05:51:11 +00:00
// Simulate a "native" click
2008-12-25 21:44:54 +00:00
$elem [ 0 ] . click = function ( ) {
2007-08-30 05:51:11 +00:00
ok ( false , "Native call was triggered" ) ;
} ;
// Trigger only the handlers (no native)
2007-08-30 16:34:34 +00:00
// Triggers 5
2011-11-06 20:27:42 +00:00
equal ( $elem . triggerHandler ( "click" , [ 1 , "2" , "abc" ] ) , "test" , "Verify handler response" ) ;
2007-08-30 05:51:11 +00:00
2012-06-11 01:54:16 +00:00
var pass = true , elem2 ;
2007-12-08 02:54:09 +00:00
try {
2012-06-11 01:54:16 +00:00
elem2 = jQuery ( "#form input:first" ) ;
elem2 . get ( 0 ) . style . display = "none" ;
elem2 . trigger ( "focus" ) ;
2007-12-08 02:54:09 +00:00
} catch ( e ) {
pass = false ;
}
ok ( pass , "Trigger focus on hidden element" ) ;
2010-12-30 06:34:48 +00:00
2009-06-17 02:31:45 +00:00
pass = true ;
try {
2011-04-17 06:43:57 +00:00
jQuery ( "#qunit-fixture table:first" ) . bind ( "test:test" , function ( ) { } ) . trigger ( "test:test" ) ;
2009-06-17 02:31:45 +00:00
} catch ( e ) {
pass = false ;
}
ok ( pass , "Trigger on a table with a colon in the even type, see #3533" ) ;
2010-01-25 18:45:07 +00:00
var form = jQuery ( "<form action=''></form>" ) . appendTo ( "body" ) ;
// Make sure it can be prevented locally
form . submit ( function ( ) {
ok ( true , "Local bind still works." ) ;
return false ;
} ) ;
// Trigger 1
form . trigger ( "submit" ) ;
form . unbind ( "submit" ) ;
jQuery ( document ) . submit ( function ( ) {
ok ( true , "Make sure bubble works up to document." ) ;
return false ;
} ) ;
// Trigger 1
form . trigger ( "submit" ) ;
jQuery ( document ) . unbind ( "submit" ) ;
form . remove ( ) ;
} ) ;
2012-05-18 20:30:28 +00:00
test ( "submit event bubbles on copied forms (#11649)" , function ( ) {
expect ( 3 ) ;
2012-06-21 19:30:24 +00:00
2012-05-18 20:30:28 +00:00
var $formByClone , $formByHTML ,
$testForm = jQuery ( "#testForm" ) ,
$fixture = jQuery ( "#qunit-fixture" ) ,
$wrapperDiv = jQuery ( "<div/>" ) . appendTo ( $fixture ) ;
2012-06-21 19:30:24 +00:00
2012-05-18 20:30:28 +00:00
function noSubmit ( e ) {
e . preventDefault ( ) ;
}
function delegatedSubmit ( ) {
ok ( true , "Make sure submit event bubbles up." ) ;
return false ;
}
2012-06-21 19:30:24 +00:00
2012-05-18 20:30:28 +00:00
// Attach a delegated submit handler to the parent element
$fixture . on ( "submit" , "form" , delegatedSubmit ) ;
2012-06-21 19:30:24 +00:00
2012-05-18 20:30:28 +00:00
// Trigger form submission to introduce the _submit_attached property
$testForm . on ( "submit" , noSubmit ) . find ( "input[name=sub1]" ) . click ( ) ;
2012-06-21 19:30:24 +00:00
2012-05-18 20:30:28 +00:00
// Copy the form via .clone() and .html()
$formByClone = $testForm . clone ( true , true ) . removeAttr ( "id" ) ;
$formByHTML = jQuery ( $fixture . html ( ) ) . filter ( "#testForm" ) . removeAttr ( "id" ) ;
$wrapperDiv . append ( $formByClone , $formByHTML ) ;
2012-06-21 19:30:24 +00:00
2012-05-18 20:30:28 +00:00
// Check submit bubbling on the copied forms
$wrapperDiv . find ( "form" ) . on ( "submit" , noSubmit ) . find ( "input[name=sub1]" ) . click ( ) ;
2012-06-21 19:30:24 +00:00
2012-05-18 20:30:28 +00:00
// Clean up
$wrapperDiv . remove ( ) ;
$fixture . off ( "submit" , "form" , delegatedSubmit ) ;
2012-05-18 21:01:17 +00:00
$testForm . off ( "submit" , noSubmit ) ;
2012-05-18 20:30:28 +00:00
} ) ;
2012-05-21 23:01:59 +00:00
test ( "change event bubbles on copied forms (#11796)" , function ( ) {
expect ( 3 ) ;
2012-06-21 19:30:24 +00:00
2012-05-21 23:01:59 +00:00
var $formByClone , $formByHTML ,
$form = jQuery ( "#form" ) ,
$fixture = jQuery ( "#qunit-fixture" ) ,
$wrapperDiv = jQuery ( "<div/>" ) . appendTo ( $fixture ) ;
2012-06-21 19:30:24 +00:00
2012-05-21 23:01:59 +00:00
function delegatedChange ( ) {
ok ( true , "Make sure change event bubbles up." ) ;
return false ;
}
2012-06-21 19:30:24 +00:00
2012-05-21 23:01:59 +00:00
// Attach a delegated change handler to the form
$fixture . on ( "change" , "form" , delegatedChange ) ;
2012-06-21 19:30:24 +00:00
2012-05-21 23:01:59 +00:00
// Trigger change event to introduce the _change_attached property
$form . find ( "select[name=select1]" ) . val ( "1" ) . change ( ) ;
2012-06-21 19:30:24 +00:00
2012-05-21 23:01:59 +00:00
// Copy the form via .clone() and .html()
$formByClone = $form . clone ( true , true ) . removeAttr ( "id" ) ;
$formByHTML = jQuery ( $fixture . html ( ) ) . filter ( "#form" ) . removeAttr ( "id" ) ;
$wrapperDiv . append ( $formByClone , $formByHTML ) ;
2012-06-21 19:30:24 +00:00
2012-05-21 23:01:59 +00:00
// Check change bubbling on the copied forms
$wrapperDiv . find ( "form select[name=select1]" ) . val ( "2" ) . change ( ) ;
2012-06-21 19:30:24 +00:00
2012-05-21 23:01:59 +00:00
// Clean up
$wrapperDiv . remove ( ) ;
$fixture . off ( "change" , "form" , delegatedChange ) ;
} ) ;
2008-12-31 02:58:13 +00:00
test ( "trigger(eventObject, [data], [fn])" , function ( ) {
2011-11-15 15:22:32 +00:00
expect ( 28 ) ;
2010-12-30 06:34:48 +00:00
2012-06-11 01:54:16 +00:00
var $parent = jQuery ( "<div id='par' />" ) . appendTo ( "body" ) ,
2011-04-12 08:47:46 +00:00
$child = jQuery ( "<p id='child'>foo</p>" ) . appendTo ( $parent ) ;
2010-12-30 06:34:48 +00:00
2012-06-11 01:54:16 +00:00
$parent . get ( 0 ) . style . display = "none" ;
2010-12-30 06:34:48 +00:00
var event = jQuery . Event ( "noNew" ) ;
2008-12-31 02:58:13 +00:00
ok ( event != window , "Instantiate jQuery.Event without the 'new' keyword" ) ;
2011-11-06 20:27:42 +00:00
equal ( event . type , "noNew" , "Verify its type" ) ;
2010-12-30 06:34:48 +00:00
2011-11-06 20:27:42 +00:00
equal ( event . isDefaultPrevented ( ) , false , "Verify isDefaultPrevented" ) ;
equal ( event . isPropagationStopped ( ) , false , "Verify isPropagationStopped" ) ;
equal ( event . isImmediatePropagationStopped ( ) , false , "Verify isImmediatePropagationStopped" ) ;
2010-12-30 06:34:48 +00:00
2008-12-31 02:58:13 +00:00
event . preventDefault ( ) ;
2011-11-06 20:27:42 +00:00
equal ( event . isDefaultPrevented ( ) , true , "Verify isDefaultPrevented" ) ;
2008-12-31 02:58:13 +00:00
event . stopPropagation ( ) ;
2011-11-06 20:27:42 +00:00
equal ( event . isPropagationStopped ( ) , true , "Verify isPropagationStopped" ) ;
2010-12-30 06:34:48 +00:00
2012-06-21 19:30:24 +00:00
event . isPropagationStopped = function ( ) { return false ; } ;
2008-12-31 02:58:13 +00:00
event . stopImmediatePropagation ( ) ;
2011-11-06 20:27:42 +00:00
equal ( event . isPropagationStopped ( ) , true , "Verify isPropagationStopped" ) ;
equal ( event . isImmediatePropagationStopped ( ) , true , "Verify isPropagationStopped" ) ;
2010-12-30 06:34:48 +00:00
2011-04-12 08:47:46 +00:00
$parent . bind ( "foo" , function ( e ) {
2008-12-31 02:58:13 +00:00
// Tries bubbling
2011-11-06 20:27:42 +00:00
equal ( e . type , "foo" , "Verify event type when passed passing an event object" ) ;
equal ( e . target . id , "child" , "Verify event.target when passed passing an event object" ) ;
equal ( e . currentTarget . id , "par" , "Verify event.currentTarget when passed passing an event object" ) ;
equal ( e . secret , "boo!" , "Verify event object's custom attribute when passed passing an event object" ) ;
2008-12-25 21:44:54 +00:00
} ) ;
2010-12-30 06:34:48 +00:00
2008-12-31 02:58:13 +00:00
// test with an event object
event = new jQuery . Event ( "foo" ) ;
2011-04-12 08:47:46 +00:00
event . secret = "boo!" ;
2008-12-31 02:58:13 +00:00
$child . trigger ( event ) ;
2010-12-30 06:34:48 +00:00
2008-12-31 02:58:13 +00:00
// test with a literal object
2012-07-05 19:52:13 +00:00
$child . trigger ( { "type" : "foo" , "secret" : "boo!" } ) ;
2010-12-30 06:34:48 +00:00
2008-12-31 02:58:13 +00:00
$parent . unbind ( ) ;
function error ( ) {
ok ( false , "This assertion shouldn't be reached" ) ;
}
2010-12-30 06:34:48 +00:00
2011-04-12 08:47:46 +00:00
$parent . bind ( "foo" , error ) ;
2010-12-30 06:34:48 +00:00
2011-04-12 08:47:46 +00:00
$child . bind ( "foo" , function ( e , a , b , c ) {
2011-11-06 20:27:42 +00:00
equal ( arguments . length , 4 , "Check arguments length" ) ;
equal ( a , 1 , "Check first custom argument" ) ;
equal ( b , 2 , "Check second custom argument" ) ;
equal ( c , 3 , "Check third custom argument" ) ;
2010-12-30 06:34:48 +00:00
2011-11-06 20:27:42 +00:00
equal ( e . isDefaultPrevented ( ) , false , "Verify isDefaultPrevented" ) ;
equal ( e . isPropagationStopped ( ) , false , "Verify isPropagationStopped" ) ;
equal ( e . isImmediatePropagationStopped ( ) , false , "Verify isImmediatePropagationStopped" ) ;
2010-12-30 06:34:48 +00:00
2008-12-31 02:58:13 +00:00
// Skips both errors
e . stopImmediatePropagation ( ) ;
2010-12-30 06:34:48 +00:00
2008-12-31 02:58:13 +00:00
return "result" ;
} ) ;
2010-12-30 06:34:48 +00:00
2009-01-05 23:06:57 +00:00
// We should add this back in when we want to test the order
// in which event handlers are iterated.
2011-04-12 08:47:46 +00:00
//$child.bind("foo", error );
2010-12-30 06:34:48 +00:00
2008-12-31 02:58:13 +00:00
event = new jQuery . Event ( "foo" ) ;
$child . trigger ( event , [ 1 , 2 , 3 ] ) . unbind ( ) ;
2011-11-06 20:27:42 +00:00
equal ( event . result , "result" , "Check event.result attribute" ) ;
2010-12-30 06:34:48 +00:00
2008-12-31 02:58:13 +00:00
// Will error if it bubbles
2011-04-12 08:47:46 +00:00
$child . triggerHandler ( "foo" ) ;
2010-12-30 06:34:48 +00:00
2008-12-31 02:58:13 +00:00
$child . unbind ( ) ;
$parent . unbind ( ) . remove ( ) ;
2011-11-15 16:38:55 +00:00
2011-11-15 15:22:32 +00:00
// Ensure triggerHandler doesn't molest its event object (#xxx)
2012-06-21 19:30:24 +00:00
event = jQuery . Event ( "zowie" ) ;
2011-11-15 15:22:32 +00:00
jQuery ( document ) . triggerHandler ( event ) ;
equal ( event . type , "zowie" , "Verify its type" ) ;
equal ( event . isPropagationStopped ( ) , false , "propagation not stopped" ) ;
equal ( event . isDefaultPrevented ( ) , false , "default not prevented" ) ;
2006-12-28 11:37:07 +00:00
} ) ;
2012-11-03 04:06:50 +00:00
// Explicitly introduce global variable for oldIE so QUnit doesn't complain if checking globals
window . onclick = undefined ;
2011-10-13 20:30:40 +00:00
test ( ".trigger() bubbling on disconnected elements (#10489)" , function ( ) {
expect ( 2 ) ;
jQuery ( window ) . on ( "click" , function ( ) {
ok ( false , "click fired on window" ) ;
} ) ;
jQuery ( "<div><p>hi</p></div>" )
. on ( "click" , function ( ) {
ok ( true , "click fired on div" ) ;
} )
. find ( "p" )
2011-11-11 02:53:07 +00:00
. on ( "click" , function ( ) {
2011-10-13 20:30:40 +00:00
ok ( true , "click fired on p" ) ;
} )
. click ( )
. off ( "click" )
. end ( )
. off ( "click" )
. remove ( ) ;
jQuery ( window ) . off ( "click" ) ;
} ) ;
2011-11-09 00:32:25 +00:00
test ( ".trigger() doesn't bubble load event (#10717)" , function ( ) {
expect ( 1 ) ;
jQuery ( window ) . on ( "load" , function ( ) {
ok ( false , "load fired on window" ) ;
} ) ;
// It's not an image, but as long as it fires load...
2012-10-16 14:17:14 +00:00
jQuery ( "<img src='index.html' />" )
2011-11-09 00:32:25 +00:00
. appendTo ( "body" )
. on ( "load" , function ( ) {
ok ( true , "load fired on img" ) ;
} )
. trigger ( "load" )
. remove ( ) ;
jQuery ( window ) . off ( "load" ) ;
} ) ;
2011-11-16 15:35:53 +00:00
test ( "Delegated events in SVG (#10791)" , function ( ) {
expect ( 2 ) ;
var svg = jQuery (
2012-10-16 14:17:14 +00:00
"<svg height='1' version='1.1' width='1' xmlns='http://www.w3.org/2000/svg'>" +
"<rect class='svg-by-class' x='10' y='20' width='100' height='60' r='10' rx='10' ry='10'></rect>" +
"<rect id='svg-by-id' x='10' y='20' width='100' height='60' r='10' rx='10' ry='10'></rect>" +
"</svg>"
2011-11-16 15:35:53 +00:00
) . appendTo ( "body" ) ;
2012-01-13 01:30:45 +00:00
2011-11-16 15:35:53 +00:00
jQuery ( "body" )
. on ( "click" , "#svg-by-id" , function ( ) {
ok ( true , "delegated id selector" ) ;
} )
2012-07-11 21:33:53 +00:00
. on ( "click" , "[class~='svg-by-class']" , function ( ) {
2011-11-16 15:35:53 +00:00
ok ( true , "delegated class selector" ) ;
} )
2012-07-11 21:33:53 +00:00
. find ( "#svg-by-id, [class~='svg-by-class']" )
2011-11-16 15:35:53 +00:00
. trigger ( "click" )
. end ( )
. off ( "click" ) ;
svg . remove ( ) ;
} ) ;
2012-08-28 14:09:22 +00:00
test ( "Delegated events in forms (#10844; #11145; #8165; #11382, #11764)" , function ( ) {
2012-06-27 01:38:30 +00:00
expect ( 5 ) ;
2011-11-21 16:33:21 +00:00
2012-03-05 02:11:50 +00:00
// Alias names like "id" cause havoc
2011-11-21 16:33:21 +00:00
var form = jQuery (
2012-10-16 14:17:14 +00:00
"<form id='myform'>" +
"<input type='text' name='id' value='secret agent man' />" +
"</form>"
2012-01-12 02:56:18 +00:00
)
. on ( "submit" , function ( event ) {
event . preventDefault ( ) ;
} )
. appendTo ( "body" ) ;
jQuery ( "body" )
. on ( "submit" , "#myform" , function ( ) {
ok ( true , "delegated id selector with aliased id" ) ;
} )
. find ( "#myform" )
. trigger ( "submit" )
. end ( )
. off ( "submit" ) ;
2012-10-16 14:17:14 +00:00
form . append ( "<input type='text' name='disabled' value='differently abled' />" ) ;
2012-01-12 02:56:18 +00:00
jQuery ( "body" )
2011-11-21 16:33:21 +00:00
. on ( "submit" , "#myform" , function ( ) {
2012-01-12 02:56:18 +00:00
ok ( true , "delegated id selector with aliased disabled" ) ;
2011-11-21 16:33:21 +00:00
} )
2012-01-12 02:56:18 +00:00
. find ( "#myform" )
. trigger ( "submit" )
2011-11-21 16:33:21 +00:00
. end ( )
2012-01-12 02:56:18 +00:00
. off ( "submit" ) ;
2011-11-21 16:33:21 +00:00
2012-01-20 03:14:24 +00:00
form
2012-10-16 14:17:14 +00:00
. append ( "<button id='nestyDisabledBtn'><span>Zing</span></button>" )
2012-01-20 03:14:24 +00:00
. on ( "click" , "#nestyDisabledBtn" , function ( ) {
2012-06-27 01:38:30 +00:00
ok ( true , "click on enabled/disabled button with nesty elements" ) ;
} )
. on ( "mouseover" , "#nestyDisabledBtn" , function ( ) {
ok ( true , "mouse on enabled/disabled button with nesty elements" ) ;
2012-01-20 03:14:24 +00:00
} )
2012-06-27 01:38:30 +00:00
. find ( "span" )
. trigger ( "click" ) // yep
. trigger ( "mouseover" ) // yep
. end ( )
2012-01-20 03:14:24 +00:00
. find ( "#nestyDisabledBtn" ) . prop ( "disabled" , true ) . end ( )
2012-06-27 01:38:30 +00:00
. find ( "span" )
. trigger ( "click" ) // nope
. trigger ( "mouseover" ) // yep
. end ( )
2012-01-20 03:14:24 +00:00
. off ( "click" ) ;
2011-11-21 16:33:21 +00:00
form . remove ( ) ;
} ) ;
2012-03-05 02:11:50 +00:00
test ( "Submit event can be stopped (#11049)" , function ( ) {
expect ( 1 ) ;
// Since we manually bubble in IE, make sure inner handlers get a chance to cancel
var form = jQuery (
2012-10-16 14:17:14 +00:00
"<form id='myform'>" +
"<input type='text' name='sue' value='bawls' />" +
"<input type='submit' />" +
"</form>"
2012-03-05 02:11:50 +00:00
)
. appendTo ( "body" ) ;
jQuery ( "body" )
. on ( "submit" , function ( ) {
ok ( true , "submit bubbled on first handler" ) ;
return false ;
} )
. find ( "#myform input[type=submit]" )
. each ( function ( ) { this . click ( ) ; } )
. end ( )
. on ( "submit" , function ( ) {
ok ( false , "submit bubbled on second handler" ) ;
return false ;
} )
. find ( "#myform input[type=submit]" )
. each ( function ( ) {
jQuery ( this . form ) . on ( "submit" , function ( e ) {
e . preventDefault ( ) ;
e . stopPropagation ( ) ;
} ) ;
this . click ( ) ;
} )
. end ( )
. off ( "submit" ) ;
form . remove ( ) ;
} ) ;
2012-08-14 20:10:10 +00:00
// Test beforeunload event only if it supported (i.e. not Opera)
if ( window . onbeforeunload === null ) {
asyncTest ( "on(beforeunload)" , 4 , function ( ) {
2012-10-21 18:17:46 +00:00
var win ,
2012-08-14 20:10:10 +00:00
forIE6 = 0 ,
2012-10-21 18:17:46 +00:00
fired = false ,
2012-08-14 20:10:10 +00:00
iframe = jQuery ( "<iframe src='data/iframe.html' />" ) ;
iframe . appendTo ( "#qunit-fixture" ) . one ( "load" , function ( ) {
2012-10-21 18:17:46 +00:00
win = this . contentWindow || this . contentDocument ;
2012-08-14 20:10:10 +00:00
2012-10-21 18:17:46 +00:00
jQuery ( win ) . on ( "beforeunload" , function ( ) {
fired = true ;
2012-08-14 20:10:10 +00:00
ok ( true , "beforeunload event is fired" ) ;
} ) ;
2012-10-21 18:17:46 +00:00
strictEqual ( win . onbeforeunload , null , "onbeforeunload property on window object still equals null" ) ;
2012-08-14 20:10:10 +00:00
2012-10-21 18:17:46 +00:00
// In old Safari beforeunload event will not fire on iframes
jQuery ( win ) . on ( "unload" , function ( ) {
if ( ! fired ) {
ok ( true , "This is suppose to be true only in old Safari" ) ;
checker ( ) ;
}
} ) ;
jQuery ( win ) . on ( "beforeunload" , function ( ) {
2012-08-14 20:10:10 +00:00
// On iframe in IE6 beforeunload event will not fire if event is binded through window object,
// nevertheless, test should continue
window . setTimeout ( function ( ) {
if ( ! forIE6 ) {
checker ( ) ;
}
} ) ;
} ) ;
2012-10-21 18:17:46 +00:00
win . onbeforeunload = function ( ) {
2012-08-14 20:10:10 +00:00
if ( ! forIE6 ) {
forIE6 ++ ;
checker ( ) ;
}
} ;
2012-06-18 02:42:24 +00:00
2012-08-14 20:10:10 +00:00
function checker ( ) {
ok ( true , "window.onbeforeunload handler is called" ) ;
iframe = jQuery ( "<iframe src='data/iframe.html' />" ) ;
2012-06-18 02:42:24 +00:00
2012-08-14 20:10:10 +00:00
iframe . appendTo ( "#qunit-fixture" ) . one ( "load" , function ( ) {
2012-10-21 18:17:46 +00:00
win = iframe [ 0 ] . contentWindow || iframe [ 0 ] . contentDocument ;
2012-06-18 02:42:24 +00:00
2012-10-21 18:17:46 +00:00
jQuery ( win ) . on ( "beforeunload" , function ( ) {
strictEqual ( win . onbeforeunload , null , "Event handler is fired, even when onbeforeunload property on window is nulled" ) ;
2012-06-18 02:42:24 +00:00
2012-08-14 20:10:10 +00:00
start ( ) ;
} ) ;
2012-06-18 02:42:24 +00:00
2012-10-21 18:17:46 +00:00
jQuery ( win ) . on ( "unload" , function ( ) {
if ( ! fired ) {
jQuery ( win ) . trigger ( "beforeunload" ) ;
}
2012-10-22 00:19:51 +00:00
} ) ;
2012-10-21 18:17:46 +00:00
win . onbeforeunload = null ;
2012-08-14 20:10:10 +00:00
2012-10-21 18:17:46 +00:00
win . location . reload ( ) ;
2012-08-14 20:10:10 +00:00
} ) ;
}
2012-10-21 18:17:46 +00:00
win . location . reload ( ) ;
2012-08-14 20:10:10 +00:00
} ) ;
} ) ;
}
2012-06-18 02:42:24 +00:00
2011-04-12 23:29:09 +00:00
test ( "jQuery.Event( type, props )" , function ( ) {
2011-04-05 19:55:40 +00:00
2011-09-29 14:34:40 +00:00
expect ( 5 ) ;
2011-04-05 19:55:40 +00:00
2011-04-12 23:29:09 +00:00
var event = jQuery . Event ( "keydown" , { keyCode : 64 } ) ,
2011-04-05 19:55:40 +00:00
handler = function ( event ) {
ok ( "keyCode" in event , "Special property 'keyCode' exists" ) ;
equal ( event . keyCode , 64 , "event.keyCode has explicit value '64'" ) ;
} ;
// Supports jQuery.Event implementation
equal ( event . type , "keydown" , "Verify type" ) ;
2011-10-12 00:30:07 +00:00
2011-09-29 14:34:40 +00:00
// ensure "type" in props won't clobber the one set by constructor
equal ( jQuery . inArray ( "type" , jQuery . event . props ) , - 1 , "'type' property not in props (#10375)" ) ;
2011-04-05 19:55:40 +00:00
ok ( "keyCode" in event , "Special 'keyCode' property exists" ) ;
jQuery ( "body" ) . bind ( "keydown" , handler ) . trigger ( event ) ;
jQuery ( "body" ) . unbind ( "keydown" ) ;
} ) ;
2009-02-17 12:38:16 +00:00
test ( "jQuery.Event.currentTarget" , function ( ) {
2011-10-24 15:17:24 +00:00
expect ( 2 ) ;
2011-11-11 02:53:07 +00:00
2012-07-02 15:30:22 +00:00
jQuery ( "<div><p><button>shiny</button></p></div>" )
2011-10-24 15:17:24 +00:00
. on ( "click" , "p" , function ( e ) {
2011-11-06 20:27:42 +00:00
equal ( e . currentTarget , this , "Check delegated currentTarget on event" ) ;
2011-10-24 15:17:24 +00:00
} )
. find ( "button" )
. on ( "click" , function ( e ) {
2011-11-06 20:27:42 +00:00
equal ( e . currentTarget , this , "Check currentTarget on event" ) ;
2011-10-24 15:17:24 +00:00
} )
. click ( )
. off ( "click" )
. end ( )
. off ( "click" ) ;
2009-02-17 12:38:16 +00:00
} ) ;
2008-12-22 04:59:34 +00:00
test ( ".live()/.die()" , function ( ) {
2011-10-24 15:17:24 +00:00
expect ( 66 ) ;
2008-12-22 04:59:34 +00:00
var submit = 0 , div = 0 , livea = 0 , liveb = 0 ;
2012-12-11 00:07:07 +00:00
jQuery ( "#qunit-fixture div" ) . live ( "submit" , function ( ) { submit ++ ; return false ; } ) ;
jQuery ( "#qunit-fixture div" ) . live ( "click" , function ( ) { div ++ ; } ) ;
2008-12-22 04:59:34 +00:00
jQuery ( "div#nothiddendiv" ) . live ( "click" , function ( ) { livea ++ ; } ) ;
jQuery ( "div#nothiddendivchild" ) . live ( "click" , function ( ) { liveb ++ ; } ) ;
// Nothing should trigger on the body
jQuery ( "body" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( submit , 0 , "Click on body" ) ;
equal ( div , 0 , "Click on body" ) ;
equal ( livea , 0 , "Click on body" ) ;
equal ( liveb , 0 , "Click on body" ) ;
2008-12-22 04:59:34 +00:00
// This should trigger two events
2012-06-21 19:30:24 +00:00
submit = 0 ; div = 0 ; livea = 0 ; liveb = 0 ;
2008-12-22 04:59:34 +00:00
jQuery ( "div#nothiddendiv" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( submit , 0 , "Click on div" ) ;
equal ( div , 1 , "Click on div" ) ;
equal ( livea , 1 , "Click on div" ) ;
equal ( liveb , 0 , "Click on div" ) ;
2008-12-22 04:59:34 +00:00
// This should trigger three events (w/ bubbling)
2012-06-21 19:30:24 +00:00
submit = 0 ; div = 0 ; livea = 0 ; liveb = 0 ;
2008-12-22 04:59:34 +00:00
jQuery ( "div#nothiddendivchild" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( submit , 0 , "Click on inner div" ) ;
equal ( div , 2 , "Click on inner div" ) ;
equal ( livea , 1 , "Click on inner div" ) ;
equal ( liveb , 1 , "Click on inner div" ) ;
2008-12-22 04:59:34 +00:00
// This should trigger one submit
2012-06-21 19:30:24 +00:00
submit = 0 ; div = 0 ; livea = 0 ; liveb = 0 ;
2008-12-22 04:59:34 +00:00
jQuery ( "div#nothiddendivchild" ) . trigger ( "submit" ) ;
2011-11-06 20:27:42 +00:00
equal ( submit , 1 , "Submit on div" ) ;
equal ( div , 0 , "Submit on div" ) ;
equal ( livea , 0 , "Submit on div" ) ;
equal ( liveb , 0 , "Submit on div" ) ;
2008-12-22 04:59:34 +00:00
// Make sure no other events were removed in the process
2012-06-21 19:30:24 +00:00
submit = 0 ; div = 0 ; livea = 0 ; liveb = 0 ;
2008-12-22 04:59:34 +00:00
jQuery ( "div#nothiddendivchild" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( submit , 0 , "die Click on inner div" ) ;
equal ( div , 2 , "die Click on inner div" ) ;
equal ( livea , 1 , "die Click on inner div" ) ;
equal ( liveb , 1 , "die Click on inner div" ) ;
2008-12-22 04:59:34 +00:00
// Now make sure that the removal works
2012-06-21 19:30:24 +00:00
submit = 0 ; div = 0 ; livea = 0 ; liveb = 0 ;
2008-12-22 04:59:34 +00:00
jQuery ( "div#nothiddendivchild" ) . die ( "click" ) ;
jQuery ( "div#nothiddendivchild" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( submit , 0 , "die Click on inner div" ) ;
equal ( div , 2 , "die Click on inner div" ) ;
equal ( livea , 1 , "die Click on inner div" ) ;
equal ( liveb , 0 , "die Click on inner div" ) ;
2008-12-22 04:59:34 +00:00
// Make sure that the click wasn't removed too early
2012-06-21 19:30:24 +00:00
submit = 0 ; div = 0 ; livea = 0 ; liveb = 0 ;
2008-12-22 04:59:34 +00:00
jQuery ( "div#nothiddendiv" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( submit , 0 , "die Click on inner div" ) ;
equal ( div , 1 , "die Click on inner div" ) ;
equal ( livea , 1 , "die Click on inner div" ) ;
equal ( liveb , 0 , "die Click on inner div" ) ;
2008-12-22 04:59:34 +00:00
2009-01-20 17:25:37 +00:00
// Make sure that stopPropgation doesn't stop live events
2012-06-21 19:30:24 +00:00
submit = 0 ; div = 0 ; livea = 0 ; liveb = 0 ;
2009-01-20 17:25:37 +00:00
jQuery ( "div#nothiddendivchild" ) . live ( "click" , function ( e ) { liveb ++ ; e . stopPropagation ( ) ; } ) ;
jQuery ( "div#nothiddendivchild" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( submit , 0 , "stopPropagation Click on inner div" ) ;
equal ( div , 1 , "stopPropagation Click on inner div" ) ;
equal ( livea , 0 , "stopPropagation Click on inner div" ) ;
equal ( liveb , 1 , "stopPropagation Click on inner div" ) ;
2009-01-20 17:25:37 +00:00
2010-01-23 16:12:26 +00:00
// Make sure click events only fire with primary click
2012-06-21 19:30:24 +00:00
submit = 0 ; div = 0 ; livea = 0 ; liveb = 0 ;
2010-01-23 16:12:26 +00:00
var event = jQuery . Event ( "click" ) ;
event . button = 1 ;
jQuery ( "div#nothiddendiv" ) . trigger ( event ) ;
2011-11-06 20:27:42 +00:00
equal ( livea , 0 , "live secondary click" ) ;
2010-01-23 16:12:26 +00:00
2009-01-20 17:25:37 +00:00
jQuery ( "div#nothiddendivchild" ) . die ( "click" ) ;
2008-12-22 04:59:34 +00:00
jQuery ( "div#nothiddendiv" ) . die ( "click" ) ;
2012-12-11 00:07:07 +00:00
jQuery ( "#qunit-fixture div" ) . die ( "click" ) ;
jQuery ( "#qunit-fixture div" ) . die ( "submit" ) ;
2008-12-30 20:45:33 +00:00
2009-03-20 03:10:07 +00:00
// Test binding with a different context
2011-04-17 06:43:57 +00:00
var clicked = 0 , container = jQuery ( "#qunit-fixture" ) [ 0 ] ;
2009-03-20 03:10:07 +00:00
jQuery ( "#foo" , container ) . live ( "click" , function ( e ) { clicked ++ ; } ) ;
2012-12-11 00:07:07 +00:00
jQuery ( "#qunit-fixture div" ) . trigger ( "click" ) ;
2011-04-12 08:47:46 +00:00
jQuery ( "#foo" ) . trigger ( "click" ) ;
2011-04-17 06:43:57 +00:00
jQuery ( "#qunit-fixture" ) . trigger ( "click" ) ;
2011-04-12 08:47:46 +00:00
jQuery ( "body" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( clicked , 2 , "live with a context" ) ;
2009-03-20 03:10:07 +00:00
// Test unbinding with a different context
jQuery ( "#foo" , container ) . die ( "click" ) ;
2011-04-12 08:47:46 +00:00
jQuery ( "#foo" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( clicked , 2 , "die with a context" ) ;
2009-03-20 03:10:07 +00:00
2009-04-30 21:44:25 +00:00
// Test binding with event data
2011-11-06 20:27:42 +00:00
jQuery ( "#foo" ) . live ( "click" , true , function ( e ) { equal ( e . data , true , "live with event data" ) ; } ) ;
2009-04-30 21:44:25 +00:00
jQuery ( "#foo" ) . trigger ( "click" ) . die ( "click" ) ;
2009-03-20 03:10:07 +00:00
2009-04-30 21:50:15 +00:00
// Test binding with trigger data
2011-11-06 20:27:42 +00:00
jQuery ( "#foo" ) . live ( "click" , function ( e , data ) { equal ( data , true , "live with trigger data" ) ; } ) ;
2009-04-30 21:50:15 +00:00
jQuery ( "#foo" ) . trigger ( "click" , true ) . die ( "click" ) ;
2009-05-07 00:50:28 +00:00
// Test binding with different this object
2011-11-06 20:27:42 +00:00
jQuery ( "#foo" ) . live ( "click" , jQuery . proxy ( function ( e ) { equal ( this . foo , "bar" , "live with event scope" ) ; } , { foo : "bar" } ) ) ;
2009-05-07 00:50:28 +00:00
jQuery ( "#foo" ) . trigger ( "click" ) . die ( "click" ) ;
// Test binding with different this object, event data, and trigger data
2009-12-31 05:37:23 +00:00
jQuery ( "#foo" ) . live ( "click" , true , jQuery . proxy ( function ( e , data ) {
2011-11-06 20:27:42 +00:00
equal ( e . data , true , "live with with different this object, event data, and trigger data" ) ;
2012-07-05 19:52:13 +00:00
equal ( this [ "foo" ] , "bar" , "live with with different this object, event data, and trigger data" ) ;
2012-06-21 19:30:24 +00:00
equal ( data , true , "live with with different this object, event data, and trigger data" ) ;
2012-07-05 19:52:13 +00:00
} , { "foo" : "bar" } ) ) ;
2009-05-07 00:50:28 +00:00
jQuery ( "#foo" ) . trigger ( "click" , true ) . die ( "click" ) ;
2008-12-30 20:45:33 +00:00
// Verify that return false prevents default action
jQuery ( "#anchor2" ) . live ( "click" , function ( ) { return false ; } ) ;
var hash = window . location . hash ;
jQuery ( "#anchor2" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( window . location . hash , hash , "return false worked" ) ;
2008-12-30 20:45:33 +00:00
jQuery ( "#anchor2" ) . die ( "click" ) ;
// Verify that .preventDefault() prevents default action
jQuery ( "#anchor2" ) . live ( "click" , function ( e ) { e . preventDefault ( ) ; } ) ;
2012-06-21 19:30:24 +00:00
hash = window . location . hash ;
2008-12-30 20:45:33 +00:00
jQuery ( "#anchor2" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( window . location . hash , hash , "e.preventDefault() worked" ) ;
2008-12-30 20:45:33 +00:00
jQuery ( "#anchor2" ) . die ( "click" ) ;
2009-01-09 22:10:42 +00:00
// Test binding the same handler to multiple points
var called = 0 ;
function callback ( ) { called ++ ; return false ; }
jQuery ( "#nothiddendiv" ) . live ( "click" , callback ) ;
jQuery ( "#anchor2" ) . live ( "click" , callback ) ;
jQuery ( "#nothiddendiv" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( called , 1 , "Verify that only one click occurred." ) ;
2009-01-09 22:10:42 +00:00
2010-03-09 17:22:25 +00:00
called = 0 ;
2009-01-09 22:10:42 +00:00
jQuery ( "#anchor2" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( called , 1 , "Verify that only one click occurred." ) ;
2009-01-09 22:10:42 +00:00
// Make sure that only one callback is removed
jQuery ( "#anchor2" ) . die ( "click" , callback ) ;
2010-03-09 17:22:25 +00:00
called = 0 ;
2009-01-09 22:10:42 +00:00
jQuery ( "#nothiddendiv" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( called , 1 , "Verify that only one click occurred." ) ;
2009-01-09 22:10:42 +00:00
2010-03-09 17:22:25 +00:00
called = 0 ;
2009-01-09 22:10:42 +00:00
jQuery ( "#anchor2" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( called , 0 , "Verify that no click occurred." ) ;
2009-01-09 22:14:48 +00:00
// Make sure that it still works if the selector is the same,
// but the event type is different
jQuery ( "#nothiddendiv" ) . live ( "foo" , callback ) ;
2009-01-09 22:10:42 +00:00
// Cleanup
jQuery ( "#nothiddendiv" ) . die ( "click" , callback ) ;
2009-01-09 22:14:48 +00:00
2010-03-09 17:22:25 +00:00
called = 0 ;
2009-01-09 22:14:48 +00:00
jQuery ( "#nothiddendiv" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( called , 0 , "Verify that no click occurred." ) ;
2009-01-09 22:14:48 +00:00
2010-03-09 17:22:25 +00:00
called = 0 ;
2009-01-09 22:14:48 +00:00
jQuery ( "#nothiddendiv" ) . trigger ( "foo" ) ;
2011-11-06 20:27:42 +00:00
equal ( called , 1 , "Verify that one foo occurred." ) ;
2009-01-09 22:14:48 +00:00
// Cleanup
jQuery ( "#nothiddendiv" ) . die ( "foo" , callback ) ;
2010-12-30 06:34:48 +00:00
2009-01-10 19:57:07 +00:00
// Make sure we don't loose the target by DOM modifications
// after the bubble already reached the liveHandler
2011-04-12 08:47:46 +00:00
var livec = 0 , elemDiv = jQuery ( "#nothiddendivchild" ) . html ( "<span></span>" ) . get ( 0 ) ;
2010-12-30 06:34:48 +00:00
2011-04-12 08:47:46 +00:00
jQuery ( "#nothiddendivchild" ) . live ( "click" , function ( e ) { jQuery ( "#nothiddendivchild" ) . html ( "" ) ; } ) ;
2009-01-10 19:57:07 +00:00
jQuery ( "#nothiddendivchild" ) . live ( "click" , function ( e ) { if ( e . target ) { livec ++ ; } } ) ;
2010-12-30 06:34:48 +00:00
2009-01-10 19:57:07 +00:00
jQuery ( "#nothiddendiv span" ) . click ( ) ;
2011-11-06 20:27:42 +00:00
equal ( jQuery ( "#nothiddendiv span" ) . length , 0 , "Verify that first handler occurred and modified the DOM." ) ;
equal ( livec , 1 , "Verify that second handler occurred even with nuked target." ) ;
2010-12-30 06:34:48 +00:00
2009-01-10 19:57:07 +00:00
// Cleanup
jQuery ( "#nothiddendivchild" ) . die ( "click" ) ;
2009-02-09 23:29:57 +00:00
// Verify that .live() ocurs and cancel buble in the same order as
// we would expect .bind() and .click() without delegation
var lived = 0 , livee = 0 ;
2010-12-30 06:34:48 +00:00
2009-02-09 23:29:57 +00:00
// bind one pair in one order
2011-04-12 08:47:46 +00:00
jQuery ( "span#liveSpan1 a" ) . live ( "click" , function ( ) { lived ++ ; return false ; } ) ;
jQuery ( "span#liveSpan1" ) . live ( "click" , function ( ) { livee ++ ; } ) ;
2009-02-09 23:29:57 +00:00
2011-04-12 08:47:46 +00:00
jQuery ( "span#liveSpan1 a" ) . click ( ) ;
2011-11-06 20:27:42 +00:00
equal ( lived , 1 , "Verify that only one first handler occurred." ) ;
equal ( livee , 0 , "Verify that second handler doesn't." ) ;
2009-02-09 23:29:57 +00:00
// and one pair in inverse
2011-04-12 08:47:46 +00:00
jQuery ( "span#liveSpan2" ) . live ( "click" , function ( ) { livee ++ ; } ) ;
jQuery ( "span#liveSpan2 a" ) . live ( "click" , function ( ) { lived ++ ; return false ; } ) ;
2009-02-09 23:29:57 +00:00
2009-11-30 18:50:25 +00:00
lived = 0 ;
livee = 0 ;
2011-04-12 08:47:46 +00:00
jQuery ( "span#liveSpan2 a" ) . click ( ) ;
2011-11-06 20:27:42 +00:00
equal ( lived , 1 , "Verify that only one first handler occurred." ) ;
equal ( livee , 0 , "Verify that second handler doesn't." ) ;
2010-12-30 06:34:48 +00:00
2009-02-09 23:29:57 +00:00
// Cleanup
2012-06-21 19:30:24 +00:00
jQuery ( "span#liveSpan1 a" ) . die ( "click" ) ;
2010-02-01 23:06:03 +00:00
jQuery ( "span#liveSpan1" ) . die ( "click" ) ;
jQuery ( "span#liveSpan2 a" ) . die ( "click" ) ;
jQuery ( "span#liveSpan2" ) . die ( "click" ) ;
2010-12-30 06:34:48 +00:00
2009-02-23 13:27:48 +00:00
// Test this, target and currentTarget are correct
2011-04-12 08:47:46 +00:00
jQuery ( "span#liveSpan1" ) . live ( "click" , function ( e ) {
2011-11-06 20:27:42 +00:00
equal ( this . id , "liveSpan1" , "Check the this within a live handler" ) ;
equal ( e . currentTarget . id , "liveSpan1" , "Check the event.currentTarget within a live handler" ) ;
equal ( e . delegateTarget , document , "Check the event.delegateTarget within a live handler" ) ;
equal ( e . target . nodeName . toUpperCase ( ) , "A" , "Check the event.target within a live handler" ) ;
2009-02-23 13:27:48 +00:00
} ) ;
2010-12-30 06:34:48 +00:00
2011-04-12 08:47:46 +00:00
jQuery ( "span#liveSpan1 a" ) . click ( ) ;
2010-12-30 06:34:48 +00:00
2011-04-12 08:47:46 +00:00
jQuery ( "span#liveSpan1" ) . die ( "click" ) ;
2009-12-09 22:43:53 +00:00
// Work with deep selectors
livee = 0 ;
function clickB ( ) { livee ++ ; }
jQuery ( "#nothiddendiv div" ) . live ( "click" , function ( ) { livee ++ ; } ) ;
jQuery ( "#nothiddendiv div" ) . live ( "click" , clickB ) ;
jQuery ( "#nothiddendiv div" ) . live ( "mouseover" , function ( ) { livee ++ ; } ) ;
2011-11-06 20:27:42 +00:00
equal ( livee , 0 , "No clicks, deep selector." ) ;
2009-12-09 22:43:53 +00:00
livee = 0 ;
jQuery ( "#nothiddendivchild" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( livee , 2 , "Click, deep selector." ) ;
2009-12-09 22:43:53 +00:00
livee = 0 ;
jQuery ( "#nothiddendivchild" ) . trigger ( "mouseover" ) ;
2011-11-06 20:27:42 +00:00
equal ( livee , 1 , "Mouseover, deep selector." ) ;
2009-12-09 22:43:53 +00:00
jQuery ( "#nothiddendiv div" ) . die ( "mouseover" ) ;
livee = 0 ;
jQuery ( "#nothiddendivchild" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( livee , 2 , "Click, deep selector." ) ;
2009-12-09 22:43:53 +00:00
livee = 0 ;
jQuery ( "#nothiddendivchild" ) . trigger ( "mouseover" ) ;
2011-11-06 20:27:42 +00:00
equal ( livee , 0 , "Mouseover, deep selector." ) ;
2009-12-09 22:43:53 +00:00
jQuery ( "#nothiddendiv div" ) . die ( "click" , clickB ) ;
livee = 0 ;
jQuery ( "#nothiddendivchild" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( livee , 1 , "Click, deep selector." ) ;
2009-12-09 22:43:53 +00:00
jQuery ( "#nothiddendiv div" ) . die ( "click" ) ;
2010-02-13 10:37:17 +00:00
2011-11-09 04:08:04 +00:00
// blur a non-input element, we should force-fire its handlers
// regardless of whether it's burring or not (unlike browsers)
2011-10-12 02:55:10 +00:00
jQuery ( "#nothiddendiv div" )
. live ( "blur" , function ( ) {
ok ( true , "Live div trigger blur." ) ;
} )
. trigger ( "blur" )
. die ( "blur" ) ;
2008-12-22 04:59:34 +00:00
} ) ;
2010-01-23 21:37:12 +00:00
test ( "die all bound events" , function ( ) {
expect ( 1 ) ;
var count = 0 ;
var div = jQuery ( "div#nothiddendivchild" ) ;
div . live ( "click submit" , function ( ) { count ++ ; } ) ;
div . die ( ) ;
div . trigger ( "click" ) ;
div . trigger ( "submit" ) ;
2011-11-06 20:27:42 +00:00
equal ( count , 0 , "Make sure no events were triggered." ) ;
2010-01-23 21:37:12 +00:00
} ) ;
2010-01-23 16:56:24 +00:00
test ( "live with multiple events" , function ( ) {
expect ( 1 ) ;
var count = 0 ;
2010-01-23 21:37:12 +00:00
var div = jQuery ( "div#nothiddendivchild" ) ;
2010-01-23 16:56:24 +00:00
div . live ( "click submit" , function ( ) { count ++ ; } ) ;
div . trigger ( "click" ) ;
div . trigger ( "submit" ) ;
2011-11-06 20:27:42 +00:00
equal ( count , 2 , "Make sure both the click and submit were triggered." ) ;
2011-01-09 21:58:47 +00:00
// manually clean up events from elements outside the fixture
div . die ( ) ;
2010-01-23 16:56:24 +00:00
} ) ;
2010-02-05 02:36:32 +00:00
test ( "live with namespaces" , function ( ) {
2011-09-20 13:16:13 +00:00
expect ( 15 ) ;
2010-02-05 02:36:32 +00:00
var count1 = 0 , count2 = 0 ;
2010-03-02 22:34:12 +00:00
jQuery ( "#liveSpan1" ) . live ( "foo.bar" , function ( e ) {
2011-11-06 20:27:42 +00:00
equal ( e . namespace , "bar" , "namespace is bar" ) ;
2010-02-05 02:36:32 +00:00
count1 ++ ;
} ) ;
2010-03-02 22:34:12 +00:00
jQuery ( "#liveSpan1" ) . live ( "foo.zed" , function ( e ) {
2011-11-06 20:27:42 +00:00
equal ( e . namespace , "zed" , "namespace is zed" ) ;
2010-02-05 02:36:32 +00:00
count2 ++ ;
} ) ;
jQuery ( "#liveSpan1" ) . trigger ( "foo.bar" ) ;
2011-11-06 20:27:42 +00:00
equal ( count1 , 1 , "Got live foo.bar" ) ;
equal ( count2 , 0 , "Got live foo.bar" ) ;
2010-03-02 22:34:12 +00:00
2012-06-21 19:30:24 +00:00
count1 = 0 ; count2 = 0 ;
2010-02-05 02:36:32 +00:00
2010-03-02 22:34:12 +00:00
jQuery ( "#liveSpan1" ) . trigger ( "foo.zed" ) ;
2011-11-06 20:27:42 +00:00
equal ( count1 , 0 , "Got live foo.zed" ) ;
equal ( count2 , 1 , "Got live foo.zed" ) ;
2010-02-05 02:36:32 +00:00
//remove one
2012-06-21 19:30:24 +00:00
count1 = 0 ; count2 = 0 ;
2010-03-02 22:34:12 +00:00
jQuery ( "#liveSpan1" ) . die ( "foo.zed" ) ;
2010-02-05 02:36:32 +00:00
jQuery ( "#liveSpan1" ) . trigger ( "foo.bar" ) ;
2011-11-06 20:27:42 +00:00
equal ( count1 , 1 , "Got live foo.bar after dieing foo.zed" ) ;
equal ( count2 , 0 , "Got live foo.bar after dieing foo.zed" ) ;
2010-02-05 02:36:32 +00:00
2012-06-21 19:30:24 +00:00
count1 = 0 ; count2 = 0 ;
2010-03-02 22:34:12 +00:00
jQuery ( "#liveSpan1" ) . trigger ( "foo.zed" ) ;
2011-11-06 20:27:42 +00:00
equal ( count1 , 0 , "Got live foo.zed" ) ;
equal ( count2 , 0 , "Got live foo.zed" ) ;
2010-02-05 02:36:32 +00:00
//remove the other
jQuery ( "#liveSpan1" ) . die ( "foo.bar" ) ;
2012-06-21 19:30:24 +00:00
count1 = 0 ; count2 = 0 ;
2010-03-02 22:34:12 +00:00
2010-02-05 02:36:32 +00:00
jQuery ( "#liveSpan1" ) . trigger ( "foo.bar" ) ;
2011-11-06 20:27:42 +00:00
equal ( count1 , 0 , "Did not respond to foo.bar after dieing it" ) ;
equal ( count2 , 0 , "Did not respond to foo.bar after dieing it" ) ;
2010-02-05 02:36:32 +00:00
2010-03-02 22:34:12 +00:00
jQuery ( "#liveSpan1" ) . trigger ( "foo.zed" ) ;
2011-11-06 20:27:42 +00:00
equal ( count1 , 0 , "Did not trigger foo.zed again" ) ;
equal ( count2 , 0 , "Did not trigger foo.zed again" ) ;
2010-02-05 02:36:32 +00:00
} ) ;
2009-12-04 16:28:50 +00:00
test ( "live with change" , function ( ) {
2010-10-15 02:37:56 +00:00
expect ( 8 ) ;
2009-12-04 16:28:50 +00:00
var selectChange = 0 , checkboxChange = 0 ;
2010-12-30 06:34:48 +00:00
2012-06-21 19:30:24 +00:00
var select = jQuery ( "select[name='S1']" ) ;
2009-12-04 16:28:50 +00:00
select . live ( "change" , function ( ) {
selectChange ++ ;
} ) ;
2010-12-30 06:34:48 +00:00
var checkbox = jQuery ( "#check2" ) ,
2009-12-04 16:28:50 +00:00
checkboxFunction = function ( ) {
checkboxChange ++ ;
2012-06-21 19:30:24 +00:00
} ;
2009-12-04 16:28:50 +00:00
checkbox . live ( "change" , checkboxFunction ) ;
2010-12-30 06:34:48 +00:00
2009-12-04 16:28:50 +00:00
// test click on select
// second click that changed it
selectChange = 0 ;
select [ 0 ] . selectedIndex = select [ 0 ] . selectedIndex ? 0 : 1 ;
2009-12-21 20:32:32 +00:00
select . trigger ( "change" ) ;
2011-11-06 20:27:42 +00:00
equal ( selectChange , 1 , "Change on click." ) ;
2010-12-30 06:34:48 +00:00
2009-12-04 16:28:50 +00:00
// test keys on select
selectChange = 0 ;
select [ 0 ] . selectedIndex = select [ 0 ] . selectedIndex ? 0 : 1 ;
2009-12-21 20:32:32 +00:00
select . trigger ( "change" ) ;
2011-11-06 20:27:42 +00:00
equal ( selectChange , 1 , "Change on keyup." ) ;
2010-12-30 06:34:48 +00:00
2009-12-04 16:28:50 +00:00
// test click on checkbox
2009-12-21 20:32:32 +00:00
checkbox . trigger ( "change" ) ;
2011-11-06 20:27:42 +00:00
equal ( checkboxChange , 1 , "Change on checkbox." ) ;
2010-12-30 06:34:48 +00:00
2009-12-04 16:28:50 +00:00
// test blur/focus on text
var text = jQuery ( "#name" ) , textChange = 0 , oldTextVal = text . val ( ) ;
text . live ( "change" , function ( ) {
textChange ++ ;
} ) ;
2010-10-15 02:37:56 +00:00
text . val ( oldTextVal + "foo" ) ;
2009-12-21 20:32:32 +00:00
text . trigger ( "change" ) ;
2011-11-06 20:27:42 +00:00
equal ( textChange , 1 , "Change on text input." ) ;
2009-12-04 16:28:50 +00:00
text . val ( oldTextVal ) ;
text . die ( "change" ) ;
2010-12-30 06:34:48 +00:00
2009-12-04 16:28:50 +00:00
// test blur/focus on password
var password = jQuery ( "#name" ) , passwordChange = 0 , oldPasswordVal = password . val ( ) ;
password . live ( "change" , function ( ) {
passwordChange ++ ;
2009-09-16 08:33:00 +00:00
} ) ;
2009-12-04 16:28:50 +00:00
password . val ( oldPasswordVal + "foo" ) ;
2009-12-21 20:32:32 +00:00
password . trigger ( "change" ) ;
2011-11-06 20:27:42 +00:00
equal ( passwordChange , 1 , "Change on password input." ) ;
2009-12-04 16:28:50 +00:00
password . val ( oldPasswordVal ) ;
password . die ( "change" ) ;
2010-12-30 06:34:48 +00:00
2009-12-04 16:28:50 +00:00
// make sure die works
2010-12-30 06:34:48 +00:00
2009-12-04 16:28:50 +00:00
// die all changes
selectChange = 0 ;
select . die ( "change" ) ;
select [ 0 ] . selectedIndex = select [ 0 ] . selectedIndex ? 0 : 1 ;
2009-12-21 20:32:32 +00:00
select . trigger ( "change" ) ;
2011-11-06 20:27:42 +00:00
equal ( selectChange , 0 , "Die on click works." ) ;
2009-12-04 16:28:50 +00:00
selectChange = 0 ;
select [ 0 ] . selectedIndex = select [ 0 ] . selectedIndex ? 0 : 1 ;
2009-12-21 20:32:32 +00:00
select . trigger ( "change" ) ;
2011-11-06 20:27:42 +00:00
equal ( selectChange , 0 , "Die on keyup works." ) ;
2010-12-30 06:34:48 +00:00
2009-12-04 16:28:50 +00:00
// die specific checkbox
checkbox . die ( "change" , checkboxFunction ) ;
2009-12-21 20:32:32 +00:00
checkbox . trigger ( "change" ) ;
2011-11-06 20:27:42 +00:00
equal ( checkboxChange , 1 , "Die on checkbox." ) ;
2009-12-04 16:28:50 +00:00
} ) ;
test ( "live with submit" , function ( ) {
2011-09-07 15:30:22 +00:00
expect ( 7 ) ;
2011-02-10 02:25:29 +00:00
2009-12-04 16:28:50 +00:00
var count1 = 0 , count2 = 0 ;
2010-12-30 06:34:48 +00:00
2009-12-04 16:28:50 +00:00
jQuery ( "#testForm" ) . live ( "submit" , function ( ev ) {
count1 ++ ;
ev . preventDefault ( ) ;
} ) ;
jQuery ( "body" ) . live ( "submit" , function ( ev ) {
count2 ++ ;
ev . preventDefault ( ) ;
} ) ;
2010-03-09 19:08:33 +00:00
jQuery ( "#testForm input[name=sub1]" ) . submit ( ) ;
2011-11-06 20:27:42 +00:00
equal ( count1 , 1 , "Verify form submit." ) ;
equal ( count2 , 1 , "Verify body submit." ) ;
2010-12-30 06:34:48 +00:00
2011-02-10 02:25:29 +00:00
jQuery ( "#testForm input[name=sub1]" ) . live ( "click" , function ( ev ) {
ok ( true , "cancelling submit still calls click handler" ) ;
} ) ;
jQuery ( "#testForm input[name=sub1]" ) [ 0 ] . click ( ) ;
2011-11-06 20:27:42 +00:00
equal ( count1 , 2 , "Verify form submit." ) ;
equal ( count2 , 2 , "Verify body submit." ) ;
2011-02-10 02:25:29 +00:00
2011-09-07 15:30:22 +00:00
jQuery ( "#testForm button[name=sub4]" ) [ 0 ] . click ( ) ;
2011-11-06 20:27:42 +00:00
equal ( count1 , 3 , "Verify form submit." ) ;
equal ( count2 , 3 , "Verify body submit." ) ;
2011-09-07 15:30:22 +00:00
2009-09-16 08:33:00 +00:00
jQuery ( "#testForm" ) . die ( "submit" ) ;
2011-02-10 02:25:29 +00:00
jQuery ( "#testForm input[name=sub1]" ) . die ( "click" ) ;
2009-12-04 16:28:50 +00:00
jQuery ( "body" ) . die ( "submit" ) ;
2009-09-16 08:33:00 +00:00
} ) ;
2010-03-09 17:22:25 +00:00
test ( "live with special events" , function ( ) {
expect ( 13 ) ;
2012-07-05 19:52:13 +00:00
jQuery . event . special [ "foo" ] = {
2010-03-09 17:22:25 +00:00
setup : function ( data , namespaces , handler ) {
ok ( true , "Setup run." ) ;
} ,
teardown : function ( namespaces ) {
ok ( true , "Teardown run." ) ;
} ,
add : function ( handleObj ) {
ok ( true , "Add run." ) ;
} ,
remove : function ( handleObj ) {
ok ( true , "Remove run." ) ;
} ,
2011-10-27 20:02:54 +00:00
_default : function ( event , arg ) {
ok ( event . type === "foo" && arg == 42 , "Default run with correct args." ) ;
2010-03-09 17:22:25 +00:00
}
} ;
// Run: setup, add
jQuery ( "#liveSpan1" ) . live ( "foo.a" , function ( e ) {
ok ( true , "Handler 1 run." ) ;
} ) ;
// Run: add
jQuery ( "#liveSpan1" ) . live ( "foo.b" , function ( e ) {
ok ( true , "Handler 2 run." ) ;
} ) ;
// Run: Handler 1, Handler 2, Default
2011-10-27 20:02:54 +00:00
jQuery ( "#liveSpan1" ) . trigger ( "foo" , 42 ) ;
2010-03-09 17:22:25 +00:00
// Run: Handler 1, Default
2011-10-27 20:02:54 +00:00
jQuery ( "#liveSpan1" ) . trigger ( "foo.a" , 42 ) ;
2010-03-09 17:22:25 +00:00
// Run: remove
jQuery ( "#liveSpan1" ) . die ( "foo.a" ) ;
// Run: Handler 2, Default
2011-10-27 20:02:54 +00:00
jQuery ( "#liveSpan1" ) . trigger ( "foo" , 42 ) ;
2010-03-09 17:22:25 +00:00
// Run: remove, teardown
jQuery ( "#liveSpan1" ) . die ( "foo" ) ;
2012-07-05 19:52:13 +00:00
delete jQuery . event . special [ "foo" ] ;
2010-03-09 17:22:25 +00:00
} ) ;
2010-02-01 23:06:03 +00:00
test ( ".delegate()/.undelegate()" , function ( ) {
2011-10-24 15:17:24 +00:00
expect ( 65 ) ;
2010-02-01 23:06:03 +00:00
var submit = 0 , div = 0 , livea = 0 , liveb = 0 ;
2012-12-11 00:07:07 +00:00
jQuery ( "#body" ) . delegate ( "#qunit-fixture div" , "submit" , function ( ) { submit ++ ; return false ; } ) ;
jQuery ( "#body" ) . delegate ( "#qunit-fixture div" , "click" , function ( ) { div ++ ; } ) ;
2010-02-01 23:06:03 +00:00
jQuery ( "#body" ) . delegate ( "div#nothiddendiv" , "click" , function ( ) { livea ++ ; } ) ;
jQuery ( "#body" ) . delegate ( "div#nothiddendivchild" , "click" , function ( ) { liveb ++ ; } ) ;
// Nothing should trigger on the body
jQuery ( "body" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( submit , 0 , "Click on body" ) ;
equal ( div , 0 , "Click on body" ) ;
equal ( livea , 0 , "Click on body" ) ;
equal ( liveb , 0 , "Click on body" ) ;
2010-02-01 23:06:03 +00:00
// This should trigger two events
2012-06-21 19:30:24 +00:00
submit = 0 ; div = 0 ; livea = 0 ; liveb = 0 ;
2010-02-01 23:06:03 +00:00
jQuery ( "div#nothiddendiv" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( submit , 0 , "Click on div" ) ;
equal ( div , 1 , "Click on div" ) ;
equal ( livea , 1 , "Click on div" ) ;
equal ( liveb , 0 , "Click on div" ) ;
2010-02-01 23:06:03 +00:00
// This should trigger three events (w/ bubbling)
2012-06-21 19:30:24 +00:00
submit = 0 ; div = 0 ; livea = 0 ; liveb = 0 ;
2010-02-01 23:06:03 +00:00
jQuery ( "div#nothiddendivchild" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( submit , 0 , "Click on inner div" ) ;
equal ( div , 2 , "Click on inner div" ) ;
equal ( livea , 1 , "Click on inner div" ) ;
equal ( liveb , 1 , "Click on inner div" ) ;
2010-02-01 23:06:03 +00:00
// This should trigger one submit
2012-06-21 19:30:24 +00:00
submit = 0 ; div = 0 ; livea = 0 ; liveb = 0 ;
2010-02-01 23:06:03 +00:00
jQuery ( "div#nothiddendivchild" ) . trigger ( "submit" ) ;
2011-11-06 20:27:42 +00:00
equal ( submit , 1 , "Submit on div" ) ;
equal ( div , 0 , "Submit on div" ) ;
equal ( livea , 0 , "Submit on div" ) ;
equal ( liveb , 0 , "Submit on div" ) ;
2010-02-01 23:06:03 +00:00
// Make sure no other events were removed in the process
2012-06-21 19:30:24 +00:00
submit = 0 ; div = 0 ; livea = 0 ; liveb = 0 ;
2010-02-01 23:06:03 +00:00
jQuery ( "div#nothiddendivchild" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( submit , 0 , "undelegate Click on inner div" ) ;
equal ( div , 2 , "undelegate Click on inner div" ) ;
equal ( livea , 1 , "undelegate Click on inner div" ) ;
equal ( liveb , 1 , "undelegate Click on inner div" ) ;
2010-02-01 23:06:03 +00:00
// Now make sure that the removal works
2012-06-21 19:30:24 +00:00
submit = 0 ; div = 0 ; livea = 0 ; liveb = 0 ;
2010-02-01 23:06:03 +00:00
jQuery ( "#body" ) . undelegate ( "div#nothiddendivchild" , "click" ) ;
jQuery ( "div#nothiddendivchild" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( submit , 0 , "undelegate Click on inner div" ) ;
equal ( div , 2 , "undelegate Click on inner div" ) ;
equal ( livea , 1 , "undelegate Click on inner div" ) ;
equal ( liveb , 0 , "undelegate Click on inner div" ) ;
2010-02-01 23:06:03 +00:00
// Make sure that the click wasn't removed too early
2012-06-21 19:30:24 +00:00
submit = 0 ; div = 0 ; livea = 0 ; liveb = 0 ;
2010-02-01 23:06:03 +00:00
jQuery ( "div#nothiddendiv" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( submit , 0 , "undelegate Click on inner div" ) ;
equal ( div , 1 , "undelegate Click on inner div" ) ;
equal ( livea , 1 , "undelegate Click on inner div" ) ;
equal ( liveb , 0 , "undelegate Click on inner div" ) ;
2010-02-01 23:06:03 +00:00
// Make sure that stopPropgation doesn't stop live events
2012-06-21 19:30:24 +00:00
submit = 0 ; div = 0 ; livea = 0 ; liveb = 0 ;
2010-02-01 23:06:03 +00:00
jQuery ( "#body" ) . delegate ( "div#nothiddendivchild" , "click" , function ( e ) { liveb ++ ; e . stopPropagation ( ) ; } ) ;
jQuery ( "div#nothiddendivchild" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( submit , 0 , "stopPropagation Click on inner div" ) ;
equal ( div , 1 , "stopPropagation Click on inner div" ) ;
equal ( livea , 0 , "stopPropagation Click on inner div" ) ;
equal ( liveb , 1 , "stopPropagation Click on inner div" ) ;
2010-02-01 23:06:03 +00:00
// Make sure click events only fire with primary click
2012-06-21 19:30:24 +00:00
submit = 0 ; div = 0 ; livea = 0 ; liveb = 0 ;
2010-02-01 23:06:03 +00:00
var event = jQuery . Event ( "click" ) ;
event . button = 1 ;
jQuery ( "div#nothiddendiv" ) . trigger ( event ) ;
2011-11-06 20:27:42 +00:00
equal ( livea , 0 , "delegate secondary click" ) ;
2010-02-01 23:06:03 +00:00
jQuery ( "#body" ) . undelegate ( "div#nothiddendivchild" , "click" ) ;
jQuery ( "#body" ) . undelegate ( "div#nothiddendiv" , "click" ) ;
2012-12-11 00:07:07 +00:00
jQuery ( "#body" ) . undelegate ( "#qunit-fixture div" , "click" ) ;
jQuery ( "#body" ) . undelegate ( "#qunit-fixture div" , "submit" ) ;
2010-02-01 23:06:03 +00:00
// Test binding with a different context
2011-04-17 06:43:57 +00:00
var clicked = 0 , container = jQuery ( "#qunit-fixture" ) [ 0 ] ;
jQuery ( "#qunit-fixture" ) . delegate ( "#foo" , "click" , function ( e ) { clicked ++ ; } ) ;
2012-12-11 00:07:07 +00:00
jQuery ( "#qunit-fixture div" ) . trigger ( "click" ) ;
2011-04-12 08:47:46 +00:00
jQuery ( "#foo" ) . trigger ( "click" ) ;
2011-04-17 06:43:57 +00:00
jQuery ( "#qunit-fixture" ) . trigger ( "click" ) ;
2011-04-12 08:47:46 +00:00
jQuery ( "body" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( clicked , 2 , "delegate with a context" ) ;
2010-02-01 23:06:03 +00:00
// Test unbinding with a different context
2011-04-17 06:43:57 +00:00
jQuery ( "#qunit-fixture" ) . undelegate ( "#foo" , "click" ) ;
2011-04-12 08:47:46 +00:00
jQuery ( "#foo" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( clicked , 2 , "undelegate with a context" ) ;
2010-02-01 23:06:03 +00:00
// Test binding with event data
2011-11-06 20:27:42 +00:00
jQuery ( "#body" ) . delegate ( "#foo" , "click" , true , function ( e ) { equal ( e . data , true , "delegate with event data" ) ; } ) ;
2010-02-01 23:06:03 +00:00
jQuery ( "#foo" ) . trigger ( "click" ) ;
jQuery ( "#body" ) . undelegate ( "#foo" , "click" ) ;
// Test binding with trigger data
2011-11-06 20:27:42 +00:00
jQuery ( "#body" ) . delegate ( "#foo" , "click" , function ( e , data ) { equal ( data , true , "delegate with trigger data" ) ; } ) ;
2010-02-01 23:06:03 +00:00
jQuery ( "#foo" ) . trigger ( "click" , true ) ;
jQuery ( "#body" ) . undelegate ( "#foo" , "click" ) ;
// Test binding with different this object
2012-07-05 19:52:13 +00:00
jQuery ( "#body" ) . delegate ( "#foo" , "click" , jQuery . proxy ( function ( e ) { equal ( this [ "foo" ] , "bar" , "delegate with event scope" ) ; } , { "foo" : "bar" } ) ) ;
2010-02-01 23:06:03 +00:00
jQuery ( "#foo" ) . trigger ( "click" ) ;
jQuery ( "#body" ) . undelegate ( "#foo" , "click" ) ;
// Test binding with different this object, event data, and trigger data
jQuery ( "#body" ) . delegate ( "#foo" , "click" , true , jQuery . proxy ( function ( e , data ) {
2011-11-06 20:27:42 +00:00
equal ( e . data , true , "delegate with with different this object, event data, and trigger data" ) ;
equal ( this . foo , "bar" , "delegate with with different this object, event data, and trigger data" ) ;
2012-06-21 19:30:24 +00:00
equal ( data , true , "delegate with with different this object, event data, and trigger data" ) ;
2012-07-05 19:52:13 +00:00
} , { "foo" : "bar" } ) ) ;
2010-02-01 23:06:03 +00:00
jQuery ( "#foo" ) . trigger ( "click" , true ) ;
jQuery ( "#body" ) . undelegate ( "#foo" , "click" ) ;
// Verify that return false prevents default action
jQuery ( "#body" ) . delegate ( "#anchor2" , "click" , function ( ) { return false ; } ) ;
var hash = window . location . hash ;
jQuery ( "#anchor2" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( window . location . hash , hash , "return false worked" ) ;
2010-02-01 23:06:03 +00:00
jQuery ( "#body" ) . undelegate ( "#anchor2" , "click" ) ;
// Verify that .preventDefault() prevents default action
jQuery ( "#body" ) . delegate ( "#anchor2" , "click" , function ( e ) { e . preventDefault ( ) ; } ) ;
2012-06-21 19:30:24 +00:00
hash = window . location . hash ;
2010-02-01 23:06:03 +00:00
jQuery ( "#anchor2" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( window . location . hash , hash , "e.preventDefault() worked" ) ;
2010-02-01 23:06:03 +00:00
jQuery ( "#body" ) . undelegate ( "#anchor2" , "click" ) ;
// Test binding the same handler to multiple points
var called = 0 ;
function callback ( ) { called ++ ; return false ; }
jQuery ( "#body" ) . delegate ( "#nothiddendiv" , "click" , callback ) ;
jQuery ( "#body" ) . delegate ( "#anchor2" , "click" , callback ) ;
jQuery ( "#nothiddendiv" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( called , 1 , "Verify that only one click occurred." ) ;
2010-02-01 23:06:03 +00:00
2010-03-09 17:22:25 +00:00
called = 0 ;
2010-02-01 23:06:03 +00:00
jQuery ( "#anchor2" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( called , 1 , "Verify that only one click occurred." ) ;
2010-02-01 23:06:03 +00:00
// Make sure that only one callback is removed
jQuery ( "#body" ) . undelegate ( "#anchor2" , "click" , callback ) ;
2010-03-09 17:22:25 +00:00
called = 0 ;
2010-02-01 23:06:03 +00:00
jQuery ( "#nothiddendiv" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( called , 1 , "Verify that only one click occurred." ) ;
2010-02-01 23:06:03 +00:00
2010-03-09 17:22:25 +00:00
called = 0 ;
2010-02-01 23:06:03 +00:00
jQuery ( "#anchor2" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( called , 0 , "Verify that no click occurred." ) ;
2010-02-01 23:06:03 +00:00
// Make sure that it still works if the selector is the same,
// but the event type is different
jQuery ( "#body" ) . delegate ( "#nothiddendiv" , "foo" , callback ) ;
// Cleanup
jQuery ( "#body" ) . undelegate ( "#nothiddendiv" , "click" , callback ) ;
2010-03-09 17:22:25 +00:00
called = 0 ;
2010-02-01 23:06:03 +00:00
jQuery ( "#nothiddendiv" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( called , 0 , "Verify that no click occurred." ) ;
2010-02-01 23:06:03 +00:00
2010-03-09 17:22:25 +00:00
called = 0 ;
2010-02-01 23:06:03 +00:00
jQuery ( "#nothiddendiv" ) . trigger ( "foo" ) ;
2011-11-06 20:27:42 +00:00
equal ( called , 1 , "Verify that one foo occurred." ) ;
2010-02-01 23:06:03 +00:00
// Cleanup
jQuery ( "#body" ) . undelegate ( "#nothiddendiv" , "foo" , callback ) ;
2010-12-30 06:34:48 +00:00
2010-02-01 23:06:03 +00:00
// Make sure we don't loose the target by DOM modifications
// after the bubble already reached the liveHandler
2011-04-12 08:47:46 +00:00
var livec = 0 , elemDiv = jQuery ( "#nothiddendivchild" ) . html ( "<span></span>" ) . get ( 0 ) ;
2010-12-30 06:34:48 +00:00
2011-04-12 08:47:46 +00:00
jQuery ( "#body" ) . delegate ( "#nothiddendivchild" , "click" , function ( e ) { jQuery ( "#nothiddendivchild" ) . html ( "" ) ; } ) ;
2010-02-01 23:06:03 +00:00
jQuery ( "#body" ) . delegate ( "#nothiddendivchild" , "click" , function ( e ) { if ( e . target ) { livec ++ ; } } ) ;
2010-12-30 06:34:48 +00:00
2010-02-01 23:06:03 +00:00
jQuery ( "#nothiddendiv span" ) . click ( ) ;
2011-11-06 20:27:42 +00:00
equal ( jQuery ( "#nothiddendiv span" ) . length , 0 , "Verify that first handler occurred and modified the DOM." ) ;
equal ( livec , 1 , "Verify that second handler occurred even with nuked target." ) ;
2010-12-30 06:34:48 +00:00
2010-02-01 23:06:03 +00:00
// Cleanup
jQuery ( "#body" ) . undelegate ( "#nothiddendivchild" , "click" ) ;
// Verify that .live() ocurs and cancel buble in the same order as
// we would expect .bind() and .click() without delegation
var lived = 0 , livee = 0 ;
2010-12-30 06:34:48 +00:00
2010-02-01 23:06:03 +00:00
// bind one pair in one order
2011-04-12 08:47:46 +00:00
jQuery ( "#body" ) . delegate ( "span#liveSpan1 a" , "click" , function ( ) { lived ++ ; return false ; } ) ;
jQuery ( "#body" ) . delegate ( "span#liveSpan1" , "click" , function ( ) { livee ++ ; } ) ;
2010-02-01 23:06:03 +00:00
2011-04-12 08:47:46 +00:00
jQuery ( "span#liveSpan1 a" ) . click ( ) ;
2011-11-06 20:27:42 +00:00
equal ( lived , 1 , "Verify that only one first handler occurred." ) ;
equal ( livee , 0 , "Verify that second handler doesn't." ) ;
2010-02-01 23:06:03 +00:00
// and one pair in inverse
2011-04-12 08:47:46 +00:00
jQuery ( "#body" ) . delegate ( "span#liveSpan2" , "click" , function ( ) { livee ++ ; } ) ;
jQuery ( "#body" ) . delegate ( "span#liveSpan2 a" , "click" , function ( ) { lived ++ ; return false ; } ) ;
2010-02-01 23:06:03 +00:00
lived = 0 ;
livee = 0 ;
2011-04-12 08:47:46 +00:00
jQuery ( "span#liveSpan2 a" ) . click ( ) ;
2011-11-06 20:27:42 +00:00
equal ( lived , 1 , "Verify that only one first handler occurred." ) ;
equal ( livee , 0 , "Verify that second handler doesn't." ) ;
2010-12-30 06:34:48 +00:00
2010-02-01 23:06:03 +00:00
// Cleanup
jQuery ( "#body" ) . undelegate ( "click" ) ;
2010-12-30 06:34:48 +00:00
2010-02-01 23:06:03 +00:00
// Test this, target and currentTarget are correct
2011-04-12 08:47:46 +00:00
jQuery ( "#body" ) . delegate ( "span#liveSpan1" , "click" , function ( e ) {
2011-11-06 20:27:42 +00:00
equal ( this . id , "liveSpan1" , "Check the this within a delegate handler" ) ;
equal ( e . currentTarget . id , "liveSpan1" , "Check the event.currentTarget within a delegate handler" ) ;
equal ( e . delegateTarget , document . body , "Check the event.delegateTarget within a delegate handler" ) ;
equal ( e . target . nodeName . toUpperCase ( ) , "A" , "Check the event.target within a delegate handler" ) ;
2010-02-01 23:06:03 +00:00
} ) ;
2010-12-30 06:34:48 +00:00
2011-04-12 08:47:46 +00:00
jQuery ( "span#liveSpan1 a" ) . click ( ) ;
2010-12-30 06:34:48 +00:00
2011-04-12 08:47:46 +00:00
jQuery ( "#body" ) . undelegate ( "span#liveSpan1" , "click" ) ;
2010-02-01 23:06:03 +00:00
// Work with deep selectors
livee = 0 ;
function clickB ( ) { livee ++ ; }
jQuery ( "#body" ) . delegate ( "#nothiddendiv div" , "click" , function ( ) { livee ++ ; } ) ;
jQuery ( "#body" ) . delegate ( "#nothiddendiv div" , "click" , clickB ) ;
jQuery ( "#body" ) . delegate ( "#nothiddendiv div" , "mouseover" , function ( ) { livee ++ ; } ) ;
2011-11-06 20:27:42 +00:00
equal ( livee , 0 , "No clicks, deep selector." ) ;
2010-02-01 23:06:03 +00:00
livee = 0 ;
jQuery ( "#nothiddendivchild" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( livee , 2 , "Click, deep selector." ) ;
2010-02-01 23:06:03 +00:00
livee = 0 ;
jQuery ( "#nothiddendivchild" ) . trigger ( "mouseover" ) ;
2011-11-06 20:27:42 +00:00
equal ( livee , 1 , "Mouseover, deep selector." ) ;
2010-02-01 23:06:03 +00:00
jQuery ( "#body" ) . undelegate ( "#nothiddendiv div" , "mouseover" ) ;
livee = 0 ;
jQuery ( "#nothiddendivchild" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( livee , 2 , "Click, deep selector." ) ;
2010-02-01 23:06:03 +00:00
livee = 0 ;
jQuery ( "#nothiddendivchild" ) . trigger ( "mouseover" ) ;
2011-11-06 20:27:42 +00:00
equal ( livee , 0 , "Mouseover, deep selector." ) ;
2010-02-01 23:06:03 +00:00
jQuery ( "#body" ) . undelegate ( "#nothiddendiv div" , "click" , clickB ) ;
livee = 0 ;
jQuery ( "#nothiddendivchild" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( livee , 1 , "Click, deep selector." ) ;
2010-02-01 23:06:03 +00:00
jQuery ( "#body" ) . undelegate ( "#nothiddendiv div" , "click" ) ;
} ) ;
2011-10-24 02:25:13 +00:00
test ( "jQuery.off using dispatched jQuery.Event" , function ( ) {
expect ( 1 ) ;
2011-11-11 02:53:07 +00:00
2012-10-16 14:17:14 +00:00
var markup = jQuery ( "<p><a href='#'>target</a></p>" ) ,
2011-10-24 02:25:13 +00:00
count = 0 ;
markup
. on ( "click.name" , "a" , function ( event ) {
2011-11-06 20:27:42 +00:00
equal ( ++ count , 1 , "event called once before removal" ) ;
2011-10-24 02:25:13 +00:00
jQuery ( ) . off ( event ) ;
} )
2012-10-16 14:17:14 +00:00
. find ( "a" ) . click ( ) . click ( ) . end ( )
2011-10-24 02:25:13 +00:00
. remove ( ) ;
} ) ;
2012-07-23 16:48:29 +00:00
test ( "delegated event with delegateTarget-relative selector" , function ( ) {
2012-08-28 14:07:16 +00:00
expect ( 3 ) ;
var markup = jQuery ( "<ul><li><a id=\"a0\"></a><ul id=\"ul0\"><li class=test><a id=\"a0_0\"></a></li><li><a id=\"a0_1\"></a></li></ul></li></ul>" ) . appendTo ( "#qunit-fixture" ) ;
2012-07-06 14:12:20 +00:00
2012-08-28 14:07:16 +00:00
// Positional selector (#11315)
2012-06-27 00:36:00 +00:00
markup
2012-07-23 16:48:29 +00:00
. on ( "click" , ">li>a" , function ( ) {
ok ( this . id === "a0" , "child li was clicked" ) ;
} )
. find ( "#ul0" )
. on ( "click" , "li:first>a" , function ( ) {
ok ( this . id === "a0_0" , "first li under #u10 was clicked" ) ;
2012-06-27 00:36:00 +00:00
} )
. end ( )
2012-07-23 16:48:29 +00:00
. find ( "a" ) . click ( ) . end ( )
2012-08-28 14:07:16 +00:00
. find ( "#ul0" ) . off ( ) ;
2012-10-15 21:20:33 +00:00
2012-08-28 14:07:16 +00:00
// Non-positional selector (#12383)
markup = markup . wrap ( "<div />" ) . parent ( ) ;
markup
. find ( "#ul0" )
. on ( "click" , "div li a" , function ( ) {
ok ( false , "div is ABOVE the delegation point!" ) ;
} )
. on ( "click" , "ul a" , function ( ) {
ok ( false , "ul is the delegation point!" ) ;
} )
. on ( "click" , "li.test a" , function ( ) {
ok ( true , "li.test is below the delegation point." ) ;
} )
. find ( "#a0_0" ) . click ( ) ;
2012-10-15 21:20:33 +00:00
2012-08-28 14:07:16 +00:00
markup . remove ( ) ;
2012-06-27 00:36:00 +00:00
} ) ;
2011-10-21 14:56:16 +00:00
test ( "stopPropagation() stops directly-bound events on delegated target" , function ( ) {
expect ( 1 ) ;
2011-11-11 02:53:07 +00:00
2012-07-02 15:30:22 +00:00
var markup = jQuery ( "<div><p><a href=\"#\">target</a></p></div>" ) ;
2011-10-21 14:56:16 +00:00
markup
. on ( "click" , function ( ) {
ok ( false , "directly-bound event on delegate target was called" ) ;
} )
. on ( "click" , "a" , function ( e ) {
e . stopPropagation ( ) ;
ok ( true , "delegated handler was called" ) ;
} )
. find ( "a" ) . click ( ) . end ( )
. remove ( ) ;
} ) ;
2010-02-01 23:06:03 +00:00
test ( "undelegate all bound events" , function ( ) {
2012-08-08 00:49:34 +00:00
expect ( 2 ) ;
2010-02-01 23:06:03 +00:00
2012-08-08 00:49:34 +00:00
var count = 0 ,
clicks = 0 ,
div = jQuery ( "#body" ) ;
2010-02-01 23:06:03 +00:00
2012-08-08 00:49:34 +00:00
div . delegate ( "div#nothiddendivchild" , "click submit" , function ( ) { count ++ ; } ) ;
div . bind ( "click" , function ( ) { clicks ++ ; } ) ;
2010-02-01 23:06:03 +00:00
div . undelegate ( ) ;
jQuery ( "div#nothiddendivchild" ) . trigger ( "click" ) ;
jQuery ( "div#nothiddendivchild" ) . trigger ( "submit" ) ;
2011-11-06 20:27:42 +00:00
equal ( count , 0 , "Make sure no events were triggered." ) ;
2012-08-08 00:49:34 +00:00
div . trigger ( "click" ) ;
equal ( clicks , 2 , "Make sure delegated and directly bound event occurred." ) ;
div . unbind ( "click" ) ;
2010-02-01 23:06:03 +00:00
} ) ;
test ( "delegate with multiple events" , function ( ) {
expect ( 1 ) ;
var count = 0 ;
var div = jQuery ( "#body" ) ;
div . delegate ( "div#nothiddendivchild" , "click submit" , function ( ) { count ++ ; } ) ;
jQuery ( "div#nothiddendivchild" ) . trigger ( "click" ) ;
jQuery ( "div#nothiddendivchild" ) . trigger ( "submit" ) ;
2011-11-06 20:27:42 +00:00
equal ( count , 2 , "Make sure both the click and submit were triggered." ) ;
2010-02-01 23:06:03 +00:00
jQuery ( "#body" ) . undelegate ( ) ;
} ) ;
test ( "delegate with change" , function ( ) {
2010-10-15 02:37:56 +00:00
expect ( 8 ) ;
2010-02-01 23:06:03 +00:00
var selectChange = 0 , checkboxChange = 0 ;
2010-12-30 06:34:48 +00:00
2010-02-01 23:06:03 +00:00
var select = jQuery ( "select[name='S1']" ) ;
jQuery ( "#body" ) . delegate ( "select[name='S1']" , "change" , function ( ) {
selectChange ++ ;
} ) ;
2010-12-30 06:34:48 +00:00
var checkbox = jQuery ( "#check2" ) ,
2010-02-01 23:06:03 +00:00
checkboxFunction = function ( ) {
checkboxChange ++ ;
2012-06-21 19:30:24 +00:00
} ;
2010-02-01 23:06:03 +00:00
jQuery ( "#body" ) . delegate ( "#check2" , "change" , checkboxFunction ) ;
2010-12-30 06:34:48 +00:00
2010-02-01 23:06:03 +00:00
// test click on select
// second click that changed it
selectChange = 0 ;
select [ 0 ] . selectedIndex = select [ 0 ] . selectedIndex ? 0 : 1 ;
select . trigger ( "change" ) ;
2011-11-06 20:27:42 +00:00
equal ( selectChange , 1 , "Change on click." ) ;
2010-12-30 06:34:48 +00:00
2010-02-01 23:06:03 +00:00
// test keys on select
selectChange = 0 ;
select [ 0 ] . selectedIndex = select [ 0 ] . selectedIndex ? 0 : 1 ;
select . trigger ( "change" ) ;
2011-11-06 20:27:42 +00:00
equal ( selectChange , 1 , "Change on keyup." ) ;
2010-12-30 06:34:48 +00:00
2010-02-01 23:06:03 +00:00
// test click on checkbox
checkbox . trigger ( "change" ) ;
2011-11-06 20:27:42 +00:00
equal ( checkboxChange , 1 , "Change on checkbox." ) ;
2010-12-30 06:34:48 +00:00
2010-02-01 23:06:03 +00:00
// test blur/focus on text
var text = jQuery ( "#name" ) , textChange = 0 , oldTextVal = text . val ( ) ;
jQuery ( "#body" ) . delegate ( "#name" , "change" , function ( ) {
textChange ++ ;
} ) ;
2010-10-15 02:37:56 +00:00
text . val ( oldTextVal + "foo" ) ;
2010-02-01 23:06:03 +00:00
text . trigger ( "change" ) ;
2011-11-06 20:27:42 +00:00
equal ( textChange , 1 , "Change on text input." ) ;
2009-09-14 22:04:22 +00:00
2010-02-01 23:06:03 +00:00
text . val ( oldTextVal ) ;
jQuery ( "#body" ) . die ( "change" ) ;
2010-12-30 06:34:48 +00:00
2010-02-01 23:06:03 +00:00
// test blur/focus on password
var password = jQuery ( "#name" ) , passwordChange = 0 , oldPasswordVal = password . val ( ) ;
jQuery ( "#body" ) . delegate ( "#name" , "change" , function ( ) {
passwordChange ++ ;
} ) ;
password . val ( oldPasswordVal + "foo" ) ;
password . trigger ( "change" ) ;
2011-11-06 20:27:42 +00:00
equal ( passwordChange , 1 , "Change on password input." ) ;
2010-02-01 23:06:03 +00:00
password . val ( oldPasswordVal ) ;
jQuery ( "#body" ) . undelegate ( "#name" , "change" ) ;
2010-12-30 06:34:48 +00:00
2010-02-01 23:06:03 +00:00
// make sure die works
2010-12-30 06:34:48 +00:00
2010-02-01 23:06:03 +00:00
// die all changes
selectChange = 0 ;
jQuery ( "#body" ) . undelegate ( "select[name='S1']" , "change" ) ;
select [ 0 ] . selectedIndex = select [ 0 ] . selectedIndex ? 0 : 1 ;
select . trigger ( "change" ) ;
2011-11-06 20:27:42 +00:00
equal ( selectChange , 0 , "Die on click works." ) ;
2010-02-01 23:06:03 +00:00
selectChange = 0 ;
select [ 0 ] . selectedIndex = select [ 0 ] . selectedIndex ? 0 : 1 ;
select . trigger ( "change" ) ;
2011-11-06 20:27:42 +00:00
equal ( selectChange , 0 , "Die on keyup works." ) ;
2010-12-30 06:34:48 +00:00
2010-02-01 23:06:03 +00:00
// die specific checkbox
jQuery ( "#body" ) . undelegate ( "#check2" , "change" , checkboxFunction ) ;
checkbox . trigger ( "change" ) ;
2011-11-06 20:27:42 +00:00
equal ( checkboxChange , 1 , "Die on checkbox." ) ;
2010-02-01 23:06:03 +00:00
} ) ;
test ( "delegate with submit" , function ( ) {
2012-10-15 16:16:49 +00:00
expect ( 2 ) ;
2010-02-01 23:06:03 +00:00
var count1 = 0 , count2 = 0 ;
2010-12-30 06:34:48 +00:00
2010-02-01 23:06:03 +00:00
jQuery ( "#body" ) . delegate ( "#testForm" , "submit" , function ( ev ) {
count1 ++ ;
ev . preventDefault ( ) ;
} ) ;
jQuery ( document ) . delegate ( "body" , "submit" , function ( ev ) {
count2 ++ ;
ev . preventDefault ( ) ;
} ) ;
2010-03-09 19:08:33 +00:00
jQuery ( "#testForm input[name=sub1]" ) . submit ( ) ;
2011-11-06 20:27:42 +00:00
equal ( count1 , 1 , "Verify form submit." ) ;
equal ( count2 , 1 , "Verify body submit." ) ;
2010-12-30 06:34:48 +00:00
2010-02-01 23:06:03 +00:00
jQuery ( "#body" ) . undelegate ( ) ;
jQuery ( document ) . undelegate ( ) ;
2009-09-14 22:04:22 +00:00
} ) ;
2011-04-14 03:10:32 +00:00
test ( "undelegate() with only namespaces" , function ( ) {
2011-04-05 22:12:50 +00:00
expect ( 2 ) ;
var $delegate = jQuery ( "#liveHandlerOrder" ) ,
2011-04-14 03:10:32 +00:00
count = 0 ;
2011-04-05 22:12:50 +00:00
$delegate . delegate ( "a" , "click.ns" , function ( e ) {
count ++ ;
} ) ;
jQuery ( "a" , $delegate ) . eq ( 0 ) . trigger ( "click.ns" ) ;
2011-11-06 20:27:42 +00:00
equal ( count , 1 , "delegated click.ns" ) ;
2011-04-05 22:12:50 +00:00
$delegate . undelegate ( ".ns" ) ;
jQuery ( "a" , $delegate ) . eq ( 1 ) . trigger ( "click.ns" ) ;
2011-11-06 20:27:42 +00:00
equal ( count , 1 , "no more .ns after undelegate" ) ;
2011-04-05 22:12:50 +00:00
} ) ;
2009-05-04 04:54:09 +00:00
test ( "Non DOM element events" , function ( ) {
2010-03-02 15:51:31 +00:00
expect ( 1 ) ;
2009-05-04 04:54:09 +00:00
var o = { } ;
2011-04-12 08:47:46 +00:00
jQuery ( o ) . bind ( "nonelementobj" , function ( e ) {
2010-03-02 15:51:31 +00:00
ok ( true , "Event on non-DOM object triggered" ) ;
} ) ;
2009-05-04 04:54:09 +00:00
2011-04-12 08:47:46 +00:00
jQuery ( o ) . trigger ( "nonelementobj" ) ;
2009-05-04 04:54:09 +00:00
} ) ;
2011-11-07 16:07:36 +00:00
test ( "inline handler returning false stops default" , function ( ) {
expect ( 1 ) ;
2011-11-11 02:53:07 +00:00
2012-07-02 15:30:22 +00:00
var markup = jQuery ( "<div><a href=\"#\" onclick=\"return false\">x</a></div>" ) ;
2011-11-07 16:07:36 +00:00
markup . click ( function ( e ) {
ok ( e . isDefaultPrevented ( ) , "inline handler prevented default" ) ;
return false ;
} ) ;
markup . find ( "a" ) . click ( ) ;
markup . off ( "click" ) ;
} ) ;
2010-10-11 22:03:54 +00:00
test ( "window resize" , function ( ) {
2010-10-11 22:20:57 +00:00
expect ( 2 ) ;
jQuery ( window ) . unbind ( ) ;
2010-10-11 22:03:54 +00:00
jQuery ( window ) . bind ( "resize" , function ( ) {
ok ( true , "Resize event fired." ) ;
} ) . resize ( ) . unbind ( "resize" ) ;
2010-10-11 22:20:57 +00:00
2011-01-09 21:52:33 +00:00
ok ( ! jQuery . _data ( window , "__events__" ) , "Make sure all the events are gone." ) ;
2010-10-11 22:03:54 +00:00
} ) ;
2010-10-15 01:40:35 +00:00
2011-03-05 02:16:40 +00:00
test ( "focusin bubbles" , function ( ) {
2011-11-14 02:03:36 +00:00
expect ( 2 ) ;
2011-03-31 15:37:48 +00:00
2011-04-12 11:12:58 +00:00
var input = jQuery ( "<input type='text' />" ) . prependTo ( "body" ) ,
2011-03-05 02:16:40 +00:00
order = 0 ;
2011-11-14 02:32:07 +00:00
// focus the element so DOM focus won't fire
input [ 0 ] . focus ( ) ;
2011-03-05 02:16:40 +00:00
jQuery ( "body" ) . bind ( "focusin.focusinBubblesTest" , function ( ) {
2011-11-06 20:27:42 +00:00
equal ( 1 , order ++ , "focusin on the body second" ) ;
2011-03-05 02:16:40 +00:00
} ) ;
input . bind ( "focusin.focusinBubblesTest" , function ( ) {
2011-11-06 20:27:42 +00:00
equal ( 0 , order ++ , "focusin on the element first" ) ;
2011-03-05 02:16:40 +00:00
} ) ;
2011-11-14 02:03:36 +00:00
// Removed since DOM focus is unreliable on test swarm
2011-03-05 02:16:40 +00:00
// DOM focus method
2011-11-14 02:03:36 +00:00
// input[0].focus();
2011-03-31 15:37:48 +00:00
2011-03-31 13:10:30 +00:00
// To make the next focus test work, we need to take focus off the input.
// This will fire another focusin event, so set order to reflect that.
2011-11-14 02:03:36 +00:00
// order = 1;
// jQuery("#text1")[0].focus();
2011-03-31 15:37:48 +00:00
2011-03-05 02:16:40 +00:00
// jQuery trigger, which calls DOM focus
order = 0 ;
input . trigger ( "focus" ) ;
input . remove ( ) ;
jQuery ( "body" ) . unbind ( "focusin.focusinBubblesTest" ) ;
} ) ;
2011-04-08 02:52:15 +00:00
test ( "custom events with colons (#3533, #8272)" , function ( ) {
expect ( 1 ) ;
var tab = jQuery ( "<table><tr><td>trigger</td></tr></table>" ) . appendTo ( "body" ) ;
try {
tab . trigger ( "back:forth" ) ;
ok ( true , "colon events don't throw" ) ;
} catch ( e ) {
ok ( false , "colon events die" ) ;
2012-06-21 19:30:24 +00:00
}
2011-04-08 02:52:15 +00:00
tab . remove ( ) ;
} ) ;
2011-03-31 15:37:48 +00:00
2011-07-29 00:43:23 +00:00
test ( ".on and .off" , function ( ) {
expect ( 9 ) ;
2011-07-29 14:20:09 +00:00
var counter , mixfn ;
2011-07-29 00:43:23 +00:00
2012-07-02 15:30:22 +00:00
var $onandoff = jQuery ( "<div id=\"onandoff\"><p>on<b>and</b>off</p><div>worked<em>or</em>borked?</div></div>" ) . appendTo ( "body" ) ;
2011-07-29 00:43:23 +00:00
// Simple case
jQuery ( "#onandoff" )
. on ( "whip" , function ( ) {
ok ( true , "whipped it good" ) ;
} )
. trigger ( "whip" )
. off ( ) ;
// Direct events only
counter = 0 ;
2011-07-29 14:20:09 +00:00
jQuery ( "#onandoff b" )
2011-07-29 00:43:23 +00:00
. on ( "click" , 5 , function ( e , trig ) {
counter += e . data + ( trig || 9 ) ; // twice, 5+9+5+17=36
} )
. one ( "click" , 7 , function ( e , trig ) {
counter += e . data + ( trig || 11 ) ; // once, 7+11=18
} )
. click ( )
. trigger ( "click" , 17 )
. off ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( counter , 54 , "direct event bindings with data" ) ;
2011-07-29 00:43:23 +00:00
// Delegated events only
counter = 0 ;
jQuery ( "#onandoff" )
. on ( "click" , "em" , 5 , function ( e , trig ) {
counter += e . data + ( trig || 9 ) ; // twice, 5+9+5+17=36
} )
. one ( "click" , "em" , 7 , function ( e , trig ) {
counter += e . data + ( trig || 11 ) ; // once, 7+11=18
} )
. find ( "em" )
. click ( )
. trigger ( "click" , 17 )
. end ( )
. off ( "click" , "em" ) ;
2011-11-06 20:27:42 +00:00
equal ( counter , 54 , "delegated event bindings with data" ) ;
2011-07-29 00:43:23 +00:00
// Mixed event bindings and types
counter = 0 ;
mixfn = function ( e , trig ) {
counter += ( e . data || 0 ) + ( trig || 1 ) ;
} ;
jQuery ( "#onandoff" )
2012-11-24 21:03:57 +00:00
. on ( " click clack cluck " , "em" , 2 , mixfn )
2011-07-29 00:43:23 +00:00
. on ( "cluck" , "b" , 7 , mixfn )
. on ( "cluck" , mixfn )
. trigger ( "what!" )
. each ( function ( ) {
2011-11-06 20:27:42 +00:00
equal ( counter , 0 , "nothing triggered yet" ) ;
2011-07-29 00:43:23 +00:00
} )
. find ( "em" )
. one ( "cluck" , 3 , mixfn )
. trigger ( "cluck" , 8 ) // 3+8 2+8 + 0+8 = 29
. off ( )
. trigger ( "cluck" , 9 ) // 2+9 + 0+9 = 20
. end ( )
. each ( function ( ) {
2011-11-06 20:27:42 +00:00
equal ( counter , 49 , "after triggering em element" ) ;
2011-07-29 00:43:23 +00:00
} )
2011-07-29 14:20:09 +00:00
. off ( "cluck" , function ( ) { } ) // shouldn't remove anything
2011-07-29 00:43:23 +00:00
. trigger ( "cluck" , 2 ) // 0+2 = 2
. each ( function ( ) {
2011-11-06 20:27:42 +00:00
equal ( counter , 51 , "after triggering #onandoff cluck" ) ;
2011-07-29 00:43:23 +00:00
} )
. find ( "b" )
. on ( "click" , 95 , mixfn )
. on ( "clack" , "p" , 97 , mixfn )
. one ( "cluck" , 3 , mixfn )
. trigger ( "quack" , 19 ) // 0
. off ( "click clack cluck" )
. end ( )
. each ( function ( ) {
2011-11-06 20:27:42 +00:00
equal ( counter , 51 , "after triggering b" ) ;
2011-07-29 00:43:23 +00:00
} )
. trigger ( "cluck" , 3 ) // 0+3 = 3
2011-07-29 14:20:09 +00:00
. off ( "clack" , "em" , mixfn )
2011-07-29 00:43:23 +00:00
. find ( "em" )
. trigger ( "clack" ) // 0
. end ( )
. each ( function ( ) {
2011-11-06 20:27:42 +00:00
equal ( counter , 54 , "final triggers" ) ;
2011-07-29 00:43:23 +00:00
} )
. off ( "click cluck" ) ;
// We should have removed all the event handlers ... kinda hacky way to check this
var data = jQuery . data [ jQuery ( "#onandoff" ) [ 0 ] . expando ] || { } ;
2012-07-05 19:52:13 +00:00
equal ( data [ "events" ] , undefined , "no events left" ) ;
2011-09-20 16:44:49 +00:00
2012-07-02 15:30:22 +00:00
$onandoff . remove ( ) ;
2011-07-29 00:43:23 +00:00
} ) ;
2011-11-11 02:53:07 +00:00
test ( "special bind/delegate name mapping" , function ( ) {
2011-12-14 02:40:59 +00:00
expect ( 7 ) ;
2011-11-11 02:53:07 +00:00
2012-07-05 19:52:13 +00:00
jQuery . event . special [ "slap" ] = {
2011-11-11 02:53:07 +00:00
bindType : "click" ,
delegateType : "swing" ,
handle : function ( event ) {
2011-11-14 01:50:36 +00:00
equal ( event . handleObj . origType , "slap" , "slapped your mammy, " + event . type ) ;
2011-11-11 02:53:07 +00:00
}
} ;
var comeback = function ( event ) {
2011-11-14 01:50:36 +00:00
ok ( true , "event " + event . type + " triggered" ) ;
2011-11-11 02:53:07 +00:00
} ;
2012-07-02 15:30:22 +00:00
jQuery ( "<div><button id=\"mammy\">Are We Not Men?</button></div>" )
2011-11-11 02:53:07 +00:00
. on ( "slap" , "button" , jQuery . noop )
. on ( "swing" , "button" , comeback )
. find ( "button" )
. on ( "slap" , jQuery . noop )
. on ( "click" , comeback )
. trigger ( "click" ) // bindType-slap and click
. off ( "slap" )
. trigger ( "click" ) // click
. off ( "click" )
. trigger ( "swing" ) // delegateType-slap and swing
. end ( )
2011-11-14 01:50:36 +00:00
. off ( "slap swing" , "button" )
. find ( "button" ) // everything should be gone
2011-11-11 02:53:07 +00:00
. trigger ( "slap" )
. trigger ( "click" )
. trigger ( "swing" )
2011-11-14 01:50:36 +00:00
. end ( )
. remove ( ) ;
2012-07-05 19:52:13 +00:00
delete jQuery . event . special [ "slap" ] ;
2011-11-14 01:50:36 +00:00
2012-07-05 19:52:13 +00:00
jQuery . event . special [ "gutfeeling" ] = {
2011-11-14 01:50:36 +00:00
bindType : "click" ,
delegateType : "click" ,
handle : function ( event ) {
equal ( event . handleObj . origType , "gutfeeling" , "got a gutfeeling" ) ;
2011-12-14 02:40:59 +00:00
// Need to call the handler since .one() uses it to unbind
return event . handleObj . handler . call ( this , event ) ;
2011-11-14 01:50:36 +00:00
}
} ;
2011-11-15 16:38:55 +00:00
// Ensure a special event isn't removed by its mapped type
2012-10-16 14:17:14 +00:00
jQuery ( "<p>Gut Feeling</p>" )
2011-11-14 01:50:36 +00:00
. on ( "click" , jQuery . noop )
. on ( "gutfeeling" , jQuery . noop )
. off ( "click" )
. trigger ( "gutfeeling" )
. remove ( ) ;
2011-11-15 16:38:55 +00:00
// Ensure special events are removed when only a namespace is provided
2012-10-16 14:17:14 +00:00
jQuery ( "<p>Gut Feeling</p>" )
2011-11-15 16:38:55 +00:00
. on ( "gutfeeling.Devo" , jQuery . noop )
. off ( ".Devo" )
. trigger ( "gutfeeling" )
. remove ( ) ;
2011-12-14 02:40:59 +00:00
// Ensure .one() events are removed after their maiden voyage
2012-10-16 14:17:14 +00:00
jQuery ( "<p>Gut Feeling</p>" )
2011-12-14 02:40:59 +00:00
. one ( "gutfeeling" , jQuery . noop )
2012-01-13 01:30:45 +00:00
. trigger ( "gutfeeling" ) // This one should
2011-12-14 02:40:59 +00:00
. trigger ( "gutfeeling" ) // This one should not
. remove ( ) ;
2012-07-05 19:52:13 +00:00
delete jQuery . event . special [ "gutfeeling" ] ;
2011-11-11 02:53:07 +00:00
} ) ;
2011-11-10 02:29:15 +00:00
test ( ".on and .off, selective mixed removal (#10705)" , function ( ) {
expect ( 7 ) ;
var clockout = 0 ,
2011-11-11 02:53:07 +00:00
timingx = function ( e ) {
2011-11-10 02:29:15 +00:00
ok ( true , "triggered " + e . type ) ;
} ;
2011-11-11 02:53:07 +00:00
2012-01-13 01:30:45 +00:00
jQuery ( "<p>Strange Pursuit</p>" )
2011-11-10 02:29:15 +00:00
. on ( "click" , timingx )
. on ( "click.duty" , timingx )
. on ( "click.now" , timingx )
. on ( "devo" , timingx )
. on ( "future" , timingx )
. trigger ( "click" ) // 3
. trigger ( "devo" ) // 1
. off ( ".duty devo " ) // trailing space
. trigger ( "future" ) // 1
. trigger ( "click" ) // 2
. off ( "future click" )
. trigger ( "click" ) ; // 0
} ) ;
2012-01-13 01:30:45 +00:00
test ( ".on( event-map, null-selector, data ) #11130" , function ( ) {
expect ( 1 ) ;
var $p = jQuery ( "<p>Strange Pursuit</p>" ) ,
data = "bar" ,
map = {
"foo" : function ( event ) {
equal ( event . data , "bar" , "event.data correctly relayed with null selector" ) ;
$p . remove ( ) ;
}
} ;
$p . on ( map , null , data ) . trigger ( "foo" ) ;
} ) ;
2012-01-28 19:58:00 +00:00
test ( "clone() delegated events (#11076)" , function ( ) {
expect ( 3 ) ;
2012-07-05 19:52:13 +00:00
var counter = { "center" : 0 , "fold" : 0 , "centerfold" : 0 } ,
2012-01-28 19:58:00 +00:00
clicked = function ( event ) {
counter [ jQuery ( this ) . text ( ) . replace ( /\s+/ , "" ) ] ++ ;
} ,
2012-03-05 02:11:50 +00:00
table =
2012-01-28 19:58:00 +00:00
jQuery ( "<table><tr><td>center</td><td>fold</td></tr></table>" )
. on ( "click" , "tr" , clicked )
. on ( "click" , "td:first-child" , clicked )
. on ( "click" , "td:last-child" , clicked ) ,
clone = table . clone ( true ) ;
clone . find ( "td" ) . click ( ) ;
2012-07-05 19:52:13 +00:00
equal ( counter [ "center" ] , 1 , "first child" ) ;
equal ( counter [ "fold" ] , 1 , "last child" ) ;
equal ( counter [ "centerfold" ] , 2 , "all children" ) ;
2012-01-28 19:58:00 +00:00
table . remove ( ) ;
clone . remove ( ) ;
} ) ;
2012-11-21 03:31:33 +00:00
test ( "checkbox state (#3827)" , function ( ) {
expect ( 9 ) ;
var markup = jQuery ( "<div><input type=checkbox><div>" ) . appendTo ( "#qunit-fixture" ) ,
cb = markup . find ( "input" ) [ 0 ] ;
jQuery ( cb ) . on ( "click" , function ( ) {
equal ( this . checked , false , "just-clicked checkbox is not checked" ) ;
} ) ;
markup . on ( "click" , function ( ) {
equal ( cb . checked , false , "checkbox is not checked in bubbled event" ) ;
} ) ;
// Native click
cb . checked = true ;
equal ( cb . checked , true , "native - checkbox is initially checked" ) ;
cb . click ( ) ;
equal ( cb . checked , false , "native - checkbox is no longer checked" ) ;
// jQuery click
cb . checked = true ;
equal ( cb . checked , true , "jQuery - checkbox is initially checked" ) ;
jQuery ( cb ) . click ( ) ;
equal ( cb . checked , false , "jQuery - checkbox is no longer checked" ) ;
// Handlers only; checkbox state remains false
jQuery ( cb ) . triggerHandler ( "click" ) ;
} ) ;
2012-11-23 20:03:55 +00:00
test ( "focus-blur order (#12868)" , function ( ) {
expect ( 5 ) ;
var $text = jQuery ( "#text1" ) ,
$radio = jQuery ( "#radio1" ) . focus ( ) ,
order ;
// IE6-10 fire focus/blur events asynchronously; this is the resulting mess.
// IE's browser window must be topmost for this to work properly!!
stop ( ) ;
$radio [ 0 ] . focus ( ) ;
setTimeout ( function ( ) {
$text
. on ( "focus" , function ( ) {
equal ( order ++ , 1 , "text focus" ) ;
} )
. on ( "blur" , function ( ) {
equal ( order ++ , 0 , "text blur" ) ;
} ) ;
$radio
. on ( "focus" , function ( ) {
equal ( order ++ , 1 , "radio focus" ) ;
} )
. on ( "blur" , function ( ) {
equal ( order ++ , 0 , "radio blur" ) ;
} ) ;
// Enabled input getting focus
order = 0 ;
equal ( document . activeElement , $radio [ 0 ] , "radio has focus" ) ;
$text . focus ( ) ;
setTimeout ( function ( ) {
equal ( document . activeElement , $text [ 0 ] , "text has focus" ) ;
// Run handlers without native method on an input
order = 1 ;
$radio . triggerHandler ( "focus" ) ;
start ( ) ;
} , 50 ) ;
} , 50 ) ;
} ) ;
2012-11-24 20:25:54 +00:00
test ( "hover event no longer special since 1.9" , function ( ) {
expect ( 1 ) ;
jQuery ( "<div>craft</div>" )
. on ( "hover" , function ( e ) {
equal ( e . type , "hover" , "I am hovering!" ) ;
} )
. trigger ( "hover" )
. off ( "hover" ) ;
} ) ;
2011-10-06 01:41:32 +00:00
test ( "fixHooks extensions" , function ( ) {
2011-09-26 17:00:45 +00:00
expect ( 2 ) ;
2011-09-25 23:56:34 +00:00
2011-09-26 02:04:52 +00:00
// IE requires focusable elements to be visible, so append to body
2011-09-26 17:00:45 +00:00
var $fixture = jQuery ( "<input type='text' id='hook-fixture' />" ) . appendTo ( "body" ) ,
2011-10-28 18:17:14 +00:00
saved = jQuery . event . fixHooks . click ;
2011-09-25 23:56:34 +00:00
// Ensure the property doesn't exist
2011-09-26 17:00:45 +00:00
$fixture . bind ( "click" , function ( event ) {
2011-09-25 23:56:34 +00:00
ok ( ! ( "blurrinessLevel" in event ) , "event.blurrinessLevel does not exist" ) ;
2011-10-28 18:17:14 +00:00
} ) ;
2012-10-16 14:17:14 +00:00
fireNative ( $fixture [ 0 ] , "click" ) ;
2011-10-06 01:41:32 +00:00
$fixture . unbind ( "click" ) ;
2011-09-26 17:00:45 +00:00
2011-10-06 01:41:32 +00:00
jQuery . event . fixHooks . click = {
2011-09-25 23:56:34 +00:00
filter : function ( event , originalEvent ) {
event . blurrinessLevel = 42 ;
return event ;
}
} ;
2011-09-26 17:00:45 +00:00
// Trigger a native click and ensure the property is set
$fixture . bind ( "click" , function ( event ) {
2011-11-06 20:27:42 +00:00
equal ( event . blurrinessLevel , 42 , "event.blurrinessLevel was set" ) ;
2011-10-28 18:17:14 +00:00
} ) ;
2012-10-16 14:17:14 +00:00
fireNative ( $fixture [ 0 ] , "click" ) ;
2011-09-25 23:56:34 +00:00
2011-10-06 01:41:32 +00:00
delete jQuery . event . fixHooks . click ;
2011-09-26 17:00:45 +00:00
$fixture . unbind ( "click" ) . remove ( ) ;
2011-10-06 01:41:32 +00:00
jQuery . event . fixHooks . click = saved ;
2011-09-25 23:56:34 +00:00
} ) ;
2011-07-29 00:43:23 +00:00
2012-08-23 21:46:12 +00:00
testIframeWithCallback ( "jQuery.ready promise" , "event/promiseReady.html" , function ( isOk ) {
expect ( 1 ) ;
ok ( isOk , "$.when( $.ready ) works" ) ;
} ) ;
2012-04-23 19:43:26 +00:00
2012-08-23 21:46:12 +00:00
// need PHP here to make the incepted IFRAME hang
2012-04-23 19:43:26 +00:00
if ( hasPHP ) {
2012-08-19 21:41:43 +00:00
testIframeWithCallback ( "jQuery.ready synchronous load with long loading subresources" , "event/syncReady.html" , function ( isOk ) {
expect ( 1 ) ;
ok ( isOk , "jQuery loaded synchronously fires ready when the DOM can truly be interacted with" ) ;
} ) ;
2012-04-23 19:43:26 +00:00
}
2011-03-31 15:37:48 +00:00
( function ( ) {
// This code must be run before DOM ready!
var notYetReady , noEarlyExecution ,
order = [ ] ,
args = { } ;
notYetReady = ! jQuery . isReady ;
test ( "jQuery.isReady" , function ( ) {
expect ( 2 ) ;
2011-11-06 20:27:42 +00:00
equal ( notYetReady , true , "jQuery.isReady should not be true before DOM ready" ) ;
equal ( jQuery . isReady , true , "jQuery.isReady should be true once DOM is ready" ) ;
2007-12-17 17:39:50 +00:00
} ) ;
2008-04-22 21:59:40 +00:00
2011-03-31 15:37:48 +00:00
// Create an event handler.
function makeHandler ( testId ) {
// When returned function is executed, push testId onto `order` array
// to ensure execution order. Also, store event handler arg to ensure
// the correct arg is being passed into the event handler.
return function ( arg ) {
order . push ( testId ) ;
args [ testId ] = arg ;
} ;
}
// Bind to the ready event in every possible way.
jQuery ( makeHandler ( "a" ) ) ;
jQuery ( document ) . ready ( makeHandler ( "b" ) ) ;
jQuery ( document ) . bind ( "ready.readytest" , makeHandler ( "c" ) ) ;
// Do it twice, just to be sure.
jQuery ( makeHandler ( "d" ) ) ;
jQuery ( document ) . ready ( makeHandler ( "e" ) ) ;
jQuery ( document ) . bind ( "ready.readytest" , makeHandler ( "f" ) ) ;
2012-06-21 19:30:24 +00:00
noEarlyExecution = order . length === 0 ;
2011-03-31 15:37:48 +00:00
// This assumes that QUnit tests are run on DOM ready!
test ( "jQuery ready" , function ( ) {
2011-03-31 17:36:16 +00:00
expect ( 10 ) ;
2011-03-31 15:37:48 +00:00
ok ( noEarlyExecution , "Handlers bound to DOM ready should not execute before DOM ready" ) ;
// Ensure execution order.
2011-11-06 20:27:42 +00:00
deepEqual ( order , [ "a" , "b" , "d" , "e" , "c" , "f" ] , "Bound DOM ready handlers should execute in bind-order, but those bound with jQuery(document).bind( 'ready', fn ) will always execute last" ) ;
2011-03-31 15:37:48 +00:00
// Ensure handler argument is correct.
2012-07-05 19:52:13 +00:00
equal ( args [ "a" ] , jQuery , "Argument passed to fn in jQuery( fn ) should be jQuery" ) ;
equal ( args [ "b" ] , jQuery , "Argument passed to fn in jQuery(document).ready( fn ) should be jQuery" ) ;
ok ( args [ "c" ] instanceof jQuery . Event , "Argument passed to fn in jQuery(document).bind( 'ready', fn ) should be an event object" ) ;
2011-03-31 15:37:48 +00:00
order = [ ] ;
// Now that the ready event has fired, again bind to the ready event
// in every possible way. These event handlers should execute immediately.
jQuery ( makeHandler ( "g" ) ) ;
2011-11-06 20:27:42 +00:00
equal ( order . pop ( ) , "g" , "Event handler should execute immediately" ) ;
2012-07-05 19:52:13 +00:00
equal ( args [ "g" ] , jQuery , "Argument passed to fn in jQuery( fn ) should be jQuery" ) ;
2011-03-31 15:37:48 +00:00
jQuery ( document ) . ready ( makeHandler ( "h" ) ) ;
2011-11-06 20:27:42 +00:00
equal ( order . pop ( ) , "h" , "Event handler should execute immediately" ) ;
2012-07-05 19:52:13 +00:00
equal ( args [ "h" ] , jQuery , "Argument passed to fn in jQuery(document).ready( fn ) should be jQuery" ) ;
2011-03-31 15:37:48 +00:00
2011-03-31 17:36:16 +00:00
jQuery ( document ) . bind ( "ready.readytest" , makeHandler ( "never" ) ) ;
2011-11-06 20:27:42 +00:00
equal ( order . length , 0 , "Event handler should never execute since DOM ready has already passed" ) ;
2011-03-31 15:37:48 +00:00
// Cleanup.
jQuery ( document ) . unbind ( "ready.readytest" ) ;
} ) ;
} ) ( ) ;
2012-08-20 00:19:44 +00:00
test ( "change handler should be detached from element" , function ( ) {
expect ( 2 ) ;
var $fixture = jQuery ( "<input type='text' id='change-ie-leak' />" ) . appendTo ( "body" ) ;
2012-08-23 21:46:12 +00:00
2012-08-20 00:19:44 +00:00
var originRemoveEvent = jQuery . removeEvent ;
2012-08-23 21:46:12 +00:00
2012-08-20 00:19:44 +00:00
var wrapperRemoveEvent = function ( elem , type , handle ) {
equal ( "change" , type , "Event handler for 'change' event should be removed" ) ;
equal ( "change-ie-leak" , jQuery ( elem ) . attr ( "id" ) , "Event handler for 'change' event should be removed from appropriate element" ) ;
originRemoveEvent ( elem , type , handle ) ;
} ;
2012-08-23 21:46:12 +00:00
2012-08-20 00:19:44 +00:00
jQuery . removeEvent = wrapperRemoveEvent ;
2012-08-23 21:46:12 +00:00
2012-08-20 00:19:44 +00:00
$fixture . bind ( "change" , function ( event ) { } ) ;
$fixture . unbind ( "change" ) ;
2012-08-23 21:46:12 +00:00
2012-08-20 00:19:44 +00:00
$fixture . remove ( ) ;
2012-08-23 21:46:12 +00:00
2012-08-20 00:19:44 +00:00
jQuery . removeEvent = originRemoveEvent ;
} ) ;
2012-04-16 13:43:59 +00:00
asyncTest ( "trigger click on checkbox, fires change event" , function ( ) {
2012-04-11 01:54:07 +00:00
expect ( 1 ) ;
var check = jQuery ( "#check2" ) ;
2011-04-05 19:55:40 +00:00
2012-04-11 01:54:07 +00:00
check . on ( "change" , function ( ) {
// get it?
check . off ( "change" ) ;
ok ( true , "Change event fired as a result of triggered click" ) ;
2012-04-16 13:43:59 +00:00
start ( ) ;
2012-04-11 01:54:07 +00:00
} ) . trigger ( "click" ) ;
} ) ;
2012-10-16 00:22:19 +00:00
test ( "Namespace preserved when passed an Event (#12739)" , function ( ) {
expect ( 4 ) ;
var markup = jQuery (
"<div id='parent'><div id='child'></div></div>"
) ,
triggered = 0 ,
fooEvent ;
markup . find ( "div" )
. addBack ( )
. on ( "foo.bar" , function ( e ) {
if ( ! e . handled ) {
triggered ++ ;
e . handled = true ;
equal ( e . namespace , "bar" , "namespace is bar" ) ;
jQuery ( e . target ) . find ( "div" ) . each ( function ( ) {
jQuery ( this ) . triggerHandler ( e ) ;
} ) ;
}
} )
. on ( "foo.bar2" , function ( e ) {
ok ( false , "foo.bar2 called on trigger " + triggered + " id " + this . id ) ;
} ) ;
markup . trigger ( "foo.bar" ) ;
markup . trigger ( jQuery . Event ( "foo.bar" ) ) ;
fooEvent = jQuery . Event ( "foo" ) ;
fooEvent . namespace = "bar" ;
markup . trigger ( fooEvent ) ;
markup . remove ( ) ;
equal ( triggered , 3 , "foo.bar triggered" ) ;
} ) ;
2012-11-18 18:53:24 +00:00
test ( "make sure events cloned correctly" , 18 , function ( ) {
var clone ,
fixture = jQuery ( "#qunit-fixture" ) ,
checkbox = jQuery ( "#check1" ) ,
p = jQuery ( "#firstp" ) ;
2012-10-16 00:22:19 +00:00
2012-11-18 18:53:24 +00:00
fixture . on ( "click change" , function ( event , result ) {
ok ( result , event . type + " on original element is fired" ) ;
} ) . on ( "click" , "#firstp" , function ( event , result ) {
ok ( result , "Click on original child element though delegation is fired" ) ;
} ) . on ( "change" , "#check1" , function ( event , result ) {
ok ( result , "Change on original child element though delegation is fired" ) ;
} ) ;
p . on ( "click" , function ( event , result ) {
ok ( true , "Click on original child element is fired" ) ;
} ) ;
checkbox . on ( "change" , function ( event , result ) {
ok ( true , "Change on original child element is fired" ) ;
} ) ;
fixture . clone ( ) . click ( ) . change ( ) ; // 0 events should be fired
clone = fixture . clone ( true ) ;
clone . find ( "p:first" ) . trigger ( "click" , true ) ; // 3 events should fire
clone . find ( "#check1" ) . trigger ( "change" , true ) ; // 3 events should fire
clone . remove ( ) ;
clone = fixture . clone ( true , true ) ;
clone . find ( "p:first" ) . trigger ( "click" , true ) ; // 3 events should fire
clone . find ( "#check1" ) . trigger ( "change" , true ) ; // 3 events should fire
fixture . off ( ) ;
p . off ( ) ;
checkbox . off ( ) ;
p . click ( ) ; // 0 should be fired
checkbox . change ( ) ; // 0 should be fired
clone . find ( "p:first" ) . trigger ( "click" , true ) ; // 3 events should fire
clone . find ( "#check1" ) . trigger ( "change" , true ) ; // 3 events should fire
clone . remove ( ) ;
clone . find ( "p:first" ) . click ( ) ; // 0 should be fired
clone . find ( "#check1" ) . change ( ) ; // 0 events should fire
} ) ;