2015-09-03 01:46:31 +00:00
|
|
|
QUnit.module( "event", {
|
2013-09-30 17:12:58 +00:00
|
|
|
setup: function() {
|
|
|
|
document.body.focus();
|
|
|
|
},
|
|
|
|
teardown: moduleTeardown
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2006-11-18 13:37:01 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "on() with non-null,defined data", function( assert ) {
|
2011-05-16 14:38:36 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.expect( 2 );
|
2011-05-16 14:38:36 +00:00
|
|
|
|
|
|
|
var handler = function( event, data ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( data, 0, "non-null, defined data (zero) is correctly passed" );
|
2011-10-28 18:17:14 +00:00
|
|
|
};
|
2011-05-16 14:38:36 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#foo" ).on( "foo.on", handler );
|
|
|
|
jQuery( "div" ).on( "foo.delegate", "#foo", handler );
|
2011-05-16 14:38:36 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#foo" ).trigger( "foo", 0 );
|
2011-05-16 14:38:36 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#foo" ).off( "foo.on", handler );
|
|
|
|
jQuery( "div" ).off( "foo.delegate", "#foo" );
|
2011-05-16 14:38:36 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-05-16 14:38:36 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "Handler changes and .trigger() order", function( assert ) {
|
|
|
|
assert.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-04-09 15:45:09 +00:00
|
|
|
.find( "*" ).addBack().on( "click", function() {
|
2011-10-13 20:30:40 +00:00
|
|
|
path += this.nodeName.toLowerCase() + " ";
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
2011-10-13 20:30:40 +00:00
|
|
|
.filter( "b" ).on( "click", function( e ) {
|
2015-09-02 23:52:01 +00:00
|
|
|
|
2011-10-13 20:30:40 +00:00
|
|
|
// Removing span should not stop propagation to original parents
|
|
|
|
if ( e.target === this ) {
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( this ).parent().remove();
|
2011-10-13 20:30:40 +00:00
|
|
|
}
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
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
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.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();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-09-08 01:02:13 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "on(), with data", function( assert ) {
|
|
|
|
assert.expect( 4 );
|
2013-04-09 15:45:09 +00:00
|
|
|
var test, handler, handler2;
|
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
handler = function( event ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( event.data, "on() with data, check passed data exists" );
|
|
|
|
assert.equal( event.data[ "foo" ], "bar", "on() with data, Check value of passed data" );
|
2007-04-24 21:48:52 +00:00
|
|
|
};
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#firstp" ).on( "click", { "foo": "bar" }, handler ).trigger( "click" ).off( "click", handler );
|
2007-09-08 23:31:23 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( !jQuery._data( jQuery( "#firstp" )[ 0 ], "events" ), "Event handler unbound when using data." );
|
2011-04-17 00:39:30 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
test = function() {};
|
|
|
|
handler2 = function( event ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( event.data, test, "on() with function data, Check value of passed data" );
|
2011-04-17 00:39:30 +00:00
|
|
|
};
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#firstp" ).on( "click", test, handler2 ).trigger( "click" ).off( "click", handler2 );
|
|
|
|
} );
|
2010-02-27 01:01:19 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "click(), with data", function( assert ) {
|
|
|
|
assert.expect( 3 );
|
2015-09-02 23:52:01 +00:00
|
|
|
var handler = function( event ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( event.data, "on() with data, check passed data exists" );
|
|
|
|
assert.equal( event.data[ "foo" ], "bar", "on() with data, Check value of passed data" );
|
2010-02-27 01:01:19 +00:00
|
|
|
};
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#firstp" ).on( "click", { "foo": "bar" }, handler ).trigger( "click" ).off( "click", handler );
|
2010-02-27 01:01:19 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( !jQuery._data( jQuery( "#firstp" )[ 0 ], "events" ), "Event handler unbound when using data." );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2008-01-14 09:33:08 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "on(), with data, trigger with data", function( assert ) {
|
|
|
|
assert.expect( 4 );
|
2015-09-02 23:52:01 +00:00
|
|
|
var handler = function( event, data ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( event.data, "check passed data exists" );
|
|
|
|
assert.equal( event.data.foo, "bar", "Check value of passed data" );
|
|
|
|
assert.ok( data, "Check trigger data" );
|
|
|
|
assert.equal( data.bar, "foo", "Check value of trigger data" );
|
2007-04-24 21:48:52 +00:00
|
|
|
};
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#firstp" ).on( "click", { foo: "bar" }, handler ).trigger( "click", [ { bar: "foo" } ] ).off( "click", handler );
|
|
|
|
} );
|
2008-01-14 09:33:08 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "on(), multiple events at once", function( assert ) {
|
|
|
|
assert.expect( 2 );
|
2013-04-09 15:45:09 +00:00
|
|
|
var handler,
|
|
|
|
clickCounter = 0,
|
2008-01-14 09:33:08 +00:00
|
|
|
mouseoverCounter = 0;
|
2015-09-02 23:52:01 +00:00
|
|
|
handler = function( event ) {
|
|
|
|
if ( event.type === "click" ) {
|
2007-12-15 05:55:33 +00:00
|
|
|
clickCounter += 1;
|
2015-09-02 23:52:01 +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-04-09 15:45:09 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#firstp" ).on( "click mouseover", handler ).trigger( "click" ).trigger( "mouseover" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( clickCounter, 1, "on() with multiple events at once" );
|
|
|
|
assert.equal( mouseoverCounter, 1, "on() with multiple events at once" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2008-01-14 09:33:08 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "on(), five events at once", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2011-04-29 01:14:12 +00:00
|
|
|
|
|
|
|
var count = 0,
|
2013-04-09 15:45:09 +00:00
|
|
|
handler = function() {
|
2011-10-28 18:17:14 +00:00
|
|
|
count++;
|
|
|
|
};
|
2011-04-29 01:14:12 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#firstp" ).on( "click mouseover foo bar baz", handler )
|
|
|
|
.trigger( "click" ).trigger( "mouseover" )
|
|
|
|
.trigger( "foo" ).trigger( "bar" )
|
|
|
|
.trigger( "baz" );
|
2011-04-29 01:14:12 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( count, 5, "on() five events at once" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-04-29 01:14:12 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "on(), multiple events at once and namespaces", function( assert ) {
|
|
|
|
assert.expect( 7 );
|
2010-01-25 21:45:39 +00:00
|
|
|
|
2013-04-09 15:45:09 +00:00
|
|
|
var cur, div,
|
|
|
|
obj = {};
|
2010-01-25 21:45:39 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
div = jQuery( "<div/>" ).on( "focusin.a", function( e ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( e.type, cur, "Verify right single event was fired." );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-01-25 21:45:39 +00:00
|
|
|
|
|
|
|
cur = "focusin";
|
2015-09-02 23:52:01 +00:00
|
|
|
div.trigger( "focusin.a" );
|
2010-01-25 21:45:39 +00:00
|
|
|
|
2011-01-09 21:58:47 +00:00
|
|
|
// manually clean up detached elements
|
|
|
|
div.remove();
|
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
div = jQuery( "<div/>" ).on( "click mouseover", obj, function( e ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( e.type, cur, "Verify right multi event was fired." );
|
|
|
|
assert.equal( e.data, obj, "Make sure the data came in correctly." );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-01-25 21:45:39 +00:00
|
|
|
|
|
|
|
cur = "click";
|
2015-09-02 23:52:01 +00:00
|
|
|
div.trigger( "click" );
|
2010-01-25 21:45:39 +00:00
|
|
|
|
|
|
|
cur = "mouseover";
|
2015-09-02 23:52:01 +00:00
|
|
|
div.trigger( "mouseover" );
|
2010-01-25 21:45:39 +00:00
|
|
|
|
2011-01-09 21:58:47 +00:00
|
|
|
// manually clean up detached elements
|
|
|
|
div.remove();
|
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
div = jQuery( "<div/>" ).on( "focusin.a focusout.b", function( e ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( e.type, cur, "Verify right multi event was fired." );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-01-25 21:45:39 +00:00
|
|
|
|
|
|
|
cur = "focusin";
|
2015-09-02 23:52:01 +00:00
|
|
|
div.trigger( "focusin.a" );
|
2010-01-25 21:45:39 +00:00
|
|
|
|
|
|
|
cur = "focusout";
|
2015-09-02 23:52:01 +00:00
|
|
|
div.trigger( "focusout.b" );
|
2011-01-09 21:58:47 +00:00
|
|
|
|
|
|
|
// manually clean up detached elements
|
|
|
|
div.remove();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-01-25 21:45:39 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "on(), namespace with special add", function( assert ) {
|
|
|
|
assert.expect( 27 );
|
2010-01-25 22:01:07 +00:00
|
|
|
|
2013-04-07 02:56:03 +00:00
|
|
|
var i = 0,
|
2015-09-02 23:52:01 +00:00
|
|
|
div = jQuery( "<div/>" ).appendTo( "#qunit-fixture" ).on( "test", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "Test event fired." );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-01-25 22:01:07 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery.event.special[ "test" ] = {
|
2013-04-07 02:56:03 +00:00
|
|
|
_default: function( e, data ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( e.type, "test", "Make sure we're dealing with a test event." );
|
|
|
|
assert.ok( data, "And that trigger data was passed." );
|
|
|
|
assert.strictEqual( e.target, div[ 0 ], "And that the target is correct." );
|
|
|
|
assert.equal( this, window, "And that the context is correct." );
|
2010-01-28 19:34:09 +00:00
|
|
|
},
|
2013-04-07 02:56:03 +00:00
|
|
|
setup: function() {},
|
|
|
|
teardown: function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "Teardown called." );
|
2010-02-11 06:42:51 +00:00
|
|
|
},
|
2010-02-04 05:20:52 +00:00
|
|
|
add: function( handleObj ) {
|
|
|
|
var handler = handleObj.handler;
|
2013-04-07 02:56:03 +00:00
|
|
|
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() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "Remove called." );
|
2010-02-13 11:10:43 +00:00
|
|
|
}
|
2010-01-25 22:01:07 +00:00
|
|
|
};
|
|
|
|
|
2013-04-07 02:56:03 +00:00
|
|
|
div.on( "test.a", { x: 1 }, function( e ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( !!e.xyz, "Make sure that the data is getting passed through." );
|
|
|
|
assert.equal( e.data[ "x" ], 1, "Make sure data is attached properly." );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-01-25 22:01:07 +00:00
|
|
|
|
2013-04-07 02:56:03 +00:00
|
|
|
div.on( "test.b", { x: 2 }, function( e ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( !!e.xyz, "Make sure that the data is getting passed through." );
|
|
|
|
assert.equal( e.data[ "x" ], 2, "Make sure data is attached properly." );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-01-25 22:01:07 +00:00
|
|
|
|
|
|
|
// Should trigger 5
|
2013-04-07 02:56:03 +00:00
|
|
|
div.trigger( "test", 33.33 );
|
2010-01-25 22:01:07 +00:00
|
|
|
|
|
|
|
// Should trigger 2
|
2013-04-07 02:56:03 +00:00
|
|
|
div.trigger( "test.a", "George Harrison" );
|
2010-01-25 22:01:07 +00:00
|
|
|
|
|
|
|
// Should trigger 2
|
2013-04-07 02:56:03 +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
|
2015-09-02 23:52:01 +00:00
|
|
|
div.off( "test" );
|
2010-02-13 11:10:43 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
div = jQuery( "<div/>" ).on( "test", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "Test event fired." );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-02-13 11:10:43 +00:00
|
|
|
|
|
|
|
// Should trigger 2
|
2015-09-02 23:52:01 +00:00
|
|
|
div.appendTo( "#qunit-fixture" ).remove();
|
2010-02-13 11:10:43 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
delete jQuery.event.special[ "test" ];
|
|
|
|
} );
|
2010-01-25 22:01:07 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "on(), no data", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2015-09-02 23:52:01 +00:00
|
|
|
var handler = function( event ) {
|
2015-09-08 00:26:29 +00:00
|
|
|
assert.ok( !event.data, "Check that no data is added to the event object" );
|
2007-04-24 21:48:52 +00:00
|
|
|
};
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#firstp" ).on( "click", handler ).trigger( "click" );
|
|
|
|
} );
|
2008-01-14 09:33:08 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "on/one/off(Object)", function( assert ) {
|
|
|
|
assert.expect( 6 );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2013-04-09 15:45:09 +00:00
|
|
|
var $elem,
|
|
|
|
clickCounter = 0,
|
|
|
|
mouseoverCounter = 0;
|
2012-06-21 19:30:24 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
function handler( event ) {
|
|
|
|
if ( event.type === "click" ) {
|
2009-09-16 02:19:18 +00:00
|
|
|
clickCounter++;
|
2015-09-02 23:52:01 +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
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
function handlerWithData( event ) {
|
|
|
|
if ( event.type === "click" ) {
|
2009-09-16 02:19:18 +00:00
|
|
|
clickCounter += event.data;
|
2015-09-02 23:52:01 +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
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
function trigger() {
|
|
|
|
$elem.trigger( "click" ).trigger( "mouseover" );
|
2009-09-16 02:19:18 +00:00
|
|
|
}
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
$elem = jQuery( "#firstp" )
|
|
|
|
|
2009-09-16 02:19:18 +00:00
|
|
|
// Regular bind
|
2015-09-02 23:52:01 +00:00
|
|
|
.on( {
|
2012-07-05 19:52:13 +00:00
|
|
|
"click":handler,
|
|
|
|
"mouseover":handler
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
|
|
|
|
2009-09-16 02:19:18 +00:00
|
|
|
// Bind with data
|
2015-09-02 23:52:01 +00:00
|
|
|
.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
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( clickCounter, 3, "on(Object)" );
|
|
|
|
assert.equal( mouseoverCounter, 3, "on(Object)" );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2009-09-16 02:19:18 +00:00
|
|
|
trigger();
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( clickCounter, 4, "on(Object)" );
|
|
|
|
assert.equal( mouseoverCounter, 4, "on(Object)" );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#firstp" ).off( {
|
2012-07-05 19:52:13 +00:00
|
|
|
"click":handler,
|
|
|
|
"mouseover":handler
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2009-09-16 02:19:18 +00:00
|
|
|
|
|
|
|
trigger();
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( clickCounter, 4, "on(Object)" );
|
|
|
|
assert.equal( mouseoverCounter, 4, "on(Object)" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2009-09-16 02:19:18 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "on/off(Object), on/off(Object, String)", function( assert ) {
|
|
|
|
assert.expect( 6 );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2013-04-09 15:45:09 +00:00
|
|
|
var events,
|
|
|
|
clickCounter = 0,
|
2013-02-19 04:52:29 +00:00
|
|
|
mouseoverCounter = 0,
|
2015-09-02 23:52:01 +00:00
|
|
|
$p = jQuery( "#firstp" ),
|
|
|
|
$a = $p.find( "a" ).eq( 0 );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2013-04-09 15:45:09 +00:00
|
|
|
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() {
|
2015-09-02 23:52:01 +00:00
|
|
|
$a.trigger( "click" ).trigger( "mouseover" );
|
2010-03-14 11:34:32 +00:00
|
|
|
}
|
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 02:22:11 +00:00
|
|
|
$p.on( events, "a", 2 );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2010-03-14 11:34:32 +00:00
|
|
|
trigger();
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( clickCounter, 3, "on" );
|
|
|
|
assert.equal( mouseoverCounter, 3, "on" );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2013-03-04 02:22:11 +00:00
|
|
|
$p.off( events, "a" );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2010-03-14 11:34:32 +00:00
|
|
|
trigger();
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( clickCounter, 4, "off" );
|
|
|
|
assert.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();
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( clickCounter, 4, "off" );
|
|
|
|
assert.equal( mouseoverCounter, 4, "off" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-03-14 11:34:32 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "on immediate propagation", function( assert ) {
|
|
|
|
assert.expect( 2 );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2013-02-19 04:52:29 +00:00
|
|
|
var lastClick,
|
2015-09-02 23:52:01 +00:00
|
|
|
$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 = "";
|
2015-09-02 23:52:01 +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();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2013-04-09 15:45:09 +00:00
|
|
|
jQuery( document ).on( "click", "#firstp a", function() {
|
2010-10-23 18:36:24 +00:00
|
|
|
lastClick = "click2";
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-10-23 18:36:24 +00:00
|
|
|
$a.trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.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 = "";
|
2015-09-02 23:52:01 +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();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2013-04-09 15:45:09 +00:00
|
|
|
$p.on( "click", "a", function() {
|
2010-10-23 18:36:24 +00:00
|
|
|
lastClick = "click2";
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-10-23 18:36:24 +00:00
|
|
|
$a.trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( lastClick, "click1", "on stopImmediatePropagation" );
|
2013-03-04 02:22:11 +00:00
|
|
|
$p.off( "click", "**" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-10-23 18:36:24 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "on bubbling, isDefaultPrevented, stopImmediatePropagation", function( assert ) {
|
|
|
|
assert.expect( 3 );
|
2010-12-27 19:30:05 +00:00
|
|
|
var $anchor2 = jQuery( "#anchor2" ),
|
2011-04-17 06:43:57 +00:00
|
|
|
$main = jQuery( "#qunit-fixture" ),
|
2014-03-06 03:46:23 +00:00
|
|
|
neverCallMe = function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( false, "immediate propagation should have been stopped" );
|
2014-03-06 03:46:23 +00:00
|
|
|
},
|
2015-09-02 23:52:01 +00:00
|
|
|
fakeClick = function( $jq ) {
|
|
|
|
|
2010-12-27 19:30:05 +00:00
|
|
|
// 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 );
|
2015-09-02 23:52:01 +00:00
|
|
|
$jq[ 0 ].dispatchEvent( e );
|
|
|
|
} else if ( $jq[ 0 ].click ) {
|
|
|
|
$jq[ 0 ].click(); // IE
|
2010-12-27 19:30:05 +00:00
|
|
|
}
|
|
|
|
};
|
2015-09-02 23:52:01 +00:00
|
|
|
$anchor2.on( "click", function( e ) {
|
2010-12-27 19:30:05 +00:00
|
|
|
e.preventDefault();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
$main.on( "click", "#foo", function( e ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( e.isDefaultPrevented(), true, "isDefaultPrevented true passed to bubbled event" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-12-27 19:30:05 +00:00
|
|
|
fakeClick( $anchor2 );
|
2013-03-04 02:22:11 +00:00
|
|
|
$anchor2.off( "click" );
|
|
|
|
$main.off( "click", "**" );
|
2013-04-09 15:45:09 +00:00
|
|
|
$anchor2.on( "click", function() {
|
2015-09-02 23:52:01 +00:00
|
|
|
|
2010-12-27 19:30:05 +00:00
|
|
|
// Let the default action occur
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
$main.on( "click", "#foo", function( e ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( e.isDefaultPrevented(), false, "isDefaultPrevented false passed to bubbled event" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-12-27 19:30:05 +00:00
|
|
|
fakeClick( $anchor2 );
|
2013-03-04 02:22:11 +00:00
|
|
|
$anchor2.off( "click" );
|
|
|
|
$main.off( "click", "**" );
|
2014-03-06 03:46:23 +00:00
|
|
|
|
2014-03-18 20:00:42 +00:00
|
|
|
// Android 2.3 doesn't support stopImmediatePropagation; jQuery fallbacks to stopPropagation
|
|
|
|
// in such a case.
|
|
|
|
// Support: Android 2.3
|
|
|
|
if ( !window.addEventListener || /android 2\.3/i.test( navigator.userAgent ) ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "Old IE or Android 2.3, skipping native stopImmediatePropagation check" );
|
2014-03-06 03:46:23 +00:00
|
|
|
} else {
|
|
|
|
$anchor2.on( "click", function( e ) {
|
|
|
|
e.stopImmediatePropagation();
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "anchor was clicked and prop stopped" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
$anchor2[ 0 ].addEventListener( "click", neverCallMe, false );
|
2014-03-06 03:46:23 +00:00
|
|
|
fakeClick( $anchor2 );
|
2015-09-02 23:52:01 +00:00
|
|
|
$anchor2[ 0 ].removeEventListener( "click", neverCallMe );
|
2014-03-06 03:46:23 +00:00
|
|
|
}
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-12-27 19:30:05 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "on(), iframes", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2012-10-15 16:16:49 +00:00
|
|
|
|
2007-07-20 21:53:37 +00:00
|
|
|
// events don't work with iframes, see #939 - this test fails in IE because of contentDocument
|
2015-09-02 23:52:01 +00:00
|
|
|
var doc = jQuery( "#loadediframe" ).contents();
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "div", doc ).on( "click", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "Binding to element inside iframe" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} ).trigger( "click" ).off( "click" );
|
|
|
|
} );
|
2008-01-14 09:33:08 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "on(), trigger change on select", function( assert ) {
|
|
|
|
assert.expect( 5 );
|
2007-03-25 18:06:18 +00:00
|
|
|
var counter = 0;
|
2015-09-02 23:52:01 +00:00
|
|
|
function selectOnChange( event ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( event.data, counter++, "Event.data is not a global event object" );
|
2012-06-21 19:30:24 +00:00
|
|
|
}
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#form select" ).each( function( i ) {
|
|
|
|
jQuery( this ).on( "change", i, selectOnChange );
|
|
|
|
} ).trigger( "change" );
|
|
|
|
} );
|
2007-09-03 14:53:09 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "on(), namespaced events, cloned events", function( assert ) {
|
|
|
|
assert.expect( 18 );
|
2015-07-30 17:48:26 +00:00
|
|
|
|
2011-02-10 02:15:32 +00:00
|
|
|
var firstp = jQuery( "#firstp" );
|
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
firstp.on( "custom.test", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( false, "Custom event triggered" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2008-02-03 04:33:11 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
firstp.on( "click", function( e ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "Normal click triggered" );
|
|
|
|
assert.equal( e.type + e.namespace, "click", "Check that only click events trigger this fn" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2007-09-03 14:53:09 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
firstp.on( "click.test", function( e ) {
|
2011-02-10 02:18:11 +00:00
|
|
|
var check = "click";
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "Namespaced click triggered" );
|
2011-02-10 02:18:11 +00:00
|
|
|
if ( e.namespace ) {
|
|
|
|
check += "test";
|
|
|
|
}
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( e.type + e.namespace, check, "Check that only click/click.test events trigger this fn" );
|
2015-09-02 23:52:01 +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)
|
2015-09-02 23:52:01 +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)
|
2015-09-02 23:52:01 +00:00
|
|
|
firstp.trigger( "click.test" );
|
2007-09-03 14:53:09 +00:00
|
|
|
|
|
|
|
// Remove only the one fn
|
2015-09-02 23:52:01 +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)
|
2015-09-02 23:52:01 +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
|
2015-09-02 23:52:01 +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)
|
2015-09-02 23:52:01 +00:00
|
|
|
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
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#nonnodes" ).contents().on( "tester", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( this.nodeType, 1, "Check node,textnode,comment on just does real nodes" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} ).trigger( "tester" );
|
2007-12-20 13:36:56 +00:00
|
|
|
|
|
|
|
// Make sure events stick with appendTo'd elements (which are cloned) #2027
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "<a href='#fail' class='test'>test</a>" ).on( "click", function() { return false; } ).appendTo( "#qunit-fixture" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( jQuery( "a.test" ).eq( 0 ).triggerHandler( "click" ) === false, "Handler is bound to appendTo'd elements" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2007-03-25 18:06:18 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "on(), multi-namespaced events", function( assert ) {
|
|
|
|
assert.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
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
function check( name, msg ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.deepEqual( name, order.shift(), msg );
|
2008-12-19 04:34:12 +00:00
|
|
|
}
|
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#firstp" ).on( "custom.test", function() {
|
|
|
|
check( "custom.test", "Custom event triggered" );
|
|
|
|
} );
|
2008-12-19 04:34:12 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#firstp" ).on( "custom.test2", function() {
|
|
|
|
check( "custom.test2", "Custom event triggered" );
|
|
|
|
} );
|
2008-12-19 04:34:12 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#firstp" ).on( "click.test", function() {
|
|
|
|
check( "click.test", "Normal click triggered" );
|
|
|
|
} );
|
2008-12-19 04:34:12 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#firstp" ).on( "click.test.abc", function() {
|
|
|
|
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)
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#firstp" ).trigger( "click.a.test" );
|
|
|
|
jQuery( "#firstp" ).off( "click.a.test" );
|
2008-12-19 04:34:12 +00:00
|
|
|
|
|
|
|
// Trigger both bound fn (1)
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#firstp" ).trigger( "click.test.abc" );
|
2008-12-19 04:34:12 +00:00
|
|
|
|
|
|
|
// Trigger one bound fn (1)
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#firstp" ).trigger( "click.abc" );
|
2008-12-19 04:34:12 +00:00
|
|
|
|
|
|
|
// Trigger two bound fn (2)
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#firstp" ).trigger( "click.test" );
|
2008-12-19 04:34:12 +00:00
|
|
|
|
|
|
|
// Remove only the one fn
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#firstp" ).off( "click.abc" );
|
2008-12-19 04:34:12 +00:00
|
|
|
|
|
|
|
// Trigger the remaining fn (1)
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#firstp" ).trigger( "click" );
|
2008-12-19 04:34:12 +00:00
|
|
|
|
|
|
|
// Remove the remaining fn
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#firstp" ).off( ".test" );
|
2008-12-19 04:34:12 +00:00
|
|
|
|
|
|
|
// Trigger the remaining fn (1)
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#firstp" ).trigger( "custom" );
|
|
|
|
} );
|
2008-12-19 04:34:12 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "namespace-only event binding is a no-op", function( assert ) {
|
|
|
|
assert.expect( 2 );
|
2013-02-26 17:19:04 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#firstp" )
|
2013-02-26 17:19:04 +00:00
|
|
|
.on( ".whoops", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( false, "called a namespace-only event" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
2013-02-26 17:19:04 +00:00
|
|
|
.on( "whoops", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "called whoops" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
|
|
|
.trigger( "whoops" ) // 1
|
|
|
|
.off( ".whoops" )
|
|
|
|
.trigger( "whoops" ) // 2
|
|
|
|
.off( "whoops" );
|
|
|
|
} );
|
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "Empty namespace is ignored", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2014-12-10 00:59:56 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#firstp" )
|
2014-12-10 00:59:56 +00:00
|
|
|
.on( "meow.", function( e ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( e.namespace, "", "triggered a namespace-less meow event" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
|
|
|
.trigger( "meow." )
|
|
|
|
.off( "meow." );
|
|
|
|
} );
|
2014-12-10 00:59:56 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "on(), with same function", function( assert ) {
|
|
|
|
assert.expect( 2 );
|
2010-02-04 05:49:46 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
var count = 0, func = function() {
|
2010-02-04 05:49:46 +00:00
|
|
|
count++;
|
|
|
|
};
|
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#liveHandlerOrder" ).on( "foo.bar", func ).on( "foo.zar", func );
|
|
|
|
jQuery( "#liveHandlerOrder" ).trigger( "foo.bar" );
|
2010-02-04 05:49:46 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( count, 1, "Verify binding function with multiple namespaces." );
|
2010-02-04 05:49:46 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#liveHandlerOrder" ).off( "foo.bar", func ).off( "foo.zar", func );
|
|
|
|
jQuery( "#liveHandlerOrder" ).trigger( "foo.bar" );
|
2010-02-04 05:49:46 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( count, 1, "Verify that removing events still work." );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-02-04 14:23:50 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "on(), make sure order is maintained", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2010-02-04 14:23:50 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
var elem = jQuery( "#firstp" ), log = [], check = [];
|
2010-02-04 14:23:50 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery.each( new Array( 100 ), function( i ) {
|
|
|
|
elem.on( "click", function() {
|
2010-02-04 14:23:50 +00:00
|
|
|
log.push( i );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-02-04 14:23:50 +00:00
|
|
|
|
|
|
|
check.push( i );
|
2012-06-21 19:30:24 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-02-04 14:23:50 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
elem.trigger( "click" );
|
2010-02-04 14:23:50 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( log.join( "," ), check.join( "," ), "Make sure order was maintained." );
|
2010-02-04 14:23:50 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
elem.off( "click" );
|
|
|
|
} );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "on(), with different this object", function( assert ) {
|
|
|
|
assert.expect( 4 );
|
2009-05-07 00:50:28 +00:00
|
|
|
var thisObject = { myThis: true },
|
|
|
|
data = { myData: true },
|
2013-04-09 15:45:09 +00:00
|
|
|
handler1 = function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( this, thisObject, "on() with different this object" );
|
2009-05-07 00:50:28 +00:00
|
|
|
},
|
|
|
|
handler2 = function( event ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( this, thisObject, "on() with different this object and data" );
|
|
|
|
assert.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
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#firstp" )
|
|
|
|
.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
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( !jQuery._data( jQuery( "#firstp" )[ 0 ], "events" ), "Event handler unbound when using different this object and data." );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2009-05-07 00:50:28 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "on(name, false), off(name, false)", function( assert ) {
|
|
|
|
assert.expect( 3 );
|
2010-02-27 14:02:13 +00:00
|
|
|
|
|
|
|
var main = 0;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#qunit-fixture" ).on( "click", function() { main++; } );
|
|
|
|
jQuery( "#ap" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( main, 1, "Verify that the trigger happened correctly." );
|
2010-02-27 14:02:13 +00:00
|
|
|
|
|
|
|
main = 0;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#ap" ).on( "click", false );
|
|
|
|
jQuery( "#ap" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( main, 0, "Verify that no bubble happened." );
|
2010-02-27 14:02:13 +00:00
|
|
|
|
|
|
|
main = 0;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#ap" ).off( "click", false );
|
|
|
|
jQuery( "#ap" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.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
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#qunit-fixture" ).off( "click" );
|
|
|
|
} );
|
2010-02-27 14:02:13 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "on(name, selector, false), off(name, selector, false)", function( assert ) {
|
|
|
|
assert.expect( 3 );
|
2011-01-01 18:49:59 +00:00
|
|
|
|
|
|
|
var main = 0;
|
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#qunit-fixture" ).on( "click", "#ap", function() { main++; } );
|
|
|
|
jQuery( "#ap" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( main, 1, "Verify that the trigger happened correctly." );
|
2011-01-01 18:49:59 +00:00
|
|
|
|
|
|
|
main = 0;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#ap" ).on( "click", "#groups", false );
|
|
|
|
jQuery( "#groups" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( main, 0, "Verify that no bubble happened." );
|
2011-01-01 18:49:59 +00:00
|
|
|
|
|
|
|
main = 0;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#ap" ).off( "click", "#groups", false );
|
|
|
|
jQuery( "#groups" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( main, 1, "Verify that the trigger happened correctly." );
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#qunit-fixture" ).off( "click", "#ap" );
|
|
|
|
} );
|
2011-01-01 18:49:59 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "on()/trigger()/off() on plain object", function( assert ) {
|
|
|
|
assert.expect( 7 );
|
2010-02-26 16:32:12 +00:00
|
|
|
|
2013-04-09 15:45:09 +00:00
|
|
|
var events,
|
|
|
|
obj = {};
|
2010-02-26 16:32:12 +00:00
|
|
|
|
|
|
|
// Make sure it doesn't complain when no events are found
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( obj ).trigger( "test" );
|
2010-02-26 16:32:12 +00:00
|
|
|
|
|
|
|
// Make sure it doesn't complain when no events are found
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( obj ).off( "test" );
|
2010-02-26 16:32:12 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( obj ).on( {
|
2012-07-05 19:52:13 +00:00
|
|
|
"test": function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "Custom event run." );
|
2010-11-21 03:31:04 +00:00
|
|
|
},
|
2012-07-05 19:52:13 +00:00
|
|
|
"submit": function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "Custom submit event run." );
|
2010-11-21 03:31:04 +00:00
|
|
|
}
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-02-26 16:32:12 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
events = jQuery._data( obj, "events" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( events, "Object has events bound." );
|
|
|
|
assert.equal( obj[ "events" ], undefined, "Events object on plain objects is not events" );
|
|
|
|
assert.equal( obj[ "test" ], undefined, "Make sure that test event is not on the plain object." );
|
|
|
|
assert.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
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( obj ).trigger( "test" );
|
|
|
|
jQuery( obj ).trigger( "submit" );
|
2010-02-26 16:32:12 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( obj ).off( "test" );
|
|
|
|
jQuery( obj ).off( "submit" );
|
2010-02-26 16:32:12 +00:00
|
|
|
|
|
|
|
// Should trigger 0
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( obj ).trigger( "test" );
|
2010-02-26 16:32:12 +00:00
|
|
|
|
|
|
|
// Make sure it doesn't complain when no events are found
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( obj ).off( "test" );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( obj && obj[ jQuery.expando ] &&
|
2011-02-10 02:15:32 +00:00
|
|
|
obj[ jQuery.expando ][ jQuery.expando ] &&
|
2015-09-02 23:52:01 +00:00
|
|
|
obj[ jQuery.expando ][ jQuery.expando ][ "events" ], undefined, "Make sure events object is removed" );
|
|
|
|
} );
|
2010-02-26 16:32:12 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "off(type)", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2013-04-09 15:45:09 +00:00
|
|
|
var message, func,
|
2015-09-02 23:52:01 +00:00
|
|
|
$elem = jQuery( "#firstp" );
|
2009-01-08 22:22:33 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
function error() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( false, message );
|
2009-01-08 22:22:33 +00:00
|
|
|
}
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2009-01-08 22:22:33 +00:00
|
|
|
message = "unbind passing function";
|
2015-09-02 23:52:01 +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";
|
2015-09-02 23:52:01 +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";
|
2015-09-02 23:52:01 +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";
|
2015-09-02 23:52:01 +00:00
|
|
|
$elem.on( "error1 error2", error )
|
|
|
|
.off( "error1 error2", error )
|
|
|
|
.trigger( "error1" ).triggerHandler( "error2" );
|
2009-01-08 22:22:33 +00:00
|
|
|
|
|
|
|
message = "unbind many"; // #3538
|
2015-09-02 23:52:01 +00:00
|
|
|
$elem.on( "error1 error2", error )
|
|
|
|
.off( "error1 error2" )
|
|
|
|
.trigger( "error1" ).triggerHandler( "error2" );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2009-04-29 21:45:58 +00:00
|
|
|
message = "unbind without a type or handler";
|
2015-09-02 23:52:01 +00:00
|
|
|
$elem.on( "error1 error2.test", error )
|
2013-03-04 02:22:11 +00:00
|
|
|
.off()
|
2015-09-02 23:52:01 +00:00
|
|
|
.trigger( "error1" ).triggerHandler( "error2" );
|
2011-10-12 00:30:07 +00:00
|
|
|
|
|
|
|
// Should only unbind the specified function
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( document ).on( "click", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "called handler after selective removal" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2013-04-09 15:45:09 +00:00
|
|
|
func = function() {};
|
2011-10-12 00:30:07 +00:00
|
|
|
jQuery( document )
|
2013-03-04 02:22:11 +00:00
|
|
|
.on( "click", func )
|
|
|
|
.off( "click", func )
|
2015-09-02 23:52:01 +00:00
|
|
|
.trigger( "click" )
|
2013-03-04 02:22:11 +00:00
|
|
|
.off( "click" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2009-01-08 22:22:33 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "off(eventObject)", function( assert ) {
|
|
|
|
assert.expect( 4 );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
var $elem = jQuery( "#firstp" ),
|
2009-01-08 22:22:33 +00:00
|
|
|
num;
|
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
function check( expected ) {
|
2009-01-08 22:22:33 +00:00
|
|
|
num = 0;
|
2015-09-02 23:52:01 +00:00
|
|
|
$elem.trigger( "foo" ).triggerHandler( "bar" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.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
|
2015-09-02 23:52:01 +00:00
|
|
|
|
2009-01-08 22:22:33 +00:00
|
|
|
// This handler shouldn't be unbound
|
2015-09-02 23:52:01 +00:00
|
|
|
.on( "foo", function() {
|
2009-01-08 22:22:33 +00:00
|
|
|
num += 1;
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
|
|
|
.on( "foo", function( e ) {
|
2013-03-04 02:22:11 +00:00
|
|
|
$elem.off( e );
|
2009-01-08 22:22:33 +00:00
|
|
|
num += 2;
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
|
|
|
|
2009-01-08 22:22:33 +00:00
|
|
|
// Neither this one
|
2015-09-02 23:52:01 +00:00
|
|
|
.on( "bar", function() {
|
2009-01-08 22:22:33 +00:00
|
|
|
num += 4;
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
check( 7 );
|
|
|
|
check( 5 );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
$elem.off( "bar" );
|
2015-09-03 01:46:31 +00:00
|
|
|
check( 1 );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2013-03-04 02:22:11 +00:00
|
|
|
$elem.off();
|
2015-09-03 01:46:31 +00:00
|
|
|
check( 0 );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2009-01-08 22:22:33 +00:00
|
|
|
|
2013-01-27 04:48:59 +00:00
|
|
|
if ( jQuery.fn.hover ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "hover() mouseenter mouseleave", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2011-10-27 21:11:40 +00:00
|
|
|
|
2013-01-27 04:48:59 +00:00
|
|
|
var times = 0,
|
2013-04-09 15:45:09 +00:00
|
|
|
handler1 = function() { ++times; },
|
|
|
|
handler2 = function() { ++times; };
|
2009-05-06 02:17:24 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#firstp" )
|
|
|
|
.hover( handler1, handler2 )
|
2013-01-27 04:48:59 +00:00
|
|
|
.mouseenter().mouseleave()
|
2015-09-02 23:52:01 +00:00
|
|
|
.off( "mouseenter", handler1 )
|
|
|
|
.off( "mouseleave", handler2 )
|
|
|
|
.hover( handler1 )
|
2013-01-27 04:48:59 +00:00
|
|
|
.mouseenter().mouseleave()
|
2015-09-02 23:52:01 +00:00
|
|
|
.off( "mouseenter mouseleave", handler1 )
|
2013-01-27 04:48:59 +00:00
|
|
|
.mouseenter().mouseleave();
|
2009-05-06 02:17:24 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( times, 4, "hover handlers fired" );
|
2011-10-27 21:11:40 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2013-01-27 04:48:59 +00:00
|
|
|
}
|
2009-05-06 02:17:24 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "mouseover triggers mouseenter", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2011-03-31 15:37:48 +00:00
|
|
|
|
2011-03-10 03:38:26 +00:00
|
|
|
var count = 0,
|
2015-09-02 23:52:01 +00:00
|
|
|
elem = jQuery( "<a />" );
|
|
|
|
elem.on( "mouseenter", function() {
|
2012-06-21 19:30:24 +00:00
|
|
|
count++;
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
elem.trigger( "mouseover" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.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();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-03-10 03:38:26 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "pointerover triggers pointerenter", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2014-03-16 14:46:57 +00:00
|
|
|
|
|
|
|
var count = 0,
|
2015-09-02 23:52:01 +00:00
|
|
|
elem = jQuery( "<a />" );
|
|
|
|
elem.on( "pointerenter", function() {
|
2014-03-16 14:46:57 +00:00
|
|
|
count++;
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
elem.trigger( "pointerover" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( count, 1, "make sure pointerover triggers a pointerenter" );
|
2014-03-16 14:46:57 +00:00
|
|
|
|
|
|
|
elem.remove();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2014-03-16 14:46:57 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "withinElement implemented with jQuery.contains()", function( assert ) {
|
2011-06-14 19:38:46 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.expect( 1 );
|
2011-06-14 19:38:46 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#qunit-fixture" ).append( "<div id='jc-outer'><div id='jc-inner'></div></div>" );
|
2011-06-14 19:38:46 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#jc-outer" ).on( "mouseenter mouseleave", function( event ) {
|
2011-06-14 19:38:46 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( this.id, "jc-outer", this.id + " " + event.type );
|
2011-06-14 19:38:46 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
} ).trigger( "mouseenter" );
|
2011-06-14 19:38:46 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#jc-inner" ).trigger( "mousenter" );
|
2011-06-14 19:38:46 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#jc-outer" ).off( "mouseenter mouseleave" ).remove();
|
|
|
|
jQuery( "#jc-inner" ).remove();
|
2011-06-14 19:38:46 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-06-14 19:38:46 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "mouseenter, mouseleave don't catch exceptions", function( assert ) {
|
|
|
|
assert.expect( 2 );
|
2011-06-14 19:38:46 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
var elem = jQuery( "#firstp" ).on( "mouseenter mouseleave", function() {
|
2013-01-27 04:48:59 +00:00
|
|
|
throw "an Exception";
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-06-14 19:38:46 +00:00
|
|
|
|
|
|
|
try {
|
2015-09-02 23:52:01 +00:00
|
|
|
elem.trigger( "mouseenter" );
|
|
|
|
} catch ( e ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( e, "an Exception", "mouseenter doesn't catch exceptions" );
|
2011-06-14 19:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2015-09-02 23:52:01 +00:00
|
|
|
elem.trigger( "mouseleave" );
|
|
|
|
} catch ( e ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( e, "an Exception", "mouseleave doesn't catch exceptions" );
|
2011-06-14 19:38:46 +00:00
|
|
|
}
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
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
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "trigger() shortcuts", function( assert ) {
|
|
|
|
assert.expect( 5 );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2013-04-09 15:45:09 +00:00
|
|
|
var counter, clickCounter,
|
2015-09-02 23:52:01 +00:00
|
|
|
elem = jQuery( "<li><a href='#'>Change location</a></li>" ).prependTo( "#firstUL" );
|
|
|
|
elem.find( "a" ).on( "click", function() {
|
|
|
|
var close = jQuery( "spanx", this ); // same with jQuery(this).find("span");
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( close.length, 0, "Context element does not exist, length must be zero" );
|
|
|
|
assert.ok( !close[ 0 ], "Context element does not exist, direct access to element must return undefined" );
|
2013-01-27 04:48:59 +00:00
|
|
|
return false;
|
2015-09-02 23:52:01 +00:00
|
|
|
} ).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
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#check1" ).click( function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "click event handler for checkbox gets fired twice, see #815" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} ).click();
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2013-04-09 15:45:09 +00:00
|
|
|
counter = 0;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#firstp" )[ 0 ].onclick = function() {
|
2013-01-27 04:48:59 +00:00
|
|
|
counter++;
|
|
|
|
};
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#firstp" ).click();
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( counter, 1, "Check that click, triggers onclick event handler also" );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2013-04-09 15:45:09 +00:00
|
|
|
clickCounter = 0;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#simon1" )[ 0 ].onclick = function() {
|
2013-01-27 04:48:59 +00:00
|
|
|
clickCounter++;
|
|
|
|
};
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#simon1" ).click();
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.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
|
|
|
// test that special handlers do not blow up with VML elements (#7071)
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "<xml:namespace ns='urn:schemas-microsoft-com:vml' prefix='v' />" ).appendTo( "head" );
|
|
|
|
jQuery( "<v:oval id='oval' style='width:100pt;height:75pt;' fillcolor='red'> </v:oval>" ).appendTo( "#form" );
|
|
|
|
jQuery( "#oval" ).click().keydown();
|
|
|
|
} );
|
2013-01-27 04:48:59 +00:00
|
|
|
|
|
|
|
}
|
2006-12-28 11:37:07 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "trigger() bubbling", function( assert ) {
|
|
|
|
assert.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
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( window ).on( "click", function() { win++; } );
|
|
|
|
jQuery( document ).on( "click", function( e ) { if ( e.target !== document ) { doc++; } } );
|
|
|
|
jQuery( "html" ).on( "click", function() { html++; } );
|
|
|
|
jQuery( "body" ).on( "click", function() { body++; } );
|
|
|
|
jQuery( "#qunit-fixture" ).on( "click", function() { main++; } );
|
|
|
|
jQuery( "#ap" ).on( "click", function() { ap++; return false; } );
|
2008-12-22 01:57:06 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "html" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( win, 1, "HTML bubble" );
|
|
|
|
assert.equal( doc, 1, "HTML bubble" );
|
|
|
|
assert.equal( html, 1, "HTML bubble" );
|
2008-12-22 01:57:06 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "body" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( win, 2, "Body bubble" );
|
|
|
|
assert.equal( doc, 2, "Body bubble" );
|
|
|
|
assert.equal( html, 2, "Body bubble" );
|
|
|
|
assert.equal( body, 1, "Body bubble" );
|
2008-12-22 01:57:06 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#qunit-fixture" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( win, 3, "Main bubble" );
|
|
|
|
assert.equal( doc, 3, "Main bubble" );
|
|
|
|
assert.equal( html, 3, "Main bubble" );
|
|
|
|
assert.equal( body, 2, "Main bubble" );
|
|
|
|
assert.equal( main, 1, "Main bubble" );
|
2008-12-22 01:57:06 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#ap" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( doc, 3, "ap bubble" );
|
|
|
|
assert.equal( html, 3, "ap bubble" );
|
|
|
|
assert.equal( body, 2, "ap bubble" );
|
|
|
|
assert.equal( main, 1, "ap bubble" );
|
|
|
|
assert.equal( ap, 1, "ap bubble" );
|
2011-01-09 21:58:47 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( document ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.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
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( window ).off( "click" );
|
|
|
|
jQuery( document ).off( "click" );
|
|
|
|
jQuery( "html, body, #qunit-fixture" ).off( "click" );
|
|
|
|
} );
|
2008-12-22 01:57:06 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "trigger(type, [data], [fn])", function( assert ) {
|
|
|
|
assert.expect( 16 );
|
2007-08-30 05:51:11 +00:00
|
|
|
|
2013-04-09 15:45:09 +00:00
|
|
|
var $elem, pass, form, elem2,
|
2015-09-02 23:52:01 +00:00
|
|
|
handler = function( event, a, b, c ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( event.type, "click", "check passed data" );
|
|
|
|
assert.equal( a, 1, "check passed data" );
|
|
|
|
assert.equal( b, "2", "check passed data" );
|
|
|
|
assert.equal( c, "abc", "check passed data" );
|
2007-08-30 05:51:11 +00:00
|
|
|
return "test";
|
|
|
|
};
|
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
$elem = jQuery( "#firstp" );
|
2007-08-30 16:34:34 +00:00
|
|
|
|
2007-08-30 05:51:11 +00:00
|
|
|
// Simulate a "native" click
|
2015-09-02 23:52:01 +00:00
|
|
|
$elem[ 0 ].click = function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "Native call was triggered" );
|
2007-04-24 21:48:52 +00:00
|
|
|
};
|
2007-08-30 05:51:11 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( document ).on( "mouseenter", "#firstp", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "Trigger mouseenter bound by on" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-04-17 18:58:20 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( document ).on( "mouseleave", "#firstp", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "Trigger mouseleave bound by on" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-04-17 18:58:20 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
$elem.trigger( "mouseenter" );
|
2011-04-17 18:58:20 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
$elem.trigger( "mouseleave" );
|
2011-04-17 18:58:20 +00:00
|
|
|
|
2015-09-02 23:52:01 +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
|
2015-09-02 23:52:01 +00:00
|
|
|
$elem.on( "click", handler ).trigger( "click", [ 1, "2", "abc" ] );
|
2007-08-30 05:51:11 +00:00
|
|
|
|
|
|
|
// Simulate a "native" click
|
2015-09-02 23:52:01 +00:00
|
|
|
$elem[ 0 ].click = function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( false, "Native call was triggered" );
|
2007-08-30 05:51:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Trigger only the handlers (no native)
|
2007-08-30 16:34:34 +00:00
|
|
|
// Triggers 5
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( $elem.triggerHandler( "click", [ 1, "2", "abc" ] ), "test", "Verify handler response" );
|
2007-08-30 05:51:11 +00:00
|
|
|
|
2013-04-09 15:45:09 +00:00
|
|
|
pass = true;
|
2007-12-08 02:54:09 +00:00
|
|
|
try {
|
2015-09-02 23:52:01 +00:00
|
|
|
elem2 = jQuery( "#form input" ).eq( 0 );
|
|
|
|
elem2.get( 0 ).style.display = "none";
|
|
|
|
elem2.trigger( "focus" );
|
|
|
|
} catch ( e ) {
|
2007-12-08 02:54:09 +00:00
|
|
|
pass = false;
|
|
|
|
}
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.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 {
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#qunit-fixture table" ).eq( 0 ).on( "test:test", function() {} ).trigger( "test:test" );
|
2013-04-09 15:45:09 +00:00
|
|
|
} catch ( e ) {
|
2009-06-17 02:31:45 +00:00
|
|
|
pass = false;
|
|
|
|
}
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( pass, "Trigger on a table with a colon in the even type, see #3533" );
|
2010-01-25 18:45:07 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
form = jQuery( "<form action=''></form>" ).appendTo( "body" );
|
2010-01-25 18:45:07 +00:00
|
|
|
|
|
|
|
// Make sure it can be prevented locally
|
2015-09-02 23:52:01 +00:00
|
|
|
form.on( "submit", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "Local `on` still works." );
|
2010-01-25 18:45:07 +00:00
|
|
|
return false;
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-01-25 18:45:07 +00:00
|
|
|
|
|
|
|
// Trigger 1
|
2015-09-02 23:52:01 +00:00
|
|
|
form.trigger( "submit" );
|
2010-01-25 18:45:07 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
form.off( "submit" );
|
2010-01-25 18:45:07 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( document ).on( "submit", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "Make sure bubble works up to document." );
|
2010-01-25 18:45:07 +00:00
|
|
|
return false;
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-01-25 18:45:07 +00:00
|
|
|
|
|
|
|
// Trigger 1
|
2015-09-02 23:52:01 +00:00
|
|
|
form.trigger( "submit" );
|
2010-01-25 18:45:07 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( document ).off( "submit" );
|
2010-01-25 18:45:07 +00:00
|
|
|
|
|
|
|
form.remove();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-01-25 18:45:07 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "submit event bubbles on copied forms (#11649)", function( assert ) {
|
|
|
|
assert.expect( 3 );
|
2012-06-21 19:30:24 +00:00
|
|
|
|
2012-05-18 20:30:28 +00:00
|
|
|
var $formByClone, $formByHTML,
|
2015-09-02 23:52:01 +00:00
|
|
|
$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() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "Make sure submit event bubbles up." );
|
2012-05-18 20:30:28 +00:00
|
|
|
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
|
2015-09-02 23:52:01 +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()
|
2015-09-02 23:52:01 +00:00
|
|
|
$formByClone = $testForm.clone( true, true ).removeAttr( "id" );
|
|
|
|
$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
|
2015-09-02 23:52:01 +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 );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2012-05-18 20:30:28 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "change event bubbles on copied forms (#11796)", function( assert ) {
|
|
|
|
assert.expect( 3 );
|
2012-06-21 19:30:24 +00:00
|
|
|
|
2012-05-21 23:01:59 +00:00
|
|
|
var $formByClone, $formByHTML,
|
2015-09-02 23:52:01 +00:00
|
|
|
$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() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "Make sure change event bubbles up." );
|
2012-05-21 23:01:59 +00:00
|
|
|
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
|
2015-09-02 23:52:01 +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()
|
2015-09-02 23:52:01 +00:00
|
|
|
$formByClone = $form.clone( true, true ).removeAttr( "id" );
|
|
|
|
$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
|
2015-09-02 23:52:01 +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 );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2012-05-21 23:01:59 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "trigger(eventObject, [data], [fn])", function( assert ) {
|
|
|
|
assert.expect( 28 );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2013-04-09 15:45:09 +00:00
|
|
|
var event,
|
2015-09-02 23:52:01 +00:00
|
|
|
$parent = jQuery( "<div id='par' />" ).appendTo( "body" ),
|
|
|
|
$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";
|
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
event = jQuery.Event( "noNew" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( event !== window, "Instantiate jQuery.Event without the 'new' keyword" );
|
|
|
|
assert.equal( event.type, "noNew", "Verify its type" );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( event.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
|
|
|
|
assert.equal( event.isPropagationStopped(), false, "Verify isPropagationStopped" );
|
|
|
|
assert.equal( event.isImmediatePropagationStopped(), false, "Verify isImmediatePropagationStopped" );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2008-12-31 02:58:13 +00:00
|
|
|
event.preventDefault();
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
|
2008-12-31 02:58:13 +00:00
|
|
|
event.stopPropagation();
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
event.isPropagationStopped = function() { return false; };
|
2008-12-31 02:58:13 +00:00
|
|
|
event.stopImmediatePropagation();
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
|
|
|
|
assert.equal( event.isImmediatePropagationStopped(), true, "Verify isPropagationStopped" );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
$parent.on( "foo", function( e ) {
|
|
|
|
|
2008-12-31 02:58:13 +00:00
|
|
|
// Tries bubbling
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( e.type, "foo", "Verify event type when passed passing an event object" );
|
|
|
|
assert.equal( e.target.id, "child", "Verify event.target when passed passing an event object" );
|
|
|
|
assert.equal( e.currentTarget.id, "par", "Verify event.currentTarget when passed passing an event object" );
|
|
|
|
assert.equal( e.secret, "boo!", "Verify event object's custom attribute when passed passing an event object" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2008-12-31 02:58:13 +00:00
|
|
|
// test with an event object
|
2015-09-02 23:52:01 +00:00
|
|
|
event = new jQuery.Event( "foo" );
|
2011-04-12 08:47:46 +00:00
|
|
|
event.secret = "boo!";
|
2015-09-02 23:52:01 +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
|
2015-09-02 23:52:01 +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
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
function error() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( false, "This assertion shouldn't be reached" );
|
2008-12-31 02:58:13 +00:00
|
|
|
}
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
$parent.on( "foo", error );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
$child.on( "foo", function( e, a, b, c ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( arguments.length, 4, "Check arguments length" );
|
|
|
|
assert.equal( a, 1, "Check first custom argument" );
|
|
|
|
assert.equal( b, 2, "Check second custom argument" );
|
|
|
|
assert.equal( c, 3, "Check third custom argument" );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( e.isDefaultPrevented(), false, "Verify isDefaultPrevented" );
|
|
|
|
assert.equal( e.isPropagationStopped(), false, "Verify isPropagationStopped" );
|
|
|
|
assert.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";
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
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
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
event = new jQuery.Event( "foo" );
|
|
|
|
$child.trigger( event, [ 1,2,3 ] ).off();
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.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
|
2015-09-02 23:52:01 +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 );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( event.type, "zowie", "Verify its type" );
|
|
|
|
assert.equal( event.isPropagationStopped(), false, "propagation not stopped" );
|
|
|
|
assert.equal( event.isDefaultPrevented(), false, "default not prevented" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
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;
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( ".trigger() bubbling on disconnected elements (#10489)", function( assert ) {
|
|
|
|
assert.expect( 2 );
|
2011-10-13 20:30:40 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( window ).on( "click", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( false, "click fired on window" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-10-13 20:30:40 +00:00
|
|
|
|
|
|
|
jQuery( "<div><p>hi</p></div>" )
|
|
|
|
.on( "click", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "click fired on div" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
2011-10-13 20:30:40 +00:00
|
|
|
.find( "p" )
|
2011-11-11 02:53:07 +00:00
|
|
|
.on( "click", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "click fired on p" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
|
|
|
.trigger( "click" )
|
2011-10-13 20:30:40 +00:00
|
|
|
.off( "click" )
|
|
|
|
.end()
|
|
|
|
.off( "click" )
|
|
|
|
.remove();
|
|
|
|
|
|
|
|
jQuery( window ).off( "click" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-10-13 20:30:40 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( ".trigger() doesn't bubble load event (#10717)", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2011-11-09 00:32:25 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( window ).on( "load", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( false, "load fired on window" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-11-09 00:32:25 +00:00
|
|
|
|
|
|
|
// It's not an image, but as long as it fires load...
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "<img src='index.html' />" )
|
2011-11-09 00:32:25 +00:00
|
|
|
.appendTo( "body" )
|
|
|
|
.on( "load", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "load fired on img" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
2011-11-09 00:32:25 +00:00
|
|
|
.trigger( "load" )
|
|
|
|
.remove();
|
|
|
|
|
|
|
|
jQuery( window ).off( "load" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-11-09 00:32:25 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "Delegated events in SVG (#10791; #13180)", function( assert ) {
|
|
|
|
assert.expect( 2 );
|
2011-11-16 15:35:53 +00:00
|
|
|
|
2013-01-14 03:59:20 +00:00
|
|
|
var useElem, e,
|
2013-01-13 18:10:40 +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-13 18:10:40 +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-13 18:10:40 +00:00
|
|
|
"<use id='use' xlink:href='#ref'></use>" +
|
2012-10-16 14:17:14 +00:00
|
|
|
"</svg>"
|
2013-01-13 18:10:40 +00:00
|
|
|
);
|
2012-01-13 01:30:45 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#qunit-fixture" )
|
2013-01-13 18:10:40 +00:00
|
|
|
.append( svg )
|
2011-11-16 15:35:53 +00:00
|
|
|
.on( "click", "#svg-by-id", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "delegated id selector" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
2012-07-11 21:33:53 +00:00
|
|
|
.on( "click", "[class~='svg-by-class']", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "delegated class selector" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
2012-07-11 21:33:53 +00:00
|
|
|
.find( "#svg-by-id, [class~='svg-by-class']" )
|
2015-09-02 23:52:01 +00:00
|
|
|
.trigger( "click" )
|
2013-01-13 18:10:40 +00:00
|
|
|
.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)
|
2015-09-02 23:52:01 +00:00
|
|
|
useElem = svg.find( "#use" )[ 0 ];
|
2013-01-14 03:59:20 +00:00
|
|
|
if ( document.createEvent && useElem && useElem.instanceRoot ) {
|
2015-09-02 23:52:01 +00:00
|
|
|
e = document.createEvent( "MouseEvents" );
|
2013-01-13 18:10:40 +00:00
|
|
|
e.initEvent( "click", true, true );
|
2013-01-14 03:59:20 +00:00
|
|
|
useElem.instanceRoot.dispatchEvent( e );
|
2013-01-13 18:10:40 +00:00
|
|
|
}
|
2011-11-16 15:35:53 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#qunit-fixture" ).off( "click" );
|
|
|
|
} );
|
2011-11-16 15:35:53 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "Delegated events in forms (#10844; #11145; #8165; #11382, #11764)", function( assert ) {
|
|
|
|
assert.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();
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
|
|
|
.appendTo( "body" );
|
2012-01-12 02:56:18 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "body" )
|
2012-01-12 02:56:18 +00:00
|
|
|
.on( "submit", "#myform", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "delegated id selector with aliased id" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
|
|
|
.find( "#myform" )
|
|
|
|
.trigger( "submit" )
|
2012-01-12 02:56:18 +00:00
|
|
|
.end()
|
2015-09-02 23:52:01 +00:00
|
|
|
.off( "submit" );
|
2012-01-12 02:56:18 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
form.append( "<input type='text' name='disabled' value='differently abled' />" );
|
|
|
|
jQuery( "body" )
|
2011-11-21 16:33:21 +00:00
|
|
|
.on( "submit", "#myform", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "delegated id selector with aliased disabled" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
|
|
|
.find( "#myform" )
|
|
|
|
.trigger( "submit" )
|
2011-11-21 16:33:21 +00:00
|
|
|
.end()
|
2015-09-02 23:52:01 +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() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "click on enabled/disabled button with nesty elements" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
2012-06-27 01:38:30 +00:00
|
|
|
.on( "mouseover", "#nestyDisabledBtn", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "mouse on enabled/disabled button with nesty elements" );
|
2015-09-02 23:52:01 +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();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-11-21 16:33:21 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "Submit event can be stopped (#11049)", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2012-03-05 02:11:50 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
)
|
2015-09-02 23:52:01 +00:00
|
|
|
.appendTo( "body" );
|
2012-03-05 02:11:50 +00:00
|
|
|
|
|
|
|
jQuery( "body" )
|
|
|
|
.on( "submit", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "submit bubbled on first handler" );
|
2012-03-05 02:11:50 +00:00
|
|
|
return false;
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
2012-03-05 02:11:50 +00:00
|
|
|
.find( "#myform input[type=submit]" )
|
2015-09-02 23:52:01 +00:00
|
|
|
.each( function() { this.click(); } )
|
2012-03-05 02:11:50 +00:00
|
|
|
.end()
|
|
|
|
.on( "submit", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( false, "submit bubbled on second handler" );
|
2012-03-05 02:11:50 +00:00
|
|
|
return false;
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
2012-03-05 02:11:50 +00:00
|
|
|
.find( "#myform input[type=submit]" )
|
2015-09-02 23:52:01 +00:00
|
|
|
.each( function() {
|
2012-03-05 02:11:50 +00:00
|
|
|
jQuery( this.form ).on( "submit", function( e ) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2012-03-05 02:11:50 +00:00
|
|
|
this.click();
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
2012-03-05 02:11:50 +00:00
|
|
|
.end()
|
|
|
|
.off( "submit" );
|
|
|
|
|
|
|
|
form.remove();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2012-03-05 02:11:50 +00:00
|
|
|
|
2014-11-03 18:53:22 +00:00
|
|
|
// Test beforeunload event only if it supported.
|
2014-02-14 00:01:43 +00:00
|
|
|
// Support: iOS 7+, Android<4.0
|
|
|
|
// iOS & old Android have the window.onbeforeunload field but don't support the beforeunload
|
2014-01-27 17:55:53 +00:00
|
|
|
// handler making it impossible to feature-detect the support.
|
2014-02-14 00:01:43 +00:00
|
|
|
if ( window.onbeforeunload === null &&
|
|
|
|
!/(ipad|iphone|ipod|android 2\.3)/i.test( navigator.userAgent ) ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.asyncTest( "on(beforeunload)", 4, function( assert ) {
|
2012-10-21 18:17:46 +00:00
|
|
|
var win,
|
|
|
|
fired = false,
|
2015-09-02 23:52:01 +00:00
|
|
|
iframe = jQuery( "<iframe src='data/iframe.html' />" );
|
2012-08-14 20:10:10 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
iframe.appendTo( "#qunit-fixture" ).one( "load", function() {
|
2012-10-21 18:17:46 +00:00
|
|
|
win = this.contentWindow || this.contentDocument;
|
2012-08-14 20:10:10 +00:00
|
|
|
|
2012-10-21 18:17:46 +00:00
|
|
|
jQuery( win ).on( "beforeunload", function() {
|
|
|
|
fired = true;
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "beforeunload event is fired" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2012-08-14 20:10:10 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.strictEqual( win.onbeforeunload, null, "onbeforeunload property on window object still equals null" );
|
2012-08-14 20:10:10 +00:00
|
|
|
|
2012-10-21 18:17:46 +00:00
|
|
|
win.onbeforeunload = function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "window.onbeforeunload handler is called" );
|
2015-09-02 23:52:01 +00:00
|
|
|
iframe = jQuery( "<iframe src='data/iframe.html' />" );
|
2012-06-18 02:42:24 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
iframe.appendTo( "#qunit-fixture" ).one( "load", function() {
|
2012-10-21 18:17:46 +00:00
|
|
|
win = iframe[ 0 ].contentWindow || iframe[ 0 ].contentDocument;
|
2012-06-18 02:42:24 +00:00
|
|
|
|
2012-10-21 18:17:46 +00:00
|
|
|
jQuery( win ).on( "beforeunload", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.strictEqual( win.onbeforeunload, null, "Event handler is fired, even when onbeforeunload property on window is nulled" );
|
2012-06-18 02:42:24 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.start();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2012-06-18 02:42:24 +00:00
|
|
|
|
2012-10-21 18:17:46 +00:00
|
|
|
jQuery( win ).on( "unload", function() {
|
|
|
|
if ( !fired ) {
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( win ).trigger( "beforeunload" );
|
2012-10-21 18:17:46 +00:00
|
|
|
}
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2012-10-21 18:17:46 +00:00
|
|
|
|
|
|
|
win.onbeforeunload = null;
|
2012-08-14 20:10:10 +00:00
|
|
|
|
2012-10-21 18:17:46 +00:00
|
|
|
win.location.reload();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2014-11-03 18:53:22 +00:00
|
|
|
};
|
2012-08-14 20:10:10 +00:00
|
|
|
|
2012-10-21 18:17:46 +00:00
|
|
|
win.location.reload();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
} );
|
2012-08-14 20:10:10 +00:00
|
|
|
}
|
2012-06-18 02:42:24 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "jQuery.Event( type, props )", function( assert ) {
|
2011-04-05 19:55:40 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.expect( 6 );
|
2011-04-05 19:55:40 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
var event = jQuery.Event( "keydown", { keyCode: 64 } ),
|
2011-04-05 19:55:40 +00:00
|
|
|
handler = function( event ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( "keyCode" in event, "Special property 'keyCode' exists" );
|
|
|
|
assert.equal( event.keyCode, 64, "event.keyCode has explicit value '64'" );
|
2011-04-05 19:55:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Supports jQuery.Event implementation
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.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
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( jQuery.inArray( "type", jQuery.event.props ), -1, "'type' property not in props (#10375)" );
|
2011-04-05 19:55:40 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( "keyCode" in event, "Special 'keyCode' property exists" );
|
2011-04-05 19:55:40 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.strictEqual( jQuery.isPlainObject( event ), false, "Instances of $.Event should not be identified as a plain object." );
|
2014-05-15 16:26:20 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "body" ).on( "keydown", handler ).trigger( event );
|
2011-04-05 19:55:40 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "body" ).off( "keydown" );
|
2011-04-05 19:55:40 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-04-05 19:55:40 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "jQuery.Event properties", function( assert ) {
|
|
|
|
assert.expect( 12 );
|
2011-11-11 02:53:07 +00:00
|
|
|
|
2013-04-04 23:26:08 +00:00
|
|
|
var handler, event,
|
2015-09-02 23:52:01 +00:00
|
|
|
$structure = jQuery( "<div id='ancestor'><p id='delegate'><span id='target'>shiny</span></p></div>" ),
|
|
|
|
$target = $structure.find( "#target" );
|
2013-02-02 02:20:15 +00:00
|
|
|
|
|
|
|
handler = function( e ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.strictEqual( e.currentTarget, this, "currentTarget at " + this.id );
|
|
|
|
assert.equal( e.isTrigger, 3, "trigger at " + this.id );
|
2013-02-02 02:20:15 +00:00
|
|
|
};
|
|
|
|
$structure.one( "click", handler );
|
|
|
|
$structure.one( "click", "p", handler );
|
|
|
|
$target.one( "click", handler );
|
2015-09-02 23:52:01 +00:00
|
|
|
$target[ 0 ].onclick = function( e ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.strictEqual( e.currentTarget, this, "currentTarget at target (native handler)" );
|
|
|
|
assert.equal( e.isTrigger, 3, "trigger at target (native handler)" );
|
2013-02-02 02:20:15 +00:00
|
|
|
};
|
2015-09-02 23:52:01 +00:00
|
|
|
$target.trigger( "click" );
|
2013-02-02 02:20:15 +00:00
|
|
|
|
|
|
|
$target.one( "click", function( e ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( e.isTrigger, 2, "triggerHandler at target" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
$target[ 0 ].onclick = function( e ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( e.isTrigger, 2, "triggerHandler at target (native handler)" );
|
2013-02-02 02:20:15 +00:00
|
|
|
};
|
2015-09-02 23:52:01 +00:00
|
|
|
$target.triggerHandler( "click" );
|
2013-02-02 02:20:15 +00:00
|
|
|
|
|
|
|
handler = function( e ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.strictEqual( e.isTrigger, undefined, "native event at " + this.id );
|
2013-04-04 23:26:08 +00:00
|
|
|
event = e;
|
2013-02-02 02:20:15 +00:00
|
|
|
};
|
|
|
|
$target.one( "click", handler );
|
2015-09-02 23:52:01 +00:00
|
|
|
$target[ 0 ].onclick = function( e ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.strictEqual( e.isTrigger, undefined, "native event at target (native handler)" );
|
2015-09-02 23:52:01 +00:00
|
|
|
$target[ 0 ].onclick = null;
|
2013-02-02 02:20:15 +00:00
|
|
|
};
|
2015-09-02 23:52:01 +00:00
|
|
|
fireNative( $target[ 0 ], "click" );
|
2013-04-04 23:26:08 +00:00
|
|
|
|
|
|
|
// Make sure that even oldIE executes the inline handler
|
2015-09-02 23:52:01 +00:00
|
|
|
if ( $target[ 0 ].onclick ) {
|
|
|
|
$target[ 0 ].onclick( event );
|
2013-04-04 23:26:08 +00:00
|
|
|
}
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2009-02-17 12:38:16 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( ".on()/.off()", function( assert ) {
|
|
|
|
assert.expect( 65 );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2013-04-09 15:45:09 +00:00
|
|
|
var event, clicked, hash, called, livec, lived, livee,
|
|
|
|
submit = 0, div = 0, livea = 0, liveb = 0;
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-09-02 23:52:01 +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
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "body" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( submit, 0, "Click on body" );
|
|
|
|
assert.equal( div, 0, "Click on body" );
|
|
|
|
assert.equal( livea, 0, "Click on body" );
|
|
|
|
assert.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;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "div#nothiddendiv" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( submit, 0, "Click on div" );
|
|
|
|
assert.equal( div, 1, "Click on div" );
|
|
|
|
assert.equal( livea, 1, "Click on div" );
|
|
|
|
assert.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;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "div#nothiddendivchild" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( submit, 0, "Click on inner div" );
|
|
|
|
assert.equal( div, 2, "Click on inner div" );
|
|
|
|
assert.equal( livea, 1, "Click on inner div" );
|
|
|
|
assert.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;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "div#nothiddendivchild" ).trigger( "submit" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( submit, 1, "Submit on div" );
|
|
|
|
assert.equal( div, 0, "Submit on div" );
|
|
|
|
assert.equal( livea, 0, "Submit on div" );
|
|
|
|
assert.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;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "div#nothiddendivchild" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( submit, 0, "off Click on inner div" );
|
|
|
|
assert.equal( div, 2, "off Click on inner div" );
|
|
|
|
assert.equal( livea, 1, "off Click on inner div" );
|
|
|
|
assert.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;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#body" ).off( "click", "div#nothiddendivchild" );
|
|
|
|
jQuery( "div#nothiddendivchild" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( submit, 0, "off Click on inner div" );
|
|
|
|
assert.equal( div, 2, "off Click on inner div" );
|
|
|
|
assert.equal( livea, 1, "off Click on inner div" );
|
|
|
|
assert.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;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "div#nothiddendiv" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( submit, 0, "off Click on inner div" );
|
|
|
|
assert.equal( div, 1, "off Click on inner div" );
|
|
|
|
assert.equal( livea, 1, "off Click on inner div" );
|
|
|
|
assert.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;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#body" ).on( "click", "div#nothiddendivchild", function( e ) { liveb++; e.stopPropagation(); } );
|
|
|
|
jQuery( "div#nothiddendivchild" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( submit, 0, "stopPropagation Click on inner div" );
|
|
|
|
assert.equal( div, 1, "stopPropagation Click on inner div" );
|
|
|
|
assert.equal( livea, 0, "stopPropagation Click on inner div" );
|
|
|
|
assert.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;
|
2015-09-02 23:52:01 +00:00
|
|
|
event = jQuery.Event( "click" );
|
2010-02-01 23:06:03 +00:00
|
|
|
event.button = 1;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "div#nothiddendiv" ).trigger( event );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( livea, 0, "on secondary click" );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-09-02 23:52:01 +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-04-09 15:45:09 +00:00
|
|
|
clicked = 0;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#qunit-fixture" ).on( "click", "#foo", function() { clicked++; } );
|
|
|
|
jQuery( "#qunit-fixture div" ).trigger( "click" );
|
|
|
|
jQuery( "#foo" ).trigger( "click" );
|
|
|
|
jQuery( "#qunit-fixture" ).trigger( "click" );
|
|
|
|
jQuery( "body" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( clicked, 2, "on with a context" );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
|
|
|
// Test unbinding with a different context
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#qunit-fixture" ).off( "click", "#foo" );
|
|
|
|
jQuery( "#foo" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( clicked, 2, "off with a context" );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
|
|
|
// Test binding with event data
|
2015-09-08 00:26:29 +00:00
|
|
|
jQuery( "#body" ).on( "click", "#foo", true, function( e ) {
|
|
|
|
assert.equal( e.data, true, "on with event data" );
|
|
|
|
} );
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#foo" ).trigger( "click" );
|
|
|
|
jQuery( "#body" ).off( "click", "#foo" );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
|
|
|
// Test binding with trigger data
|
2015-09-08 00:26:29 +00:00
|
|
|
jQuery( "#body" ).on( "click", "#foo", function( e, data ) {
|
|
|
|
assert.equal( data, true, "on with trigger data" );
|
|
|
|
} );
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#foo" ).trigger( "click", true );
|
|
|
|
jQuery( "#body" ).off( "click", "#foo" );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
|
|
|
// Test binding with different this object
|
2015-09-08 00:26:29 +00:00
|
|
|
jQuery( "#body" ).on( "click", "#foo", jQuery.proxy( function() {
|
|
|
|
assert.equal( this[ "foo" ], "bar", "on with event scope" ); }, { "foo": "bar" }
|
|
|
|
) );
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#foo" ).trigger( "click" );
|
|
|
|
jQuery( "#body" ).off( "click", "#foo" );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
|
|
|
// Test binding with different this object, event data, and trigger data
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#body" ).on( "click", "#foo", true, jQuery.proxy( function( e, data ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( e.data, true, "on with with different this object, event data, and trigger data" );
|
|
|
|
assert.equal( this.foo, "bar", "on with with different this object, event data, and trigger data" );
|
|
|
|
assert.equal( data, true, "on with with different this object, event data, and trigger data" );
|
2015-09-02 23:52:01 +00:00
|
|
|
}, { "foo": "bar" } ) );
|
|
|
|
jQuery( "#foo" ).trigger( "click", true );
|
|
|
|
jQuery( "#body" ).off( "click", "#foo" );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
|
|
|
// Verify that return false prevents default action
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#body" ).on( "click", "#anchor2", function() { return false; } );
|
2013-04-09 15:45:09 +00:00
|
|
|
hash = window.location.hash;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#anchor2" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( window.location.hash, hash, "return false worked" );
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#body" ).off( "click", "#anchor2" );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
|
|
|
// Verify that .preventDefault() prevents default action
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#body" ).on( "click", "#anchor2", function( e ) { e.preventDefault(); } );
|
2012-06-21 19:30:24 +00:00
|
|
|
hash = window.location.hash;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#anchor2" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( window.location.hash, hash, "e.preventDefault() worked" );
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#body" ).off( "click", "#anchor2" );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
|
|
|
// Test binding the same handler to multiple points
|
2013-04-09 15:45:09 +00:00
|
|
|
called = 0;
|
2015-09-02 23:52:01 +00:00
|
|
|
function callback() { called++; return false; }
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#body" ).on( "click", "#nothiddendiv", callback );
|
|
|
|
jQuery( "#body" ).on( "click", "#anchor2", callback );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#nothiddendiv" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.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;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#anchor2" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.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
|
2015-09-02 23:52:01 +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;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#nothiddendiv" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.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;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#anchor2" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.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
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#body" ).on( "foo", "#nothiddendiv", callback );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
|
|
|
// Cleanup
|
2015-09-02 23:52:01 +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;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#nothiddendiv" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.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;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#nothiddendiv" ).trigger( "foo" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( called, 1, "Verify that one foo occurred." );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
|
|
|
// Cleanup
|
2015-09-02 23:52:01 +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-04-09 15:45:09 +00:00
|
|
|
livec = 0;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#nothiddendivchild" ).html( "<span></span>" );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#body" ).on( "click", "#nothiddendivchild", function() { jQuery( "#nothiddendivchild" ).html( "" ); } );
|
|
|
|
jQuery( "#body" ).on( "click", "#nothiddendivchild", function( e ) { if ( e.target ) {livec++;} } );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#nothiddendiv span" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( jQuery( "#nothiddendiv span" ).length, 0, "Verify that first handler occurred and modified the DOM." );
|
|
|
|
assert.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
|
2015-09-02 23:52:01 +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
|
2013-04-09 15:45:09 +00:00
|
|
|
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
|
2015-09-02 23:52:01 +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
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "span#liveSpan1 a" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( lived, 1, "Verify that only one first handler occurred." );
|
|
|
|
assert.equal( livee, 0, "Verify that second handler doesn't." );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
|
|
|
// and one pair in inverse
|
2015-09-02 23:52:01 +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;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "span#liveSpan2 a" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( lived, 1, "Verify that only one first handler occurred." );
|
|
|
|
assert.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
|
2015-09-02 23:52:01 +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
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#body" ).on( "click", "span#liveSpan1", function( e ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( this.id, "liveSpan1", "Check the this within a on handler" );
|
|
|
|
assert.equal( e.currentTarget.id, "liveSpan1", "Check the event.currentTarget within a on handler" );
|
|
|
|
assert.equal( e.delegateTarget, document.body, "Check the event.delegateTarget within a on handler" );
|
|
|
|
assert.equal( e.target.nodeName.toUpperCase(), "A", "Check the event.target within a on handler" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "span#liveSpan1 a" ).trigger( "click" );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#body" ).off( "click", "span#liveSpan1" );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
|
|
|
// Work with deep selectors
|
|
|
|
livee = 0;
|
|
|
|
|
2013-04-09 15:45:09 +00:00
|
|
|
function clickB() { livee++; }
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-09-02 23:52:01 +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
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( livee, 0, "No clicks, deep selector." );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
|
|
|
livee = 0;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#nothiddendivchild" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( livee, 2, "Click, deep selector." );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
|
|
|
livee = 0;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#nothiddendivchild" ).trigger( "mouseover" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( livee, 1, "Mouseover, deep selector." );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#body" ).off( "mouseover", "#nothiddendiv div" );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
|
|
|
livee = 0;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#nothiddendivchild" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( livee, 2, "Click, deep selector." );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
|
|
|
livee = 0;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#nothiddendivchild" ).trigger( "mouseover" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( livee, 0, "Mouseover, deep selector." );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#body" ).off( "click", "#nothiddendiv div", clickB );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
|
|
|
livee = 0;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#nothiddendivchild" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( livee, 1, "Click, deep selector." );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#body" ).off( "click", "#nothiddendiv div" );
|
|
|
|
} );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "jQuery.off using dispatched jQuery.Event", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2011-11-11 02:53:07 +00:00
|
|
|
|
2015-09-02 23:52:01 +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 ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( ++count, 1, "event called once before removal" );
|
2011-10-24 02:25:13 +00:00
|
|
|
jQuery().off( event );
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
|
|
|
.find( "a" ).trigger( "click" ).trigger( "click" ).end()
|
2011-10-24 02:25:13 +00:00
|
|
|
.remove();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-10-24 02:25:13 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "delegated event with delegateTarget-relative selector", function( assert ) {
|
|
|
|
assert.expect( 3 );
|
2015-09-02 23:52:01 +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" );
|
2013-02-19 04:52:29 +00:00
|
|
|
|
|
|
|
// Non-positional selector (#12383)
|
2015-09-02 23:52:01 +00:00
|
|
|
markup.find( "#ul0" )
|
2013-02-19 04:52:29 +00:00
|
|
|
.on( "click", "div li a", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( false, "div is ABOVE the delegation point!" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
2013-02-19 04:52:29 +00:00
|
|
|
.on( "click", "ul a", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( false, "ul IS the delegation point!" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
2013-02-19 04:52:29 +00:00
|
|
|
.on( "click", "li.test a", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "li.test is below the delegation point." );
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
|
|
|
.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)
|
2015-09-02 23:52:01 +00:00
|
|
|
markup.find( "ul" ).eq( 0 )
|
2012-07-23 16:48:29 +00:00
|
|
|
.on( "click", ">li>a", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( this.id === "a0", "child li was clicked" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
|
|
|
.find( "#ul0" )
|
2012-07-23 16:48:29 +00:00
|
|
|
.on( "click", "li:first>a", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( this.id === "a0_0", "first li under #u10 was clicked" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
2012-06-27 00:36:00 +00:00
|
|
|
.end()
|
2015-09-02 23:52:01 +00:00
|
|
|
.find( "a" ).trigger( "click" ).end()
|
|
|
|
.find( "#ul0" ).off();
|
2012-10-15 21:20:33 +00:00
|
|
|
|
2012-08-28 14:07:16 +00:00
|
|
|
markup.remove();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2012-06-27 00:36:00 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "delegated event with selector matching Object.prototype property (#13203)", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2013-01-14 02:38:40 +00:00
|
|
|
|
|
|
|
var matched = 0;
|
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#foo" ).on( "click", "toString", function() {
|
2013-01-14 02:38:40 +00:00
|
|
|
matched++;
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2013-01-14 02:38:40 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#anchor2" ).trigger( "click" );
|
2013-01-14 02:38:40 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( matched, 0, "Nothing matched 'toString'" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2013-01-14 02:38:40 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "delegated event with intermediate DOM manipulation (#13208)", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2013-01-16 05:14:57 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#foo" ).on( "click", "[id=sap]", function() {} );
|
|
|
|
jQuery( "#sap" ).on( "click", "[id=anchor2]", function() {
|
2013-01-16 05:14:57 +00:00
|
|
|
document.createDocumentFragment().appendChild( this.parentNode );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "Element removed" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
jQuery( "#anchor2" ).trigger( "click" );
|
|
|
|
} );
|
2013-01-16 05:14:57 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "stopPropagation() stops directly-bound events on delegated target", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2011-11-11 02:53:07 +00:00
|
|
|
|
2015-09-02 23:52:01 +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() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( false, "directly-bound event on delegate target was called" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
2011-10-21 14:56:16 +00:00
|
|
|
.on( "click", "a", function( e ) {
|
|
|
|
e.stopPropagation();
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "delegated handler was called" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
|
|
|
.find( "a" ).trigger( "click" ).end()
|
2011-10-21 14:56:16 +00:00
|
|
|
.remove();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-10-21 14:56:16 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "off all bound delegated events", function( assert ) {
|
|
|
|
assert.expect( 2 );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2012-08-08 00:49:34 +00:00
|
|
|
var count = 0,
|
|
|
|
clicks = 0,
|
2015-09-02 23:52:01 +00:00
|
|
|
div = jQuery( "#body" );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
div.on( "click submit", "div#nothiddendivchild", function() { count++; } );
|
|
|
|
div.on( "click", function() { clicks++; } );
|
2013-03-04 02:22:11 +00:00
|
|
|
div.off( undefined, "**" );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "div#nothiddendivchild" ).trigger( "click" );
|
|
|
|
jQuery( "div#nothiddendivchild" ).trigger( "submit" );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( count, 0, "Make sure no events were triggered." );
|
2012-08-08 00:49:34 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
div.trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( clicks, 2, "Make sure delegated and directly bound event occurred." );
|
2015-09-02 23:52:01 +00:00
|
|
|
div.off( "click" );
|
|
|
|
} );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "on with multiple delegated events", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2013-04-09 15:45:09 +00:00
|
|
|
var count = 0,
|
2015-09-02 23:52:01 +00:00
|
|
|
div = jQuery( "#body" );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
div.on( "click submit", "div#nothiddendivchild", function() { count++; } );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "div#nothiddendivchild" ).trigger( "click" );
|
|
|
|
jQuery( "div#nothiddendivchild" ).trigger( "submit" );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( count, 2, "Make sure both the click and submit were triggered." );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#body" ).off( undefined, "**" );
|
|
|
|
} );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "delegated on with change", function( assert ) {
|
|
|
|
assert.expect( 8 );
|
2010-10-15 02:37:56 +00:00
|
|
|
|
2013-04-09 15:45:09 +00:00
|
|
|
var select, checkbox, checkboxFunction,
|
|
|
|
text, textChange, oldTextVal,
|
|
|
|
password, passwordChange, oldPasswordVal,
|
|
|
|
selectChange = 0,
|
|
|
|
checkboxChange = 0;
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
select = jQuery( "select[name='S1']" );
|
|
|
|
jQuery( "#body" ).on( "change", "select[name='S1']", function() {
|
2010-02-01 23:06:03 +00:00
|
|
|
selectChange++;
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
checkbox = jQuery( "#check2" );
|
|
|
|
checkboxFunction = function() {
|
2013-04-09 15:45:09 +00:00
|
|
|
checkboxChange++;
|
|
|
|
};
|
2015-09-02 23:52:01 +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;
|
2015-09-02 23:52:01 +00:00
|
|
|
select[ 0 ].selectedIndex = select[ 0 ].selectedIndex ? 0 : 1;
|
|
|
|
select.trigger( "change" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.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;
|
2015-09-02 23:52:01 +00:00
|
|
|
select[ 0 ].selectedIndex = select[ 0 ].selectedIndex ? 0 : 1;
|
|
|
|
select.trigger( "change" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.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
|
2015-09-02 23:52:01 +00:00
|
|
|
checkbox.trigger( "change" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.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
|
2015-09-02 23:52:01 +00:00
|
|
|
text = jQuery( "#name" );
|
2013-04-09 15:45:09 +00:00
|
|
|
textChange = 0;
|
|
|
|
oldTextVal = text.val();
|
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#body" ).on( "change", "#name", function() {
|
2010-02-01 23:06:03 +00:00
|
|
|
textChange++;
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
text.val( oldTextVal + "foo" );
|
|
|
|
text.trigger( "change" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( textChange, 1, "Change on text input." );
|
2009-09-14 22:04:22 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
text.val( oldTextVal );
|
|
|
|
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
|
2015-09-02 23:52:01 +00:00
|
|
|
password = jQuery( "#name" );
|
2013-04-09 15:45:09 +00:00
|
|
|
passwordChange = 0;
|
|
|
|
oldPasswordVal = password.val();
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#body" ).on( "change", "#name", function() {
|
2010-02-01 23:06:03 +00:00
|
|
|
passwordChange++;
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
password.val( oldPasswordVal + "foo" );
|
|
|
|
password.trigger( "change" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( passwordChange, 1, "Change on password input." );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
password.val( oldPasswordVal );
|
|
|
|
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;
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#body" ).off( "change", "select[name='S1']" );
|
|
|
|
select[ 0 ].selectedIndex = select[ 0 ].selectedIndex ? 0 : 1;
|
|
|
|
select.trigger( "change" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( selectChange, 0, "Die on click works." );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
|
|
|
selectChange = 0;
|
2015-09-02 23:52:01 +00:00
|
|
|
select[ 0 ].selectedIndex = select[ 0 ].selectedIndex ? 0 : 1;
|
|
|
|
select.trigger( "change" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.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
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#body" ).off( "change", "#check2", checkboxFunction );
|
|
|
|
checkbox.trigger( "change" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( checkboxChange, 1, "Die on checkbox." );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "delegated on with submit", function( assert ) {
|
|
|
|
assert.expect( 2 );
|
2012-10-15 16:16:49 +00:00
|
|
|
|
2010-02-01 23:06:03 +00:00
|
|
|
var count1 = 0, count2 = 0;
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#body" ).on( "submit", "#testForm", function( ev ) {
|
2010-02-01 23:06:03 +00:00
|
|
|
count1++;
|
|
|
|
ev.preventDefault();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( document ).on( "submit", "body", function( ev ) {
|
2010-02-01 23:06:03 +00:00
|
|
|
count2++;
|
|
|
|
ev.preventDefault();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#testForm input[name=sub1]" ).trigger( "submit" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( count1, 1, "Verify form submit." );
|
|
|
|
assert.equal( count2, 1, "Verify body submit." );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "#body" ).off( undefined, "**" );
|
|
|
|
jQuery( document ).off( undefined, "**" );
|
|
|
|
} );
|
2009-09-14 22:04:22 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "delegated off() with only namespaces", function( assert ) {
|
|
|
|
assert.expect( 2 );
|
2011-04-05 22:12:50 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
var $delegate = jQuery( "#liveHandlerOrder" ),
|
2011-04-14 03:10:32 +00:00
|
|
|
count = 0;
|
2011-04-05 22:12:50 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
$delegate.on( "click.ns", "a", function() {
|
2011-04-05 22:12:50 +00:00
|
|
|
count++;
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-04-05 22:12:50 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "a", $delegate ).eq( 0 ).trigger( "click.ns" );
|
2011-04-05 22:12:50 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( count, 1, "delegated click.ns" );
|
2011-04-05 22:12:50 +00:00
|
|
|
|
2013-03-04 02:22:11 +00:00
|
|
|
$delegate.off( ".ns", "**" );
|
2011-04-05 22:12:50 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "a", $delegate ).eq( 1 ).trigger( "click.ns" );
|
2011-04-05 22:12:50 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( count, 1, "no more .ns after off" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-04-05 22:12:50 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "Non DOM element events", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2009-05-04 04:54:09 +00:00
|
|
|
|
|
|
|
var o = {};
|
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( o ).on( "nonelementobj", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "Event on non-DOM object triggered" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2009-05-04 04:54:09 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( o ).trigger( "nonelementobj" ).off( "nonelementobj" );
|
|
|
|
} );
|
2009-05-04 04:54:09 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "inline handler returning false stops default", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2011-11-11 02:53:07 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
var markup = jQuery( "<div><a href=\"#\" onclick=\"return false\">x</a></div>" );
|
|
|
|
markup.on( "click", function( e ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( e.isDefaultPrevented(), "inline handler prevented default" );
|
2011-11-07 16:07:36 +00:00
|
|
|
return false;
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
markup.find( "a" ).trigger( "click" );
|
|
|
|
markup.off( "click" );
|
|
|
|
} );
|
2011-11-07 16:07:36 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "window resize", function( assert ) {
|
|
|
|
assert.expect( 2 );
|
2010-10-11 22:20:57 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( window ).off();
|
2010-10-11 22:03:54 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( window ).on( "resize", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "Resize event fired." );
|
2015-09-02 23:52:01 +00:00
|
|
|
} ).trigger( "resize" ).off( "resize" );
|
2010-10-11 22:20:57 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( !jQuery._data( window, "events" ), "Make sure all the events are gone." );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2010-10-15 01:40:35 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "focusin bubbles", function( assert ) {
|
|
|
|
assert.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
|
2015-09-02 23:52:01 +00:00
|
|
|
input[ 0 ].focus();
|
2011-11-14 02:32:07 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "body" ).on( "focusin.focusinBubblesTest", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( 1, order++, "focusin on the body second" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-03-05 02:16:40 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
input.on( "focusin.focusinBubblesTest", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( 0, order++, "focusin on the element first" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
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" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-03-05 02:16:40 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "custom events with colons (#3533, #8272)", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2011-04-08 02:52:15 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
var tab = jQuery( "<table><tr><td>trigger</td></tr></table>" ).appendTo( "body" );
|
2011-04-08 02:52:15 +00:00
|
|
|
try {
|
2015-09-02 23:52:01 +00:00
|
|
|
tab.trigger( "back:forth" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "colon events don't throw" );
|
2011-04-08 02:52:15 +00:00
|
|
|
} catch ( e ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( false, "colon events die" );
|
2012-06-21 19:30:24 +00:00
|
|
|
}
|
2011-04-08 02:52:15 +00:00
|
|
|
tab.remove();
|
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-03-31 15:37:48 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( ".on and .off", function( assert ) {
|
|
|
|
assert.expect( 9 );
|
2013-04-09 15:45:09 +00:00
|
|
|
var counter, mixfn, data,
|
2015-09-02 23:52:01 +00:00
|
|
|
$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() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "whipped it good" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
2011-07-29 00:43:23 +00:00
|
|
|
.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 ) {
|
2015-09-02 23:52:01 +00:00
|
|
|
counter += e.data + ( trig || 9 ); // twice, 5+9+5+17=36
|
|
|
|
} )
|
2011-07-29 00:43:23 +00:00
|
|
|
.one( "click", 7, function( e, trig ) {
|
2015-09-02 23:52:01 +00:00
|
|
|
counter += e.data + ( trig || 11 ); // once, 7+11=18
|
|
|
|
} )
|
|
|
|
.trigger( "click" )
|
2011-07-29 00:43:23 +00:00
|
|
|
.trigger( "click", 17 )
|
|
|
|
.off( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.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 ) {
|
2015-09-02 23:52:01 +00:00
|
|
|
counter += e.data + ( trig || 9 ); // twice, 5+9+5+17=36
|
|
|
|
} )
|
2011-07-29 00:43:23 +00:00
|
|
|
.one( "click", "em", 7, function( e, trig ) {
|
2015-09-02 23:52:01 +00:00
|
|
|
counter += e.data + ( trig || 11 ); // once, 7+11=18
|
|
|
|
} )
|
|
|
|
.find( "em" )
|
|
|
|
.trigger( "click" )
|
2011-07-29 00:43:23 +00:00
|
|
|
.trigger( "click", 17 )
|
|
|
|
.end()
|
|
|
|
.off( "click", "em" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( counter, 54, "delegated event bindings with data" );
|
2011-07-29 00:43:23 +00:00
|
|
|
|
|
|
|
// Mixed event bindings and types
|
|
|
|
counter = 0;
|
2015-09-02 23:52:01 +00:00
|
|
|
mixfn = function( e, trig ) {
|
|
|
|
counter += ( e.data || 0 ) + ( trig || 1 );
|
2011-07-29 00:43:23 +00:00
|
|
|
};
|
|
|
|
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() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( counter, 0, "nothing triggered yet" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
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() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( counter, 49, "after triggering em element" );
|
2015-09-02 23:52:01 +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() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( counter, 51, "after triggering #onandoff cluck" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
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() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( counter, 51, "after triggering b" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
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() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( counter, 54, "final triggers" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
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
|
2015-09-02 23:52:01 +00:00
|
|
|
data = jQuery.data[ jQuery( "#onandoff" )[ 0 ].expando ] || {};
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.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();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-07-29 00:43:23 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "special on name mapping", function( assert ) {
|
|
|
|
assert.expect( 7 );
|
2011-11-11 02:53:07 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery.event.special[ "slap" ] = {
|
2011-11-11 02:53:07 +00:00
|
|
|
bindType: "click",
|
|
|
|
delegateType: "swing",
|
|
|
|
handle: function( event ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( event.handleObj.origType, "slap", "slapped your mammy, " + event.type );
|
2011-11-11 02:53:07 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var comeback = function( event ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "event " + event.type + " triggered" );
|
2011-11-11 02:53:07 +00:00
|
|
|
};
|
|
|
|
|
2015-09-02 23:52:01 +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();
|
2015-09-02 23:52:01 +00:00
|
|
|
delete jQuery.event.special[ "slap" ];
|
2011-11-14 01:50:36 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery.event.special[ "gutfeeling" ] = {
|
2011-11-14 01:50:36 +00:00
|
|
|
bindType: "click",
|
|
|
|
delegateType: "click",
|
|
|
|
handle: function( event ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( event.handleObj.origType, "gutfeeling", "got a gutfeeling" );
|
2015-09-02 23:52:01 +00:00
|
|
|
|
2011-12-14 02:40:59 +00:00
|
|
|
// Need to call the handler since .one() uses it to unbind
|
2015-09-02 23:52:01 +00:00
|
|
|
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();
|
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
delete jQuery.event.special[ "gutfeeling" ];
|
|
|
|
} );
|
2011-11-10 02:29:15 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( ".on and .off, selective mixed removal (#10705)", function( assert ) {
|
|
|
|
assert.expect( 7 );
|
2011-11-10 02:29:15 +00:00
|
|
|
|
2013-04-09 15:45:09 +00:00
|
|
|
var timingx = function( e ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "triggered " + e.type );
|
2013-04-09 15:45:09 +00:00
|
|
|
};
|
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
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-11-10 02:29:15 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( ".on( event-map, null-selector, data ) #11130", function( assert ) {
|
2012-01-13 01:30:45 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.expect( 1 );
|
2012-01-13 01:30:45 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
var $p = jQuery( "<p>Strange Pursuit</p>" ),
|
2012-01-13 01:30:45 +00:00
|
|
|
data = "bar",
|
|
|
|
map = {
|
|
|
|
"foo": function( event ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( event.data, "bar", "event.data correctly relayed with null selector" );
|
2012-01-13 01:30:45 +00:00
|
|
|
$p.remove();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
$p.on( map, null, data ).trigger( "foo" );
|
|
|
|
} );
|
2012-01-13 01:30:45 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "clone() delegated events (#11076)", function( assert ) {
|
|
|
|
assert.expect( 3 );
|
2012-01-28 19:58:00 +00:00
|
|
|
|
2012-07-05 19:52:13 +00:00
|
|
|
var counter = { "center": 0, "fold": 0, "centerfold": 0 },
|
2013-04-09 15:45:09 +00:00
|
|
|
clicked = function() {
|
2015-09-02 23:52:01 +00:00
|
|
|
counter[ jQuery( this ).text().replace( /\s+/, "" ) ]++;
|
2012-01-28 19:58:00 +00:00
|
|
|
},
|
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 );
|
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
clone.find( "td" ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( counter[ "center" ], 1, "first child" );
|
|
|
|
assert.equal( counter[ "fold" ], 1, "last child" );
|
|
|
|
assert.equal( counter[ "centerfold" ], 2, "all children" );
|
2012-01-28 19:58:00 +00:00
|
|
|
|
|
|
|
table.remove();
|
|
|
|
clone.remove();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2012-01-28 19:58:00 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "checkbox state (#3827)", function( assert ) {
|
|
|
|
assert.expect( 9 );
|
2012-11-21 03:31:33 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
var markup = jQuery( "<div><input type=checkbox><div>" ).appendTo( "#qunit-fixture" ),
|
|
|
|
cb = markup.find( "input" )[ 0 ];
|
2012-11-21 03:31:33 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( cb ).on( "click", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( this.checked, false, "just-clicked checkbox is not checked" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
|
|
|
markup.on( "click", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( cb.checked, false, "checkbox is not checked in bubbled event" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2012-11-21 03:31:33 +00:00
|
|
|
|
|
|
|
// Native click
|
|
|
|
cb.checked = true;
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( cb.checked, true, "native - checkbox is initially checked" );
|
2012-11-21 03:31:33 +00:00
|
|
|
cb.click();
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( cb.checked, false, "native - checkbox is no longer checked" );
|
2012-11-21 03:31:33 +00:00
|
|
|
|
|
|
|
// jQuery click
|
|
|
|
cb.checked = true;
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( cb.checked, true, "jQuery - checkbox is initially checked" );
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( cb ).trigger( "click" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( cb.checked, false, "jQuery - checkbox is no longer checked" );
|
2012-11-21 03:31:33 +00:00
|
|
|
|
|
|
|
// Handlers only; checkbox state remains false
|
|
|
|
jQuery( cb ).triggerHandler( "click" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2012-11-21 03:31:33 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "hover event no longer special since 1.9", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2012-11-24 20:25:54 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "<div>craft</div>" )
|
2012-11-24 20:25:54 +00:00
|
|
|
.on( "hover", function( e ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( e.type, "hover", "I am hovering!" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
|
|
|
.trigger( "hover" )
|
|
|
|
.off( "hover" );
|
|
|
|
} );
|
2012-11-24 20:25:54 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "fixHooks extensions", function( assert ) {
|
|
|
|
assert.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 ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( !( "blurrinessLevel" in event ), "event.blurrinessLevel does not exist" );
|
2015-09-02 23:52:01 +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 = {
|
2013-04-09 15:45:09 +00:00
|
|
|
filter: function( event ) {
|
2011-09-25 23:56:34 +00:00
|
|
|
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 ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( event.blurrinessLevel, 42, "event.blurrinessLevel was set" );
|
2015-09-02 23:52:01 +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;
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2011-07-29 00:43:23 +00:00
|
|
|
|
2014-12-20 23:41:24 +00:00
|
|
|
// IE8 doesn't support custom event triggering natively, but we can skip
|
|
|
|
// this test in IE8 since a native HTML5 drag event will never occur there.
|
|
|
|
if ( document.createEvent ) {
|
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "drag/drop events copy mouse-related event properties (gh-1925, gh-2009)", function( assert ) {
|
|
|
|
assert.expect( 4 );
|
2014-12-20 23:41:24 +00:00
|
|
|
|
|
|
|
var $fixture = jQuery( "<div id='drag-fixture'></div>" ).appendTo( "body" );
|
|
|
|
|
|
|
|
$fixture.on( "dragmove", function( evt ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( "pageX" in evt, "checking for pageX property on dragmove" );
|
|
|
|
assert.ok( "pageY" in evt, "checking for pageY property on dragmove" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2014-12-20 23:41:24 +00:00
|
|
|
fireNative( $fixture[ 0 ], "dragmove" );
|
2015-01-14 19:34:09 +00:00
|
|
|
|
|
|
|
$fixture.on( "drop", function( evt ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( "pageX" in evt, "checking for pageX property on drop" );
|
|
|
|
assert.ok( "pageY" in evt, "checking for pageY property on drop" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2015-01-14 19:34:09 +00:00
|
|
|
fireNative( $fixture[ 0 ], "drop" );
|
|
|
|
|
|
|
|
$fixture.unbind( "dragmove drop" ).remove();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2014-12-20 23:41:24 +00:00
|
|
|
}
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "focusin using non-element targets", function( assert ) {
|
|
|
|
assert.expect( 2 );
|
2013-11-14 21:33:12 +00:00
|
|
|
|
|
|
|
jQuery( document ).on( "focusin", function( e ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( e.type === "focusin", "got a focusin event on a document" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} ).trigger( "focusin" ).off( "focusin" );
|
2013-11-14 21:33:12 +00:00
|
|
|
|
|
|
|
jQuery( window ).on( "focusin", function( e ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( e.type === "focusin", "got a focusin event on a window" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} ).trigger( "focusin" ).off( "focusin" );
|
2013-11-14 21:33:12 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2013-11-14 21:33:12 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
testIframeWithCallback(
|
|
|
|
"focusin from an iframe",
|
|
|
|
"event/focusinCrossFrame.html",
|
|
|
|
function( frameDoc, assert ) {
|
|
|
|
assert.expect( 1 );
|
2013-09-17 22:51:54 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
var input = jQuery( frameDoc ).find( "#frame-input" );
|
2013-09-17 22:51:54 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
// Create a focusin handler on the parent; shouldn't affect the iframe's fate
|
2015-09-08 01:02:51 +00:00
|
|
|
jQuery( "body" ).on( "focusin.iframeTest", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( false, "fired a focusin event in the parent document" );
|
|
|
|
} );
|
2013-09-17 22:51:54 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
input.on( "focusin", function() {
|
|
|
|
assert.ok( true, "fired a focusin event in the iframe" );
|
|
|
|
} );
|
2013-09-17 22:51:54 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
// Avoid a native event; Chrome can't force focus to another frame
|
|
|
|
input.trigger( "focusin" );
|
2013-09-17 22:51:54 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
// Must manually remove handler to avoid leaks in our data store
|
|
|
|
input.remove();
|
2013-09-17 22:51:54 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
// Be sure it was removed; nothing should happen
|
|
|
|
input.trigger( "focusin" );
|
2013-09-17 22:51:54 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
// Remove body handler manually since it's outside the fixture
|
|
|
|
jQuery( "body" ).off( "focusin.iframeTest" );
|
|
|
|
}
|
|
|
|
);
|
2013-09-17 22:51:54 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
testIframeWithCallback(
|
|
|
|
"jQuery.ready promise",
|
|
|
|
"event/promiseReady.html",
|
|
|
|
function( isOk, assert ) {
|
|
|
|
assert.expect( 1 );
|
|
|
|
assert.ok( isOk, "$.when( $.ready ) works" );
|
|
|
|
}
|
|
|
|
);
|
2012-04-23 19:43:26 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
testIframeWithCallback(
|
|
|
|
"Focusing iframe element",
|
|
|
|
"event/focusElem.html",
|
|
|
|
function( isOk, assert ) {
|
|
|
|
assert.expect( 1 );
|
|
|
|
assert.ok( isOk, "Focused an element in an iframe" );
|
|
|
|
}
|
|
|
|
);
|
2013-04-09 01:33:25 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
testIframeWithCallback(
|
|
|
|
"triggerHandler(onbeforeunload)",
|
|
|
|
"event/triggerunload.html",
|
|
|
|
function( isOk, assert ) {
|
|
|
|
assert.expect( 1 );
|
|
|
|
assert.ok( isOk, "Triggered onbeforeunload without an error" );
|
|
|
|
}
|
|
|
|
);
|
2014-03-04 03:43:51 +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 ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
testIframeWithCallback(
|
|
|
|
"jQuery.ready synchronous load with long loading subresources",
|
|
|
|
"event/syncReady.html",
|
|
|
|
function( isOk, assert ) {
|
|
|
|
assert.expect( 1 );
|
|
|
|
assert.ok(
|
|
|
|
isOk,
|
|
|
|
"jQuery loaded synchronously fires ready when the DOM can truly be interacted with"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2012-04-23 19:43:26 +00:00
|
|
|
}
|
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "change handler should be detached from element", function( assert ) {
|
|
|
|
assert.expect( 2 );
|
2012-08-20 00:19:44 +00:00
|
|
|
|
2013-04-09 15:45:09 +00:00
|
|
|
var $fixture = jQuery( "<input type='text' id='change-ie-leak' />" ).appendTo( "body" ),
|
|
|
|
originRemoveEvent = jQuery.removeEvent,
|
2015-09-02 23:52:01 +00:00
|
|
|
wrapperRemoveEvent = function( elem, type, handle ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( "change", type, "Event handler for 'change' event should be removed" );
|
|
|
|
assert.equal( "change-ie-leak", jQuery( elem ).attr( "id" ), "Event handler for 'change' event should be removed from appropriate element" );
|
2015-09-02 23:52:01 +00:00
|
|
|
originRemoveEvent( elem, type, handle );
|
2013-04-09 15:45:09 +00:00
|
|
|
};
|
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
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
$fixture.on( "change", function() {} );
|
2013-03-04 02:22:11 +00:00
|
|
|
$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;
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2012-08-20 00:19:44 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.asyncTest( "trigger click on checkbox, fires change event", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2012-04-11 01:54:07 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
var check = jQuery( "#check2" );
|
2011-04-05 19:55:40 +00:00
|
|
|
|
2012-04-11 01:54:07 +00:00
|
|
|
check.on( "change", function() {
|
2015-09-02 23:52:01 +00:00
|
|
|
|
2012-04-11 01:54:07 +00:00
|
|
|
// get it?
|
2015-09-02 23:52:01 +00:00
|
|
|
check.off( "change" );
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "Change event fired as a result of triggered click" );
|
|
|
|
QUnit.start();
|
2015-09-02 23:52:01 +00:00
|
|
|
} ).trigger( "click" );
|
|
|
|
} );
|
2012-10-16 00:22:19 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "Namespace preserved when passed an Event (#12739)", function( assert ) {
|
|
|
|
assert.expect( 4 );
|
2012-10-16 00:22:19 +00:00
|
|
|
|
|
|
|
var markup = jQuery(
|
|
|
|
"<div id='parent'><div id='child'></div></div>"
|
|
|
|
),
|
|
|
|
triggered = 0,
|
|
|
|
fooEvent;
|
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
markup.find( "div" )
|
2012-10-16 00:22:19 +00:00
|
|
|
.addBack()
|
|
|
|
.on( "foo.bar", function( e ) {
|
|
|
|
if ( !e.handled ) {
|
|
|
|
triggered++;
|
|
|
|
e.handled = true;
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( e.namespace, "bar", "namespace is bar" );
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( e.target ).find( "div" ).each( function() {
|
2012-10-16 00:22:19 +00:00
|
|
|
jQuery( this ).triggerHandler( e );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2012-10-16 00:22:19 +00:00
|
|
|
}
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
2013-04-09 15:45:09 +00:00
|
|
|
.on( "foo.bar2", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( false, "foo.bar2 called on trigger " + triggered + " id " + this.id );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2012-10-16 00:22:19 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
markup.trigger( "foo.bar" );
|
|
|
|
markup.trigger( jQuery.Event( "foo.bar" ) );
|
|
|
|
fooEvent = jQuery.Event( "foo" );
|
2012-10-16 00:22:19 +00:00
|
|
|
fooEvent.namespace = "bar";
|
|
|
|
markup.trigger( fooEvent );
|
|
|
|
markup.remove();
|
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( triggered, 3, "foo.bar triggered" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2012-10-16 00:22:19 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "make sure events cloned correctly", function( assert ) {
|
|
|
|
assert.expect( 18 );
|
2015-07-30 17:48:26 +00:00
|
|
|
|
2012-11-18 18:53:24 +00:00
|
|
|
var clone,
|
2015-09-02 23:52:01 +00:00
|
|
|
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 ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( result, event.type + " on original element is fired" );
|
2012-11-18 18:53:24 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
} ).on( "click", "#firstp", function( event, result ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( result, "Click on original child element though delegation is fired" );
|
2012-11-18 18:53:24 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
} ).on( "change", "#check1", function( event, result ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( result, "Change on original child element though delegation is fired" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2012-11-18 18:53:24 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
p.on( "click", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "Click on original child element is fired" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2012-11-18 18:53:24 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
checkbox.on( "change", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( true, "Change on original child element is fired" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2012-11-18 18:53:24 +00:00
|
|
|
|
2015-09-02 23:52:01 +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 );
|
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
clone.find( "p" ).eq( 0 ).trigger( "click", true ); // 3 events should fire
|
|
|
|
clone.find( "#check1" ).trigger( "change", true ); // 3 events should fire
|
2012-11-18 18:53:24 +00:00
|
|
|
clone.remove();
|
|
|
|
|
|
|
|
clone = fixture.clone( true, true );
|
2015-09-02 23:52:01 +00:00
|
|
|
clone.find( "p" ).eq( 0 ).trigger( "click", true ); // 3 events should fire
|
|
|
|
clone.find( "#check1" ).trigger( "change", true ); // 3 events should fire
|
2012-11-18 18:53:24 +00:00
|
|
|
|
|
|
|
fixture.off();
|
|
|
|
p.off();
|
|
|
|
checkbox.off();
|
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
p.trigger( "click" ); // 0 should be fired
|
|
|
|
checkbox.trigger( "change" ); // 0 should be fired
|
2012-11-18 18:53:24 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
clone.find( "p" ).eq( 0 ).trigger( "click", true ); // 3 events should fire
|
|
|
|
clone.find( "#check1" ).trigger( "change", true ); // 3 events should fire
|
2012-11-18 18:53:24 +00:00
|
|
|
clone.remove();
|
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
clone.find( "p" ).eq( 0 ).trigger( "click" ); // 0 should be fired
|
|
|
|
clone.find( "#check1" ).trigger( "change" ); // 0 events should fire
|
|
|
|
} );
|
2012-12-17 14:17:39 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "String.prototype.namespace does not cause trigger() to throw (#13360)", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2013-01-31 18:01:34 +00:00
|
|
|
var errored = false;
|
|
|
|
|
|
|
|
String.prototype.namespace = function() {};
|
|
|
|
|
|
|
|
try {
|
2015-09-02 23:52:01 +00:00
|
|
|
jQuery( "<p>" ).trigger( "foo.bar" );
|
|
|
|
} catch ( e ) {
|
2013-01-31 18:01:34 +00:00
|
|
|
errored = true;
|
|
|
|
}
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( errored, false, "trigger() did not throw exception" );
|
2013-01-31 18:01:34 +00:00
|
|
|
delete String.prototype.namespace;
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2013-09-19 15:34:48 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "Inline event result is returned (#13993)", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2013-09-19 15:34:48 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
var result = jQuery( "<p onclick='return 42'>hello</p>" ).triggerHandler( "click" );
|
2013-09-19 15:34:48 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( result, 42, "inline handler returned value" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2013-11-07 21:32:12 +00:00
|
|
|
|
2015-09-08 18:30:54 +00:00
|
|
|
QUnit.test( ".off() removes the expando when there's no more data", function( assert ) {
|
2015-09-14 18:22:19 +00:00
|
|
|
// Support: IE 8 only
|
|
|
|
// IE 8 gets the expando removed via removeAttribute so the second assertion
|
|
|
|
// won't be reached.
|
|
|
|
assert.expect( document.documentMode < 9 ? 1 : 2 );
|
2015-09-08 18:30:54 +00:00
|
|
|
|
|
|
|
var key,
|
|
|
|
div = jQuery( "<div/>" ).appendTo( "#qunit-fixture" );
|
|
|
|
|
|
|
|
div.on( "click", false );
|
|
|
|
div.on( "custom", function() {
|
|
|
|
assert.ok( true, "Custom event triggered" );
|
|
|
|
} );
|
|
|
|
div.trigger( "custom" );
|
|
|
|
div.off( "click custom" );
|
|
|
|
|
|
|
|
// Make sure the expando is gone
|
|
|
|
for ( key in div[ 0 ] ) {
|
|
|
|
if ( /^jQuery/.test( key ) ) {
|
|
|
|
assert.strictEqual(
|
|
|
|
div[ 0 ][ key ], undefined,
|
|
|
|
"Expando was not removed when there was no more data"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "preventDefault() on focusin does not throw exception", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2015-05-20 15:14:13 +00:00
|
|
|
|
|
|
|
var done = assert.async(),
|
|
|
|
input = jQuery( "<input/>" ).appendTo( "#form" );
|
|
|
|
|
|
|
|
input.on( "focusin", function( event ) {
|
|
|
|
var exceptionCaught;
|
|
|
|
|
|
|
|
try {
|
|
|
|
event.preventDefault();
|
|
|
|
} catch ( theException ) {
|
|
|
|
exceptionCaught = theException;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.strictEqual( exceptionCaught, undefined,
|
|
|
|
"Preventing default on focusin throws no exception" );
|
|
|
|
|
|
|
|
done();
|
|
|
|
} ).trigger( "focus" );
|
|
|
|
} );
|
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "Donor event interference", function( assert ) {
|
2015-05-20 15:14:13 +00:00
|
|
|
assert.expect( 10 );
|
|
|
|
|
|
|
|
var html = "<div id='donor-outer'>" +
|
|
|
|
"<form id='donor-form'>" +
|
|
|
|
"<input id='donor-input' type='radio' />" +
|
|
|
|
"</form>" +
|
|
|
|
"</div>";
|
|
|
|
|
|
|
|
jQuery( "#qunit-fixture" ).append( html );
|
|
|
|
|
|
|
|
jQuery( "#donor-outer" ).on( "click", function( event ) {
|
|
|
|
assert.ok( true, "click bubbled to outer div" );
|
|
|
|
assert.equal( typeof event.originalEvent, "object", "make sure originalEvent exist" );
|
|
|
|
assert.equal( event.type, "click", "make sure event type is correct" );
|
|
|
|
} );
|
|
|
|
jQuery( "#donor-input" ).on( "click", function( event ) {
|
|
|
|
assert.ok( true, "got a click event from the input" );
|
|
|
|
assert.ok( !event.isPropagationStopped(), "propagation says it's not stopped" );
|
|
|
|
assert.equal( event.type, "click", "make sure event type is correct" );
|
|
|
|
assert.equal( typeof event.originalEvent, "object", "make sure originalEvent exist" );
|
|
|
|
} );
|
|
|
|
jQuery( "#donor-input" ).on( "change", function( event ) {
|
|
|
|
assert.equal( typeof event.originalEvent, "object", "make sure originalEvent exist" );
|
|
|
|
assert.equal( event.type, "change", "make sure event type is correct" );
|
|
|
|
assert.ok( true, "got a change event from the input" );
|
|
|
|
event.stopPropagation();
|
|
|
|
} );
|
|
|
|
|
|
|
|
jQuery( "#donor-input" )[ 0 ].click();
|
|
|
|
} );
|
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "originalEvent property for IE8", function( assert ) {
|
2015-09-02 23:52:01 +00:00
|
|
|
if ( !( /msie 8\.0/i.test( window.navigator.userAgent ) ) ) {
|
2015-05-20 15:14:13 +00:00
|
|
|
assert.expect( 1 );
|
|
|
|
assert.ok( true, "Assertions should run only in IE" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.expect( 12 );
|
|
|
|
|
|
|
|
var html = "<div id='donor-outer'>" +
|
|
|
|
"<form id='donor-form'>" +
|
|
|
|
"<input id='donor-input' type='radio' />" +
|
|
|
|
"</form>" +
|
|
|
|
"</div>";
|
|
|
|
|
|
|
|
jQuery( "#qunit-fixture" ).append( html );
|
|
|
|
|
|
|
|
jQuery( "#donor-outer" ).on( "change", function( event ) {
|
|
|
|
assert.ok( true, "click bubbled to outer div" );
|
|
|
|
assert.equal( event.originalEvent.type, "click", "make sure simulated event is a click" );
|
|
|
|
assert.equal( event.type, "change", "make sure event type is correct" );
|
|
|
|
} );
|
|
|
|
|
|
|
|
jQuery( "#donor-outer" ).on( "click", function( event ) {
|
|
|
|
assert.ok( true, "click bubbled to outer div" );
|
|
|
|
assert.equal( event.originalEvent.type, "click", "make sure originalEvent exist" );
|
|
|
|
} );
|
|
|
|
jQuery( "#donor-input" ).on( "click", function( event ) {
|
|
|
|
assert.ok( true, "got a click event from the input" );
|
|
|
|
assert.ok( !event.isPropagationStopped(), "propagation says it's not stopped" );
|
|
|
|
assert.equal( event.originalEvent.type, "click", "make sure originalEvent exist" );
|
|
|
|
assert.equal( event.type, "click", "make sure event type is correct" );
|
|
|
|
} );
|
|
|
|
jQuery( "#donor-input" ).on( "change", function( event ) {
|
|
|
|
assert.equal( event.originalEvent.type, "click", "make sure originalEvent exist" );
|
|
|
|
assert.equal( event.type, "change", "make sure event type is correct" );
|
|
|
|
assert.ok( true, "got a change event from the input" );
|
|
|
|
} );
|
|
|
|
|
|
|
|
jQuery( "#donor-input" )[ 0 ].click();
|
|
|
|
} );
|
|
|
|
|
2015-09-14 19:14:23 +00:00
|
|
|
QUnit.test( "originalEvent property for Chrome, Safari, Fx & Edge of simulated event", function( assert ) {
|
2015-05-20 15:14:13 +00:00
|
|
|
var userAgent = window.navigator.userAgent;
|
|
|
|
|
2015-09-14 19:14:23 +00:00
|
|
|
if ( !( /firefox/i.test( userAgent ) || /safari/i.test( userAgent ) ) ) {
|
2015-05-20 15:14:13 +00:00
|
|
|
assert.expect( 1 );
|
2015-09-14 19:14:23 +00:00
|
|
|
assert.ok( true, "Assertions should run only in Chrome, Safari, Fx & Edge" );
|
2015-05-20 15:14:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.expect( 4 );
|
|
|
|
|
|
|
|
var html = "<div id='donor-outer'>" +
|
|
|
|
"<form id='donor-form'>" +
|
|
|
|
"<input id='donor-input' type='radio' />" +
|
|
|
|
"</form>" +
|
|
|
|
"</div>";
|
|
|
|
|
|
|
|
jQuery( "#qunit-fixture" ).append( html );
|
|
|
|
|
|
|
|
jQuery( "#donor-outer" ).on( "focusin", function( event ) {
|
|
|
|
assert.ok( true, "focusin bubbled to outer div" );
|
|
|
|
assert.equal( event.originalEvent.type, "focus",
|
|
|
|
"make sure originalEvent type is correct" );
|
|
|
|
assert.equal( event.type, "focusin", "make sure type is correct" );
|
|
|
|
} );
|
|
|
|
jQuery( "#donor-input" ).on( "focus", function() {
|
|
|
|
assert.ok( true, "got a focus event from the input" );
|
|
|
|
} );
|
|
|
|
jQuery( "#donor-input" ).trigger( "focus" );
|
|
|
|
} );
|
|
|
|
|
2015-09-14 19:14:23 +00:00
|
|
|
// These tests are unreliable in Firefox
|
2015-09-02 23:52:01 +00:00
|
|
|
if ( !( /firefox/i.test( window.navigator.userAgent ) ) ) {
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "Check order of focusin/focusout events", function( assert ) {
|
|
|
|
assert.expect( 2 );
|
2015-07-30 17:48:26 +00:00
|
|
|
|
2013-11-07 21:32:12 +00:00
|
|
|
var focus, blur,
|
|
|
|
input = jQuery( "#name" );
|
|
|
|
|
|
|
|
input.on( "focus", function() {
|
|
|
|
focus = true;
|
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
} ).on( "focusin", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( !focus, "Focusin event should fire before focus does" );
|
2013-11-07 21:32:12 +00:00
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
} ).on( "blur", function() {
|
2013-11-07 21:32:12 +00:00
|
|
|
blur = true;
|
|
|
|
|
2015-09-02 23:52:01 +00:00
|
|
|
} ).on( "focusout", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.ok( !blur, "Focusout event should fire before blur does" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2013-11-07 21:32:12 +00:00
|
|
|
|
|
|
|
// gain focus
|
|
|
|
input.trigger( "focus" );
|
|
|
|
|
|
|
|
// then lose it
|
|
|
|
jQuery( "#search" ).trigger( "focus" );
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
input.off();
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2013-11-07 21:32:12 +00:00
|
|
|
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.test( "focus-blur order (#12868)", function( assert ) {
|
|
|
|
assert.expect( 5 );
|
2013-11-07 21:32:12 +00:00
|
|
|
|
|
|
|
var order,
|
2015-09-02 23:52:01 +00:00
|
|
|
$text = jQuery( "#text1" ),
|
|
|
|
$radio = jQuery( "#radio1" ).trigger( "focus" );
|
2013-11-07 21:32:12 +00:00
|
|
|
|
2014-11-03 18:53:22 +00:00
|
|
|
// Support: IE<11
|
|
|
|
// IE8-10 fire focus/blur events asynchronously; this is the resulting mess.
|
2013-11-07 21:32:12 +00:00
|
|
|
// IE's browser window must be topmost for this to work properly!!
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.stop();
|
2015-09-02 23:52:01 +00:00
|
|
|
$radio[ 0 ].focus();
|
2013-11-07 21:32:12 +00:00
|
|
|
|
|
|
|
setTimeout( function() {
|
|
|
|
|
|
|
|
$text
|
2015-09-02 23:52:01 +00:00
|
|
|
.on( "focus", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( order++, 1, "text focus" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
|
|
|
.on( "blur", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( order++, 0, "text blur" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2013-11-07 21:32:12 +00:00
|
|
|
$radio
|
2015-09-02 23:52:01 +00:00
|
|
|
.on( "focus", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( order++, 1, "radio focus" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} )
|
|
|
|
.on( "blur", function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( order++, 0, "radio blur" );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2013-11-07 21:32:12 +00:00
|
|
|
|
|
|
|
// Enabled input getting focus
|
|
|
|
order = 0;
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( document.activeElement, $radio[ 0 ], "radio has focus" );
|
2015-09-02 23:52:01 +00:00
|
|
|
$text.trigger( "focus" );
|
2013-11-07 21:32:12 +00:00
|
|
|
setTimeout( function() {
|
2015-09-03 01:46:31 +00:00
|
|
|
assert.equal( document.activeElement, $text[ 0 ], "text has focus" );
|
2013-11-07 21:32:12 +00:00
|
|
|
|
|
|
|
// Run handlers without native method on an input
|
|
|
|
order = 1;
|
|
|
|
$radio.triggerHandler( "focus" );
|
|
|
|
$text.off();
|
2015-09-03 01:46:31 +00:00
|
|
|
QUnit.start();
|
2013-11-07 21:32:12 +00:00
|
|
|
}, 50 );
|
|
|
|
}, 50 );
|
2015-09-02 23:52:01 +00:00
|
|
|
} );
|
2013-11-07 21:32:12 +00:00
|
|
|
}
|