🌐 AI搜索 & 代理 主页
Skip to content

Commit 23d600c

Browse files
committed
Make sure that wrapInner works on elements that have no contents. Fixes #3552.
1 parent 3e9ef6f commit 23d600c

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/manipulation.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,14 @@ jQuery.fn.extend({
7777

7878
wrapInner: function( html ) {
7979
return this.each(function() {
80-
jQuery( this ).contents().wrapAll( html );
80+
var self = jQuery( this ), contents = self.contents();
81+
82+
if ( contents.length ) {
83+
contents.wrapAll( html );
84+
85+
} else {
86+
self.append( html );
87+
}
8188
});
8289
},
8390

test/unit/manipulation.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ test("wrapAll(String|Element)", function() {
145145
// })
146146

147147
var testWrapInner = function(val) {
148-
expect(6);
148+
expect(8);
149149
var num = jQuery("#first").children().length;
150150
var result = jQuery('#first').wrapInner('<div class="red"><div id="tmp"></div></div>');
151151
equals( jQuery("#first").children().length, 1, "Only one child" );
@@ -158,6 +158,11 @@ var testWrapInner = function(val) {
158158
equals( jQuery("#first").children().length, 1, "Only one child" );
159159
ok( jQuery("#first").children().is("#empty"), "Verify Right Element" );
160160
equals( jQuery("#first").children().children().length, num, "Verify Elements Intact" );
161+
162+
var div = jQuery("<div/>");
163+
div.wrapInner("<span></span>");
164+
equals(div.children().length, 1, "The contents were wrapped.");
165+
equals(div.children()[0].nodeName.toLowerCase(), "span", "A span was inserted.");
161166
}
162167

163168
test("wrapInner(String|Element)", function() {

0 commit comments

Comments
 (0)