Skip to content

Commit

Permalink
Fix #11076. If .clone() won't delegate, we must remediate.
Browse files Browse the repository at this point in the history
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 633ca9c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/event.js
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/manipulation.js
Expand Up @@ -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 ] );
}
}
}
Expand Down
23 changes: 23 additions & 0 deletions test/unit/event.js
Expand Up @@ -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(
Expand Down

0 comments on commit 633ca9c

Please sign in to comment.