jQuery selector source code interpretation (7) : elementMatcher function

  • 2020-05-19 04:17:23
  • OfStack

To understand the Compile execution process of Sizzle, we need to understand the functions, key variables and functions of each subprogram involved. I will explain the Compile code of jQuery-1.10.2 one by one. I hope you can help me.

elementMatcher(matchers)

1, the source


function elementMatcher(matchers) {
 return matchers.length > 1 ? function(elem, context, xml) {
  var i = matchers.length;
  while (i--) {
   if (!matchers[i](elem, context, xml)) {
    return false;
   }
  }
  return true;
 } : matchers[0];
}

2, functionality,

This function returns 1 function to determine whether the incoming elem matches the array matchers of execution functions. If it does not, false is returned, otherwise, true is returned.

If matchers has only one element, it returns the element itself, otherwise it returns a new function -- the function(elem, context, xml) function in the code.

The return function has a somewhat similar effect to context.filter (selectors), although, of course, the result only returns true or false, not jQuery objects.

3, parameters,
matchers - an array in which each element is the matcher execution function of a nonpseudo class. For example, in the actual execution process, div.map span:lt(10), where the matching execution functions of div and map are passed into the elementMatcher function as two elements of matchers to filter whether the parent node of span node meets the requirements.

4. Return function

4.1 if matchers has more than one element, the following function is returned:


function(elem, context, xml) {
 var i = matchers.length;
 while (i--) {
  if (!matchers[i](elem, context, xml)) {
   return false;
  }
 }
 return true;
}

4.4.1 function
From the last element of matchers to the first element, it is called successively to match whether the incoming elem node meets the requirements. All of them meet the requirements to return true; otherwise, false is returned.

4.1.2 parameter

elem -- single node element to examine.

context -- the context node that performs the entire selector string matching, most of the time for no purpose.

xml -- the current search object is an HTML or XML document; if it is HTML, the xml parameter is false.

4.2 if matchers has only one element, it returns the element itself.
2 function
Check whether the incoming elem matches the selector, and if so, return true; otherwise, return false.

4.2.2 parameters
Same parameter as 4.1.2.


Related articles: