Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame] | 1 | define( [ |
| 2 | "./core", |
| 3 | "./var/indexOf", |
| 4 | "./traversing/var/dir", |
| 5 | "./traversing/var/siblings", |
| 6 | "./traversing/var/rneedsContext", |
| 7 | "./core/nodeName", |
| 8 | |
| 9 | "./core/init", |
| 10 | "./traversing/findFilter", |
| 11 | "./selector" |
| 12 | ], function( jQuery, indexOf, dir, siblings, rneedsContext, nodeName ) { |
| 13 | |
| 14 | "use strict"; |
| 15 | |
| 16 | var rparentsprev = /^(?:parents|prev(?:Until|All))/, |
| 17 | |
| 18 | // Methods guaranteed to produce a unique set when starting from a unique set |
| 19 | guaranteedUnique = { |
| 20 | children: true, |
| 21 | contents: true, |
| 22 | next: true, |
| 23 | prev: true |
| 24 | }; |
| 25 | |
| 26 | jQuery.fn.extend( { |
| 27 | has: function( target ) { |
| 28 | var targets = jQuery( target, this ), |
| 29 | l = targets.length; |
| 30 | |
| 31 | return this.filter( function() { |
| 32 | var i = 0; |
| 33 | for ( ; i < l; i++ ) { |
| 34 | if ( jQuery.contains( this, targets[ i ] ) ) { |
| 35 | return true; |
| 36 | } |
| 37 | } |
| 38 | } ); |
| 39 | }, |
| 40 | |
| 41 | closest: function( selectors, context ) { |
| 42 | var cur, |
| 43 | i = 0, |
| 44 | l = this.length, |
| 45 | matched = [], |
| 46 | targets = typeof selectors !== "string" && jQuery( selectors ); |
| 47 | |
| 48 | // Positional selectors never match, since there's no _selection_ context |
| 49 | if ( !rneedsContext.test( selectors ) ) { |
| 50 | for ( ; i < l; i++ ) { |
| 51 | for ( cur = this[ i ]; cur && cur !== context; cur = cur.parentNode ) { |
| 52 | |
| 53 | // Always skip document fragments |
| 54 | if ( cur.nodeType < 11 && ( targets ? |
| 55 | targets.index( cur ) > -1 : |
| 56 | |
| 57 | // Don't pass non-elements to Sizzle |
| 58 | cur.nodeType === 1 && |
| 59 | jQuery.find.matchesSelector( cur, selectors ) ) ) { |
| 60 | |
| 61 | matched.push( cur ); |
| 62 | break; |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return this.pushStack( matched.length > 1 ? jQuery.uniqueSort( matched ) : matched ); |
| 69 | }, |
| 70 | |
| 71 | // Determine the position of an element within the set |
| 72 | index: function( elem ) { |
| 73 | |
| 74 | // No argument, return index in parent |
| 75 | if ( !elem ) { |
| 76 | return ( this[ 0 ] && this[ 0 ].parentNode ) ? this.first().prevAll().length : -1; |
| 77 | } |
| 78 | |
| 79 | // Index in selector |
| 80 | if ( typeof elem === "string" ) { |
| 81 | return indexOf.call( jQuery( elem ), this[ 0 ] ); |
| 82 | } |
| 83 | |
| 84 | // Locate the position of the desired element |
| 85 | return indexOf.call( this, |
| 86 | |
| 87 | // If it receives a jQuery object, the first element is used |
| 88 | elem.jquery ? elem[ 0 ] : elem |
| 89 | ); |
| 90 | }, |
| 91 | |
| 92 | add: function( selector, context ) { |
| 93 | return this.pushStack( |
| 94 | jQuery.uniqueSort( |
| 95 | jQuery.merge( this.get(), jQuery( selector, context ) ) |
| 96 | ) |
| 97 | ); |
| 98 | }, |
| 99 | |
| 100 | addBack: function( selector ) { |
| 101 | return this.add( selector == null ? |
| 102 | this.prevObject : this.prevObject.filter( selector ) |
| 103 | ); |
| 104 | } |
| 105 | } ); |
| 106 | |
| 107 | function sibling( cur, dir ) { |
| 108 | while ( ( cur = cur[ dir ] ) && cur.nodeType !== 1 ) {} |
| 109 | return cur; |
| 110 | } |
| 111 | |
| 112 | jQuery.each( { |
| 113 | parent: function( elem ) { |
| 114 | var parent = elem.parentNode; |
| 115 | return parent && parent.nodeType !== 11 ? parent : null; |
| 116 | }, |
| 117 | parents: function( elem ) { |
| 118 | return dir( elem, "parentNode" ); |
| 119 | }, |
| 120 | parentsUntil: function( elem, i, until ) { |
| 121 | return dir( elem, "parentNode", until ); |
| 122 | }, |
| 123 | next: function( elem ) { |
| 124 | return sibling( elem, "nextSibling" ); |
| 125 | }, |
| 126 | prev: function( elem ) { |
| 127 | return sibling( elem, "previousSibling" ); |
| 128 | }, |
| 129 | nextAll: function( elem ) { |
| 130 | return dir( elem, "nextSibling" ); |
| 131 | }, |
| 132 | prevAll: function( elem ) { |
| 133 | return dir( elem, "previousSibling" ); |
| 134 | }, |
| 135 | nextUntil: function( elem, i, until ) { |
| 136 | return dir( elem, "nextSibling", until ); |
| 137 | }, |
| 138 | prevUntil: function( elem, i, until ) { |
| 139 | return dir( elem, "previousSibling", until ); |
| 140 | }, |
| 141 | siblings: function( elem ) { |
| 142 | return siblings( ( elem.parentNode || {} ).firstChild, elem ); |
| 143 | }, |
| 144 | children: function( elem ) { |
| 145 | return siblings( elem.firstChild ); |
| 146 | }, |
| 147 | contents: function( elem ) { |
| 148 | if ( typeof elem.contentDocument !== "undefined" ) { |
| 149 | return elem.contentDocument; |
| 150 | } |
| 151 | |
| 152 | // Support: IE 9 - 11 only, iOS 7 only, Android Browser <=4.3 only |
| 153 | // Treat the template element as a regular one in browsers that |
| 154 | // don't support it. |
| 155 | if ( nodeName( elem, "template" ) ) { |
| 156 | elem = elem.content || elem; |
| 157 | } |
| 158 | |
| 159 | return jQuery.merge( [], elem.childNodes ); |
| 160 | } |
| 161 | }, function( name, fn ) { |
| 162 | jQuery.fn[ name ] = function( until, selector ) { |
| 163 | var matched = jQuery.map( this, fn, until ); |
| 164 | |
| 165 | if ( name.slice( -5 ) !== "Until" ) { |
| 166 | selector = until; |
| 167 | } |
| 168 | |
| 169 | if ( selector && typeof selector === "string" ) { |
| 170 | matched = jQuery.filter( selector, matched ); |
| 171 | } |
| 172 | |
| 173 | if ( this.length > 1 ) { |
| 174 | |
| 175 | // Remove duplicates |
| 176 | if ( !guaranteedUnique[ name ] ) { |
| 177 | jQuery.uniqueSort( matched ); |
| 178 | } |
| 179 | |
| 180 | // Reverse order for parents* and prev-derivatives |
| 181 | if ( rparentsprev.test( name ) ) { |
| 182 | matched.reverse(); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | return this.pushStack( matched ); |
| 187 | }; |
| 188 | } ); |
| 189 | |
| 190 | return jQuery; |
| 191 | } ); |