Expose cleanData, make sure that all bound event handlers are properly cleaned up...
[jquery.git] / src / manipulation.js
index 2af2d7e..401cf86 100644 (file)
@@ -33,8 +33,9 @@ if ( !jQuery.support.htmlSerialize ) {
 jQuery.fn.extend({
        text: function( text ) {
                if ( jQuery.isFunction(text) ) {
-                       return this.each(function() {
-                               return jQuery(this).text( text.call(this) );
+                       return this.each(function(i) {
+                               var self = jQuery(this);
+                               return self.text( text.call(this, i, self.text()) );
                        });
                }
 
@@ -47,8 +48,8 @@ jQuery.fn.extend({
 
        wrapAll: function( html ) {
                if ( jQuery.isFunction( html ) ) {
-                       return this.each(function() {
-                               jQuery(this).wrapAll( html.apply(this, arguments) );
+                       return this.each(function(i) {
+                               jQuery(this).wrapAll( html.call(this, i) );
                        });
                }
 
@@ -76,7 +77,14 @@ jQuery.fn.extend({
 
        wrapInner: function( html ) {
                return this.each(function() {
-                       jQuery( this ).contents().wrapAll( html );
+                       var self = jQuery( this ), contents = self.contents();
+
+                       if ( contents.length ) {
+                               contents.wrapAll( html );
+
+                       } else {
+                               self.append( html );
+                       }
                });
        },
 
@@ -172,7 +180,7 @@ jQuery.fn.extend({
 
        html: function( value ) {
                if ( value === undefined ) {
-                       return this[0] ?
+                       return this[0] && this[0].nodeType === 1 ?
                                this[0].innerHTML.replace(rinlinejQuery, "") :
                                null;
 
@@ -185,7 +193,7 @@ jQuery.fn.extend({
                                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("*") );
+                                               jQuery.cleanData( this[i].getElementsByTagName("*") );
                                                this[i].innerHTML = value;
                                        }
                                }
@@ -195,6 +203,14 @@ jQuery.fn.extend({
                                this.empty().append( value );
                        }
 
+               } else if ( jQuery.isFunction( value ) ) {
+                       this.each(function(i){
+                               var self = jQuery(this), old = self.html();
+                               self.empty().append(function(){
+                                       return value.call( this, i, old );
+                               });
+                       });
+
                } else {
                        this.empty().append( value );
                }
@@ -204,6 +220,12 @@ jQuery.fn.extend({
 
        replaceWith: function( value ) {
                if ( this[0] && this[0].parentNode ) {
+                       // Make sure that the elements are removed from the DOM before they are inserted
+                       // this can help fix replacing a parent with child elements
+                       if ( !jQuery.isFunction( value ) ) {
+                               value = jQuery( value ).detach();
+                       }
+
                        return this.each(function() {
                                var next = this.nextSibling, parent = this.parentNode;
 
@@ -228,9 +250,10 @@ jQuery.fn.extend({
                var results, first, value = args[0], scripts = [];
 
                if ( jQuery.isFunction(value) ) {
-                       return this.each(function() {
-                               args[0] = value.call(this);
-                               return jQuery(this).domManip( args, table, callback );
+                       return this.each(function(i) {
+                               var self = jQuery(this);
+                               args[0] = value.call(this, i, table ? self.html() : undefined);
+                               return self.domManip( args, table, callback );
                        });
                }
 
@@ -299,7 +322,7 @@ function cloneCopyEvent(orig, ret) {
 }
 
 function buildFragment( args, nodes, scripts ) {
-       var fragment, cacheable, cached, cacheresults, doc;
+       var fragment, cacheable, cacheresults, doc;
 
        if ( args.length === 1 && typeof args[0] === "string" && args[0].length < 512 && args[0].indexOf("<option") < 0 ) {
                cacheable = true;
@@ -308,7 +331,6 @@ function buildFragment( args, nodes, scripts ) {
                        if ( cacheresults !== 1 ) {
                                fragment = cacheresults;
                        }
-                       cached = true;
                }
        }
 
@@ -351,8 +373,8 @@ jQuery.each({
        remove: function( selector, keepData ) {
                if ( !selector || jQuery.filter( selector, [ this ] ).length ) {
                        if ( !keepData && this.nodeType === 1 ) {
-                               cleanData( this.getElementsByTagName("*") );
-                               cleanData( [ this ] );
+                               jQuery.cleanData( this.getElementsByTagName("*") );
+                               jQuery.cleanData( [ this ] );
                        }
 
                        if ( this.parentNode ) {
@@ -364,7 +386,7 @@ jQuery.each({
        empty: function() {
                // Remove element nodes and prevent memory leaks
                if ( this.nodeType === 1 ) {
-                       cleanData( this.getElementsByTagName("*") );
+                       jQuery.cleanData( this.getElementsByTagName("*") );
                }
 
                // Remove any remaining nodes
@@ -471,13 +493,12 @@ jQuery.extend({
                }
 
                return ret;
-       }
-});
-
-function cleanData( elems ) {
-       for ( var i = 0, elem, id; (elem = elems[i]) != null; i++ ) {
-               if ( !jQuery.noData[elem.nodeName.toLowerCase()] && (id = elem[expando]) ) {
-                       delete jQuery.cache[ id ];
+       },
+       
+       cleanData: function( elems ) {
+               for ( var i = 0, elem, id; (elem = elems[i]) != null; i++ ) {
+                       jQuery.event.remove( elem );
+                       jQuery.removeData( elem );
                }
        }
-}
+});
\ No newline at end of file