Skip to content

Commit 359b03c

Browse files
committed
Manipulation: make wrapAll funarg execute only once
Fixes jquerygh-1843 Closes jquerygh-1912
1 parent 6f65f5f commit 359b03c

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

src/wrap.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@ jQuery.fn.extend({
99
wrapAll: function( html ) {
1010
var wrap;
1111

12-
if ( jQuery.isFunction( html ) ) {
13-
return this.each(function( i ) {
14-
jQuery( this ).wrapAll( html.call(this, i) );
15-
});
16-
}
17-
1812
if ( this[ 0 ] ) {
13+
if ( jQuery.isFunction( html ) ) {
14+
html = html.call( this[ 0 ] );
15+
}
1916

2017
// The elements to wrap the target around
2118
wrap = jQuery( html, this[ 0 ].ownerDocument ).eq( 0 ).clone( true );

test/unit/wrap.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,36 @@ test( "wrapAll(String)", function() {
148148

149149
});
150150

151+
test( "wrapAll(Function)", 5, function() {
152+
var prev = jQuery( "#firstp" )[ 0 ].previousSibling,
153+
p = jQuery( "#firstp,#first" )[ 0 ].parentNode,
154+
result = jQuery( "#firstp,#first" ).wrapAll(function() {
155+
return "<div class='red'><div class='tmp'></div></div>";
156+
});
157+
158+
equal( result.parent().length, 1, "Check for wrapping of on-the-fly html" );
159+
ok( jQuery( "#first" ).parent().parent().is( ".red" ), "Check if wrapper has class 'red'" );
160+
ok( jQuery( "#firstp" ).parent().parent().is( ".red" ), "Check if wrapper has class 'red'" );
161+
ok( jQuery( "#first" ).parent().parent().parent().is( p ), "Correct Parent" );
162+
strictEqual( jQuery( "#first" ).parent().parent()[ 0 ].previousSibling, prev, "Correct Previous Sibling" );
163+
});
164+
165+
test( "wrapAll(Function) check execution characteristics", 3, function() {
166+
var i = 0;
167+
168+
jQuery( "non-existent" ).wrapAll(function() {
169+
i++;
170+
return "";
171+
});
172+
173+
ok( !i, "should not execute function argument if target element does not exist" );
174+
175+
jQuery( "#firstp" ).wrapAll(function( index ) {
176+
strictEqual( this, jQuery( "#firstp" )[ 0 ], "context must be the first found element" );
177+
strictEqual( index, undefined, "index argument should not be included in function execution" );
178+
});
179+
});
180+
151181
test( "wrapAll(Element)", function() {
152182

153183
expect( 3 );

0 commit comments

Comments
 (0)