jQuery.API源码深入剖析以及应用实现 - 核心函数篇
2010-09-14 13:38:00 来源:WEB开发网varSizzle=function(selector,context,results,seed){
results=results||[];
context=context||document;
if(context.nodeType!==1&&context.nodeType!==9)
return[];
if(!selector||typeofselector!=="string"){
returnresults;
}
varparts=[],m,set,checkSet,check,mode,extra,prune=true;
//Resetthepositionofthechunkerregexp(startfromhead)
chunker.lastIndex=0;
while((m=chunker.exec(selector))!==null){
parts.push(m[1]);
if(m[2]){
extra=RegExp.rightContext;
break;
}
}
if(parts.length>1&&origPOS.exec(selector)){
if(parts.length===2&&Expr.relative[parts[0]]){
set=posProcess(parts[0]+parts[1],context);
}else{
set=Expr.relative[parts[0]]?
[context]:
Sizzle(parts.shift(),context);
while(parts.length){
selector=parts.shift();
if(Expr.relative[selector])
selector+=parts.shift();
set=posProcess(selector,set);
}
}
}else{
varret=seed?
{expr:parts.pop(),set:makeArray(seed)}:
Sizzle.find(parts.pop(),parts.length===1&&context.parentNode?context.parentNode:context,isXML(context));
set=Sizzle.filter(ret.expr,ret.set);
if(parts.length>0){
checkSet=makeArray(set);
}else{
prune=false;
}
while(parts.length){
varcur=parts.pop(),pop=cur;
if(!Expr.relative[cur]){
cur="";
}else{
pop=parts.pop();
}
if(pop==null){
pop=context;
}
Expr.relative[cur](checkSet,pop,isXML(context));
}
}
if(!checkSet){
checkSet=set;
}
if(!checkSet){
throw"Syntaxerror,unrecognizedexpression:"+(cur||selector);
}
if(toString.call(checkSet)==="[objectArray]"){
if(!prune){
results.push.apply(results,checkSet);
}elseif(context.nodeType===1){
for(vari=0;checkSet[i]!=null;i++){
if(checkSet[i]&&(checkSet[i]===true||checkSet[i].nodeType===1&&contains(context,checkSet[i]))){
results.push(set[i]);
}
}
}else{
for(vari=0;checkSet[i]!=null;i++){
if(checkSet[i]&&checkSet[i].nodeType===1){
results.push(set[i]);
}
}
}
}else{
makeArray(checkSet,results);
}
if(extra){
Sizzle(extra,context,results,seed);
}
returnresults;
};
更多精彩
赞助商链接