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 {
2013-01-27 04:48:59 +00:00
jQuery ( "#firstp" ) . on ( "click" , null ) ;
2011-02-10 02:15:32 +00:00
ok ( true , "Passing a null handler will not throw an exception" ) ;
} catch ( e ) { }
try {
2013-01-27 04:48:59 +00:00
jQuery ( "#firstp" ) . on ( "click" , undefined ) ;
2011-02-10 02:15:32 +00:00
ok ( true , "Passing an undefined handler will not throw an exception" ) ;
} catch ( e ) { }
2010-10-24 16:18:33 +00:00
} ) ;
2013-03-04 02:22:11 +00:00
test ( "on() with non-null,defined data" , function ( ) {
2011-05-16 14:38:36 +00:00
2013-03-04 02:22:11 +00:00
expect ( 2 ) ;
2011-05-16 14:38:36 +00:00
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
2012-12-18 19:42:37 +00:00
jQuery ( "#foo" ) . on ( "foo.on" , handler ) ;
2013-03-04 03:00:57 +00:00
jQuery ( "div" ) . on ( "foo.delegate" , "#foo" , handler ) ;
2011-05-16 14:38:36 +00:00
jQuery ( "#foo" ) . trigger ( "foo" , 0 ) ;
2012-12-18 19:42:37 +00:00
jQuery ( "#foo" ) . off ( "foo.on" , handler ) ;
2013-03-04 03:00:57 +00:00
jQuery ( "div" ) . off ( "foo.delegate" , "#foo" ) ;
2011-05-16 14:38:36 +00:00
} ) ;
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
2013-02-27 03:55:36 +00:00
. find ( "*" ) . addBack ( ) . on ( "click" , function ( e ) {
2011-10-13 20:30:40 +00:00
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
} ) ;
2013-03-04 02:22:11 +00:00
test ( "on(), 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 ) {
2013-03-04 02:22:11 +00:00
ok ( event . data , "on() with data, check passed data exists" ) ;
equal ( event . data [ "foo" ] , "bar" , "on() with data, Check value of passed data" ) ;
2007-04-24 21:48:52 +00:00
} ;
2013-03-04 02:22:11 +00:00
jQuery ( "#firstp" ) . on ( "click" , { "foo" : "bar" } , handler ) . trigger ( "click" ) . off ( "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 ) {
2013-03-04 02:22:11 +00:00
equal ( event . data , test , "on() with function data, Check value of passed data" ) ;
2011-04-17 00:39:30 +00:00
} ;
2013-03-04 02:22:11 +00:00
jQuery ( "#firstp" ) . on ( "click" , test , handler2 ) . trigger ( "click" ) . off ( "click" , handler2 ) ;
2010-02-27 01:01:19 +00:00
} ) ;
test ( "click(), with data" , function ( ) {
expect ( 3 ) ;
var handler = function ( event ) {
2013-03-04 02:22:11 +00:00
ok ( event . data , "on() with data, check passed data exists" ) ;
equal ( event . data [ "foo" ] , "bar" , "on() with data, Check value of passed data" ) ;
2010-02-27 01:01:19 +00:00
} ;
2013-03-04 02:22:11 +00:00
jQuery ( "#firstp" ) . on ( "click" , { "foo" : "bar" } , handler ) . trigger ( "click" ) . off ( "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
} ) ;
2013-03-04 02:22:11 +00:00
test ( "on(), with data, trigger with data" , function ( ) {
2008-01-14 09:33:08 +00:00
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
} ;
2013-03-04 02:22:11 +00:00
jQuery ( "#firstp" ) . on ( "click" , { foo : "bar" } , handler ) . trigger ( "click" , [ { bar : "foo" } ] ) . off ( "click" , handler ) ;
2008-01-14 09:33:08 +00:00
} ) ;
2013-03-04 02:22:11 +00:00
test ( "on(), multiple events at once" , function ( ) {
2008-01-14 09:33:08 +00:00
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
} ;
2013-03-04 02:22:11 +00:00
jQuery ( "#firstp" ) . on ( "click mouseover" , handler ) . trigger ( "click" ) . trigger ( "mouseover" ) ;
equal ( clickCounter , 1 , "on() with multiple events at once" ) ;
equal ( mouseoverCounter , 1 , "on() with multiple events at once" ) ;
2008-01-14 09:33:08 +00:00
} ) ;
2013-03-04 02:22:11 +00:00
test ( "on(), five events at once" , function ( ) {
2011-04-29 01:14:12 +00:00
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
2013-03-04 02:22:11 +00:00
jQuery ( "#firstp" ) . on ( "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
2013-03-04 02:22:11 +00:00
equal ( count , 5 , "on() five events at once" ) ;
2011-04-29 01:14:12 +00:00
} ) ;
2013-03-04 02:22:11 +00:00
test ( "on(), multiple events at once and namespaces" , function ( ) {
2010-01-25 21:45:39 +00:00
expect ( 7 ) ;
var cur , obj = { } ;
2013-03-04 02:22:11 +00:00
var div = jQuery ( "<div/>" ) . on ( "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 ( ) ;
2013-03-04 02:22:11 +00:00
div = jQuery ( "<div/>" ) . on ( "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 ( ) ;
2013-03-04 02:22:11 +00:00
div = jQuery ( "<div/>" ) . on ( "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
} ) ;
2013-03-04 02:22:11 +00:00
test ( "on(), namespace with special add" , function ( ) {
2011-08-03 02:50:58 +00:00
expect ( 27 ) ;
2010-01-25 22:01:07 +00:00
2013-03-04 02:22:11 +00:00
var div = jQuery ( "<div/>" ) . on ( "test" , function ( e ) {
2010-01-25 22:01:07 +00:00
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
} ;
2013-03-04 02:22:11 +00:00
div . on ( "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
} ) ;
2013-03-04 02:22:11 +00:00
div . on ( "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
2013-03-04 02:22:11 +00:00
div . off ( "test" ) ;
2010-02-13 11:10:43 +00:00
2013-03-04 02:22:11 +00:00
div = jQuery ( "<div/>" ) . on ( "test" , function ( e ) {
2010-02-13 11:10:43 +00:00
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
} ) ;
2013-03-04 02:22:11 +00:00
test ( "on(), no data" , function ( ) {
2008-01-14 09:33:08 +00:00
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" ) ;
} ;
2013-03-04 02:22:11 +00:00
jQuery ( "#firstp" ) . on ( "click" , handler ) . trigger ( "click" ) ;
2008-01-14 09:33:08 +00:00
} ) ;
2013-03-04 02:22:11 +00:00
test ( "on/one/off(Object)" , function ( ) {
2009-09-16 02:19:18 +00:00
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
2013-03-04 02:22:11 +00:00
. on ( {
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
2013-03-04 02:22:11 +00:00
equal ( clickCounter , 3 , "on(Object)" ) ;
equal ( mouseoverCounter , 3 , "on(Object)" ) ;
2010-12-30 06:34:48 +00:00
2009-09-16 02:19:18 +00:00
trigger ( ) ;
2013-03-04 02:22:11 +00:00
equal ( clickCounter , 4 , "on(Object)" ) ;
equal ( mouseoverCounter , 4 , "on(Object)" ) ;
2010-12-30 06:34:48 +00:00
2013-03-04 02:22:11 +00:00
jQuery ( "#firstp" ) . off ( {
2012-07-05 19:52:13 +00:00
"click" : handler ,
"mouseover" : handler
2009-09-16 02:19:18 +00:00
} ) ;
trigger ( ) ;
2013-03-04 02:22:11 +00:00
equal ( clickCounter , 4 , "on(Object)" ) ;
equal ( mouseoverCounter , 4 , "on(Object)" ) ;
2009-09-16 02:19:18 +00:00
} ) ;
2013-03-04 03:00:57 +00:00
test ( "on/off(Object), on/off(Object, String)" , function ( ) {
2010-03-14 11:34:32 +00:00
expect ( 6 ) ;
2010-12-30 06:34:48 +00:00
2013-02-19 04:52:29 +00:00
var clickCounter = 0 ,
mouseoverCounter = 0 ,
$p = jQuery ( "#firstp" ) ,
$a = $p . find ( "a" ) . eq ( 0 ) ;
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
2013-02-19 04:52:29 +00:00
jQuery ( document ) . on ( events , "#firstp a" ) ;
2013-03-04 03:00:57 +00:00
$p . on ( events , "a" , 2 ) ;
2010-12-30 06:34:48 +00:00
2010-03-14 11:34:32 +00:00
trigger ( ) ;
2013-03-04 03:00:57 +00:00
equal ( clickCounter , 3 , "on" ) ;
equal ( mouseoverCounter , 3 , "on" ) ;
2010-12-30 06:34:48 +00:00
2013-03-04 03:00:57 +00:00
$p . off ( events , "a" ) ;
2010-12-30 06:34:48 +00:00
2010-03-14 11:34:32 +00:00
trigger ( ) ;
2013-03-04 03:00:57 +00:00
equal ( clickCounter , 4 , "off" ) ;
equal ( mouseoverCounter , 4 , "off" ) ;
2010-12-30 06:34:48 +00:00
2013-02-19 04:52:29 +00:00
jQuery ( document ) . off ( events , "#firstp a" ) ;
2010-12-30 06:34:48 +00:00
2010-03-14 11:34:32 +00:00
trigger ( ) ;
2012-12-18 19:42:37 +00:00
equal ( clickCounter , 4 , "off" ) ;
equal ( mouseoverCounter , 4 , "off" ) ;
2010-03-14 11:34:32 +00:00
} ) ;
2013-03-04 03:00:57 +00:00
test ( "on immediate propagation" , function ( ) {
2010-10-23 18:36:24 +00:00
expect ( 2 ) ;
2010-12-30 06:34:48 +00:00
2013-02-19 04:52:29 +00:00
var lastClick ,
$p = jQuery ( "#firstp" ) ,
$a = $p . find ( "a" ) . eq ( 0 ) ;
2010-12-30 06:34:48 +00:00
2010-10-23 18:36:24 +00:00
lastClick = "" ;
2013-02-19 04:52:29 +00:00
jQuery ( document ) . on ( "click" , "#firstp a" , function ( e ) {
2010-12-30 06:34:48 +00:00
lastClick = "click1" ;
2010-10-23 18:36:24 +00:00
e . stopImmediatePropagation ( ) ;
} ) ;
2013-02-19 04:52:29 +00:00
jQuery ( document ) . on ( "click" , "#firstp a" , function ( e ) {
2010-10-23 18:36:24 +00:00
lastClick = "click2" ;
} ) ;
$a . trigger ( "click" ) ;
2012-12-18 19:42:37 +00:00
equal ( lastClick , "click1" , "on stopImmediatePropagation" ) ;
2013-02-19 04:52:29 +00:00
jQuery ( document ) . off ( "click" , "#firstp a" ) ;
2010-12-30 06:34:48 +00:00
2010-10-23 18:36:24 +00:00
lastClick = "" ;
2013-03-04 03:00:57 +00:00
$p . on ( "click" , "a" , function ( e ) {
2010-12-30 06:34:48 +00:00
lastClick = "click1" ;
2010-10-23 18:36:24 +00:00
e . stopImmediatePropagation ( ) ;
} ) ;
2013-03-04 03:00:57 +00:00
$p . on ( "click" , "a" , function ( e ) {
2010-10-23 18:36:24 +00:00
lastClick = "click2" ;
} ) ;
$a . trigger ( "click" ) ;
2013-03-04 03:00:57 +00:00
equal ( lastClick , "click1" , "on stopImmediatePropagation" ) ;
$p . off ( "click" , "**" ) ;
2010-10-23 18:36:24 +00:00
} ) ;
2013-03-04 03:00:57 +00:00
test ( "on 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
}
} ;
2013-01-27 04:48:59 +00:00
$anchor2 . on ( "click" , function ( e ) {
2010-12-27 19:30:05 +00:00
e . preventDefault ( ) ;
} ) ;
2013-03-04 03:00:57 +00:00
$main . on ( "click" , "#foo" , 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 ) ;
2013-03-04 02:22:11 +00:00
$anchor2 . off ( "click" ) ;
2013-03-04 03:00:57 +00:00
$main . off ( "click" , "**" ) ;
2013-01-27 04:48:59 +00:00
$anchor2 . on ( "click" , function ( e ) {
2010-12-27 19:30:05 +00:00
// Let the default action occur
} ) ;
2013-03-04 03:00:57 +00:00
$main . on ( "click" , "#foo" , 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 ) ;
2013-03-04 02:22:11 +00:00
$anchor2 . off ( "click" ) ;
2013-03-04 03:00:57 +00:00
$main . off ( "click" , "**" ) ;
2010-12-27 19:30:05 +00:00
} ) ;
2013-03-04 02:22:11 +00:00
test ( "on(), 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
2013-03-04 02:22:11 +00:00
jQuery ( "div" , doc ) . on ( "click" , function ( ) {
2009-04-29 22:04:41 +00:00
ok ( true , "Binding to element inside iframe" ) ;
2013-03-04 02:22:11 +00:00
} ) . trigger ( "click" ) . off ( "click" ) ;
2008-01-14 09:33:08 +00:00
} ) ;
2013-03-04 02:22:11 +00:00
test ( "on(), 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 ) {
2013-03-04 02:22:11 +00:00
jQuery ( this ) . on ( "change" , i , selectOnChange ) ;
2011-04-12 08:47:46 +00:00
} ) . trigger ( "change" ) ;
2008-01-14 09:33:08 +00:00
} ) ;
2007-09-03 14:53:09 +00:00
2013-03-04 02:22:11 +00:00
test ( "on(), namespaced events, cloned events" , 18 , function ( ) {
2011-02-10 02:15:32 +00:00
var firstp = jQuery ( "#firstp" ) ;
2013-03-04 02:22:11 +00:00
firstp . on ( "custom.test" , function ( e ) {
2011-02-10 02:15:32 +00:00
ok ( false , "Custom event triggered" ) ;
2008-02-03 04:33:11 +00:00
} ) ;
2013-03-04 02:22:11 +00:00
firstp . on ( "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
2013-03-04 02:22:11 +00:00
firstp . on ( "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
2013-03-04 02:22:11 +00:00
firstp . off ( "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
2013-03-04 02:22:11 +00:00
firstp . off ( ".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
2013-03-04 02:22:11 +00:00
jQuery ( "#nonnodes" ) . contents ( ) . on ( "tester" , function ( ) {
equal ( this . nodeType , 1 , "Check node,textnode,comment on 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
2013-01-27 04:48:59 +00:00
jQuery ( "<a href='#fail' class='test'>test</a>" ) . on ( "click" , function ( ) { return false ; } ) . appendTo ( "#qunit-fixture" ) ;
2013-02-19 04:52:29 +00:00
ok ( jQuery ( "a.test" ) . eq ( 0 ) . triggerHandler ( "click" ) === false , "Handler is bound to appendTo'd elements" ) ;
2007-03-25 18:06:18 +00:00
} ) ;
2013-03-04 02:22:11 +00:00
test ( "on(), multi-namespaced events" , function ( ) {
2008-12-19 04:34:12 +00:00
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
}
2013-03-04 02:22:11 +00:00
jQuery ( "#firstp" ) . on ( "custom.test" , function ( e ) {
2008-12-19 04:34:12 +00:00
check ( "custom.test" , "Custom event triggered" ) ;
} ) ;
2013-03-04 02:22:11 +00:00
jQuery ( "#firstp" ) . on ( "custom.test2" , function ( e ) {
2008-12-19 04:34:12 +00:00
check ( "custom.test2" , "Custom event triggered" ) ;
} ) ;
2013-03-04 02:22:11 +00:00
jQuery ( "#firstp" ) . on ( "click.test" , function ( e ) {
2008-12-19 04:34:12 +00:00
check ( "click.test" , "Normal click triggered" ) ;
} ) ;
2013-03-04 02:22:11 +00:00
jQuery ( "#firstp" ) . on ( "click.test.abc" , function ( e ) {
2008-12-19 04:34:12 +00:00
check ( "click.test.abc" , "Namespaced click triggered" ) ;
} ) ;
2010-12-30 06:34:48 +00:00
2013-03-04 02:22:11 +00:00
// Those would not trigger/off (#5303)
2009-11-19 05:11:29 +00:00
jQuery ( "#firstp" ) . trigger ( "click.a.test" ) ;
2013-03-04 02:22:11 +00:00
jQuery ( "#firstp" ) . off ( "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
2013-03-04 02:22:11 +00:00
jQuery ( "#firstp" ) . off ( "click.abc" ) ;
2008-12-19 04:34:12 +00:00
// Trigger the remaining fn (1)
jQuery ( "#firstp" ) . trigger ( "click" ) ;
// Remove the remaining fn
2013-03-04 02:22:11 +00:00
jQuery ( "#firstp" ) . off ( ".test" ) ;
2008-12-19 04:34:12 +00:00
// Trigger the remaining fn (1)
jQuery ( "#firstp" ) . trigger ( "custom" ) ;
} ) ;
2013-02-26 17:19:04 +00:00
test ( "namespace-only event binding is a no-op" , function ( ) {
expect ( 2 ) ;
jQuery ( "#firstp" )
. on ( ".whoops" , function ( ) {
ok ( false , "called a namespace-only event" ) ;
} )
. on ( "whoops" , function ( ) {
ok ( true , "called whoops" ) ;
} )
. trigger ( "whoops" ) // 1
. off ( ".whoops" )
. trigger ( "whoops" ) // 2
. off ( "whoops" ) ;
} ) ;
2013-03-04 02:22:11 +00:00
test ( "on(), 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 ++ ;
} ;
2013-03-04 02:22:11 +00:00
jQuery ( "#liveHandlerOrder" ) . on ( "foo.bar" , func ) . on ( "foo.zar" , func ) ;
2010-02-04 05:49:46 +00:00
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
2013-03-04 02:22:11 +00:00
jQuery ( "#liveHandlerOrder" ) . off ( "foo.bar" , func ) . off ( "foo.zar" , func ) ;
2010-02-04 05:49:46 +00:00
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
2013-03-04 02:22:11 +00:00
test ( "on(), make sure order is maintained" , function ( ) {
2010-02-04 14:23:50 +00:00
expect ( 1 ) ;
var elem = jQuery ( "#firstp" ) , log = [ ] , check = [ ] ;
2012-06-21 19:30:24 +00:00
jQuery . each ( new Array ( 100 ) , function ( i ) {
2013-03-04 02:22:11 +00:00
elem . on ( "click" , function ( ) {
2010-02-04 14:23:50 +00:00
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
2013-03-04 02:22:11 +00:00
elem . off ( "click" ) ;
2010-02-04 14:23:50 +00:00
} ) ;
2010-12-30 06:34:48 +00:00
2013-03-04 02:22:11 +00:00
test ( "on(), with different this object" , function ( ) {
2009-05-07 00:50:28 +00:00
expect ( 4 ) ;
var thisObject = { myThis : true } ,
data = { myData : true } ,
handler1 = function ( event ) {
2013-03-04 02:22:11 +00:00
equal ( this , thisObject , "on() with different this object" ) ;
2009-05-07 00:50:28 +00:00
} ,
handler2 = function ( event ) {
2013-03-04 02:22:11 +00:00
equal ( this , thisObject , "on() with different this object and data" ) ;
equal ( event . data , data , "on() 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" )
2013-03-04 02:22:11 +00:00
. on ( "click" , jQuery . proxy ( handler1 , thisObject ) ) . trigger ( "click" ) . off ( "click" , handler1 )
. on ( "click" , data , jQuery . proxy ( handler2 , thisObject ) ) . trigger ( "click" ) . off ( "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
} ) ;
2013-03-04 02:22:11 +00:00
test ( "on(name, false), off(name, false)" , function ( ) {
2010-02-27 14:02:13 +00:00
expect ( 3 ) ;
var main = 0 ;
2013-03-04 02:22:11 +00:00
jQuery ( "#qunit-fixture" ) . on ( "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 ;
2013-03-04 02:22:11 +00:00
jQuery ( "#ap" ) . on ( "click" , false ) ;
2010-02-27 14:02:13 +00:00
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 ;
2013-03-04 02:22:11 +00:00
jQuery ( "#ap" ) . off ( "click" , false ) ;
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." ) ;
2011-01-09 21:58:47 +00:00
// manually clean up events from elements outside the fixture
2013-03-04 02:22:11 +00:00
jQuery ( "#qunit-fixture" ) . off ( "click" ) ;
2010-02-27 14:02:13 +00:00
} ) ;
2013-03-04 03:00:57 +00:00
test ( "on(name, selector, false), off(name, selector, false)" , function ( ) {
2011-01-01 18:49:59 +00:00
expect ( 3 ) ;
var main = 0 ;
2013-03-04 03:00:57 +00:00
jQuery ( "#qunit-fixture" ) . on ( "click" , "#ap" , 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 ;
2013-03-04 03:00:57 +00:00
jQuery ( "#ap" ) . on ( "click" , "#groups" , false ) ;
2011-01-01 18:49:59 +00:00
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 ;
2013-03-04 03:00:57 +00:00
jQuery ( "#ap" ) . off ( "click" , "#groups" , false ) ;
2011-01-01 18:49:59 +00:00
jQuery ( "#groups" ) . trigger ( "click" ) ;
2011-11-06 20:27:42 +00:00
equal ( main , 1 , "Verify that the trigger happened correctly." ) ;
2013-03-04 03:00:57 +00:00
jQuery ( "#qunit-fixture" ) . off ( "click" , "#ap" ) ;
2011-01-01 18:49:59 +00:00
} ) ;
2013-03-04 02:22:11 +00:00
test ( "on()/trigger()/off() 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
2013-03-04 02:22:11 +00:00
jQuery ( obj ) . off ( "test" ) ;
2010-02-26 16:32:12 +00:00
2013-03-04 02:22:11 +00:00
jQuery ( obj ) . on ( {
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
2013-03-04 02:22:11 +00:00
jQuery ( obj ) . off ( "test" ) ;
jQuery ( obj ) . off ( "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
2013-03-04 02:22:11 +00:00
jQuery ( obj ) . off ( "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
} ) ;
2013-03-04 02:22:11 +00:00
test ( "off(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" ;
2013-03-04 02:22:11 +00:00
$elem . on ( "error1" , error ) . off ( "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" ;
2013-03-04 02:22:11 +00:00
$elem . on ( "error1" , error ) . off ( "error1" ) . triggerHandler ( "error1" ) ;
2010-12-30 06:34:48 +00:00
2009-01-08 22:22:33 +00:00
message = "unbind all" ;
2013-03-04 02:22:11 +00:00
$elem . on ( "error1" , error ) . off ( ) . triggerHandler ( "error1" ) ;
2010-12-30 06:34:48 +00:00
2009-01-08 22:22:33 +00:00
message = "unbind many with function" ;
2013-03-04 02:22:11 +00:00
$elem . on ( "error1 error2" , error )
. off ( "error1 error2" , error )
2011-04-12 08:47:46 +00:00
. trigger ( "error1" ) . triggerHandler ( "error2" ) ;
2009-01-08 22:22:33 +00:00
message = "unbind many" ; // #3538
2013-03-04 02:22:11 +00:00
$elem . on ( "error1 error2" , error )
. off ( "error1 error2" )
2011-04-12 08:47:46 +00:00
. 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" ;
2013-03-04 02:22:11 +00:00
$elem . on ( "error1 error2.test" , error )
. off ( )
2011-02-15 21:03:23 +00:00
. trigger ( "error1" ) . triggerHandler ( "error2" ) ;
2011-10-12 00:30:07 +00:00
// Should only unbind the specified function
2013-03-04 02:22:11 +00:00
jQuery ( document ) . on ( "click" , function ( ) {
2011-10-12 00:30:07 +00:00
ok ( true , "called handler after selective removal" ) ;
} ) ;
var func = function ( ) { } ;
jQuery ( document )
2013-03-04 02:22:11 +00:00
. on ( "click" , func )
. off ( "click" , func )
2013-01-27 04:48:59 +00:00
. trigger ( "click" )
2013-03-04 02:22:11 +00:00
. off ( "click" ) ;
2009-01-08 22:22:33 +00:00
} ) ;
2013-03-04 02:22:11 +00:00
test ( "off(eventObject)" , function ( ) {
2009-01-08 22:22:33 +00:00
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
2013-03-04 02:22:11 +00:00
. on ( "foo" , function ( ) {
2009-01-08 22:22:33 +00:00
num += 1 ;
} )
2013-03-04 02:22:11 +00:00
. on ( "foo" , function ( e ) {
$elem . off ( e ) ;
2009-01-08 22:22:33 +00:00
num += 2 ;
} )
// Neither this one
2013-03-04 02:22:11 +00:00
. on ( "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
2013-03-04 02:22:11 +00:00
$elem . off ( "bar" ) ;
2009-01-08 22:22:33 +00:00
assert ( 1 ) ;
2010-12-30 06:34:48 +00:00
2013-03-04 02:22:11 +00:00
$elem . off ( ) ;
2009-01-08 22:22:33 +00:00
assert ( 0 ) ;
} ) ;
2013-01-27 04:48:59 +00:00
if ( jQuery . fn . hover ) {
test ( "hover() mouseenter mouseleave" , function ( ) {
expect ( 1 ) ;
2011-10-27 21:11:40 +00:00
2013-01-27 04:48:59 +00:00
var times = 0 ,
handler1 = function ( event ) { ++ times ; } ,
handler2 = function ( event ) { ++ times ; } ;
2009-05-06 02:17:24 +00:00
2013-01-27 04:48:59 +00:00
jQuery ( "#firstp" )
. hover ( handler1 , handler2 )
. mouseenter ( ) . mouseleave ( )
2013-03-04 02:22:11 +00:00
. off ( "mouseenter" , handler1 )
. off ( "mouseleave" , handler2 )
2013-01-27 04:48:59 +00:00
. hover ( handler1 )
. mouseenter ( ) . mouseleave ( )
2013-03-04 02:22:11 +00:00
. off ( "mouseenter mouseleave" , handler1 )
2013-01-27 04:48:59 +00:00
. mouseenter ( ) . mouseleave ( ) ;
2009-05-06 02:17:24 +00:00
2013-01-27 04:48:59 +00:00
equal ( times , 4 , "hover handlers fired" ) ;
2011-10-27 21:11:40 +00:00
2013-01-27 04:48:59 +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 />" ) ;
2013-01-27 04:48:59 +00:00
elem . on ( "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
2013-03-04 02:22:11 +00:00
jQuery ( "#jc-outer" ) . on ( "mouseenter mouseleave" , function ( event ) {
2011-06-14 19:38:46 +00:00
equal ( this . id , "jc-outer" , this . id + " " + event . type ) ;
} ) . trigger ( "mouseenter" ) ;
jQuery ( "#jc-inner" ) . trigger ( "mousenter" ) ;
2013-03-04 02:22:11 +00:00
jQuery ( "#jc-outer" ) . off ( "mouseenter mouseleave" ) . remove ( ) ;
2011-06-14 19:38:46 +00:00
jQuery ( "#jc-inner" ) . remove ( ) ;
} ) ;
test ( "mouseenter, mouseleave don't catch exceptions" , function ( ) {
expect ( 2 ) ;
2013-01-27 04:48:59 +00:00
var elem = jQuery ( "#firstp" ) . on ( "mouseenter mouseleave" , function ( ) {
throw "an Exception" ;
} ) ;
2011-06-14 19:38:46 +00:00
try {
2013-01-27 04:48:59 +00:00
elem . trigger ( "mouseenter" ) ;
2011-06-14 19:38:46 +00:00
} 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 {
2013-01-27 04:48:59 +00:00
elem . trigger ( "mouseleave" ) ;
2011-06-14 19:38:46 +00:00
} 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
}
} ) ;
2013-01-27 04:48:59 +00:00
if ( jQuery . fn . click ) {
2011-01-09 21:58:47 +00:00
2013-01-27 04:48:59 +00:00
test ( "trigger() shortcuts" , function ( ) {
expect ( 6 ) ;
2010-12-30 06:34:48 +00:00
2013-01-27 04:48:59 +00:00
var elem = jQuery ( "<li><a href='#'>Change location</a></li>" ) . prependTo ( "#firstUL" ) ;
2013-03-04 02:22:11 +00:00
elem . find ( "a" ) . on ( "click" , function ( ) {
2013-01-27 04:48:59 +00:00
var close = jQuery ( "spanx" , this ) ; // same with jQuery(this).find("span");
equal ( close . length , 0 , "Context element does not exist, length must be zero" ) ;
ok ( ! close [ 0 ] , "Context element does not exist, direct access to element must return undefined" ) ;
return false ;
} ) . click ( ) ;
2011-01-09 21:58:47 +00:00
2013-01-27 04:48:59 +00:00
// manually clean up detached elements
elem . remove ( ) ;
2010-12-30 06:34:48 +00:00
2013-01-27 04:48:59 +00:00
jQuery ( "#check1" ) . click ( function ( ) {
ok ( true , "click event handler for checkbox gets fired twice, see #815" ) ;
} ) . click ( ) ;
2010-12-30 06:34:48 +00:00
2013-01-27 04:48:59 +00:00
var counter = 0 ;
jQuery ( "#firstp" ) [ 0 ] . onclick = function ( event ) {
counter ++ ;
} ;
jQuery ( "#firstp" ) . click ( ) ;
equal ( counter , 1 , "Check that click, triggers onclick event handler also" ) ;
2010-12-30 06:34:48 +00:00
2013-01-27 04:48:59 +00:00
var clickCounter = 0 ;
jQuery ( "#simon1" ) [ 0 ] . onclick = function ( event ) {
clickCounter ++ ;
} ;
jQuery ( "#simon1" ) . click ( ) ;
equal ( clickCounter , 1 , "Check that click, triggers onclick event handler on an a tag also" ) ;
2011-01-09 21:58:47 +00:00
2013-01-27 04:48:59 +00:00
elem = jQuery ( "<img />" ) . load ( function ( ) {
ok ( true , "Trigger the load event, using the shortcut .load() (#2819)" ) ;
} ) . load ( ) ;
2011-04-12 20:48:22 +00:00
2013-01-27 04:48:59 +00:00
// manually clean up detached elements
elem . remove ( ) ;
// test that special handlers do not blow up with VML elements (#7071)
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" ) ;
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
2013-03-04 02:22:11 +00:00
jQuery ( window ) . on ( "click" , function ( e ) { win ++ ; } ) ;
jQuery ( document ) . on ( "click" , function ( e ) { if ( e . target !== document ) { doc ++ ; } } ) ;
jQuery ( "html" ) . on ( "click" , function ( e ) { html ++ ; } ) ;
jQuery ( "body" ) . on ( "click" , function ( e ) { body ++ ; } ) ;
jQuery ( "#qunit-fixture" ) . on ( "click" , function ( e ) { main ++ ; } ) ;
jQuery ( "#ap" ) . on ( "click" , function ( ) { ap ++ ; return false ; } ) ;
2008-12-22 01:57:06 +00:00
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
2013-03-04 02:22:11 +00:00
jQuery ( document ) . off ( "click" ) ;
jQuery ( "html, body, #qunit-fixture" ) . off ( "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-12-18 19:42:37 +00:00
jQuery ( document ) . on ( "mouseenter" , "#firstp" , function ( ) {
ok ( true , "Trigger mouseenter bound by on" ) ;
2011-04-17 18:58:20 +00:00
} ) ;
2012-12-18 19:42:37 +00:00
jQuery ( document ) . on ( "mouseleave" , "#firstp" , function ( ) {
ok ( true , "Trigger mouseleave bound by on" ) ;
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-12-18 19:42:37 +00:00
jQuery ( document ) . off ( "mouseenter mouseleave" , "#firstp" ) ;
2011-07-26 15:49:23 +00:00
2013-02-27 20:44:34 +00:00
// Triggers handlers and native
2007-08-30 05:51:11 +00:00
// Trigger 5
2013-03-04 02:22:11 +00:00
$elem . on ( "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 {
2013-02-19 04:52:29 +00:00
elem2 = jQuery ( "#form input" ) . eq ( 0 ) ;
2012-06-11 01:54:16 +00:00
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 {
2013-03-04 02:22:11 +00:00
jQuery ( "#qunit-fixture table" ) . eq ( 0 ) . on ( "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
2013-01-27 04:48:59 +00:00
form . on ( "submit" , function ( ) {
2013-03-04 02:22:11 +00:00
ok ( true , "Local `on` still works." ) ;
2010-01-25 18:45:07 +00:00
return false ;
} ) ;
// Trigger 1
form . trigger ( "submit" ) ;
2013-03-04 02:22:11 +00:00
form . off ( "submit" ) ;
2010-01-25 18:45:07 +00:00
2013-01-27 04:48:59 +00:00
jQuery ( document ) . on ( "submit" , function ( ) {
2010-01-25 18:45:07 +00:00
ok ( true , "Make sure bubble works up to document." ) ;
return false ;
} ) ;
// Trigger 1
form . trigger ( "submit" ) ;
2013-03-04 02:22:11 +00:00
jQuery ( document ) . off ( "submit" ) ;
2010-01-25 18:45:07 +00:00
form . remove ( ) ;
} ) ;
2012-12-13 20:52:59 +00:00
test ( "submit event bubbles on copied forms (#11649)" , function ( ) {
2012-05-18 20:30:28 +00:00
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
2013-01-27 04:48:59 +00:00
$testForm . on ( "submit" , noSubmit ) . find ( "input[name=sub1]" ) . trigger ( "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" ) ;
2012-12-13 20:52:59 +00:00
$formByHTML = jQuery ( jQuery . parseHTML ( $fixture . html ( ) ) ) . filter ( "#testForm" ) . removeAttr ( "id" ) ;
2012-05-18 20:30:28 +00:00
$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
2013-01-27 04:48:59 +00:00
$wrapperDiv . find ( "form" ) . on ( "submit" , noSubmit ) . find ( "input[name=sub1]" ) . trigger ( "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
2013-01-27 04:48:59 +00:00
$form . find ( "select[name=select1]" ) . val ( "1" ) . trigger ( "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" ) ;
2012-12-13 20:52:59 +00:00
$formByHTML = jQuery ( jQuery . parseHTML ( $fixture . html ( ) ) ) . filter ( "#form" ) . removeAttr ( "id" ) ;
2012-05-21 23:01:59 +00:00
$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
2013-01-27 04:48:59 +00:00
$wrapperDiv . find ( "form select[name=select1]" ) . val ( "2" ) . trigger ( "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
2013-03-04 02:22:11 +00:00
$parent . on ( "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
2013-03-04 02:22:11 +00:00
$parent . off ( ) ;
2008-12-31 02:58:13 +00:00
function error ( ) {
ok ( false , "This assertion shouldn't be reached" ) ;
}
2010-12-30 06:34:48 +00:00
2013-03-04 02:22:11 +00:00
$parent . on ( "foo" , error ) ;
2010-12-30 06:34:48 +00:00
2013-03-04 02:22:11 +00:00
$child . on ( "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.
2013-03-04 02:22:11 +00:00
//$child.on("foo", error );
2010-12-30 06:34:48 +00:00
2008-12-31 02:58:13 +00:00
event = new jQuery . Event ( "foo" ) ;
2013-03-04 02:22:11 +00:00
$child . trigger ( event , [ 1 , 2 , 3 ] ) . off ( ) ;
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
2013-03-04 02:22:11 +00:00
$child . off ( ) ;
$parent . off ( ) . 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" ) ;
} )
2013-01-27 04:48:59 +00:00
. trigger ( "click" )
2011-10-13 20:30:40 +00:00
. 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" ) ;
} ) ;
2013-01-14 00:53:19 +00:00
test ( "Delegated events in SVG (#10791; #13180)" , function ( ) {
2011-11-16 15:35:53 +00:00
expect ( 2 ) ;
2013-01-14 03:59:20 +00:00
var useElem , e ,
2013-01-14 00:53:19 +00:00
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'>" +
2013-01-14 00:53:19 +00:00
"<defs><rect id='ref' x='10' y='20' width='100' height='60' r='10' rx='10' ry='10'></rect></defs>" +
2012-10-16 14:17:14 +00:00
"<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>" +
2013-01-14 00:53:19 +00:00
"<use id='use' xlink:href='#ref'></use>" +
2012-10-16 14:17:14 +00:00
"</svg>"
2013-01-14 00:53:19 +00:00
) ;
2012-01-13 01:30:45 +00:00
2013-01-14 00:53:19 +00:00
jQuery ( "#qunit-fixture" )
. append ( svg )
2011-11-16 15:35:53 +00:00
. 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']" )
2013-01-14 00:53:19 +00:00
. trigger ( "click" )
. end ( ) ;
// Fire a native click on an SVGElementInstance (the instance tree of an SVG <use>)
// to confirm that it doesn't break our event delegation handling (#13180)
2013-01-14 03:59:20 +00:00
useElem = svg . find ( "#use" ) [ 0 ] ;
if ( document . createEvent && useElem && useElem . instanceRoot ) {
2013-01-14 00:53:19 +00:00
e = document . createEvent ( "MouseEvents" ) ;
e . initEvent ( "click" , true , true ) ;
2013-01-14 03:59:20 +00:00
useElem . instanceRoot . dispatchEvent ( e ) ;
2013-01-14 00:53:19 +00:00
}
2011-11-16 15:35:53 +00:00
2013-01-14 00:53:19 +00:00
jQuery ( "#qunit-fixture" ) . off ( "click" ) ;
2011-11-16 15:35:53 +00:00
} ) ;
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 ) {
2013-02-03 20:27:55 +00:00
asyncTest ( "on(beforeunload)" , 1 , function ( ) {
2013-01-28 04:34:38 +00:00
var iframe = jQuery ( jQuery . parseHTML ( "<iframe src='data/event/onbeforeunload.html'><iframe>" ) ) ;
2012-06-18 02:42:24 +00:00
2013-02-03 20:27:55 +00:00
window . onmessage = function ( event ) {
var payload = JSON . parse ( event . data ) ;
2012-06-18 02:42:24 +00:00
2013-02-03 20:27:55 +00:00
ok ( payload . event , "beforeunload" , "beforeunload event" ) ;
2012-06-18 02:42:24 +00:00
2013-02-03 20:27:55 +00:00
iframe . remove ( ) ;
window . onmessage = null ;
start ( ) ;
} ;
2012-08-14 20:10:10 +00:00
2013-02-03 20:27:55 +00:00
iframe . appendTo ( "#qunit-fixture" ) ;
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" ) ;
2013-03-04 02:22:11 +00:00
jQuery ( "body" ) . on ( "keydown" , handler ) . trigger ( event ) ;
2011-04-05 19:55:40 +00:00
2013-03-04 02:22:11 +00:00
jQuery ( "body" ) . off ( "keydown" ) ;
2011-04-05 19:55:40 +00:00
} ) ;
2013-02-02 02:20:15 +00:00
test ( "jQuery.Event properties" , function ( ) {
expect ( 12 ) ;
2011-11-11 02:53:07 +00:00
2013-02-02 02:20:15 +00:00
var handler ,
$structure = jQuery ( "<div id='ancestor'><p id='delegate'><span id='target'>shiny</span></p></div>" ) ,
$target = $structure . find ( "#target" ) ;
handler = function ( e ) {
strictEqual ( e . currentTarget , this , "currentTarget at " + this . id ) ;
equal ( e . isTrigger , 3 , "trigger at " + this . id ) ;
} ;
$structure . one ( "click" , handler ) ;
$structure . one ( "click" , "p" , handler ) ;
$target . one ( "click" , handler ) ;
$target [ 0 ] . onclick = function ( e ) {
strictEqual ( e . currentTarget , this , "currentTarget at target (native handler)" ) ;
equal ( e . isTrigger , 3 , "trigger at target (native handler)" ) ;
} ;
$target . trigger ( "click" ) ;
$target . one ( "click" , function ( e ) {
equal ( e . isTrigger , 2 , "triggerHandler at target" ) ;
} ) ;
$target [ 0 ] . onclick = function ( e ) {
equal ( e . isTrigger , 2 , "triggerHandler at target (native handler)" ) ;
} ;
$target . triggerHandler ( "click" ) ;
handler = function ( e ) {
strictEqual ( e . isTrigger , undefined , "native event at " + this . id ) ;
} ;
$target . one ( "click" , handler ) ;
$target [ 0 ] . onclick = function ( e ) {
strictEqual ( e . isTrigger , undefined , "native event at target (native handler)" ) ;
} ;
fireNative ( $target [ 0 ] , "click" ) ;
2009-02-17 12:38:16 +00:00
} ) ;
2013-03-04 03:00:57 +00:00
test ( ".on()/.off()" , 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 ;
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . on ( "submit" , "#qunit-fixture div" , function ( ) { submit ++ ; return false ; } ) ;
jQuery ( "#body" ) . on ( "click" , "#qunit-fixture div" , function ( ) { div ++ ; } ) ;
jQuery ( "#body" ) . on ( "click" , "div#nothiddendiv" , function ( ) { livea ++ ; } ) ;
jQuery ( "#body" ) . on ( "click" , "div#nothiddendivchild" , function ( ) { liveb ++ ; } ) ;
2010-02-01 23:06:03 +00:00
// 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" ) ;
2013-03-04 03:00:57 +00:00
equal ( submit , 0 , "off Click on inner div" ) ;
equal ( div , 2 , "off Click on inner div" ) ;
equal ( livea , 1 , "off Click on inner div" ) ;
equal ( liveb , 1 , "off 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 ;
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . off ( "click" , "div#nothiddendivchild" ) ;
2010-02-01 23:06:03 +00:00
jQuery ( "div#nothiddendivchild" ) . trigger ( "click" ) ;
2013-03-04 03:00:57 +00:00
equal ( submit , 0 , "off Click on inner div" ) ;
equal ( div , 2 , "off Click on inner div" ) ;
equal ( livea , 1 , "off Click on inner div" ) ;
equal ( liveb , 0 , "off 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" ) ;
2013-03-04 03:00:57 +00:00
equal ( submit , 0 , "off Click on inner div" ) ;
equal ( div , 1 , "off Click on inner div" ) ;
equal ( livea , 1 , "off Click on inner div" ) ;
equal ( liveb , 0 , "off Click on inner div" ) ;
2010-02-01 23:06:03 +00:00
2013-02-27 20:44:34 +00:00
// Make sure that stopPropagation doesn't stop live events
2012-06-21 19:30:24 +00:00
submit = 0 ; div = 0 ; livea = 0 ; liveb = 0 ;
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . on ( "click" , "div#nothiddendivchild" , function ( e ) { liveb ++ ; e . stopPropagation ( ) ; } ) ;
2010-02-01 23:06:03 +00:00
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 ) ;
2013-03-04 03:00:57 +00:00
equal ( livea , 0 , "on secondary click" ) ;
2010-02-01 23:06:03 +00:00
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . off ( "click" , "div#nothiddendivchild" ) ;
jQuery ( "#body" ) . off ( "click" , "div#nothiddendiv" ) ;
jQuery ( "#body" ) . off ( "click" , "#qunit-fixture div" ) ;
jQuery ( "#body" ) . off ( "submit" , "#qunit-fixture div" ) ;
2010-02-01 23:06:03 +00:00
// Test binding with a different context
2013-03-04 03:00:57 +00:00
var clicked = 0 ;
jQuery ( "#qunit-fixture" ) . on ( "click" , "#foo" , 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" ) ;
2013-03-04 03:00:57 +00:00
equal ( clicked , 2 , "on with a context" ) ;
2010-02-01 23:06:03 +00:00
// Test unbinding with a different context
2013-03-04 03:00:57 +00:00
jQuery ( "#qunit-fixture" ) . off ( "click" , "#foo" ) ;
2011-04-12 08:47:46 +00:00
jQuery ( "#foo" ) . trigger ( "click" ) ;
2013-03-04 03:00:57 +00:00
equal ( clicked , 2 , "off with a context" ) ;
2010-02-01 23:06:03 +00:00
// Test binding with event data
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . on ( "click" , "#foo" , true , function ( e ) { equal ( e . data , true , "on with event data" ) ; } ) ;
2010-02-01 23:06:03 +00:00
jQuery ( "#foo" ) . trigger ( "click" ) ;
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . off ( "click" , "#foo" ) ;
2010-02-01 23:06:03 +00:00
// Test binding with trigger data
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . on ( "click" , "#foo" , function ( e , data ) { equal ( data , true , "on with trigger data" ) ; } ) ;
2010-02-01 23:06:03 +00:00
jQuery ( "#foo" ) . trigger ( "click" , true ) ;
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . off ( "click" , "#foo" ) ;
2010-02-01 23:06:03 +00:00
// Test binding with different this object
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . on ( "click" , "#foo" , jQuery . proxy ( function ( e ) { equal ( this [ "foo" ] , "bar" , "on with event scope" ) ; } , { "foo" : "bar" } ) ) ;
2010-02-01 23:06:03 +00:00
jQuery ( "#foo" ) . trigger ( "click" ) ;
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . off ( "click" , "#foo" ) ;
2010-02-01 23:06:03 +00:00
// Test binding with different this object, event data, and trigger data
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . on ( "click" , "#foo" , true , jQuery . proxy ( function ( e , data ) {
equal ( e . data , true , "on with with different this object, event data, and trigger data" ) ;
equal ( this . foo , "bar" , "on with with different this object, event data, and trigger data" ) ;
equal ( data , true , "on 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 ) ;
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . off ( "click" , "#foo" ) ;
2010-02-01 23:06:03 +00:00
// Verify that return false prevents default action
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . on ( "click" , "#anchor2" , function ( ) { return false ; } ) ;
2010-02-01 23:06:03 +00:00
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" ) ;
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . off ( "click" , "#anchor2" ) ;
2010-02-01 23:06:03 +00:00
// Verify that .preventDefault() prevents default action
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . on ( "click" , "#anchor2" , 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" ) ;
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . off ( "click" , "#anchor2" ) ;
2010-02-01 23:06:03 +00:00
// Test binding the same handler to multiple points
var called = 0 ;
function callback ( ) { called ++ ; return false ; }
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . on ( "click" , "#nothiddendiv" , callback ) ;
jQuery ( "#body" ) . on ( "click" , "#anchor2" , callback ) ;
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 , 1 , "Verify that only one click occurred." ) ;
2010-02-01 23:06:03 +00:00
// Make sure that only one callback is removed
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . off ( "click" , "#anchor2" , callback ) ;
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 ( "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
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . on ( "foo" , "#nothiddendiv" , callback ) ;
2010-02-01 23:06:03 +00:00
// Cleanup
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . off ( "click" , "#nothiddendiv" , callback ) ;
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 ( "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
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . off ( "foo" , "#nothiddendiv" , 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
2013-03-04 03:00:57 +00:00
var livec = 0 ;
jQuery ( "#nothiddendivchild" ) . html ( "<span></span>" ) ;
2010-12-30 06:34:48 +00:00
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . on ( "click" , "#nothiddendivchild" , function ( e ) { jQuery ( "#nothiddendivchild" ) . html ( "" ) ; } ) ;
jQuery ( "#body" ) . on ( "click" , "#nothiddendivchild" , function ( e ) { if ( e . target ) { livec ++ ; } } ) ;
2010-12-30 06:34:48 +00:00
2013-01-27 04:48:59 +00:00
jQuery ( "#nothiddendiv span" ) . trigger ( "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
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . off ( "click" , "#nothiddendivchild" ) ;
2010-02-01 23:06:03 +00:00
2013-02-27 20:44:34 +00:00
// Verify that .live() occurs and cancel bubble in the same order as
2013-03-04 02:22:11 +00:00
// we would expect .on() and .click() without delegation
2010-02-01 23:06:03 +00:00
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
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . on ( "click" , "span#liveSpan1 a" , function ( ) { lived ++ ; return false ; } ) ;
jQuery ( "#body" ) . on ( "click" , "span#liveSpan1" , function ( ) { livee ++ ; } ) ;
2010-02-01 23:06:03 +00:00
2013-01-27 04:48:59 +00:00
jQuery ( "span#liveSpan1 a" ) . trigger ( "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
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . on ( "click" , "span#liveSpan2" , function ( ) { livee ++ ; } ) ;
jQuery ( "#body" ) . on ( "click" , "span#liveSpan2 a" , function ( ) { lived ++ ; return false ; } ) ;
2010-02-01 23:06:03 +00:00
lived = 0 ;
livee = 0 ;
2013-01-27 04:48:59 +00:00
jQuery ( "span#liveSpan2 a" ) . trigger ( "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
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . off ( "click" , "**" ) ;
2010-12-30 06:34:48 +00:00
2010-02-01 23:06:03 +00:00
// Test this, target and currentTarget are correct
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . on ( "click" , "span#liveSpan1" , function ( e ) {
equal ( this . id , "liveSpan1" , "Check the this within a on handler" ) ;
equal ( e . currentTarget . id , "liveSpan1" , "Check the event.currentTarget within a on handler" ) ;
equal ( e . delegateTarget , document . body , "Check the event.delegateTarget within a on handler" ) ;
equal ( e . target . nodeName . toUpperCase ( ) , "A" , "Check the event.target within a on handler" ) ;
2010-02-01 23:06:03 +00:00
} ) ;
2010-12-30 06:34:48 +00:00
2013-01-27 04:48:59 +00:00
jQuery ( "span#liveSpan1 a" ) . trigger ( "click" ) ;
2010-12-30 06:34:48 +00:00
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . off ( "click" , "span#liveSpan1" ) ;
2010-02-01 23:06:03 +00:00
// Work with deep selectors
livee = 0 ;
function clickB ( ) { livee ++ ; }
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . on ( "click" , "#nothiddendiv div" , function ( ) { livee ++ ; } ) ;
jQuery ( "#body" ) . on ( "click" , "#nothiddendiv div" , clickB ) ;
jQuery ( "#body" ) . on ( "mouseover" , "#nothiddendiv div" , function ( ) { livee ++ ; } ) ;
2010-02-01 23:06:03 +00:00
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
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . off ( "mouseover" , "#nothiddendiv div" ) ;
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 , 0 , "Mouseover, deep selector." ) ;
2010-02-01 23:06:03 +00:00
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . off ( "click" , "#nothiddendiv div" , clickB ) ;
2010-02-01 23:06:03 +00:00
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
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . off ( "click" , "#nothiddendiv div" ) ;
2010-02-01 23:06:03 +00:00
} ) ;
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 ) ;
} )
2013-01-27 04:48:59 +00:00
. find ( "a" ) . trigger ( "click" ) . trigger ( "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 ) ;
2013-02-19 04:52:29 +00:00
var markup = jQuery ( "<div><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></div>" ) . appendTo ( "#qunit-fixture" ) ;
// Non-positional selector (#12383)
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" ) . trigger ( "click" ) . end ( )
. off ( "click" ) ;
2012-07-06 14:12:20 +00:00
2012-08-28 14:07:16 +00:00
// Positional selector (#11315)
2013-02-19 04:52:29 +00:00
markup . find ( "ul" ) . eq ( 0 )
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 ( )
2013-01-27 04:48:59 +00:00
. find ( "a" ) . trigger ( "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
markup . remove ( ) ;
2012-06-27 00:36:00 +00:00
} ) ;
2013-01-14 02:38:40 +00:00
test ( "delegated event with selector matching Object.prototype property (#13203)" , function ( ) {
expect ( 1 ) ;
var matched = 0 ;
jQuery ( "#foo" ) . on ( "click" , "toString" , function ( e ) {
matched ++ ;
} ) ;
jQuery ( "#anchor2" ) . trigger ( "click" ) ;
equal ( matched , 0 , "Nothing matched 'toString'" ) ;
} ) ;
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" ) ;
} )
2013-01-27 04:48:59 +00:00
. find ( "a" ) . trigger ( "click" ) . end ( )
2011-10-21 14:56:16 +00:00
. remove ( ) ;
} ) ;
2013-03-04 03:00:57 +00:00
test ( "off all bound delegated 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
2013-03-04 03:00:57 +00:00
div . on ( "click submit" , "div#nothiddendivchild" , function ( ) { count ++ ; } ) ;
2013-03-04 02:22:11 +00:00
div . on ( "click" , function ( ) { clicks ++ ; } ) ;
2013-03-04 03:00:57 +00:00
div . off ( undefined , "**" ) ;
2010-02-01 23:06:03 +00:00
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." ) ;
2013-03-04 02:22:11 +00:00
div . off ( "click" ) ;
2010-02-01 23:06:03 +00:00
} ) ;
2013-03-04 03:00:57 +00:00
test ( "on with multiple delegated events" , function ( ) {
2010-02-01 23:06:03 +00:00
expect ( 1 ) ;
var count = 0 ;
var div = jQuery ( "#body" ) ;
2013-03-04 03:00:57 +00:00
div . on ( "click submit" , "div#nothiddendivchild" , function ( ) { count ++ ; } ) ;
2010-02-01 23:06:03 +00:00
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
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . off ( undefined , "**" ) ;
2010-02-01 23:06:03 +00:00
} ) ;
2013-03-04 03:00:57 +00:00
test ( "delegated on 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']" ) ;
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . on ( "change" , "select[name='S1']" , function ( ) {
2010-02-01 23:06:03 +00:00
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
} ;
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . on ( "change" , "#check2" , 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 ( ) ;
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . on ( "change" , "#name" , function ( ) {
2010-02-01 23:06:03 +00:00
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 ) ;
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . off ( "change" , "#name" ) ;
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 ( ) ;
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . on ( "change" , "#name" , function ( ) {
2010-02-01 23:06:03 +00:00
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 ) ;
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . off ( "change" , "#name" ) ;
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 ;
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . off ( "change" , "select[name='S1']" ) ;
2010-02-01 23:06:03 +00:00
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
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . off ( "change" , "#check2" , checkboxFunction ) ;
2010-02-01 23:06:03 +00:00
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
} ) ;
2013-03-04 03:00:57 +00:00
test ( "delegated on 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
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . on ( "submit" , "#testForm" , function ( ev ) {
2010-02-01 23:06:03 +00:00
count1 ++ ;
ev . preventDefault ( ) ;
} ) ;
2013-03-04 03:00:57 +00:00
jQuery ( document ) . on ( "submit" , "body" , function ( ev ) {
2010-02-01 23:06:03 +00:00
count2 ++ ;
ev . preventDefault ( ) ;
} ) ;
2013-01-27 04:48:59 +00:00
jQuery ( "#testForm input[name=sub1]" ) . trigger ( "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
2013-03-04 03:00:57 +00:00
jQuery ( "#body" ) . off ( undefined , "**" ) ;
jQuery ( document ) . off ( undefined , "**" ) ;
2009-09-14 22:04:22 +00:00
} ) ;
2013-03-04 03:00:57 +00:00
test ( "delegated off() 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
2013-03-04 03:00:57 +00:00
$delegate . on ( "click.ns" , "a" , function ( e ) {
2011-04-05 22:12:50 +00:00
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
2013-03-04 03:00:57 +00:00
$delegate . off ( ".ns" , "**" ) ;
2011-04-05 22:12:50 +00:00
jQuery ( "a" , $delegate ) . eq ( 1 ) . trigger ( "click.ns" ) ;
2013-03-04 03:00:57 +00:00
equal ( count , 1 , "no more .ns after off" ) ;
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 = { } ;
2013-03-04 02:22:11 +00:00
jQuery ( o ) . on ( "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>" ) ;
2013-01-27 04:48:59 +00:00
markup . on ( "click" , function ( e ) {
2011-11-07 16:07:36 +00:00
ok ( e . isDefaultPrevented ( ) , "inline handler prevented default" ) ;
return false ;
} ) ;
2013-01-27 04:48:59 +00:00
markup . find ( "a" ) . trigger ( "click" ) ;
2011-11-07 16:07:36 +00:00
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 ) ;
2013-01-27 04:48:59 +00:00
jQuery ( window ) . off ( ) ;
2010-10-11 22:03:54 +00:00
2013-01-27 04:48:59 +00:00
jQuery ( window ) . on ( "resize" , function ( ) {
2010-10-11 22:03:54 +00:00
ok ( true , "Resize event fired." ) ;
2013-01-27 04:48:59 +00:00
} ) . trigger ( "resize" ) . off ( "resize" ) ;
2010-10-11 22:20:57 +00:00
2013-01-27 04:48:59 +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 ( ) ;
2013-03-04 02:22:11 +00:00
jQuery ( "body" ) . on ( "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
} ) ;
2013-03-04 02:22:11 +00:00
input . on ( "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 ( ) ;
2013-03-04 02:22:11 +00:00
jQuery ( "body" ) . off ( "focusin.focusinBubblesTest" ) ;
2011-03-05 02:16:40 +00:00
} ) ;
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
} )
2013-01-27 04:48:59 +00:00
. trigger ( "click" )
2011-07-29 00:43:23 +00:00
. 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" )
2013-01-27 04:48:59 +00:00
. trigger ( "click" )
2011-07-29 00:43:23 +00:00
. 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
} ) ;
2013-03-04 03:00:57 +00:00
test ( "special on 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 ) ;
2013-01-27 04:48:59 +00:00
clone . find ( "td" ) . trigger ( "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" ) ;
2013-01-27 04:48:59 +00:00
jQuery ( cb ) . trigger ( "click" ) ;
2012-11-21 03:31:33 +00:00
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" ) ,
2013-01-27 04:48:59 +00:00
$radio = jQuery ( "#radio1" ) . trigger ( "focus" ) ,
2012-11-23 20:03:55 +00:00
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" ) ;
2013-01-27 04:48:59 +00:00
$text . trigger ( "focus" ) ;
2012-11-23 20:03:55 +00:00
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
2013-03-04 02:22:11 +00:00
$fixture . on ( "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" ) ;
2013-03-04 02:22:11 +00:00
$fixture . off ( "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
2013-03-04 02:22:11 +00:00
$fixture . on ( "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 ;
2013-03-04 02:22:11 +00:00
$fixture . off ( "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" ) ) ;
2013-03-04 02:22:11 +00:00
jQuery ( document ) . on ( "ready.readytest" , makeHandler ( "c" ) ) ;
2011-03-31 15:37:48 +00:00
// Do it twice, just to be sure.
jQuery ( makeHandler ( "d" ) ) ;
jQuery ( document ) . ready ( makeHandler ( "e" ) ) ;
2013-03-04 02:22:11 +00:00
jQuery ( document ) . on ( "ready.readytest" , makeHandler ( "f" ) ) ;
2011-03-31 15:37:48 +00:00
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.
2013-03-04 02:22:11 +00:00
deepEqual ( order , [ "a" , "b" , "d" , "e" , "c" , "f" ] , "Bound DOM ready handlers should execute in on-order, but those bound with jQuery(document).on( '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" ) ;
2013-03-04 02:22:11 +00:00
ok ( args [ "c" ] instanceof jQuery . Event , "Argument passed to fn in jQuery(document).on( '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
2013-03-04 02:22:11 +00:00
jQuery ( document ) . on ( "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.
2013-03-04 02:22:11 +00:00
jQuery ( document ) . off ( "ready.readytest" ) ;
2011-03-31 15:37:48 +00:00
} ) ;
} ) ( ) ;
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
2013-03-04 02:22:11 +00:00
$fixture . on ( "change" , function ( event ) { } ) ;
$fixture . off ( "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 ( ) {
2013-02-03 20:27:55 +00:00
jQuery ( this ) . triggerHandler ( e ) ;
2012-10-16 00:22:19 +00:00
} ) ;
}
} )
. 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" ) ;
} ) ;
2013-01-27 04:48:59 +00:00
fixture . clone ( ) . trigger ( "click" ) . trigger ( "change" ) ; // 0 events should be fired
2012-11-18 18:53:24 +00:00
clone = fixture . clone ( true ) ;
2013-02-19 04:52:29 +00:00
clone . find ( "p" ) . eq ( 0 ) . trigger ( "click" , true ) ; // 3 events should fire
2012-11-18 18:53:24 +00:00
clone . find ( "#check1" ) . trigger ( "change" , true ) ; // 3 events should fire
clone . remove ( ) ;
clone = fixture . clone ( true , true ) ;
2013-02-19 04:52:29 +00:00
clone . find ( "p" ) . eq ( 0 ) . trigger ( "click" , true ) ; // 3 events should fire
2012-11-18 18:53:24 +00:00
clone . find ( "#check1" ) . trigger ( "change" , true ) ; // 3 events should fire
fixture . off ( ) ;
p . off ( ) ;
checkbox . off ( ) ;
2013-01-27 04:48:59 +00:00
p . trigger ( "click" ) ; // 0 should be fired
checkbox . trigger ( "change" ) ; // 0 should be fired
2012-11-18 18:53:24 +00:00
2013-02-19 04:52:29 +00:00
clone . find ( "p" ) . eq ( 0 ) . trigger ( "click" , true ) ; // 3 events should fire
2012-11-18 18:53:24 +00:00
clone . find ( "#check1" ) . trigger ( "change" , true ) ; // 3 events should fire
clone . remove ( ) ;
2013-02-19 04:52:29 +00:00
clone . find ( "p" ) . eq ( 0 ) . trigger ( "click" ) ; // 0 should be fired
2013-01-27 04:48:59 +00:00
clone . find ( "#check1" ) . trigger ( "change" ) ; // 0 events should fire
2012-11-18 18:53:24 +00:00
} ) ;
2012-12-17 14:17:39 +00:00
test ( "Check order of focusin/focusout events" , 2 , function ( ) {
var focus , blur ,
input = jQuery ( "#name" ) ;
input . on ( "focus" , function ( ) {
focus = true ;
} ) . on ( "focusin" , function ( ) {
ok ( ! focus , "Focusin event should fire before focus does" ) ;
} ) . on ( "blur" , function ( ) {
blur = true ;
} ) . on ( "focusout" , function ( ) {
ok ( ! blur , "Focusout event should fire before blur does" ) ;
} ) ;
// gain focus
2013-01-27 04:48:59 +00:00
input . trigger ( "focus" ) ;
2012-12-17 14:17:39 +00:00
// then lose it
2013-01-27 04:48:59 +00:00
jQuery ( "#search" ) . trigger ( "focus" ) ;
2012-12-17 14:17:39 +00:00
// cleanup
input . off ( ) ;
} ) ;
2013-01-31 18:01:34 +00:00
test ( "String.prototype.namespace does not cause trigger() to throw (#13360)" , function ( ) {
expect ( 1 ) ;
var errored = false ;
String . prototype . namespace = function ( ) { } ;
try {
jQuery ( "<p>" ) . trigger ( "foo.bar" ) ;
} catch ( e ) {
errored = true ;
}
equal ( errored , false , "trigger() did not throw exception" ) ;
delete String . prototype . namespace ;
} ) ;