Make work wrapInner(fn) work consistently. Fixes #5799.

This commit is contained in:
jeresig 2010-01-25 19:16:23 -05:00
parent 55a64cf095
commit 9f17e70ae9
2 changed files with 20 additions and 8 deletions

View File

@ -77,6 +77,12 @@ jQuery.fn.extend({
}, },
wrapInner: function( html ) { wrapInner: function( html ) {
if ( jQuery.isFunction( html ) ) {
return this.each(function(i) {
jQuery(this).wrapInner( html.call(this, i) );
});
}
return this.each(function() { return this.each(function() {
var self = jQuery( this ), contents = self.contents(); var self = jQuery( this ), contents = self.contents();

View File

@ -140,22 +140,29 @@ test("wrapAll(String|Element)", function() {
}); });
var testWrapInner = function(val) { var testWrapInner = function(val) {
expect(8); expect(11);
var num = jQuery("#first").children().length; var num = jQuery("#first").children().length;
var result = jQuery('#first').wrapInner('<div class="red"><div id="tmp"></div></div>'); var result = jQuery('#first').wrapInner(val('<div class="red"><div id="tmp"></div></div>'));
equals( jQuery("#first").children().length, 1, "Only one child" );
ok( jQuery("#first").children().is(".red"), "Verify Right Element" );
equals( jQuery("#first").children().children().children().length, num, "Verify Elements Intact" );
reset();
var num = jQuery("#first").html("foo<div>test</div><div>test2</div>").children().length;
var result = jQuery('#first').wrapInner(val('<div class="red"><div id="tmp"></div></div>'));
equals( jQuery("#first").children().length, 1, "Only one child" ); equals( jQuery("#first").children().length, 1, "Only one child" );
ok( jQuery("#first").children().is(".red"), "Verify Right Element" ); ok( jQuery("#first").children().is(".red"), "Verify Right Element" );
equals( jQuery("#first").children().children().children().length, num, "Verify Elements Intact" ); equals( jQuery("#first").children().children().children().length, num, "Verify Elements Intact" );
reset(); reset();
var num = jQuery("#first").children().length; var num = jQuery("#first").children().length;
var result = jQuery('#first').wrapInner(document.getElementById('empty')); var result = jQuery('#first').wrapInner(val(document.getElementById('empty')));
equals( jQuery("#first").children().length, 1, "Only one child" ); equals( jQuery("#first").children().length, 1, "Only one child" );
ok( jQuery("#first").children().is("#empty"), "Verify Right Element" ); ok( jQuery("#first").children().is("#empty"), "Verify Right Element" );
equals( jQuery("#first").children().children().length, num, "Verify Elements Intact" ); equals( jQuery("#first").children().children().length, num, "Verify Elements Intact" );
var div = jQuery("<div/>"); var div = jQuery("<div/>");
div.wrapInner("<span></span>"); div.wrapInner(val("<span></span>"));
equals(div.children().length, 1, "The contents were wrapped."); equals(div.children().length, 1, "The contents were wrapped.");
equals(div.children()[0].nodeName.toLowerCase(), "span", "A span was inserted."); equals(div.children()[0].nodeName.toLowerCase(), "span", "A span was inserted.");
} }
@ -164,10 +171,9 @@ test("wrapInner(String|Element)", function() {
testWrapInner(bareObj); testWrapInner(bareObj);
}); });
// TODO: wrapInner uses wrapAll -- get wrapAll working with Function test("wrapInner(Function)", function() {
// test("wrapInner(Function)", function() { testWrapInner(functionReturningObj)
// testWrapInner(functionReturningObj) });
// })
test("unwrap()", function() { test("unwrap()", function() {
expect(9); expect(9);