【jQuery源码】select方法

 /**
* select方法是Sizzle选择器包的核心方法之一,其主要完成下列任务:
* 1、调用tokenize方法完成对选择器的解析
* 2、对于没有初始集合(即seed没有赋值)且是单一块选择器(即选择器字符串中没有逗号),
* 完成下列事项:
* 1) 对于首选择器是ID类型且context是document的,则直接获取对象替代传入的context对象
* 2) 若选择器是单一选择器,且是id、class、tag类型的,则直接获取并返回匹配的DOM元素
* 3) 获取最后一个id、class、tag类型选择器的匹配DOM元素赋值给初始集合(即seed变量)
* 3、通过调用compile方法获取“预编译”代码并执行,获取并返回匹配的DOM元素
*
* @param selector 已去掉头尾空白的选择器字符串
* @param context 执行匹配的最初的上下文(即DOM元素集合)。若context没有赋值,则取document。
* @param results 已匹配出的部分最终结果。若results没有赋值,则赋予空数组。
* @param seed 初始集合
*/
select = Sizzle.select = function( selector, context, results, seed ) {
var i, tokens, token, type, find,
compiled = typeof selector === "function" && selector,
//在没有seed的时候,调用tokensize方法进行词法解析,结果放在match中
//seed - 种子合集(搜索器搜到符合条件的标签),放入到这个初始集合seed中
match = !seed && tokenize( (selector = compiled.selector || selector) ); results = results || []; // Try to minimize operations if there is no seed and only one group
//只有一组选择器,也就是选择器中没有逗号
if ( match.length === 1 ) { // Take a shortcut and set the context if the root selector is an ID
//创建一个新的集合赋给tokens,确保原有集合不被更改
tokens = match[0] = match[0].slice( 0 );
/*
* 若选择器是以id类型开始,且第二个是关系符(即+~>或空格),
* 则获取id所属对象作为context继续完成后续的匹配
*
* 此处的条件判断依次为:
* tokens.length > 2 :若tokens有两个以上的选择器
* (token = tokens[0]).type === "ID" :第一个选择器的类型为ID(即以#开头的),
* support.getById :支持getElementById函数
* context.nodeType === 9 :context对象是document
* documentIsHTML :当前处理的是HTML代码
* Expr.relative[tokens[1].type] :第二个tokens元素是一个关系(即+~>或空格)
* 在满足上面所有条件的情况下,执行if内的语句体
*/
if ( tokens.length > 2 && (token = tokens[0]).type === "ID" &&
support.getById && context.nodeType === 9 && documentIsHTML &&
Expr.relative[ tokens[1].type ] ) { //查找id为matches[1]的元素
context = ( Expr.find["ID"]( token.matches[0].replace(runescape, funescape), context ) || [] )[0];
if ( !context ) {
return results; // Precompiled matchers will still verify ancestry, so step up a level
} else if ( compiled ) {
context = context.parentNode;
} //去掉selector中第一个选择器
selector = selector.slice( tokens.shift().value.length );
} // Fetch a seed set for right-to-left matching
//其中: "needsContext"= new RegExp( "^" + whitespace + "*[>+~]|:(even|odd|eq|gt|lt|nth|first|last)(?:\\(" + whitespace + "*((?:-\\d)?\\d*)" + whitespace + "*\\)|)(?=[^-]|$)", "i" )
//即是表示如果没有一些结构伪类,这些是需要用另一种方式过滤,在之后文章再详细剖析。
//那么就从最后一条规则开始,先找出seed集合
i = matchExpr["needsContext"].test( selector ) ? 0 : tokens.length;
while ( i-- ) {
token = tokens[i]; // Abort if we hit a combinator
//如果遇到了关系选择器则终止循环,> + ~ 空
if ( Expr.relative[ (type = token.type) ] ) {
break;
}
/*
先看看有没有搜索器find,搜索器就是浏览器一些原生的取DOM接口,简单的表述就是以下对象了
Expr.find = {
'ID' : context.getElementById,
'CLASS' : context.getElementsByClassName,
'NAME' : context.getElementsByName,
'TAG' : context.getElementsByTagName
}
*/
if ( (find = Expr.find[ type ]) ) {
// Search, expanding context for leading sibling combinators
// 尝试一下能否通过这个搜索器搜到符合条件的初始集合seed
if ( (seed = find(
token.matches[0].replace( runescape, funescape ),
rsibling.test( tokens[0].type ) && testContext( context.parentNode ) || context
)) ) { // If seed is empty or no tokens remain, we can return early
//删除这条标签
tokens.splice( i, 1 );
selector = seed.length && toSelector( tokens );
//若当前选择器是否为空,把seed推入results,返回结果
if ( !selector ) {
push.apply( results, seed );
return results;
}
//已经找到了符合条件的seed集合,此时前边还有其他规则,跳出去
break;
}
}
}
} // Compile and execute a filtering function if one is not provided
// Provide `match` to avoid retokenization if we modified the selector above
( compiled || compile( selector, match ) )(
seed,
context,
!documentIsHTML,
results,
rsibling.test( selector ) && testContext( context.parentNode ) || context
);
return results;
};
 
上一篇:.NET分布式事务处理总结【下】 - 包含MSMQ的分布式事务处理


下一篇:python中.py和.pyw文件的区别