2 var chars = jQuery.browser.safari && parseInt(jQuery.browser.version) < 417 ?
4 "(?:[\\w\u0128-\uFFFF*_-]|\\\\.)",
5 quickChild = new RegExp("^[/>]\\s*(" + chars + "+)"),
6 quickID = new RegExp("^(" + chars + "+)(#)(" + chars + "+)"),
7 quickClass = new RegExp("^([#.]?)(" + chars + "*)");
11 "": "m[2]=='*'||jQuery.nodeName(a,m[2])",
12 "#": "a.getAttribute('id')==m[2]",
20 last: "i==r.length-1",
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')",
30 parent: "a.firstChild",
31 empty: "!a.firstChild",
34 contains: "(a.textContent||a.innerText||'').indexOf(m[3])>=0",
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"',
41 enabled: "!a.disabled",
42 disabled: "a.disabled",
44 selected: "a.selected||jQuery.attr(a,'selected')",
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)"
58 "[": "jQuery.find(m[2],a).length"
61 // The regular expressions that power the parsing engine
63 // Match: [@value='test'], [@foo]
64 /^\[ *(@)([\w-]+) *([!*$^~=]*) *('?"?)(.*?)\4 *\]/,
66 // Match: [div], [div p]
67 /^(\[)\s*(.*?(\[.*?\])?[^[]*?)\s*\]/,
69 // Match: :contains('foo')
70 /^(:)([\w-]+)\("?'?(.*?(\(.*?\))?[^(]*?)"?'?\)/,
72 // Match: :even, :last-chlid, #id, .class
73 new RegExp("^([:.#]*)(" + chars + "+)")
76 multiFilter: function( expr, elems, not ) {
79 while ( expr && expr != old ) {
81 var f = jQuery.filter( expr, elems, not );
82 expr = f.t.replace(/^\s*,\s*/, "" );
83 cur = not ? elems = f.r : jQuery.merge( cur, f.r );
91 * @type Array<Element>
95 find: function( t, context ) {
96 // Quickly handle non-string expressions
97 if ( typeof t != "string" )
100 // Make sure that the context is a DOM Element
101 if ( context && !context.nodeType )
104 // Set the correct context (if none is provided)
105 context = context || document;
107 // Handle the common XPath // expression
108 if ( !t.indexOf("//") ) {
109 //context = context.documentElement;
110 t = t.substr(2,t.length);
112 // And the / root expression
113 } else if ( !t.indexOf("/") && !context.ownerDocument ) {
114 context = context.documentElement;
115 t = t.substr(1,t.length);
116 if ( t.indexOf("/") >= 1 )
117 t = t.substr(t.indexOf("/"),t.length);
120 // Initialize the search
121 var ret = [context], done = [], last;
123 // Continue while a selector expression exists, and while
124 // we're no longer looping upon ourselves
125 while ( t && last != t ) {
129 t = jQuery.trim(t).replace( /^\/\//, "" );
131 var foundToken = false;
133 // An attempt at speeding up child selectors that
134 // point to a specific element tag
139 var nodeName = m[1].toUpperCase();
141 // Perform our own iteration and filter
142 for ( var i = 0; ret[i]; i++ )
143 for ( var c = ret[i].firstChild; c; c = c.nextSibling )
144 if ( c.nodeType == 1 && (nodeName == "*" || c.nodeName.toUpperCase() == nodeName.toUpperCase()) )
148 t = t.replace( re, "" );
149 if ( t.indexOf(" ") == 0 ) continue;
152 re = /^((\/?\.\.)|([>\/+~]))\s*(\w*)/i;
154 if ( (m = re.exec(t)) != null ) {
157 var nodeName = m[4], mergeNum = jQuery.mergeNum++;
160 for ( var j = 0, rl = ret.length; j < rl; j++ )
161 if ( m.indexOf("..") < 0 ) {
162 var n = m == "~" || m == "+" ? ret[j].nextSibling : ret[j].firstChild;
163 for ( ; n; n = n.nextSibling )
164 if ( n.nodeType == 1 ) {
165 if ( m == "~" && n.mergeNum == mergeNum ) break;
167 if (!nodeName || n.nodeName.toUpperCase() == nodeName.toUpperCase() ) {
168 if ( m == "~" ) n.mergeNum = mergeNum;
172 if ( m == "+" ) break;
175 r.push( ret[j].parentNode );
179 // And remove the token
180 t = jQuery.trim( t.replace( re, "" ) );
185 // See if there's still an expression, and that we haven't already
187 if ( t && !foundToken ) {
188 // Handle multiple expressions
189 if ( !t.indexOf(",") ) {
190 // Clean the result set
191 if ( context == ret[0] ) ret.shift();
193 // Merge the result sets
194 done = jQuery.merge( done, ret );
199 // Touch up the selector string
200 t = " " + t.substr(1,t.length);
203 // Optimize for the case nodeName#idName
207 // Re-organize the results, so that they're consistent
209 m = [ 0, m[2], m[3], m[1] ];
212 // Otherwise, do a traditional filter check for
213 // ID, class, and element selectors
218 m[2] = m[2].replace(/\\/g, "");
220 var elem = ret[ret.length-1];
222 // Try to do a global search by ID, where we can
223 if ( m[1] == "#" && elem && elem.getElementById && !jQuery.isXMLDoc(elem) ) {
224 // Optimization for HTML document case
225 var oid = elem.getElementById(m[2]);
227 // Do a quick check for the existence of the actual ID attribute
228 // to avoid selecting by the name attribute in IE
229 // also check to insure id is a string to avoid selecting an element with the name of 'id' inside a form
230 if ( (jQuery.browser.msie||jQuery.browser.opera) && oid && typeof oid.id == "string" && oid.id != m[2] )
231 oid = jQuery('[@id="'+m[2]+'"]', elem)[0];
233 // Do a quick check for node name (where applicable) so
234 // that div#foo searches will be really fast
235 ret = r = oid && (!m[3] || jQuery.nodeName(oid, m[3])) ? [oid] : [];
237 // We need to find all descendant elements
238 for ( var i = 0; ret[i]; i++ ) {
239 // Grab the tag name being searched for
240 var tag = m[1] != "" || m[0] == "" ? "*" : m[2];
242 // Handle IE7 being really dumb about <object>s
243 if ( tag == "*" && ret[i].nodeName.toLowerCase() == "object" )
246 r = jQuery.merge( r, ret[i].getElementsByTagName( tag ));
249 // It's faster to filter by class and be done with it
251 r = jQuery.classFilter( r, m[2] );
253 // Same with ID filtering
257 // Try to find the element with the ID
258 for ( var i = 0; r[i]; i++ )
259 if ( r[i].getAttribute("id") == m[2] ) {
270 t = t.replace( re2, "" );
275 // If a selector string still exists
277 // Attempt to filter it
278 var val = jQuery.filter(t,r);
280 t = jQuery.trim(val.t);
284 // An error occurred with the selector;
285 // just return an empty set instead
289 // Remove the root context
290 if ( ret && context == ret[0] )
293 // And combine the results
294 done = jQuery.merge( done, ret );
299 classFilter: function(r,m,not){
302 for ( var i = 0; r[i]; i++ ) {
303 var pass = (" " + r[i].className + " ").indexOf( m ) >= 0;
304 if ( !not && pass || not && !pass )
310 filter: function(t,r,not) {
313 // Look for common filter expressions
314 while ( t && t != last ) {
317 var p = jQuery.parse, m;
319 for ( var i = 0; p[i]; i++ ) {
323 // Remove what we just matched
324 t = t.substring( m[0].length );
326 m[2] = m[2].replace(/\\/g, "");
334 // :not() is a special case that can be optimized by
335 // keeping it out of the expression list
336 if ( m[1] == ":" && m[2] == "not" )
337 r = jQuery.filter(m[3], r, true).r;
339 // We can get a big speed boost by filtering by class here
340 else if ( m[1] == "." )
341 r = jQuery.classFilter(r, m[2], not);
343 else if ( m[1] == "@" ) {
344 var tmp = [], type = m[3];
346 for ( var i = 0, rl = r.length; i < rl; i++ ) {
347 var a = r[i], z = a[ jQuery.props[m[2]] || m[2] ];
349 if ( z == null || /href|src|selected/.test(m[2]) )
350 z = jQuery.attr(a,m[2]) || '';
352 if ( (type == "" && !!z ||
353 type == "=" && z == m[5] ||
354 type == "!=" && z != m[5] ||
355 type == "^=" && z && !z.indexOf(m[5]) ||
356 type == "$=" && z.substr(z.length - m[5].length) == m[5] ||
357 (type == "*=" || type == "~=") && z.indexOf(m[5]) >= 0) ^ not )
363 // We can get a speed boost by handling nth-child here
364 } else if ( m[1] == ":" && m[2] == "nth-child" ) {
365 var num = jQuery.mergeNum++, tmp = [],
366 test = /(\d*)n\+?(\d*)/.exec(
367 m[3] == "even" && "2n" || m[3] == "odd" && "2n+1" ||
368 !/\D/.test(m[3]) && "n+" + m[3] || m[3]),
369 first = (test[1] || 1) - 0, last = test[2] - 0;
371 for ( var i = 0, rl = r.length; i < rl; i++ ) {
372 var node = r[i], parentNode = node.parentNode;
374 if ( num != parentNode.mergeNum ) {
377 for ( var n = parentNode.firstChild; n; n = n.nextSibling )
378 if ( n.nodeType == 1 )
381 parentNode.mergeNum = num;
387 if ( last == 0 || node.nodeIndex == last )
389 } else if ( (node.nodeIndex + last) % first == 0 )
398 // Otherwise, find the expression to execute
400 var f = jQuery.expr[m[1]];
401 if ( typeof f != "string" )
402 f = jQuery.expr[m[1]][m[2]];
404 // Build a custom macro to enclose it
405 eval("f = function(a,i){return " + f + "}");
407 // Execute it against the current filter
408 r = jQuery.grep( r, f, not );
412 // Return an array of filtered elements (r)
413 // and the modified expression string (t)
414 return { r: r, t: t };
418 * All ancestors of a given element.
422 * @type Array<Element>
423 * @param Element elem The element to find the ancestors of.
424 * @cat DOM/Traversing
426 parents: function( elem ){
428 var cur = elem.parentNode;
429 while ( cur && cur != document ) {
431 cur = cur.parentNode;
437 * A handy, and fast, way to traverse in a particular direction and find
438 * a specific element.
443 * @param DOMElement cur The element to search from.
444 * @param String|Number num The Nth result to match. Can be a number or a string (like 'even' or 'odd').
445 * @param String dir The direction to move in (pass in something like 'previousSibling' or 'nextSibling').
446 * @cat DOM/Traversing
448 nth: function(cur,result,dir,elem){
449 result = result || 1;
452 for ( ; cur; cur = cur[dir] )
453 if ( cur.nodeType == 1 && ++num == result )
460 * All elements on a specified axis.
465 * @param Element elem The element to find all the siblings of (including itself).
466 * @cat DOM/Traversing
468 sibling: function( n, elem ) {
471 for ( ; n; n = n.nextSibling ) {
472 if ( n.nodeType == 1 && (!elem || n != elem) )