3 "": "m[2]== '*'||a.nodeName.toUpperCase()==m[2].toUpperCase()",
4 "#": "a.getAttribute('id')==m[2]",
12 last: "i==r.length-1",
17 "nth-child": "jQuery.nth(a.parentNode.firstChild,m[3],'nextSibling')==a",
18 "first-child": "jQuery.nth(a.parentNode.firstChild,1,'nextSibling')==a",
19 "last-child": "jQuery.nth(a.parentNode.lastChild,1,'previousSibling')==a",
20 "only-child": "jQuery.sibling(a.parentNode.firstChild).length==1",
23 parent: "a.firstChild",
24 empty: "!a.firstChild",
27 contains: "jQuery.fn.text.apply([a]).indexOf(m[3])>=0",
30 visible: "a.type!='hidden'&&jQuery.css(a,'display')!='none'&&jQuery.css(a,'visibility')!='hidden'",
31 hidden: "a.type=='hidden'||jQuery.css(a,'display')=='none'||jQuery.css(a,'visibility')=='hidden'",
34 enabled: "!a.disabled",
35 disabled: "a.disabled",
37 selected: "a.selected || jQuery.attr(a, 'selected')",
40 text: "a.type=='text'",
41 radio: "a.type=='radio'",
42 checkbox: "a.type=='checkbox'",
43 file: "a.type=='file'",
44 password: "a.type=='password'",
45 submit: "a.type=='submit'",
46 image: "a.type=='image'",
47 reset: "a.type=='reset'",
48 button: "a.type=='button'||a.nodeName=='BUTTON'",
49 input: "/input|select|textarea|button/i.test(a.nodeName)"
51 ".": "jQuery.className.has(a,m[2])",
55 "^=": "z && !z.indexOf(m[4])",
56 "$=": "z && z.substr(z.length - m[4].length,m[4].length)==m[4]",
57 "*=": "z && z.indexOf(m[4])>=0",
60 return ["", m[1], m[3], m[2], m[5]];
62 _prefix: "z=jQuery.attr(a,m[3]);"
64 "[": "jQuery.find(m[2],a).length"
67 // The regular expressions that power the parsing engine
69 // Match: [@value='test'], [@foo]
70 "\\[ *(@)S *([!*$^=]*) *('?\"?)(.*?)\\4 *\\]",
72 // Match: [div], [div p]
73 "(\\[)\\s*(.*?)\\s*\\]",
75 // Match: :contains('foo')
76 "(:)S\\(\"?'?([^\\)]*?)\"?'?\\)",
78 // Match: :even, :last-chlid
83 "\\.\\.|/\\.\\.", "a.parentNode",
84 ">|/", "jQuery.sibling(a.firstChild)",
85 "\\+", "jQuery.nth(a,2,'nextSibling')",
87 var s = jQuery.sibling(a.parentNode.firstChild);
88 return s.slice(0, jQuery.inArray(a,s));
94 * @type Array<Element>
98 find: function( t, context ) {
99 // Quickly handle non-string expressions
100 if ( typeof t != "string" )
103 // Make sure that the context is a DOM Element
104 if ( context && context.nodeType == undefined )
107 // Set the correct context (if none is provided)
108 context = context || document;
110 // Handle the common XPath // expression
111 if ( !t.indexOf("//") ) {
112 context = context.documentElement;
113 t = t.substr(2,t.length);
115 // And the / root expression
116 } else if ( !t.indexOf("/") ) {
117 context = context.documentElement;
118 t = t.substr(1,t.length);
119 if ( t.indexOf("/") >= 1 )
120 t = t.substr(t.indexOf("/"),t.length);
123 // Initialize the search
124 var ret = [context], done = [], last = null;
126 // Continue while a selector expression exists, and while
127 // we're no longer looping upon ourselves
128 while ( t && last != t ) {
132 t = jQuery.trim(t).replace( /^\/\//i, "" );
134 var foundToken = false;
136 // An attempt at speeding up child selectors that
137 // point to a specific element tag
138 var re = /^[\/>]\s*([a-z0-9*-]+)/i;
142 // Perform our own iteration and filter
143 for ( var i = 0, rl = ret.length; i < rl; i++ )
144 for ( var c = ret[i].firstChild; c; c = c.nextSibling )
145 if ( c.nodeType == 1 && ( c.nodeName == m[1].toUpperCase() || m[1] == "*" ) )
149 t = jQuery.trim( t.replace( re, "" ) );
152 // Look for pre-defined expression tokens
153 for ( var i = 0; i < jQuery.token.length; i += 2 ) {
154 // Attempt to match each, individual, token in
155 // the specified order
156 var re = new RegExp("^(" + jQuery.token[i] + ")");
159 // If the token match was found
161 // Map it against the token's handler
162 r = ret = jQuery.map( ret, jQuery.token[i+1].constructor == Function ?
164 function(a){ return eval(jQuery.token[i+1]); });
166 // And remove the token
167 t = jQuery.trim( t.replace( re, "" ) );
174 // See if there's still an expression, and that we haven't already
176 if ( t && !foundToken ) {
177 // Handle multiple expressions
178 if ( !t.indexOf(",") || !t.indexOf("|") ) {
179 // Clean teh result set
180 if ( ret[0] == context ) ret.shift();
182 // Merge the result sets
183 jQuery.merge( done, ret );
188 // Touch up the selector string
189 t = " " + t.substr(1,t.length);
192 // Optomize for the case nodeName#idName
193 var re2 = /^([a-z0-9_-]+)(#)([a-z0-9\\*_-]*)/i;
196 // Re-organize the results, so that they're consistent
198 m = [ 0, m[2], m[3], m[1] ];
201 // Otherwise, do a traditional filter check for
202 // ID, class, and element selectors
203 re2 = /^([#.]?)([a-z0-9\\*_-]*)/i;
207 // Try to do a global search by ID, where we can
208 if ( m[1] == "#" && ret[ret.length-1].getElementById ) {
209 // Optimization for HTML document case
210 var oid = ret[ret.length-1].getElementById(m[2]);
212 // Do a quick check for node name (where applicable) so
213 // that div#foo searches will be really fast
215 (!m[3] || oid.nodeName == m[3].toUpperCase()) ? [oid] : [];
217 // Use the DOM 0 shortcut for the body element
218 } else if ( m[1] == "" && m[2] == "body" ) {
219 ret = r = [ document.body ];
222 // Pre-compile a regular expression to handle class searches
224 var rec = new RegExp("(^|\\s)" + m[2] + "(\\s|$)");
226 // We need to find all descendant elements, it is more
227 // efficient to use getAll() when we are already further down
228 // the tree - we try to recognize that here
229 for ( var i = 0, rl = ret.length; i < rl; i++ )
231 m[1] != "" && ret.length != 1 ?
232 jQuery.getAll( ret[i], [], m[1], m[2], rec ) :
233 ret[i].getElementsByTagName( m[1] != "" || m[0] == "" ? "*" : m[2] )
236 // It's faster to filter by class and be done with it
237 if ( m[1] == "." && ret.length == 1 )
238 r = jQuery.grep( r, function(e) {
239 return rec.test(e.className);
242 // Same with ID filtering
243 if ( m[1] == "#" && ret.length == 1 ) {
244 // Remember, then wipe out, the result set
248 // Then try to find the element with the ID
249 for ( var i = 0, tl = tmp.length; i < tl; i++ )
250 if ( tmp[i].getAttribute("id") == m[2] ) {
259 t = t.replace( re2, "" );
264 // If a selector string still exists
266 // Attempt to filter it
267 var val = jQuery.filter(t,r);
269 t = jQuery.trim(val.t);
273 // Remove the root context
274 if ( ret && ret[0] == context ) ret.shift();
276 // And combine the results
277 jQuery.merge( done, ret );
282 filter: function(t,r,not) {
283 // Look for common filter expressions
284 while ( t && /^[a-z[({<*:.#]/i.test(t) ) {
286 var p = jQuery.parse;
288 for ( var i = 0, pl = p.length; i < pl; i++ ) {
290 // Look for, and replace, string-like sequences
291 // and finally build a regexp out of it
293 "^" + p[i].replace("S", "([a-z*_-][a-z0-9_-]*)"), "i" );
295 var m = re.exec( t );
298 // Re-organize the first match
299 if ( jQuery.expr[ m[1] ]._resort )
300 m = jQuery.expr[ m[1] ]._resort( m );
302 // Remove what we just matched
303 t = t.replace( re, "" );
309 // :not() is a special case that can be optimized by
310 // keeping it out of the expression list
311 if ( m[1] == ":" && m[2] == "not" )
312 r = jQuery.filter(m[3], r, true).r;
314 // Handle classes as a special case (this will help to
315 // improve the speed, as the regexp will only be compiled once)
316 else if ( m[1] == "." ) {
318 var re = new RegExp("(^|\\s)" + m[2] + "(\\s|$)");
319 r = jQuery.grep( r, function(e){
320 return re.test(e.className || '');
323 // Otherwise, find the expression to execute
325 var f = jQuery.expr[m[1]];
326 if ( typeof f != "string" )
327 f = jQuery.expr[m[1]][m[2]];
329 // Build a custom macro to enclose it
330 eval("f = function(a,i){" +
331 ( jQuery.expr[ m[1] ]._prefix || "" ) +
332 "return " + f + "}");
334 // Execute it against the current filter
335 r = jQuery.grep( r, f, not );
339 // Return an array of filtered elements (r)
340 // and the modified expression string (t)
341 return { r: r, t: t };
344 getAll: function( o, r, token, name, re ) {
345 for ( var s = o.firstChild; s; s = s.nextSibling )
346 if ( s.nodeType == 1 ) {
350 add = s.className && re.test(s.className);
351 else if ( token == "#" )
352 add = s.getAttribute('id') == name;
357 if ( token == "#" && r.length ) break;
360 jQuery.getAll( s, r, token, name, re );
367 * All ancestors of a given element.
371 * @type Array<Element>
372 * @param Element elem The element to find the ancestors of.
373 * @cat DOM/Traversing
375 parents: function( elem ){
377 var cur = elem.parentNode;
378 while ( cur && cur != document ) {
380 cur = cur.parentNode;
386 * A handy, and fast, way to traverse in a particular direction and find
387 * a specific element.
392 * @param DOMElement cur The element to search from.
393 * @param Number|String num The Nth result to match. Can be a number or a string (like 'even' or 'odd').
394 * @param String dir The direction to move in (pass in something like 'previousSibling' or 'nextSibling').
395 * @cat DOM/Traversing
397 nth: function(cur,result,dir){
398 result = result || 1;
400 for ( ; cur; cur = cur[dir] ) {
401 if ( cur.nodeType == 1 ) num++;
402 if ( num == result || result == "even" && num % 2 == 0 && num > 1 ||
403 result == "odd" && num % 2 == 1 ) return cur;
408 * All elements on a specified axis.
413 * @param Element elem The element to find all the siblings of (including itself).
414 * @cat DOM/Traversing
416 sibling: function( n, elem ) {
419 for ( ; n; n = n.nextSibling ) {
420 if ( n.nodeType == 1 && (!elem || n != elem) )