Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Landing in jQuery.contains, jQuery.fn.contains, and jQuery.fn.has sup…

…port. Fixes #4101.
  • Loading branch information...
commit 4e27f17007c2329e31b449e61bb31197b90a37f1 1 parent 9e60fec
Yehuda Katz authored jeresig committed
1  src/sizzle-jquery.js
@@ -4,5 +4,6 @@ jQuery.expr[":"] = jQuery.expr.filters;
4 4
 jQuery.unique = Sizzle.uniqueSort;
5 5
 jQuery.getText = getText;
6 6
 jQuery.isXMLDoc = isXML;
  7
+jQuery.contains = contains;
7 8
 
8 9
 return;
15  src/traversing.js
@@ -57,6 +57,21 @@ jQuery.fn.extend({
57 57
 		return ret;
58 58
 	},
59 59
 
  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
+
60 75
 	not: function( selector ) {
61 76
 		return this.pushStack( winnow(this, selector, false), "not", selector);
62 77
 	},
38  test/unit/traversing.js
@@ -152,7 +152,43 @@ test("not(jQuery)", function() {
152 152
 	expect(1);
153 153
 
154 154
 	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
+});
156 192
 
157 193
 test("andSelf()", function() {
158 194
 	expect(4);

0 notes on commit 4e27f17

Please sign in to comment.
Something went wrong with that request. Please try again.