zepto-selector.js簡單分析

來源:互聯網
上載者:User

標籤:des   style   class   blog   code   http   

zepto 的selector模組是對jquery擴充選取器(jquery-selector-extensions)的部分實現。比如這樣的選擇方法:$(‘div:first‘) 和 el.is(‘:visible‘)。

下面是原代碼,簡單的寫了一些注釋~

;(function($){  var zepto = $.zepto, oldQsa = zepto.qsa, oldMatches = zepto.matches  /*  *  檢察一個元素是否可見。除了要判斷display是否是none之外,還判斷了width和height是否是0,  雙歎號是強制轉化成boolean類型  */  function visible(elem){    elem = $(elem)    return !!(elem.width() || elem.height()) && elem.css("display") !== "none"  }  // 實現的是jquey選取器擴充的一部分  // http://api.jquery.com/category/selectors/jquery-selector-extensions/  //  // 每一個filter函數的參數都能接受當前值,所有考慮範圍內的節點和括弧中的值  // this就是當前被考慮的node. 函數返回的是node(s), null 或者是undefined  //  // 複雜的選取器是不被支援的,比如下面的:  //   li:has(label:contains("foo")) + li:has(label:contains("bar"))  //   ul.inner:first > li  var filters = $.expr[‘:‘] = {    visible:  function(){ if (visible(this)) return this },//可見    hidden:   function(){ if (!visible(this)) return this },//不可見    selected: function(){ if (this.selected) return this },//選中    checked:  function(){ if (this.checked) return this },//勾選中    parent:   function(){ return this.parentNode },//父節點    first:    function(idx){ if (idx === 0) return this },//第一個元素    last:     function(idx, nodes){ if (idx === nodes.length - 1) return this },//最後一個元素    eq:       function(idx, _, value){ if (idx === value) return this },//相同的元素    contains: function(idx, _, text){ if ($(this).text().indexOf(text) > -1) return this },//內容含有的元素    has:      function(idx, _, sel){ if (zepto.qsa(this, sel).length) return this }//  }  var filterRe = new RegExp(‘(.*):(\\w+)(?:\\(([^)]+)\\))?$\\s*‘),//一個強大的Regex用來分解選取器的的,見下面      childRe  = /^\s*>/,      classTag = ‘Zepto‘ + (+new Date())  function process(sel, fn) {//分解選取器為三部分,第一部分是選取器本身,第二部分是選取器的值filter中的函數名稱,第三部分是參數    //例如:(1)filterRe.exec(":eq(2)")    //得到的結果:[":eq(2)", "", "eq", "2"]    //(2)filterRe.exec(":visible")    //得到的結果:[":visible", "", "visible", undefined]    // quote the hash in `a[href^=#]` expression    sel = sel.replace(/=#\]/g, ‘="#"]‘)    var filter, arg, match = filterRe.exec(sel)    if (match && match[2] in filters) {      filter = filters[match[2]], arg = match[3]//filter為filters中對應的函數      sel = match[1]      if (arg) {        var num = Number(arg)        if (isNaN(num)) arg = arg.replace(/^["‘]|["‘]$/g, ‘‘)        else arg = num      }    }    return fn(sel, filter, arg)  }  zepto.qsa = function(node, selector) {    return process(selector, function(sel, filter, arg){      try {        var taggedParent        if (!sel && filter) sel = ‘*‘        else if (childRe.test(sel))          // support "> *" child queries by tagging the parent node with a          // unique class and prepending that classname onto the selector          taggedParent = $(node).addClass(classTag), sel = ‘.‘+classTag+‘ ‘+sel        var nodes = oldQsa(node, sel)      } catch(e) {        console.error(‘error performing selector: %o‘, selector)        throw e      } finally {        if (taggedParent) taggedParent.removeClass(classTag)      }      return !filter ? nodes :        zepto.uniq($.map(nodes, function(n, i){ return filter.call(n, i, nodes, arg) }))    })  }
//
 //selector和function(sel,filter,arg){}傳到process中,
//處理完後,運行function(sel,filter,arg){},其中sel是selector經過強大正則之後的第二塊,filter是filters中對於的函數,arg是selector中的參數
  zepto.matches = function(node, selector){    return process(selector, function(sel, filter, arg){      return (!sel || oldMatches(node, sel)) &&        (!filter || filter.call(node, null, arg) === node)    })  }})(Zepto)

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.