上一節(jié)我們處理分組選擇器,這次我們看看剩余的2種處理情況。
關(guān)系處理器處理
在層級(jí)關(guān)系中有幾種特殊的劃分 Token : >, +, 空格, ~ 用來表明:父與子,兄弟,祖輩子孫之間的層級(jí)關(guān)系。
selector = 'div.aaron,div > p'
從 > 劃分
rcombinators = new RegExp( "^" + whitespace + "*([>+~]|" + whitespace + ")" + whitespace + "*" )
可以是>+~
或者空白
這個(gè)分組是為了之后的關(guān)系選擇確定。
if ( (match = rcombinators.exec( soFar )) ) { matched = match.shift(); tokens.push({ value: matched, // Cast descendant combinators to space type: match[0].replace( rtrim, " " ) }); soFar = soFar.slice( matched.length ); }
元素的匹配器:
Expr.filter :TAG, ID, CLASS, ATTR, CHILD, PSEUDO
通過一系列的正則抽出表達(dá)式中的內(nèi)容。
ID:
///^#((?:\\.|[\w-] | [^\x00-\xa0] ) +)/ var characterEncoding = "(?:\\\\.|[\\w-]|[^\\x00-\\xa0])+"; var ID = new RegExp("^#(" + characterEncoding + ")")
TAG:
var TAG = new RegExp( "^(" + characterEncoding.replace( "w", "w*" ) + ")" );
CLASS:
var Class = new RegExp( "^\\.(" + characterEncoding + ")" );
ATTR:
屬性選擇器有點(diǎn)復(fù)雜,通過第一次正則只能匹配器出整體,所以需要第二次分解,引入了Expr.preFilter,Expr.preFilter保留了3個(gè)兼容處理分別是ATTR,CHILD,PSEUDO復(fù)雜的選擇器。
var identifier = characterEncoding.replace( "w", "w#" ); var attributes = "\\[" + whitespace + "*(" + characterEncoding + ")(?:" + whitespace + // Operator (capture 2) "*([*^$|!~]?=)" + whitespace + // "Attribute values must be CSS identifiers [capture 5] or strings [capture 3 or capture 4]" "*(?:'((?:\\\\.|[^\\\\'])*)'|\"((?:\\\\.|[^\\\\\"])*)\"|(" + identifier + "))|)" + whitespace + "*\\]"; var ATTR = new RegExp( "^" + attributes ); preFilter: { "ATTR": function( match ) { match[1] = match[1].replace( runescape, funescape ); // Move the given value to match[3] whether quoted or unquoted match[3] = ( match[3] || match[4] || match[5] || "" ).replace( runescape, funescape ); if ( match[2] === "~=" ) { match[3] = " " + match[3] + " "; } return match.slice( 0, 4 ); }
請(qǐng)驗(yàn)證,完成請(qǐng)求
由于請(qǐng)求次數(shù)過多,請(qǐng)先驗(yàn)證,完成再次請(qǐng)求
打開微信掃碼自動(dòng)綁定
綁定后可得到
使用 Ctrl+D 可將課程添加到書簽
舉報(bào)