Zepto-selector.js Simple Analysis

Source: Internet
Author: User

The Zepto selector module is a partial implementation of the jquery expansion selector (jquery-selector-extensions). For example, the choice method: $(‘div:first‘) andel.is(‘:visible‘)。

Here is the original code, simply write some comments ~

;(function($){  varZepto = $.zepto, OLDQSA = ZEPTO.QSA, oldmatches =zepto.matches/** Examine whether an element is visible. In addition to judging if display is none, it also determines if width and height are 0, and double exclamation marks are coerced into a Boolean type*/  functionvisible (elem) {Elem=$ (elem)return!! (Elem.width () | | elem.height ()) && elem.css ("display")!== "None"  }  //implemented as part of the Jquey selector extension  //http://api.jquery.com/category/selectors/jquery-selector-extensions/////each parameter of the filter function can accept the current value, all the nodes in the range and the values in parentheses  //This is the node that is currently being considered. The function returns node (s), null or undefined  //  //complex selectors are not supported, such as the following:  //Li:has (Label:contains ("foo")) + Li:has (label:contains ("Bar" ))  //Ul.inner:first > Li  varfilters = $.expr[': '] ={visible:function(){if(Visible ( This))return  This},//VisibleHiddenfunction(){if(!visible ( This))return  This},//not visibleSelectedfunction(){if( This. Selected)return  This},//selectedCheckedfunction(){if( This. Checked)return  This},//tick CheckParentfunction(){return  This. parentnode},//parent NodeFirstfunction(IDX) {if(idx = = = 0)return  This},//first elementLastfunction(IDX, nodes) {if(idx = = = nodes.length-1)return  This},//last elementeqfunction(idx, _, value) {if(idx = = = value)return  This},//the same elementContainsfunction(idx, _, text) {if($( This). Text (). IndexOf (text) >-1)return  This},//elements contained in contentHas:function(idx, _, SEL) {if(ZEPTO.QSA ( This, sel). Length)return  This}//  }  varFilterre =NewRegExp (' (. *):(\\w+) (?: \ \ (([^)]+) \)] $\\s* '),//a powerful regular expression used to break down selectors, see below childre=/^\s*>/, Classtag= ' Zepto ' + (+NewDate ())functionProcess (SEL, FN) {//The decomposition selector is three parts, the first part is the selector itself, the second part is the value of the selector, the function name in the filter, and the third part is the parameter    ///For example: (1)filterre.exec (": eq (2)")    //Results obtained:[": Eq (2)", "", "eq", "2"]    //(2)filterre.exec (": Visible")    //Results obtained:[": Visible", "", "visible", undefined]    //quote the hash in ' a[href^=#] ' expressionsel = Sel.replace (/=#\]/g, ' = "#"] ')    varFilter, arg, match =filterre.exec (SEL)if(Match && Match[2]inchfilters) {Filter= Filters[match[2]], arg = match[3]//filter is the corresponding function sel in the filters = Match[1]      if(ARG) {varnum =Number (ARG)if(IsNaN (num)) arg = Arg.replace (/^["']|[" ') $/g, ")        Elsearg =num}} returnfn (sel, filter, ARG)} ZEPTO.QSA=function(node, selector) {returnProcess (Selector,function(sel, filter, arg) {Try {        vartaggedparentif(!sel && filter) sel = ' * 'Else if(Childre.test (SEL))//Support for "> *" Child queries by tagging the parent node with a          //Unique class and prepending that classname onto the selectorTaggedparent = $ (node). addclass (Classtag), sel = '. ' +classtag+ ' +selvarnodes =OLDQSA (node, sel)}Catch(e) {console.error (' Error performing selector:%o ', selector)Throwe}finally {        if(taggedparent) Taggedparent.removeclass (Classtag)}return!filter?Nodes:zepto.uniq ($.map (nodes,function(N, i) {returnFilter.call (n, I, Nodes, Arg)})})}
//
Selector and function (Sel,filter,arg) {} are passed to the process,
After processing, run function (Sel,filter,arg) {}, where Sel is the second block after strong regular selector, filter is the function in filters, ARG is the parameter in Selector
  function (node, selector) {    returnfunction(SEL, filter, arg) {      return (!sel | | oldmatches (node, sel) &&        (null, arg) = = = node)}    )  } }) (Zepto)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.