X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fselector%2Fselector.js;h=6ad8e5cca8a85ffa46edf1e53969d3e82c459860;hb=7c6100f5ed840b49e8e0de1f9eb90ea2ef1c47ef;hp=d7fe9065a0ab6a694dd6d30111d888615e222100;hpb=3446c3af764c4d91f3676b214575168b82205e83;p=jquery.git diff --git a/src/selector/selector.js b/src/selector/selector.js index d7fe906..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,9 +59,9 @@ 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 @@ -77,7 +77,7 @@ jQuery.extend({ // Match: :even, :last-chlid, #id, .class new RegExp("^([:.#]*)(" + - ( jQuery.chars = "(?:[\\w\u0128-\uFFFF*-]|\\\\.)" ) + "+)") + ( jQuery.chars = "(?:[\\w\u0128-\uFFFF*_-]|\\\\.)" ) + "+)") ], token: [ @@ -149,16 +149,15 @@ jQuery.extend({ // An attempt at speeding up child selectors that // point to a specific element tag - var re = /^[\/>]\s*([\w*-]+)/; + 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, "" ); @@ -166,7 +165,7 @@ 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], fn = jQuery.token[i+1]; @@ -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 = new RegExp("^(\\w+)(#)(" + jQuery.chars + "+)"); + var re2 = new RegExp("^(" + jQuery.chars + "+)(#)(" + jQuery.chars + "+)"); var m = re2.exec(t); // Re-organize the results, so that they're consistent @@ -230,16 +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||jQuery.browser.opera) && 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 - for ( var i = 0, rl = ret.length; i < rl; i++ ) { + for ( var i = 0; ret[i]; i++ ) { // Grab the tag name being searched for var tag = m[1] != "" || m[0] == "" ? "*" : m[2]; @@ -252,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; @@ -294,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 @@ -303,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; @@ -312,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 @@ -328,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]]; @@ -398,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; }, /**