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

Commit 173c147

Browse files
committed
Added support for .before(), .after(), and .replaceWith() on disconnected DOM nodes. Fixes bug #3940.
1 parent bca8225 commit 173c147

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

src/manipulation.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,29 @@ jQuery.fn.extend({
106106
},
107107

108108
before: function() {
109-
return this.domManip(arguments, false, function(elem){
110-
this.parentNode.insertBefore( elem, this );
111-
});
109+
if ( this[0] && this[0].parentNode ) {
110+
return this.domManip(arguments, false, function(elem){
111+
this.parentNode.insertBefore( elem, this );
112+
});
113+
} else {
114+
var set = jQuery.isFunction(arguments[0]) ?
115+
jQuery( arguments[0]() ) :
116+
jQuery.apply(jQuery, arguments);
117+
118+
return this.pushStack( set.add( this ), "before", arguments );
119+
}
112120
},
113121

114122
after: function() {
115-
return this.domManip(arguments, false, function(elem){
116-
this.parentNode.insertBefore( elem, this.nextSibling );
117-
});
123+
if ( this[0] && this[0].parentNode ) {
124+
return this.domManip(arguments, false, function(elem){
125+
this.parentNode.insertBefore( elem, this.nextSibling );
126+
});
127+
} else {
128+
return jQuery.isFunction(arguments[0]) ?
129+
this.add( arguments[0]() ) :
130+
this.add.apply( this, arguments );
131+
}
118132
},
119133

120134
clone: function( events ) {
@@ -193,7 +207,11 @@ jQuery.fn.extend({
193207
},
194208

195209
replaceWith: function( value ) {
196-
return this.after( value ).remove();
210+
if ( this[0] && this[0].parentNode ) {
211+
return this.after( value ).remove();
212+
} else {
213+
return this.pushStack( jQuery(jQuery.isFunction(value) ? value() : value), "replaceWith", value );
214+
}
197215
},
198216

199217
detach: function( selector ) {

test/unit/manipulation.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ test("prependTo(String|Element|Array<Element>|jQuery)", function() {
320320
});
321321

322322
var testBefore = function(val) {
323-
expect(4);
323+
expect(6);
324324
var expected = 'This is a normal link: bugaYahoo';
325325
jQuery('#yahoo').before(val( '<b>buga</b>' ));
326326
equals( expected, jQuery('#en').text(), 'Insert String before' );
@@ -339,6 +339,10 @@ var testBefore = function(val) {
339339
expected = "This is a normal link: diveintomarkTry them out:Yahoo";
340340
jQuery('#yahoo').before(val( jQuery("#first, #mark") ));
341341
equals( expected, jQuery('#en').text(), "Insert jQuery before" );
342+
343+
var set = jQuery("<div/>").before(val("<span>test</span>"));
344+
equals( set[0].nodeName.toLowerCase(), "span", "Insert the element before the disconnected node." );
345+
equals( set.length, 2, "Insert the element before the disconnected node." );
342346
}
343347

344348
test("before(String|Element|Array&lt;Element&gt;|jQuery)", function() {
@@ -372,7 +376,7 @@ test("insertBefore(String|Element|Array&lt;Element&gt;|jQuery)", function() {
372376
});
373377

374378
var testAfter = function(val) {
375-
expect(4);
379+
expect(6);
376380
var expected = 'This is a normal link: Yahoobuga';
377381
jQuery('#yahoo').after(val( '<b>buga</b>' ));
378382
equals( expected, jQuery('#en').text(), 'Insert String after' );
@@ -391,6 +395,10 @@ var testAfter = function(val) {
391395
expected = "This is a normal link: YahoodiveintomarkTry them out:";
392396
jQuery('#yahoo').after(val( jQuery("#first, #mark") ));
393397
equals( expected, jQuery('#en').text(), "Insert jQuery after" );
398+
399+
var set = jQuery("<div/>").after(val("<span>test</span>"));
400+
equals( set[1].nodeName.toLowerCase(), "span", "Insert the element after the disconnected node." );
401+
equals( set.length, 2, "Insert the element after the disconnected node." );
394402
};
395403

396404
test("after(String|Element|Array&lt;Element&gt;|jQuery)", function() {
@@ -424,7 +432,7 @@ test("insertAfter(String|Element|Array&lt;Element&gt;|jQuery)", function() {
424432
});
425433

426434
var testReplaceWith = function(val) {
427-
expect(10);
435+
expect(12);
428436
jQuery('#yahoo').replaceWith(val( '<b id="replace">buga</b>' ));
429437
ok( jQuery("#replace")[0], 'Replace element with string' );
430438
ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
@@ -445,6 +453,10 @@ var testReplaceWith = function(val) {
445453
ok( jQuery("#first")[0], 'Replace element with set of elements' );
446454
ok( jQuery("#mark")[0], 'Replace element with set of elements' );
447455
ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
456+
457+
var set = jQuery("<div/>").replaceWith(val("<span>test</span>"));
458+
equals( set[0].nodeName.toLowerCase(), "span", "Replace the disconnected node." );
459+
equals( set.length, 1, "Replace the disconnected node." );
448460
}
449461

450462
test("replaceWith(String|Element|Array&lt;Element&gt;|jQuery)", function() {

0 commit comments

Comments
 (0)