]> source.dussan.org Git - jquery.git/commitdiff
Fixed #1854 by using wizzud's suggestion. The only real difference is the code is...
authorDavid Serduke <davidserduke@gmail.com>
Thu, 13 Dec 2007 22:24:59 +0000 (22:24 +0000)
committerDavid Serduke <davidserduke@gmail.com>
Thu, 13 Dec 2007 22:24:59 +0000 (22:24 +0000)
src/core.js
src/selector.js
test/index.html
test/unit/core.js
test/unit/selector.js

index 53d5f4a4b1601bd0c4e83a00051d659beb0aaf05..30820bf9ee1952ddb9c0cbed6192072361a6e0d3 100644 (file)
@@ -342,15 +342,14 @@ jQuery.fn = jQuery.prototype = {
        },
 
        not: function( selector ) {
-               return this.pushStack(
-                       selector.constructor == String &&
-                       jQuery.multiFilter( selector, this, true ) ||
-
-                       jQuery.grep(this, function(elem) {
-                               return selector.constructor == Array || selector.jquery ?
-                                       jQuery.inArray( elem, selector ) < 0 :
-                                       elem != selector;
-                       }) );
+               if (selector.constructor == String)
+                       // test special case where just one selector is passed in
+                       if ( /^.[^:#\[\.]*$/.test(selector) )
+                               return this.pushStack( jQuery.multiFilter( selector, this, true ) );
+                       else
+                               selector = jQuery.multiFilter( selector, this );
+
+               return this.pushStack( jQuery.removeFromArray( selector, this ) );
        },
 
        add: function( selector ) {
@@ -1093,6 +1092,13 @@ jQuery.extend({
                return -1;
        },
 
+       removeFromArray: function( remove, from ) {
+               var isArrayLike = remove.length && remove[remove.length - 1] !== undefined;
+               return jQuery.grep(from, function(elem) {
+                               return isArrayLike ? jQuery.inArray( elem, remove ) < 0 : elem != from;
+                       });
+       },
+
        merge: function( first, second ) {
                // We have to loop this way because IE & Opera overwrite the length
                // expando of getElementsByTagName
index c35fbb1eb9ae99c14cfc02cd06f670fca2f75c42..6e916a2e4e795274a0f9f14c517ce8d1ad547676 100644 (file)
@@ -320,7 +320,11 @@ jQuery.extend({
                        // :not() is a special case that can be optimized by
                        // keeping it out of the expression list
                        if ( m[1] == ":" && m[2] == "not" )
-                               r = jQuery.filter(m[3], r, true).r;
+                               // optimize if only one selector found (most common case)
+                               if ( /^.[^:#\[\.]*$/.test(m[3]) )
+                                       r = jQuery.filter(m[3], r, true).r;
+                               else
+                                       r = jQuery.removeFromArray(jQuery.multiFilter(m[3], r), r);
 
                        // We can get a big speed boost by filtering by class here
                        else if ( m[1] == "." )
index cdd6a30be073b2157aa95f4c6632b0ca4dba7642..b062179f9f7185ebea46723b28eb88e02be41c5f 100644 (file)
                        <textarea id="area1" maxlength="30">foobar</textarea>
                        
                        <select name="select1" id="select1">
-                               <option id="option1a" value="">Nothing</option>
+                               <option id="option1a" class="emptyopt" value="">Nothing</option>
                                <option id="option1b" value="1">1</option>
                                <option id="option1c" value="2">2</option>
                                <option id="option1d" value="3">3</option>
                        </select>
                        <select name="select2" id="select2">
-                               <option id="option2a" value="">Nothing</option>
+                               <option id="option2a" class="emptyopt" value="">Nothing</option>
                                <option id="option2b" value="1">1</option>
                                <option id="option2c" value="2">2</option>
                                <option id="option2d" selected="selected" value="3">3</option>
                        </select>
                        <select name="select3" id="select3" multiple="multiple">
-                               <option id="option3a" value="">Nothing</option>
+                               <option id="option3a" class="emptyopt" value="">Nothing</option>
                                <option id="option3b" selected="selected" value="1">1</option>
                                <option id="option3c" selected="selected" value="2">2</option>
                                <option id="option3d" value="3">3</option>
index 4e774857853490b82400ba5c29b69ed96fc04a93..12b405644ac4dc2c6b5ce8b4d6575d214e13d664 100644 (file)
@@ -1052,10 +1052,12 @@ test("filter()", function() {
 });
 
 test("not()", function() {
-       expect(3);
+       expect(5);
        ok( $("#main > p#ap > a").not("#google").length == 2, "not('selector')" );
+       isSet( $("p").not(".result").get(), q("firstp", "ap", "sndp", "en", "sap", "first"), "not('.class')" );
        isSet( $("p").not("#ap, #sndp, .result").get(), q("firstp", "en", "sap", "first"), "not('selector, selector')" );
        isSet( $("p").not($("#ap, #sndp, .result")).get(), q("firstp", "en", "sap", "first"), "not(jQuery)" );
+       isSet( $("#form option").not("option.emptyopt:contains('Nothing'),[selected],[value='1']").get(), q("option1c", "option1d", "option2c", "option3d" ), "not('complex selector')");
 });
 
 test("andSelf()", function() {
index 3c0c6761da801d014e2f9d2df54736ffdd3271a2..9f3fb6f2c0f6598e6abf8c7b6ff57d16bf489c02 100644 (file)
@@ -182,7 +182,7 @@ test("attributes", function() {
 });
 
 test("pseudo (:) selectors", function() {
-       expect(32);
+       expect(35);
        t( "First Child", "p:first-child", ["firstp","sndp"] );
        t( "Last Child", "p:last-child", ["sap"] );
        t( "Only Child", "a:only-child", ["simon1","anchor1","yahoo","anchor2"] );
@@ -195,6 +195,9 @@ test("pseudo (:) selectors", function() {
        t( "Text Contains", "a:contains('Google Groups')", ["groups"] );
        t( "Element Preceded By", "p ~ div", ["foo","fx-queue","fx-tests", "moretests"] );
        t( "Not", "a.blog:not(.link)", ["mark"] );
+       t( "Not - multiple", "#form option:not(:contains('Nothing'),#option1b,:selected)", ["option1c", "option1d", "option2b", "option2c", "option3d"] );
+       t( "Not - complex", "#form option:not([id^='opt']:gt(0):nth-child(-n+3))", [ "option1a", "option1d", "option2d", "option3d"] );
+       t( "Not - recursive", "#form option:not(:not(:selected))[id^='option3']", [ "option3b", "option3c"] );
        
        t( "nth Element", "p:nth(1)", ["ap"] );
        t( "First Element", "p:first", ["firstp"] );