From ea9ec9527647f0cea396ca536e6f34b4a4491d11 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Fri, 15 Jun 2012 22:17:05 -0400 Subject: [PATCH] Fix #11231, (append|prepend|before|after) w/ array of jQuery objects. Closes gh-666, thanks to @rkatic! --- src/manipulation.js | 4 ++++ test/unit/manipulation.js | 29 +++++++++++++++++++++++++---- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/manipulation.js b/src/manipulation.js index a7751ac82..3c64105c0 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -289,6 +289,10 @@ jQuery.fn.extend({ }, domManip: function( args, table, callback ) { + + // Flatten any nested arrays + args = [].concat.apply( [], args ); + var results, first, fragment, iNoClone, i = 0, value = args[0], diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js index 3c3542ef2..846a8ac67 100644 --- a/test/unit/manipulation.js +++ b/test/unit/manipulation.js @@ -324,6 +324,12 @@ var testAppendForObject = function(valueObj, isFragment) { obj.append(valueObj( 5 )); ok( obj.text().match( /5$/ ), "Check for appending a number" + objType ); + QUnit.reset(); + expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:GoogleYahoo"; + obj = getObj(); + obj.append( valueObj( [ jQuery("#first"), jQuery("#yahoo, #google") ] ) ); + equal( obj.text(), expected, "Check for appending of array of jQuery objects" ); + QUnit.reset(); obj = getObj(); obj.append(valueObj( " text with spaces " )); @@ -355,7 +361,7 @@ var testAppendForObject = function(valueObj, isFragment) { } var testAppend = function(valueObj) { - expect(56); + expect(58); testAppendForObject(valueObj, false); testAppendForObject(valueObj, true); @@ -743,7 +749,7 @@ test("appendTo(String|Element|Array<Element>|jQuery)", function() { }); var testPrepend = function(val) { - expect(5); + expect(6); var defaultText = "Try them out:" var result = jQuery("#first").prepend(val( "buga" )); equal( result.text(), "buga" + defaultText, "Check if text prepending works" ); @@ -763,6 +769,11 @@ var testPrepend = function(val) { expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog"; jQuery("#sap").prepend(val( jQuery("#yahoo, #first") )); equal( jQuery("#sap").text(), expected, "Check for prepending of jQuery object" ); + + QUnit.reset(); + expected = "Try them out:GoogleYahooThis link has class=\"blog\": Simon Willison's Weblog"; + jQuery("#sap").prepend( val( [ jQuery("#first"), jQuery("#yahoo, #google") ] ) ); + equal( jQuery("#sap").text(), expected, "Check for prepending of array of jQuery objects" ); }; test("prepend(String|Element|Array<Element>|jQuery)", function() { @@ -854,7 +865,7 @@ test("prependTo(String|Element|Array<Element>|jQuery)", function() { }); var testBefore = function(val) { - expect(6); + expect(7); var expected = "This is a normal link: bugaYahoo"; jQuery("#yahoo").before(val( "buga" )); equal( jQuery("#en").text(), expected, "Insert String before" ); @@ -874,6 +885,11 @@ var testBefore = function(val) { jQuery("#yahoo").before(val( jQuery("#mark, #first") )); equal( jQuery("#en").text(), expected, "Insert jQuery before" ); + QUnit.reset(); + expected = "This is a normal link: Try them out:GooglediveintomarkYahoo"; + jQuery("#yahoo").before( val( [ jQuery("#first"), jQuery("#mark, #google") ] ) ); + equal( jQuery("#en").text(), expected, "Insert array of jQuery objects before" ); + var set = jQuery("
").before("test"); equal( set[0].nodeName.toLowerCase(), "span", "Insert the element before the disconnected node." ); equal( set.length, 2, "Insert the element before the disconnected node." ); @@ -918,7 +934,7 @@ test("insertBefore(String|Element|Array<Element>|jQuery)", function() { }); var testAfter = function(val) { - expect(6); + expect(7); var expected = "This is a normal link: Yahoobuga"; jQuery("#yahoo").after(val( "buga" )); equal( jQuery("#en").text(), expected, "Insert String after" ); @@ -938,6 +954,11 @@ var testAfter = function(val) { jQuery("#yahoo").after(val( jQuery("#mark, #first") )); equal( jQuery("#en").text(), expected, "Insert jQuery after" ); + QUnit.reset(); + expected = "This is a normal link: YahooTry them out:Googlediveintomark"; + jQuery("#yahoo").after( val( [ jQuery("#first"), jQuery("#mark, #google") ] ) ); + equal( jQuery("#en").text(), expected, "Insert array of jQuery objects after" ); + var set = jQuery("
").after("test"); equal( set[1].nodeName.toLowerCase(), "span", "Insert the element after the disconnected node." ); equal( set.length, 2, "Insert the element after the disconnected node." ); -- 2.39.5