aboutsummaryrefslogtreecommitdiffstats
path: root/src/core.js
diff options
context:
space:
mode:
authorJohn Resig <jeresig@gmail.com>2009-02-09 14:48:15 +0000
committerJohn Resig <jeresig@gmail.com>2009-02-09 14:48:15 +0000
commitce90accc58d213fcf567ab2ca464ee9164601dc4 (patch)
tree4a9ace5c3c7616e337569b6039685e88c2f999c7 /src/core.js
parentf38648c7cd3a37f4070b77b2c92151f68aa66fef (diff)
downloadjquery-ce90accc58d213fcf567ab2ca464ee9164601dc4.tar.gz
jquery-ce90accc58d213fcf567ab2ca464ee9164601dc4.zip
Reworked the .clone() function in IE. Fixes jQuery bugs #3500 (jQuery expandos were causing extra elements to appear from using .html() cloning), #3254 (Mis-match in clone result length causes problem), and #2845 (Cloning an <object/> causes exceptions to be thrown).
Diffstat (limited to 'src/core.js')
-rw-r--r--src/core.js44
1 files changed, 24 insertions, 20 deletions
diff --git a/src/core.js b/src/core.js
index 5c37858a4..ebad0c1b6 100644
--- a/src/core.js
+++ b/src/core.js
@@ -297,33 +297,37 @@ jQuery.fn = jQuery.prototype = {
// attributes in IE that are actually only stored
// as properties will not be copied (such as the
// the name attribute on an input).
- var clone = this.cloneNode(true),
- container = document.createElement("div");
- container.appendChild(clone);
- return jQuery.clean([container.innerHTML])[0];
+ var html = this.outerHTML;
+ if ( !html ) {
+ var div = this.ownerDocument.createElement("div");
+ div.appendChild( this.cloneNode(true) );
+ html = div.innerHTML;
+ }
+
+ return jQuery.clean([html.replace(/ jQuery\d+="(?:\d+|null)"/g, "").replace(/^\s*/, "")])[0];
} else
return this.cloneNode(true);
});
- // Need to set the expando to null on the cloned set if it exists
- // removeData doesn't work here, IE removes it from the original as well
- // this is primarily for IE but the data expando shouldn't be copied over in any browser
- var clone = ret.find("*").andSelf().each(function(){
- if ( this[ expando ] !== undefined )
- this[ expando ] = null;
- });
-
// Copy the events from the original to the clone
- if ( events === true )
- this.find("*").andSelf().each(function(i){
- if (this.nodeType == 3)
+ if ( events === true ) {
+ var orig = this.find("*").andSelf(), i = 0;
+
+ ret.find("*").andSelf().each(function(){
+ if ( this.nodeName !== orig[i].nodeName )
return;
- var events = jQuery.data( this, "events" );
- for ( var type in events )
- for ( var handler in events[ type ] )
- jQuery.event.add( clone[ i ], type, events[ type ][ handler ], events[ type ][ handler ].data );
+ var events = jQuery.data( orig[i], "events" );
+
+ for ( var type in events ) {
+ for ( var handler in events[ type ] ) {
+ jQuery.event.add( this, type, events[ type ][ handler ], events[ type ][ handler ].data );
+ }
+ }
+
+ i++;
});
+ }
// Return the cloned set
return ret;
@@ -462,7 +466,7 @@ jQuery.fn = jQuery.prototype = {
html: function( value ) {
return value === undefined ?
(this[0] ?
- this[0].innerHTML :
+ this[0].innerHTML.replace(/ jQuery\d+="(?:\d+|null)"/g, "") :
null) :
this.empty().append( value );
},