Lots of improvements. Added support for .not( jQuery("...") ) and support for multi...
authorJohn Resig <jeresig@gmail.com>
Wed, 10 Jan 2007 17:54:42 +0000 (17:54 +0000)
committerJohn Resig <jeresig@gmail.com>
Wed, 10 Jan 2007 17:54:42 +0000 (17:54 +0000)
src/jquery/coreTest.js
src/jquery/jquery.js
src/selector/selector.js

index d8328c9..ccd84f7 100644 (file)
@@ -472,9 +472,8 @@ test("html(String)", function() {
 });\r
 \r
 test("filter()", function() {\r
-       expect(5);\r
+       expect(4);\r
        isSet( $("input").filter(":checked").get(), q("radio2", "check1"), "filter(String)" );\r
-       isSet( $("p").filter(["#ap", "#sndp"]).get(), q("ap", "sndp"), "filter(Array&lt;String&gt;)" );\r
        isSet( $("p").filter("#ap, #sndp").get(), q("ap", "sndp"), "filter('String, String')" );\r
        isSet( $("p").filter("#ap,#sndp").get(), q("ap", "sndp"), "filter('String,String')" );\r
        isSet( $("p").filter(function() { return !$("a", this).length }).get(), q("sndp", "first"), "filter(Function)" );\r
@@ -483,7 +482,7 @@ test("filter()", function() {
 test("not(String)", function() {\r
        expect(2);\r
        ok( $("#main > p#ap > a").not("#google").length == 2, "not('selector')" );\r
-       isSet( $("p").not("#ap, #sndp").get(), q("firstp", "en", "sap", "first", "result"), "not('selector, selector')" );\r
+       isSet( $("p").not("#ap, #sndp, .result").get(), q("firstp", "en", "sap", "first"), "not('selector, selector')" );\r
 });\r
 \r
 \r
@@ -647,4 +646,4 @@ test("eq(), gt(), lt(), contains()", function() {
        isSet( $("#ap a").gt(0).get(), q("groups", "anchor1", "mark"), "gt()" );\r
        isSet( $("#ap a").lt(3).get(), q("google", "groups", "anchor1"), "lt()" );\r
        isSet( $("#foo a").contains("log").get(), q("anchor2", "simon"), "contains()" );\r
-});
\ No newline at end of file
+});\r
index 401fd14..cc0af52 100644 (file)
@@ -812,21 +812,21 @@ jQuery.fn = jQuery.prototype = {
         * match the specified expression(s). This method is used to narrow down
         * the results of a search.
         *
-        * Provide a String array of expressions to apply multiple filters at once.
+        * Provide a comma-separated list of expressions to apply multiple filters at once.
         *
         * @example $("p").filter(".selected")
         * @before <p class="selected">Hello</p><p>How are you?</p>
         * @result [ <p class="selected">Hello</p> ]
         * @desc Selects all paragraphs and removes those without a class "selected".
         *
-        * @example $("p").filter([".selected", ":first"])
+        * @example $("p").filter(".selected, :first")
         * @before <p>Hello</p><p>Hello Again</p><p class="selected">And Again</p>
         * @result [ <p>Hello</p>, <p class="selected">And Again</p> ]
         * @desc Selects all paragraphs and removes those without class "selected" and being the first one.
         *
         * @name filter
         * @type jQuery
-        * @param String|Array<String> expression Expression(s) to search with.
+        * @param String expression Expression(s) to search with.
         * @cat DOM/Traversing
         */
         
@@ -849,21 +849,12 @@ jQuery.fn = jQuery.prototype = {
         */
        filter: function(t) {
                return this.pushStack(
-                       t.constructor == Array &&
-                       jQuery.map(this,function(a){
-                               for ( var i = 0, tl = t.length; i < tl; i++ )
-                                       if ( jQuery.filter(t[i],[a]).r.length )
-                                               return a;
-                               return null;
+                       t.constructor == Function &&
+                       jQuery.grep(this, function(el, index){
+                               return t.apply(el, [index])
                        }) ||
 
-                       t.constructor == Boolean &&
-                       ( t ? this.get() : [] ) ||
-
-                       typeof t == "function" &&
-                       jQuery.grep( this, function(el, index) { return t.apply(el, [index]) }) ||
-
-                       jQuery.filter(t,this).r );
+                       jQuery.multiFilter(t,this) );
        },
 
        /**
@@ -896,10 +887,33 @@ jQuery.fn = jQuery.prototype = {
         * @param String expr An expression with which to remove matching elements
         * @cat DOM/Traversing
         */
+
+       /**
+        * Removes any elements inside the array of elements from the set
+        * of matched elements. This method is used to remove one or more
+        * elements from a jQuery object.
+        *
+        * @example $("p").not( $("div p.selected") )
+        * @before <div><p>Hello</p><p class="selected">Hello Again</p></div>
+        * @result [ <p>Hello</p> ]
+        * @desc Removes all elements that match "div p.selected" from the total set of all paragraphs.
+        *
+        * @name not
+        * @type jQuery
+        * @param Array|jQuery elems A set of elements to remove from the jQuery set of matched elements.
+        * @cat DOM/Traversing
+        */
        not: function(t) {
-               return this.pushStack( typeof t == "string" ?
-                       jQuery.filter(t,this,true).r :
-                       jQuery.grep(this,function(a){ return a != t; }) );
+               return this.pushStack(
+                       t.constructor == String &&
+                       jQuery.multiFilter(t,this,true) ||
+
+                       jQuery.grep(this,function(a){
+                                       if ( t.constructor == Array || t.jquery )
+                                               return !jQuery.inArray( t, a );
+                                       else
+                                               return a != t;
+                       }) );
        },
 
        /**
@@ -1007,7 +1021,9 @@ jQuery.fn = jQuery.prototype = {
         * @cat DOM/Attributes
         */
        val: function( val ) {
-               return val == undefined ?\r                      ( this.length ? this[0].value : null ) :\r                       this.attr( "value", val );
+               return val == undefined ?
+                       ( this.length ? this[0].value : null ) :
+                       this.attr( "value", val );
        },
        
        /**
@@ -1037,7 +1053,9 @@ jQuery.fn = jQuery.prototype = {
         * @cat DOM/Attributes
         */
        html: function( val ) {
-               return val == undefined ?\r                      ( this.length ? this[0].innerHTML : null ) :\r                   this.empty().append( val );
+               return val == undefined ?
+                       ( this.length ? this[0].innerHTML : null ) :
+                       this.empty().append( val );
        },
        
        /**
@@ -1828,7 +1846,7 @@ jQuery.each({
        jQuery.fn[ i ] = function(a) {
                var ret = jQuery.map(this,n);
                if ( a && typeof a == "string" )
-                       ret = jQuery.filter(a,ret).r;
+                       ret = jQuery.multiFilter(a,ret);
                return this.pushStack( ret );
        };
 });
index e21102c..1bad21b 100644 (file)
@@ -89,6 +89,24 @@ jQuery.extend({
                }
        ],
 
+       multiFilter: function( expr, elems, not ) {
+               var old, cur = [];
+
+               while ( expr && expr != old ) {
+console.log( cur, expr, elems, not );
+                       var f = jQuery.filter( expr, elems, not );
+                       expr = f.t.replace(/^\s*,\s*/, "" );
+
+                       if ( not )
+                               cur = elems = f.r;
+                       else
+                               cur = jQuery.merge( cur, f.r );
+               }
+console.log( "DONE", cur, expr, elems, not );
+
+               return cur;
+       },
+
        /**
         * @name $.find
         * @type Array<Element>