MCPcopy Create free account
hub / github.com/JsAaron/jQuery / Sizzle

Function Sizzle

2.0.3/Sizzle-annotation.js:208–322  ·  view source on GitHub ↗

* [Sizzle description] selector css选择器, context上下文,results结果集,seed筛选集 前面已经说到CSS选择器解析时是按照从右到左的顺序分析, 因此第一步是先取出最右的规则, 如果说我们能够通过最右的规则先确定一个基本符合条件的集合那效率肯定是最好的。 如果确定不了,那就只能把整个DOM树节点拿出来作为初始集合了, 在Sizzle里边,这个集合命名为seed。Sizzle里边搜索开始的入口是select函数。

(selector, context, results, seed)

Source from the content-addressed store, hash-verified

206 */
207
208 function Sizzle(selector, context, results, seed) {
209
210 var match, elem, m, nodeType,
211 // QSA vars
212 i, groups, old, nid, newContext, newSelector;
213
214 if ((context ? context.ownerDocument || context : preferredDoc) !== document) {
215 setDocument(context);
216 }
217
218 context = context || document;
219 results = results || [];
220
221 if (!selector || typeof selector !== "string") {
222 return results;
223 }
224
225 if ((nodeType = context.nodeType) !== 1 && nodeType !== 9) {
226 return [];
227 }
228
229 //如果是单条规则的选择器,能通过直接原生接口:
230 //getElementById|getElementsByTagName|getElementsByClassName,得到的,那就直接调原生接口
231 //如果是多条规则的情况,先看看浏览器有没有原生的querySelectorAll接口
232 /**
233 if ( documentIsHTML && !seed ) {
234
235 // Shortcuts
236 if ( (match = rquickExpr.exec( selector )) ) {
237 // Speed-up: Sizzle("#ID")
238 if ( (m = match[1]) ) {
239 if ( nodeType === 9 ) {
240 elem = context.getElementById( m );
241 // Check parentNode to catch when Blackberry 4.6 returns
242 // nodes that are no longer in the document #6963
243 if ( elem && elem.parentNode ) {
244 // Handle the case where IE, Opera, and Webkit return items
245 // by name instead of ID
246 if ( elem.id === m ) {
247 results.push( elem );
248 return results;
249 }
250 } else {
251 return results;
252 }
253 } else {
254 // Context is not a document
255 if ( context.ownerDocument && (elem = context.ownerDocument.getElementById( m )) &&
256 contains( context, elem ) && elem.id === m ) {
257 results.push( elem );
258 return results;
259 }
260 }
261
262 // Speed-up: Sizzle("TAG")
263 } else if ( match[2] ) {
264 push.apply( results, context.getElementsByTagName( selector ) );
265 return results;

Callers 2

multipleContextsFunction · 0.70

Calls 1

selectFunction · 0.70

Tested by

no test coverage detected