本文接續 Sizzle 源碼系列,聚焦於產生「關係選擇器」執行函式的 addCombinator。函式簽名如下:
function addCombinator(matcher, combinator, base)
function addCombinator(matcher, combinator, base) { var dir = combinator.dir, checkNonElements = base && dir === "parentNode", doneName = done++; return combinator.first ? // Check against closest ancestor/preceding element function(elem, context, xml) { while ((elem = elem[dir])) { if (elem.nodeType === 1 || checkNonElements) { return matcher(elem, context, xml); } } } : // Check against all ancestor/preceding elements function(elem, context, xml) { var data, cache, outerCache, dirkey = dirruns + " " + doneName; // We can't set arbitrary data on XML nodes, so they don't // benefit from dir caching if (xml) { while ((elem = elem[dir])) { if (elem.nodeType === 1 || checkNonElements) { if (matcher(elem, context, xml)) { return true; } } } } else { while ((elem = elem[dir])) { if (elem.nodeType === 1 || checkNonElements) { outerCache = elem[expando] || (elem[expando] = {}); if ((cache = outerCache[dir]) && cache[0] === dirkey) { if ((data = cache[1]) === true || data === cachedruns) { return data === true; } } else { cache = outerCache[dir] = [ dirkey ]; cache[1] = matcher(elem, context, xml) || cachedruns; if (cache[1] === true) { return true; } } } } } }; }
產生關係選擇器(Combinator)的執行函式。
matcher——位置關係之前、連續的過濾選擇器匹配函式陣列,用來判斷透過位置關係取得的節點是否符合選擇器。實際執行時,它往往就是關係選擇器之前已生成的 elementMatcher(matchers)。例如 div.map>span,Sizzle 編譯遇到 > 時,會把 div.map 的編譯函式作為第一個參數傳入 addCombinator,用來檢查取到的 span 父節點是否同時滿足 div 與 .map。
combinator——關係選擇器對應 Expr.relative 中的設定,其值如下。程式碼用它的 first 屬性決定回傳的是「只檢查緊鄰對象」還是「遍歷所有可能對象」的函式;並透過 elem = elem[dir] 取得指定位置關係的節點,其中 dir 等於 combinator.dir。
Expr.relative : { ">" : { dir : "parentNode", first : true }, " " : { dir : "parentNode" }, "+" : { dir : "previousSibling", first : true }, "~" : { dir : "previousSibling" } }
base——與 combinator.dir 共同決定 checkNonElements 的值。顧名思義,它代表「當前檢查的是否為非 DOM 元素」:當 elem.nodeType 不等於 1 時,若此值為 true 仍會執行匹配函式,否則結束本次迴圈。
4.1 當關係選擇器是 > 或 + 時,回傳:
function(elem, context, xml) { while ((elem = elem[dir])) { if (elem.nodeType === 1 || checkNonElements) { return matcher(elem, context, xml); } } }
4.1.1 功能:若只檢查 element 類型節點(checkNonElements 為 false),會迭代取得 elem 指定位置關係上「第一個 element 節點」並執行匹配,符合回傳 true,否則 false;若檢查所有類型節點(checkNonElements 為 true),則取指定關係的緊鄰節點執行匹配。之所以需要迭代,是因為部分瀏覽器會把標籤之間的換行當成 TextNode,必須跳過它們直到下一個 element 節點。
4.1.2 參數:elem 為待檢查的單一節點;context 為上下文節點,多數時候用不到;xml 標示 HTML 或 XML 文件。
4.2 當關係選擇器是 ~ 或空白時,回傳帶有 dir 緩存的函式(源碼同上)。
4.2.1 功能:若檢查的是 XML 文件,流程與 4.1 一致(見 if (xml) { ... } 區塊);若為 HTML 文件,則依 matcher 匹配當前元素,成功回傳 true,否則繼續往上/往前找符合關係的節點。
4.2.3 內部變數與緩存說明:
dirkey——節點檢測結果的緩存鍵。同一次執行中,某節點若已被檢測過,會在其 dirkey 屬性裡記錄結果(true 或 false),再次遇到時便無需重算。由於多個節點常共用同一父節點或兄弟節點,緩存能顯著減少檢測次數、提升效能。
dirruns——每次執行由 matcherFromGroupMatchers 組織的預編譯碼時都會產生一個偽隨機數,用來區分不同的執行過程。
doneName——每次呼叫 addCombinator,done 都會加 1,用來區分生成的不同位置關係匹配函式。
cachedruns——用來記錄本次匹配是第幾個 DOM 元素。例如 div.map>span 若有 3 個元素符合 span,則針對每個元素執行 > 匹配時,cachedruns 依序為 0、1、2。其作用是在同一次執行中,若針對同一元素以 elementMatchers 匹配時再次遇到它,可直接取用先前的不匹配結果——不過作者坦言尚未想出究竟何種情境會觸發此情形,若讀者有發現,歡迎分享。
核心迴圈邏輯可歸納為:先取 outerCache,若 outerCache[dir] 的首元素等於當前 dirkey,代表本次執行已檢測過該節點,直接從緩存取結果(結果為 true 或 cachedruns 時回傳,非 true 一律視為 false 並繼續往前找);否則寫入 [ dirkey ] 並執行 matcher,把結果(true 或 cachedruns)存入 cache[1],命中 true 即回傳,否則繼續迴圈。