The Sizzle (selector, context, results, seed) function is used to find a set of elements that match the selector expression. This function is the entry of the selector engine.
The six key steps for executing the function Sizzle are as follows:
1. parse the selector expression to parse the block expression and link character.
2. If a location pseudo class exists, search from left to right:
A. Search for the element set matched by the first block expression to obtain the first context element set.
B. traverse the remaining block expressions and the relationship between blocks to narrow down the context element set.
3. Otherwise, search from right to left:
A. Search for the element set matched by the last block expression to obtain the candidate set and ing set.
B. traverse the remaining block expression and the inter-block link operator to filter the inter-block RELATIONSHIP OF THE ing set.
4. Filter candidate sets based on the ing set and put final matching elements into the result set.
5. If a parallel selector expression exists, recursive Sizzle () is used to find the matching Element Set, merge, sort, and deduplicate.
6. The final returned result set.
3967 defined sizzle.
Selector: css selector expression.
Context: context.
Results: an optional array or a class array. sizzle adds the searched element to it.
Seed: an optional element set. The sizzle function filters out the Element Set matching the selector expression from this element set.
Row 3968: if the results parameter is not input, the default value is an empty array [].
Row 3969: If context is not input, the current document object is used by default.
Row 3: Backup context. if the selector parameter starts with # id, the context may be changed to the element matching # id. the backup origContext is used in the case of a parallel selector expression.
Row 3973: if the context parameter is neither an element nor a document Object, [] is returned.
Row 3977: If the selector parameter is a null string or is not a string, results is returned directly.
M: stores the result of the regular chunker matching selector expression each time.
Set: In the right-to-left search method, the variable set becomes a "Candidate set", and the last block expression matches the element set, other block expressions and inter-block Relational operators filter the candidate set through rows. For the search method from left to right, the variable set is the set of elements matching the current block expression, it is also the context of the next block expression.
CheckSet: for right-to-left searches, checkSet is called a "ing set", and its initial value is a copy of the candidate set, other block expressions and inter-block Relational operators filter the checkSet of the ing set. When filtering, replace the elements with the parent element, ancestor element, or sibling element based on the Inter-block relationship, then, replace the element that does not match the block expression with false, and filter the candidate set based on the checking set checkSet. For the right-to-left search method, in fact, the variable checkSet is not involved in the search process. At the end of the Sizzle () function, the variable checkSe and set are directed to the same Array in order to uniformly filter and merge matching elements.
Extra: used to store other parallel selector expressions following the first comma in a selector expression. if a parallel selector expression exists, the Sizzle () function is called recursively to find the matching Element Set and perform merge, sort, and deduplicate operations.
Ret: used only in the right-to-left execution mode. It is used to store the search result of the search selector Sizzle. find (expr, context, isXML) for the last block expression.
Pop: used only in the right-to-left search mode to indicate a single block expression.
Prune: used only in the right-to-left search mode, indicating whether the candidate set needs to be filtered. The default value is true, indicating that filtering is required. If there is only one block expression in the selector expression, the prune variable is false.
ContextXML: indicates whether the context is an XML document.
Parts: stores the block expressions extracted by the regular chunker from the selector expression and the relationship between blocks.
SoFar: used to save the remaining part of the regular chunker after extracting the block expression or inter-block link character from the selector expression each time. The initial value is the complete selector expression.
3989 ~ 3990: before the regular chunker matches the remaining part of the selector expression, it first matches an empty character to reset the starting matching position of the regular chunker, so that the regular chunker will start from the beginning during each matching. directly set "chunker. lastIndex = 0; "can achieve the same effect.
3992 ~ 4001: if the regular chunker can match the remaining part of the selector expression, assign the third group (that is, the remaining part after the current match) to the soFar variable, the next do-while loop continues matching. In this way, you can also filter out some junk characters (such as spaces). At the same time, insert the block expression or inter-block link character in the first group into the array parts. In addition, if the second group is not an empty string, that is, if a comma is encountered, it indicates a parallel selector expression, save the third group in the variable extra and end the loop.