Skip to content
Permalink
Browse files

Fix #11076. If .clone() won't delegate, we must remediate.

Since `jQuery.event.add` can accept a handleObj there's no need to reiterate them as args, but we *do* need to set the `selector` variable correctly.
  • Loading branch information
dmethvin committed Jan 28, 2012
1 parent 4996589 commit 633ca9c1610c49dbb780e565f4f1202e1fe20fae
Showing with 25 additions and 1 deletion.
  1. +1 −0 src/event.js
  2. +1 −1 src/manipulation.js
  3. +23 −0 test/unit/event.js
@@ -50,6 +50,7 @@ jQuery.event = {
if ( handler.handler ) {
handleObjIn = handler;
handler = handleObjIn.handler;
selector = handleObjIn.selector;
}

// Make sure that the handler has a unique ID, used to find/remove it later
@@ -394,7 +394,7 @@ function cloneCopyEvent( src, dest ) {

for ( type in events ) {
for ( i = 0, l = events[ type ].length; i < l; i++ ) {
jQuery.event.add( dest, type + ( events[ type ][ i ].namespace ? "." : "" ) + events[ type ][ i ].namespace, events[ type ][ i ], events[ type ][ i ].data );
jQuery.event.add( dest, type, events[ type ][ i ] );
}
}
}
@@ -2657,6 +2657,29 @@ test(".on( event-map, null-selector, data ) #11130", function() {
$p.on( map, null, data ).trigger("foo");
});

test("clone() delegated events (#11076)", function() {
expect(3);

var counter = { center: 0, fold: 0, centerfold: 0 },
clicked = function( event ) {
counter[ jQuery(this).text().replace(/\s+/, "") ]++;
},
table =
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 );

clone.find("td").click();
equal( counter.center, 1, "first child" );
equal( counter.fold, 1, "last child" );
equal( counter.centerfold, 2, "all children" );

table.remove();
clone.remove();
});

test("delegated events quickIs", function() {
expect(14);
var markup = jQuery(

0 comments on commit 633ca9c

Please sign in to comment.
You can’t perform that action at this time.