Sizzle 源码分析 (转)

 1 /**
 2      * 
 3      * 这个方法是Sizzle 的入口
 4      */
 5     var Sizzle = function(selector, context, results, seed) {
 6         /**
 7          * results是保存结果的数组 context默认为document 各种局部变量的定义
 8          */
 9         results = results || [];
10         context = context || document;
11         var match, elem, contextXML, m, nodeType = context.nodeType;
12         // nodeType ==9说明context是document nodeType ==1表示context是element
13         if (nodeType !== 1 && nodeType !== 9) {
14             return [];
15         }
16         // 如果选择器不是字符串或者为空 返回一个空的数组
17         if (!selector || typeof selector !== "string") {
18             return results;
19         }
20         // 是否是xml格式
21         contextXML = isXML(context);
22         // seed是预留的参数 一般情况下不会有值
23         if (!contextXML && !seed) {
24             /**
25              * rquickExpr正则表达式匹配#号.号开头后接多个单词字符包括上划线- 或者多个单词字符 一旦匹配成功------------
26              * match[1]对应 #ID -----------match[2]对应 TAG -------match[3]对应 .class
27              * 
28              */
29             if ((match = rquickExpr.exec(selector))) {
30                 /**
31                  * 处理Sizzle("#ID")
32                  */
33                 // Speed-up: Sizzle("#ID")
34                 if ((m = match[1])) {
35                     // 如果上下文是document 可以直接使用document.getElementById方法获取元素
36                     if (nodeType === 9) {
37                         elem = context.getElementById(m);
38                         // Check parentNode to catch when Blackberry 4.6 returns
39                         // nodes that are no longer in the document #6963
40                         if (elem && elem.parentNode) {
41                             // Handle the case where IE, Opera, and Webkit
42                             // return items
43                             // by name instead of ID
44                             if (elem.id === m) {
45                                 /**
46                                  * makeArray的作用就是合并两个对象或者数组 并返回合并后的数组或对象
47                                  * 把结果保存到results
48                                  */
49                                 return makeArray([elem], results);
50                             }
51                         } else {
52                             return makeArray([], results);
53                         }
54                     } else {
55                         /**
56                          * 如果Context不是document 先通过ownerDocument属性取得document
57                          * 然后在再通过document.getElementById来获取element
58                          */
59                         // Context is not a document
60                         if (context.ownerDocument
61                                 && (elem = context.ownerDocument
62                                         .getElementById(m))
63                                 && contains(context, elem) && elem.id === m) {
64                             return makeArray([elem], results);
65                         }
66                     }
67                     /**
68                      * 处理 Sizzle("TAG")
69                      * 通过element元素的getElementsByTagName方法获取选择器表达式
70                      */
71                     // Speed-up: Sizzle("TAG")
72                 } else if (match[2]) {
73                     return makeArray(context.getElementsByTagName(selector),
74                             results);
75                     /**
76                      * 处理 Sizzle(".CLASS")
77                      * assertUsableClassName测试各个浏览器element是否支持getElementsByClassName方法
78                      * 
79                      */
80                     // Speed-up: Sizzle(".CLASS")
81                 } else if (assertUsableClassName && (m = match[3])
82                         && context.getElementsByClassName) {
83                     return makeArray(context.getElementsByClassName(m), results);
84                 }
85             }
86         }
87 
88         // All others
89         return select(selector, context, results, seed, contextXML);
90     };


来源地址:http://goodscript.iteye.com/blog/1569285

posted @ 2013-09-06 15:28  糜基  阅读(240)  评论(0)    收藏  举报