Commit 47c605f6 authored by Toby Brain's avatar Toby Brain Committed by Dave Methvin

Fix #10177. Pass correct index to function-parameter of `.wrap`

parent 90c019de
...@@ -117,8 +117,10 @@ jQuery.fn.extend({ ...@@ -117,8 +117,10 @@ jQuery.fn.extend({
}, },
wrap: function( html ) { wrap: function( html ) {
return this.each(function() { var isFunction = jQuery.isFunction( html );
jQuery( this ).wrapAll( html );
return this.each(function(i) {
jQuery( this ).wrapAll( isFunction ? html.call(this, i) : html );
}); });
}, },
......
...@@ -135,7 +135,34 @@ test("wrap(String|Element)", function() { ...@@ -135,7 +135,34 @@ test("wrap(String|Element)", function() {
test("wrap(Function)", function() { test("wrap(Function)", function() {
testWrap(functionReturningObj); 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) { var testWrapAll = function(val) {
expect(8); expect(8);
......
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