Sizzle. selectors. relative source code analysis !, Selectorsapi

Source: Internet
Author: User

Sizzle. selectors. relative source code analysis !, Selectorsapi

1. The jQuery object Sizzle. selectors. relative stores the inter-block relationship character and the corresponding inter-block relationship filtering function, which is called the "Inter-block relationship filtering function set"

There are four types of Inter-block relationship characters, including their meanings and filtering methods.

 

When Sizzle (selector, context, results, seed) is filtered from right to left, the inter-block relationship filtering function is
Check whether the elements in the checkSet of the ing set match the block expression on the left of the inter-block link character. Parameters for calling
Number Format:

Sizzle. selectors. relative [Inter-block relational character cur] (ing set checkSet, left-side block expression pop, contextXML );

The Inter-block relationship filtering function accepts three parameters:
CheckSet: ing set. filter the element set.
‰ Parameter part: In most cases, it is the block expression on the left side of the inter-block link operator. This parameter can also be a DOM element.
IsXML: A boolean value indicating whether to run in an XML document.
The following are three key steps to implement the Inter-block relationship filtering function:
1) traverse the checkSet of the ing set.
2) Find the sibling, parent, or ancestor element of each element based on the Inter-block relationship.
3) check whether the element found matches the parameter part and replace the element at the corresponding position in the ing set checkSet.
A. If the part parameter is a tag, check whether the node name nodeName of the element is equal to the tag,
If they are equal, replace them with the found elements. If they are not equal, replace them with false.
B. If the part of the parameter is a DOM element, check whether the element found is equal to it. If the part is equal, replace
Is true. If not equal, it is replaced with false.
C. If the part parameter is a non-label string, the Sizzle. filter (selector, set, inplace, not) method is called to filter.
That is to say, after the traversal, the elements in the checkSet of the ing set may be sibling elements, parent elements,
Ancestor element, true or false.


One inter-relationship character "+" matching selector "prev + next", that is, matching all sibling elements following the element prev
Next. For example, $ ("div + span") and $ (". lastdiv + span "). For the right-to-left search method, check the Meta
Whether the sibling element before sunext matches the block expression prev.

