selector-native.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. define([
  2. "./core"
  3. ], function( jQuery ) {
  4. /*
  5. * Optional (non-Sizzle) selector module for custom builds.
  6. *
  7. * Note that this DOES NOT SUPPORT many documented jQuery
  8. * features in exchange for its smaller size:
  9. *
  10. * Attribute not equal selector
  11. * Positional selectors (:first; :eq(n); :odd; etc.)
  12. * Type selectors (:input; :checkbox; :button; etc.)
  13. * State-based selectors (:animated; :visible; :hidden; etc.)
  14. * :has(selector)
  15. * :not(complex selector)
  16. * custom selectors via Sizzle extensions
  17. * Leading combinators (e.g., $collection.find("> *"))
  18. * Reliable functionality on XML fragments
  19. * Requiring all parts of a selector to match elements under context
  20. * (e.g., $div.find("div > *") now matches children of $div)
  21. * Matching against non-elements
  22. * Reliable sorting of disconnected nodes
  23. * querySelectorAll bug fixes (e.g., unreliable :focus on WebKit)
  24. *
  25. * If any of these are unacceptable tradeoffs, either use Sizzle or
  26. * customize this stub for the project's specific needs.
  27. */
  28. var docElem = window.document.documentElement,
  29. selector_hasDuplicate,
  30. matches = docElem.matches ||
  31. docElem.webkitMatchesSelector ||
  32. docElem.mozMatchesSelector ||
  33. docElem.oMatchesSelector ||
  34. docElem.msMatchesSelector,
  35. selector_sortOrder = function( a, b ) {
  36. // Flag for duplicate removal
  37. if ( a === b ) {
  38. selector_hasDuplicate = true;
  39. return 0;
  40. }
  41. var compare = b.compareDocumentPosition && a.compareDocumentPosition && a.compareDocumentPosition( b );
  42. if ( compare ) {
  43. // Disconnected nodes
  44. if ( compare & 1 ) {
  45. // Choose the first element that is related to our document
  46. if ( a === document || jQuery.contains(document, a) ) {
  47. return -1;
  48. }
  49. if ( b === document || jQuery.contains(document, b) ) {
  50. return 1;
  51. }
  52. // Maintain original order
  53. return 0;
  54. }
  55. return compare & 4 ? -1 : 1;
  56. }
  57. // Not directly comparable, sort on existence of method
  58. return a.compareDocumentPosition ? -1 : 1;
  59. };
  60. jQuery.extend({
  61. find: function( selector, context, results, seed ) {
  62. var elem, nodeType,
  63. i = 0;
  64. results = results || [];
  65. context = context || document;
  66. // Same basic safeguard as Sizzle
  67. if ( !selector || typeof selector !== "string" ) {
  68. return results;
  69. }
  70. // Early return if context is not an element or document
  71. if ( (nodeType = context.nodeType) !== 1 && nodeType !== 9 ) {
  72. return [];
  73. }
  74. if ( seed ) {
  75. while ( (elem = seed[i++]) ) {
  76. if ( jQuery.find.matchesSelector(elem, selector) ) {
  77. results.push( elem );
  78. }
  79. }
  80. } else {
  81. jQuery.merge( results, context.querySelectorAll(selector) );
  82. }
  83. return results;
  84. },
  85. unique: function( results ) {
  86. var elem,
  87. duplicates = [],
  88. i = 0,
  89. j = 0;
  90. selector_hasDuplicate = false;
  91. results.sort( selector_sortOrder );
  92. if ( selector_hasDuplicate ) {
  93. while ( (elem = results[i++]) ) {
  94. if ( elem === results[ i ] ) {
  95. j = duplicates.push( i );
  96. }
  97. }
  98. while ( j-- ) {
  99. results.splice( duplicates[ j ], 1 );
  100. }
  101. }
  102. return results;
  103. },
  104. text: function( elem ) {
  105. var node,
  106. ret = "",
  107. i = 0,
  108. nodeType = elem.nodeType;
  109. if ( !nodeType ) {
  110. // If no nodeType, this is expected to be an array
  111. while ( (node = elem[i++]) ) {
  112. // Do not traverse comment nodes
  113. ret += jQuery.text( node );
  114. }
  115. } else if ( nodeType === 1 || nodeType === 9 || nodeType === 11 ) {
  116. // Use textContent for elements
  117. return elem.textContent;
  118. } else if ( nodeType === 3 || nodeType === 4 ) {
  119. return elem.nodeValue;
  120. }
  121. // Do not include comment or processing instruction nodes
  122. return ret;
  123. },
  124. contains: function( a, b ) {
  125. var adown = a.nodeType === 9 ? a.documentElement : a,
  126. bup = b && b.parentNode;
  127. return a === bup || !!( bup && bup.nodeType === 1 && adown.contains(bup) );
  128. },
  129. isXMLDoc: function( elem ) {
  130. return (elem.ownerDocument || elem).documentElement.nodeName !== "HTML";
  131. },
  132. expr: {
  133. attrHandle: {},
  134. match: {
  135. bool: /^(?:checked|selected|async|autofocus|autoplay|controls|defer|disabled|hidden|ismap|loop|multiple|open|readonly|required|scoped)$/i,
  136. needsContext: /^[\x20\t\r\n\f]*[>+~]/
  137. }
  138. }
  139. });
  140. jQuery.extend( jQuery.find, {
  141. matches: function( expr, elements ) {
  142. return jQuery.find( expr, null, null, elements );
  143. },
  144. matchesSelector: function( elem, expr ) {
  145. return matches.call( elem, expr );
  146. },
  147. attr: function( elem, name ) {
  148. return elem.getAttribute( name );
  149. }
  150. });
  151. });