We only support wrapping the first element around an element (all others are ignored). Fixes ticket #4903.

This commit is contained in:
John Resig 2009-07-14 23:43:58 +00:00
parent 991dafae16
commit 7a04d1da54
2 changed files with 8 additions and 2 deletions

View file

@ -24,7 +24,7 @@ jQuery.fn.extend({
if ( this[0] ) { if ( this[0] ) {
// The elements to wrap the target around // The elements to wrap the target around
var wrap = jQuery( html, this[0].ownerDocument ).clone(); var wrap = jQuery( html, this[0].ownerDocument ).eq(0).clone();
if ( this[0].parentNode ) if ( this[0].parentNode )
wrap.insertBefore( this[0] ); wrap.insertBefore( this[0] );

View file

@ -10,7 +10,7 @@ test("text()", function() {
}); });
var testWrap = function(val) { var testWrap = function(val) {
expect(12); expect(15);
var defaultText = 'Try them out:' var defaultText = 'Try them out:'
var result = jQuery('#first').wrap(val( '<div class="red"><span></span></div>' )).text(); var result = jQuery('#first').wrap(val( '<div class="red"><span></span></div>' )).text();
equals( defaultText, result, 'Check for wrapping of on-the-fly html' ); equals( defaultText, result, 'Check for wrapping of on-the-fly html' );
@ -45,6 +45,12 @@ var testWrap = function(val) {
j = jQuery("<span/>").wrap("<div>test</div>"); j = jQuery("<span/>").wrap("<div>test</div>");
equals( j[0].previousSibling.nodeType, 3, "Make sure the previous node is a text element" ); equals( j[0].previousSibling.nodeType, 3, "Make sure the previous node is a text element" );
equals( j[0].parentNode.nodeName.toUpperCase(), "DIV", "And that we're in the div element." ); equals( j[0].parentNode.nodeName.toUpperCase(), "DIV", "And that we're in the div element." );
// Try to wrap an element with multiple elements (should fail)
j = jQuery("<div><span></span></div>").children().wrap("<p></p><div></div>");
equals( j[0].parentNode.parentNode.childNodes.length, 1, "There should only be one element wrapping." );
equals( j.length, 1, "There should only be one element (no cloning)." );
equals( j[0].parentNode.nodeName.toUpperCase(), "P", "The span should be in the paragraph." );
} }
test("wrap(String|Element)", function() { test("wrap(String|Element)", function() {