Fix for #4011, crash when two text nodes are appended in IE.
[jquery.git] / src / manipulation.js
index 789bf54..7d7038e 100644 (file)
@@ -4,7 +4,7 @@ var rinlinejQuery = / jQuery\d+="(?:\d+|null)"/g,
        rselfClosing = /^(?:abbr|br|col|img|input|link|meta|param|hr|area|embed)$/i,
        rtagName = /<([\w:]+)/,
        rtbody = /<tbody/i,
-       rhtml = /</,
+       rhtml = /<|&\w+;/,
        fcloseTag = function(all, front, tag){
                return rselfClosing.test(tag) ?
                        all :
@@ -31,19 +31,21 @@ if ( !jQuery.support.htmlSerialize ) {
 
 jQuery.fn.extend({
        text: function( text ) {
-               if ( typeof text !== "object" && text !== undefined )
+               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 ] );
-                               }
-                       });
+               jQuery.each( this, function() {
+                       // Get the text from text nodes and CDATA nodes
+                       if ( this.nodeType === 3 || this.nodeType === 4 ) {
+                               ret += this.nodeValue;
+
+                       // Traverse everything else, except comment nodes
+                       } else if ( this.nodeType !== 8 ) {
+                               ret += jQuery.fn.text.call( this.childNodes );
+                       }
                });
 
                return ret;
@@ -166,21 +168,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
@@ -195,7 +184,7 @@ 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 )) &&
+                       (jQuery.support.leadingWhitespace || !rleadingWhitespace.test( value )) &&
                        !wrapMap[ (rtagName.exec( value ) || ["", ""])[1].toLowerCase() ] ) {
 
                        try {
@@ -221,7 +210,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 );
                }
@@ -282,6 +281,24 @@ jQuery.fn.extend({
        }
 });
 
+function cloneCopyEvent(orig, ret) {
+       var i = 0;
+
+       ret.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 );
+                       }
+               }
+       });
+}
+
 function buildFragment(args, nodes, scripts){
        var fragment, cacheable, cached, cacheresults, doc;
 
@@ -333,7 +350,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 ] );
@@ -371,7 +388,7 @@ 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){
                        if ( typeof elem === "number" ) {
@@ -391,7 +408,8 @@ jQuery.extend({
                                // 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];