Skip to content

Commit ce90acc

Browse files
committed
Reworked the .clone() function in IE. Fixes jQuery bugs jquery#3500 (jQuery expandos were causing extra elements to appear from using .html() cloning), jquery#3254 (Mis-match in clone result length causes problem), and jquery#2845 (Cloning an <object/> causes exceptions to be thrown).
1 parent f38648c commit ce90acc

File tree

2 files changed

+50
-21
lines changed

2 files changed

+50
-21
lines changed

src/core.js

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -297,33 +297,37 @@ jQuery.fn = jQuery.prototype = {
297297
// attributes in IE that are actually only stored
298298
// as properties will not be copied (such as the
299299
// the name attribute on an input).
300-
var clone = this.cloneNode(true),
301-
container = document.createElement("div");
302-
container.appendChild(clone);
303-
return jQuery.clean([container.innerHTML])[0];
300+
var html = this.outerHTML;
301+
if ( !html ) {
302+
var div = this.ownerDocument.createElement("div");
303+
div.appendChild( this.cloneNode(true) );
304+
html = div.innerHTML;
305+
}
306+
307+
return jQuery.clean([html.replace(/ jQuery\d+="(?:\d+|null)"/g, "").replace(/^\s*/, "")])[0];
304308
} else
305309
return this.cloneNode(true);
306310
});
307311

308-
// Need to set the expando to null on the cloned set if it exists
309-
// removeData doesn't work here, IE removes it from the original as well
310-
// this is primarily for IE but the data expando shouldn't be copied over in any browser
311-
var clone = ret.find("*").andSelf().each(function(){
312-
if ( this[ expando ] !== undefined )
313-
this[ expando ] = null;
314-
});
315-
316312
// Copy the events from the original to the clone
317-
if ( events === true )
318-
this.find("*").andSelf().each(function(i){
319-
if (this.nodeType == 3)
313+
if ( events === true ) {
314+
var orig = this.find("*").andSelf(), i = 0;
315+
316+
ret.find("*").andSelf().each(function(){
317+
if ( this.nodeName !== orig[i].nodeName )
320318
return;
321-
var events = jQuery.data( this, "events" );
322319

323-
for ( var type in events )
324-
for ( var handler in events[ type ] )
325-
jQuery.event.add( clone[ i ], type, events[ type ][ handler ], events[ type ][ handler ].data );
320+
var events = jQuery.data( orig[i], "events" );
321+
322+
for ( var type in events ) {
323+
for ( var handler in events[ type ] ) {
324+
jQuery.event.add( this, type, events[ type ][ handler ], events[ type ][ handler ].data );
325+
}
326+
}
327+
328+
i++;
326329
});
330+
}
327331

328332
// Return the cloned set
329333
return ret;
@@ -462,7 +466,7 @@ jQuery.fn = jQuery.prototype = {
462466
html: function( value ) {
463467
return value === undefined ?
464468
(this[0] ?
465-
this[0].innerHTML :
469+
this[0].innerHTML.replace(/ jQuery\d+="(?:\d+|null)"/g, "") :
466470
null) :
467471
this.empty().append( value );
468472
},

test/unit/core.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,7 @@ test("find(String)", function() {
11561156
});
11571157

11581158
test("clone()", function() {
1159-
expect(20);
1159+
expect(28);
11601160
equals( 'This is a normal link: Yahoo', jQuery('#en').text(), 'Assert text for #en' );
11611161
var clone = jQuery('#yahoo').clone();
11621162
equals( 'Try them out:Yahoo', jQuery('#first').append(clone).text(), 'Check for clone' );
@@ -1176,6 +1176,31 @@ test("clone()", function() {
11761176
// using contents will get comments regular, text, and comment nodes
11771177
var cl = jQuery("#nonnodes").contents().clone();
11781178
ok( cl.length >= 2, "Check node,textnode,comment clone works (some browsers delete comments on clone)" );
1179+
1180+
var div = jQuery("<div><ul><li>test</li></ul></div>").click(function(){
1181+
ok( true, "Bound event still exists." );
1182+
});
1183+
1184+
div = div.clone(true).clone(true);
1185+
equals( div.length, 1, "One element cloned" );
1186+
equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
1187+
div.trigger("click");
1188+
1189+
div = jQuery("<div/>").append([ document.createElement("table"), document.createElement("table") ]);
1190+
div.find("table").click(function(){
1191+
ok( true, "Bound event still exists." );
1192+
});
1193+
1194+
div = div.clone(true);
1195+
equals( div.length, 1, "One element cloned" );
1196+
equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
1197+
div.find("table:last").trigger("click");
1198+
1199+
div = jQuery("<div/>").html('<object height="355" width="425"> <param name="movie" value="http://www.youtube.com/v/JikaHBDoV3k&amp;hl=en"> <param name="wmode" value="transparent"> </object>');
1200+
1201+
div = div.clone(true);
1202+
equals( div.length, 1, "One element cloned" );
1203+
equals( div[0].nodeName.toUpperCase(), "DIV", "DIV element cloned" );
11791204
});
11801205

11811206
if (!isLocal) {

0 commit comments

Comments
 (0)