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.
This commit is contained in:
Dave Methvin 2012-01-28 14:58:00 -05:00
parent 499658970b
commit 633ca9c161
3 changed files with 25 additions and 1 deletions

View File

@ -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

View File

@ -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 ] );
}
}
}

View File

@ -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(