X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fselector%2Fselector.js;h=6ad8e5cca8a85ffa46edf1e53969d3e82c459860;hb=7c6100f5ed840b49e8e0de1f9eb90ea2ef1c47ef;hp=cc37584f57b1e3b9e600cb1ccacb52edec993635;hpb=25b424134f9927a5bf0bab5cba836a0aa6c3cfc1;p=jquery.git diff --git a/src/selector/selector.js b/src/selector/selector.js index cc37584..6ad8e5c 100644 --- a/src/selector/selector.js +++ b/src/selector/selector.js @@ -27,8 +27,8 @@ jQuery.extend({ contains: "jQuery.fn.text.apply([a]).indexOf(m[3])>=0", // Visibility - visible: 'a.type!="hidden"&&jQuery.css(a,"display")!="none"&&jQuery.css(a,"visibility")!="hidden"', - hidden: 'a.type=="hidden"||jQuery.css(a,"display")=="none"||jQuery.css(a,"visibility")=="hidden"', + visible: '"hidden"!=a.type&&jQuery.css(a,"display")!="none"&&jQuery.css(a,"visibility")!="hidden"', + hidden: '"hidden"==a.type||jQuery.css(a,"display")=="none"||jQuery.css(a,"visibility")=="hidden"', // Form attributes enabled: "!a.disabled", @@ -37,15 +37,15 @@ jQuery.extend({ selected: "a.selected||jQuery.attr(a,'selected')", // Form elements - text: "a.type=='text'", - radio: "a.type=='radio'", - checkbox: "a.type=='checkbox'", - file: "a.type=='file'", - password: "a.type=='password'", - submit: "a.type=='submit'", - image: "a.type=='image'", - reset: "a.type=='reset'", - button: 'a.type=="button"||jQuery.nodeName(a,"button")', + text: "'text'==a.type", + radio: "'radio'==a.type", + checkbox: "'checkbox'==a.type", + file: "'file'==a.type", + password: "'password'==a.type", + submit: "'submit'==a.type", + image: "'image'==a.type", + reset: "'reset'==a.type", + button: '"button"==a.type||jQuery.nodeName(a,"button")', input: "/input|select|textarea|button/i.test(a.nodeName)" }, ".": "jQuery.className.has(a,m[2])", @@ -59,24 +59,25 @@ jQuery.extend({ _resort: function(m){ return ["", m[1], m[3], m[2], m[5]]; }, - _prefix: "z=a[m[3]];if(!z||/href|src/.test(m[3]))z=jQuery.attr(a,m[3]);" + _prefix: "var z=a[m[3]];if(!z||/href|src/.test(m[3]))z=jQuery.attr(a,m[3]);" }, - "[": "parseInt(m[2])?jQuery.nth(a.parentNode.firstChild,parseInt(m[2]),'nextSibling',a)==a:jQuery.find(m[2],a).length" + "[": "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 = new RegExp("^[/>]\\s*(" + jQuery.chars + "+)"); 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, "" ) ); @@ -192,7 +191,7 @@ jQuery.extend({ // Handle multiple expressions if ( !t.indexOf(",") ) { // Clean the result set - if ( ret[0] == context ) ret.shift(); + if ( context == ret[0] ) ret.shift(); // Merge the result sets done = jQuery.merge( done, ret ); @@ -205,7 +204,7 @@ jQuery.extend({ } else { // Optomize for the case nodeName#idName - var re2 = /^(\w+)(#)((?:[\w\u0128-\uFFFF*-]|\\.)+)/i; + var re2 = new RegExp("^(" + jQuery.chars + "+)(#)(" + jQuery.chars + "+)"); var m = re2.exec(t); // Re-organize the results, so that they're consistent @@ -215,7 +214,7 @@ 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); } @@ -230,18 +229,16 @@ 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] ) + // also check to insure id is a string to avoid selecting an element with the name of 'id' inside a form + if ( (jQuery.browser.msie||jQuery.browser.opera) && oid && typeof oid.id == "string" && 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 { - // 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]; @@ -254,23 +251,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 jQuery.className.has(e, m[2]); - }); + 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; @@ -296,7 +290,7 @@ jQuery.extend({ ret = []; // Remove the root context - if ( ret && ret[0] == context ) + if ( ret && context == ret[0] ) ret.shift(); // And combine the results @@ -305,6 +299,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; @@ -314,11 +319,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,18 +332,22 @@ jQuery.extend({ m[2] = m[2].replace(/\\/g, ""); - return false; + 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; + // 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 { var f = jQuery.expr[m[1]]; @@ -400,8 +406,9 @@ jQuery.extend({ for ( ; cur; cur = cur[dir] ) { if ( cur.nodeType == 1 ) num++; if ( num == result || result == "even" && num % 2 == 0 && num > 1 && cur == elem || - result == "odd" && num % 2 == 1 && cur == elem ) return cur; + result == "odd" && num % 2 == 1 && cur == elem ) break; } + return cur; }, /**