]> source.dussan.org Git - jquery.git/commitdiff
Fix #10177. Pass correct index to function-parameter of `.wrap`
authorToby Brain <tobyb@freshview.com>
Sun, 6 Nov 2011 21:59:41 +0000 (16:59 -0500)
committerDave Methvin <dave.methvin@gmail.com>
Sun, 6 Nov 2011 21:59:41 +0000 (16:59 -0500)
src/manipulation.js
test/unit/manipulation.js

index 6e77e2daa3144dffea340207c8aff2e4c8e05a48..6f0b44803a7935978087c4f4a8dca2ec0a47da2d 100644 (file)
@@ -117,8 +117,10 @@ jQuery.fn.extend({
        },
 
        wrap: function( html ) {
-               return this.each(function() {
-                       jQuery( this ).wrapAll( html );
+               var isFunction = jQuery.isFunction( html );
+
+               return this.each(function(i) {
+                       jQuery( this ).wrapAll( isFunction ? html.call(this, i) : html );
                });
        },
 
index a55a3db0df29d2fdc914f543dbe3285183d8de48..5604642242c6c706ab5d952e1ac7b089342449e2 100644 (file)
@@ -135,7 +135,34 @@ test("wrap(String|Element)", function() {
 
 test("wrap(Function)", function() {
        testWrap(functionReturningObj);
-})
+});
+
+test("wrap(Function) with index (#10177)", function() {
+       var expectedIndex = 0,
+           targets = jQuery("#qunit-fixture p");
+
+       expect(targets.length);
+       targets.wrap(function(i) {
+               equal( i, expectedIndex, "Check if the provided index (" + i + ") is as expected (" + expectedIndex + ")" );
+               expectedIndex++;
+
+               return "<div id='wrap_index_'" + i + "'></div>";
+       });
+});
+
+test("wrap(String) consecutive elements (#10177)", function() {
+       var targets = jQuery("#qunit-fixture p");
+
+       expect(targets.length * 2);
+       targets.wrap("<div class='wrapper'></div>");
+       
+       targets.each(function() {
+               var $this = jQuery(this);
+               
+               ok( $this.parent().is('.wrapper'), "Check each elements parent is correct (.wrapper)" );
+               equal( $this.siblings().length, 0, "Each element should be wrapped individually" );
+       });
+});
 
 var testWrapAll = function(val) {
        expect(8);