selector-native.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.webkitMatchesSelector ||
  31. docElem.mozMatchesSelector ||
  32. docElem.oMatchesSelector ||
  33. docElem.msMatchesSelector,
  34. selector_sortOrder = function( a, b ) {
  35. // Flag for duplicate removal
  36. if ( a === b ) {
  37. selector_hasDuplicate = true;
  38. return 0;
  39. }
  40. var compare = b.compareDocumentPosition && a.compareDocumentPosition && a.compareDocumentPosition( b );
  41. if ( compare ) {
  42. // Disconnected nodes
  43. if ( compare & 1 ) {
  44. // Choose the first element that is related to our document
  45. if ( a === document || jQuery.contains(document, a) ) {
  46. return -1;
  47. }
  48. if ( b === document || jQuery.contains(document, b) ) {
  49. return 1;
  50. }
  51. // Maintain original order
  52. return 0;
  53. }
  54. return compare & 4 ? -1 : 1;
  55. }
  56. // Not directly comparable, sort on existence of method
  57. return a.compareDocumentPosition ? -1 : 1;
  58. };
  59. jQuery.extend({
  60. find: function( selector, context, results, seed ) {
  61. var elem, nodeType,
  62. i = 0;
  63. results = results || [];
  64. context = context || document;
  65. // Same basic safeguard as Sizzle
  66. if ( !selector || typeof selector !== "string" ) {
  67. return results;
  68. }
  69. // Early return if context is not an element or document
  70. if ( (nodeType = context.nodeType) !== 1 && nodeType !== 9 ) {
  71. return [];
  72. }
  73. if ( seed ) {
  74. while ( (elem = seed[i++]) ) {
  75. if ( jQuery.find.matchesSelector(elem, selector) ) {
  76. results.push( elem );
  77. }
  78. }
  79. } else {
  80. jQuery.merge( results, context.querySelectorAll(selector) );
  81. }
  82. return results;
  83. },
  84. unique: function( results ) {
  85. var elem,
  86. duplicates = [],
  87. i = 0,
  88. j = 0;
  89. selector_hasDuplicate = false;
  90. results.sort( selector_sortOrder );
  91. if ( selector_hasDuplicate ) {
  92. while ( (elem = results[i++]) ) {
  93. if ( elem === results[ i ] ) {
  94. j = duplicates.push( i );
  95. }
  96. }
  97. while ( j-- ) {
  98. results.splice( duplicates[ j ], 1 );
  99. }
  100. }
  101. return results;
  102. },
  103. text: function( elem ) {
  104. var node,
  105. ret = "",
  106. i = 0,
  107. nodeType = elem.nodeType;
  108. if ( !nodeType ) {
  109. // If no nodeType, this is expected to be an array
  110. while ( (node = elem[i++]) ) {
  111. // Do not traverse comment nodes
  112. ret += jQuery.text( node );
  113. }
  114. } else if ( nodeType === 1 || nodeType === 9 || nodeType === 11 ) {
  115. // Use textContent for elements
  116. return elem.textContent;
  117. } else if ( nodeType === 3 || nodeType === 4 ) {
  118. return elem.nodeValue;
  119. }
  120. // Do not include comment or processing instruction nodes
  121. return ret;
  122. },
  123. contains: function( a, b ) {
  124. var adown = a.nodeType === 9 ? a.documentElement : a,
  125. bup = b && b.parentNode;
  126. return a === bup || !!( bup && bup.nodeType === 1 && adown.contains(bup) );
  127. },
  128. isXMLDoc: function( elem ) {
  129. return (elem.ownerDocument || elem).documentElement.nodeName !== "HTML";
  130. },
  131. expr: {
  132. attrHandle: {},
  133. match: {
  134. bool: /^(?:checked|selected|async|autofocus|autoplay|controls|defer|disabled|hidden|ismap|loop|multiple|open|readonly|required|scoped)$/i,
  135. needsContext: /^[\x20\t\r\n\f]*[>+~]/
  136. }
  137. }
  138. });
  139. jQuery.extend( jQuery.find, {
  140. matches: function( expr, elements ) {
  141. return jQuery.find( expr, null, null, elements );
  142. },
  143. matchesSelector: function( elem, expr ) {
  144. return matches.call( elem, expr );
  145. },
  146. attr: function( elem, name ) {
  147. return elem.getAttribute( name );
  148. }
  149. });
  150. });