From 4e27f17007c2329e31b449e61bb31197b90a37f1 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Wed, 9 Dec 2009 12:43:13 -0800 Subject: [PATCH] Landing in jQuery.contains, jQuery.fn.contains, and jQuery.fn.has support. Fixes #4101. --- src/sizzle-jquery.js | 1 + src/traversing.js | 15 +++++++++++++++ test/unit/traversing.js | 38 +++++++++++++++++++++++++++++++++++++- 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/sizzle-jquery.js b/src/sizzle-jquery.js index cb4c8e173..4243c504c 100644 --- a/src/sizzle-jquery.js +++ b/src/sizzle-jquery.js @@ -4,5 +4,6 @@ jQuery.expr[":"] = jQuery.expr.filters; jQuery.unique = Sizzle.uniqueSort; jQuery.getText = getText; jQuery.isXMLDoc = isXML; +jQuery.contains = contains; return; diff --git a/src/traversing.js b/src/traversing.js index 57621a390..d6947ace6 100644 --- a/src/traversing.js +++ b/src/traversing.js @@ -57,6 +57,21 @@ jQuery.fn.extend({ return ret; }, + has: function( target ) { + var targets = jQuery( target ); + return this.filter(function() { + for ( var i = 0, l = targets.length; i < l; i++ ) { + if ( jQuery.contains( this, targets[i] ) ) { + return true; + } + } + }); + }, + + contains: function( target ) { + return this.has( target ).length > 0; + }, + not: function( selector ) { return this.pushStack( winnow(this, selector, false), "not", selector); }, diff --git a/test/unit/traversing.js b/test/unit/traversing.js index 7b046e3d1..a235bbd5a 100644 --- a/test/unit/traversing.js +++ b/test/unit/traversing.js @@ -152,7 +152,43 @@ test("not(jQuery)", function() { expect(1); same( jQuery("p").not(jQuery("#ap, #sndp, .result")).get(), q("firstp", "en", "sap", "first"), "not(jQuery)" ); -}) +}); + +test("has(Element)", function() { + expect(2); + + var obj = jQuery("#main").has(jQuery("#sndp")[0]); + same( obj.get(), q("main"), "Keeps elements that have the element as a descendant" ); + + var multipleParent = jQuery("#main, #header").has(jQuery("#sndp")[0]); + same( obj.get(), q("main"), "Does not include elements that do not have the element as a descendant" ); +}); + +test("has(Selector)", function() { + expect(3); + + var obj = jQuery("#main").has("#sndp"); + same( obj.get(), q("main"), "Keeps elements that have any element matching the selector as a descendant" ); + + var multipleParent = jQuery("#main, #header").has("#sndp"); + same( obj.get(), q("main"), "Does not include elements that do not have the element as a descendant" ); + + var multipleHas = jQuery("#main").has("#sndp, #first"); + same( multipleHas.get(), q("main"), "Only adds elements once" ); +}); + +test("has(Arrayish)", function() { + expect(3); + + var simple = jQuery("#main").has(jQuery("#sndp")); + same( simple.get(), q("main"), "Keeps elements that have any element in the jQuery list as a descendant" ); + + var multipleParent = jQuery("#main, #header").has(jQuery("#sndp")); + same( multipleParent.get(), q("main"), "Does not include elements that do not have an element in the jQuery list as a descendant" ); + + var multipleHas = jQuery("#main").has(jQuery("#sndp, #first")); + same( simple.get(), q("main"), "Only adds elements once" ); +}); test("andSelf()", function() { expect(4); -- 2.39.5