Explicitly re-bind the events on clone. Copying over the data isn't enough. Fixes...
[jquery.git] / src / manipulation.js
index 4746dc8..27bc0b4 100644 (file)
@@ -1,11 +1,12 @@
 var rinlinejQuery = / jQuery\d+="(?:\d+|null)"/g,
        rleadingWhitespace = /^\s+/,
-       rxhtmlTag = /(<(\w+)[^>]*?)\/>/g,
-       rselfClosing = /^(?:abbr|br|col|img|input|link|meta|param|hr|area|embed)$/i,
-       rtagName = /<(\w+)/,
+       rxhtmlTag = /(<([\w:]+)[^>]*?)\/>/g,
+       rselfClosing = /^(?:area|br|col|embed|hr|img|input|link|meta|param)$/i,
+       rtagName = /<([\w:]+)/,
        rtbody = /<tbody/i,
-       fcloseTag = function(all, front, tag){
-               return rselfClosing.test(tag) ?
+       rhtml = /<|&\w+;/,
+       fcloseTag = function( all, front, tag ) {
+               return rselfClosing.test( tag ) ?
                        all :
                        front + "></" + tag + ">";
        },
@@ -16,6 +17,7 @@ var rinlinejQuery = / jQuery\d+="(?:\d+|null)"/g,
                tr: [ 2, "<table><tbody>", "</tbody></table>" ],
                td: [ 3, "<table><tbody><tr>", "</tr></tbody></table>" ],
                col: [ 2, "<table><tbody></tbody><colgroup>", "</colgroup></table>" ],
+               area: [ 1, "<map>", "</map>" ],
                _default: [ 0, "", "" ]
        };
 
