原創文章,轉載請注明出處,多謝!
/*
* 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 初始集合
*/
function select(selector, context, results, seed) {
var i, tokens, token, type, find,
// 調用tokenize函數解析selector
match = tokenize(selector);
// 若沒有提供初始集合
if (!seed) {
// Try to minimize operations if there is only one group
// 若隻有一組選擇器,即選擇器字元串沒有逗号
if (match.length === 1) {
// Take a shortcut and set the context if the root selector
// is an ID
/*
* 下面代碼是用來處理根選擇器是ID類型的快捷方式
*
* 在此使用slice[0]來建立一個新的集合,
* 確定原有的集合不會被之後代碼變更掉
*/
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選擇器指定的節點對象
context = (Expr.find["ID"](token.matches[0].replace(
runescape, funescape), context) || [])[0];
// 若目前上下文内沒有指定ID對象,則直接傳回results
if (!context) {
return results;
}
// 選擇器字元串去掉第一個ID選擇器
selector = selector.slice(tokens.shift().value.length);
}
// Fetch a seed set for right-to-left matching
/*
* 下面while循環的作用是用來根據最後一個id、class、tag類型的選擇器擷取初始集合
* 舉個簡單例子:若選擇器是"div[title='2']",
* 代碼根據div擷取出所有的context下的div節點,并把這個集合賦給seed變量,
* 然後在調用compile函數,産生預編譯代碼,
* 預編譯代碼完成在上述初始集合中執行[title='2']的比對
*
* 首先,檢查選擇器字元串中是否存在與needsContext正規表達式相比對的字元
* 若沒有,則将依據選擇器從右到左過濾DOM節點
* 否則,将先生成預編譯代碼後執行(調用compile方法)。
*/
/*
* "needsContext" : new RegExp("^" + whitespace
* + "*[>+~]|:(even|odd|eq|gt|lt|nth|first|last)(?:\\("
* + whitespace + "*((?:-\\d)?\\d*)" + whitespace
* + "*\\)|)(?=[^-]|$)", "i")
* needsContext用來比對選擇器字元串中是否包含下列内容:
* 1、>+~三種關系符
* 2、:even、:odd、:eq、:gt、:lt、:nth、:first、:last八種僞類
* 其中,(?=[^-]|$)用來過濾掉類似于:first-child等帶中杠的且以上述八個單詞開頭的其它選擇器
*/
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;
}
if ((find = Expr.find[type])) {
// Search, expanding context for leading sibling
// combinators
/*
* rsibling = new RegExp(whitespace + "*[+~]")
* rsibling用于判定token選擇器是否是兄弟關系符
*/
if ((seed = find(token.matches[0].replace(
runescape, funescape), rsibling
.test(tokens[0].type)
&& context.parentNode || context))) {
// If seed is empty or no tokens remain, we can
// return early
// 剔除剛用過的選擇器
tokens.splice(i, 1);
selector = seed.length && toSelector(tokens);
/*
* 若selector為空,說明選擇器僅為單一id、class、tag類型的,
* 故直接傳回擷取的結果,否則,在擷取seed的基礎上繼續比對
*/
if (!selector) {
push.apply(results, seed);
return results;
}
break;
}
}
}
}
}
// Compile and execute a filtering function
// Provide `match` to avoid retokenization if we modified the
// selector above
/*
* 先執行compile(selector, match),它會傳回一個“預編譯”函數,
* 然後調用該函數擷取最後比對結果
*/
compile(selector, match)(seed, context, !documentIsHTML, results,
rsibling.test(selector));
return results;
}
各位朋友,若覺得寫得不錯,幫我頂一下,給點動力,多謝!
jQuery選擇器代碼詳解(一)——Sizzle方法
jQuery選擇器代碼詳解(二)——select方法
jQuery選擇器代碼詳解(三)——tokenize方法
jQuery選擇器代碼詳解(四)——Expr.preFilter
jQuery選擇器代碼詳解(五)——執行個體說明tokenize的解析過程
jQuery選擇器代碼詳解(六)——Sizzle選擇器比對邏輯分析
jQuery選擇器代碼詳解(七)——elementMatcher函數
jQuery選擇器代碼詳解(八)——addCombinator函數