jquery 源码分析九 - Sizzle
最近一直在搞畢設(shè),實(shí)在是沒時(shí)間寫博客了,零碎的時(shí)間看代碼進(jìn)度也不快,所以寫博客一拖再拖了,今天先補(bǔ)一篇上來。。。
在上次寫得setDocument以后,緊接著的是一些零碎的功能函數(shù)
1 Sizzle.matches = function( expr, elements ) { 2 return Sizzle( expr, null, null, elements ); 3 }; 4 5 Sizzle.matchesSelector = function( elem, expr ) { 6 // 設(shè)置document以防萬一 7 if ( ( elem.ownerDocument || elem ) !== document ) { 8 setDocument( elem ); 9 } 10 11 // 確保屬性值被引號(hào)包裹 12 expr = expr.replace( rattributeQuotes, "='$1']" ); 13 14 if ( support.matchesSelector && documentIsHTML && 15 ( !rbuggyMatches || !rbuggyMatches.test( expr ) ) && 16 ( !rbuggyQSA || !rbuggyQSA.test( expr ) ) ) { 17 18 try { 19 var ret = matches.call( elem, expr ); 20 21 // 在IE9下matchesSelector會(huì)返回false(在IE10+情況下,開發(fā)者模式調(diào)到IE9,8是正常的,IETester下返回false) 22 if ( ret || support.disconnectedMatch || 23 // 同樣的,在IE9下,這種無關(guān)節(jié)點(diǎn)被認(rèn)為是在文檔片段中 24 elem.document && elem.document.nodeType !== 11 ) { 25 return ret; 26 } 27 } catch(e) {} 28 } 29 30 return Sizzle( expr, document, null, [elem] ).length > 0; 31 }; 32 33 Sizzle.contains = function( context, elem ) { 34 // 設(shè)置document 35 if ( ( context.ownerDocument || context ) !== document ) { 36 setDocument( context ); 37 } 38 return contains( context, elem ); 39 }; 40 41 Sizzle.attr = function( elem, name ) { 42 // 設(shè)置document 43 if ( ( elem.ownerDocument || elem ) !== document ) { 44 setDocument( elem ); 45 } 46 47 var fn = Expr.attrHandle[ name.toLowerCase() ], 48 // 防止取到Object.prototype里的屬性 (jQuery #13807) 49 val = fn && hasOwn.call( Expr.attrHandle, name.toLowerCase() ) ? 50 fn( elem, name, !documentIsHTML ) : 51 undefined; 52 53 return val !== undefined ? 54 val : 55 support.attributes || !documentIsHTML ? 56 elem.getAttribute( name ) : 57 (val = elem.getAttributeNode(name)) && val.specified ? 58 val.value : 59 null; 60 }; 61 62 Sizzle.error = function( msg ) { 63 throw new Error( "Syntax error, unrecognized expression: " + msg ); 64 };在這邊,Sizzle.matches和Sizzle.matchesSelector在最后都是以return Sizzle( expr, null, null, elements )形式返回的,由于在Sizzle中,在第四項(xiàng)有值時(shí),就會(huì)跳過一個(gè)大的if語句塊
if ( documentIsHTML && !seed ) { }然后會(huì)直接跳到最后執(zhí)行select函數(shù)
return select( selector.replace( rtrim, "$1" ), context, results, seed );對于select函數(shù),先放一下。。。等看到的時(shí)候再分析。。。
接下來的是一個(gè)主要剔除重復(fù)+排序的函數(shù),源碼如下:
Sizzle.uniqueSort = function( results ) {var elem,duplicates = [],j = 0,i = 0;// 默認(rèn)確實(shí)有重復(fù)hasDuplicate = !support.detectDuplicates;sortInput = !support.sortStable && results.slice( 0 );results.sort( sortOrder );if ( hasDuplicate ) {while ( (elem = results[i++]) ) {if ( elem === results[ i ] ) {j = duplicates.push( i );}}while ( j-- ) {results.splice( duplicates[ j ], 1 );}}// See https://github.com/jquery/sizzle/pull/225sortInput = null;return results; };在這里一開始先默認(rèn)確實(shí)有重復(fù)項(xiàng),然后在result.sort的時(shí)候,用sortOrder來排序,這個(gè)函數(shù)在setDocument中定義了,其中有一段就是設(shè)置hasDuplicate的:
if ( a === b ) {hasDuplicate = true; }注:hasDuplicate在Sizzle閉包作用域中屬于全局變量。
然后是一個(gè)拿元素內(nèi)文本內(nèi)容的函數(shù),主要調(diào)用了textContent,源碼如下:
1 getText = Sizzle.getText = function( elem ) { 2 var node, 3 ret = "", 4 i = 0, 5 nodeType = elem.nodeType; 6 7 if ( !nodeType ) { 8 // 如果沒有nodeType,那么就認(rèn)為是一個(gè)普通數(shù)組 9 while ( (node = elem[i++]) ) { 10 ret += getText( node ); 11 } 12 } else if ( nodeType === 1 || nodeType === 9 || nodeType === 11 ) { 13 // 對于元素用textContent 14 // innerText 已被移除 15 if ( typeof elem.textContent === "string" ) { 16 return elem.textContent; 17 } else { 18 // 對子節(jié)點(diǎn)遍歷 19 for ( elem = elem.firstChild; elem; elem = elem.nextSibling ) { 20 ret += getText( elem ); 21 } 22 } 23 } else if ( nodeType === 3 || nodeType === 4 ) { 24 return elem.nodeValue; 25 } 26 // 排除注釋節(jié)點(diǎn) 27 28 return ret; 29 };它用了一個(gè)遞歸調(diào)用的方法,防止注釋等節(jié)點(diǎn)的混入。
在后面就是一個(gè)
1 Expr = Sizzle.selectors = { 2 // ..... 3 }這里放了許多關(guān)于篩選元素的函數(shù),暫時(shí)先跳過了。下一篇的話從后面的tokenize開始講。明后天估計(jì)還是要去實(shí)驗(yàn)室搬磚,爭取在零碎時(shí)間多看看源碼,早點(diǎn)把下一篇發(fā)上來。。。
這篇好像有點(diǎn)短了。。。
?
轉(zhuǎn)載于:https://www.cnblogs.com/cyITtech/p/3650885.html
總結(jié)
以上是生活随笔為你收集整理的jquery 源码分析九 - Sizzle的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于1.3.3版本tooltip的dat
- 下一篇: native关键字(本地方法)、 jav