You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
4.0KB

  1. define( [
  2. "./core",
  3. "./var/indexOf",
  4. "./traversing/var/dir",
  5. "./traversing/var/siblings",
  6. "./traversing/var/rneedsContext",
  7. "./core/init",
  8. "./traversing/findFilter",
  9. "./selector"
  10. ], function( jQuery, indexOf, dir, siblings, rneedsContext ) {
  11. var rparentsprev = /^(?:parents|prev(?:Until|All))/,
  12. // Methods guaranteed to produce a unique set when starting from a unique set
  13. guaranteedUnique = {
  14. children: true,
  15. contents: true,
  16. next: true,
  17. prev: true
  18. };
  19. jQuery.fn.extend( {
  20. has: function( target ) {
  21. var targets = jQuery( target, this ),
  22. l = targets.length;
  23. return this.filter( function() {
  24. var i = 0;
  25. for ( ; i < l; i++ ) {
  26. if ( jQuery.contains( this, targets[ i ] ) ) {
  27. return true;
  28. }
  29. }
  30. } );
  31. },
  32. closest: function( selectors, context ) {
  33. var cur,
  34. i = 0,
  35. l = this.length,
  36. matched = [],
  37. pos = rneedsContext.test( selectors ) || typeof selectors !== "string" ?
  38. jQuery( selectors, context || this.context ) :
  39. 0;
  40. for ( ; i < l; i++ ) {
  41. for ( cur = this[ i ]; cur && cur !== context; cur = cur.parentNode ) {
  42. // Always skip document fragments
  43. if ( cur.nodeType < 11 && ( pos ?
  44. pos.index( cur ) > -1 :
  45. // Don't pass non-elements to Sizzle
  46. cur.nodeType === 1 &&
  47. jQuery.find.matchesSelector( cur, selectors ) ) ) {
  48. matched.push( cur );
  49. break;
  50. }
  51. }
  52. }
  53. return this.pushStack( matched.length > 1 ? jQuery.uniqueSort( matched ) : matched );
  54. },
  55. // Determine the position of an element within the set
  56. index: function( elem ) {
  57. // No argument, return index in parent
  58. if ( !elem ) {
  59. return ( this[ 0 ] && this[ 0 ].parentNode ) ? this.first().prevAll().length : -1;
  60. }
  61. // Index in selector
  62. if ( typeof elem === "string" ) {
  63. return indexOf.call( jQuery( elem ), this[ 0 ] );
  64. }
  65. // Locate the position of the desired element
  66. return indexOf.call( this,
  67. // If it receives a jQuery object, the first element is used
  68. elem.jquery ? elem[ 0 ] : elem
  69. );
  70. },
  71. add: function( selector, context ) {
  72. return this.pushStack(
  73. jQuery.uniqueSort(
  74. jQuery.merge( this.get(), jQuery( selector, context ) )
  75. )
  76. );
  77. },
  78. addBack: function( selector ) {
  79. return this.add( selector == null ?
  80. this.prevObject : this.prevObject.filter( selector )
  81. );
  82. }
  83. } );
  84. function sibling( cur, dir ) {
  85. while ( ( cur = cur[ dir ] ) && cur.nodeType !== 1 ) {}
  86. return cur;
  87. }
  88. jQuery.each( {
  89. parent: function( elem ) {
  90. var parent = elem.parentNode;
  91. return parent && parent.nodeType !== 11 ? parent : null;
  92. },
  93. parents: function( elem ) {
  94. return dir( elem, "parentNode" );
  95. },
  96. parentsUntil: function( elem, i, until ) {
  97. return dir( elem, "parentNode", until );
  98. },
  99. next: function( elem ) {
  100. return sibling( elem, "nextSibling" );
  101. },
  102. prev: function( elem ) {
  103. return sibling( elem, "previousSibling" );
  104. },
  105. nextAll: function( elem ) {
  106. return dir( elem, "nextSibling" );
  107. },
  108. prevAll: function( elem ) {
  109. return dir( elem, "previousSibling" );
  110. },
  111. nextUntil: function( elem, i, until ) {
  112. return dir( elem, "nextSibling", until );
  113. },
  114. prevUntil: function( elem, i, until ) {
  115. return dir( elem, "previousSibling", until );
  116. },
  117. siblings: function( elem ) {
  118. return siblings( ( elem.parentNode || {} ).firstChild, elem );
  119. },
  120. children: function( elem ) {
  121. return siblings( elem.firstChild );
  122. },
  123. contents: function( elem ) {
  124. return elem.contentDocument || jQuery.merge( [], elem.childNodes );
  125. }
  126. }, function( name, fn ) {
  127. jQuery.fn[ name ] = function( until, selector ) {
  128. var matched = jQuery.map( this, fn, until );
  129. if ( name.slice( -5 ) !== "Until" ) {
  130. selector = until;
  131. }
  132. if ( selector && typeof selector === "string" ) {
  133. matched = jQuery.filter( selector, matched );
  134. }
  135. if ( this.length > 1 ) {
  136. // Remove duplicates
  137. if ( !guaranteedUnique[ name ] ) {
  138. jQuery.uniqueSort( matched );
  139. }
  140. // Reverse order for parents* and prev-derivatives
  141. if ( rparentsprev.test( name ) ) {
  142. matched.reverse();
  143. }
  144. }
  145. return this.pushStack( matched );
  146. };
  147. } );
  148. return jQuery;
  149. } );