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:47:21 -0400 |
commit | db0326b1fdd22b97e24af34245e38914c04a70ba (patch) | |
tree | 9893816f5054ff7835f91807717f9c6714e3c42d /src | |
parent | 562ca75e06a07067cace674e4e492119d78ca161 (diff) | |
download | jquery-db0326b1fdd22b97e24af34245e38914c04a70ba.tar.gz jquery-db0326b1fdd22b97e24af34245e38914c04a70ba.zip |
Fix #13596; #13722: .replaceWith consistency. Close gh-1216.
Diffstat (limited to 'src')
-rw-r--r-- | src/manipulation.js | 78 | ||||
m--------- | src/sizzle | 0 |
2 files changed, 41 insertions, 37 deletions
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 -Subproject 2dca62f52cc86a56e36753ab52e1dffe3c25794 +Subproject 26d2cef803e613fcab99d049c1f947340af968f |