Disabled the automatic inclusion of offset.js
[jquery.git] / src / selector.js
1
2 var chars = jQuery.browser.safari && parseInt(jQuery.browser.version) < 417 ?
3                 "(?:[\\w*_-]|\\\\.)" :
4                 "(?:[\\w\u0128-\uFFFF*_-]|\\\\.)",
5         quickChild = new RegExp("^>\\s*(" + chars + "+)"),
6         quickID = new RegExp("^(" + chars + "+)(#)(" + chars + "+)"),
7         quickClass = new RegExp("^([#.]?)(" + chars + "*)");
8
9 jQuery.extend({
10         expr: {
11                 "": "m[2]=='*'||jQuery.nodeName(a,m[2])",
12                 "#": "a.getAttribute('id')==m[2]",
13                 ":": {
14                         // Position Checks
15                         lt: "i<m[3]-0",
16                         gt: "i>m[3]-0",
17                         nth: "m[3]-0==i",
18                         eq: "m[3]-0==i",
19                         first: "i==0",
20                         last: "i==r.length-1",
21                         even: "i%2==0",
22                         odd: "i%2",
23
24                         // Child Checks
25                         "first-child": "a.parentNode.getElementsByTagName('*')[0]==a",
26                         "last-child": "jQuery.nth(a.parentNode.lastChild,1,'previousSibling')==a",
27                         "only-child": "!jQuery.nth(a.parentNode.lastChild,2,'previousSibling')",
28
29                         // Parent Checks
30                         parent: "a.firstChild",
31                         empty: "!a.firstChild",
32
33                         // Text Check
34                         contains: "(a.textContent||a.innerText||'').indexOf(m[3])>=0",
35
36                         // Visibility
37                         visible: '"hidden"!=a.type&&jQuery.css(a,"display")!="none"&&jQuery.css(a,"visibility")!="hidden"',
38                         hidden: '"hidden"==a.type||jQuery.css(a,"display")=="none"||jQuery.css(a,"visibility")=="hidden"',
39
40                         // Form attributes
41                         enabled: "!a.disabled",
42                         disabled: "a.disabled",
43                         checked: "a.checked",
44                         selected: "a.selected||jQuery.attr(a,'selected')",
45
46                         // Form elements
47                         text: "'text'==a.type",
48                         radio: "'radio'==a.type",
49                         checkbox: "'checkbox'==a.type",
50                         file: "'file'==a.type",
51                         password: "'password'==a.type",
52                         submit: "'submit'==a.type",
53                         image: "'image'==a.type",
54                         reset: "'reset'==a.type",
55                         button: '"button"==a.type||jQuery.nodeName(a,"button")',
56                         input: "/input|select|textarea|button/i.test(a.nodeName)",
57
58                         // :has()
59                         has: "jQuery.find(m[3],a).length",
60
61                         // :header
62                         header: "/h\\d/i.test(a.nodeName)",
63
64                         // :animated
65                         animated: "jQuery.grep(jQuery.timers,function(fn){return a==fn.elem;}).length"
66                 }
67         },
68         
69         // The regular expressions that power the parsing engine
70         parse: [
71                 // Match: [@value='test'], [@foo]
72                 /^(\[) *@?([\w-]+) *([!*$^~=]*) *('?"?)(.*?)\4 *\]/,
73
74                 // Match: :contains('foo')
75                 /^(:)([\w-]+)\("?'?(.*?(\(.*?\))?[^(]*?)"?'?\)/,
76
77                 // Match: :even, :last-chlid, #id, .class
78                 new RegExp("^([:.#]*)(" + chars + "+)")
79         ],
80
81         multiFilter: function( expr, elems, not ) {
82                 var old, cur = [];
83
84                 while ( expr && expr != old ) {
85                         old = expr;
86                         var f = jQuery.filter( expr, elems, not );
87                         expr = f.t.replace(/^\s*,\s*/, "" );
88                         cur = not ? elems = f.r : jQuery.merge( cur, f.r );
89                 }
90
91                 return cur;
92         },
93
94         find: function( t, context ) {
95                 // Quickly handle non-string expressions
96                 if ( typeof t != "string" )
97                         return [ t ];
98
99                 // Make sure that the context is a DOM Element
100                 if ( context && !context.nodeType )
101                         context = null;
102
103                 // Set the correct context (if none is provided)
104                 context = context || document;
105
106                 // Initialize the search
107                 var ret = [context], done = [], last;
108
109                 // Continue while a selector expression exists, and while
110                 // we're no longer looping upon ourselves
111                 while ( t && last != t ) {
112                         var r = [];
113                         last = t;
114
115                         t = jQuery.trim(t);
116
117                         var foundToken = false;
118
119                         // An attempt at speeding up child selectors that
120                         // point to a specific element tag
121                         var re = quickChild;
122                         var m = re.exec(t);
123
124                         if ( m ) {
125                                 var nodeName = m[1].toUpperCase();
126
127                                 // Perform our own iteration and filter
128                                 for ( var i = 0; ret[i]; i++ )
129                                         for ( var c = ret[i].firstChild; c; c = c.nextSibling )
130                                                 if ( c.nodeType == 1 && (nodeName == "*" || c.nodeName.toUpperCase() == nodeName.toUpperCase()) )
131                                                         r.push( c );
132
133                                 ret = r;
134                                 t = t.replace( re, "" );
135                                 if ( t.indexOf(" ") == 0 ) continue;
136                                 foundToken = true;
137                         } else {
138                                 re = /^([>+~])\s*(\w*)/i;
139
140                                 if ( (m = re.exec(t)) != null ) {
141                                         r = [];
142
143                                         var nodeName = m[2], mergeNum = jQuery.mergeNum++;
144                                         m = m[1];
145
146                                         for ( var j = 0, rl = ret.length; j < rl; j++ ) {
147                                                 var n = m == "~" || m == "+" ? ret[j].nextSibling : ret[j].firstChild;
148                                                 for ( ; n; n = n.nextSibling )
149                                                         if ( n.nodeType == 1 ) {
150                                                                 if ( m == "~" && n.mergeNum == mergeNum ) break;
151                                                                 
152                                                                 if (!nodeName || n.nodeName.toUpperCase() == nodeName.toUpperCase() ) {
153                                                                         if ( m == "~" ) n.mergeNum = mergeNum;
154                                                                         r.push( n );
155                                                                 }
156                                                                 
157                                                                 if ( m == "+" ) break;
158                                                         }
159                                         }
160
161                                         ret = r;
162
163                                         // And remove the token
164                                         t = jQuery.trim( t.replace( re, "" ) );
165                                         foundToken = true;
166                                 }
167                         }
168
169                         // See if there's still an expression, and that we haven't already
170                         // matched a token
171                         if ( t && !foundToken ) {
172                                 // Handle multiple expressions
173                                 if ( !t.indexOf(",") ) {
174                                         // Clean the result set
175                                         if ( context == ret[0] ) ret.shift();
176
177                                         // Merge the result sets
178                                         done = jQuery.merge( done, ret );
179
180                                         // Reset the context
181                                         r = ret = [context];
182
183                                         // Touch up the selector string
184                                         t = " " + t.substr(1,t.length);
185
186                                 } else {
187                                         // Optimize for the case nodeName#idName
188                                         var re2 = quickID;
189                                         var m = re2.exec(t);
190                                         
191                                         // Re-organize the results, so that they're consistent
192                                         if ( m ) {
193                                            m = [ 0, m[2], m[3], m[1] ];
194
195                                         } else {
196                                                 // Otherwise, do a traditional filter check for
197                                                 // ID, class, and element selectors
198                                                 re2 = quickClass;
199                                                 m = re2.exec(t);
200                                         }
201
202                                         m[2] = m[2].replace(/\\/g, "");
203
204                                         var elem = ret[ret.length-1];
205
206                                         // Try to do a global search by ID, where we can
207                                         if ( m[1] == "#" && elem && elem.getElementById && !jQuery.isXMLDoc(elem) ) {
208                                                 // Optimization for HTML document case
209                                                 var oid = elem.getElementById(m[2]);
210                                                 
211                                                 // Do a quick check for the existence of the actual ID attribute
212                                                 // to avoid selecting by the name attribute in IE
213                                                 // also check to insure id is a string to avoid selecting an element with the name of 'id' inside a form
214                                                 if ( (jQuery.browser.msie||jQuery.browser.opera) && oid && typeof oid.id == "string" && oid.id != m[2] )
215                                                         oid = jQuery('[@id="'+m[2]+'"]', elem)[0];
216
217                                                 // Do a quick check for node name (where applicable) so
218                                                 // that div#foo searches will be really fast
219                                                 ret = r = oid && (!m[3] || jQuery.nodeName(oid, m[3])) ? [oid] : [];
220                                         } else {
221                                                 // We need to find all descendant elements
222                                                 for ( var i = 0; ret[i]; i++ ) {
223                                                         // Grab the tag name being searched for
224                                                         var tag = m[1] == "#" && m[3] ? m[3] : m[1] != "" || m[0] == "" ? "*" : m[2];
225
226                                                         // Handle IE7 being really dumb about <object>s
227                                                         if ( tag == "*" && ret[i].nodeName.toLowerCase() == "object" )
228                                                                 tag = "param";
229
230                                                         r = jQuery.merge( r, ret[i].getElementsByTagName( tag ));
231                                                 }
232
233                                                 // It's faster to filter by class and be done with it
234                                                 if ( m[1] == "." )
235                                                         r = jQuery.classFilter( r, m[2] );
236
237                                                 // Same with ID filtering
238                                                 if ( m[1] == "#" ) {
239                                                         var tmp = [];
240
241                                                         // Try to find the element with the ID
242                                                         for ( var i = 0; r[i]; i++ )
243                                                                 if ( r[i].getAttribute("id") == m[2] ) {
244                                                                         tmp = [ r[i] ];
245                                                                         break;
246                                                                 }
247
248                                                         r = tmp;
249                                                 }
250
251                                                 ret = r;
252                                         }
253
254                                         t = t.replace( re2, "" );
255                                 }
256
257                         }
258
259                         // If a selector string still exists
260                         if ( t ) {
261                                 // Attempt to filter it
262                                 var val = jQuery.filter(t,r);
263                                 ret = r = val.r;
264                                 t = jQuery.trim(val.t);
265                         }
266                 }
267
268                 // An error occurred with the selector;
269                 // just return an empty set instead
270                 if ( t )
271                         ret = [];
272
273                 // Remove the root context
274                 if ( ret && context == ret[0] )
275                         ret.shift();
276
277                 // And combine the results
278                 done = jQuery.merge( done, ret );
279
280                 return done;
281         },
282
283         classFilter: function(r,m,not){
284                 m = " " + m + " ";
285                 var tmp = [];
286                 for ( var i = 0; r[i]; i++ ) {
287                         var pass = (" " + r[i].className + " ").indexOf( m ) >= 0;
288                         if ( !not && pass || not && !pass )
289                                 tmp.push( r[i] );
290                 }
291                 return tmp;
292         },
293
294         filter: function(t,r,not) {
295                 var last;
296
297                 // Look for common filter expressions
298                 while ( t  && t != last ) {
299                         last = t;
300
301                         var p = jQuery.parse, m;
302
303                         for ( var i = 0; p[i]; i++ ) {
304                                 m = p[i].exec( t );
305
306                                 if ( m ) {
307                                         // Remove what we just matched
308                                         t = t.substring( m[0].length );
309
310                                         m[2] = m[2].replace(/\\/g, "");
311                                         break;
312                                 }
313                         }
314
315                         if ( !m )
316                                 break;
317
318                         // :not() is a special case that can be optimized by
319                         // keeping it out of the expression list
320                         if ( m[1] == ":" && m[2] == "not" )
321                                 r = jQuery.filter(m[3], r, true).r;
322
323                         // We can get a big speed boost by filtering by class here
324                         else if ( m[1] == "." )
325                                 r = jQuery.classFilter(r, m[2], not);
326
327                         else if ( m[1] == "[" ) {
328                                 var tmp = [], type = m[3];
329                                 
330                                 for ( var i = 0, rl = r.length; i < rl; i++ ) {
331                                         var a = r[i], z = a[ jQuery.props[m[2]] || m[2] ];
332                                         
333                                         if ( z == null || /href|src|selected/.test(m[2]) )
334                                                 z = jQuery.attr(a,m[2]) || '';
335
336                                         if ( (type == "" && !!z ||
337                                                  type == "=" && z == m[5] ||
338                                                  type == "!=" && z != m[5] ||
339                                                  type == "^=" && z && !z.indexOf(m[5]) ||
340                                                  type == "$=" && z.substr(z.length - m[5].length) == m[5] ||
341                                                  (type == "*=" || type == "~=") && z.indexOf(m[5]) >= 0) ^ not )
342                                                         tmp.push( a );
343                                 }
344                                 
345                                 r = tmp;
346
347                         // We can get a speed boost by handling nth-child here
348                         } else if ( m[1] == ":" && m[2] == "nth-child" ) {
349                                 var num = jQuery.mergeNum++, tmp = [],
350                                         test = /(\d*)n\+?(\d*)/.exec(
351                                                 m[3] == "even" && "2n" || m[3] == "odd" && "2n+1" ||
352                                                 !/\D/.test(m[3]) && "n+" + m[3] || m[3]),
353                                         first = (test[1] || 1) - 0, last = test[2] - 0;
354
355                                 for ( var i = 0, rl = r.length; i < rl; i++ ) {
356                                         var node = r[i], parentNode = node.parentNode;
357
358                                         if ( num != parentNode.mergeNum ) {
359                                                 var c = 1;
360
361                                                 for ( var n = parentNode.firstChild; n; n = n.nextSibling )
362                                                         if ( n.nodeType == 1 )
363                                                                 n.nodeIndex = c++;
364
365                                                 parentNode.mergeNum = num;
366                                         }
367
368                                         var add = false;
369
370                                         if ( first == 1 ) {
371                                                 if ( last == 0 || node.nodeIndex == last )
372                                                         add = true;
373                                         } else if ( (node.nodeIndex + last) % first == 0 )
374                                                 add = true;
375
376                                         if ( add ^ not )
377                                                 tmp.push( node );
378                                 }
379
380                                 r = tmp;
381
382                         // Otherwise, find the expression to execute
383                         } else {
384                                 var f = jQuery.expr[m[1]];
385                                 if ( typeof f != "string" )
386                                         f = jQuery.expr[m[1]][m[2]];
387
388                                 // Build a custom macro to enclose it
389                                 f = eval("false||function(a,i){return " + f + "}");
390
391                                 // Execute it against the current filter
392                                 r = jQuery.grep( r, f, not );
393                         }
394                 }
395
396                 // Return an array of filtered elements (r)
397                 // and the modified expression string (t)
398                 return { r: r, t: t };
399         },
400
401         parents: function( elem ){
402                 var matched = [];
403                 var cur = elem.parentNode;
404                 while ( cur && cur != document ) {
405                         matched.push( cur );
406                         cur = cur.parentNode;
407                 }
408                 return matched;
409         },
410         
411         nth: function(cur,result,dir,elem){
412                 result = result || 1;
413                 var num = 0;
414
415                 for ( ; cur; cur = cur[dir] )
416                         if ( cur.nodeType == 1 && ++num == result )
417                                 break;
418
419                 return cur;
420         },
421         
422         sibling: function( n, elem ) {
423                 var r = [];
424
425                 for ( ; n; n = n.nextSibling ) {
426                         if ( n.nodeType == 1 && (!elem || n != elem) )
427                                 r.push( n );
428                 }
429
430                 return r;
431         }
432 });