aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorRichard Gibson <richard.gibson@gmail.com>2015-04-13 16:05:48 -0400
committerRichard Gibson <richard.gibson@gmail.com>2015-04-30 11:36:38 -0400
commit4b27ae16a2b911f75b341b56d9d939bc65a9657a (patch)
tree545cac9a732aeb4f0898b39d1df2f49acab6da53 /src
parent154166458284bcce7d6a86328b7fd13483232a1a (diff)
downloadjquery-4b27ae16a2b911f75b341b56d9d939bc65a9657a.tar.gz
jquery-4b27ae16a2b911f75b341b56d9d939bc65a9657a.zip
Manipulation: Detect sneaky no-content replaceWith input
Fixes gh-2204 Ref 642e9a45579cfa90861b8ea71a95dd077775caaf Closes gh-1752 Closes gh-2206
Diffstat (limited to 'src')
-rw-r--r--src/manipulation.js39
1 files changed, 21 insertions, 18 deletions
diff --git a/src/manipulation.js b/src/manipulation.js
index 16a5a5208..160b9ec79 100644
--- a/src/manipulation.js
+++ b/src/manipulation.js
@@ -201,7 +201,7 @@ jQuery.extend({
return clone;
},
- buildFragment: function( elems, context, scripts, selection ) {
+ buildFragment: function( elems, context, scripts, selection, ignored ) {
var elem, tmp, tag, wrap, contains, j,
fragment = context.createDocumentFragment(),
nodes = [],
@@ -257,9 +257,11 @@ jQuery.extend({
i = 0;
while ( (elem = nodes[ i++ ]) ) {
- // #4087 - If origin and destination elements are the same, and this is
- // that element, do not do anything
+ // Skip elements already in the context collection (trac-4087)
if ( selection && jQuery.inArray( elem, selection ) > -1 ) {
+ if ( ignored ) {
+ ignored.push( elem );
+ }
continue;
}
@@ -446,28 +448,28 @@ jQuery.fn.extend({
},
replaceWith: function() {
- var arg = arguments[ 0 ];
-
- // Make the changes, replacing each context element with the new content
- this.domManip( arguments, function( elem ) {
- arg = this.parentNode;
+ var ignored = [];
- jQuery.cleanData( getAll( this ) );
+ // Make the changes, replacing each non-ignored context element with the new content
+ return this.domManip( arguments, function( elem ) {
+ var parent = this.parentNode;
- if ( arg ) {
- arg.replaceChild( elem, this );
+ if ( jQuery.inArray( this, ignored ) < 0 ) {
+ jQuery.cleanData( getAll( this ) );
+ if ( parent ) {
+ parent.replaceChild( elem, this );
+ }
}
- });
- // Force removal if there was no new content (e.g., from empty arguments)
- return arg && (arg.length || arg.nodeType) ? this : this.remove();
+ // Force callback invocation
+ }, ignored );
},
detach: function( selector ) {
return this.remove( selector, true );
},
- domManip: function( args, callback ) {
+ domManip: function( args, callback, ignored ) {
// Flatten any nested arrays
args = concat.apply( [], args );
@@ -489,19 +491,20 @@ jQuery.fn.extend({
if ( isFunction ) {
args[ 0 ] = value.call( this, index, self.html() );
}
- self.domManip( args, callback );
+ self.domManip( args, callback, ignored );
});
}
if ( l ) {
- fragment = jQuery.buildFragment( args, this[ 0 ].ownerDocument, false, this );
+ fragment = jQuery.buildFragment( args, this[ 0 ].ownerDocument, false, this, ignored );
first = fragment.firstChild;
if ( fragment.childNodes.length === 1 ) {
fragment = first;
}
- if ( first ) {
+ // Require either new content or an interest in ignored elements to invoke the callback
+ if ( first || ignored ) {
scripts = jQuery.map( getAll( fragment, "script" ), disableScript );
hasScripts = scripts.length;