Skip to content

Commit e2eac3f

Browse files
committed
Fix #12120. Always stack .before/.after, and fix disconnected nodes.
1 parent 2263134 commit e2eac3f

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

src/manipulation.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,7 @@ jQuery.fn.extend({
148148
});
149149
}
150150

151-
if ( arguments.length ) {
152-
var set = jQuery.clean( arguments );
153-
return this.pushStack( jQuery.merge( set, this ), "before", this.selector );
154-
}
151+
return this.pushStack( jQuery.merge( jQuery.clean( arguments ), this ), "before", this.selector );
155152
},
156153

157154
after: function() {
@@ -161,10 +158,7 @@ jQuery.fn.extend({
161158
});
162159
}
163160

164-
if ( arguments.length ) {
165-
var set = jQuery.clean( arguments );
166-
return this.pushStack( jQuery.merge( this, set ), "after", this.selector );
167-
}
161+
return this.pushStack( jQuery.merge( this.toArray(), jQuery.clean( arguments ) ), "after", this.selector );
168162
},
169163

170164
// keepData is for internal use only--do not document
@@ -721,6 +715,7 @@ jQuery.extend({
721715

722716
// Fix #11356: Clear elements from safeFragment
723717
if ( div ) {
718+
div.innerHTML = "";
724719
safe.removeChild( div );
725720
elem = div = safe = null;
726721
}

test/unit/manipulation.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -909,12 +909,13 @@ test("before(Function)", function() {
909909
testBefore(manipulationFunctionReturningObj);
910910
});
911911

912-
test("before and after w/ empty object (#10812)", function() {
913-
expect(2);
912+
test("before and after w/ empty object (#10812, #12120)", function() {
913+
expect(3);
914914

915-
var res = jQuery( "#notInTheDocument" ).before( "(" ).after( ")" );
916-
equal( res.length, 2, "didn't choke on empty object" );
917-
equal( res.wrapAll("<div/>").parent().text(), "()", "correctly appended text" );
915+
var res = jQuery("#notInTheDocument").before("<span>(</span>").after("<span>)</span>");
916+
equal( res.length, 2, "added two elements to the empty object" );
917+
equal( res.text(), "()", "correctly appended text" );
918+
equal( res.end().text(), "(", "stacked the previous value" );
918919
});
919920

920921
test("before and after on disconnected node (#10517)", function() {

0 commit comments

Comments
 (0)