Var Expr = Sizzle. selectors = {relative: {"+": function (checkSet, part) {// check whether the parameter is a string var isPartStr = typeof part = "string ", // indicates whether the part is a tag string isTag = isPartStr &&! RNonWord. test (part), // isPartStrNotTag: indicates whether the part parameter is a non-tag string. IsPartStrNotTag = isPartStr &&! IsTag; if (isTag) {part = part. toLowerCase ();}/* traverse the ing set checkSet to find the first sibling element of each element and replace the elements at the corresponding position in the checking set checkSet. There are three logical branches: 1. If no sibling element is found, replace it with false. 2. If the sibling element is found and the parameter part is a tag, check whether the node name nodeName of the sibling element is the same. If it is equal, replace it with the sibling element, if they are not equal, replace them with false. 3. If the sibling element is found and the part parameter is a DOM element, check whether the two are equal. If they are equal, replace them with true. If they are not equal, replace them with false. Therefore, after the traversal, the elements in the checkSet of the ing set may be sibling, true, or false. */For (var I = 0, l = checkSet. length, elem; I <l; I ++) {if (elem = checkSet [I]) {/* filter out non-element nodes while traversing sibling elements, and exit the while loop as long as a sibling element is obtained. */While (elem = elem. previussibling) & elem. nodeType! = 1) {} checkSet [I] = isPartStrNotTag | elem & elem. nodeName. toLowerCase () = part? Elem | false: elem = part;}/* If the part parameter is a non-label string, the method Sizzle is called. filter (selector, set, inplace, not) filters ing set checkSet. When the parameter part is a tag and a DOM element, it has already been processed during the previous traversal of the checking set checkSet. */If (isPartStrNotTag) {Sizzle. filter (part, checkSet, true );}},},};

The two Relational operators ">" used for selector "parent> child" match the child element under the parent element parent.
For example, $ ("div + span") and $ (". lastdiv + span "). For the right-to-left search method, the child element is checked.
Whether the child parent element matches the block expression parent.


Var Expr = Sizzle. selectors = {relative: {">": function (checkSet, part) {var elem, isPartStr = typeof part = "string", I = 0, l = checkSet. length;/* If the parameter part is a tag, traverse the ing set checkSet to find the parent element of each element and check whether the node name nodeName of the parent element is equal to the parameter part, if they are equal, replace the element at the corresponding position in the checkSet of the ing set with the parent element. If they are not equal, replace the element with false. */If (isPartStr &&! RNonWord. test (part) {part = part. toLowerCase (); for (; I <l; I ++) {elem = checkSet [I]; if (elem) {var parent = elem. parentNode; checkSet [I] = parent. nodeName. toLowerCase () = part? Parent: false ;}} else {/* If the part parameter is not a tag, it may be a non-tag string or DOM element. It also traverses the ing set checkSet to find the parent element of each element, replace the elements at the corresponding position in the checkSet of the ing set. In this process, there are two logical branches: 1. If the part parameter is a non-tagstring, during the process of traversing the checkSet of the ing set, replace the element at the corresponding position in the checkSet of the ing set as the parent element. After the traversal ends, call the Sizzle method. filter (selector, set, inplace, not) filters ing set checkSet. 2. If the parameter part is an element, when traversing the checkSet of the ing set, check whether the parent element of each element is equal to it, if the values are equal, the elements in the corresponding position in the checkSet of the ing set are replaced with true, and those in the non-equal values are replaced with false. Therefore, after the traversal, the elements in the checkSet of the ing set may be the parent element, true, or false. */For (; I <l; I ++) {elem = checkSet [I]; if (elem) {checkSet [I] = isPartStr? Elem. parentNode: elem. parentNode = part ;}} if (isPartStr) {Sizzle. filter (part, checkSet, true );}}},},};


The three Relational operators "" used to select "ancestor descendant", that is, match all descendants of the ancestor.
The descendant element. For example, $ ("div button") and $ ("div. btn "). For the right-to-left search method
Check whether the ancestor element of the descendant element descendant matches the block expression ancestor.
Var Expr = Sizzle. selectors = {relative: {"": function (checkSet, part, isXML) {var nodeCheck, doneName = done ++, checkFn = dirCheck; /* 1 if the part parameter is a non-tag string or DOM element, call the dirCheck () function to filter the checkSet of the ing set. 2. If the part parameter is a tag, call the dirNodeCheck () function to filter the checkSet of the ing set. When the dirCheck () and dirNodeCheck () functions are called, the parameter format is: checkFn (Direction "parentNode/previussibling", block expression part, cache counter doneName, ing set checkSet, nodeCheck, isXML) the dirCheck () and dirNodeCheck () functions traverse the checkSet of the ing set to find the ancestor elements of each element and check whether the ancestor element matches the parameter part, replace the elements at the corresponding position in the checkSet of the ing set. */If (typeof part === "string "&&! RNonWord. test (part) {part = part. toLowerCase (); nodeCheck = part; checkFn = dirNodeCheck;} checkFn ("parentNode", part, doneName, checkSet, nodeCheck, isXML );},},};


Four Relational operators "~ "Used for selector" prev ~ Siblings ", that is, all brothers that match the element prev
Susiblings. For example, $ ('div ~ P '). For the search method from the right to the left, it is to check
Whether the sibling element matches the block expression prev.
Sizzle. selectors. relative ["~ "] (CheckSet, part) source code implementation and Sizzle. selectors. relative [" "]
(CheckSet, part) is almost the same. The difference between the two is that when the dirCheck () and dirNodeCheck () functions are called
The value of a parameter is different. The former is "previussibling", and the latter is "parentNode ".
The related code is as follows:

var Expr = Sizzle.selectors = {relative: {"~": function( checkSet, part, isXML ) {var nodeCheck,doneName = done++,checkFn = dirCheck;if ( typeof part === "string" && !rNonWord.test( part ) ) {part = part.toLowerCase();nodeCheck = part;checkFn = dirNodeCheck;}checkFn( "previousSibling", part, doneName, checkSet, nodeCheck, isXML );}},};



How can I analyze the source code of android?

If you have a high level, you can start from which module, but if you are still at a general level and not very familiar with the code, I personally suggest you buy a related book so that it is better to take the book with you for analysis!

Is there a source code analysis tool in linux?

There are many code analysis tools. We recommend that you use the source insight software. It supports multiple languages, code redirection, structure charts, and other powerful functions.

Related Article

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.