Commit 84d2307e authored by Dave Methvin's avatar Dave Methvin

Fix #10563. Ensure event.currentTarget==this if delegated.

Now, event.delegateTarget is always the element where the event was handled, regardless of whether delegated handlers are attached.
parent 560c33b3
...@@ -412,7 +412,7 @@ jQuery.event = { ...@@ -412,7 +412,7 @@ jQuery.event = {
// Use the fix-ed jQuery.Event rather than the (read-only) native event // Use the fix-ed jQuery.Event rather than the (read-only) native event
args[0] = event; args[0] = event;
event.currentTarget = this; event.delegateTarget = this;
// Determine handlers that should run if there are delegated events // Determine handlers that should run if there are delegated events
// Avoid disabled elements in IE (#6911) and non-left-click bubbling in Firefox (#3861) // Avoid disabled elements in IE (#6911) and non-left-click bubbling in Firefox (#3861)
...@@ -447,8 +447,11 @@ jQuery.event = { ...@@ -447,8 +447,11 @@ jQuery.event = {
handlerQueue.push({ elem: this, matches: handlers.slice( delegateCount ) }); handlerQueue.push({ elem: this, matches: handlers.slice( delegateCount ) });
} }
// Run delegates first; they may want to stop propagation beneath us
for ( i = 0; i < handlerQueue.length && !event.isPropagationStopped(); i++ ) { for ( i = 0; i < handlerQueue.length && !event.isPropagationStopped(); i++ ) {
matched = handlerQueue[ i ]; matched = handlerQueue[ i ];
event.currentTarget = matched.elem;
for ( j = 0; j < matched.matches.length && !event.isImmediatePropagationStopped(); j++ ) { for ( j = 0; j < matched.matches.length && !event.isImmediatePropagationStopped(); j++ ) {
handleObj = matched.matches[ j ]; handleObj = matched.matches[ j ];
...@@ -456,9 +459,6 @@ jQuery.event = { ...@@ -456,9 +459,6 @@ jQuery.event = {
// 2) have namespace(s) a subset or equal to those in the bound event (both can have no namespace). // 2) have namespace(s) a subset or equal to those in the bound event (both can have no namespace).
if ( run_all || (!event.namespace && !handleObj.namespace) || event.namespace_re && event.namespace_re.test( handleObj.namespace ) ) { if ( run_all || (!event.namespace && !handleObj.namespace) || event.namespace_re && event.namespace_re.test( handleObj.namespace ) ) {
// Pass in a reference to the handler function itself
// So that we can later remove it
event.handler = handleObj.handler;
event.data = handleObj.data; event.data = handleObj.data;
event.handleObj = handleObj; event.handleObj = handleObj;
...@@ -929,7 +929,7 @@ jQuery.fn.extend({ ...@@ -929,7 +929,7 @@ jQuery.fn.extend({
if ( types && types.preventDefault && types.handleObj ) { if ( types && types.preventDefault && types.handleObj ) {
// ( event ) dispatched jQuery.Event // ( event ) dispatched jQuery.Event
var handleObj = types.handleObj; var handleObj = types.handleObj;
jQuery( types.currentTarget ).off( jQuery( types.delegateTarget ).off(
handleObj.namespace? handleObj.type + "." + handleObj.namespace : handleObj.type, handleObj.namespace? handleObj.type + "." + handleObj.namespace : handleObj.type,
handleObj.selector, handleObj.selector,
handleObj.handler handleObj.handler
......
...@@ -1060,7 +1060,7 @@ test("trigger(eventObject, [data], [fn])", function() { ...@@ -1060,7 +1060,7 @@ test("trigger(eventObject, [data], [fn])", function() {
// Tries bubbling // Tries bubbling
equals( e.type, "foo", "Verify event type when passed passing an event object" ); equals( e.type, "foo", "Verify event type when passed passing an event object" );
equals( e.target.id, "child", "Verify event.target when passed passing an event object" ); equals( e.target.id, "child", "Verify event.target when passed passing an event object" );
equals( e.currentTarget.id, "par", "Verify event.target when passed passing an event object" ); equals( e.currentTarget.id, "par", "Verify event.currentTarget when passed passing an event object" );
equals( e.secret, "boo!", "Verify event object's custom attribute when passed passing an event object" ); equals( e.secret, "boo!", "Verify event object's custom attribute when passed passing an event object" );
}); });
...@@ -1160,18 +1160,20 @@ test("jQuery.Event( type, props )", function() { ...@@ -1160,18 +1160,20 @@ test("jQuery.Event( type, props )", function() {
}); });
test("jQuery.Event.currentTarget", function(){ test("jQuery.Event.currentTarget", function(){
expect(1); expect(2);
var counter = 0, jQuery('<div><p><button>shiny</button></p></div>')
$elem = jQuery("<button>a</button>").click(function(e){ .on( "click", "p", function( e ){
equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" ); equals( e.currentTarget, this, "Check delegated currentTarget on event" );
}); })
.find( "button" )
// Fake event .on( "click", function( e ){
$elem.trigger("click"); equals( e.currentTarget, this, "Check currentTarget on event" );
})
// Cleanup .click()
$elem.unbind(); .off( "click" )
.end()
.off( "click" );
}); });
test("toggle(Function, Function, ...)", function() { test("toggle(Function, Function, ...)", function() {
...@@ -1253,7 +1255,7 @@ test("toggle(Function, Function, ...)", function() { ...@@ -1253,7 +1255,7 @@ test("toggle(Function, Function, ...)", function() {
}); });
test(".live()/.die()", function() { test(".live()/.die()", function() {
expect(65); expect(66);
var submit = 0, div = 0, livea = 0, liveb = 0; var submit = 0, div = 0, livea = 0, liveb = 0;
...@@ -1476,7 +1478,8 @@ test(".live()/.die()", function() { ...@@ -1476,7 +1478,8 @@ test(".live()/.die()", function() {
// Test this, target and currentTarget are correct // Test this, target and currentTarget are correct
jQuery("span#liveSpan1").live("click", function(e){ jQuery("span#liveSpan1").live("click", function(e){
equals( this.id, "liveSpan1", "Check the this within a live handler" ); equals( this.id, "liveSpan1", "Check the this within a live handler" );
equals( e.currentTarget, document, "Check the event.currentTarget within a live handler" ); equals( e.currentTarget.id, "liveSpan1", "Check the event.currentTarget within a live handler" );
equals( e.delegateTarget, document, "Check the event.delegateTarget within a live handler" );
equals( e.target.nodeName.toUpperCase(), "A", "Check the event.target within a live handler" ); equals( e.target.nodeName.toUpperCase(), "A", "Check the event.target within a live handler" );
}); });
...@@ -1784,7 +1787,7 @@ test("live with special events", function() { ...@@ -1784,7 +1787,7 @@ test("live with special events", function() {
}); });
test(".delegate()/.undelegate()", function() { test(".delegate()/.undelegate()", function() {
expect(64); expect(65);
var submit = 0, div = 0, livea = 0, liveb = 0; var submit = 0, div = 0, livea = 0, liveb = 0;
...@@ -2008,7 +2011,8 @@ test(".delegate()/.undelegate()", function() { ...@@ -2008,7 +2011,8 @@ test(".delegate()/.undelegate()", function() {
// Test this, target and currentTarget are correct // Test this, target and currentTarget are correct
jQuery("#body").delegate("span#liveSpan1", "click", function(e){ jQuery("#body").delegate("span#liveSpan1", "click", function(e){
equals( this.id, "liveSpan1", "Check the this within a delegate handler" ); equals( this.id, "liveSpan1", "Check the this within a delegate handler" );
equals( e.currentTarget, document.body, "Check the event.currentTarget within a delegate handler" ); equals( e.currentTarget.id, "liveSpan1", "Check the event.currentTarget within a delegate handler" );
equals( e.delegateTarget, document.body, "Check the event.delegateTarget within a delegate handler" );
equals( e.target.nodeName.toUpperCase(), "A", "Check the event.target within a delegate handler" ); equals( e.target.nodeName.toUpperCase(), "A", "Check the event.target within a delegate handler" );
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment