From 23d600c66d8e1f7298dcb46eedba862279cd251d Mon Sep 17 00:00:00 2001 From: jeresig Date: Mon, 11 Jan 2010 16:31:31 -0500 Subject: [PATCH] Make sure that wrapInner works on elements that have no contents. Fixes #3552. --- src/manipulation.js | 9 ++++++++- test/unit/manipulation.js | 7 ++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/manipulation.js b/src/manipulation.js index 3db2c35..742ec25 100644 --- a/src/manipulation.js +++ b/src/manipulation.js @@ -77,7 +77,14 @@ jQuery.fn.extend({ wrapInner: function( html ) { return this.each(function() { - jQuery( this ).contents().wrapAll( html ); + var self = jQuery( this ), contents = self.contents(); + + if ( contents.length ) { + contents.wrapAll( html ); + + } else { + self.append( html ); + } }); }, diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js index 3eef122..a0239a4 100644 --- a/test/unit/manipulation.js +++ b/test/unit/manipulation.js @@ -145,7 +145,7 @@ test("wrapAll(String|Element)", function() { // }) var testWrapInner = function(val) { - expect(6); + expect(8); var num = jQuery("#first").children().length; var result = jQuery('#first').wrapInner('
'); equals( jQuery("#first").children().length, 1, "Only one child" ); @@ -158,6 +158,11 @@ var testWrapInner = function(val) { equals( jQuery("#first").children().length, 1, "Only one child" ); ok( jQuery("#first").children().is("#empty"), "Verify Right Element" ); equals( jQuery("#first").children().children().length, num, "Verify Elements Intact" ); + + var div = jQuery("
"); + div.wrapInner(""); + equals(div.children().length, 1, "The contents were wrapped."); + equals(div.children()[0].nodeName.toLowerCase(), "span", "A span was inserted."); } test("wrapInner(String|Element)", function() { -- 1.7.10.4