From 3dcee021603d5c25cd429cb25c2de5046044ca4c Mon Sep 17 00:00:00 2001 From: Oleg Gaidarenko Date: Wed, 24 Dec 2014 01:56:21 +0300 Subject: [PATCH] Manipulation: make wrapAll funarg execute only once Ref 359b03cac74d7336676a6992f14b7ccab9b28659 --- src/wrap.js | 18 +++++++------- test/unit/wrap.js | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 9 deletions(-) diff --git a/src/wrap.js b/src/wrap.js index 93c7b2cfd..053b9b5b9 100644 --- a/src/wrap.js +++ b/src/wrap.js @@ -7,18 +7,18 @@ define([ jQuery.fn.extend({ wrapAll: function( html ) { - if ( jQuery.isFunction( html ) ) { - return this.each(function(i) { - jQuery(this).wrapAll( html.call(this, i) ); - }); - } + var wrap; + + if ( this[ 0 ] ) { + if ( jQuery.isFunction( html ) ) { + html = html.call( this[ 0 ] ); + } - if ( this[0] ) { // The elements to wrap the target around - var wrap = jQuery( html, this[0].ownerDocument ).eq(0).clone(true); + wrap = jQuery( html, this[ 0 ].ownerDocument ).eq( 0 ).clone( true ); - if ( this[0].parentNode ) { - wrap.insertBefore( this[0] ); + if ( this[ 0 ].parentNode ) { + wrap.insertBefore( this[ 0 ] ); } wrap.map(function() { diff --git a/test/unit/wrap.js b/test/unit/wrap.js index 114e9faf8..ffc5a92e7 100644 --- a/test/unit/wrap.js +++ b/test/unit/wrap.js @@ -149,6 +149,66 @@ test( "wrapAll(String)", function() { }); +test( "wrapAll(Function)", 5, function() { + var prev = jQuery( "#firstp" )[ 0 ].previousSibling, + p = jQuery( "#firstp,#first" )[ 0 ].parentNode, + result = jQuery( "#firstp,#first" ).wrapAll(function() { + return "
"; + }); + + equal( result.parent().length, 1, "Check for wrapping of on-the-fly html" ); + ok( jQuery( "#first" ).parent().parent().is( ".red" ), "Check if wrapper has class 'red'" ); + ok( jQuery( "#firstp" ).parent().parent().is( ".red" ), "Check if wrapper has class 'red'" ); + ok( jQuery( "#first" ).parent().parent().parent().is( p ), "Correct Parent" ); + strictEqual( jQuery( "#first" ).parent().parent()[ 0 ].previousSibling, prev, "Correct Previous Sibling" ); +}); + +test( "wrapAll(Function) check execution characteristics", 3, function() { + var i = 0; + + jQuery( "non-existent" ).wrapAll(function() { + i++; + return ""; + }); + + ok( !i, "should not execute function argument if target element does not exist" ); + + jQuery( "#firstp" ).wrapAll(function( index ) { + strictEqual( this, jQuery( "#firstp" )[ 0 ], "context must be the first found element" ); + strictEqual( index, undefined, "index argument should not be included in function execution" ); + }); +}); + +test( "wrapAll(Function)", 5, function() { + var prev = jQuery( "#firstp" )[ 0 ].previousSibling, + p = jQuery( "#firstp,#first" )[ 0 ].parentNode, + result = jQuery( "#firstp,#first" ).wrapAll(function() { + return "
"; + }); + + equal( result.parent().length, 1, "Check for wrapping of on-the-fly html" ); + ok( jQuery( "#first" ).parent().parent().is( ".red" ), "Check if wrapper has class 'red'" ); + ok( jQuery( "#firstp" ).parent().parent().is( ".red" ), "Check if wrapper has class 'red'" ); + ok( jQuery( "#first" ).parent().parent().parent().is( p ), "Correct Parent" ); + strictEqual( jQuery( "#first" ).parent().parent()[ 0 ].previousSibling, prev, "Correct Previous Sibling" ); +}); + +test( "wrapAll(Function) check execution characteristics", 3, function() { + var i = 0; + + jQuery( "non-existent" ).wrapAll(function() { + i++; + return ""; + }); + + ok( !i, "should not execute function argument if target element does not exist" ); + + jQuery( "#firstp" ).wrapAll(function( index ) { + strictEqual( this, jQuery( "#firstp" )[ 0 ], "context must be the first found element" ); + strictEqual( index, undefined, "index argument should not be included in function execution" ); + }); +}); + test( "wrapAll(Element)", function() { expect( 3 ); -- 2.39.5