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

Commit 4e27f17

Browse files
wycatsjeresig
authored andcommitted
Landing in jQuery.contains, jQuery.fn.contains, and jQuery.fn.has support. Fixes #4101.
1 parent 9e60fec commit 4e27f17

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

src/sizzle-jquery.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ jQuery.expr[":"] = jQuery.expr.filters;
44
jQuery.unique = Sizzle.uniqueSort;
55
jQuery.getText = getText;
66
jQuery.isXMLDoc = isXML;
7+
jQuery.contains = contains;
78

89
return;

src/traversing.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,21 @@ jQuery.fn.extend({
5757
return ret;
5858
},
5959

60+
has: function( target ) {
61+
var targets = jQuery( target );
62+
return this.filter(function() {
63+
for ( var i = 0, l = targets.length; i < l; i++ ) {
64+
if ( jQuery.contains( this, targets[i] ) ) {
65+
return true;
66+
}
67+
}
68+
});
69+
},
70+
71+
contains: function( target ) {
72+
return this.has( target ).length > 0;
73+
},
74+
6075
not: function( selector ) {
6176
return this.pushStack( winnow(this, selector, false), "not", selector);
6277
},

test/unit/traversing.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,43 @@ test("not(jQuery)", function() {
152152
expect(1);
153153

154154
same( jQuery("p").not(jQuery("#ap, #sndp, .result")).get(), q("firstp", "en", "sap", "first"), "not(jQuery)" );
155-
})
155+
});
156+
157+
test("has(Element)", function() {
158+
expect(2);
159+
160+
var obj = jQuery("#main").has(jQuery("#sndp")[0]);
161+
same( obj.get(), q("main"), "Keeps elements that have the element as a descendant" );
162+
163+
var multipleParent = jQuery("#main, #header").has(jQuery("#sndp")[0]);
164+
same( obj.get(), q("main"), "Does not include elements that do not have the element as a descendant" );
165+
});
166+
167+
test("has(Selector)", function() {
168+
expect(3);
169+
170+
var obj = jQuery("#main").has("#sndp");
171+
same( obj.get(), q("main"), "Keeps elements that have any element matching the selector as a descendant" );
172+
173+
var multipleParent = jQuery("#main, #header").has("#sndp");
174+
same( obj.get(), q("main"), "Does not include elements that do not have the element as a descendant" );
175+
176+
var multipleHas = jQuery("#main").has("#sndp, #first");
177+
same( multipleHas.get(), q("main"), "Only adds elements once" );
178+
});
179+
180+
test("has(Arrayish)", function() {
181+
expect(3);
182+
183+
var simple = jQuery("#main").has(jQuery("#sndp"));
184+
same( simple.get(), q("main"), "Keeps elements that have any element in the jQuery list as a descendant" );
185+
186+
var multipleParent = jQuery("#main, #header").has(jQuery("#sndp"));
187+
same( multipleParent.get(), q("main"), "Does not include elements that do not have an element in the jQuery list as a descendant" );
188+
189+
var multipleHas = jQuery("#main").has(jQuery("#sndp, #first"));
190+
same( simple.get(), q("main"), "Only adds elements once" );
191+
});
156192

157193
test("andSelf()", function() {
158194
expect(4);

0 commit comments

Comments
 (0)