UPDATE 4.4.0.0
[phpmyadmin.git] / js / jquery / src / jquery / traversing.js
blobee3676963a36887c35e539e84d4f5f79324ad256
1 define([
2         "./core",
3         "./traversing/var/rneedsContext",
4         "./core/init",
5         "./traversing/findFilter",
6         "./selector"
7 ], function( jQuery, rneedsContext ) {
9 var rparentsprev = /^(?:parents|prev(?:Until|All))/,
10         // methods guaranteed to produce a unique set when starting from a unique set
11         guaranteedUnique = {
12                 children: true,
13                 contents: true,
14                 next: true,
15                 prev: true
16         };
18 jQuery.extend({
19         dir: function( elem, dir, until ) {
20                 var matched = [],
21                         cur = elem[ dir ];
23                 while ( cur && cur.nodeType !== 9 && (until === undefined || cur.nodeType !== 1 || !jQuery( cur ).is( until )) ) {
24                         if ( cur.nodeType === 1 ) {
25                                 matched.push( cur );
26                         }
27                         cur = cur[dir];
28                 }
29                 return matched;
30         },
32         sibling: function( n, elem ) {
33                 var r = [];
35                 for ( ; n; n = n.nextSibling ) {
36                         if ( n.nodeType === 1 && n !== elem ) {
37                                 r.push( n );
38                         }
39                 }
41                 return r;
42         }
43 });
45 jQuery.fn.extend({
46         has: function( target ) {
47                 var i,
48                         targets = jQuery( target, this ),
49                         len = targets.length;
51                 return this.filter(function() {
52                         for ( i = 0; i < len; i++ ) {
53                                 if ( jQuery.contains( this, targets[i] ) ) {
54                                         return true;
55                                 }
56                         }
57                 });
58         },
60         closest: function( selectors, context ) {
61                 var cur,
62                         i = 0,
63                         l = this.length,
64                         matched = [],
65                         pos = rneedsContext.test( selectors ) || typeof selectors !== "string" ?
66                                 jQuery( selectors, context || this.context ) :
67                                 0;
69                 for ( ; i < l; i++ ) {
70                         for ( cur = this[i]; cur && cur !== context; cur = cur.parentNode ) {
71                                 // Always skip document fragments
72                                 if ( cur.nodeType < 11 && (pos ?
73                                         pos.index(cur) > -1 :
75                                         // Don't pass non-elements to Sizzle
76                                         cur.nodeType === 1 &&
77                                                 jQuery.find.matchesSelector(cur, selectors)) ) {
79                                         matched.push( cur );
80                                         break;
81                                 }
82                         }
83                 }
85                 return this.pushStack( matched.length > 1 ? jQuery.unique( matched ) : matched );
86         },
88         // Determine the position of an element within
89         // the matched set of elements
90         index: function( elem ) {
92                 // No argument, return index in parent
93                 if ( !elem ) {
94                         return ( this[0] && this[0].parentNode ) ? this.first().prevAll().length : -1;
95                 }
97                 // index in selector
98                 if ( typeof elem === "string" ) {
99                         return jQuery.inArray( this[0], jQuery( elem ) );
100                 }
102                 // Locate the position of the desired element
103                 return jQuery.inArray(
104                         // If it receives a jQuery object, the first element is used
105                         elem.jquery ? elem[0] : elem, this );
106         },
108         add: function( selector, context ) {
109                 return this.pushStack(
110                         jQuery.unique(
111                                 jQuery.merge( this.get(), jQuery( selector, context ) )
112                         )
113                 );
114         },
116         addBack: function( selector ) {
117                 return this.add( selector == null ?
118                         this.prevObject : this.prevObject.filter(selector)
119                 );
120         }
123 function sibling( cur, dir ) {
124         do {
125                 cur = cur[ dir ];
126         } while ( cur && cur.nodeType !== 1 );
128         return cur;
131 jQuery.each({
132         parent: function( elem ) {
133                 var parent = elem.parentNode;
134                 return parent && parent.nodeType !== 11 ? parent : null;
135         },
136         parents: function( elem ) {
137                 return jQuery.dir( elem, "parentNode" );
138         },
139         parentsUntil: function( elem, i, until ) {
140                 return jQuery.dir( elem, "parentNode", until );
141         },
142         next: function( elem ) {
143                 return sibling( elem, "nextSibling" );
144         },
145         prev: function( elem ) {
146                 return sibling( elem, "previousSibling" );
147         },
148         nextAll: function( elem ) {
149                 return jQuery.dir( elem, "nextSibling" );
150         },
151         prevAll: function( elem ) {
152                 return jQuery.dir( elem, "previousSibling" );
153         },
154         nextUntil: function( elem, i, until ) {
155                 return jQuery.dir( elem, "nextSibling", until );
156         },
157         prevUntil: function( elem, i, until ) {
158                 return jQuery.dir( elem, "previousSibling", until );
159         },
160         siblings: function( elem ) {
161                 return jQuery.sibling( ( elem.parentNode || {} ).firstChild, elem );
162         },
163         children: function( elem ) {
164                 return jQuery.sibling( elem.firstChild );
165         },
166         contents: function( elem ) {
167                 return jQuery.nodeName( elem, "iframe" ) ?
168                         elem.contentDocument || elem.contentWindow.document :
169                         jQuery.merge( [], elem.childNodes );
170         }
171 }, function( name, fn ) {
172         jQuery.fn[ name ] = function( until, selector ) {
173                 var ret = jQuery.map( this, fn, until );
175                 if ( name.slice( -5 ) !== "Until" ) {
176                         selector = until;
177                 }
179                 if ( selector && typeof selector === "string" ) {
180                         ret = jQuery.filter( selector, ret );
181                 }
183                 if ( this.length > 1 ) {
184                         // Remove duplicates
185                         if ( !guaranteedUnique[ name ] ) {
186                                 ret = jQuery.unique( ret );
187                         }
189                         // Reverse order for parents* and prev-derivatives
190                         if ( rparentsprev.test( name ) ) {
191                                 ret = ret.reverse();
192                         }
193                 }
195                 return this.pushStack( ret );
196         };
199 return jQuery;