@@ -30,22 +32,17 @@ if ( !jQuery.support.htmlSerialize ) {
 
 jQuery.fn.extend({
        text: function( text ) {
-               if ( typeof text !== "object" && text !== undefined )
-                       return this.empty().append( (this[0] && this[0].ownerDocument || document).createTextNode( text ) );
-
-               var ret = "";
-
-               jQuery.each( text || this, function(){
-                       jQuery.each( this.childNodes, function(){
-                               if ( this.nodeType !== 8 ) {
-                                       ret += this.nodeType !== 1 ?
-                                               this.nodeValue :
-                                               jQuery.fn.text( [ this ] );
-                               }
+               if ( jQuery.isFunction(text) ) {
+                       return this.each(function() {
+                               return jQuery(this).text( text.call(this) );
                        });
-               });
+               }
 
-               return ret;
+               if ( typeof text !== "object" && text !== undefined ) {
+                       return this.empty().append( (this[0] && this[0].ownerDocument || document).createTextNode( text ) );
+               }
+
+               return jQuery.getText( this );
        },
 
        wrapAll: function( html ) {
@@ -57,13 +54,13 @@ jQuery.fn.extend({
 
                if ( this[0] ) {
                        // The elements to wrap the target around
-                       var wrap = jQuery( html, this[0].ownerDocument ).eq(0).clone();
+                       var wrap = jQuery( html, this[0].ownerDocument ).eq(0).clone(true);
 
                        if ( this[0].parentNode ) {
                                wrap.insertBefore( this[0] );
                        }
 
-                       wrap.map(function(){
+                       wrap.map(function() {
                                var elem = this;
 
                                while ( elem.firstChild && elem.firstChild.nodeType === 1 ) {
@@ -78,19 +75,27 @@ jQuery.fn.extend({
        },
 
        wrapInner: function( html ) {
-               return this.each(function(){
+               return this.each(function() {
                        jQuery( this ).contents().wrapAll( html );
                });
        },
 
        wrap: function( html ) {
-               return this.each(function(){
+               return this.each(function() {
                        jQuery( this ).wrapAll( html );
                });
        },
 
+       unwrap: function() {
+               return this.parent().each(function() {
+                       if ( !jQuery.nodeName( this, "body" ) ) {
+                               jQuery( this ).replaceWith( this.childNodes );
+                       }
+               }).end();
+       },
+
        append: function() {
-               return this.domManip(arguments, true, function(elem){
+               return this.domManip(arguments, true, function( elem ) {
                        if ( this.nodeType === 1 ) {
                                this.appendChild( elem );
                        }
@@ -98,7 +103,7 @@ jQuery.fn.extend({
        },
 
        prepend: function() {
-               return this.domManip(arguments, true, function(elem){
+               return this.domManip(arguments, true, function( elem ) {
                        if ( this.nodeType === 1 ) {
                                this.insertBefore( elem, this.firstChild );
                        }
@@ -107,33 +112,31 @@ jQuery.fn.extend({
 
        before: function() {
                if ( this[0] && this[0].parentNode ) {
-                       return this.domManip(arguments, false, function(elem){
+                       return this.domManip(arguments, false, function( elem ) {
                                this.parentNode.insertBefore( elem, this );
                        });
-               } else {
-                       var set = jQuery.isFunction(arguments[0]) ?
-                               jQuery( arguments[0]() ) :
-                               jQuery.apply(jQuery, arguments);
-
-                       return this.pushStack( set.add( this ), "before", arguments );
+               } else if ( arguments.length ) {
+                       var set = jQuery(arguments[0]);
+                       set.push.apply( set, this.toArray() );
+                       return this.pushStack( set, "before", arguments );
                }
        },
 
        after: function() {
                if ( this[0] && this[0].parentNode ) {
-                       return this.domManip(arguments, false, function(elem){
+                       return this.domManip(arguments, false, function( elem ) {
                                this.parentNode.insertBefore( elem, this.nextSibling );
                        });
-               } else {
-                       return jQuery.isFunction(arguments[0]) ?
-                               this.add( arguments[0]() ) :
-                               this.add.apply( this, arguments );
+               } else if ( arguments.length ) {
+                       var set = this.pushStack( this, "after", arguments );
+                       set.push.apply( set, jQuery(arguments[0]).toArray() );
+                       return set;
                }
        },
 
        clone: function( events ) {
                // Do the clone
-               var ret = this.map(function(){
+               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
@@ -159,21 +162,8 @@ jQuery.fn.extend({
 
                // Copy the events from the original to the clone
                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( 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++;
-                       });
+                       cloneCopyEvent( this, ret );
+                       cloneCopyEvent( this.find("*"), ret.find("*") );
                }
 
                // Return the cloned set
@@ -188,15 +178,21 @@ jQuery.fn.extend({
 
                // See if we can take a shortcut and just use innerHTML
                } else if ( typeof value === "string" && !/<script/i.test( value ) &&
+                       (jQuery.support.leadingWhitespace || !rleadingWhitespace.test( value )) &&
                        !wrapMap[ (rtagName.exec( value ) || ["", ""])[1].toLowerCase() ] ) {
 
-                       for ( var i = 0, l = this.length; i < l; i++ ) {
-                               // Remove element nodes and prevent memory leaks
-                               if ( this[i].nodeType === 1 ) {
-                                       cleanData( this[i].getElementsByTagName("*") );
+                       try {
+                               for ( var i = 0, l = this.length; i < l; i++ ) {
+                                       // Remove element nodes and prevent memory leaks
+                                       if ( this[i].nodeType === 1 ) {
+                                               cleanData( this[i].getElementsByTagName("*") );
+                                               this[i].innerHTML = value;
+                                       }
                                }
 
-                               this[i].innerHTML = value;
+                       // If using innerHTML throws an exception, use the fallback method
+                       } catch(e) {
+                               this.empty().append( value );
                        }
 
                } else {
@@ -208,7 +204,17 @@ jQuery.fn.extend({
 
        replaceWith: function( value ) {
                if ( this[0] && this[0].parentNode ) {
-                       return this.after( value ).remove();
+                       return this.each(function() {
+                               var next = this.nextSibling, parent = this.parentNode;
+
+                               jQuery(this).remove();
+
+                               if ( next ) {
+                                       jQuery(next).before( value );
+                               } else {
+                                       jQuery(parent).append( value );
+                               }
+                       });
                } else {
                        return this.pushStack( jQuery(jQuery.isFunction(value) ? value() : value), "replaceWith", value );
                }
@@ -233,7 +239,7 @@ jQuery.fn.extend({
                        if ( args[0] && args[0].parentNode && args[0].parentNode.nodeType === 11 ) {
                                results = { fragment: args[0].parentNode };
                        } else {
-                               results = buildFragment( args, this[0], scripts );
+                               results = buildFragment( args, this, scripts );
                        }
 
                        first = results.fragment.firstChild;
@@ -269,7 +275,30 @@ jQuery.fn.extend({
        }
 });
 
-function buildFragment(args, nodes, scripts){
+function cloneCopyEvent(orig, ret) {
+       var i = 0;
+
+       ret.each(function() {
+               if ( this.nodeName !== (orig[i] && orig[i].nodeName) ) {
+                       return;
+               }
+
+               var oldData = jQuery.data( orig[i++] ), curData = jQuery.data( this, oldData ), events = oldData.events;
+
+               if ( events ) {
+                       delete curData.handle;
+                       curData.events = {};
+
+                       for ( var type in events ) {
+                               for ( var handler in events[ type ] ) {
+                                       jQuery.event.add( this, type, events[ type ][ handler ], events[ type ][ handler ].data );
+                               }
+                       }
+               }
+       });
+}
+
+function buildFragment( args, nodes, scripts ) {
        var fragment, cacheable, cached, cacheresults, doc;
 
        if ( args.length === 1 && typeof args[0] === "string" && args[0].length < 512 && args[0].indexOf("<option") < 0 ) {
@@ -304,7 +333,7 @@ jQuery.each({
        insertBefore: "before",
        insertAfter: "after",
        replaceAll: "replaceWith"
-}, function(name, original){
+}, function( name, original ) {
        jQuery.fn[ name ] = function( selector ) {
                var ret = [], insert = jQuery( selector );
 
@@ -320,7 +349,7 @@ jQuery.each({
 jQuery.each({
        // keepData is for internal use only--do not document
        remove: function( selector, keepData ) {
-               if ( !selector || jQuery.multiFilter( selector, [ this ] ).length ) {
+               if ( !selector || jQuery.filter( selector, [ this ] ).length ) {
                        if ( !keepData && this.nodeType === 1 ) {
                                cleanData( this.getElementsByTagName("*") );
                                cleanData( [ this ] );
@@ -343,8 +372,8 @@ jQuery.each({
                        this.removeChild( this.firstChild );
                }
        }
-}, function(name, fn){
-       jQuery.fn[ name ] = function(){
+}, function( name, fn ) {
+       jQuery.fn[ name ] = function() {
                return this.each( fn, arguments );
        };
 });
@@ -358,24 +387,30 @@ jQuery.extend({
                        context = context.ownerDocument || context[0] && context[0].ownerDocument || document;
                }
 
-               var ret = [], div = context.createElement("div");
+               var ret = [];
 
-               jQuery.each(elems, function(i, elem){
+               jQuery.each(elems, function( i, elem ) {
                        if ( typeof elem === "number" ) {
-                               elem += '';
+                               elem += "";
                        }
 
-                       if ( !elem ) { return; }
+                       if ( !elem ) {
+                               return;
+                       }
 
                        // Convert html string into DOM nodes
-                       if ( typeof elem === "string" ) {
+                       if ( typeof elem === "string" && !rhtml.test( elem ) ) {
+                               elem = context.createTextNode( elem );
+
+                       } else if ( typeof elem === "string" ) {
                                // Fix "XHTML"-style tags in all browsers
                                elem = elem.replace(rxhtmlTag, fcloseTag);
 
                                // Trim whitespace, otherwise indexOf won't work as expected
                                var tag = (rtagName.exec( elem ) || ["", ""])[1].toLowerCase(),
                                        wrap = wrapMap[ tag ] || wrapMap._default,
-                                       depth = wrap[0];
+                                       depth = wrap[0],
+                                       div = context.createElement("div");
 
                                // Go to html and back, then peel off extra wrappers
                                div.innerHTML = wrap[1] + elem + wrap[2];
@@ -394,7 +429,7 @@ jQuery.extend({
                                                        div.firstChild && div.firstChild.childNodes :
 
                                                        // String was a bare <thead> or <tfoot>
-                                                       wrap[1] == "<table>" && !hasBody ?
+                                                       wrap[1] === "<table>" && !hasBody ?
                                                                div.childNodes :
                                                                [];
 
@@ -440,9 +475,8 @@ jQuery.extend({
 });
 
 function cleanData( elems ) {
-       for ( var i = 0, l = elems.length; i < l; i++ ) {
-               var id = elems[i][expando];
-               if ( id ) {
+       for ( var i = 0, elem, id; (elem = elems[i]) != null; i++ ) {
+               if ( !jQuery.noData[elem.nodeName.toLowerCase()] && (id = elem[expando]) ) {
                        delete jQuery.cache[ id ];
                }
        }