sizzle是jquery的核心,它用来选择匹配的元素,其代码包含在一个匿名函数中,并以window作为其上下文环境:
(function( window, undefined ) {
//此处为sizzle代码
})( window );
匿名函数体内,首先申明了很多变量,比如下边这些正则表达式:
  1. classCache = createCache(),
  2. /*classCache:
  3. function cache( key, value ) {
  4. // Use (key + " ") to avoid collision with native prototype properties (see Issue #157)
  5. if ( keys.push( key += " " ) > Expr.cacheLength ) {
  6. // Only keep the most recent entries
  7. delete cache[ keys.shift() ];
  8. }
  9. return (cache[ key ] = value);
  10. }
  11. */
  12. tokenCache = createCache(),
  13. /*tokenCache:
  14. function cache( key, value ) {
  15. // Use (key + " ") to avoid collision with native prototype properties (see Issue #157)
  16. if ( keys.push( key += " " ) > Expr.cacheLength ) {
  17. // Only keep the most recent entries
  18. delete cache[ keys.shift() ];
  19. }
  20. return (cache[ key ] = value);
  21. }
  22. */
  23. compilerCache = createCache(),
  24. /*compilerCache:
  25. function cache( key, value ) {
  26. // Use (key + " ") to avoid collision with native prototype properties (see Issue #157)
  27. if ( keys.push( key += " " ) > Expr.cacheLength ) {
  28. // Only keep the most recent entries
  29. delete cache[ keys.shift() ];
  30. }
  31. return (cache[ key ] = value);
  32. }
  33. */
  34. hasDuplicate = false,
  35. sortOrder = function( a, b ) {
  36. if ( a === b ) {
  37. hasDuplicate = true;
  38. return 0;
  39. }
  40. return 0;
  41. },
  42.  
  43. // General-purpose constants
  44. strundefined = typeof undefined,
  45. MAX_NEGATIVE = 1 << 31,
  46.  
  47. // Instance methods
  48. hasOwn = ({}).hasOwnProperty,
  49. arr = [],
  50. pop = arr.pop,
  51. push_native = arr.push,
  52. push = arr.push,
  53. slice = arr.slice,
  54. // Use a stripped-down indexOf if we can't use a native one
  55. indexOf = arr.indexOf || function( elem ) {
  56. var i = 0,
  57. len = this.length;
  58. for ( ; i < len; i++ ) {
  59. if ( this[i] === elem ) {
  60. return i;
  61. }
  62. }
  63. return -1;
  64. },
  65.  
  66. booleans = "checked|selected|async|autofocus|autoplay|controls|defer|disabled|hidden|ismap|loop|multiple|open|readonly|required|scoped",
  67.  
  68. // Regular expressions
  69.  
  70. // Whitespace characters http://www.w3.org/TR/css3-selectors/#whitespace
  71. //匹配空白 水平制表符 回车 换行 换页
  72. whitespace = "[\\x20\\t\\r\\n\\f]",
  73. // http://www.w3.org/TR/css3-syntax/#characters
  74. //可以匹配 由\\. 或 单词字符(所有字母数字下划线) 或 汉字(包括汉字标点符号),这里也可以说是全角符号组成
  75. characterEncoding = "(?:\\\\.|[\\w-]|[^\\x00-\\xa0])+",
  76.  
  77. // Loosely modeled on CSS identifier characters
  78. // An unquoted value should be a CSS identifier http://www.w3.org/TR/css3-selectors/#attribute-selectors
  79. // Proper syntax: http://www.w3.org/TR/CSS21/syndata.html#value-def-identifier
  80. //识别码
  81. identifier = characterEncoding.replace( "w", "w#" ),
  82.  
  83. // Acceptable operators http://www.w3.org/TR/selectors/#attribute-selectors
  84. //attributes ="\[[\x20\t\r\n\f]*((?:\\.|[\w-]|[^\x00-\xa0])+)[\x20\t\r\n\f]*(?:([*^$|!~]?=)[\x20\t\r\n\f]*(?:(['"])((?:\\.|[^\\])*?)\3|((?:\\.|[\w#-]|[^\x00-\xa0])+)|)|)[\x20\t\r\n\f]*\]"
  85. //匹配属性 例如:[a="b"] [a=b] [a|='er']
  86. attributes = "\\[" + whitespace + "*(" + characterEncoding + ")" + whitespace +
  87. "*(?:([*^$|!~]?=)" + whitespace + "*(?:(['\"])((?:\\\\.|[^\\\\])*?)\\3|(" + identifier + ")|)|)" + whitespace + "*\\]",
  88.  
  89. // Prefer arguments quoted,
  90. // then not containing pseudos/brackets,
  91. // then attribute selectors/non-parenthetical expressions,
  92. // then anything else
  93. // These preferences are here to reduce the number of selectors
  94. // needing tokenize in the PSEUDO preFilter
  95. pseudos = ":(" + characterEncoding + ")(?:\\(((['\"])((?:\\\\.|[^\\\\])*?)\\3|((?:\\\\.|[^\\\\()[\\]]|" + attributes.replace( 3, 8 ) + ")*)|.*)\\)|)",
  96.  
  97. // Leading and non-escaped trailing whitespace, capturing some non-whitespace characters preceding the latter
  98. //匹配开始或结束的空白字符
  99. rtrim = new RegExp( "^" + whitespace + "+|((?:^|[^\\\\])(?:\\\\.)*)" + whitespace + "+$", "g" ),
  100. //匹配逗号
  101. rcomma = new RegExp( "^" + whitespace + "*," + whitespace + "*" ),
  102. //匹配组合器 包括> + ~ 及"[\\x20\\t\\r\\n\\f]"
  103. rcombinators = new RegExp( "^" + whitespace + "*([>+~]|" + whitespace + ")" + whitespace + "*" ),
  104. //兄弟节点?
  105. rsibling = new RegExp( whitespace + "*[+~]" ),
  106. //属性选择器的等号后边部分,如:=aa]
  107. rattributeQuotes = new RegExp( "=" + whitespace + "*([^\\]'\"]*)" + whitespace + "*\\]", "g" ),
  108. //rpseudo=/:((?:\\.|[\w-]|[^\x00-\xa0])+)(?:\(((['"])((?:\\.|[^\\])*?)\3|((?:\\.|[^\\()[\]]|\[[\x20\t\r\n\f]*((?:\\.|[\w-]|[^\x00-\xa0])+)[\x20\t\r\n\f]*(?:([*^$|!~]?=)[\x20\t\r\n\f]*(?:(['"])((?:\\.|[^\\])*?)\8|((?:\\.|[\w#-]|[^\x00-\xa0])+)|)|)[\x20\t\r\n\f]*\])*)|.*)\)|)/
  109. //伪类选择器 如::nth-child(3)
  110. rpseudo = new RegExp( pseudos ),
  111. //匹配识别码
  112. ridentifier = new RegExp( "^" + identifier + "$" ),
  113.  
  114. matchExpr = {//匹配各种选择器的正则表达式
  115. "ID": new RegExp( "^#(" + characterEncoding + ")" ),//ID选择器
  116. "CLASS": new RegExp( "^\\.(" + characterEncoding + ")" ),//类
  117. "TAG": new RegExp( "^(" + characterEncoding.replace( "w", "w*" ) + ")" ),
  118. "ATTR": new RegExp( "^" + attributes ),
  119. "PSEUDO": new RegExp( "^" + pseudos ),
  120. "CHILD": new RegExp( "^:(only|first|last|nth|nth-last)-(child|of-type)(?:\\(" + whitespace +
  121. "*(even|odd|(([+-]|)(\\d*)n|)" + whitespace + "*(?:([+-]|)" + whitespace +
  122. "*(\\d+)|))" + whitespace + "*\\)|)", "i" ),
  123. "bool": new RegExp( "^(?:" + booleans + ")$", "i" ),
  124. // For use in libraries implementing .is()
  125. // We use this for POS matching in `select`
  126. "needsContext": new RegExp( "^" + whitespace + "*[>+~]|:(even|odd|eq|gt|lt|nth|first|last)(?:\\(" +
  127. whitespace + "*((?:-\\d)?\\d*)" + whitespace + "*\\)|)(?=[^-]|$)", "i" )
  128. },
  129. //检查是否原生代码
  130. rnative = /^[^{]+\{\s*\[native \w/,
  131.  
  132. // Easily-parseable/retrievable ID or TAG or CLASS selectors
  133. rquickExpr = /^(?:#([\w-]+)|(\w+)|\.([\w-]+))$/,
  134. //匹配input元素,其实包含了四种:input|select|textarea|button
  135. rinputs = /^(?:input|select|textarea|button)$/i,
  136. rheader = /^h\d$/i,
  137.  
  138. rescape = /'|\\/g,
  139.  
  140. // CSS escapes http://www.w3.org/TR/CSS21/syndata.html#escaped-characters
  141. //匹配解码字符
  142. runescape = new RegExp( "\\\\([\\da-f]{1,6}" + whitespace + "?|(" + whitespace + ")|.)", "ig" ),
  143. funescape = function( _, escaped, escapedWhitespace ) {//_代表整个匹配项,escaped代表第一个匹配项,escapedWhitespace代表第二个分组匹配项,即空白匹配项
  144. var high = "0x" + escaped - 0x10000;
  145. // NaN means non-codepoint
  146. // Support: Firefox
  147. // Workaround erroneous numeric interpretation of +"0x"
  148. return high !== high || escapedWhitespace ?
  149. escaped :
  150. // BMP codepoint
  151. high < 0 ?
  152. String.fromCharCode( high + 0x10000 ) :
  153. // Supplemental Plane codepoint (surrogate pair)
  154. String.fromCharCode( high >> 10 | 0xD800, high & 0x3FF | 0xDC00 );
  155. };
接下来就是一些功能函数,下边这张图说明了其中的功能函数的作用:

 
最后把sizzle和jquery关联起来:
jQuery.find = Sizzle;
jQuery.expr = Sizzle.selectors;
jQuery.expr[":"] = jQuery.expr.pseudos;
jQuery.unique = Sizzle.uniqueSort;
jQuery.text = Sizzle.getText;
jQuery.isXMLDoc = Sizzle.isXML;
jQuery.contains = Sizzle.contains;

sizzle源码分析 (1)sizzle架构的更多相关文章

  1. Sizzle源码分析 (一)

    Sizzle 源码分析 (一) 2.1 稳定 版本 Sizzle 选择器引擎博大精深,下面开始阅读它的源代码,并从中做出标记 .先从入口开始,之后慢慢切入 . 入口函数 Sizzle () 源码 19 ...

  2. jQuery1.11源码分析(1)-----Sizzle源码概览[原创]

    最近在啃jQuery1.11源码,上来就遇到Sizzle这个jQuery的大核心,虽然已经清楚了Sizzle的用途,先绕过去也没事,但明知山有虎偏向虎山行才是我们要做的. 本文面向的阅读对象:正在学习 ...

  3. sizzle源码分析 (4)sizzle 技术总结及值得我们学习的地方

    分析sizzle源码并不是为了去钻牛角尖,而是去了解它的思想,学习下期中一些技术的运用. 1,sizzle中的正则表达式jquery源码中充斥着各种正则表达式,能否看懂其源码的关键之一就是对正则表达式 ...

  4. jQuery1.11源码分析(5)-----Sizzle编译和过滤阶段[原创]

    在上一章中,我们说到在之前的查找阶段我们已经获得了待选集seed,那么这一章我们就来讲如何将seed待选集过滤,以获得我们最终要用的元素. 其实思路本质上还是不停地根据token过滤,但compile ...

  5. Sizzle源码分析:一 设计思路

    一.前言 DOM选择器(Sizzle)是jQuery框架中非常重要的一部分,在H5还没有流行起来的时候,jQuery为我们提供了一个简洁,方便,高效的DOM操作模式,成为那个时代的经典.虽然现在Vue ...

  6. jQuery 源码分析 7: sizzle

    jQuery使用的是sizzle这个选择器引擎,这个引擎以其高速著称,其实现十分精妙但是也足够复杂,下面现简单分析一下相关的代码. 在jQuery的部分API接口是直接引用了Sizzle的方法,这些接 ...

  7. Sizzle源码分析:三 筛选和编译

    好了有了之前的词法分析过程,现在我们来到select函数来,这个函数的整体流程,前面也大概说过: 1. 先做词法分析获得token列表 2. 如果有种子集合直接到编译过程 3. 如果没有种子集合并且是 ...

  8. jQuery1.11源码分析(2)-----Sizzle源码中的正则表达式[原创]

    看完了上篇,对Sizzle有了一个大致的了解,我们接下来就可以正式开始啃Sizzle的源码了.上来就讲matcher难度太大,先来点开胃菜,讲讲Sizzle中的各个正则表达式的作用吧(本来还想讲初始化 ...

  9. jQuery源码分析-01总体架构

    1. 总体架构 1.1自调用匿名函数 self-invoking anonymous function 打开jQuery源码,首先你会看到这样的代码结构: (function( window, und ...

  10. jQuery 2.0.3 源码分析core - 整体架构

    拜读一个开源框架,最想学到的就是设计的思想和实现的技巧. 废话不多说,jquery这么多年了分析都写烂了,老早以前就拜读过, 不过这几年都是做移动端,一直御用zepto, 最近抽出点时间把jquery ...

随机推荐

  1. CSS多列布局

    × 目录 [1]列宽 [2]列数 [3]列间距[4]列rule[5]跨列[6]列填充[7]多列 前面的话 CSS新增了多列布局特性,可以让浏览器确定何时结束一列和开始下一列,无需任何额外的标记.简单来 ...

  2. vs xamarin android 监听返回键退出程序

    public override bool OnKeyDown([GeneratedEnum]Keycode keyCode, KeyEvent e) { if (keyCode == Keycode. ...

  3. Tools - VirtualBox

    为CentOS虚拟机安装增强功能 启动CentOS虚拟机,点击"菜单 -> 设备 -> 安装增强功能". vboxadd的映像文件将会被挂载到虚拟机,在桌面也可以看到, ...

  4. 15个来自 CodePen 的酷炫 CSS 动画效果【下篇】

    CodePen 是一个在线的前端代码编辑和展示网站,能够编写代码并即时预览效果.你在上面可以在线分享自己的 Web 作品,也可以欣赏到世界各地的优秀开发者在网页中实现的各种令人惊奇的效果. 今天这篇文 ...

  5. 第一篇博文:PHP函数原型中的可选参数写法为什么这么写?

    第一篇,算是开始吧.简单写点儿东西. 刚开始学PHP,在看PHP Manual时遇到一个问题:含可选参数的函数原型中,可选参数的写法看不懂. 例如explode函数 array explode ( s ...

  6. 2.SDK目录结构和adb工具及命令介绍

    安卓开发学习笔记 1.安卓开发之环境搭建 2.SDK目录结构和adb工具及命令介绍 1.SDK目录介绍: ******************************** add-ons:Androi ...

  7. 在这个变化的年代,IT人的方向在哪里?看两个故事

    王超是我的朋友,来京四年整.最初在一家民企做LINUX运维工程师,月薪5000.工作很认真,埋头苦干型,每天工作时间很长,让加班从来无怨言.即使是周末休假,只要有工作任务也是随叫随到.然而当他提涨薪时 ...

  8. redis学习之二from github

    大概敲了一遍基本命令,熟悉了redis的存储方式.现在开始进一步系统的学习.学习教程目前计划有三个,一个是github上的https://github.com/JasonLai256/the-litt ...

  9. Laravel4中的Validator

    不管写接口还是写web页面,实质都是传入参数,然后进行业务逻辑,然后再输出具体内容.所以,对参数的验证是不可避免的一个环节,比如传过来的email是不是为空,是不是合法的email格式?laravel ...

  10. SQL - 内连接与外连接

    PDF下载地址:SQL-内连接与外连接.pdf 连接查询在关系型数据库中经常用到,是多表联合查询的基础. 主要包含:内连接,外连接,交叉连接. SQL - 内连接与外连接 内连接 等值连接 不等值连接 ...