Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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.
  • Loading branch information...
commit 84d2307e0e20e9245523731a4450314cc0de6a95 1 parent 560c33b
Dave Methvin authored
10  src/event.js
@@ -412,7 +412,7 @@ jQuery.event = {
412 412
 
413 413
 		// Use the fix-ed jQuery.Event rather than the (read-only) native event
414 414
 		args[0] = event;
415  
-		event.currentTarget = this;
  415
+		event.delegateTarget = this;
416 416
 
417 417
 		// Determine handlers that should run if there are delegated events
418 418
 		// Avoid disabled elements in IE (#6911) and non-left-click bubbling in Firefox (#3861)
@@ -447,8 +447,11 @@ jQuery.event = {
447 447
 			handlerQueue.push({ elem: this, matches: handlers.slice( delegateCount ) });
448 448
 		}
449 449
 
  450
+		// Run delegates first; they may want to stop propagation beneath us
450 451
 		for ( i = 0; i < handlerQueue.length && !event.isPropagationStopped(); i++ ) {
451 452
 			matched = handlerQueue[ i ];
  453
+			event.currentTarget = matched.elem;
  454
+
452 455
 			for ( j = 0; j < matched.matches.length && !event.isImmediatePropagationStopped(); j++ ) {
453 456
 				handleObj = matched.matches[ j ];
454 457
 
@@ -456,9 +459,6 @@ jQuery.event = {
456 459
 				// 2) have namespace(s) a subset or equal to those in the bound event (both can have no namespace).
457 460
 				if ( run_all || (!event.namespace && !handleObj.namespace) || event.namespace_re && event.namespace_re.test( handleObj.namespace ) ) {
458 461
 
459  
-					// Pass in a reference to the handler function itself
460  
-					// So that we can later remove it
461  
-					event.handler = handleObj.handler;
462 462
 					event.data = handleObj.data;
463 463
 					event.handleObj = handleObj;
464 464
 
@@ -929,7 +929,7 @@ jQuery.fn.extend({
929 929
 		if ( types && types.preventDefault && types.handleObj ) {
930 930
 			// ( event )  dispatched jQuery.Event
931 931
 			var handleObj = types.handleObj;
932  
-			jQuery( types.currentTarget ).off( 
  932
+			jQuery( types.delegateTarget ).off( 
933 933
 				handleObj.namespace? handleObj.type + "." + handleObj.namespace : handleObj.type, 
934 934
 				handleObj.selector, 
935 935
 				handleObj.handler
38  test/unit/event.js
@@ -1060,7 +1060,7 @@ test("trigger(eventObject, [data], [fn])", function() {
1060 1060
 		// Tries bubbling
1061 1061
 		equals( e.type, "foo", "Verify event type when passed passing an event object" );
1062 1062
 		equals( e.target.id, "child", "Verify event.target when passed passing an event object" );
1063  
-		equals( e.currentTarget.id, "par", "Verify event.target when passed passing an event object" );
  1063
+		equals( e.currentTarget.id, "par", "Verify event.currentTarget when passed passing an event object" );
1064 1064
 		equals( e.secret, "boo!", "Verify event object's custom attribute when passed passing an event object" );
1065 1065
 	});
1066 1066
 
@@ -1160,18 +1160,20 @@ test("jQuery.Event( type, props )", function() {
1160 1160
 });
1161 1161
 
1162 1162
 test("jQuery.Event.currentTarget", function(){
1163  
-	expect(1);
1164  
-
1165  
-	var counter = 0,
1166  
-		$elem = jQuery("<button>a</button>").click(function(e){
1167  
-		equals( e.currentTarget, this, "Check currentTarget on "+(counter++?"native":"fake") +" event" );
1168  
-	});
1169  
-
1170  
-	// Fake event
1171  
-	$elem.trigger("click");
1172  
-
1173  
-	// Cleanup
1174  
-	$elem.unbind();
  1163
+	expect(2);
  1164
+	
  1165
+	jQuery('<div><p><button>shiny</button></p></div>')
  1166
+		.on( "click", "p", function( e ){
  1167
+				equals( e.currentTarget, this, "Check delegated currentTarget on event" );
  1168
+		})
  1169
+		.find( "button" )
  1170
+			.on( "click", function( e ){
  1171
+				equals( e.currentTarget, this, "Check currentTarget on event" );
  1172
+			})
  1173
+			.click()
  1174
+			.off( "click" )
  1175
+		.end()
  1176
+		.off( "click" );
1175 1177
 });
1176 1178
 
1177 1179
 test("toggle(Function, Function, ...)", function() {
@@ -1253,7 +1255,7 @@ test("toggle(Function, Function, ...)", function() {
1253 1255
 });
1254 1256
 
1255 1257
 test(".live()/.die()", function() {
1256  
-	expect(65);
  1258
+	expect(66);
1257 1259
 
1258 1260
 	var submit = 0, div = 0, livea = 0, liveb = 0;
1259 1261
 
@@ -1476,7 +1478,8 @@ test(".live()/.die()", function() {
1476 1478
 	// Test this, target and currentTarget are correct
1477 1479
 	jQuery("span#liveSpan1").live("click", function(e){
1478 1480
 		equals( this.id, "liveSpan1", "Check the this within a live handler" );
1479  
-		equals( e.currentTarget, document, "Check the event.currentTarget within a live handler" );
  1481
+		equals( e.currentTarget.id, "liveSpan1", "Check the event.currentTarget within a live handler" );
  1482
+		equals( e.delegateTarget, document, "Check the event.delegateTarget within a live handler" );
1480 1483
 		equals( e.target.nodeName.toUpperCase(), "A", "Check the event.target within a live handler" );
1481 1484
 	});
1482 1485
 
@@ -1784,7 +1787,7 @@ test("live with special events", function() {
1784 1787
 });
1785 1788
 
1786 1789
 test(".delegate()/.undelegate()", function() {
1787  
-	expect(64);
  1790
+	expect(65);
1788 1791
 
1789 1792
 	var submit = 0, div = 0, livea = 0, liveb = 0;
1790 1793
 
@@ -2008,7 +2011,8 @@ test(".delegate()/.undelegate()", function() {
2008 2011
 	// Test this, target and currentTarget are correct
2009 2012
 	jQuery("#body").delegate("span#liveSpan1", "click", function(e){
2010 2013
 		equals( this.id, "liveSpan1", "Check the this within a delegate handler" );
2011  
-		equals( e.currentTarget, document.body, "Check the event.currentTarget within a delegate handler" );
  2014
+		equals( e.currentTarget.id, "liveSpan1", "Check the event.currentTarget within a delegate handler" );
  2015
+		equals( e.delegateTarget, document.body, "Check the event.delegateTarget within a delegate handler" );
2012 2016
 		equals( e.target.nodeName.toUpperCase(), "A", "Check the event.target within a delegate handler" );
2013 2017
 	});
2014 2018
 

1 note on commit 84d2307

Alex Sexton

I think something might be weird with this commit. I've reopened the ticket for investigation: http://bugs.jquery.com/ticket/10563#comment:6

Please sign in to comment.
Something went wrong with that request. Please try again.