X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fselector%2Fselector.js;h=48c6e1573fb739557bc094ef7293c78856c000f8;hb=9c94ef4c411867d38f301ccbf406af21e277188c;hp=9d36ca7077fed146a9e4a0bc126d47ec7f040115;hpb=af79bb1f255433d553d8d9dde0306cc340903c1a;p=jquery.git diff --git a/src/selector/selector.js b/src/selector/selector.js index 9d36ca7..48c6e15 100644 --- a/src/selector/selector.js +++ b/src/selector/selector.js @@ -61,22 +61,23 @@ jQuery.extend({ }, _prefix: "z=a[m[3]];if(!z||/href|src/.test(m[3]))z=jQuery.attr(a,m[3]);" }, - "[": "jQuery.find(m[2],a).length" + "[": "parseInt(m[2])?jQuery.nth(a.parentNode.firstChild,parseInt(m[2]),'nextSibling',a)==a:jQuery.find(m[2],a).length" }, // The regular expressions that power the parsing engine parse: [ // Match: [@value='test'], [@foo] - /^\[ *(@)([\w-]+) *([!*$^=]*) *('?"?)(.*?)\4 *\]/i, + /^\[ *(@)([\w-]+) *([!*$^=]*) *('?"?)(.*?)\4 *\]/, // Match: [div], [div p] /^(\[)\s*(.*?(\[.*?\])?[^[]*?)\s*\]/, // Match: :contains('foo') - /^(:)([\w-]+)\("?'?(.*?(\(.*?\))?[^(]*?)"?'?\)/i, + /^(:)([\w-]+)\("?'?(.*?(\(.*?\))?[^(]*?)"?'?\)/, // Match: :even, :last-chlid, #id, .class - /^([:.#]*)([\w\u0128-\uFFFF*-]+)/i + new RegExp("^([:.#]*)(" + + ( jQuery.chars = "(?:[\\w\u0128-\uFFFF*-]|\\\\.)" ) + "+)") ], token: [ @@ -148,16 +149,15 @@ jQuery.extend({ // An attempt at speeding up child selectors that // point to a specific element tag - var re = /^[\/>]\s*([\w*-]+)/i; + var re = /^[\/>]\s*([\w*-]+)/; var m = re.exec(t); if ( m ) { // Perform our own iteration and filter - jQuery.each( ret, function(){ - for ( var c = this.firstChild; c; c = c.nextSibling ) - if ( c.nodeType == 1 && ( jQuery.nodeName(c, m[1]) || m[1] == "*" ) ) + for ( var i = 0; ret[i]; i++ ) + for ( var c = ret[i].firstChild; c; c = c.nextSibling ) + if ( c.nodeType == 1 && ( m[1] == "*" || jQuery.nodeName(c, m[1]) ) ) r.push( c ); - }); ret = r; t = t.replace( re, "" ); @@ -165,18 +165,17 @@ jQuery.extend({ foundToken = true; } else { // Look for pre-defined expression tokens - for ( var i = 0; i < jQuery.token.length; i += 2 ) { + for ( var i = 0, tl = jQuery.token.length; i < tl; i += 2 ) { // Attempt to match each, individual, token in // the specified order - var re = jQuery.token[i]; + var re = jQuery.token[i], fn = jQuery.token[i+1]; var m = re.exec(t); // If the token match was found if ( m ) { // Map it against the token's handler - r = ret = jQuery.map( ret, jQuery.isFunction( jQuery.token[i+1] ) ? - jQuery.token[i+1] : - function(a){ return eval(jQuery.token[i+1]); }); + r = ret = jQuery.map( ret, jQuery.isFunction( fn ) ? + fn : new Function( "a", "return " + fn ) ); // And remove the token t = jQuery.trim( t.replace( re, "" ) ); @@ -205,7 +204,7 @@ jQuery.extend({ } else { // Optomize for the case nodeName#idName - var re2 = /^(\w+)(#)([\w\u0128-\uFFFF*-]+)/i; + var re2 = new RegExp("^(\\w+)(#)(" + jQuery.chars + "+)"); var m = re2.exec(t); // Re-organize the results, so that they're consistent @@ -215,10 +214,12 @@ jQuery.extend({ } else { // Otherwise, do a traditional filter check for // ID, class, and element selectors - re2 = /^([#.]?)([\w\u0128-\uFFFF*-]*)/i; + re2 = new RegExp("^([#.]?)(" + jQuery.chars + "*)"); m = re2.exec(t); } + m[2] = m[2].replace(/\\/g, ""); + var elem = ret[ret.length-1]; // Try to do a global search by ID, where we can @@ -228,22 +229,15 @@ jQuery.extend({ // Do a quick check for the existence of the actual ID attribute // to avoid selecting by the name attribute in IE - if ( jQuery.browser.msie && oid && oid.id != m[2] ) + if ( (jQuery.browser.msie||jQuery.browser.opera) && oid && oid.id != m[2] ) oid = jQuery('[@id="'+m[2]+'"]', elem)[0]; // Do a quick check for node name (where applicable) so // that div#foo searches will be really fast ret = r = oid && (!m[3] || jQuery.nodeName(oid, m[3])) ? [oid] : []; - } else { - // Pre-compile a regular expression to handle class searches - if ( m[1] == "." ) - var rec = new RegExp("(^|\\s)" + m[2] + "(\\s|$)"); - - // We need to find all descendant elements, it is more - // efficient to use getAll() when we are already further down - // the tree - we try to recognize that here - for ( var i = 0, rl = ret.length; i < rl; i++ ) { + // We need to find all descendant elements + for ( var i = 0; ret[i]; i++ ) { // Grab the tag name being searched for var tag = m[1] != "" || m[0] == "" ? "*" : m[2]; @@ -256,23 +250,20 @@ jQuery.extend({ // It's faster to filter by class and be done with it if ( m[1] == "." ) - r = jQuery.grep( r, function(e) { - return rec.test(e.className); - }); + r = jQuery.classFilter( r, m[2] ); // Same with ID filtering if ( m[1] == "#" ) { - // Remember, then wipe out, the result set - var tmp = r; - r = []; - - // Then try to find the element with the ID - jQuery.each( tmp, function(){ - if ( this.getAttribute("id") == m[2] ) { - r = [ this ]; - return false; + var tmp = []; + + // Try to find the element with the ID + for ( var i = 0; r[i]; i++ ) + if ( r[i].getAttribute("id") == m[2] ) { + tmp = [ r[i] ]; + break; } - }); + + r = tmp; } ret = r; @@ -307,6 +298,17 @@ jQuery.extend({ return done; }, + classFilter: function(r,m,not){ + m = " " + m + " "; + var tmp = []; + for ( var i = 0; r[i]; i++ ) { + var pass = (" " + r[i].className + " ").indexOf( m ) >= 0; + if ( !not && pass || not && !pass ) + tmp.push( r[i] ); + } + return tmp; + }, + filter: function(t,r,not) { var last; @@ -316,11 +318,8 @@ jQuery.extend({ var p = jQuery.parse, m; - jQuery.each( p, function(i,re){ - - // Look for, and replace, string-like sequences - // and finally build a regexp out of it - m = re.exec( t ); + for ( var i = 0; p[i]; i++ ) { + m = p[i].exec( t ); if ( m ) { // Remove what we just matched @@ -330,29 +329,26 @@ jQuery.extend({ if ( jQuery.expr[ m[1] ]._resort ) m = jQuery.expr[ m[1] ]._resort( m ); - return false; + m[2] = m[2].replace(/\\/g, ""); + + break; } - }); + } if ( !m ) - continue; + break; // :not() is a special case that can be optimized by // keeping it out of the expression list if ( m[1] == ":" && m[2] == "not" ) r = jQuery.filter(m[3], r, true).r; - // Handle classes as a special case (this will help to - // improve the speed, as the regexp will only be compiled once) - else if ( m[1] == "." ) { - - var re = new RegExp("(^|\\s)" + m[2] + "(\\s|$)"); - r = jQuery.grep( r, function(e){ - return re.test(e.className || ""); - }, not); + // We can get a big speed boost by filtering by class here + else if ( m[1] == "." ) + r = jQuery.classFilter(r, m[2], not); // Otherwise, find the expression to execute - } else { + else { var f = jQuery.expr[m[1]]; if ( typeof f != "string" ) f = jQuery.expr[m[1]][m[2]];