1 import { jQuery } from "../core.js";
2 import { rcomma } from "./var/rcomma.js";
3 import { rleadingCombinator } from "./var/rleadingCombinator.js";
4 import { rtrimCSS } from "../var/rtrimCSS.js";
5 import { createCache } from "./createCache.js";
6 import { selectorError } from "./selectorError.js";
7 import { filterMatchExpr } from "./filterMatchExpr.js";
9 var tokenCache = createCache();
11 export function tokenize( selector, parseOnly ) {
12 var matched, match, tokens, type,
13 soFar, groups, preFilters,
14 cached = tokenCache[ selector + " " ];
17 return parseOnly ? 0 : cached.slice( 0 );
22 preFilters = jQuery.expr.preFilter;
26 // Comma and first run
27 if ( !matched || ( match = rcomma.exec( soFar ) ) ) {
30 // Don't consume trailing commas as valid
31 soFar = soFar.slice( match[ 0 ].length ) || soFar;
33 groups.push( ( tokens = [] ) );
39 if ( ( match = rleadingCombinator.exec( soFar ) ) ) {
40 matched = match.shift();
44 // Cast descendant combinators to space
45 type: match[ 0 ].replace( rtrimCSS, " " )
47 soFar = soFar.slice( matched.length );
51 for ( type in filterMatchExpr ) {
52 if ( ( match = jQuery.expr.match[ type ].exec( soFar ) ) && ( !preFilters[ type ] ||
53 ( match = preFilters[ type ]( match ) ) ) ) {
54 matched = match.shift();
60 soFar = soFar.slice( matched.length );
69 // Return the length of the invalid excess
70 // if we're just parsing
71 // Otherwise, throw an error or return tokens
77 selectorError( selector ) :
80 tokenCache( selector, groups ).slice( 0 );