Skip to content

Commit

Permalink
Remove the need for the selectorFromTokens function by tracking each …
Browse files Browse the repository at this point in the history
…group's selector.
  • Loading branch information
timmywil committed Aug 24, 2012
1 parent 89724d5 commit e335982
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions sizzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ var cachedruns,
});



function Sizzle( selector, context, results, seed ) {
results = results || [];
context = context || document;
Expand Down Expand Up @@ -986,7 +985,7 @@ Sizzle.uniqueSort = function( results ) {

function tokenize( selector, context, xml ) {
var matched, match, tokens, type,
soFar, groups, i,
soFar, groups, group, i,
preFilters, filters,
checkContext = !xml && context !== document,
// Token cache should maintain spaces
Expand All @@ -1009,8 +1008,10 @@ function tokenize( selector, context, xml ) {
if ( !matched || (match = rcomma.exec( soFar )) ) {
if ( match ) {
soFar = soFar.slice( match[0].length );
tokens.selector = group;
}
groups.push( tokens = [] );
group = "";

// Need to make sure we're within a narrower context if necessary
// Adding a descendant combinator will generate what is needed
Expand All @@ -1023,6 +1024,7 @@ function tokenize( selector, context, xml ) {

// Combinators
if ( (match = rcombinators.exec( soFar )) ) {
group += match[0];
soFar = soFar.slice( match[0].length );

// Cast descendant combinators to space
Expand All @@ -1036,8 +1038,9 @@ function tokenize( selector, context, xml ) {
// Filters
for ( type in filters ) {
if ( (match = matchExpr[ type ].exec( soFar )) && (!preFilters[ type ] ||
(match = preFilters[ type ]( match, context, xml )) ) ) {
( match = preFilters[ type ](match, context, xml) )) ) {

group += match[0];
soFar = soFar.slice( match[0].length );
matched = tokens.push({
part: type,
Expand All @@ -1052,6 +1055,11 @@ function tokenize( selector, context, xml ) {
}
}

// Attach the full group as a selector
if ( group ) {
tokens.selector = group;
}

// Cache the tokens
return slice.call( tokenCache.s(key, groups), 0 );
}
Expand Down Expand Up @@ -1160,20 +1168,6 @@ var compile = Sizzle.compile = function( selector, context, xml ) {
return cached;
};

/**
* @param {Array} An array of tokens to convert
* @return A selector representing the array of tokens
*/
function selectorFromTokens( tokens ) {
var selector = "",
i = 0,
len = tokens.length;
for ( ; i < len; i++ ) {
selector += tokens[i].string;
}
return selector;
}

function multipleContexts( selector, contexts, results, seed ) {
var i = 0,
len = contexts.length;
Expand Down Expand Up @@ -1219,7 +1213,7 @@ function handlePOS( selector, context, results, seed, groups ) {
for ( ; i < len; i++ ) {
// Reset regex index to 0
rpos.exec("");
selector = selectorFromTokens( groups[i] );
selector = groups[i].selector;
ret = [];
anchor = 0;
elements = seed;
Expand Down Expand Up @@ -1444,7 +1438,7 @@ if ( document.querySelectorAll ) {
// There is always a context check
nid = "[id='" + nid + "']";
for ( i = 0, len = groups.length; i < len; i++ ) {
groups[i] = nid + selectorFromTokens( groups[i] );
groups[i] = nid + groups[i].selector;
}
try {
push.apply( results, slice.call( newContext.querySelectorAll(
Expand Down

0 comments on commit e335982

Please sign in to comment.