Bug Tracker

Ticket #4101: 0001-Adds-.fn.has-depends-on-Sizzle-patch.patch

File 0001-Adds-.fn.has-depends-on-Sizzle-patch.patch, 2.6 KB (added by yehuda, 7 years ago)
  • src/traversing.js

    From 6b860c88e1edb6833a57905d9dc38a0e378e6e06 Mon Sep 17 00:00:00 2001
    From: Yehuda Katz <wycats@gmail.com>
    Date: Tue, 21 Jul 2009 00:51:11 -0700
    Subject: [PATCH] Adds $.fn.has (depends on Sizzle patch)
    
    ---
     src/traversing.js       |    8 ++++++++
     test/unit/traversing.js |   38 +++++++++++++++++++++++++++++++++++++-
     2 files changed, 45 insertions(+), 1 deletions(-)
    
    diff --git a/src/traversing.js b/src/traversing.js
    index 669c70a..f6b7e62 100644
    a b jQuery.fn.extend({ 
    4343                return ret;
    4444        },
    4545
     46        has: function( target ) {
     47          var targets = jQuery(target);
     48          return this.filter(function() {
     49                        for( var i=0, j=targets.length; i<j; i++ )
     50                                if(jQuery.contains( this, targets[i] )) return true;
     51          });
     52        },
     53
    4654        not: function( selector ) {
    4755                return this.pushStack( jQuery.winnow(this, selector, false), "not", selector);
    4856        },
  • test/unit/traversing.js

    diff --git a/test/unit/traversing.js b/test/unit/traversing.js
    index f585d9c..8e0fc04 100644
    a b test("not(jQuery)", function() { 
    135135        expect(1);
    136136
    137137        isSet( jQuery("p").not(jQuery("#ap, #sndp, .result")).get(), q("firstp", "en", "sap", "first"), "not(jQuery)" );
    138 })
     138});
     139
     140test("has(Element)", function() {
     141        expect(2);
     142
     143        var obj = jQuery("#main").has(jQuery("#sndp")[0]);
     144        isSet( obj, q("main"), "Keeps elements that have the element as a descendant" );
     145
     146        var multipleParent = jQuery("#main, #header").has(jQuery("#sndp")[0]);
     147        isSet( obj, q("main"), "Does not include elements that do not have the element as a descendant" );
     148});
     149
     150test("has(Selector)", function() {
     151        expect(3);
     152
     153        var obj = jQuery("#main").has("#sndp");
     154        isSet( obj, q("main"), "Keeps elements that have any element matching the selector as a descendant" );
     155
     156        var multipleParent = jQuery("#main, #header").has("#sndp");
     157        isSet( obj, q("main"), "Does not include elements that do not have the element as a descendant" );
     158
     159        var multipleHas = jQuery("#main").has("#sndp, #first");
     160        isSet( multipleHas, q("main"), "Only adds elements once" );
     161});
     162
     163test("has(Arrayish)", function() {
     164        expect(3);
     165
     166        var simple = jQuery("#main").has(jQuery("#sndp"));
     167        isSet( simple, q("main"), "Keeps elements that have any element in the jQuery list as a descendant" );
     168
     169        var multipleParent = jQuery("#main, #header").has(jQuery("#sndp"));
     170        isSet( multipleParent, q("main"), "Does not include elements that do not have an element in the jQuery list as a descendant" );
     171
     172        var multipleHas = jQuery("#main").has(jQuery("#sndp, #first"));
     173        isSet( simple, q("main"), "Only adds elements once" );
     174});
    139175
    140176test("andSelf()", function() {
    141177        expect(4);