Fixed a bug introduced in SVN rev [6537] that caused XML-based fragment creation...
[jquery.git] / src / manipulation.js
index 95e2f7a..ae4430a 100644 (file)
@@ -4,6 +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 = /</,
        fcloseTag = function(all, front, tag){
                return rselfClosing.test(tag) ?
                        all :
@@ -106,15 +107,29 @@ jQuery.fn.extend({
        },
 
        before: function() {
-               return this.domManip(arguments, false, function(elem){
-                       this.parentNode.insertBefore( elem, this );
-               });
+               if ( this[0] && this[0].parentNode ) {
+                       return this.domManip(arguments, false, function(elem){
+                               this.parentNode.insertBefore( elem, this );
+                       });
+               } else {
+                       var set = jQuery.isFunction(arguments[0]) ?
+                               jQuery( arguments[0]() ) :
+                               jQuery.apply(jQuery, arguments);
+
+                       return this.pushStack( set.add( this ), "before", arguments );
+               }
        },
 
        after: function() {
-               return this.domManip(arguments, false, function(elem){
-                       this.parentNode.insertBefore( elem, this.nextSibling );
-               });
+               if ( this[0] && this[0].parentNode ) {
+                       return this.domManip(arguments, false, function(elem){
+                               this.parentNode.insertBefore( elem, this.nextSibling );
+                       });
+               } else {
+                       return jQuery.isFunction(arguments[0]) ?
+                               this.add( arguments[0]() ) :
+                               this.add.apply( this, arguments );
+               }
        },
 
        clone: function( events ) {
@@ -193,7 +208,11 @@ jQuery.fn.extend({
        },
 
        replaceWith: function( value ) {
-               return this.after( value ).remove();
+               if ( this[0] && this[0].parentNode ) {
+                       return this.after( value ).remove();
+               } else {
+                       return this.pushStack( jQuery(jQuery.isFunction(value) ? value() : value), "replaceWith", value );
+               }
        },
 
        detach: function( selector ) {
@@ -215,7 +234,7 @@ jQuery.fn.extend({
                        if ( args[0] && args[0].parentNode && args[0].parentNode.nodeType === 11 ) {
                                results = { fragment: args[0].parentNode };
                        } else {
-                               results = buildFragment( args, this[0], scripts );
+                               results = buildFragment( args, this, scripts );
                        }
 
                        first = results.fragment.firstChild;
@@ -295,8 +314,7 @@ jQuery.each({
                        jQuery.fn[ original ].apply( jQuery(insert[i]), elems );
                        ret = ret.concat( elems );
                }
-
-               return this.pushStack( ret, name, selector );
+               return this.pushStack( ret, name, insert.selector );
        };
 });
 
@@ -351,7 +369,10 @@ jQuery.extend({
                        if ( !elem ) { return; }
 
                        // Convert html string into DOM nodes
-                       if ( typeof elem === "string" ) {
+                       if ( typeof elem === "string" && !rhtml.test( elem ) ) {
+                               elem = context.createTextNode( elem );
+
+                       } else if ( typeof elem === "string" ) {
                                // Fix "XHTML"-style tags in all browsers
                                elem = elem.replace(rxhtmlTag, fcloseTag);