Skip to content

Commit

Permalink
Fixed wrapping of elements that hold text nodes. Thanks to David Flan…
Browse files Browse the repository at this point in the history
…agan for the patch. Fixes #4902.
  • Loading branch information
jeresig committed Jul 14, 2009
1 parent ad5ba1a commit 991dafa
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/manipulation.js
Expand Up @@ -32,7 +32,7 @@ jQuery.fn.extend({
wrap.map(function(){
var elem = this;

while ( elem.firstChild )
while ( elem.firstChild && elem.firstChild.nodeType === 1 )
elem = elem.firstChild;

return elem;
Expand Down
7 changes: 6 additions & 1 deletion test/unit/manipulation.js
Expand Up @@ -10,7 +10,7 @@ test("text()", function() {
});

var testWrap = function(val) {
expect(10);
expect(12);
var defaultText = 'Try them out:'
var result = jQuery('#first').wrap(val( '<div class="red"><span></span></div>' )).text();
equals( defaultText, result, 'Check for wrapping of on-the-fly html' );
Expand Down Expand Up @@ -40,6 +40,11 @@ var testWrap = function(val) {
j = jQuery("<label/>").wrap(val( "<li/>" ));
equals( j[0].nodeName.toUpperCase(), "LABEL", "Element is a label" );
equals( j[0].parentNode.nodeName.toUpperCase(), "LI", "Element has been wrapped" );

// Wrap an element containing a text node
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].parentNode.nodeName.toUpperCase(), "DIV", "And that we're in the div element." );
}

test("wrap(String|Element)", function() {
Expand Down

0 comments on commit 991dafa

Please sign in to comment.