JQuery diary 5.11Sizzle selector (5)

Source: Internet
Author: User
Set the variables and methods corresponding to the current document and document. setDocumentSizzlesetDocumentfunction (node) {varhasCompare. If node is Element, if the documentnode to which node belongs is Document, return nodenode as null.
// Set the variables and methods for the current document and document
SetDocument = Sizzle. setDocument = function (node) {var hasCompare, // If node is Element, the system returns the document to which node belongs. // If node is Document, the system returns the node when node is empty? Node. ownerDocument | node: preferredDoc, // windowparent = doc to which the document belongs. defaultView; // If no document and documentElement is available, returnif (doc = document | doc. nodeType! = 9 |! Doc.doc umentElement) {return document;} // Set the global document to the current docdocument = doc; docElem = doc.doc umentElement; // Support testsdocumentIsHTML =! IsXML (doc); // Support: IE> 8 // If iframe document is assigned to "document" variable and if iframe has been reloaded, // IE will throw "permission denied" error when accessing "document" variable, see jQuery #13936 // IE6-8 do not support the defaultView property so parent will be undefined/** if the iframe document has been specified and if iframe is overloaded * When the document variable is accessed, IE will throw "permission denied" Error * The IE6-8 does not support defaultView so pa The rent variable is undefined * // so you need to re-setDocument () when unload. // This judgment indicates that the current code is in the frame if (parent & parent! = Parent. top) {// IE11 does not have attachEvent, so all must suffer // reset entif (parent. addEventListener) {parent. addEventListener ("unload", function () {// not passed into your documentemediaworkflow Doc umentsetDocument () ;}, false) ;}else if (parent. attachEvent) {parent. attachEvent ("onunload", function () {setDocument () ;}}// some capability checks are added to support/* Attributes -------------------------------------------- -------------------------- * // Support: IE <8 // Verify that getAttribute really returns attributes and not properties (excluding IE8 booleans) // IE8 div. getAttribute ("className") will get classsupport. attributes = assert (function (div) {div. className = "I"; return! Div. getAttribute ("className") ;});/* getElement (s) By * alias * // Check if getElementsByTagName ("*") returns only elements // check whether getElementsByTagName only returns Element support. getElementsByTagName = assert (function (div) {div. appendChild (doc. createComment (""); return! Div. getElementsByTagName ("*"). length;}); // Check if getElementsByClassName can be trusted // Check whether getElementsByClassName can correctly handle multiple classes. getElementsByClassName = rnative. test (doc. getElementsByClassName) & assert (function (div) {div. innerHTML = ""; // Support: Safari <4 // Catch class over-cachingdiv.firstChild.className = "I"; // Support: Opera <10 // Catch gEBCN failure to find non-leading classes Return div. getElementsByClassName ("I "). length = 2;}); // Support: IE <10 // Check if getElementById returns elements by name // The broken getElementById methods don't pick up programatically-set names, // so use a roundabout getElementsByName test // check whether getElementById returns the support element based on the name. getById = assert (function (div) {docElem. appendChild (div ). id = expando; return! Doc. getElementsByName |! Doc. getElementsByName (expando ). length ;}); // ID find and filterif (support. getById) {Expr. find ["ID"] = function (id, context) {if (typeof context. getElementById! ==Strundefined & documentIsHTML) {var m = context. getElementById (id); // Check parentNode to catch when Blackberry 4.6 returns // nodes that are no longer in the document #6963 return m & m. parentNode? [M]: [] ;}}; Expr. filter ["ID"] = function (id) {// convert the Escape Character back to the string var attrId = id. replace (runescape, funescape); return function (elem) {return elem. getAttribute ("id") === attrId ;};};} else {// Support: IE6/7 // getElementById is not reliable as a find your cut // IE6, because the getElementById of 7 returns the element // Based on the name, the native getElementById method cannot be used to obtain the element //. Therefore, delete Expr. find ["ID"]; Expr. filter ["ID"] = function (id) {var attrId = Id. replace (runescape, funescape); return function (elem) {var node = typeof elem. getAttributeNode! = Strundefined & elem. getAttributeNode ("id"); return node & node. value === attrId ;};}/// Tag // getElementsByTagName whether only the Element Expr is returned. find ["TAG"] = support. getElementsByTagName? Function (tag, context) {if (typeof context. getElementsByTagName! = Strundefined) {return context. getElementsByTagName (tag) ;}: function (tag, context) {var elem, tmp = [], I = 0, results = context. getElementsByTagName (tag); // Filter out possible commentsif (tag = "*") {while (elem = results [I ++]) {if (elem. nodeType = 1) {tmp. push (elem) ;}} return tmp;} return results ;}; // ClassExpr. find ["CLASS"] = support. getElementsByClassName & function (classN Ame, context) {if (typeof context. getElementsByClassName! = Strundefined & documentIsHTML) {return context. getElementsByClassName (className) ;};/* QSA/matchesSelector extends * // QSA and matchesSelector support // matchesSelector (: active) reports false when true (IE9/Opera 11.5) rbuggyMatches = []; // qSa (: focus) reports false when true (Chrome 21) // We allow this because of a bug in IE8/9 that throws an error // whenever 'document. activeElement 'is accessed on an iframe // So, we allow: focus to pass through QSA all the time to avoid the IE error // See http://bugs.jquery.com/ticket/13378rbuggyQSA = []; If (support. qsa = rnative. test (doc. querySelectorAll) {// Build QSA regex // Regex strategy adopted from Diego Periniassert (function (div) {// Select is set to empty string on purpose // This is to test IE's treatment of not explicitly // setting a boolean content attribute, // since its presence shocould be enough // http://bugs.jquery.com/ticket/12359div.innerHTML =""; // Support: IE8, Opera 10-12 // Nothing shocould be selected when empty strings follow ^ = or $ = or * = if (div. querySelectorAll ("[t ^ =''] "). length) {rbuggyQSA. push ("[* ^ $] =" + whitespace + "*(?: ''| \" \ ")");} // Support: IE8 // Boolean attributes and "value" are not treated correctlyif (! Div. querySelectorAll ("[selected]"). length) {rbuggyQSA. push ("\ [" + whitespace + "*(?: Value | "+ booleans +") ");} // Webkit/Opera-: checked shoshould return selected option elements // http://www.w3.org/TR/2011/REC-css3-selectors-20110929/#checked// IE8 throws error here and will not see later testsif (! Div. querySelectorAll (": checked "). length) {rbuggyQSA. push (": checked") ;}}); assert (function (div) {// Support: Windows 8 Native Apps // The type and name attributes are restricted. innerHTML assignmentvar input = doc. createElement ("input"); input. setAttribute ("type", "hidden"); div. appendChild (input ). setAttribute ("name", "D"); // Support: IE8 // Enforce case-sensitivity of name attributeif (Div. querySelectorAll ("[name = d]"). length) {rbuggyQSA. push ("name" + whitespace + "* [* ^ $ |! ~]? = ");} // FF 3.5-: enabled/: disabled and hidden elements (hidden elements are still enabled) // IE8 throws error here and will not see later testsif (! Div. querySelectorAll (": enabled "). length) {rbuggyQSA. push (": enabled", ": disabled");} // Opera 10-11 does not throw on post-comma invalid pseudo dosdiv. querySelectorAll ("*,: x"); rbuggyQSA. push (",. *: ") ;}) ;}if (support. matchesSelector = rnative. test (matches = docElem. webkitMatchesSelector | docElem. extends matchesselector | docElem. oMatchesSelector | docElem. msMatchesSelector) {assert (function (div) {// Check to see if it's possible to do matchesSelector // on a disconnected node (IE 9) support. disconnectedMatch = matches. call (div, "div"); // This shoshould fail with an exception // Gecko does not error, returns false insteadmatches. call (div, "[s! = '']: X"); rbuggyMatches. push ("! = ", Pseudo DoS) ;}) ;}rbuggyqsa = rbuggyQSA. length & new RegExp (rbuggyQSA. join ("|"); rbuggyMatches = rbuggyMatches. length & new RegExp (rbuggyMatches. join ("|");/* Contains ---------------------------------------------------------------------- */hasCompare = rnative. test (docElem. compareDocumentPosition); // Element contains another // Purposefully does not implement passive descendent // As in, Element does not contain itself // when the node contains itself, returns truecontains = hasCompare | rnative. test (docElem. contains )? Function (a, B) {// a is Document, and adown is HtmlElement. Others are avar adown = a. nodeType = 9? A.doc umentElement: a, // bup is the parent node of Node B. bup = B & B. parentNode; // (1) If a is the parent node of B, return true quickly. // (2) if B has a parent node and its parent node is Element // a contains B, the returned result is truereturn a === bup | !! (Bup & bup. nodeType = 1 & (adown. contains? // Contains is bup, But B is returned to avoid trueadown when contains are themselves. contains (bup): // Why & 16, please refer to the return value of compareDocumentPosition/* 000000 0 element consistent 000001 1 node in different documents (or one out of the document) 000010 2 node B before node A 000100 4 node A before Node B 001000 8 Node B contains node A010000 16 node A contains node B100000 32 browser private use */. compareDocumentPosition &. compareDocumentPosition (bup) & 16);}: // when no native method is available, iteration judge function (a, B) {if (B) {// an ancestor node of B ==== a indicates that a contains bwhile (B = B. p ArentNode) {if (B = a) {return true ;}} return false ;};/* Sorting condition * // Document order sortingsortOrder = hasCompare? Function (a, B) {// Flag for duplicate removalif (a = B) {hasDuplicate = true; return 0 ;} // Sort on method existence if only one input has compareDocumentPosition // if only one element has the sorting method. // node a has a sorting method to return-1, and Node B has a sorting method to return 1var compare =! A. compareDocumentPosition -! B. compareDocumentPosition; if (compare) {return compare;} // Calculate position if both inputs belong to the same document // compare = (. ownerDocument | a) === (B. ownerDocument | B )? A. compareDocumentPosition (B): // Otherwise we know they are disconnected1; // Disconnected nodes // If the node is no longer the same document or does not support separation and sorting and, B is no longer the same document if (compare & 1 | (! Support. sortDetached & B. compareDocumentPosition (a) === compare )) {// Choose the first element that is related to our preferred document // If node a is included in the current document, return-1if (a = doc |. ownerDocument === preferredDoc & contains (preferredDoc, a) {return-1;} // If node B is included in the current doucment, return 1if (B === doc | B. ownerDocument === preferredDoc & contains (preferredDoc, B) {return 1;} // Maintain original order // Keep original order return sortInput? (IndexOf. call (sortInput, a)-indexOf. call (sortInput, B): 0;} // compare // a returns-1 before B; otherwise, 1 return compare & 4? -1: 1;}: function (a, B) {// Exit early if the nodes are identicalif (a = B) {hasDuplicate = true; return 0 ;} var cur, I = 0, aup =. parentNode, bup = B. parentNode, ap = [a], bp = [B]; // Parentless nodes are either documents or disconnected // if (! Aup |! Bup) {// if a is a document, it means a first returns-1 // If B is a document, B first returns 1 // otherwise, if a has a parent node, B is disconnected and-1/is returned/ /otherwise, if B has a parent node and a is disconnected, return 1 // If a and B are both disconnected, use the indexOf method return a = doc? -1: B = doc? 1: aup? -1: bup? 1: sortInput? (IndexOf. call (sortInput, a)-indexOf. call (sortInput, B): 0; // If the nodes are siblings, we can do a quick check // the father is a, using siblingCheck (a, B) method Quick check} else if (aup === bup) {return siblingCheck (a, B );} // Otherwise we need full lists of their ancestors for comparison // Otherwise, traverse the entire ancestor cur = a; while (cur = cur. parentNode) {// Add all the ancestors of a to the stack ap. unshift (cur);} cur = B; while (cur = cur. parentNode )) {// Put all the ancestors of B into the stack bp. unshift (cur);} // Walk down the tree looking for a discrepancy // If the ancestor of the same layer is a while (ap [I] === bp [I]) {I ++;} return I? // Do a sibling check if the nodes have a common ancestor // compare the position of siblingCheck (ap [I], bp [I]): // Otherwise nodes in our document sort firstap [I] === preferredDoc? -1: bp [I] === preferredDoc? 1: 0;}; // return documentreturn doc ;};

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.