jquery Selector Source code interpretation (i): Sizzle method _jquery

Source: Internet
Author: User
Tags rtrim

The sizzle of jquery has been analyzed in depth (also refer to some online data), the results are shared. I will use the serialization method to explain some of the methods used by sizzle, each article introduces a method.

If you need to reprint, please specify the source, thank you.

* * Sizzle method is the main entrance to the Sizzle selector package, and the Find method of jquery is to call this method to get the matching node * This method mainly completes the following tasks: * 1, for a single selector, and is ID, Tag, class three types, then directly obtain and return the result of * 2, for browsers that support the Queryselectorall method, obtain and return a matching DOM element * 3 by executing the Queryselectorall method, and then call the Select method to fetch and return the matching DOM element, except the top (* * * @param SE The lector selector String * @param the context to perform the matching initial contexts (that is, the DOM element collection).
 If the context is not assigned a value, the document is taken. * @param results has been matched with a partial final result.
 An empty array is assigned if the results is not assigned a value.  * @param seed Initial set */function Sizzle (selector, context, results, seed) {var match, Elem, M, NodeType,//QSA I,

	Groups, old, nid, Newcontext, Newselector; * * Preferreddoc = window.document * * Setdocument method completes some initialization work */if (context? context.ownerdocument | | contex
	T:preferreddoc)!== document) {setdocument (context); Context = Context | |
	Document Results = Results | |

	[];
	* * If selector is not a valid string type data, it returns directly to Results/if (!selector | | typeof selector!== "string") {return results; }/* If the context is neither document (nodetype=9) nor element (nodetype=1), return an empty set/if (NodeType = COntext.nodetype)!== 1 && nodeType!== 9) {return []; //If the current filter is an HTML document and no seed is set, the statement body if (documentishtml &&!seed) {/* * If selector is a single selector and is ID, Tag, class three Kind Type, you get and return the result directly * * rquickexpr =/^ (?: # ([\w-]+) | ( \w+) |\. ([\w-]+)] $/* The above regular expression brackets three paragraphs in order to determine whether the ID, single selector for TAG, class type * The above regular expression has three subexpression (that is, three parentheses) in the outermost parentheses, * representing the ID, TAG, class selector, respectively.
			Values, in the following code, are embodied in match[1], match[2], match[3] */if (match = rquickexpr.exec (selector)) {//Speed-up:sizzle ("#ID") Handles an ID type selector, such as: #ID if ((M = match[1])) {//If the current context is a document, execute if (NodeType = = 9) {Elem = Co
					Ntext.getelementbyid (m);
					Check parentnode to catch when BlackBerrys 4.6//Returns//nodes that are no longer in the document #6963
						if (Elem && elem.parentnode) {//Handle the case where IE, Opera, and Webkit/return items By name instead of ID/* * Some older browsers will treat name as ID, * return incorrect results, so you need to compare the return node ID Property */if (elem.id = = m) {Results.push (elem);
						return results;
					} else {return results;
					 } else {//context isn't a document/* CONTAINS (context, elem) to confirm that the obtained elem is a child of the current context object */if (context.ownerdocument && (elem = Context.ownerDocument.getElementById (m)) && C
						Ontains (context, elem) && elem.id = = m) {Results.push (elem);
					return results; }//Speed-up:sizzle ("tag")//handling tag type selector, such as SPAN} else if (Match[2]) {push.apply (results, context
				. getElementsByTagName (selector));

				return results; Speed-up:sizzle (". Class ")/* Processing class type Selector, such as:. Class * The following conditions are judged: * m = match[3]: Valid class type Selector * support.getelements  
				 Byclassname this selector div support Getelementsbyclassname * Context.getelementsbyclassname Current context node has Getelementsbyclassname method * */Else if ((M = match[3]) && support.Getelementsbyclassname && context.getelementsbyclassname) {push.apply (results, Context.getelementsbyclas
				Sname (m));
			return results; }//QSA path/* * If the browser supports the Queryselectorall method and the selector conforms to the Queryselectorall call standard, execute the IF within the statement body * The check here is just a simple match * First call S Izzle, the RBUGGYQSA is an empty * * * If statement in the body of the current context object's ID assignment and recovery, is used to fix a bug in Queryselectorall * The bug will in some cases also return the current node (context) as a result
		 Back again.
		 * The specific method is to add a property selector to the existing selector: [Id=xxx], * XXX is the ID of the context, and a default value of expando if there is no set ID for the event itself.
			*/if (SUPPORT.QSA && (!RBUGGYQSA | |!rbuggyqsa.test (selector)) {nid = old = expando;
			Newcontext = context;

			If the context is document, then Newselector is taken from selector, or false newselector = NodeType = 9 && selector; 
			QSA works strangely on element-rooted queries//We can work around this by specifying a extra ID on the//root and working up from there ("Andrew Dupont for//" technique)//IE 8 doesn ' t work on object Eleme NTS if (nodEType = = 1 && context.nodeName.toLowerCase ()!== "Object") {groups = Tokenize (selector); if ((old = Context.getattribute ("id")) {/* Rescape =/' |\\/g, * here is the single quotation mark, the vertical bar, the backslash, plus a backslash * old
				The $& in the "Replace" (Rescape, "\\$&") code represents the match/nid = Old.replace (Rescape, "\\$&");
				else {context.setattribute ("id", nid);

				} nid = "[id= '" + nid + "']";
				Re-assemble the new selector i = Groups.length;
				while (i--) {Groups[i] = nid + Toselector (groups[i)); } * * rsibling = new RegExp (whitespace + "*[+~]") * rsibling is used to determine if the selector exists a sibling * If the +~ symbol is included, then the parent of the context is taken as Current node */newcontext = rsibling.test (selector) && Context.parentnode | |
				Context
			Newselector = Groups.join (","); } if (Newselector) {/* * * This is where try...catch is needed, * because some of the selectors supported by jquery are not supported by Queryselectorall, * when using these selected
				 When the selector, Queryselectorall will report the illegal selector, * so need jquery itself to achieve. */try {//Will QueryselThe results obtained by Ectorall are merged into results and then returned to Resulsts push.apply (results, Newcontext. Queryselectorall (Newselector));
				return results;
					The catch (Qsaerror) {} finally {if (!old) {Context.removeattribute ("id"); //all others////Except for the above shortcuts and the Queryselectorall way to get results directly, the rest calls the select to obtain the result/* RTrim = new RegExp (" ^ "+ whitespace +" +| ((?:^| [^\\\\]) (?:\ \\\.)
	 *) "* + whitespace +" +$ "," G "), * whitespace =" [\\x20\\t\\r\\n\\f] ";  * The function of the above RTrim regular expression is to remove the selector on both sides, and the white space character is defined by the whitespace variable RTrim effect with the new RegExp ("^" + whitespace + "+|" + whitespace + "+$"),
"G") similar to/return select (Selector.replace (RTrim, "$"), context, results, seed); }

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

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.