]> source.dussan.org Git - jquery.git/commitdiff
Selector: Drop support for legacy pseudos, test custom pseudos
authorMichał Gołębiowski-Owczarek <m.goleb@gmail.com>
Tue, 11 Oct 2022 09:55:46 +0000 (11:55 +0200)
committerGitHub <noreply@github.com>
Tue, 11 Oct 2022 09:55:46 +0000 (11:55 +0200)
This backports custom pseudos tests from Sizzle; they were missed in original
test backports. Also, the support for legacy custom pseudos has been dropped.

The `jQuery.expr` test cleanup has been wrapped in `try-finally` for cleaner
test isolation in case anything goes wrong.

Closes gh-5137

src/selector.js
test/unit/selector.js

index 871cf8682250ea528fc517ef199493ecec6abe78..a69e16556077dce78e4c96c0c3c9ccac95c2824f 100644 (file)
@@ -804,9 +804,8 @@ Expr = jQuery.expr = {
                        // https://www.w3.org/TR/selectors/#pseudo-classes
                        // Prioritize by case sensitivity in case custom pseudos are added with uppercase letters
                        // Remember that setFilters inherits from pseudos
-                       var args,
-                               fn = Expr.pseudos[ pseudo ] || Expr.setFilters[ pseudo.toLowerCase() ] ||
-                                       selectorError( "unsupported pseudo: " + pseudo );
+                       var fn = Expr.pseudos[ pseudo ] || Expr.setFilters[ pseudo.toLowerCase() ] ||
+                               selectorError( "unsupported pseudo: " + pseudo );
 
                        // The user may use createPseudo to indicate that
                        // arguments are needed to create the filter function
@@ -815,24 +814,6 @@ Expr = jQuery.expr = {
                                return fn( argument );
                        }
 
-                       // But maintain support for old signatures
-                       if ( fn.length > 1 ) {
-                               args = [ pseudo, pseudo, "", argument ];
-                               return Expr.setFilters.hasOwnProperty( pseudo.toLowerCase() ) ?
-                                       markFunction( function( seed, matches ) {
-                                               var idx,
-                                                       matched = fn( seed, argument ),
-                                                       i = matched.length;
-                                               while ( i-- ) {
-                                                       idx = indexOf.call( seed, matched[ i ] );
-                                                       seed[ idx ] = !( matches[ idx ] = matched[ i ] );
-                                               }
-                                       } ) :
-                                       function( elem ) {
-                                               return fn( elem, 0, args );
-                                       };
-                       }
-
                        return fn;
                }
        },
index b1529175b50855c85eb03c9caf78563d9c941fbf..12297fae9fdbb3da953a0b67b75b2f37f12ed3b6 100644 (file)
@@ -2142,3 +2142,77 @@ QUnit.test( "jQuery.escapeSelector", function( assert ) {
        assert.equal( jQuery.escapeSelector( "\uDF06" ), "\uDF06", "Doesn't escape lone low surrogate" );
        assert.equal( jQuery.escapeSelector( "\uD834" ), "\uD834", "Doesn't escape lone high surrogate" );
 } );
+
+QUnit.test( "custom pseudos", function( assert ) {
+       assert.expect( 6 );
+
+       try {
+               jQuery.expr.filters.foundation = jQuery.expr.filters.root;
+               assert.deepEqual( jQuery.find( ":foundation" ), [ document.documentElement ], "Copy element filter with new name" );
+       } finally {
+               delete jQuery.expr.filters.foundation;
+       }
+
+       try {
+               jQuery.expr.setFilters.primary = jQuery.expr.setFilters.first;
+               assert.t( "Copy set filter with new name", "div#qunit-fixture :primary", [ "firstp" ] );
+       } finally {
+               delete jQuery.expr.setFilters.primary;
+       }
+
+       try {
+               jQuery.expr.filters.aristotlean = jQuery.expr.createPseudo( function() {
+                       return function( elem ) {
+                               return !!elem.id;
+                       };
+               } );
+               assert.t( "Custom element filter", "#foo :aristotlean", [ "sndp", "en", "yahoo", "sap", "anchor2", "simon" ] );
+       } finally {
+               delete jQuery.expr.filters.aristotlean;
+       }
+
+       try {
+               jQuery.expr.filters.endswith = jQuery.expr.createPseudo( function( text ) {
+                       return function( elem ) {
+                               return jQuery.text( elem ).slice( -text.length ) === text;
+                       };
+               } );
+               assert.t( "Custom element filter with argument", "a:endswith(ogle)", [ "google" ] );
+       } finally {
+               delete jQuery.expr.filters.endswith;
+       }
+
+       try {
+               jQuery.expr.setFilters.second = jQuery.expr.createPseudo( function() {
+                       return jQuery.expr.createPseudo( function( seed, matches ) {
+                               if ( seed[ 1 ] ) {
+                                       matches[ 1 ] = seed[ 1 ];
+                                       seed[ 1 ] = false;
+                               }
+                       } );
+               } );
+               assert.t( "Custom set filter", "#qunit-fixture p:second", [ "ap" ] );
+       } finally {
+               delete jQuery.expr.filters.second;
+       }
+
+       try {
+               jQuery.expr.setFilters.slice = jQuery.expr.createPseudo( function( argument ) {
+                       var bounds = argument.split( ":" );
+                       return jQuery.expr.createPseudo( function( seed, matches ) {
+                               var i = bounds[ 1 ];
+
+                               // Match elements found at the specified indexes
+                               while ( --i >= bounds[ 0 ] ) {
+                                       if ( seed[ i ] ) {
+                                               matches[ i ] = seed[ i ];
+                                               seed[ i ] = false;
+                                       }
+                               }
+                       } );
+               } );
+               assert.t( "Custom set filter with argument", "#qunit-fixture p:slice(1:3)", [ "ap", "sndp" ] );
+       } finally {
+               delete jQuery.expr.filters.slice;
+       }
+} );