Landed some improvements for improving the performance of ID-rooted queries (e.g. '#foo p'). Fixes jQuery bug #4236.

This commit is contained in:
John Resig 2009-02-25 04:08:04 +00:00
parent cb3f842c88
commit 9c0ddfa2ad

View file

@ -12,16 +12,17 @@ var chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^[\]]*\]|['"][^'"]*['"]|[^
var Sizzle = function(selector, context, results, seed) { var Sizzle = function(selector, context, results, seed) {
results = results || []; results = results || [];
context = context || document; var origContext = context = context || document;
if ( context.nodeType !== 1 && context.nodeType !== 9 ) if ( context.nodeType !== 1 && context.nodeType !== 9 ) {
return []; return [];
}
if ( !selector || typeof selector !== "string" ) { if ( !selector || typeof selector !== "string" ) {
return results; return results;
} }
var parts = [], m, set, checkSet, check, mode, extra, prune = true; var parts = [], m, set, checkSet, check, mode, extra, prune = true, contextXML = isXML(context);
// Reset the position of the chunker regexp (start from head) // Reset the position of the chunker regexp (start from head)
chunker.lastIndex = 0; chunker.lastIndex = 0;
@ -53,31 +54,43 @@ var Sizzle = function(selector, context, results, seed) {
} }
} }
} else { } else {
var ret = seed ? // Take a shortcut and set the context if the root selector is an ID
{ expr: parts.pop(), set: makeArray(seed) } : // (but not if it'll be faster if the inner selector is an ID)
Sizzle.find( parts.pop(), parts.length === 1 && context.parentNode ? context.parentNode : context, isXML(context) ); if ( !seed && parts.length > 1 && context.nodeType === 9 && !contextXML &&
set = Sizzle.filter( ret.expr, ret.set ); Expr.match.ID.test(parts[0]) && !Expr.match.ID.test(parts[parts.length - 1]) ) {
var ret = Sizzle.find( parts.shift(), context, contextXML );
if ( parts.length > 0 ) { context = ret.expr ? Sizzle.filter( ret.expr, ret.set )[0] : ret.set[0];
checkSet = makeArray(set);
} else {
prune = false;
} }
while ( parts.length ) { if ( context ) {
var cur = parts.pop(), pop = cur; var ret = seed ?
{ expr: parts.pop(), set: makeArray(seed) } :
Sizzle.find( parts.pop(), parts.length === 1 && context.parentNode ? context.parentNode : context, contextXML );
set = ret.expr ? Sizzle.filter( ret.expr, ret.set ) : ret.set;
if ( !Expr.relative[ cur ] ) { if ( parts.length > 0 ) {
cur = ""; checkSet = makeArray(set);
} else { } else {
pop = parts.pop(); prune = false;
} }
if ( pop == null ) { while ( parts.length ) {
pop = context; var cur = parts.pop(), pop = cur;
}
Expr.relative[ cur ]( checkSet, pop, isXML(context) ); if ( !Expr.relative[ cur ] ) {
cur = "";
} else {
pop = parts.pop();
}
if ( pop == null ) {
pop = context;
}
Expr.relative[ cur ]( checkSet, pop, contextXML );
}
} else {
checkSet = parts = [];
} }
} }
@ -92,7 +105,7 @@ var Sizzle = function(selector, context, results, seed) {
if ( toString.call(checkSet) === "[object Array]" ) { if ( toString.call(checkSet) === "[object Array]" ) {
if ( !prune ) { if ( !prune ) {
results.push.apply( results, checkSet ); results.push.apply( results, checkSet );
} else if ( context.nodeType === 1 ) { } else if ( context && context.nodeType === 1 ) {
for ( var i = 0; checkSet[i] != null; i++ ) { for ( var i = 0; checkSet[i] != null; i++ ) {
if ( checkSet[i] && (checkSet[i] === true || checkSet[i].nodeType === 1 && contains(context, checkSet[i])) ) { if ( checkSet[i] && (checkSet[i] === true || checkSet[i].nodeType === 1 && contains(context, checkSet[i])) ) {
results.push( set[i] ); results.push( set[i] );
@ -110,7 +123,7 @@ var Sizzle = function(selector, context, results, seed) {
} }
if ( extra ) { if ( extra ) {
Sizzle( extra, context, results, seed ); Sizzle( extra, origContext, results, seed );
if ( sortOrder ) { if ( sortOrder ) {
hasDuplicate = false; hasDuplicate = false;