2015-08-16 03:45:28 +00:00
|
|
|
QUnit.module( "event", {
|
2019-02-18 18:02:38 +00:00
|
|
|
beforeEach: function() {
|
2013-09-30 17:12:58 +00:00
|
|
|
document.body.focus();
|
|
|
|
},
|
2019-02-18 18:02:38 +00:00
|
|
|
afterEach: moduleTeardown
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2006-11-18 13:37:01 +00:00
|
|
|
|
2016-01-14 04:06:43 +00:00
|
|
|
QUnit.test( "null or undefined handler", function( assert ) {
|
|
|
|
assert.expect( 4 );
|
|
|
|
|
|
|
|
// Supports Fixes bug #7229
|
|
|
|
try {
|
|
|
|
jQuery( "#firstp" ).on( "click", null );
|
|
|
|
assert.ok( true, "Passing a null handler will not throw an exception" );
|
|
|
|
} catch ( e ) {}
|
|
|
|
|
|
|
|
try {
|
|
|
|
jQuery( "#firstp" ).on( "click", undefined );
|
|
|
|
assert.ok( true, "Passing an undefined handler will not throw an exception" );
|
|
|
|
} catch ( e ) {}
|
|
|
|
|
|
|
|
var expectedElem = jQuery( "#firstp" );
|
|
|
|
var actualElem = expectedElem.on( "click", null );
|
2016-05-10 09:12:28 +00:00
|
|
|
assert.equal( actualElem, expectedElem, "Passing a null handler should return the original element" );
|
2016-01-14 04:06:43 +00:00
|
|
|
|
|
|
|
actualElem = expectedElem.on( "click", undefined );
|
2016-05-10 09:12:28 +00:00
|
|
|
assert.equal( actualElem, expectedElem, "Passing a null handler should return the original element" );
|
2016-01-14 04:06:43 +00:00
|
|
|
} );
|
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
QUnit.test( "on() with non-null,defined data", function( assert ) {
|
2011-05-16 14:38:36 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
assert.expect( 2 );
|
2011-05-16 14:38:36 +00:00
|
|
|
|
|
|
|
var handler = function( event, data ) {
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "#foo" ).on( "foo.on", handler );
|
|
|
|
jQuery( "div" ).on( "foo.delegate", "#foo", handler );
|
2011-05-16 14:38:36 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#foo" ).trigger( "foo", 0 );
|
2011-05-16 14:38:36 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#foo" ).off( "foo.on", handler );
|
|
|
|
jQuery( "div" ).off( "foo.delegate", "#foo" );
|
2011-05-16 14:38:36 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-05-16 14:38:36 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
} )
|
2011-10-13 20:30:40 +00:00
|
|
|
.filter( "b" ).on( "click", function( e ) {
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2011-10-13 20:30:40 +00:00
|
|
|
// Removing span should not stop propagation to original parents
|
|
|
|
if ( e.target === this ) {
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( this ).parent().remove();
|
2011-10-13 20:30:40 +00:00
|
|
|
}
|
2015-08-16 06:59:58 +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-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-09-08 01:02:13 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
handler = function( event ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( event.data, "on() with data, check passed data exists" );
|
2016-05-10 09:12:28 +00:00
|
|
|
assert.equal( event.data.foo, "bar", "on() with data, Check value of passed data" );
|
2007-04-24 21:48:52 +00:00
|
|
|
};
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#firstp" ).on( "click", { "foo": "bar" }, handler ).trigger( "click" ).off( "click", handler );
|
2007-09-08 23:31:23 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
test = function() {};
|
|
|
|
handler2 = function( event ) {
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "#firstp" ).on( "click", test, handler2 ).trigger( "click" ).off( "click", handler2 );
|
|
|
|
} );
|
2010-02-27 01:01:19 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
QUnit.test( "click(), with data", function( assert ) {
|
|
|
|
assert.expect( 3 );
|
|
|
|
var handler = function( event ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( event.data, "on() with data, check passed data exists" );
|
2016-05-10 09:12:28 +00:00
|
|
|
assert.equal( event.data.foo, "bar", "on() with data, Check value of passed data" );
|
2010-02-27 01:01:19 +00:00
|
|
|
};
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#firstp" ).on( "click", { "foo": "bar" }, handler ).trigger( "click" ).off( "click", handler );
|
2010-02-27 01:01:19 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
assert.ok( !jQuery._data( jQuery( "#firstp" )[ 0 ], "events" ), "Event handler unbound when using data." );
|
|
|
|
} );
|
2008-01-14 09:33:08 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
QUnit.test( "on(), with data, trigger with data", function( assert ) {
|
|
|
|
assert.expect( 4 );
|
|
|
|
var handler = function( event, data ) {
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
handler = function( event ) {
|
|
|
|
if ( event.type === "click" ) {
|
2007-12-15 05:55:33 +00:00
|
|
|
clickCounter += 1;
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "#firstp" ).on( "click mouseover", handler ).trigger( "click" ).trigger( "mouseover" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( clickCounter, 1, "on() with multiple events at once" );
|
|
|
|
assert.equal( mouseoverCounter, 1, "on() with multiple events at once" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2008-01-14 09:33:08 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 03:45:28 +00:00
|
|
|
assert.equal( count, 5, "on() five events at once" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-04-29 01:14:12 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
div = jQuery( "<div/>" ).on( "focusin.a", function( e ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( e.type, cur, "Verify right single event was fired." );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2010-01-25 21:45:39 +00:00
|
|
|
|
|
|
|
cur = "focusin";
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
div = jQuery( "<div/>" ).on( "click mouseover", obj, function( e ) {
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
} );
|
2010-01-25 21:45:39 +00:00
|
|
|
|
|
|
|
cur = "click";
|
2015-08-16 06:59:58 +00:00
|
|
|
div.trigger( "click" );
|
2010-01-25 21:45:39 +00:00
|
|
|
|
|
|
|
cur = "mouseover";
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
div = jQuery( "<div/>" ).on( "focusin.a focusout.b", function( e ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( e.type, cur, "Verify right multi event was fired." );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2010-01-25 21:45:39 +00:00
|
|
|
|
|
|
|
cur = "focusin";
|
2015-08-16 06:59:58 +00:00
|
|
|
div.trigger( "focusin.a" );
|
2010-01-25 21:45:39 +00:00
|
|
|
|
|
|
|
cur = "focusout";
|
2015-08-16 06:59:58 +00:00
|
|
|
div.trigger( "focusout.b" );
|
2011-01-09 21:58:47 +00:00
|
|
|
|
|
|
|
// manually clean up detached elements
|
|
|
|
div.remove();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2010-01-25 21:45:39 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
div = jQuery( "<div/>" ).appendTo( "#qunit-fixture" ).on( "test", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "Test event fired." );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2010-01-25 22:01:07 +00:00
|
|
|
|
2016-05-10 09:12:28 +00:00
|
|
|
jQuery.event.special.test = {
|
2013-04-07 02:56:03 +00:00
|
|
|
_default: function( e, data ) {
|
2015-08-16 03:45:28 +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." );
|
2015-08-16 06:59:58 +00:00
|
|
|
assert.strictEqual( e.target, div[ 0 ], "And that the target is correct." );
|
2015-08-16 03:45:28 +00:00
|
|
|
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-08-16 03:45:28 +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-08-16 03:45:28 +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-08-16 03:45:28 +00:00
|
|
|
assert.ok( !!e.xyz, "Make sure that the data is getting passed through." );
|
2015-08-16 06:59:58 +00:00
|
|
|
assert.equal( e.data[ "x" ], 1, "Make sure data is attached properly." );
|
|
|
|
} );
|
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-08-16 03:45:28 +00:00
|
|
|
assert.ok( !!e.xyz, "Make sure that the data is getting passed through." );
|
2015-08-16 06:59:58 +00:00
|
|
|
assert.equal( e.data[ "x" ], 2, "Make sure data is attached properly." );
|
|
|
|
} );
|
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-08-16 06:59:58 +00:00
|
|
|
div.off( "test" );
|
2010-02-13 11:10:43 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
div = jQuery( "<div/>" ).on( "test", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "Test event fired." );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2010-02-13 11:10:43 +00:00
|
|
|
|
|
|
|
// Should trigger 2
|
2015-08-16 06:59:58 +00:00
|
|
|
div.appendTo( "#qunit-fixture" ).remove();
|
2010-02-13 11:10:43 +00:00
|
|
|
|
2016-05-10 09:12:28 +00:00
|
|
|
delete jQuery.event.special.test;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2010-01-25 22:01:07 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
QUnit.test( "on(), no data", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
|
|
|
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-08-16 06:59:58 +00:00
|
|
|
jQuery( "#firstp" ).on( "click", handler ).trigger( "click" );
|
|
|
|
} );
|
2008-01-14 09:33:08 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
function handler( event ) {
|
|
|
|
if ( event.type === "click" ) {
|
2009-09-16 02:19:18 +00:00
|
|
|
clickCounter++;
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
function handlerWithData( event ) {
|
|
|
|
if ( event.type === "click" ) {
|
2009-09-16 02:19:18 +00:00
|
|
|
clickCounter += event.data;
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
$elem = jQuery( "#firstp" )
|
|
|
|
|
2009-09-16 02:19:18 +00:00
|
|
|
// Regular bind
|
2015-08-16 06:59:58 +00:00
|
|
|
.on( {
|
2012-07-05 19:52:13 +00:00
|
|
|
"click":handler,
|
|
|
|
"mouseover":handler
|
2015-08-16 06:59:58 +00:00
|
|
|
} )
|
|
|
|
|
2009-09-16 02:19:18 +00:00
|
|
|
// Bind with data
|
2015-08-16 06:59:58 +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-08-16 03:45:28 +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-08-16 03:45:28 +00:00
|
|
|
assert.equal( clickCounter, 4, "on(Object)" );
|
|
|
|
assert.equal( mouseoverCounter, 4, "on(Object)" );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#firstp" ).off( {
|
2012-07-05 19:52:13 +00:00
|
|
|
"click":handler,
|
|
|
|
"mouseover":handler
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2009-09-16 02:19:18 +00:00
|
|
|
|
|
|
|
trigger();
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( clickCounter, 4, "on(Object)" );
|
|
|
|
assert.equal( mouseoverCounter, 4, "on(Object)" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2009-09-16 02:19:18 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +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 03:00:57 +00:00
|
|
|
$p.on( events, "a", 2 );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2010-03-14 11:34:32 +00:00
|
|
|
trigger();
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( clickCounter, 3, "on" );
|
|
|
|
assert.equal( mouseoverCounter, 3, "on" );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2013-03-04 03:00:57 +00:00
|
|
|
$p.off( events, "a" );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2010-03-14 11:34:32 +00:00
|
|
|
trigger();
|
2015-08-16 03:45:28 +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-08-16 03:45:28 +00:00
|
|
|
assert.equal( clickCounter, 4, "off" );
|
|
|
|
assert.equal( mouseoverCounter, 4, "off" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2010-03-14 11:34:32 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
} );
|
2010-10-23 18:36:24 +00:00
|
|
|
$a.trigger( "click" );
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
} );
|
2010-10-23 18:36:24 +00:00
|
|
|
$a.trigger( "click" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( lastClick, "click1", "on stopImmediatePropagation" );
|
2013-03-04 03:00:57 +00:00
|
|
|
$p.off( "click", "**" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2010-10-23 18:36:24 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
QUnit.test( "on bubbling, isDefaultPrevented, stopImmediatePropagation", function( assert ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.expect( 3 );
|
2014-03-18 19:55:50 +00:00
|
|
|
|
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-08-16 03:45:28 +00:00
|
|
|
assert.ok( false, "immediate propagation should have been stopped" );
|
2014-03-06 03:46:23 +00:00
|
|
|
},
|
2015-08-16 06:59:58 +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
|
2013-09-07 00:46:55 +00:00
|
|
|
var e = document.createEvent( "MouseEvents" );
|
|
|
|
e.initEvent( "click", true, true );
|
|
|
|
$jq[ 0 ].dispatchEvent( e );
|
2010-12-27 19:30:05 +00:00
|
|
|
};
|
2015-08-16 06:59:58 +00:00
|
|
|
$anchor2.on( "click", function( e ) {
|
2010-12-27 19:30:05 +00:00
|
|
|
e.preventDefault();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2013-09-07 00:46:55 +00:00
|
|
|
$main.on( "click", "#foo", function( e ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( e.isDefaultPrevented(), true, "isDefaultPrevented true passed to bubbled event" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2010-12-27 19:30:05 +00:00
|
|
|
fakeClick( $anchor2 );
|
2013-03-04 02:22:11 +00:00
|
|
|
$anchor2.off( "click" );
|
2013-03-04 03:00:57 +00:00
|
|
|
$main.off( "click", "**" );
|
2013-04-09 15:45:09 +00:00
|
|
|
$anchor2.on( "click", function() {
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2010-12-27 19:30:05 +00:00
|
|
|
// Let the default action occur
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
$main.on( "click", "#foo", function( e ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( e.isDefaultPrevented(), false, "isDefaultPrevented false passed to bubbled event" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2010-12-27 19:30:05 +00:00
|
|
|
fakeClick( $anchor2 );
|
2013-03-04 02:22:11 +00:00
|
|
|
$anchor2.off( "click" );
|
2013-03-04 03:00:57 +00:00
|
|
|
$main.off( "click", "**" );
|
2014-03-06 03:46:23 +00:00
|
|
|
|
2016-02-25 00:18:55 +00:00
|
|
|
$anchor2.on( "click", function( e ) {
|
|
|
|
e.stopImmediatePropagation();
|
|
|
|
assert.ok( true, "anchor was clicked and prop stopped" );
|
|
|
|
} );
|
|
|
|
$anchor2[ 0 ].addEventListener( "click", neverCallMe, false );
|
|
|
|
fakeClick( $anchor2 );
|
|
|
|
$anchor2[ 0 ].removeEventListener( "click", neverCallMe );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2010-12-27 19:30:05 +00:00
|
|
|
|
2017-06-13 16:22:08 +00:00
|
|
|
QUnit.test( "triggered events stopPropagation() for natively-bound events", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
|
|
|
|
|
|
|
var $button = jQuery( "#button" ),
|
|
|
|
$parent = $button.parent(),
|
|
|
|
neverCallMe = function() {
|
|
|
|
assert.ok( false, "propagation should have been stopped" );
|
|
|
|
},
|
|
|
|
stopPropagationCallback = function( e ) {
|
|
|
|
assert.ok( true, "propagation is stopped" );
|
|
|
|
e.stopPropagation();
|
|
|
|
};
|
|
|
|
|
|
|
|
$parent[ 0 ].addEventListener( "click", neverCallMe );
|
|
|
|
$button.on( "click", stopPropagationCallback );
|
|
|
|
$button.trigger( "click" );
|
|
|
|
$parent[ 0 ].removeEventListener( "click", neverCallMe );
|
|
|
|
$button.off( "click", stopPropagationCallback );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( "trigger() works with events that were previously stopped", function( assert ) {
|
|
|
|
assert.expect( 0 );
|
|
|
|
|
|
|
|
var $button = jQuery( "#button" ),
|
|
|
|
$parent = $button.parent(),
|
|
|
|
neverCallMe = function() {
|
|
|
|
assert.ok( false, "propagation should have been stopped" );
|
|
|
|
};
|
|
|
|
|
|
|
|
$parent[ 0 ].addEventListener( "click", neverCallMe );
|
|
|
|
$button.on( "click", neverCallMe );
|
|
|
|
|
|
|
|
var clickEvent = jQuery.Event( "click" );
|
|
|
|
clickEvent.stopPropagation();
|
|
|
|
$button.trigger( clickEvent );
|
|
|
|
|
|
|
|
$parent[ 0 ].removeEventListener( "click", neverCallMe );
|
|
|
|
$button.off( "click", neverCallMe );
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
QUnit.test( "on(), iframes", function( assert ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
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-08-16 06:59:58 +00:00
|
|
|
var doc = jQuery( "#loadediframe" ).contents();
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "div", doc ).on( "click", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "Binding to element inside iframe" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} ).trigger( "click" ).off( "click" );
|
|
|
|
} );
|
2008-01-14 09:33:08 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
function selectOnChange( event ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( event.data, counter++, "Event.data is not a global event object" );
|
2012-06-21 19:30:24 +00:00
|
|
|
}
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
QUnit.test( "on(), namespaced events, cloned events", function( assert ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.expect( 18 );
|
2015-07-29 15:10:04 +00:00
|
|
|
|
2011-02-10 02:15:32 +00:00
|
|
|
var firstp = jQuery( "#firstp" );
|
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
firstp.on( "custom.test", function() {
|
|
|
|
assert.ok( false, "Custom event triggered" );
|
|
|
|
} );
|
2008-02-03 04:33:11 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
firstp.on( "click", function( e ) {
|
|
|
|
assert.ok( true, "Normal click triggered" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( e.type + e.namespace, "click", "Check that only click events trigger this fn" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2007-09-03 14:53:09 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
firstp.on( "click.test", function( e ) {
|
2011-02-10 02:18:11 +00:00
|
|
|
var check = "click";
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "Namespaced click triggered" );
|
2011-02-10 02:18:11 +00:00
|
|
|
if ( e.namespace ) {
|
|
|
|
check += "test";
|
|
|
|
}
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( e.type + e.namespace, check, "Check that only click/click.test events trigger this fn" );
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
firstp.trigger( "click.test" );
|
2007-09-03 14:53:09 +00:00
|
|
|
|
|
|
|
// Remove only the one fn
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "#nonnodes" ).contents().on( "tester", function() {
|
|
|
|
assert.equal( this.nodeType, 1, "Check node,textnode,comment on just does real nodes" );
|
|
|
|
} ).trigger( "tester" );
|
2007-12-20 13:36:56 +00:00
|
|
|
|
|
|
|
// Make sure events stick with appendTo'd elements (which are cloned) #2027
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "<a href='#fail' class='test'>test</a>" ).on( "click", function() { return false; } ).appendTo( "#qunit-fixture" );
|
|
|
|
assert.ok( jQuery( "a.test" ).eq( 0 ).triggerHandler( "click" ) === false, "Handler is bound to appendTo'd elements" );
|
|
|
|
} );
|
2007-03-25 18:06:18 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
function check( name, msg ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.deepEqual( name, order.shift(), msg );
|
2008-12-19 04:34:12 +00:00
|
|
|
}
|
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#firstp" ).on( "custom.test", function() {
|
|
|
|
check( "custom.test", "Custom event triggered" );
|
|
|
|
} );
|
2008-12-19 04:34:12 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#firstp" ).on( "custom.test2", function() {
|
|
|
|
check( "custom.test2", "Custom event triggered" );
|
|
|
|
} );
|
2008-12-19 04:34:12 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#firstp" ).on( "click.test", function() {
|
|
|
|
check( "click.test", "Normal click triggered" );
|
|
|
|
} );
|
2008-12-19 04:34:12 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "#firstp" ).trigger( "click.test.abc" );
|
2008-12-19 04:34:12 +00:00
|
|
|
|
|
|
|
// Trigger one bound fn (1)
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#firstp" ).trigger( "click.abc" );
|
2008-12-19 04:34:12 +00:00
|
|
|
|
|
|
|
// Trigger two bound fn (2)
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#firstp" ).trigger( "click.test" );
|
2008-12-19 04:34:12 +00:00
|
|
|
|
|
|
|
// Remove only the one fn
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#firstp" ).off( "click.abc" );
|
2008-12-19 04:34:12 +00:00
|
|
|
|
|
|
|
// Trigger the remaining fn (1)
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#firstp" ).trigger( "click" );
|
2008-12-19 04:34:12 +00:00
|
|
|
|
|
|
|
// Remove the remaining fn
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#firstp" ).off( ".test" );
|
2008-12-19 04:34:12 +00:00
|
|
|
|
|
|
|
// Trigger the remaining fn (1)
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#firstp" ).trigger( "custom" );
|
|
|
|
} );
|
2008-12-19 04:34:12 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "#firstp" )
|
2013-02-26 17:19:04 +00:00
|
|
|
.on( ".whoops", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( false, "called a namespace-only event" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} )
|
2013-02-26 17:19:04 +00:00
|
|
|
.on( "whoops", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "called whoops" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} )
|
|
|
|
.trigger( "whoops" ) // 1
|
|
|
|
.off( ".whoops" )
|
|
|
|
.trigger( "whoops" ) // 2
|
|
|
|
.off( "whoops" );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( "Empty namespace is ignored", function( assert ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.expect( 1 );
|
2014-12-10 00:59:56 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#firstp" )
|
2014-12-10 00:59:56 +00:00
|
|
|
.on( "meow.", function( e ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( e.namespace, "", "triggered a namespace-less meow event" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} )
|
|
|
|
.trigger( "meow." )
|
|
|
|
.off( "meow." );
|
|
|
|
} );
|
2014-12-10 00:59:56 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
QUnit.test( "on(), with same function", function( assert ) {
|
|
|
|
assert.expect( 2 );
|
2010-02-04 05:49:46 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
var count = 0, func = function() {
|
2010-02-04 05:49:46 +00:00
|
|
|
count++;
|
|
|
|
};
|
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
assert.equal( count, 1, "Verify binding function with multiple namespaces." );
|
2010-02-04 05:49:46 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
assert.equal( count, 1, "Verify that removing events still work." );
|
|
|
|
} );
|
2010-02-04 14:23:50 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
QUnit.test( "on(), make sure order is maintained", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2010-02-04 14:23:50 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
var elem = jQuery( "#firstp" ), log = [], check = [];
|
2010-02-04 14:23:50 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
} );
|
2010-02-04 14:23:50 +00:00
|
|
|
|
|
|
|
check.push( i );
|
2012-06-21 19:30:24 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2010-02-04 14:23:50 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
elem.trigger( "click" );
|
2010-02-04 14:23:50 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
assert.equal( log.join( "," ), check.join( "," ), "Make sure order was maintained." );
|
2010-02-04 14:23:50 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
elem.off( "click" );
|
|
|
|
} );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 03:45:28 +00:00
|
|
|
assert.equal( this, thisObject, "on() with different this object" );
|
2018-01-08 16:55:14 +00:00
|
|
|
}.bind( thisObject ),
|
2009-05-07 00:50:28 +00:00
|
|
|
handler2 = function( event ) {
|
2015-08-16 03:45:28 +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" );
|
2018-01-08 16:55:14 +00:00
|
|
|
}.bind( thisObject );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#firstp" )
|
2018-01-08 16:55:14 +00:00
|
|
|
.on( "click", handler1 ).trigger( "click" ).off( "click", handler1 )
|
|
|
|
.on( "click", data, handler2 ).trigger( "click" ).off( "click", handler2 );
|
2009-05-07 00:50:28 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
assert.ok( !jQuery._data( jQuery( "#firstp" )[ 0 ], "events" ), "Event handler unbound when using different this object and data." );
|
|
|
|
} );
|
2009-05-07 00:50:28 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "#qunit-fixture" ).on( "click", function() { main++; } );
|
|
|
|
jQuery( "#ap" ).trigger( "click" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( main, 1, "Verify that the trigger happened correctly." );
|
2010-02-27 14:02:13 +00:00
|
|
|
|
|
|
|
main = 0;
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#ap" ).on( "click", false );
|
|
|
|
jQuery( "#ap" ).trigger( "click" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( main, 0, "Verify that no bubble happened." );
|
2010-02-27 14:02:13 +00:00
|
|
|
|
|
|
|
main = 0;
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#ap" ).off( "click", false );
|
|
|
|
jQuery( "#ap" ).trigger( "click" );
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "#qunit-fixture" ).off( "click" );
|
|
|
|
} );
|
2010-02-27 14:02:13 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "#qunit-fixture" ).on( "click", "#ap", function() { main++; } );
|
|
|
|
jQuery( "#ap" ).trigger( "click" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( main, 1, "Verify that the trigger happened correctly." );
|
2011-01-01 18:49:59 +00:00
|
|
|
|
|
|
|
main = 0;
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#ap" ).on( "click", "#groups", false );
|
|
|
|
jQuery( "#groups" ).trigger( "click" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( main, 0, "Verify that no bubble happened." );
|
2011-01-01 18:49:59 +00:00
|
|
|
|
|
|
|
main = 0;
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#ap" ).off( "click", "#groups", false );
|
|
|
|
jQuery( "#groups" ).trigger( "click" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( main, 1, "Verify that the trigger happened correctly." );
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#qunit-fixture" ).off( "click", "#ap" );
|
|
|
|
} );
|
2011-01-01 18:49:59 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
QUnit.test( "on()/trigger()/off() on plain object", function( assert ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
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-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( obj ).off( "test" );
|
2010-02-26 16:32:12 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( obj ).on( {
|
2012-07-05 19:52:13 +00:00
|
|
|
"test": function() {
|
2015-08-16 03:45:28 +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-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "Custom submit event run." );
|
2010-11-21 03:31:04 +00:00
|
|
|
}
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2010-02-26 16:32:12 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
events = jQuery._data( obj, "events" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( events, "Object has events bound." );
|
2015-08-16 06:59:58 +00:00
|
|
|
assert.equal( obj[ "events" ], undefined, "Events object on plain objects is not events" );
|
2016-05-10 09:12:28 +00:00
|
|
|
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-08-16 06:59:58 +00:00
|
|
|
jQuery( obj ).trigger( "test" );
|
|
|
|
jQuery( obj ).trigger( "submit" );
|
2010-02-26 16:32:12 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( obj ).off( "test" );
|
|
|
|
jQuery( obj ).off( "submit" );
|
2010-02-26 16:32:12 +00:00
|
|
|
|
|
|
|
// Should trigger 0
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( obj ).off( "test" );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( obj && obj[ jQuery.expando ] &&
|
2011-02-10 02:15:32 +00:00
|
|
|
obj[ jQuery.expando ][ jQuery.expando ] &&
|
2015-08-16 06:59:58 +00:00
|
|
|
obj[ jQuery.expando ][ jQuery.expando ][ "events" ], undefined, "Make sure events object is removed" );
|
|
|
|
} );
|
2010-02-26 16:32:12 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
QUnit.test( "off(type)", function( assert ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.expect( 1 );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2013-04-09 15:45:09 +00:00
|
|
|
var message, func,
|
2015-08-16 06:59:58 +00:00
|
|
|
$elem = jQuery( "#firstp" );
|
2009-01-08 22:22:33 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
function error() {
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
$elem.on( "error1 error2.test", error )
|
2013-03-04 02:22:11 +00:00
|
|
|
.off()
|
2015-08-16 06:59:58 +00:00
|
|
|
.trigger( "error1" ).triggerHandler( "error2" );
|
2011-10-12 00:30:07 +00:00
|
|
|
|
|
|
|
// Should only unbind the specified function
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( document ).on( "click", function() {
|
|
|
|
assert.ok( true, "called handler after selective removal" );
|
|
|
|
} );
|
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-08-16 06:59:58 +00:00
|
|
|
.trigger( "click" )
|
2013-03-04 02:22:11 +00:00
|
|
|
.off( "click" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2009-01-08 22:22:33 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
QUnit.test( "off(eventObject)", function( assert ) {
|
|
|
|
assert.expect( 4 );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
var $elem = jQuery( "#firstp" ),
|
2009-01-08 22:22:33 +00:00
|
|
|
num;
|
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
function check( expected ) {
|
2009-01-08 22:22:33 +00:00
|
|
|
num = 0;
|
2015-08-16 06:59:58 +00:00
|
|
|
$elem.trigger( "foo" ).triggerHandler( "bar" );
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
|
2009-01-08 22:22:33 +00:00
|
|
|
// This handler shouldn't be unbound
|
2015-08-16 06:59:58 +00:00
|
|
|
.on( "foo", function() {
|
2009-01-08 22:22:33 +00:00
|
|
|
num += 1;
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
} )
|
|
|
|
|
2009-01-08 22:22:33 +00:00
|
|
|
// Neither this one
|
2015-08-16 06:59:58 +00:00
|
|
|
.on( "bar", function() {
|
2009-01-08 22:22:33 +00:00
|
|
|
num += 4;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
check( 7 );
|
|
|
|
check( 5 );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
$elem.off( "bar" );
|
2015-08-16 03:45:28 +00:00
|
|
|
check( 1 );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2013-03-04 02:22:11 +00:00
|
|
|
$elem.off();
|
2015-08-16 03:45:28 +00:00
|
|
|
check( 0 );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2009-01-08 22:22:33 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
elem = jQuery( "<a />" );
|
|
|
|
elem.on( "mouseenter", function() {
|
2012-06-21 19:30:24 +00:00
|
|
|
count++;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
elem.trigger( "mouseover" );
|
|
|
|
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-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-03-10 03:38:26 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
QUnit.test( "pointerover triggers pointerenter", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2014-03-16 14:46:57 +00:00
|
|
|
|
|
|
|
var count = 0,
|
2015-08-16 06:59:58 +00:00
|
|
|
elem = jQuery( "<a />" );
|
|
|
|
elem.on( "pointerenter", function() {
|
2014-03-16 14:46:57 +00:00
|
|
|
count++;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
elem.trigger( "pointerover" );
|
|
|
|
assert.equal( count, 1, "make sure pointerover triggers a pointerenter" );
|
2014-03-16 14:46:57 +00:00
|
|
|
|
|
|
|
elem.remove();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2014-03-16 14:46:57 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
QUnit.test( "withinElement implemented with jQuery.contains()", function( assert ) {
|
2011-06-14 19:38:46 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
assert.expect( 1 );
|
2011-06-14 19:38:46 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "#jc-outer" ).on( "mouseenter mouseleave", function( event ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( this.id, "jc-outer", this.id + " " + event.type );
|
2015-10-12 15:37:04 +00:00
|
|
|
} );
|
2011-06-14 19:38:46 +00:00
|
|
|
|
2015-09-18 16:59:48 +00:00
|
|
|
jQuery( "#jc-inner" ).trigger( "mouseenter" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-06-14 19:38:46 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
QUnit.test( "mouseenter, mouseleave don't catch exceptions", function( assert ) {
|
|
|
|
assert.expect( 2 );
|
2011-06-14 19:38:46 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
var elem = jQuery( "#firstp" ).on( "mouseenter mouseleave", function() {
|
2013-01-27 04:48:59 +00:00
|
|
|
throw "an Exception";
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-06-14 19:38:46 +00:00
|
|
|
|
|
|
|
try {
|
2015-08-16 06:59:58 +00:00
|
|
|
elem.trigger( "mouseenter" );
|
|
|
|
} catch ( e ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( e, "an Exception", "mouseenter doesn't catch exceptions" );
|
2011-06-14 19:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2015-08-16 06:59:58 +00:00
|
|
|
elem.trigger( "mouseleave" );
|
|
|
|
} catch ( e ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( e, "an Exception", "mouseleave doesn't catch exceptions" );
|
2011-06-14 19:38:46 +00:00
|
|
|
}
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-06-14 19:38:46 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "html" ).trigger( "click" );
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "body" ).trigger( "click" );
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "#qunit-fixture" ).trigger( "click" );
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "#ap" ).trigger( "click" );
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( document ).trigger( "click" );
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
handler = function( event, a, b, c ) {
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
$elem[ 0 ].click = function() {
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( document ).on( "mouseenter", "#firstp", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "Trigger mouseenter bound by on" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-04-17 18:58:20 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( document ).on( "mouseleave", "#firstp", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "Trigger mouseleave bound by on" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-04-17 18:58:20 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
$elem.trigger( "mouseenter" );
|
2011-04-17 18:58:20 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
$elem.trigger( "mouseleave" );
|
2011-04-17 18:58:20 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
$elem.on( "click", handler ).trigger( "click", [ 1, "2", "abc" ] );
|
2007-08-30 05:51:11 +00:00
|
|
|
|
|
|
|
// Simulate a "native" click
|
2015-08-16 06:59:58 +00:00
|
|
|
$elem[ 0 ].click = function() {
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 03:45:28 +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-08-16 06:59:58 +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-08-16 03:45:28 +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-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
form.on( "submit", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "Local `on` still works." );
|
2010-01-25 18:45:07 +00:00
|
|
|
return false;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2010-01-25 18:45:07 +00:00
|
|
|
|
|
|
|
// Trigger 1
|
2015-08-16 06:59:58 +00:00
|
|
|
form.trigger( "submit" );
|
2010-01-25 18:45:07 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
form.off( "submit" );
|
2010-01-25 18:45:07 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( document ).on( "submit", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "Make sure bubble works up to document." );
|
2010-01-25 18:45:07 +00:00
|
|
|
return false;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2010-01-25 18:45:07 +00:00
|
|
|
|
|
|
|
// Trigger 1
|
2015-08-16 06:59:58 +00:00
|
|
|
form.trigger( "submit" );
|
2010-01-25 18:45:07 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( document ).off( "submit" );
|
2010-01-25 18:45:07 +00:00
|
|
|
|
|
|
|
form.remove();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2010-01-25 18:45:07 +00:00
|
|
|
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +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-08-16 03:45:28 +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-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
} );
|
2012-05-18 20:30:28 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
QUnit.test( "change event bubbles on copied forms (#11796)", function( assert ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.expect( 3 );
|
2012-06-21 19:30:24 +00:00
|
|
|
|
2012-05-21 23:01:59 +00:00
|
|
|
var $formByClone, $formByHTML,
|
2015-08-16 06:59:58 +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-08-16 03:45:28 +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-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
} );
|
2012-05-21 23:01:59 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
event = jQuery.Event( "noNew" );
|
2015-08-16 03:45:28 +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-08-16 03:45:28 +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-08-16 03:45:28 +00:00
|
|
|
assert.equal( event.isDefaultPrevented(), true, "Verify isDefaultPrevented" );
|
2008-12-31 02:58:13 +00:00
|
|
|
event.stopPropagation();
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( event.isPropagationStopped(), true, "Verify isPropagationStopped" );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
event.isPropagationStopped = function() { return false; };
|
2008-12-31 02:58:13 +00:00
|
|
|
event.stopImmediatePropagation();
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
$parent.on( "foo", function( e ) {
|
|
|
|
|
2008-12-31 02:58:13 +00:00
|
|
|
// Tries bubbling
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
} );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2008-12-31 02:58:13 +00:00
|
|
|
// test with an event object
|
2015-08-16 06:59:58 +00:00
|
|
|
event = new jQuery.Event( "foo" );
|
2011-04-12 08:47:46 +00:00
|
|
|
event.secret = "boo!";
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
function error() {
|
|
|
|
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-08-16 06:59:58 +00:00
|
|
|
$parent.on( "foo", error );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
$child.on( "foo", function( e, a, b, c ) {
|
|
|
|
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-08-16 03:45:28 +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-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
event = new jQuery.Event( "foo" );
|
2016-05-10 09:12:28 +00:00
|
|
|
$child.trigger( event, [ 1, 2, 3 ] ).off();
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
} );
|
2006-12-28 11:37:07 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
QUnit.test( ".trigger() bubbling on disconnected elements (#10489)", function( assert ) {
|
|
|
|
assert.expect( 2 );
|
2011-10-13 20:30:40 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( window ).on( "click", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( false, "click fired on window" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-10-13 20:30:40 +00:00
|
|
|
|
|
|
|
jQuery( "<div><p>hi</p></div>" )
|
|
|
|
.on( "click", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "click fired on div" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} )
|
2011-10-13 20:30:40 +00:00
|
|
|
.find( "p" )
|
2011-11-11 02:53:07 +00:00
|
|
|
.on( "click", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "click fired on p" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} )
|
|
|
|
.trigger( "click" )
|
2011-10-13 20:30:40 +00:00
|
|
|
.off( "click" )
|
|
|
|
.end()
|
|
|
|
.off( "click" )
|
|
|
|
.remove();
|
|
|
|
|
|
|
|
jQuery( window ).off( "click" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-10-13 20:30:40 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( window ).on( "load", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( false, "load fired on window" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-11-09 00:32:25 +00:00
|
|
|
|
2017-08-01 16:52:45 +00:00
|
|
|
jQuery( "<img src='" + baseURL + "1x1.jpg' />" )
|
2011-11-09 00:32:25 +00:00
|
|
|
.appendTo( "body" )
|
|
|
|
.on( "load", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "load fired on img" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} )
|
2011-11-09 00:32:25 +00:00
|
|
|
.trigger( "load" )
|
|
|
|
.remove();
|
|
|
|
|
|
|
|
jQuery( window ).off( "load" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-11-09 00:32:25 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-14 00:53:19 +00:00
|
|
|
svg = jQuery(
|
2012-10-16 14:17:14 +00:00
|
|
|
"<svg height='1' version='1.1' width='1' xmlns='http://www.w3.org/2000/svg'>" +
|
2013-01-14 00:53:19 +00:00
|
|
|
"<defs><rect id='ref' x='10' y='20' width='100' height='60' r='10' rx='10' ry='10'></rect></defs>" +
|
2012-10-16 14:17:14 +00:00
|
|
|
"<rect class='svg-by-class' x='10' y='20' width='100' height='60' r='10' rx='10' ry='10'></rect>" +
|
|
|
|
"<rect id='svg-by-id' x='10' y='20' width='100' height='60' r='10' rx='10' ry='10'></rect>" +
|
2013-01-14 00:53:19 +00:00
|
|
|
"<use id='use' xlink:href='#ref'></use>" +
|
2012-10-16 14:17:14 +00:00
|
|
|
"</svg>"
|
2013-01-14 00:53:19 +00:00
|
|
|
);
|
2012-01-13 01:30:45 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#qunit-fixture" )
|
2013-01-14 00:53:19 +00:00
|
|
|
.append( svg )
|
2011-11-16 15:35:53 +00:00
|
|
|
.on( "click", "#svg-by-id", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "delegated id selector" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} )
|
2012-07-11 21:33:53 +00:00
|
|
|
.on( "click", "[class~='svg-by-class']", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "delegated class selector" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} )
|
2012-07-11 21:33:53 +00:00
|
|
|
.find( "#svg-by-id, [class~='svg-by-class']" )
|
2015-08-16 06:59:58 +00:00
|
|
|
.trigger( "click" )
|
2013-01-14 00:53:19 +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-08-16 06:59:58 +00:00
|
|
|
useElem = svg.find( "#use" )[ 0 ];
|
2013-01-14 03:59:20 +00:00
|
|
|
if ( document.createEvent && useElem && useElem.instanceRoot ) {
|
2015-08-16 06:59:58 +00:00
|
|
|
e = document.createEvent( "MouseEvents" );
|
2013-01-14 00:53:19 +00:00
|
|
|
e.initEvent( "click", true, true );
|
2013-01-14 03:59:20 +00:00
|
|
|
useElem.instanceRoot.dispatchEvent( e );
|
2013-01-14 00:53:19 +00:00
|
|
|
}
|
2011-11-16 15:35:53 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#qunit-fixture" ).off( "click" );
|
|
|
|
} );
|
2011-11-16 15:35:53 +00:00
|
|
|
|
2016-05-07 02:12:53 +00:00
|
|
|
QUnit.test( "Delegated events with malformed selectors (gh-3071)", function( assert ) {
|
|
|
|
assert.expect( 3 );
|
2016-05-01 21:40:20 +00:00
|
|
|
|
2016-05-07 02:12:53 +00:00
|
|
|
assert.throws( function() {
|
|
|
|
jQuery( "#foo" ).on( "click", ":not", function() {} );
|
|
|
|
}, "malformed selector throws on attach" );
|
2016-05-01 21:40:20 +00:00
|
|
|
|
2016-05-07 02:12:53 +00:00
|
|
|
assert.throws( function() {
|
|
|
|
jQuery( "#foo" ).on( "click", "nonexistent:not", function() {} );
|
|
|
|
}, "short-circuitable malformed selector throws on attach" );
|
2016-05-01 21:40:20 +00:00
|
|
|
|
2016-05-07 02:12:53 +00:00
|
|
|
jQuery( "#foo > :first-child" ).click();
|
|
|
|
assert.ok( true, "malformed selector does not throw on event" );
|
2016-05-01 21:40:20 +00:00
|
|
|
} );
|
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
} )
|
|
|
|
.appendTo( "body" );
|
2012-01-12 02:56:18 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "body" )
|
2012-01-12 02:56:18 +00:00
|
|
|
.on( "submit", "#myform", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "delegated id selector with aliased id" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} )
|
|
|
|
.find( "#myform" )
|
|
|
|
.trigger( "submit" )
|
2012-01-12 02:56:18 +00:00
|
|
|
.end()
|
2015-08-16 06:59:58 +00:00
|
|
|
.off( "submit" );
|
2012-01-12 02:56:18 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "delegated id selector with aliased disabled" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} )
|
|
|
|
.find( "#myform" )
|
|
|
|
.trigger( "submit" )
|
2011-11-21 16:33:21 +00:00
|
|
|
.end()
|
2015-08-16 06:59:58 +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-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "click on enabled/disabled button with nesty elements" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} )
|
2012-06-27 01:38:30 +00:00
|
|
|
.on( "mouseover", "#nestyDisabledBtn", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "mouse on enabled/disabled button with nesty elements" );
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-11-21 16:33:21 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
.appendTo( "body" );
|
2012-03-05 02:11:50 +00:00
|
|
|
|
|
|
|
jQuery( "body" )
|
|
|
|
.on( "submit", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "submit bubbled on first handler" );
|
2012-03-05 02:11:50 +00:00
|
|
|
return false;
|
2015-08-16 06:59:58 +00:00
|
|
|
} )
|
2012-03-05 02:11:50 +00:00
|
|
|
.find( "#myform input[type=submit]" )
|
2015-08-16 06:59:58 +00:00
|
|
|
.each( function() { this.click(); } )
|
2012-03-05 02:11:50 +00:00
|
|
|
.end()
|
|
|
|
.on( "submit", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( false, "submit bubbled on second handler" );
|
2012-03-05 02:11:50 +00:00
|
|
|
return false;
|
2015-08-16 06:59:58 +00:00
|
|
|
} )
|
2012-03-05 02:11:50 +00:00
|
|
|
.find( "#myform input[type=submit]" )
|
2015-08-16 06:59:58 +00:00
|
|
|
.each( function() {
|
2012-03-05 02:11:50 +00:00
|
|
|
jQuery( this.form ).on( "submit", function( e ) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2012-03-05 02:11:50 +00:00
|
|
|
this.click();
|
2015-08-16 06:59:58 +00:00
|
|
|
} )
|
2012-03-05 02:11:50 +00:00
|
|
|
.end()
|
|
|
|
.off( "submit" );
|
|
|
|
|
|
|
|
form.remove();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2012-03-05 02:11:50 +00:00
|
|
|
|
2019-04-29 20:56:09 +00:00
|
|
|
// Support: iOS <=7 - 12+
|
2016-02-25 00:18:55 +00:00
|
|
|
// iOS has the window.onbeforeunload field but doesn't support the beforeunload
|
2014-01-23 04:43:58 +00:00
|
|
|
// handler making it impossible to feature-detect the support.
|
2016-02-25 00:18:55 +00:00
|
|
|
QUnit[ /(ipad|iphone|ipod)/i.test( navigator.userAgent ) ? "skip" : "test" ](
|
2019-02-18 18:02:38 +00:00
|
|
|
"on(beforeunload)", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2017-08-01 16:52:45 +00:00
|
|
|
var iframe = jQuery( jQuery.parseHTML( "<iframe src='" + baseURL + "event/onbeforeunload.html'><iframe>" ) );
|
2016-02-25 00:18:55 +00:00
|
|
|
var done = assert.async();
|
2012-06-18 02:42:24 +00:00
|
|
|
|
2016-02-25 00:18:55 +00:00
|
|
|
window.onmessage = function( event ) {
|
|
|
|
var payload = JSON.parse( event.data );
|
2012-06-18 02:42:24 +00:00
|
|
|
|
2016-02-25 00:18:55 +00:00
|
|
|
assert.ok( payload.event, "beforeunload", "beforeunload event" );
|
2012-06-18 02:42:24 +00:00
|
|
|
|
2016-02-25 00:18:55 +00:00
|
|
|
iframe.remove();
|
|
|
|
window.onmessage = null;
|
|
|
|
done();
|
|
|
|
};
|
2012-08-14 20:10:10 +00:00
|
|
|
|
2016-02-25 00:18:55 +00:00
|
|
|
iframe.appendTo( "#qunit-fixture" );
|
|
|
|
} );
|
2012-06-18 02:42:24 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
QUnit.test( "jQuery.Event( type, props )", function( assert ) {
|
2011-04-05 19:55:40 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
assert.expect( 6 );
|
2011-04-05 19:55:40 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
var event = jQuery.Event( "keydown", { keyCode: 64 } ),
|
2011-04-05 19:55:40 +00:00
|
|
|
handler = function( event ) {
|
2015-08-16 03:45:28 +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-08-16 03:45:28 +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-08-16 06:59:58 +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-08-16 03:45:28 +00:00
|
|
|
assert.ok( "keyCode" in event, "Special 'keyCode' property exists" );
|
2011-04-05 19:55:40 +00:00
|
|
|
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "body" ).on( "keydown", handler ).trigger( event );
|
2011-04-05 19:55:40 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "body" ).off( "keydown" );
|
2011-04-05 19:55:40 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-04-05 19:55:40 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
QUnit.test( "jQuery.Event properties", function( assert ) {
|
|
|
|
assert.expect( 12 );
|
2011-11-11 02:53:07 +00:00
|
|
|
|
2013-02-02 02:20:15 +00:00
|
|
|
var handler,
|
2015-08-16 06:59:58 +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-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
$target[ 0 ].onclick = function( e ) {
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
$target.trigger( "click" );
|
2013-02-02 02:20:15 +00:00
|
|
|
|
|
|
|
$target.one( "click", function( e ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( e.isTrigger, 2, "triggerHandler at target" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
$target[ 0 ].onclick = function( e ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( e.isTrigger, 2, "triggerHandler at target (native handler)" );
|
2013-02-02 02:20:15 +00:00
|
|
|
};
|
2015-08-16 06:59:58 +00:00
|
|
|
$target.triggerHandler( "click" );
|
2013-02-02 02:20:15 +00:00
|
|
|
|
|
|
|
handler = function( e ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( e.isTrigger, undefined, "native event at " + this.id );
|
2013-02-02 02:20:15 +00:00
|
|
|
};
|
|
|
|
$target.one( "click", handler );
|
2015-08-16 06:59:58 +00:00
|
|
|
$target[ 0 ].onclick = function( e ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( e.isTrigger, undefined, "native event at target (native handler)" );
|
2013-02-02 02:20:15 +00:00
|
|
|
};
|
2015-08-16 06:59:58 +00:00
|
|
|
fireNative( $target[ 0 ], "click" );
|
|
|
|
} );
|
2009-02-17 12:38:16 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "body" ).trigger( "click" );
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "div#nothiddendiv" ).trigger( "click" );
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "div#nothiddendivchild" ).trigger( "click" );
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "div#nothiddendivchild" ).trigger( "submit" );
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "div#nothiddendivchild" ).trigger( "click" );
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "#body" ).off( "click", "div#nothiddendivchild" );
|
|
|
|
jQuery( "div#nothiddendivchild" ).trigger( "click" );
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "div#nothiddendiv" ).trigger( "click" );
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "#body" ).on( "click", "div#nothiddendivchild", function( e ) { liveb++; e.stopPropagation(); } );
|
|
|
|
jQuery( "div#nothiddendivchild" ).trigger( "click" );
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
event = jQuery.Event( "click" );
|
2010-02-01 23:06:03 +00:00
|
|
|
event.button = 1;
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "div#nothiddendiv" ).trigger( event );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( livea, 0, "on secondary click" );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "#qunit-fixture" ).off( "click", "#foo" );
|
|
|
|
jQuery( "#foo" ).trigger( "click" );
|
|
|
|
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-08-16 06:59:58 +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-08-16 06:59:58 +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
|
2017-12-11 17:58:35 +00:00
|
|
|
jQuery( "#body" ).on( "click", "#foo", function() {
|
|
|
|
assert.equal( this.foo, "bar", "on with event scope" );
|
|
|
|
}.bind( { "foo": "bar" } ) );
|
|
|
|
|
2015-08-16 06:59:58 +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
|
2017-12-11 17:58:35 +00:00
|
|
|
jQuery( "#body" ).on( "click", "#foo", true, function( e, data ) {
|
2015-08-16 03:45:28 +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" );
|
2015-08-16 06:59:58 +00:00
|
|
|
assert.equal( data, true, "on with with different this object, event data, and trigger data" );
|
2017-12-11 17:58:35 +00:00
|
|
|
}.bind( { "foo": "bar" } ) );
|
2015-08-16 06:59:58 +00:00
|
|
|
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-08-16 06:59:58 +00:00
|
|
|
jQuery( "#body" ).on( "click", "#anchor2", function() { return false; } );
|
2013-04-09 15:45:09 +00:00
|
|
|
hash = window.location.hash;
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#anchor2" ).trigger( "click" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( window.location.hash, hash, "return false worked" );
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#body" ).off( "click", "#anchor2" );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
|
|
|
// Verify that .preventDefault() prevents default action
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#body" ).on( "click", "#anchor2", function( e ) { e.preventDefault(); } );
|
2012-06-21 19:30:24 +00:00
|
|
|
hash = window.location.hash;
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#anchor2" ).trigger( "click" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( window.location.hash, hash, "e.preventDefault() worked" );
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
function callback() { called++; return false; }
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#body" ).on( "click", "#nothiddendiv", callback );
|
|
|
|
jQuery( "#body" ).on( "click", "#anchor2", callback );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#nothiddendiv" ).trigger( "click" );
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "#anchor2" ).trigger( "click" );
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "#nothiddendiv" ).trigger( "click" );
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "#anchor2" ).trigger( "click" );
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "#body" ).on( "foo", "#nothiddendiv", callback );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
|
|
|
// Cleanup
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "#nothiddendiv" ).trigger( "click" );
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "#nothiddendiv" ).trigger( "foo" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( called, 1, "Verify that one foo occurred." );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
|
|
|
// Cleanup
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "#nothiddendivchild" ).html( "<span></span>" );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "#nothiddendiv span" ).trigger( "click" );
|
|
|
|
assert.equal( jQuery( "#nothiddendiv span" ).length, 0, "Verify that first handler occurred and modified the DOM." );
|
2015-08-16 03:45:28 +00:00
|
|
|
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-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "span#liveSpan1 a" ).trigger( "click" );
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "span#liveSpan2 a" ).trigger( "click" );
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "#body" ).on( "click", "span#liveSpan1", function( e ) {
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
} );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "span#liveSpan1 a" ).trigger( "click" );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 03:45:28 +00:00
|
|
|
assert.equal( livee, 0, "No clicks, deep selector." );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
|
|
|
livee = 0;
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#nothiddendivchild" ).trigger( "click" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( livee, 2, "Click, deep selector." );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
|
|
|
livee = 0;
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#nothiddendivchild" ).trigger( "mouseover" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( livee, 1, "Mouseover, deep selector." );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#body" ).off( "mouseover", "#nothiddendiv div" );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
|
|
|
livee = 0;
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#nothiddendivchild" ).trigger( "click" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( livee, 2, "Click, deep selector." );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
|
|
|
livee = 0;
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#nothiddendivchild" ).trigger( "mouseover" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( livee, 0, "Mouseover, deep selector." );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#body" ).off( "click", "#nothiddendiv div", clickB );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
|
|
|
livee = 0;
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#nothiddendivchild" ).trigger( "click" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( livee, 1, "Click, deep selector." );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#body" ).off( "click", "#nothiddendiv div" );
|
|
|
|
} );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
QUnit.test( "jQuery.off using dispatched jQuery.Event", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2011-11-11 02:53:07 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 03:45:28 +00:00
|
|
|
assert.equal( ++count, 1, "event called once before removal" );
|
2011-10-24 02:25:13 +00:00
|
|
|
jQuery().off( event );
|
2015-08-16 06:59:58 +00:00
|
|
|
} )
|
|
|
|
.find( "a" ).trigger( "click" ).trigger( "click" ).end()
|
2011-10-24 02:25:13 +00:00
|
|
|
.remove();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-10-24 02:25:13 +00:00
|
|
|
|
2015-11-10 18:26:15 +00:00
|
|
|
// selector-native does not support scope-fixing in delegation
|
|
|
|
QUnit[ jQuery.find.compile ? "test" : "skip" ]( "delegated event with delegateTarget-relative selector", function( assert ) {
|
2015-08-16 06:59:58 +00:00
|
|
|
assert.expect( 3 );
|
|
|
|
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-08-16 06:59:58 +00:00
|
|
|
markup.find( "#ul0" )
|
2013-02-19 04:52:29 +00:00
|
|
|
.on( "click", "div li a", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( false, "div is ABOVE the delegation point!" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} )
|
2013-02-19 04:52:29 +00:00
|
|
|
.on( "click", "ul a", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( false, "ul IS the delegation point!" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} )
|
2013-02-19 04:52:29 +00:00
|
|
|
.on( "click", "li.test a", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "li.test is below the delegation point." );
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
markup.find( "ul" ).eq( 0 )
|
2012-07-23 16:48:29 +00:00
|
|
|
.on( "click", ">li>a", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( this.id === "a0", "child li was clicked" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} )
|
|
|
|
.find( "#ul0" )
|
2012-07-23 16:48:29 +00:00
|
|
|
.on( "click", "li:first>a", function() {
|
2015-08-16 06:59:58 +00:00
|
|
|
assert.ok( this.id === "a0_0", "first li under #u10 was clicked" );
|
|
|
|
} )
|
2012-06-27 00:36:00 +00:00
|
|
|
.end()
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
} );
|
2012-06-27 00:36:00 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "delegated event with selector matching Object.prototype property (#13203)", function( assert ) {
|
2015-08-16 06:59:58 +00:00
|
|
|
assert.expect( 1 );
|
2013-01-14 02:38:40 +00:00
|
|
|
|
|
|
|
var matched = 0;
|
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#foo" ).on( "click", "toString", function() {
|
2013-01-14 02:38:40 +00:00
|
|
|
matched++;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2013-01-14 02:38:40 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#anchor2" ).trigger( "click" );
|
2013-01-14 02:38:40 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( matched, 0, "Nothing matched 'toString'" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2013-01-14 02:38:40 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "delegated event with intermediate DOM manipulation (#13208)", function( assert ) {
|
2015-08-16 06:59:58 +00:00
|
|
|
assert.expect( 1 );
|
2013-01-16 05:14:57 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "Element removed" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
jQuery( "#anchor2" ).trigger( "click" );
|
|
|
|
} );
|
2013-01-16 05:14:57 +00:00
|
|
|
|
2015-10-18 16:24:01 +00:00
|
|
|
QUnit.test( "ignore comment nodes in event delegation (gh-2055)", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
|
|
|
|
|
|
|
// Test if DOMNodeInserted is supported
|
|
|
|
// This is a back-up for when DOMNodeInserted support
|
|
|
|
// is eventually removed from browsers
|
|
|
|
function test() {
|
|
|
|
var ret = false;
|
|
|
|
var $fixture = jQuery( "#qunit-fixture" );
|
|
|
|
$fixture.on( "DOMNodeInserted", function() {
|
|
|
|
ret = true;
|
|
|
|
$fixture.off( "DOMNodeInserted" );
|
|
|
|
} ).append( "<div></div>" );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
var $foo = jQuery( "#foo" ).on( "DOMNodeInserted", "[id]", function() {
|
|
|
|
assert.ok( true, "No error thrown on comment node" );
|
|
|
|
} ),
|
|
|
|
$comment = jQuery( document.createComment( "comment" ) )
|
|
|
|
.appendTo( $foo.find( "#sap" ) );
|
|
|
|
|
|
|
|
if ( !test() ) {
|
2016-05-10 09:12:28 +00:00
|
|
|
fireNative( $comment[ 0 ], "DOMNodeInserted" );
|
2015-10-18 16:24:01 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 03:45:28 +00:00
|
|
|
assert.ok( false, "directly-bound event on delegate target was called" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} )
|
2011-10-21 14:56:16 +00:00
|
|
|
.on( "click", "a", function( e ) {
|
|
|
|
e.stopPropagation();
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "delegated handler was called" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} )
|
|
|
|
.find( "a" ).trigger( "click" ).end()
|
2011-10-21 14:56:16 +00:00
|
|
|
.remove();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-10-21 14:56:16 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
div = jQuery( "#body" );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
div.on( "click submit", "div#nothiddendivchild", function() { count++; } );
|
|
|
|
div.on( "click", function() { clicks++; } );
|
2013-03-04 03:00:57 +00:00
|
|
|
div.off( undefined, "**" );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "div#nothiddendivchild" ).trigger( "click" );
|
|
|
|
jQuery( "div#nothiddendivchild" ).trigger( "submit" );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( count, 0, "Make sure no events were triggered." );
|
2012-08-08 00:49:34 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
div.trigger( "click" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( clicks, 2, "Make sure delegated and directly bound event occurred." );
|
2015-08-16 06:59:58 +00:00
|
|
|
div.off( "click" );
|
|
|
|
} );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
div = jQuery( "#body" );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
div.on( "click submit", "div#nothiddendivchild", function() { count++; } );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "div#nothiddendivchild" ).trigger( "click" );
|
|
|
|
jQuery( "div#nothiddendivchild" ).trigger( "submit" );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( count, 2, "Make sure both the click and submit were triggered." );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#body" ).off( undefined, "**" );
|
|
|
|
} );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
} );
|
2010-12-30 06:34:48 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
checkbox = jQuery( "#check2" );
|
|
|
|
checkboxFunction = function() {
|
2013-04-09 15:45:09 +00:00
|
|
|
checkboxChange++;
|
|
|
|
};
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
select[ 0 ].selectedIndex = select[ 0 ].selectedIndex ? 0 : 1;
|
|
|
|
select.trigger( "change" );
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
select[ 0 ].selectedIndex = select[ 0 ].selectedIndex ? 0 : 1;
|
|
|
|
select.trigger( "change" );
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
checkbox.trigger( "change" );
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
text = jQuery( "#name" );
|
2013-04-09 15:45:09 +00:00
|
|
|
textChange = 0;
|
|
|
|
oldTextVal = text.val();
|
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#body" ).on( "change", "#name", function() {
|
2010-02-01 23:06:03 +00:00
|
|
|
textChange++;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
text.val( oldTextVal + "foo" );
|
|
|
|
text.trigger( "change" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( textChange, 1, "Change on text input." );
|
2009-09-14 22:04:22 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
password = jQuery( "#name" );
|
2013-04-09 15:45:09 +00:00
|
|
|
passwordChange = 0;
|
|
|
|
oldPasswordVal = password.val();
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#body" ).on( "change", "#name", function() {
|
2010-02-01 23:06:03 +00:00
|
|
|
passwordChange++;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
password.val( oldPasswordVal + "foo" );
|
|
|
|
password.trigger( "change" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( passwordChange, 1, "Change on password input." );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "#body" ).off( "change", "select[name='S1']" );
|
|
|
|
select[ 0 ].selectedIndex = select[ 0 ].selectedIndex ? 0 : 1;
|
|
|
|
select.trigger( "change" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( selectChange, 0, "Die on click works." );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
|
|
|
selectChange = 0;
|
2015-08-16 06:59:58 +00:00
|
|
|
select[ 0 ].selectedIndex = select[ 0 ].selectedIndex ? 0 : 1;
|
|
|
|
select.trigger( "change" );
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "#body" ).off( "change", "#check2", checkboxFunction );
|
|
|
|
checkbox.trigger( "change" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( checkboxChange, 1, "Die on checkbox." );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
QUnit.test( "delegated on with submit", function( assert ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
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-08-16 06:59:58 +00:00
|
|
|
jQuery( "#body" ).on( "submit", "#testForm", function( ev ) {
|
2010-02-01 23:06:03 +00:00
|
|
|
count1++;
|
|
|
|
ev.preventDefault();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( document ).on( "submit", "body", function( ev ) {
|
2010-02-01 23:06:03 +00:00
|
|
|
count2++;
|
|
|
|
ev.preventDefault();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2010-02-01 23:06:03 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "#testForm input[name=sub1]" ).trigger( "submit" );
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "#body" ).off( undefined, "**" );
|
|
|
|
jQuery( document ).off( undefined, "**" );
|
|
|
|
} );
|
2009-09-14 22:04:22 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
QUnit.test( "delegated off() with only namespaces", function( assert ) {
|
|
|
|
assert.expect( 2 );
|
2011-04-05 22:12:50 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
$delegate.on( "click.ns", "a", function() {
|
2011-04-05 22:12:50 +00:00
|
|
|
count++;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-04-05 22:12:50 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "a", $delegate ).eq( 0 ).trigger( "click.ns" );
|
2011-04-05 22:12:50 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
assert.equal( count, 1, "delegated click.ns" );
|
2011-04-05 22:12:50 +00:00
|
|
|
|
2013-03-04 03:00:57 +00:00
|
|
|
$delegate.off( ".ns", "**" );
|
2011-04-05 22:12:50 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "a", $delegate ).eq( 1 ).trigger( "click.ns" );
|
2011-04-05 22:12:50 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
assert.equal( count, 1, "no more .ns after off" );
|
|
|
|
} );
|
2011-04-05 22:12:50 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
QUnit.test( "Non DOM element events", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2009-05-04 04:54:09 +00:00
|
|
|
|
|
|
|
var o = {};
|
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( o ).on( "nonelementobj", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "Event on non-DOM object triggered" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2009-05-04 04:54:09 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( o ).trigger( "nonelementobj" ).off( "nonelementobj" );
|
|
|
|
} );
|
2009-05-04 04:54:09 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
QUnit.test( "inline handler returning false stops default", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2011-11-11 02:53:07 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
var markup = jQuery( "<div><a href=\"#\" onclick=\"return false\">x</a></div>" );
|
|
|
|
markup.on( "click", function( e ) {
|
|
|
|
assert.ok( e.isDefaultPrevented(), "inline handler prevented default" );
|
2011-11-07 16:07:36 +00:00
|
|
|
return false;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
markup.find( "a" ).trigger( "click" );
|
|
|
|
markup.off( "click" );
|
|
|
|
} );
|
2011-11-07 16:07:36 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
QUnit.test( "window resize", function( assert ) {
|
|
|
|
assert.expect( 2 );
|
2010-10-11 22:20:57 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( window ).off();
|
2010-10-11 22:03:54 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( window ).on( "resize", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "Resize event fired." );
|
2015-08-16 06:59:58 +00:00
|
|
|
} ).trigger( "resize" ).off( "resize" );
|
2010-10-11 22:20:57 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
assert.ok( !jQuery._data( window, "events" ), "Make sure all the events are gone." );
|
|
|
|
} );
|
2010-10-15 01:40:35 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
input[ 0 ].focus();
|
2011-11-14 02:32:07 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( "body" ).on( "focusin.focusinBubblesTest", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( 1, order++, "focusin on the body second" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-03-05 02:16:40 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
input.on( "focusin.focusinBubblesTest", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( 0, order++, "focusin on the element first" );
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-03-05 02:16:40 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
QUnit.test( "custom events with colons (#3533, #8272)", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2011-04-08 02:52:15 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
var tab = jQuery( "<table><tr><td>trigger</td></tr></table>" ).appendTo( "body" );
|
2011-04-08 02:52:15 +00:00
|
|
|
try {
|
2015-08-16 06:59:58 +00:00
|
|
|
tab.trigger( "back:forth" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "colon events don't throw" );
|
2011-04-08 02:52:15 +00:00
|
|
|
} catch ( e ) {
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-03-31 15:37:48 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "whipped it good" );
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 03:45:28 +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-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 03:45:28 +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-08-16 06:59:58 +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-08-16 03:45:28 +00:00
|
|
|
assert.equal( counter, 0, "nothing triggered yet" );
|
2015-08-16 06:59:58 +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-08-16 03:45:28 +00:00
|
|
|
assert.equal( counter, 49, "after triggering em element" );
|
2015-08-16 06:59:58 +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-08-16 03:45:28 +00:00
|
|
|
assert.equal( counter, 51, "after triggering #onandoff cluck" );
|
2015-08-16 06:59:58 +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-08-16 03:45:28 +00:00
|
|
|
assert.equal( counter, 51, "after triggering b" );
|
2015-08-16 06:59:58 +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-08-16 03:45:28 +00:00
|
|
|
assert.equal( counter, 54, "final triggers" );
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
data = jQuery.data[ jQuery( "#onandoff" )[ 0 ].expando ] || {};
|
|
|
|
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-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-07-29 00:43:23 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
QUnit.test( "special on name mapping", function( assert ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.expect( 7 );
|
2011-11-11 02:53:07 +00:00
|
|
|
|
2016-05-10 09:12:28 +00:00
|
|
|
jQuery.event.special.slap = {
|
2011-11-11 02:53:07 +00:00
|
|
|
bindType: "click",
|
|
|
|
delegateType: "swing",
|
|
|
|
handle: function( event ) {
|
2015-08-16 03:45:28 +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-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "event " + event.type + " triggered" );
|
2011-11-11 02:53:07 +00:00
|
|
|
};
|
|
|
|
|
2015-08-16 06:59:58 +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();
|
2016-05-10 09:12:28 +00:00
|
|
|
delete jQuery.event.special.slap;
|
2011-11-14 01:50:36 +00:00
|
|
|
|
2016-05-10 09:12:28 +00:00
|
|
|
jQuery.event.special.gutfeeling = {
|
2011-11-14 01:50:36 +00:00
|
|
|
bindType: "click",
|
|
|
|
delegateType: "click",
|
|
|
|
handle: function( event ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( event.handleObj.origType, "gutfeeling", "got a gutfeeling" );
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2011-12-14 02:40:59 +00:00
|
|
|
// Need to call the handler since .one() uses it to unbind
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
delete jQuery.event.special[ "gutfeeling" ];
|
|
|
|
} );
|
2011-11-10 02:29:15 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-11-10 02:29:15 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
QUnit.test( ".on( event-map, null-selector, data ) #11130", function( assert ) {
|
2012-01-13 01:30:45 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.expect( 1 );
|
2012-01-13 01:30:45 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
var $p = jQuery( "<p>Strange Pursuit</p>" ),
|
2012-01-13 01:30:45 +00:00
|
|
|
data = "bar",
|
|
|
|
map = {
|
|
|
|
"foo": function( event ) {
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
$p.on( map, null, data ).trigger( "foo" );
|
|
|
|
} );
|
2012-01-13 01:30:45 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
clone.find( "td" ).trigger( "click" );
|
2016-05-10 09:12:28 +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-08-16 06:59:58 +00:00
|
|
|
} );
|
2012-01-28 19:58:00 +00:00
|
|
|
|
2017-01-11 22:19:30 +00:00
|
|
|
QUnit.test( "checkbox state (trac-3827)", function( assert ) {
|
|
|
|
assert.expect( 16 );
|
2012-11-21 03:31:33 +00:00
|
|
|
|
2017-01-11 22:19:30 +00:00
|
|
|
var markup = jQuery( "<div class='parent'><input type=checkbox><div>" ),
|
2017-03-20 16:53:15 +00:00
|
|
|
cb = markup.find( "input" )[ 0 ];
|
2012-11-21 03:31:33 +00:00
|
|
|
|
2017-01-11 22:19:30 +00:00
|
|
|
markup.appendTo( "#qunit-fixture" );
|
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( cb ).on( "click", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( this.checked, false, "just-clicked checkbox is not checked" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2017-03-20 16:53:15 +00:00
|
|
|
markup.on( "click", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( cb.checked, false, "checkbox is not checked in bubbled event" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2012-11-21 03:31:33 +00:00
|
|
|
|
|
|
|
// Native click
|
2017-03-20 16:53:15 +00:00
|
|
|
cb.checked = true;
|
2017-01-11 22:19:30 +00:00
|
|
|
assert.equal( cb.checked, true, "native event - checkbox is initially checked" );
|
2012-11-21 03:31:33 +00:00
|
|
|
cb.click();
|
2017-01-11 22:19:30 +00:00
|
|
|
assert.equal( cb.checked, false, "native event - checkbox is no longer checked" );
|
2012-11-21 03:31:33 +00:00
|
|
|
|
|
|
|
// jQuery click
|
2017-03-20 16:53:15 +00:00
|
|
|
cb.checked = true;
|
2017-01-11 22:19:30 +00:00
|
|
|
assert.equal( cb.checked, true, "jQuery event - checkbox is initially checked" );
|
2017-03-20 16:53:15 +00:00
|
|
|
jQuery( cb ).trigger( "click" );
|
2017-01-11 22:19:30 +00:00
|
|
|
assert.equal( cb.checked, false, "jQuery event - checkbox is no longer checked" );
|
2012-11-21 03:31:33 +00:00
|
|
|
|
|
|
|
// Handlers only; checkbox state remains false
|
2017-03-20 16:53:15 +00:00
|
|
|
jQuery( cb ).triggerHandler( "click" );
|
2017-01-11 22:19:30 +00:00
|
|
|
assert.equal( cb.checked, false, "handlers only - checkbox is still unchecked" );
|
|
|
|
|
|
|
|
// Trigger parameters are preserved (trac-13353, gh-4139)
|
|
|
|
cb.checked = true;
|
|
|
|
assert.equal( cb.checked, true, "jQuery event with data - checkbox is initially checked" );
|
|
|
|
jQuery( cb ).on( "click", function( e, data ) {
|
|
|
|
assert.equal( data, "clicked", "trigger data passed to handler" );
|
|
|
|
} );
|
|
|
|
markup.on( "click", function( e, data ) {
|
|
|
|
assert.equal( data, "clicked", "trigger data passed to bubbled handler" );
|
|
|
|
} );
|
|
|
|
jQuery( cb ).trigger( "click", [ "clicked" ] );
|
|
|
|
assert.equal( cb.checked, false, "jQuery event with data - checkbox is no longer checked" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2012-11-21 03:31:33 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "event object properties on natively-triggered event", function( assert ) {
|
|
|
|
assert.expect( 3 );
|
2014-12-03 20:56:50 +00:00
|
|
|
|
|
|
|
var link = document.createElement( "a" ),
|
|
|
|
$link = jQuery( link ),
|
|
|
|
evt = document.createEvent( "MouseEvents" );
|
|
|
|
|
2019-04-29 20:56:09 +00:00
|
|
|
// Support: IE <=9 - 11+
|
2016-02-25 00:18:55 +00:00
|
|
|
// IE requires element to be in the body before it will dispatch
|
2014-12-03 20:56:50 +00:00
|
|
|
$link.appendTo( "body" ).on( "click", function( e ) {
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2014-12-03 20:56:50 +00:00
|
|
|
// Not trying to assert specific values here, just ensure the property exists
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( "detail" in e, true, "has .detail" );
|
|
|
|
assert.equal( "cancelable" in e, true, "has .cancelable" );
|
|
|
|
assert.equal( "bubbles" in e, true, "has .bubbles" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2014-12-03 20:56:50 +00:00
|
|
|
evt.initEvent( "click", true, true );
|
|
|
|
link.dispatchEvent( evt );
|
|
|
|
$link.off( "click" ).remove();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2014-12-03 20:56:50 +00:00
|
|
|
|
2013-11-01 05:36:38 +00:00
|
|
|
QUnit.test( "addProp extensions", function( assert ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.expect( 2 );
|
2011-09-25 23:56:34 +00:00
|
|
|
|
2013-11-01 05:36:38 +00:00
|
|
|
var $fixture = jQuery( "<div>" ).appendTo( "#qunit-fixture" );
|
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 ) {
|
2013-11-01 05:36:38 +00:00
|
|
|
assert.ok( !( "testProperty" in event ), "event.testProperty does not exist" );
|
2015-08-16 06:59:58 +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
|
|
|
|
2013-11-01 05:36:38 +00:00
|
|
|
jQuery.event.addProp( "testProperty", function() { return 42; } );
|
2011-09-25 23:56:34 +00:00
|
|
|
|
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 ) {
|
2013-11-01 05:36:38 +00:00
|
|
|
assert.equal( event.testProperty, 42, "event.testProperty getter was invoked" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
fireNative( $fixture[ 0 ], "click" );
|
2013-11-01 05:36:38 +00:00
|
|
|
$fixture.off( "click" );
|
2011-09-25 23:56:34 +00:00
|
|
|
|
2013-11-01 05:36:38 +00:00
|
|
|
$fixture.remove();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-07-29 00:43:23 +00:00
|
|
|
|
2015-08-16 03:45:28 +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-08-16 03:45:28 +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-08-16 06:59:58 +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-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
} );
|
2015-01-14 19:34:09 +00:00
|
|
|
|
|
|
|
fireNative( $fixture[ 0 ], "drop" );
|
|
|
|
|
|
|
|
$fixture.unbind( "dragmove drop" ).remove();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2014-12-20 23:41:24 +00:00
|
|
|
|
2015-08-16 03:45:28 +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-08-16 03:45:28 +00:00
|
|
|
assert.ok( e.type === "focusin", "got a focusin event on a document" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} ).trigger( "focusin" ).off( "focusin" );
|
2013-11-14 21:33:12 +00:00
|
|
|
|
|
|
|
jQuery( window ).on( "focusin", function( e ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( e.type === "focusin", "got a focusin event on a window" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} ).trigger( "focusin" ).off( "focusin" );
|
2013-11-14 21:33:12 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2013-11-14 21:33:12 +00:00
|
|
|
|
2016-04-10 19:42:44 +00:00
|
|
|
testIframe(
|
2015-08-16 03:45:28 +00:00
|
|
|
"focusin from an iframe",
|
|
|
|
"event/focusinCrossFrame.html",
|
2016-04-10 19:42:44 +00:00
|
|
|
function( assert, framejQuery, frameWin, frameDoc ) {
|
2015-08-16 06:59:58 +00:00
|
|
|
assert.expect( 1 );
|
2013-09-17 22:51:54 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
var input = jQuery( frameDoc ).find( "#frame-input" );
|
2013-09-17 22:51:54 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
// Create a focusin handler on the parent; shouldn't affect the iframe's fate
|
2015-09-08 00:28:56 +00:00
|
|
|
jQuery( "body" ).on( "focusin.iframeTest", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( false, "fired a focusin event in the parent document" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2013-09-17 22:51:54 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
input.on( "focusin", function() {
|
|
|
|
assert.ok( true, "fired a focusin event in the iframe" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2013-09-17 22:51:54 +00:00
|
|
|
|
2015-08-16 03:45:28 +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-08-16 03:45:28 +00:00
|
|
|
// Must manually remove handler to avoid leaks in our data store
|
|
|
|
input.remove();
|
2013-09-17 22:51:54 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
// Be sure it was removed; nothing should happen
|
|
|
|
input.trigger( "focusin" );
|
2012-04-23 19:43:26 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
// Remove body handler manually since it's outside the fixture
|
|
|
|
jQuery( "body" ).off( "focusin.iframeTest" );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2016-04-10 19:42:44 +00:00
|
|
|
testIframe(
|
2015-08-16 03:45:28 +00:00
|
|
|
"jQuery.ready promise",
|
|
|
|
"event/promiseReady.html",
|
2016-04-10 19:42:44 +00:00
|
|
|
function( assert, jQuery, window, document, isOk ) {
|
2015-08-16 06:59:58 +00:00
|
|
|
assert.expect( 1 );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( isOk, "$.when( $.ready ) works" );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2015-10-28 17:54:04 +00:00
|
|
|
// need PHP here to make the incepted IFRAME hang
|
|
|
|
if ( hasPHP ) {
|
2016-04-10 19:42:44 +00:00
|
|
|
testIframe(
|
2015-10-28 17:54:04 +00:00
|
|
|
"jQuery.ready uses interactive",
|
|
|
|
"event/interactiveReady.html",
|
2016-04-10 19:42:44 +00:00
|
|
|
function( assert, jQuery, window, document, isOk ) {
|
2015-10-28 17:54:04 +00:00
|
|
|
assert.expect( 1 );
|
|
|
|
assert.ok( isOk, "jQuery fires ready when the DOM can truly be interacted with" );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-04-10 19:42:44 +00:00
|
|
|
testIframe(
|
2015-08-16 03:45:28 +00:00
|
|
|
"Focusing iframe element",
|
|
|
|
"event/focusElem.html",
|
2016-04-10 19:42:44 +00:00
|
|
|
function( assert, jQuery, window, document, isOk ) {
|
2015-08-16 06:59:58 +00:00
|
|
|
assert.expect( 1 );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( isOk, "Focused an element in an iframe" );
|
|
|
|
}
|
|
|
|
);
|
2013-04-09 01:33:25 +00:00
|
|
|
|
2016-04-10 19:42:44 +00:00
|
|
|
testIframe(
|
2015-08-16 03:45:28 +00:00
|
|
|
"triggerHandler(onbeforeunload)",
|
|
|
|
"event/triggerunload.html",
|
2016-04-10 19:42:44 +00:00
|
|
|
function( assert, jQuery, window, document, isOk ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
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 ) {
|
2016-04-10 19:42:44 +00:00
|
|
|
testIframe(
|
2015-08-16 03:45:28 +00:00
|
|
|
"jQuery.ready synchronous load with long loading subresources",
|
|
|
|
"event/syncReady.html",
|
2016-04-10 19:42:44 +00:00
|
|
|
function( assert, jQuery, window, document, isOk ) {
|
2015-08-16 06:59:58 +00:00
|
|
|
assert.expect( 1 );
|
2015-08-16 03:45:28 +00:00
|
|
|
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-08-16 06:59:58 +00:00
|
|
|
QUnit.test( "change handler should be detached from element", function( assert ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
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-08-16 06:59:58 +00:00
|
|
|
wrapperRemoveEvent = function( elem, type, handle ) {
|
|
|
|
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" );
|
|
|
|
originRemoveEvent( elem, type, handle );
|
2013-04-09 15:45:09 +00:00
|
|
|
};
|
2012-08-23 21:46:12 +00:00
|
|
|
|
2016-05-10 09:12:28 +00:00
|
|
|
jQuery.removeEvent = wrapperRemoveEvent;
|
2012-08-23 21:46:12 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +00:00
|
|
|
} );
|
2012-08-20 00:19:44 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
QUnit.test( "trigger click on checkbox, fires change event", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2012-04-11 01:54:07 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
var check = jQuery( "#check2" );
|
2015-08-16 03:45:28 +00:00
|
|
|
var done = assert.async();
|
2011-04-05 19:55:40 +00:00
|
|
|
|
2012-04-11 01:54:07 +00:00
|
|
|
check.on( "change", function() {
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2012-04-11 01:54:07 +00:00
|
|
|
// get it?
|
2015-08-16 06:59:58 +00:00
|
|
|
check.off( "change" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "Change event fired as a result of triggered click" );
|
|
|
|
done();
|
2015-08-16 06:59:58 +00:00
|
|
|
} ).trigger( "click" );
|
|
|
|
} );
|
2012-10-16 00:22:19 +00:00
|
|
|
|
2015-08-16 03:45:28 +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-08-16 06:59:58 +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-08-16 03:45:28 +00:00
|
|
|
assert.equal( e.namespace, "bar", "namespace is bar" );
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery( e.target ).find( "div" ).each( function() {
|
2013-02-03 20:27:55 +00:00
|
|
|
jQuery( this ).triggerHandler( e );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2012-10-16 00:22:19 +00:00
|
|
|
}
|
2015-08-16 06:59:58 +00:00
|
|
|
} )
|
2013-04-09 15:45:09 +00:00
|
|
|
.on( "foo.bar2", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( false, "foo.bar2 called on trigger " + triggered + " id " + this.id );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2012-10-16 00:22:19 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 03:45:28 +00:00
|
|
|
assert.equal( triggered, 3, "foo.bar triggered" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2012-10-16 00:22:19 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "make sure events cloned correctly", function( assert ) {
|
|
|
|
assert.expect( 18 );
|
2015-07-29 15:10:04 +00:00
|
|
|
|
2012-11-18 18:53:24 +00:00
|
|
|
var clone,
|
2015-08-16 06:59:58 +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-08-16 03:45:28 +00:00
|
|
|
assert.ok( result, event.type + " on original element is fired" );
|
2012-11-18 18:53:24 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
} ).on( "click", "#firstp", function( event, result ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( result, "Click on original child element though delegation is fired" );
|
2012-11-18 18:53:24 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
} ).on( "change", "#check1", function( event, result ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( result, "Change on original child element though delegation is fired" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2012-11-18 18:53:24 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
p.on( "click", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "Click on original child element is fired" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2012-11-18 18:53:24 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
checkbox.on( "change", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "Change on original child element is fired" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2012-11-18 18:53:24 +00:00
|
|
|
|
2015-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 06:59:58 +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-08-16 03:45:28 +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-08-16 06:59:58 +00:00
|
|
|
jQuery( "<p>" ).trigger( "foo.bar" );
|
|
|
|
} catch ( e ) {
|
2013-01-31 18:01:34 +00:00
|
|
|
errored = true;
|
|
|
|
}
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( errored, false, "trigger() did not throw exception" );
|
2013-01-31 18:01:34 +00:00
|
|
|
delete String.prototype.namespace;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2013-09-19 15:34:48 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "Inline event result is returned (#13993)", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2013-09-19 15:34:48 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
var result = jQuery( "<p onclick='return 42'>hello</p>" ).triggerHandler( "click" );
|
2013-09-19 15:34:48 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( result, 42, "inline handler returned value" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2013-11-07 21:32:12 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( ".off() removes the expando when there's no more data", function( assert ) {
|
2015-07-18 18:27:11 +00:00
|
|
|
assert.expect( 2 );
|
2015-05-06 22:29:06 +00:00
|
|
|
|
|
|
|
var key,
|
|
|
|
div = jQuery( "<div/>" ).appendTo( "#qunit-fixture" );
|
|
|
|
|
|
|
|
div.on( "click", false );
|
|
|
|
div.on( "custom", function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "Custom event triggered" );
|
2015-05-06 22:29:06 +00:00
|
|
|
} );
|
|
|
|
div.trigger( "custom" );
|
|
|
|
div.off( "click custom" );
|
|
|
|
|
|
|
|
// Make sure the expando is gone
|
|
|
|
for ( key in div[ 0 ] ) {
|
|
|
|
if ( /^jQuery/.test( key ) ) {
|
2015-07-18 18:27:11 +00:00
|
|
|
assert.strictEqual(
|
|
|
|
div[ 0 ][ key ], undefined,
|
|
|
|
"Expando was not removed when there was no more data"
|
|
|
|
);
|
2015-05-06 22:29:06 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2015-05-06 22:29:06 +00:00
|
|
|
|
2016-06-02 22:33:47 +00:00
|
|
|
QUnit.test( "jQuery.Event( src ) does not require a target property", function( assert ) {
|
|
|
|
assert.expect( 2 );
|
|
|
|
|
|
|
|
var event = jQuery.Event( { type: "offtarget" } );
|
|
|
|
|
|
|
|
assert.equal( event.type, "offtarget", "correct type" );
|
|
|
|
assert.equal( event.target, undefined, "no target" );
|
|
|
|
} );
|
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "preventDefault() on focusin does not throw exception", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
2015-05-12 12:30:05 +00:00
|
|
|
|
|
|
|
var done = assert.async(),
|
|
|
|
input = jQuery( "<input/>" ).appendTo( "#form" );
|
2015-08-16 03:45:28 +00:00
|
|
|
|
2017-07-19 00:32:01 +00:00
|
|
|
input.on( "focusin", function( event ) {
|
|
|
|
if ( !done ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var exceptionCaught;
|
|
|
|
try {
|
|
|
|
event.preventDefault();
|
|
|
|
} catch ( theException ) {
|
|
|
|
exceptionCaught = theException;
|
|
|
|
}
|
2015-08-16 03:45:28 +00:00
|
|
|
|
2017-07-19 00:32:01 +00:00
|
|
|
assert.strictEqual( exceptionCaught, undefined,
|
|
|
|
"Preventing default on focusin throws no exception" );
|
2015-08-16 03:45:28 +00:00
|
|
|
|
2017-07-19 00:32:01 +00:00
|
|
|
done();
|
|
|
|
done = null;
|
|
|
|
} );
|
|
|
|
input.trigger( "focus" );
|
2017-07-19 15:45:03 +00:00
|
|
|
|
2017-07-20 17:16:04 +00:00
|
|
|
// DOM focus is unreliable in TestSwarm; set a simulated event workaround timeout
|
2017-07-19 00:49:47 +00:00
|
|
|
setTimeout( function() {
|
|
|
|
if ( !done ) {
|
|
|
|
return;
|
|
|
|
}
|
2017-07-20 17:16:04 +00:00
|
|
|
input[ 0 ].addEventListener( "click", function( nativeEvent ) {
|
|
|
|
jQuery.event.simulate( "focusin", this, jQuery.event.fix( nativeEvent ) );
|
|
|
|
} );
|
|
|
|
input[ 0 ].click();
|
2017-07-19 15:45:03 +00:00
|
|
|
}, QUnit.config.testTimeout / 4 || 1000 );
|
2015-05-12 12:30:05 +00:00
|
|
|
} );
|
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "Donor event interference", function( assert ) {
|
2017-07-20 17:16:04 +00:00
|
|
|
assert.expect( 8 );
|
|
|
|
|
|
|
|
var outer = jQuery(
|
|
|
|
"<div id='donor-outer'>" +
|
|
|
|
"<form id='donor-form'>" +
|
|
|
|
"<input id='donor-input' type='checkbox' />" +
|
|
|
|
"</form>" +
|
|
|
|
"</div>"
|
|
|
|
).appendTo( "#qunit-fixture" ),
|
|
|
|
input = jQuery( "#donor-input" );
|
|
|
|
|
|
|
|
input.on( "click", function( event ) {
|
|
|
|
assert.equal( event.type, "click", "click event at input" );
|
|
|
|
assert.ok( !event.isPropagationStopped(), "click event at input is still propagating" );
|
|
|
|
assert.equal( typeof event.originalEvent, "object",
|
|
|
|
"click event at input has originalEvent property" );
|
|
|
|
} );
|
|
|
|
outer.on( "click", function( event ) {
|
|
|
|
assert.equal( event.type, "click", "click event at ancestor" );
|
|
|
|
assert.ok( !event.isPropagationStopped(), "click event at ancestor is still propagating" );
|
|
|
|
assert.equal( typeof event.originalEvent, "object",
|
|
|
|
"click event at ancestor has originalEvent property" );
|
|
|
|
} );
|
|
|
|
input.on( "change", function( event ) {
|
|
|
|
assert.equal( event.type, "change", "change event at input" );
|
|
|
|
assert.equal( typeof event.originalEvent, "object",
|
|
|
|
"change event at input has originalEvent property" );
|
2015-05-12 12:30:05 +00:00
|
|
|
event.stopPropagation();
|
|
|
|
} );
|
2017-07-20 17:16:04 +00:00
|
|
|
input[ 0 ].click();
|
2015-05-20 15:09:46 +00:00
|
|
|
} );
|
|
|
|
|
2016-05-19 17:56:39 +00:00
|
|
|
QUnit.test(
|
2017-07-20 18:17:09 +00:00
|
|
|
"simulated events shouldn't forward stopPropagation/preventDefault methods",
|
2016-05-19 17:56:39 +00:00
|
|
|
function( assert ) {
|
|
|
|
assert.expect( 3 );
|
|
|
|
|
2017-07-20 18:17:09 +00:00
|
|
|
var outer = jQuery(
|
2017-07-20 17:16:04 +00:00
|
|
|
"<div id='donor-outer'>" +
|
|
|
|
"<form id='donor-form'>" +
|
|
|
|
"<input id='donor-input' type='checkbox' />" +
|
|
|
|
"</form>" +
|
|
|
|
"</div>"
|
|
|
|
).appendTo( "#qunit-fixture" ),
|
|
|
|
input = jQuery( "#donor-input" ),
|
2017-07-20 18:17:09 +00:00
|
|
|
spy = {};
|
2016-05-19 17:56:39 +00:00
|
|
|
|
2017-07-20 18:17:09 +00:00
|
|
|
jQuery( "#donor-form" )
|
|
|
|
.on( "simulated", function( event ) {
|
2017-07-20 17:16:04 +00:00
|
|
|
spy.prevent = sinon.stub( event.originalEvent, "preventDefault" );
|
2016-05-19 17:56:39 +00:00
|
|
|
event.preventDefault();
|
|
|
|
} )
|
2017-07-20 18:17:09 +00:00
|
|
|
.on( "simulated", function( event ) {
|
2017-07-20 17:16:04 +00:00
|
|
|
spy.stop = sinon.stub( event.originalEvent, "stopPropagation" );
|
2016-05-19 17:56:39 +00:00
|
|
|
event.stopPropagation();
|
|
|
|
} )
|
2017-07-20 18:17:09 +00:00
|
|
|
.on( "simulated", function( event ) {
|
2017-07-20 17:16:04 +00:00
|
|
|
spy.immediate = sinon.stub( event.originalEvent, "stopImmediatePropagation" );
|
2016-05-19 17:56:39 +00:00
|
|
|
event.stopImmediatePropagation();
|
2017-07-20 18:17:09 +00:00
|
|
|
} )
|
|
|
|
.on( "simulated", function( event ) {
|
|
|
|
assert.ok( false, "simulated event immediate propagation stopped" );
|
2016-05-19 17:56:39 +00:00
|
|
|
} );
|
2017-07-20 18:17:09 +00:00
|
|
|
outer
|
|
|
|
.on( "simulated", function( event ) {
|
|
|
|
assert.ok( false, "simulated event propagation stopped" );
|
2017-07-20 17:16:04 +00:00
|
|
|
} );
|
2017-07-20 18:17:09 +00:00
|
|
|
|
|
|
|
// Force a simulated event
|
|
|
|
input[ 0 ].addEventListener( "click", function( nativeEvent ) {
|
|
|
|
jQuery.event.simulate( "simulated", this, jQuery.event.fix( nativeEvent ) );
|
|
|
|
} );
|
|
|
|
input[ 0 ].click();
|
|
|
|
|
|
|
|
assert.strictEqual( spy.prevent.called, false, "Native preventDefault not called" );
|
|
|
|
assert.strictEqual( spy.stop.called, false, "Native stopPropagation not called" );
|
|
|
|
assert.strictEqual( spy.immediate.called, false,
|
|
|
|
"Native stopImmediatePropagation not called" );
|
2016-05-19 17:56:39 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2017-07-20 17:16:04 +00:00
|
|
|
QUnit.test( "originalEvent type of simulated event", function( assert ) {
|
|
|
|
assert.expect( 2 );
|
2015-05-20 15:09:46 +00:00
|
|
|
|
2017-01-11 22:19:30 +00:00
|
|
|
var outer = jQuery(
|
2017-07-20 17:16:04 +00:00
|
|
|
"<div id='donor-outer'>" +
|
|
|
|
"<form id='donor-form'>" +
|
2017-01-11 22:19:30 +00:00
|
|
|
"<input id='donor-input' type='text' />" +
|
2017-07-20 17:16:04 +00:00
|
|
|
"</form>" +
|
|
|
|
"</div>"
|
|
|
|
).appendTo( "#qunit-fixture" ),
|
|
|
|
input = jQuery( "#donor-input" ),
|
2017-01-11 22:19:30 +00:00
|
|
|
done = assert.async(),
|
2017-07-20 17:16:04 +00:00
|
|
|
finish = function() {
|
|
|
|
|
|
|
|
// Remove jQuery handlers to ensure removal of capturing handlers on the document
|
|
|
|
outer.off( "focusin" );
|
|
|
|
|
|
|
|
done();
|
|
|
|
};
|
2016-05-10 09:12:28 +00:00
|
|
|
|
2017-07-20 17:16:04 +00:00
|
|
|
outer.on( "focusin", function( event ) {
|
|
|
|
assert.equal( event.type, "focusin", "focusin event at ancestor" );
|
2017-01-11 22:19:30 +00:00
|
|
|
assert.equal( event.originalEvent.type, "click",
|
2017-07-20 17:16:04 +00:00
|
|
|
"focus event at ancestor has correct originalEvent type" );
|
|
|
|
setTimeout( finish );
|
2015-05-20 15:09:46 +00:00
|
|
|
} );
|
2015-05-12 12:30:05 +00:00
|
|
|
|
2017-01-11 22:19:30 +00:00
|
|
|
input[ 0 ].addEventListener( "click", function( nativeEvent ) {
|
|
|
|
jQuery.event.simulate( "focusin", this, jQuery.event.fix( nativeEvent ) );
|
|
|
|
} );
|
|
|
|
input[ 0 ].click();
|
2016-05-19 17:56:39 +00:00
|
|
|
} );
|
2015-11-06 14:59:14 +00:00
|
|
|
|
2017-03-20 16:54:38 +00:00
|
|
|
QUnit.test( "trigger('click') on radio passes extra params", function( assert ) {
|
|
|
|
assert.expect( 1 );
|
|
|
|
var $radio = jQuery( "<input type='radio' />" ).appendTo( "#qunit-fixture" )
|
|
|
|
.on( "click", function( e, data ) {
|
|
|
|
assert.ok( data, "Trigger data is passed to radio click handler" );
|
|
|
|
} );
|
|
|
|
|
|
|
|
$radio.trigger( "click", [ true ] );
|
|
|
|
} );
|
|
|
|
|
2019-04-29 19:13:36 +00:00
|
|
|
QUnit.test( "focusout/focusin support", function( assert ) {
|
|
|
|
assert.expect( 6 );
|
|
|
|
|
|
|
|
var focus,
|
|
|
|
parent = jQuery( "<div>" ),
|
|
|
|
input = jQuery( "<input>" ),
|
2019-04-29 19:40:36 +00:00
|
|
|
inputExternal = jQuery( "<input>" ),
|
|
|
|
|
|
|
|
// Support: IE <=9 - 11+
|
|
|
|
// focus and blur events are asynchronous; this is the resulting mess.
|
|
|
|
// The browser window must be topmost for this to work properly!!
|
|
|
|
done = assert.async();
|
2015-07-29 15:10:04 +00:00
|
|
|
|
2019-04-29 19:13:36 +00:00
|
|
|
parent.append( input );
|
|
|
|
jQuery( "#qunit-fixture" ).append( parent ).append( inputExternal );
|
|
|
|
|
2019-04-29 19:40:36 +00:00
|
|
|
// initially, lose focus
|
|
|
|
inputExternal[ 0 ].focus();
|
2013-11-07 21:32:12 +00:00
|
|
|
|
2019-04-29 19:40:36 +00:00
|
|
|
setTimeout( function() {
|
|
|
|
parent
|
|
|
|
.on( "focus", function() {
|
|
|
|
assert.ok( false, "parent: focus not fired" );
|
|
|
|
} )
|
|
|
|
.on( "focusin", function() {
|
|
|
|
assert.ok( true, "parent: focusin fired" );
|
|
|
|
} )
|
|
|
|
.on( "blur", function() {
|
|
|
|
assert.ok( false, "parent: blur not fired" );
|
|
|
|
} )
|
|
|
|
.on( "focusout", function() {
|
|
|
|
assert.ok( true, "parent: focusout fired" );
|
|
|
|
} );
|
2013-11-07 21:32:12 +00:00
|
|
|
|
2019-04-29 19:40:36 +00:00
|
|
|
input
|
|
|
|
.on( "focus", function() {
|
|
|
|
assert.ok( true, "element: focus fired" );
|
|
|
|
} )
|
|
|
|
.on( "focusin", function() {
|
|
|
|
assert.ok( true, "element: focusin fired" );
|
|
|
|
focus = true;
|
|
|
|
} )
|
|
|
|
.on( "blur", function() {
|
|
|
|
assert.ok( true, "parent: blur fired" );
|
|
|
|
} )
|
|
|
|
.on( "focusout", function() {
|
|
|
|
assert.ok( true, "element: focusout fired" );
|
|
|
|
} );
|
2013-11-07 21:32:12 +00:00
|
|
|
|
2019-04-29 19:40:36 +00:00
|
|
|
// gain focus
|
|
|
|
input[ 0 ].focus();
|
|
|
|
|
|
|
|
// then lose it
|
|
|
|
inputExternal[ 0 ].focus();
|
|
|
|
|
|
|
|
setTimeout( function() {
|
|
|
|
|
|
|
|
// DOM focus is unreliable in TestSwarm
|
|
|
|
if ( QUnit.isSwarm && !focus ) {
|
|
|
|
assert.ok( true, "GAP: Could not observe focus change" );
|
|
|
|
assert.ok( true, "GAP: Could not observe focus change" );
|
|
|
|
assert.ok( true, "GAP: Could not observe focus change" );
|
|
|
|
assert.ok( true, "GAP: Could not observe focus change" );
|
|
|
|
assert.ok( true, "GAP: Could not observe focus change" );
|
|
|
|
assert.ok( true, "GAP: Could not observe focus change" );
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
parent.off();
|
|
|
|
input.off();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}, 50 );
|
|
|
|
}, 50 );
|
2017-07-20 18:17:09 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( "focus-blur order (#12868)", function( assert ) {
|
|
|
|
assert.expect( 5 );
|
2013-11-07 21:32:12 +00:00
|
|
|
|
2017-07-20 18:17:09 +00:00
|
|
|
var order,
|
|
|
|
$text = jQuery( "#text1" ),
|
2017-01-11 22:19:30 +00:00
|
|
|
$radio = jQuery( "#radio1" ),
|
2013-11-07 21:32:12 +00:00
|
|
|
|
2017-01-11 22:19:30 +00:00
|
|
|
// Support: IE <=9 - 11+
|
|
|
|
// focus and blur events are asynchronous; this is the resulting mess.
|
|
|
|
// The browser window must be topmost for this to work properly!!
|
2017-07-20 18:17:09 +00:00
|
|
|
done = assert.async();
|
|
|
|
|
|
|
|
$radio[ 0 ].focus();
|
|
|
|
|
|
|
|
setTimeout( function() {
|
|
|
|
|
|
|
|
$text
|
|
|
|
.on( "focus", function() {
|
|
|
|
assert.equal( order++, 1, "text focus" );
|
|
|
|
} )
|
|
|
|
.on( "blur", function() {
|
|
|
|
assert.equal( order++, 0, "text blur" );
|
|
|
|
} );
|
|
|
|
$radio
|
|
|
|
.on( "focus", function() {
|
|
|
|
assert.equal( order++, 1, "radio focus" );
|
|
|
|
} )
|
|
|
|
.on( "blur", function() {
|
|
|
|
assert.equal( order++, 0, "radio blur" );
|
|
|
|
} );
|
2013-11-07 21:32:12 +00:00
|
|
|
|
2017-07-20 18:17:09 +00:00
|
|
|
// Enabled input getting focus
|
|
|
|
order = 0;
|
|
|
|
assert.equal( document.activeElement, $radio[ 0 ], "radio has focus" );
|
|
|
|
$text.trigger( "focus" );
|
2013-11-07 21:32:12 +00:00
|
|
|
setTimeout( function() {
|
|
|
|
|
2017-07-20 18:17:09 +00:00
|
|
|
// DOM focus is unreliable in TestSwarm
|
2019-04-16 04:47:02 +00:00
|
|
|
if ( QUnit.isSwarm && order === 0 ) {
|
2017-07-20 18:17:09 +00:00
|
|
|
assert.ok( true, "GAP: Could not observe focus change" );
|
|
|
|
assert.ok( true, "GAP: Could not observe focus change" );
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.equal( document.activeElement, $text[ 0 ], "text has focus" );
|
2013-11-07 21:32:12 +00:00
|
|
|
|
2017-07-20 18:17:09 +00:00
|
|
|
// Run handlers without native method on an input
|
|
|
|
order = 1;
|
|
|
|
$radio.triggerHandler( "focus" );
|
|
|
|
|
|
|
|
// Clean up
|
|
|
|
$text.off();
|
|
|
|
$radio.off();
|
|
|
|
done();
|
2013-11-07 21:32:12 +00:00
|
|
|
}, 50 );
|
2017-07-20 18:17:09 +00:00
|
|
|
}, 50 );
|
|
|
|
} );
|
2017-01-11 22:19:30 +00:00
|
|
|
|
2019-04-29 17:18:08 +00:00
|
|
|
QUnit.test( "Event handling works with multiple async focus events (gh-4350)", function( assert ) {
|
|
|
|
assert.expect( 3 );
|
|
|
|
|
|
|
|
var remaining = 3,
|
|
|
|
input = jQuery( "#name" ),
|
|
|
|
|
|
|
|
// Support: IE <=9 - 11+
|
|
|
|
// focus and blur events are asynchronous; this is the resulting mess.
|
|
|
|
// The browser window must be topmost for this to work properly!!
|
|
|
|
done = assert.async();
|
|
|
|
|
|
|
|
input
|
|
|
|
.on( "focus", function() {
|
|
|
|
remaining--;
|
|
|
|
assert.ok( true, "received focus event, expecting " + remaining + " more" );
|
|
|
|
if ( remaining > 0 ) {
|
|
|
|
input.trigger( "blur" );
|
|
|
|
} else {
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
} )
|
|
|
|
.on( "blur", function() {
|
|
|
|
setTimeout( function() {
|
|
|
|
input.trigger( "focus" );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
|
|
|
// gain focus
|
|
|
|
input.trigger( "focus" );
|
|
|
|
|
|
|
|
// DOM focus is unreliable in TestSwarm
|
|
|
|
setTimeout( function() {
|
|
|
|
if ( QUnit.isSwarm && remaining === 3 ) {
|
|
|
|
assert.ok( true, "GAP: Could not observe focus change" );
|
|
|
|
assert.ok( true, "GAP: Could not observe focus change" );
|
|
|
|
assert.ok( true, "GAP: Could not observe focus change" );
|
|
|
|
setTimeout( function() {
|
|
|
|
done();
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
|
2017-01-11 22:19:30 +00:00
|
|
|
QUnit.test( "native-backed events preserve trigger data (gh-1741, gh-4139)", function( assert ) {
|
|
|
|
assert.expect( 17 );
|
|
|
|
|
|
|
|
var parent = supportjQuery(
|
|
|
|
"<div class='parent'><input type='checkbox'><input type='radio'></div>"
|
|
|
|
).appendTo( "#qunit-fixture" ),
|
|
|
|
targets = jQuery( parent[ 0 ].childNodes ),
|
|
|
|
checkbox = jQuery( targets[ 0 ] ),
|
|
|
|
data = [ "arg1", "arg2" ],
|
|
|
|
slice = data.slice,
|
|
|
|
|
|
|
|
// Support: IE <=9 - 11+
|
|
|
|
// focus and blur events are asynchronous; this is the resulting mess.
|
|
|
|
// The browser window must be topmost for this to work properly!!
|
|
|
|
done = assert.async();
|
|
|
|
|
|
|
|
// click (gh-4139)
|
|
|
|
assert.strictEqual( targets[ 0 ].checked, false, "checkbox unchecked before click" );
|
|
|
|
assert.strictEqual( targets[ 1 ].checked, false, "radio unchecked before click" );
|
|
|
|
targets.add( parent ).on( "click", function( event ) {
|
|
|
|
var type = event.target.type,
|
|
|
|
level = event.currentTarget === parent[ 0 ] ? "parent" : "";
|
|
|
|
assert.strictEqual( event.target.checked, true,
|
|
|
|
type + " toggled before invoking " + level + " handler" );
|
|
|
|
assert.deepEqual( slice.call( arguments, 1 ), data,
|
|
|
|
type + " " + level + " handler received correct data" );
|
|
|
|
} );
|
|
|
|
targets.trigger( "click", data );
|
|
|
|
assert.strictEqual( targets[ 0 ].checked, true,
|
|
|
|
"checkbox toggled after click (default action)" );
|
|
|
|
assert.strictEqual( targets[ 1 ].checked, true,
|
|
|
|
"radio toggled after event (default action)" );
|
|
|
|
|
|
|
|
// focus (gh-1741)
|
|
|
|
assert.notEqual( document.activeElement, checkbox[ 0 ],
|
|
|
|
"element not focused before focus event" );
|
|
|
|
checkbox.on( "focus blur", function( event ) {
|
|
|
|
var type = event.type;
|
|
|
|
assert.deepEqual( slice.call( arguments, 1 ), data,
|
|
|
|
type + " handler received correct data" );
|
|
|
|
} );
|
|
|
|
checkbox.trigger( "focus", data );
|
|
|
|
setTimeout( function() {
|
|
|
|
assert.strictEqual( document.activeElement, checkbox[ 0 ],
|
|
|
|
"element focused after focus event (default action)" );
|
|
|
|
checkbox.trigger( "blur", data );
|
|
|
|
setTimeout( function() {
|
|
|
|
assert.notEqual( document.activeElement, checkbox[ 0 ],
|
|
|
|
"element not focused after blur event (default action)" );
|
|
|
|
done();
|
|
|
|
}, 50 );
|
|
|
|
}, 50 );
|
|
|
|
} );
|
|
|
|
|
|
|
|
// TODO replace with an adaptation of
|
|
|
|
// https://github.com/jquery/jquery/pull/1367/files#diff-a215316abbaabdf71857809e8673ea28R2464
|
|
|
|
( function() {
|
|
|
|
supportjQuery.each(
|
|
|
|
{
|
|
|
|
checkbox: "<input type='checkbox'>",
|
|
|
|
radio: "<input type='radio'>"
|
|
|
|
},
|
|
|
|
makeTestFor3751
|
|
|
|
);
|
|
|
|
|
|
|
|
function makeTestFor3751( type, html ) {
|
|
|
|
var testName = "native-backed namespaced clicks are handled correctly (gh-3751) - " + type;
|
|
|
|
QUnit.test( testName, function( assert ) {
|
|
|
|
assert.expect( 2 );
|
|
|
|
|
|
|
|
var parent = supportjQuery( "<div class='parent'>" + html + "</div>" ),
|
|
|
|
target = jQuery( parent[ 0 ].firstChild );
|
|
|
|
|
|
|
|
parent.appendTo( "#qunit-fixture" );
|
|
|
|
|
|
|
|
target.add( parent )
|
|
|
|
.on( "click.notFired", function( event ) {
|
|
|
|
assert.ok( false, "namespaced event should not be received" +
|
|
|
|
" by wrong-namespace listener at " + event.currentTarget.nodeName );
|
|
|
|
} )
|
|
|
|
.on( "click.fired", function( event ) {
|
|
|
|
assert.equal( event.target.checked, true,
|
|
|
|
"toggled before invoking handler at " + event.currentTarget.nodeName );
|
|
|
|
} )
|
|
|
|
.on( "click", function( event ) {
|
|
|
|
assert.ok( false, "namespaced event should not be received" +
|
|
|
|
" by non-namespaced listener at " + event.currentTarget.nodeName );
|
|
|
|
} );
|
|
|
|
|
|
|
|
target.trigger( "click.fired" );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
} )();
|