Adds detach()
[jquery.git] / src / manipulation.js
index 59973de..1b8066f 100644 (file)
@@ -1,16 +1,30 @@
+var rinlinejQuery = / jQuery\d+="(?:\d+|null)"/g,
+       rleadingWhitespace = /^\s+/,
+       rsingleTag = /^<(\w+)\s*\/?>$/,
+       rxhtmlTag = /(<(\w+)[^>]*?)\/>/g,
+       rselfClosing = /^(?:abbr|br|col|img|input|link|meta|param|hr|area|embed)$/i,
+       rinsideTable = /^<(thead|tbody|tfoot|colg|cap)/,
+       rtbody = /<tbody/i,
+       fcloseTag = function(all, front, tag){
+               return rselfClosing.test(tag) ?
+                       all :
+                       front + "></" + tag + ">";
+       };
+
 jQuery.fn.extend({
        text: function( text ) {
-               if ( typeof text !== "object" && text != null )
+               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 ?
+                               if ( this.nodeType !== 8 ) {
+                                       ret += this.nodeType !== 1 ?
                                                this.nodeValue :
                                                jQuery.fn.text( [ this ] );
+                               }
                        });
                });
 
@@ -18,18 +32,26 @@ jQuery.fn.extend({
        },
 
        wrapAll: function( html ) {
+               if ( jQuery.isFunction( html ) ) {
+                       return this.each(function() {
+                               jQuery(this).wrapAll( html.apply(this, arguments) );
+                       });
+               }
+
                if ( this[0] ) {
                        // The elements to wrap the target around
-                       var wrap = jQuery( html, this[0].ownerDocument ).clone();
+                       var wrap = jQuery( html, this[0].ownerDocument ).eq(0).clone();
 
-                       if ( this[0].parentNode )
+                       if ( this[0].parentNode ) {
                                wrap.insertBefore( this[0] );
+                       }
 
                        wrap.map(function(){
                                var elem = this;
 
-                               while ( elem.firstChild )
+                               while ( elem.firstChild && elem.firstChild.nodeType === 1 ) {
                                        elem = elem.firstChild;
+                               }
 
                                return elem;
                        }).append(this);
@@ -52,15 +74,17 @@ jQuery.fn.extend({
 
        append: function() {
                return this.domManip(arguments, true, function(elem){
-                       if (this.nodeType == 1)
+                       if ( this.nodeType === 1 ) {
                                this.appendChild( elem );
+                       }
                });
        },
 
        prepend: function() {
                return this.domManip(arguments, true, function(elem){
-                       if (this.nodeType == 1)
+                       if ( this.nodeType === 1 ) {
                                this.insertBefore( elem, this.firstChild );
+                       }
                });
        },
 
@@ -95,9 +119,11 @@ jQuery.fn.extend({
                                        html = div.innerHTML;
                                }
 
-                               return jQuery.clean([html.replace(/ jQuery\d+="(?:\d+|null)"/g, "").replace(/^\s*/, "")], ownerDocument)[0];
-                       } else
+                               return jQuery.clean([html.replace(rinlinejQuery, "")
+                                       .replace(rleadingWhitespace, "")], ownerDocument)[0];
+                       } else {
                                return this.cloneNode(true);
+                       }
                });
 
                // Copy the events from the original to the clone
@@ -105,8 +131,7 @@ jQuery.fn.extend({
                        var orig = this.find("*").andSelf(), i = 0;
 
                        ret.find("*").andSelf().each(function(){
-                               if ( this.nodeName !== orig[i].nodeName )
-                                       return;
+                               if ( this.nodeName !== orig[i].nodeName ) { return; }
 
                                var events = jQuery.data( orig[i], "events" );
 
@@ -127,7 +152,7 @@ jQuery.fn.extend({
        html: function( value ) {
                return value === undefined ?
                        (this[0] ?
-                               this[0].innerHTML.replace(/ jQuery\d+="(?:\d+|null)"/g, "") :
+                               this[0].innerHTML.replace(rinlinejQuery, "") :
                                null) :
                        this.empty().append( value );
        },
@@ -137,15 +162,15 @@ jQuery.fn.extend({
        },
 
        domManip: function( args, table, callback ) {
-               var fragment, scripts, cacheable, cached, cacheresults, first;
-               var value = args[0];
+               var fragment, scripts, cacheable, cached, cacheresults, first,
+                       value = args[0];
 
                if ( jQuery.isFunction(value) ) {
                        return this.each(function() {
                                args[0] = value.call(this);
                                return jQuery(this).domManip( args, table, callback );
                        });
-               };
+               }
 
                if ( this[0] ) {
                        if ( args.length === 1 && typeof args[0] === "string" && args[0].length < 512 && args[0].indexOf("<option") < 0 ) {
@@ -158,7 +183,7 @@ jQuery.fn.extend({
                                        cached = true;
                                }
                        }
-                       
+
                        if ( !fragment ) {
                                fragment = (this[0].ownerDocument || this[0]).createDocumentFragment();
                                scripts = jQuery.clean( args, (this[0].ownerDocument || this[0]), fragment );
@@ -224,18 +249,21 @@ jQuery.each({
 });
 
 jQuery.each({
-       remove: function( selector ) {
+       // keepData is for internal use only--do not document
+       remove: function( selector, keepData ) {
                if ( !selector || jQuery.multiFilter( selector, [ this ] ).length ) {
-                       if ( this.nodeType === 1 ) {
+                       if ( !keepData && this.nodeType === 1 ) {
                                cleanData( jQuery("*", this).add(this) );
                        }
 
-                       if ( this.parentNode ) {
-                               this.parentNode.removeChild( this );
-                       }
+                       this.parentNode && this.parentNode.removeChild( this );
                }
        },
 
+       detach: function( selector ) {
+               jQuery( this ).remove( selector, true )
+       },
+
        empty: function() {
                // Remove element nodes and prevent memory leaks
                if ( this.nodeType === 1 ) {
@@ -258,37 +286,36 @@ jQuery.extend({
                context = context || document;
 
                // !context.createElement fails in IE with an error but returns typeof 'object'
-               if ( typeof context.createElement === "undefined" )
+               if ( typeof context.createElement === "undefined" ) {
                        context = context.ownerDocument || context[0] && context[0].ownerDocument || document;
+               }
 
                // If a single string is passed in and it's a single tag
                // just do a createElement and skip the rest
                if ( !fragment && elems.length === 1 && typeof elems[0] === "string" ) {
-                       var match = /^<(\w+)\s*\/?>$/.exec(elems[0]);
-                       if ( match )
+                       var match = rsingleTag.exec(elems[0]);
+                       if ( match ) {
                                return [ context.createElement( match[1] ) ];
+                       }
                }
 
                var ret = [], scripts = [], div = context.createElement("div");
 
                jQuery.each(elems, function(i, elem){
-                       if ( typeof elem === "number" )
+                       if ( typeof elem === "number" ) {
                                elem += '';
+                       }
 
-                       if ( !elem )
-                               return;
+                       if ( !elem ) { return; }
 
                        // Convert html string into DOM nodes
                        if ( typeof elem === "string" ) {
                                // Fix "XHTML"-style tags in all browsers
-                               elem = elem.replace(/(<(\w+)[^>]*?)\/>/g, function(all, front, tag){
-                                       return tag.match(/^(abbr|br|col|img|input|link|meta|param|hr|area|embed)$/i) ?
-                                               all :
-                                               front + "></" + tag + ">";
-                               });
+                               elem = elem.replace(rxhtmlTag, fcloseTag);
 
                                // Trim whitespace, otherwise indexOf won't work as expected
-                               var tags = elem.replace(/^\s+/, "").substring(0, 10).toLowerCase();
+                               var tags = elem.replace(rleadingWhitespace, "")
+                                       .substring(0, 10).toLowerCase();
 
                                var wrap =
                                        // option or optgroup
@@ -298,7 +325,7 @@ jQuery.extend({
                                        !tags.indexOf("<leg") &&
                                        [ 1, "<fieldset>", "</fieldset>" ] ||
 
-                                       tags.match(/^<(thead|tbody|tfoot|colg|cap)/) &&
+                                       rinsideTable.test(tags) &&
                                        [ 1, "<table>", "</table>" ] ||
 
                                        !tags.indexOf("<tr") &&
@@ -321,39 +348,44 @@ jQuery.extend({
                                div.innerHTML = wrap[1] + elem + wrap[2];
 
                                // Move to the right depth
-                               while ( wrap[0]-- )
+                               while ( wrap[0]-- ) {
                                        div = div.lastChild;
+                               }
 
                                // Remove IE's autoinserted <tbody> from table fragments
                                if ( !jQuery.support.tbody ) {
 
                                        // String was a <table>, *may* have spurious <tbody>
-                                       var hasBody = /<tbody/i.test(elem),
+                                       var hasBody = rtbody.test(elem),
                                                tbody = !tags.indexOf("<table") && !hasBody ?
                                                        div.firstChild && div.firstChild.childNodes :
 
-                                               // String was a bare <thead> or <tfoot>
-                                               wrap[1] == "<table>" && !hasBody ?
-                                                       div.childNodes :
-                                                       [];
+                                                       // String was a bare <thead> or <tfoot>
+                                                       wrap[1] == "<table>" && !hasBody ?
+                                                               div.childNodes :
+                                                               [];
 
-                                       for ( var j = tbody.length - 1; j >= 0 ; --j )
-                                               if ( jQuery.nodeName( tbody[ j ], "tbody" ) && !tbody[ j ].childNodes.length )
+                                       for ( var j = tbody.length - 1; j >= 0 ; --j ) {
+                                               if ( jQuery.nodeName( tbody[ j ], "tbody" ) && !tbody[ j ].childNodes.length ) {
                                                        tbody[ j ].parentNode.removeChild( tbody[ j ] );
-
+                                               }
                                        }
 
+                               }
+
                                // IE completely kills leading whitespace when innerHTML is used
-                               if ( !jQuery.support.leadingWhitespace && /^\s/.test( elem ) )
-                                       div.insertBefore( context.createTextNode( elem.match(/^\s*/)[0] ), div.firstChild );
+                               if ( !jQuery.support.leadingWhitespace && rleadingWhitespace.test( elem ) ) {
+                                       div.insertBefore( context.createTextNode( rleadingWhitespace.exec(elem)[0] ), div.firstChild );
+                               }
 
                                elem = jQuery.makeArray( div.childNodes );
                        }
 
-                       if ( elem.nodeType )
+                       if ( elem.nodeType ) {
                                ret.push( elem );
-                       else
+                       } else {
                                ret = jQuery.merge( ret, elem );
+                       }
 
                });
 
@@ -362,8 +394,9 @@ jQuery.extend({
                                if ( 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 )
+                                       if ( ret[i].nodeType === 1 ) {
                                                ret.splice.apply( ret, [i + 1, 0].concat(jQuery.makeArray(ret[i].getElementsByTagName("script"))) );
+                                       }
                                        fragment.appendChild( ret[i] );
                                }
                        }