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