From db0326b1fdd22b97e24af34245e38914c04a70ba Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Wed, 3 Apr 2013 13:17:02 -0400 Subject: Fix #13596; #13722: .replaceWith consistency. Close gh-1216. --- src/manipulation.js | 78 ++++++++++++++++++++++++++++------------------------- src/sizzle | 2 +- 2 files changed, 42 insertions(+), 38 deletions(-) (limited to 'src') diff --git a/src/manipulation.js b/src/manipulation.js index c794470dc..3747e0924 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -38,23 +38,25 @@ jQuery.fn.extend({ }, append: function() { - return this.domManip(arguments, true, function( elem ) { + return this.domManip( arguments, function( elem ) { if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) { - this.appendChild( elem ); + var target = manipulationTarget( this, elem ); + target.appendChild( elem ); } }); }, prepend: function() { - return this.domManip(arguments, true, function( elem ) { + return this.domManip( arguments, function( elem ) { if ( this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9 ) { - this.insertBefore( elem, this.firstChild ); + var target = manipulationTarget( this, elem ); + target.insertBefore( elem, target.firstChild ); } }); }, before: function() { - return this.domManip(arguments, false, function( elem ) { + return this.domManip( arguments, function( elem ) { if ( this.parentNode ) { this.parentNode.insertBefore( elem, this ); } @@ -62,7 +64,7 @@ jQuery.fn.extend({ }, after: function() { - return this.domManip(arguments, false, function( elem ) { + return this.domManip( arguments, function( elem ) { if ( this.parentNode ) { this.parentNode.insertBefore( elem, this.nextSibling ); } @@ -162,33 +164,35 @@ jQuery.fn.extend({ }, null, value, arguments.length ); }, - replaceWith: function( value ) { - var isFunction = jQuery.isFunction( value ); + replaceWith: function() { + var + // Snapshot the DOM in case .domManip sweeps something relevant into its fragment + args = jQuery.map( this, function( elem ) { + return [ elem.nextSibling, elem.parentNode ]; + }), + i = 0; - // 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 ( !isFunction && typeof value !== "string" ) { - value = jQuery( value ).not( this ).detach(); - } + // Make the changes, replacing each context element with the new content + this.domManip( arguments, function( elem ) { + var next = args[ i++ ], + parent = args[ i++ ]; - return value !== "" ? - this.domManip( [ value ], true, function( elem ) { - var next = this.nextSibling, - parent = this.parentNode; + if ( parent ) { + jQuery( this ).remove(); + parent.insertBefore( elem, next ); + } + // Allow new content to include elements from the context set + }, true ); - if ( parent ) { - jQuery( this ).remove(); - parent.insertBefore( elem, next ); - } - }) : - this.remove(); + // Force removal if there was no new content (e.g., from empty arguments) + return i ? this : this.remove(); }, detach: function( selector ) { return this.remove( selector, true ); }, - domManip: function( args, table, callback ) { + domManip: function( args, callback, allowIntersection ) { // Flatten any nested arrays args = core_concat.apply( [], args ); @@ -206,14 +210,14 @@ jQuery.fn.extend({ return this.each(function( index ) { var self = set.eq( index ); if ( isFunction ) { - args[ 0 ] = value.call( this, index, table ? self.html() : undefined ); + args[ 0 ] = value.call( this, index, self.html() ); } - self.domManip( args, table, callback ); + self.domManip( args, callback, allowIntersection ); }); } if ( l ) { - fragment = jQuery.buildFragment( args, this[ 0 ].ownerDocument, false, this ); + fragment = jQuery.buildFragment( args, this[ 0 ].ownerDocument, false, !allowIntersection && this ); first = fragment.firstChild; if ( fragment.childNodes.length === 1 ) { @@ -221,7 +225,6 @@ jQuery.fn.extend({ } if ( first ) { - table = table && jQuery.nodeName( first, "tr" ); scripts = jQuery.map( getAll( fragment, "script" ), disableScript ); hasScripts = scripts.length; @@ -241,13 +244,7 @@ jQuery.fn.extend({ } } - callback.call( - table && jQuery.nodeName( this[ i ], "table" ) ? - findOrAppend( this[ i ], "tbody" ) : - this[ i ], - node, - i - ); + callback.call( this[ i ], node, i ); } if ( hasScripts ) { @@ -476,8 +473,15 @@ jQuery.extend({ } }); -function findOrAppend( elem, tag ) { - return elem.getElementsByTagName( tag )[ 0 ] || elem.appendChild( elem.ownerDocument.createElement(tag) ); +// Support: 1.x compatibility +// Manipulating tables requires a tbody +function manipulationTarget( elem, content ) { + return jQuery.nodeName( elem, "table" ) && + jQuery.nodeName( content.nodeType === 1 ? content : content.firstChild, "tr" ) ? + + elem.getElementsByTagName("tbody")[0] || + elem.appendChild( elem.ownerDocument.createElement("tbody") ) : + elem; } // Replace/restore the type attribute of script elements for safe DOM manipulation diff --git a/src/sizzle b/src/sizzle index 2dca62f52..26d2cef80 160000 --- a/src/sizzle +++ b/src/sizzle @@ -1 +1 @@ -Subproject commit 2dca62f52cc86a56e36753ab52e1dffe3c257940 +Subproject commit 26d2cef803e613fcab99d049c1f947340af968f5 -- cgit v1.2.3