Make sure that jQuery works even when the individual modules are loaded separately...
[jquery.git] / src / manipulation.js
index 01c6b0b..3054eea 100644 (file)
@@ -1,10 +1,13 @@
+(function( jQuery ) {
+
 var rinlinejQuery = / jQuery\d+="(?:\d+|null)"/g,
        rleadingWhitespace = /^\s+/,
        rxhtmlTag = /(<([\w:]+)[^>]*?)\/>/g,
        rselfClosing = /^(?:area|br|col|embed|hr|img|input|link|meta|param)$/i,
        rtagName = /<([\w:]+)/,
        rtbody = /<tbody/i,
-       rhtml = /<|&\w+;/,
+       rhtml = /<|&#?\w+;/,
+       rnocache = /<script|<object|<embed|<option|<style/i,
        rchecked = /checked\s*(?:[^=]|=\s*.checked.)/i,  // checked="checked" or checked (html5)
        fcloseTag = function( all, front, tag ) {
                return rselfClosing.test( tag ) ?
@@ -44,7 +47,7 @@ jQuery.fn.extend({
                        return this.empty().append( (this[0] && this[0].ownerDocument || document).createTextNode( text ) );
                }
 
-               return jQuery.getText( this );
+               return jQuery.text( this );
        },
 
        wrapAll: function( html ) {
@@ -203,6 +206,8 @@ jQuery.fn.extend({
                                }
 
                                return jQuery.clean([html.replace(rinlinejQuery, "")
+                                       // Handle the case in IE 8 where action=/test/> self-closes a tag
+                                       .replace(/\=([^="'>\s]+\/)>/g, '="$1">')
                                        .replace(rleadingWhitespace, "")], ownerDocument)[0];
                        } else {
                                return this.cloneNode(true);
@@ -226,7 +231,7 @@ jQuery.fn.extend({
                                null;
 
                // See if we can take a shortcut and just use innerHTML
-               } else if ( typeof value === "string" && !/<script/i.test( value ) &&
+               } else if ( typeof value === "string" && !rnocache.test( value ) &&
                        (jQuery.support.leadingWhitespace || !rleadingWhitespace.test( value )) &&
                        !wrapMap[ (rtagName.exec( value ) || ["", ""])[1].toLowerCase() ] ) {
 
@@ -265,16 +270,17 @@ jQuery.fn.extend({
                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();
-
-                       } else {
+                       if ( jQuery.isFunction( value ) ) {
                                return this.each(function(i) {
                                        var self = jQuery(this), old = self.html();
                                        self.replaceWith( value.call( this, i, old ) );
                                });
                        }
 
+                       if ( typeof value !== "string" ) {
+                               value = jQuery(value).detach();
+                       }
+
                        return this.each(function() {
                                var next = this.nextSibling, parent = this.parentNode;
 
@@ -296,7 +302,7 @@ jQuery.fn.extend({
        },
 
        domManip: function( args, table, callback ) {
-               var results, first, value = args[0], scripts = [];
+               var results, first, value = args[0], scripts = [], fragment, parent;
 
                // We can't cloneNode fragments that contain checked, in WebKit
                if ( !jQuery.support.checkClone && arguments.length === 3 && typeof value === "string" && rchecked.test( value ) ) {
@@ -314,14 +320,23 @@ jQuery.fn.extend({
                }
 
                if ( this[0] ) {
+                       parent = value && value.parentNode;
+
                        // If we're in a fragment, just use that instead of building a new one
-                       if ( args[0] && args[0].parentNode && args[0].parentNode.nodeType === 11 ) {
-                               results = { fragment: args[0].parentNode };
+                       if ( jQuery.support.parentNode && parent && parent.nodeType === 11 && parent.childNodes.length === this.length ) {
+                               results = { fragment: parent };
+
                        } else {
                                results = buildFragment( args, this, scripts );
                        }
-
-                       first = results.fragment.firstChild;
+                       
+                       fragment = results.fragment;
+                       
+                       if ( fragment.childNodes.length === 1 ) {
+                               first = fragment = fragment.firstChild;
+                       } else {
+                               first = fragment.firstChild;
+                       }
 
                        if ( first ) {
                                table = table && jQuery.nodeName( first, "tr" );
@@ -331,29 +346,29 @@ jQuery.fn.extend({
                                                table ?
                                                        root(this[i], first) :
                                                        this[i],
-                                               results.cacheable || this.length > 1 || i > 0 ?
-                                                       results.fragment.cloneNode(true) :
-                                                       results.fragment
+                                               i > 0 || results.cacheable || this.length > 1  ?
+                                                       fragment.cloneNode(true) :
+                                                       fragment
                                        );
                                }
                        }
 
-                       if ( scripts ) {
+                       if ( scripts.length ) {
                                jQuery.each( scripts, evalScript );
                        }
                }
 
                return this;
-
-               function root( elem, cur ) {
-                       return jQuery.nodeName(elem, "table") ?
-                               (elem.getElementsByTagName("tbody")[0] ||
-                               elem.appendChild(elem.ownerDocument.createElement("tbody"))) :
-                               elem;
-               }
        }
 });
 
+function root( elem, cur ) {
+       return jQuery.nodeName(elem, "table") ?
+               (elem.getElementsByTagName("tbody")[0] ||
+               elem.appendChild(elem.ownerDocument.createElement("tbody"))) :
+               elem;
+}
+
 function cloneCopyEvent(orig, ret) {
        var i = 0;
 
@@ -378,10 +393,16 @@ function cloneCopyEvent(orig, ret) {
 }
 
 function buildFragment( args, nodes, scripts ) {
-       var fragment, cacheable, cacheresults, doc;
+       var fragment, cacheable, cacheresults,
+               doc = (nodes && nodes[0] ? nodes[0].ownerDocument || nodes[0] : document);
+
+       // Only cache "small" (1/2 KB) strings that are associated with the main document
+       // Cloning options loses the selected state, so don't cache them
+       // IE 6 doesn't like it when you put <object> or <embed> elements in a fragment
+       // Also, WebKit does not clone 'checked' attributes on cloneNode, so don't cache
+       if ( args.length === 1 && typeof args[0] === "string" && args[0].length < 512 && doc === document &&
+               !rnocache.test( args[0] ) && (jQuery.support.checkClone || !rchecked.test( args[0] )) ) {
 
-       // webkit does not clone 'checked' attribute of radio inputs on cloneNode, so don't cache if string has a checked
-       if ( args.length === 1 && typeof args[0] === "string" && args[0].length < 512 && args[0].indexOf("<option") < 0 && (jQuery.support.checkClone || !rchecked.test( args[0] )) ) {
                cacheable = true;
                cacheresults = jQuery.fragments[ args[0] ];
                if ( cacheresults ) {
@@ -392,7 +413,6 @@ function buildFragment( args, nodes, scripts ) {
        }
 
        if ( !fragment ) {
-               doc = (nodes && nodes[0] ? nodes[0].ownerDocument || nodes[0] : document);
                fragment = doc.createDocumentFragment();
                jQuery.clean( args, doc, fragment, scripts );
        }
@@ -414,16 +434,17 @@ jQuery.each({
        replaceAll: "replaceWith"
 }, function( name, original ) {
        jQuery.fn[ name ] = function( selector ) {
-               var ret = [], insert = jQuery( selector );
+               var ret = [], insert = jQuery( selector ),
+                       parent = this.length === 1 && this[0].parentNode;
                
-               if ( this.length === 1 && this[0].parentNode && this[0].parentNode.nodeType === 11 && insert.length === 1 ) {
+               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.fn[ original ].apply( jQuery(insert[i]), elems );
+                               jQuery( insert[i] )[ original ]( elems );
                                ret = ret.concat( elems );
                        }
                
@@ -511,7 +532,7 @@ jQuery.extend({
                }
 
                if ( fragment ) {
-                       for ( var i = 0; ret[i]; i++ ) {
+                       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] );
                                
@@ -528,24 +549,58 @@ jQuery.extend({
        },
        
        cleanData: function( elems ) {
-               var data, id, cache = jQuery.cache;
+               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.events ) {
-                                       for ( var event in data.events ) {
-                                               removeEvent( elem, event, data.handle );
+                               if ( data && data.events ) {
+                                       for ( var type in data.events ) {
+                                               if ( special[ type ] ) {
+                                                       jQuery.event.remove( elem, type );
+
+                                               } else {
+                                                       removeEvent( elem, type, data.handle );
+                                               }
                                        }
                                }
                                
-                               removeExpando( elem );
+                               if ( deleteExpando ) {
+                                       delete elem[ jQuery.expando ];
+
+                               } else if ( elem.removeAttribute ) {
+                                       elem.removeAttribute( jQuery.expando );
+                               }
                                
                                delete cache[ id ];
                        }
                }
        }
 });
+
+function evalScript( i, elem ) {
+       if ( elem.src ) {
+               jQuery.ajax({
+                       url: elem.src,
+                       async: false,
+                       dataType: "script"
+               });
+       } else {
+               jQuery.globalEval( elem.text || elem.textContent || elem.innerHTML || "" );
+       }
+
+       if ( elem.parentNode ) {
+               elem.parentNode.removeChild( elem );
+       }
+}
+
+})( jQuery );