// 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
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;
}
},
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;
+ }
+} );