Commit 75a973da authored by John Resig's avatar John Resig

Made it so that appendTo, etc. return the inserted elements (thus using...

Made it so that appendTo, etc. return the inserted elements (thus using pushStack, as well). Fixes bugs #3966 and #4182.
parent 3e46bce7
...@@ -502,13 +502,13 @@ jQuery.fn = jQuery.prototype = { ...@@ -502,13 +502,13 @@ jQuery.fn = jQuery.prototype = {
if ( this[0] ) { if ( this[0] ) {
var fragment = (this[0].ownerDocument || this[0]).createDocumentFragment(), var fragment = (this[0].ownerDocument || this[0]).createDocumentFragment(),
scripts = jQuery.clean( args, (this[0].ownerDocument || this[0]), fragment ), scripts = jQuery.clean( args, (this[0].ownerDocument || this[0]), fragment ),
first = fragment.firstChild, first = fragment.firstChild;
extra = this.length > 1 ? fragment.cloneNode(true) : fragment;
if ( first ) if ( first )
for ( var i = 0, l = this.length; i < l; i++ ) for ( var i = 0, l = this.length; i < l; i++ )
callback.call( root(this[i], first), i > 0 ? extra.cloneNode(true) : fragment ); callback.call( root(this[i], first), this.length > 1 || i > 0 ?
fragment.cloneNode(true) : fragment );
if ( scripts ) if ( scripts )
jQuery.each( scripts, evalScript ); jQuery.each( scripts, evalScript );
} }
...@@ -1189,13 +1189,16 @@ jQuery.each({ ...@@ -1189,13 +1189,16 @@ jQuery.each({
insertAfter: "after", insertAfter: "after",
replaceAll: "replaceWith" replaceAll: "replaceWith"
}, function(name, original){ }, function(name, original){
jQuery.fn[ name ] = function() { jQuery.fn[ name ] = function( selector ) {
var args = arguments; var ret = [], insert = jQuery( selector );
return this.each(function(){ for ( var i = 0, l = insert.length; i < l; i++ ) {
for ( var i = 0, length = args.length; i < length; i++ ) var elems = (i > 0 ? this.clone(true) : this).get();
jQuery( args[ i ] )[ original ]( this ); jQuery.fn[ original ].apply( jQuery(insert[i]), elems );
}); ret = ret.concat( elems );
}
return this.pushStack( ret, name, selector );
}; };
}); });
......
...@@ -909,7 +909,7 @@ test("append(String|Element|Array&lt;Element&gt;|jQuery)", function() { ...@@ -909,7 +909,7 @@ test("append(String|Element|Array&lt;Element&gt;|jQuery)", function() {
}); });
test("appendTo(String|Element|Array&lt;Element&gt;|jQuery)", function() { test("appendTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
expect(7); expect(12);
var defaultText = 'Try them out:' var defaultText = 'Try them out:'
jQuery('<b>buga</b>').appendTo('#first'); jQuery('<b>buga</b>').appendTo('#first');
equals( jQuery("#first").text(), defaultText + 'buga', 'Check if text appending works' ); equals( jQuery("#first").text(), defaultText + 'buga', 'Check if text appending works' );
...@@ -936,6 +936,27 @@ test("appendTo(String|Element|Array&lt;Element&gt;|jQuery)", function() { ...@@ -936,6 +936,27 @@ test("appendTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
reset(); reset();
jQuery('#select1').appendTo('#foo'); jQuery('#select1').appendTo('#foo');
t( 'Append select', '#foo select', ['select1'] ); t( 'Append select', '#foo select', ['select1'] );
reset();
var div = jQuery("<div/>").click(function(){
ok(true, "Running a cloned click.");
});
div.appendTo("#main, #moretests");
jQuery("#main div:last").click();
jQuery("#moretests div:last").click();
reset();
var div = jQuery("<div/>").appendTo("#main, #moretests");
equals( div.length, 2, "appendTo returns the inserted elements" );
div.addClass("test");
ok( jQuery("#main div:last").hasClass("test"), "appendTo element was modified after the insertion" );
ok( jQuery("#moretests div:last").hasClass("test"), "appendTo element was modified after the insertion" );
reset();
}); });
test("prepend(String|Element|Array&lt;Element&gt;|jQuery)", function() { test("prepend(String|Element|Array&lt;Element&gt;|jQuery)", function() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment