MCPcopy
hub / github.com/jquery/jquery / matcherFromTokens

Function matcherFromTokens

src/selector.js:1057–1123  ·  view source on GitHub ↗
( tokens )

Source from the content-addressed store, hash-verified

1055}
1056
1057function matcherFromTokens( tokens ) {
1058 var checkContext, matcher, j,
1059 len = tokens.length,
1060 leadingRelative = jQuery.expr.relative[ tokens[ 0 ].type ],
1061 implicitRelative = leadingRelative || jQuery.expr.relative[ " " ],
1062 i = leadingRelative ? 1 : 0,
1063
1064 // The foundational matcher ensures that elements are reachable from top-level context(s)
1065 matchContext = addCombinator( function( elem ) {
1066 return elem === checkContext;
1067 }, implicitRelative, true ),
1068 matchAnyContext = addCombinator( function( elem ) {
1069 return indexOf.call( checkContext, elem ) > -1;
1070 }, implicitRelative, true ),
1071 matchers = [ function( elem, context, xml ) {
1072
1073 // Support: IE 11+
1074 // IE sometimes throws a "Permission denied" error when strict-comparing
1075 // two documents; shallow comparisons work.
1076 // eslint-disable-next-line eqeqeq
1077 var ret = ( !leadingRelative && ( xml || context != outermostContext ) ) || (
1078 ( checkContext = context ).nodeType ?
1079 matchContext( elem, context, xml ) :
1080 matchAnyContext( elem, context, xml ) );
1081
1082 // Avoid hanging onto element
1083 // (see https://github.com/jquery/sizzle/issues/299)
1084 checkContext = null;
1085 return ret;
1086 } ];
1087
1088 for ( ; i < len; i++ ) {
1089 if ( ( matcher = jQuery.expr.relative[ tokens[ i ].type ] ) ) {
1090 matchers = [ addCombinator( elementMatcher( matchers ), matcher ) ];
1091 } else {
1092 matcher = jQuery.expr.filter[ tokens[ i ].type ].apply( null, tokens[ i ].matches );
1093
1094 // Return special upon seeing a positional matcher
1095 if ( matcher[ jQuery.expando ] ) {
1096
1097 // Find the next relative operator (if any) for proper handling
1098 j = ++i;
1099 for ( ; j < len; j++ ) {
1100 if ( jQuery.expr.relative[ tokens[ j ].type ] ) {
1101 break;
1102 }
1103 }
1104 return setMatcher(
1105 i > 1 && elementMatcher( matchers ),
1106 i > 1 && toSelector(
1107
1108 // If the preceding token was a descendant combinator, insert an implicit any-element `*`
1109 tokens.slice( 0, i - 1 )
1110 .concat( { value: tokens[ i - 2 ].type === " " ? "*" : "" } )
1111 ).replace( rtrimCSS, "$1" ),
1112 matcher,
1113 i < j && matcherFromTokens( tokens.slice( i, j ) ),
1114 j < len && matcherFromTokens( ( tokens = tokens.slice( j ) ) ),

Callers 1

compileFunction · 0.70

Calls 4

toSelectorFunction · 0.90
addCombinatorFunction · 0.70
elementMatcherFunction · 0.70
setMatcherFunction · 0.70

Tested by

no test coverage detected