Tweaked formatting of src/manipulation.js.
[jquery.git] / src / manipulation.js
index ce9b917..9406aa8 100644 (file)
@@ -1,16 +1,17 @@
 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 +19,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 +61,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 );
+                       }
                });
        },
 
@@ -105,8 +116,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" );
 
@@ -137,24 +147,63 @@ jQuery.fn.extend({
        },
 
        domManip: function( args, table, callback ) {
+               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] ) {
-                       var fragment = (this[0].ownerDocument || this[0]).createDocumentFragment(),
-                               scripts = jQuery.clean( args, (this[0].ownerDocument || this[0]), fragment ),
-                               first = fragment.firstChild;
-
-                       if ( first )
-                               for ( var i = 0, l = this.length; i < l; i++ )
-                                       callback.call( root(this[i], first), this.length > 1 || i > 0 ?
-                                                       fragment.cloneNode(true) : fragment );
-               
-                       if ( scripts )
+                       if ( args.length === 1 && typeof args[0] === "string" && args[0].length < 512 && args[0].indexOf("<option") < 0 ) {
+                               cacheable = true;
+                               cacheresults = jQuery.fragments[ args[0] ];
+                               if ( cacheresults ) {
+                                       if ( cacheresults !== 1 ) {
+                                               fragment = cacheresults;
+                                       }
+                                       cached = true;
+                               }
+                       }
+                       
+                       if ( !fragment ) {
+                               fragment = (this[0].ownerDocument || this[0]).createDocumentFragment();
+                               scripts = jQuery.clean( args, (this[0].ownerDocument || this[0]), fragment );
+                       }
+
+                       first = fragment.firstChild;
+
+                       if ( first ) {
+                               table = table && jQuery.nodeName( first, "tr" );
+
+                               for ( var i = 0, l = this.length; i < l; i++ ) {
+                                       callback.call(
+                                               table ?
+                                                       root(this[i], first) :
+                                                       this[i],
+                                               cacheable || this.length > 1 || i > 0 ?
+                                                       fragment.cloneNode(true) :
+                                                       fragment
+                                       );
+                               }
+                       }
+
+                       if ( scripts ) {
                                jQuery.each( scripts, evalScript );
+                       }
+
+                       if ( cacheable ) {
+                               jQuery.fragments[ args[0] ] = cacheresults ? fragment : 1;
+                       }
                }
 
                return this;
-               
+
                function root( elem, cur ) {
-                       return table && jQuery.nodeName(elem, "table") && jQuery.nodeName(cur, "tr") ?
+                       return jQuery.nodeName(elem, "table") ?
                                (elem.getElementsByTagName("tbody")[0] ||
                                elem.appendChild(elem.ownerDocument.createElement("tbody"))) :
                                elem;
@@ -162,6 +211,8 @@ jQuery.fn.extend({
        }
 });
 
+jQuery.fragments = {};
+
 jQuery.each({
        appendTo: "append",
        prependTo: "prepend",
@@ -186,8 +237,7 @@ jQuery.each({
        remove: function( selector ) {
                if ( !selector || jQuery.multiFilter( selector, [ this ] ).length ) {
                        if ( this.nodeType === 1 ) {
-                               cleanData( this.getElementsByTagName("*") );
-                               cleanData( [this] );
+                               cleanData( jQuery("*", this).add(this) );
                        }
 
                        if ( this.parentNode ) {
@@ -199,7 +249,7 @@ jQuery.each({
        empty: function() {
                // Remove element nodes and prevent memory leaks
                if ( this.nodeType === 1 ) {
-                       cleanData( this.getElementsByTagName("*") );
+                       cleanData( jQuery("*", this) );
                }
 
                // Remove any remaining nodes
@@ -218,8 +268,9 @@ 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
@@ -232,17 +283,17 @@ jQuery.extend({
                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) ?
+                                       return /^(abbr|br|col|img|input|link|meta|param|hr|area|embed)$/i.test(tag) ?
                                                all :
                                                front + "></" + tag + ">";
                                });
@@ -258,7 +309,7 @@ jQuery.extend({
                                        !tags.indexOf("<leg") &&
                                        [ 1, "<fieldset>", "</fieldset>" ] ||
 
-                                       tags.match(/^<(thead|tbody|tfoot|colg|cap)/) &&
+                                       /^<(thead|tbody|tfoot|colg|cap)/.test(tags) &&
                                        [ 1, "<table>", "</table>" ] ||
 
                                        !tags.indexOf("<tr") &&
@@ -281,8 +332,9 @@ 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 ) {
@@ -292,28 +344,32 @@ jQuery.extend({
                                                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 && /^\s/.test( elem ) ) {
+                                       div.insertBefore( context.createTextNode( /^\s*/.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 );
+                       }
 
                });
 
@@ -327,7 +383,7 @@ jQuery.extend({
                                        fragment.appendChild( ret[i] );
                                }
                        }
-                       
+
                        return scripts;
                }