diff options
author | Richard Gibson <richard.gibson@gmail.com> | 2013-04-03 13:17:02 -0400 |
---|---|---|
committer | Richard Gibson <richard.gibson@gmail.com> | 2013-04-16 17:28:35 -0400 |
commit | bdc4f3ebbe747b7531ce3588c8428852ebfb4ce5 (patch) | |
tree | 96af3d1c9f8bd32b5304f0594530b7043e257d3f /src/manipulation.js | |
parent | 0c927172b835a409d5f05833a05ad882264c51bd (diff) | |
download | jquery-bdc4f3ebbe747b7531ce3588c8428852ebfb4ce5.tar.gz jquery-bdc4f3ebbe747b7531ce3588c8428852ebfb4ce5.zip |
Fix #13596; #13722: .replaceWith consistency. Close gh-1216.
Diffstat (limited to 'src/manipulation.js')
-rw-r--r-- | src/manipulation.js | 78 |
1 files changed, 41 insertions, 37 deletions
diff --git a/src/manipulation.js b/src/manipulation.js index 2ab8b59ed..aeeaddf35 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -126,23 +126,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 ); } @@ -150,7 +152,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 ); } @@ -257,33 +259,35 @@ jQuery.fn.extend({ }, null, value, arguments.length ); }, - replaceWith: function( value ) { - var isFunc = 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 ( !isFunc && 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 ); @@ -302,14 +306,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 ) { @@ -317,7 +321,6 @@ jQuery.fn.extend({ } if ( first ) { - table = table && jQuery.nodeName( first, "tr" ); scripts = jQuery.map( getAll( fragment, "script" ), disableScript ); hasScripts = scripts.length; @@ -335,13 +338,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 ) { @@ -382,8 +379,15 @@ jQuery.fn.extend({ } }); -function findOrAppend( elem, tag ) { - return elem.getElementsByTagName( tag )[0] || elem.appendChild( elem.ownerDocument.createElement( tag ) ); +// Support: IE<8 +// 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 |