Merge branch 'bug5566' into csnover-bug5566. Fixes #4386, #5566, #6997.
[jquery.git] / src / manipulation.js
index e09dd7e..3895a7a 100644 (file)
@@ -9,9 +9,6 @@ var rinlinejQuery = / jQuery\d+="(?:\d+|null)"/g,
        rnocache = /<(?:script|object|embed|option|style)/i,
        // checked="checked" or checked (html5)
        rchecked = /checked\s*(?:[^=]|=\s*.checked.)/i,
-       raction = /\=([^="'>\s]+\/)>/g,
-       rbodystart = /^\s*<body/i,
-       rbodyend = /<\/body>\s*$/i,
        wrapMap = {
                option: [ 1, "<select multiple='multiple'>", "</select>" ],
                legend: [ 1, "<fieldset>", "</fieldset>" ],
@@ -151,7 +148,7 @@ jQuery.fn.extend({
                        return set;
                }
        },
-       
+
        // keepData is for internal use only--do not document
        remove: function( selector, keepData ) {
                for ( var i = 0, elem; (elem = this[i]) != null; i++ ) {
@@ -166,7 +163,7 @@ jQuery.fn.extend({
                                }
                        }
                }
-               
+
                return this;
        },
 
@@ -182,39 +179,45 @@ jQuery.fn.extend({
                                elem.removeChild( elem.firstChild );
                        }
                }
-               
+
                return this;
        },
 
        clone: function( events ) {
                // Do the clone
                var ret = this.map(function() {
-                       if ( !jQuery.support.noCloneEvent && !jQuery.isXMLDoc(this) ) {
-                               // IE copies events bound via attachEvent when
-                               // using cloneNode. Calling detachEvent on the
-                               // clone will also remove the events from the orignal
-                               // In order to get around this, we use innerHTML.
-                               // Unfortunately, this means some modifications to
-                               // attributes in IE that are actually only stored
-                               // as properties will not be copied (such as the
-                               // the name attribute on an input).
-                               var html = this.outerHTML,
-                                       ownerDocument = this.ownerDocument;
-                               if ( !html ) {
-                                       var div = ownerDocument.createElement("div");
-                                       div.appendChild( this.cloneNode(true) );
-                                       html = div.innerHTML;
-                               } else if ( rbodystart.test(html) && rbodyend.test(html) ) {
-                                       html = html.replace( rbodystart, "<div>" ).replace( rbodyend, "</div>" );
-                               }
+                       var clone = this.cloneNode(true);
+                       if ( !jQuery.support.noCloneEvent && (this.nodeType === 1 || this.nodeType === 11) && !jQuery.isXMLDoc(this) ) {
+                               // IE copies events bound via attachEvent when using cloneNode.
+                               // Calling detachEvent on the clone will also remove the events
+                               // from the original. In order to get around this, we use some
+                               // proprietary methods to clear the events. Thanks to MooTools
+                               // guys for this hotness.
+                               var srcElements = jQuery(this).find('*').andSelf();
+                               jQuery(clone).find('*').andSelf().each(function (i, clone) {
+                                       // We do not need to do anything for non-Elements
+                                       if (this.nodeType !== 1) {
+                                               return;
+                                       }
 
-                               return jQuery.clean([html.replace(rinlinejQuery, "")
-                                       // Handle the case in IE 8 where action=/test/> self-closes a tag
-                                       .replace(raction, '="$1">')
-                                       .replace(rleadingWhitespace, "")], ownerDocument)[0];
-                       } else {
-                               return this.cloneNode(true);
+                                       // clearAttributes removes the attributes, but also
+                                       // removes the attachEvent events
+                                       clone.clearAttributes();
+
+                                       // mergeAttributes only merges back on the original attributes,
+                                       // not the events
+                                       clone.mergeAttributes(srcElements[i]);
+
+                                       // IE6-8 fail to clone children inside object elements that use
+                                       // the proprietary classid attribute value (rather than the type
+                                       // attribute) to identify the type of content to display
+                                       if (clone.nodeName.toLowerCase() === 'object') {
+                                               clone.outerHTML = srcElements[i].outerHTML;
+                                       }
+                               });
                        }
+
+                       return clone;
                });
 
                // Copy the events from the original to the clone
@@ -334,9 +337,9 @@ jQuery.fn.extend({
                        } else {
                                results = jQuery.buildFragment( args, this, scripts );
                        }
-                       
+
                        fragment = results.fragment;
-                       
+
                        if ( fragment.childNodes.length === 1 ) {
                                first = fragment = fragment.firstChild;
                        } else {
@@ -378,7 +381,7 @@ function cloneCopyEvent(orig, ret) {
        var i = 0;
 
        ret.each(function() {
-               if ( this.nodeName !== (orig[i] && orig[i].nodeName) ) {
+               if ( this.nodeType !== 1 || this.nodeName !== (orig[i] && orig[i].nodeName) ) {
                        return;
                }
 
@@ -444,18 +447,18 @@ jQuery.each({
                var ret = [],
                        insert = jQuery( selector ),
                        parent = this.length === 1 && this[0].parentNode;
-               
+
                if ( parent && parent.nodeType === 11 && parent.childNodes.length === 1 && insert.length === 1 ) {
                        insert[ original ]( this[0] );
                        return this;
-                       
+
                } else {
                        for ( var i = 0, l = insert.length; i < l; i++ ) {
                                var elems = (i > 0 ? this.clone(true) : this).get();
                                jQuery( insert[i] )[ original ]( elems );
                                ret = ret.concat( elems );
                        }
-               
+
                        return this.pushStack( ret, name, insert.selector );
                }
        };
@@ -543,7 +546,7 @@ jQuery.extend({
                        for ( i = 0; ret[i]; i++ ) {
                                if ( scripts && jQuery.nodeName( ret[i], "script" ) && (!ret[i].type || ret[i].type.toLowerCase() === "text/javascript") ) {
                                        scripts.push( ret[i].parentNode ? ret[i].parentNode.removeChild( ret[i] ) : ret[i] );
-                               
+
                                } else {
                                        if ( ret[i].nodeType === 1 ) {
                                                ret.splice.apply( ret, [i + 1, 0].concat(jQuery.makeArray(ret[i].getElementsByTagName("script"))) );
@@ -555,22 +558,22 @@ jQuery.extend({
 
                return ret;
        },
-       
+
        cleanData: function( elems ) {
                var data, id, cache = jQuery.cache,
                        special = jQuery.event.special,
                        deleteExpando = jQuery.support.deleteExpando;
-               
+
                for ( var i = 0, elem; (elem = elems[i]) != null; i++ ) {
                        if ( elem.nodeName && jQuery.noData[elem.nodeName.toLowerCase()] ) {
                                continue;
                        }
 
                        id = elem[ jQuery.expando ];
-                       
+
                        if ( id ) {
                                data = cache[ id ];
-                               
+
                                if ( data && data.events ) {
                                        for ( var type in data.events ) {
                                                if ( special[ type ] ) {
@@ -581,14 +584,14 @@ jQuery.extend({
                                                }
                                        }
                                }
-                               
+
                                if ( deleteExpando ) {
                                        delete elem[ jQuery.expando ];
 
                                } else if ( elem.removeAttribute ) {
                                        elem.removeAttribute( jQuery.expando );
                                }
-                               
+
                                delete cache[ id ];
                        }
                }