jquery Selector code in detail (iv)--expr.prefilter

Source: Internet
Author: User

Original articles, reproduced please specify the source, thank you!

Expr.prefilter is a method for preprocessing attr, child, pseudo three selectors in the Tokenize method. Specific as follows:

Expr.prefilter: {"ATTR": function (Match) {/* * * Complete the following tasks: * 1, property name decoding * 2, attribute value decoding * 3, if the judgment is ~=, then add a space * 4 to the property value, return the final Mtach object *     * Match[1] denotes the property name, * Match[1].replace (RuneScape, Funescape): Decodes the hexadecimal number in the property name to * Single-byte Unicode characters or double-byte Unicode characters (Chinese or other text that requires two-byte expression) * Detailed descriptions of regular expressions can be see my "detailed jquery selector regular expression" article */match[1] = Match[1].replace (  RuneScape, funescape);/* Decode attribute value * Match[4]: denotes attribute value placed in single or double quotation marks * Match[5]: denotes attribute values not enclosed in quotation marks */match[3] = (Match[4] | | | match[5] || ""). Replace (runescape,funescape);/* ~= means word match, the definition of words in the "word" is blank for different words of the delimiter * so in match[3] on both sides with a space, you can use IndexOf, Correctly identify if the word exists */if (match[2] = = = "~=") {match[3] = "+ match[3] +" ";} /* * Returns useful first four element results */return match.slice (0, 4);}, "Child": function (Match) {/* * * * Complete several tasks: * 1, change the character before child and Of-type in the command to lowercase characters * 2, for the nth beginning of the selector check the data validity in parentheses * 3, match[4] and match[5] respectively stored in xn+b x and b,x and b allowed is negative * 4, return the final match object * * Match[1]: (only|first|last| Nth|nth-last) */match[1] = Match[1].tolowercase ();/* for Nth-child, Nth-of-type, Nth-last-child, Nth-last-of-type four types of brackets domestic demand set valid data * while others are enclosedNo data is allowed in */if (Match[1].slice (0, 3) = = = "Nth") {/* * If there are no valid parameters in the selector brackets, an exception is thrown * Example: if the selector is nth or nth (ABC) is an illegal selector */if (!match[3]) {Sizzle.error (match[0]);} /* * the following nth-child () as an example to explain the syntax, in order to better understand the role of the following code * Nth-child allowed to use the following: *: Nth-child (even) *: Nth-child (ODD) *: Nth-chil D (3n) *: Nth-child (+2n+1) *: Nth-child (2n-1) * In the code below the assignment number to the left of the match[4], match[5] is used to record in parentheses, respectively, before and after n values, including the sign * for: Nth-child (even ) and: Nth-child (Odd), match[4] is empty, * so return 2 * (match[3] = = = "even" | | match[3] = = = "odd") calculation result * because in JS true=1,false=0 , so (match[3] = = = "even" | | match[3] = = = "Odd") equals 1 * Therefore, 2 * (match[3] = = = "even" | | match[3] = = = "Odd") evaluates to 2 * * equals right  The function of the side "+" is to force the type conversion, convert the string after to the numeric type */match[4] = + (Match[4]? Match[5] + (Match[6] | | 1): 2 * (match[3] = = = "even" | | match[3] = = = "Odd")); match[5] = + ((match[7] + match[8]) | | match[3] = = = "Odd");} else if (Match[3]) {/* * If the other child type selector is not nth starts with parentheses, the exception is thrown * Here jquery is not strictly determined by the rules of the, because it allows: this form of first-child () exists * That is, for jquery: First-child () is equivalent to: First-child, which is the legal selector */sizZle.error (Match[0]);}    return match;}, "PSEUDO": function (Match) {/* * * Complete the following tasks: * 1, get the value enclosed in the pseudo-class * 2, for non-quoted values, if there is a pseudo-class nesting, then further resolve to determine the current pseudo-class actual end position, * Gets the full string and value of the current pseudo-class * 3, returning a copy of the first three items in the match. * * unquoted denotes non-quoted values in parentheses, * to: EQ (2) For example, unquoted=2 */var excess, unquoted =!match[5] && match[2];/* * because pseudo and CH The matching regular expression of ILD has a intersection, so the part that belongs to child needs to be ignored */if (matchexpr["Child"].test (Match[0])) {return null;} /* * If the value in parentheses is enclosed in quotation marks (match[3]), * The value of the exception (Match[4]) is assigned to match[2]. * Match[3] denotes quotation marks. */if (Match[3] && match[4]!== undefined) {match[2] = match[4];} else if (unquoted/* * rpseudo.test (unquoted): Used to test unquoted contains pseudo-class, * if it contains pseudo-class, it is possible to have pseudo-class nesting possibility, need to further parse unquoted * For example:: Not (: eq (3)) */&& Rpseudo.test (unquoted) &&/* * Gets the position of the last character in the unquoted (excess = Tokenize (unquoted, True)) &&/* * Unquoted.indexof (")", unquoted.length-excess) * The location of the last character in a successively valid selector that was previously obtained, followed by the position of ")", which is usually after the current position. * Minus Unquoted.length, used to get the valid complete pseudo-class string last position in match[0], * Note that at this point the excess is a negative value */(Excess= Unquoted.indexof (")", unquoted.length-excess)-unquoted.length)) {//Get valid complete pseudo-class match[0] and pseudo-class data in parentheses match[2]match[0] = Match[0].slice (0, excess); match[2] = unquoted.slice (0, excess);} Returns a copy of the first three elements of Match return Match.slice (0, 3);}}

Dear friends, If you feel well written, help me to the top, give some impetus, thank you!


jquery Selector code detailed (a)--sizzle method

jquery Selector code in detail (ii)--select method

jquery Selector code in detail (iii)--tokenize method



jquery Selector code in detail (iv)--expr.prefilter

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.