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

Commit 7a67f88

Browse files
committed
Adds detach()
1 parent 29ff710 commit 7a67f88

File tree

2 files changed

+66
-41
lines changed

2 files changed

+66
-41
lines changed

src/manipulation.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jQuery.fn.extend({
3737
jQuery(this).wrapAll( html.apply(this, arguments) );
3838
});
3939
}
40-
40+
4141
if ( this[0] ) {
4242
// The elements to wrap the target around
4343
var wrap = jQuery( html, this[0].ownerDocument ).eq(0).clone();
@@ -183,7 +183,7 @@ jQuery.fn.extend({
183183
cached = true;
184184
}
185185
}
186-
186+
187187
if ( !fragment ) {
188188
fragment = (this[0].ownerDocument || this[0]).createDocumentFragment();
189189
scripts = jQuery.clean( args, (this[0].ownerDocument || this[0]), fragment );
@@ -249,18 +249,21 @@ jQuery.each({
249249
});
250250

251251
jQuery.each({
252-
remove: function( selector ) {
252+
// keepData is for internal use only--do not document
253+
remove: function( selector, keepData ) {
253254
if ( !selector || jQuery.multiFilter( selector, [ this ] ).length ) {
254-
if ( this.nodeType === 1 ) {
255+
if ( !keepData && this.nodeType === 1 ) {
255256
cleanData( jQuery("*", this).add(this) );
256257
}
257258

258-
if ( this.parentNode ) {
259-
this.parentNode.removeChild( this );
260-
}
259+
this.parentNode && this.parentNode.removeChild( this );
261260
}
262261
},
263262

263+
detach: function( selector ) {
264+
jQuery( this ).remove( selector, true )
265+
},
266+
264267
empty: function() {
265268
// Remove element nodes and prevent memory leaks
266269
if ( this.nodeType === 1 ) {

test/unit/manipulation.js

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ var testWrapAll = function(val) {
6565
expect(8);
6666
var prev = jQuery("#firstp")[0].previousSibling;
6767
var p = jQuery("#firstp,#first")[0].parentNode;
68-
68+
6969
var result = jQuery('#firstp,#first').wrapAll(val( '<div class="red"><div class="tmp"></div></div>' ));
7070
equals( result.parent().length, 1, 'Check for wrapping of on-the-fly html' );
7171
ok( jQuery('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
@@ -79,7 +79,7 @@ var testWrapAll = function(val) {
7979
var result = jQuery('#firstp,#first').wrapAll(val( document.getElementById('empty') ));
8080
equals( jQuery("#first").parent()[0], jQuery("#firstp").parent()[0], "Same Parent" );
8181
equals( jQuery("#first").parent()[0].previousSibling, prev, "Correct Previous Sibling" );
82-
equals( jQuery("#first").parent()[0].parentNode, p, "Correct Parent" );
82+
equals( jQuery("#first").parent()[0].parentNode, p, "Correct Parent" );
8383
}
8484

8585
test("wrapAll(String|Element)", function() {
@@ -104,7 +104,7 @@ var testWrapInner = function(val) {
104104
var result = jQuery('#first').wrapInner(document.getElementById('empty'));
105105
equals( jQuery("#first").children().length, 1, "Only one child" );
106106
ok( jQuery("#first").children().is("#empty"), "Verify Right Element" );
107-
equals( jQuery("#first").children().children().length, num, "Verify Elements Intact" );
107+
equals( jQuery("#first").children().children().length, num, "Verify Elements Intact" );
108108
}
109109

110110
test("wrapInner(String|Element)", function() {
@@ -250,7 +250,7 @@ test("appendTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
250250
var div = jQuery("<div/>").appendTo("#main, #moretests");
251251

252252
equals( div.length, 2, "appendTo returns the inserted elements" );
253-
253+
254254
div.addClass("test");
255255

256256
ok( jQuery("#main div:last").hasClass("test"), "appendTo element was modified after the insertion" );
@@ -390,7 +390,7 @@ var testAfter = function(val) {
390390
reset();
391391
expected = "This is a normal link: YahoodiveintomarkTry them out:";
392392
jQuery('#yahoo').after(val( jQuery("#first, #mark") ));
393-
equals( expected, jQuery('#en').text(), "Insert jQuery after" );
393+
equals( expected, jQuery('#en').text(), "Insert jQuery after" );
394394
};
395395

396396
test("after(String|Element|Array&lt;Element&gt;|jQuery)", function() {
@@ -444,7 +444,7 @@ var testReplaceWith = function(val) {
444444
jQuery('#yahoo').replaceWith(val( jQuery("#first, #mark") ));
445445
ok( jQuery("#first")[0], 'Replace element with set of elements' );
446446
ok( jQuery("#mark")[0], 'Replace element with set of elements' );
447-
ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
447+
ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
448448
}
449449

450450
test("replaceWith(String|Element|Array&lt;Element&gt;|jQuery)", function() {
@@ -460,7 +460,7 @@ test("replaceAll(String|Element|Array&lt;Element&gt;|jQuery)", function() {
460460
jQuery('<b id="replace">buga</b>').replaceAll("#yahoo");
461461
ok( jQuery("#replace")[0], 'Replace element with string' );
462462
ok( !jQuery("#yahoo")[0], 'Verify that original element is gone, after string' );
463-
463+
464464
reset();
465465
jQuery(document.getElementById('first')).replaceAll("#yahoo");
466466
ok( jQuery("#first")[0], 'Replace element with element' );
@@ -549,32 +549,32 @@ test("val()", function() {
549549

550550
document.getElementById('text1').value = "bla";
551551
equals( jQuery("#text1").val(), "bla", "Check for modified value of input element" );
552-
552+
553553
reset();
554554

555555
equals( jQuery("#text1").val(), "Test", "Check for value of input element" );
556556
// ticket #1714 this caused a JS error in IE
557557
equals( jQuery("#first").val(), "", "Check a paragraph element to see if it has a value" );
558558
ok( jQuery([]).val() === undefined, "Check an empty jQuery object will return undefined from val" );
559-
559+
560560
equals( jQuery('#select2').val(), '3', 'Call val() on a single="single" select' );
561561

562562
isSet( jQuery('#select3').val(), ['1', '2'], 'Call val() on a multiple="multiple" select' );
563563

564564
equals( jQuery('#option3c').val(), '2', 'Call val() on a option element with value' );
565-
565+
566566
equals( jQuery('#option3a').val(), '', 'Call val() on a option element with empty value' );
567-
567+
568568
equals( jQuery('#option3e').val(), 'no value', 'Call val() on a option element with no value attribute' );
569-
569+
570570
});
571571

572572
var testVal = function(valueObj) {
573573
expect(5);
574574

575575
jQuery("#text1").val(valueObj( 'test' ));
576576
equals( document.getElementById('text1').value, "test", "Check for modified (via val(String)) value of input element" );
577-
577+
578578
jQuery("#text1").val(valueObj( 67 ));
579579
equals( document.getElementById('text1').value, "67", "Check for modified (via val(Number)) value of input element" );
580580

@@ -601,51 +601,51 @@ test("val(Function)", function() {
601601

602602
var testHtml = function(valueObj) {
603603
expect(17);
604-
604+
605605
window.debug = true;
606-
606+
607607
jQuery.scriptorder = 0;
608-
608+
609609
var div = jQuery("#main > div");
610610
div.html(valueObj("<b>test</b>"));
611611
var pass = true;
612612
for ( var i = 0; i < div.size(); i++ ) {
613613
if ( div.get(i).childNodes.length != 1 ) pass = false;
614614
}
615615
ok( pass, "Set HTML" );
616-
616+
617617
window.debug = false;
618618

619619
reset();
620620
// using contents will get comments regular, text, and comment nodes
621621
var j = jQuery("#nonnodes").contents();
622622
j.html(valueObj("<b>bold</b>"));
623-
623+
624624
// this is needed, or the expando added by jQuery unique will yield a different html
625625
j.find('b').removeData();
626626
equals( j.html().replace(/ xmlns="[^"]+"/g, "").toLowerCase(), "<b>bold</b>", "Check node,textnode,comment with html()" );
627-
627+
628628
jQuery("#main").html(valueObj("<select/>"));
629629
jQuery("#main select").html(valueObj("<option>O1</option><option selected='selected'>O2</option><option>O3</option>"));
630630
equals( jQuery("#main select").val(), "O2", "Selected option correct" );
631-
631+
632632
var $div = jQuery('<div />');
633633
equals( $div.html(valueObj( 5 )).html(), '5', 'Setting a number as html' );
634634
equals( $div.html(valueObj( 0 )).html(), '0', 'Setting a zero as html' );
635-
635+
636636
reset();
637-
637+
638638
jQuery("#main").html(valueObj('<script type="something/else">ok( false, "Non-script evaluated." );</script><script type="text/javascript">ok( true, "text/javascript is evaluated." );</script><script>ok( true, "No type is evaluated." );</script><div><script type="text/javascript">ok( true, "Inner text/javascript is evaluated." );</script><script>ok( true, "Inner No type is evaluated." );</script><script type="something/else">ok( false, "Non-script evaluated." );</script></div>'));
639-
639+
640640
stop();
641-
641+
642642
jQuery("#main").html(valueObj('<script type="text/javascript">ok( true, "jQuery().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script>'));
643-
643+
644644
jQuery("#main").html(valueObj('foo <form><script type="text/javascript">ok( true, "jQuery().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script></form>'));
645-
645+
646646
// it was decided that waiting to execute ALL scripts makes sense since nested ones have to wait anyway so this test case is changed, see #1959
647647
jQuery("#main").html(valueObj("<script>equals(jQuery.scriptorder++, 0, 'Script is executed in order');equals(jQuery('#scriptorder').length, 1,'Execute after html (even though appears before)')<\/script><span id='scriptorder'><script>equals(jQuery.scriptorder++, 1, 'Script (nested) is executed in order');equals(jQuery('#scriptorder').length, 1,'Execute after html')<\/script></span><script>equals(jQuery.scriptorder++, 2, 'Script (unnested) is executed in order');equals(jQuery('#scriptorder').length, 1,'Execute after html')<\/script>"));
648-
648+
649649
setTimeout( start, 100 );
650650
}
651651

@@ -666,7 +666,7 @@ var testText = function(valueObj) {
666666
j.text("hi!");
667667
equals( jQuery(j[0]).text(), "hi!", "Check node,textnode,comment with text()" );
668668
equals( j[1].nodeValue, " there ", "Check node,textnode,comment with text()" );
669-
equals( j[2].nodeType, 8, "Check node,textnode,comment with text()" );
669+
equals( j[2].nodeType, 8, "Check node,textnode,comment with text()" );
670670
}
671671

672672
test("text(String)", function() {
@@ -677,24 +677,46 @@ test("text(Function)", function() {
677677
testText(functionReturningObj);
678678
})
679679

680-
test("remove()", function() {
681-
expect(7);
682-
jQuery("#ap").children().remove();
680+
var testRemove = function(method) {
681+
expect(9);
682+
683+
var first = jQuery("#ap").children(":first");
684+
first.data("foo", "bar");
685+
686+
jQuery("#ap").children()[method]();
683687
ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
684688
equals( jQuery("#ap").children().length, 0, "Check remove" );
685689

690+
equals( first.data("foo"), method == "remove" ? null : "bar" );
691+
686692
reset();
687-
jQuery("#ap").children().remove("a");
693+
jQuery("#ap").children()[method]("a");
688694
ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
689695
equals( jQuery("#ap").children().length, 1, "Check filtered remove" );
690696

691-
jQuery("#ap").children().remove("a, code");
697+
jQuery("#ap").children()[method]("a, code");
692698
equals( jQuery("#ap").children().length, 0, "Check multi-filtered remove" );
693699

694700
// using contents will get comments regular, text, and comment nodes
695701
equals( jQuery("#nonnodes").contents().length, 3, "Check node,textnode,comment remove works" );
696-
jQuery("#nonnodes").contents().remove();
702+
jQuery("#nonnodes").contents()[method]();
697703
equals( jQuery("#nonnodes").contents().length, 0, "Check node,textnode,comment remove works" );
704+
705+
reset();
706+
707+
var count = 0;
708+
var first = jQuery("#ap").children(":first");
709+
first.click(function() { count++ })[method]().appendTo("body").click();
710+
711+
equals( method == "remove" ? 0 : 1, count );
712+
};
713+
714+
test("remove()", function() {
715+
testRemove("remove");
716+
});
717+
718+
test("detach()", function() {
719+
testRemove("detach");
698720
});
699721

700722
test("empty()", function() {

0 commit comments

Comments
 (0)