X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fselector%2Fselector.js;h=3bd6b3439bc43df8531428a4dfad66b276a65194;hb=ec2b688920cf38f5bb2487b8a1897f7eb7557248;hp=6a5760a70ab20b65e25e980dcb69cbb3a3a35fe7;hpb=34355cd6986d5939dc711c531263cb561cba5f24;p=jquery.git diff --git a/src/selector/selector.js b/src/selector/selector.js index 6a5760a..3bd6b34 100644 --- a/src/selector/selector.js +++ b/src/selector/selector.js @@ -1,6 +1,6 @@ jQuery.extend({ expr: { - "": "m[2]=='*'||a.nodeName.toUpperCase()==m[2].toUpperCase()", + "": "m[2]=='*'||jQuery.nodeName(a,m[2])", "#": "a.getAttribute('id')==m[2]", ":": { // Position Checks @@ -45,7 +45,7 @@ jQuery.extend({ submit: "a.type=='submit'", image: "a.type=='image'", reset: "a.type=='reset'", - button: 'a.type=="button"||a.nodeName=="BUTTON"', + button: 'a.type=="button"||jQuery.nodeName(a,"button")', input: "/input|select|textarea|button/i.test(a.nodeName)" }, ".": "jQuery.className.has(a,m[2])", @@ -59,7 +59,7 @@ jQuery.extend({ _resort: function(m){ return ["", m[1], m[3], m[2], m[5]]; }, - _prefix: "z=a[m[3]]||jQuery.attr(a,m[3]);" + _prefix: "z=a[m[3]];if(!z||/href|src/.test(m[3]))z=jQuery.attr(a,m[3]);" }, "[": "jQuery.find(m[2],a).length" }, @@ -85,7 +85,7 @@ jQuery.extend({ /^(\+)/, "jQuery.nth(a,2,'nextSibling')", /^(~)/, function(a){ var s = jQuery.sibling(a.parentNode.firstChild); - return s.slice(0, jQuery.inArray(a,s)); + return s.slice(jQuery.inArray(a,s) + 1); } ], @@ -126,7 +126,7 @@ jQuery.extend({ t = t.substr(2,t.length); // And the / root expression - } else if ( !t.indexOf("/") ) { + } else if ( !t.indexOf("/") && !context.ownerDocument ) { context = context.documentElement; t = t.substr(1,t.length); if ( t.indexOf("/") >= 1 ) @@ -155,12 +155,13 @@ jQuery.extend({ // Perform our own iteration and filter jQuery.each( ret, function(){ for ( var c = this.firstChild; c; c = c.nextSibling ) - if ( c.nodeType == 1 && ( c.nodeName == m[1].toUpperCase() || m[1] == "*" ) ) + if ( c.nodeType == 1 && ( jQuery.nodeName(c, m[1]) || m[1] == "*" ) ) r.push( c ); }); ret = r; - t = jQuery.trim( t.replace( re, "" ) ); + t = t.replace( re, "" ); + if ( t.indexOf(" ") == 0 ) continue; foundToken = true; } else { // Look for pre-defined expression tokens @@ -194,7 +195,7 @@ jQuery.extend({ if ( ret[0] == context ) ret.shift(); // Merge the result sets - jQuery.merge( done, ret ); + done = jQuery.merge( done, ret ); // Reset the context r = ret = [context]; @@ -218,15 +219,21 @@ jQuery.extend({ m = re2.exec(t); } + var last = ret[ret.length-1]; + // Try to do a global search by ID, where we can - if ( m[1] == "#" && ret[ret.length-1].getElementById ) { + if ( m[1] == "#" && last && last.getElementById ) { // Optimization for HTML document case - var oid = ret[ret.length-1].getElementById(m[2]); + var oid = last.getElementById(m[2]); + + // 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] ) + oid = jQuery('[@id="'+m[2]+'"]', last)[0]; // Do a quick check for node name (where applicable) so // that div#foo searches will be really fast - ret = r = oid && - (!m[3] || oid.nodeName == m[3].toUpperCase()) ? [oid] : []; + ret = r = oid && (!m[3] || jQuery.nodeName(oid, m[3])) ? [oid] : []; } else { // Pre-compile a regular expression to handle class searches @@ -236,29 +243,25 @@ jQuery.extend({ // 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 - jQuery.each( ret, function(){ + for ( var i = 0, rl = ret.length; i < rl; i++ ) { // Grab the tag name being searched for var tag = m[1] != "" || m[0] == "" ? "*" : m[2]; // Handle IE7 being really dumb about s - if ( this.nodeName.toUpperCase() == "OBJECT" && tag == "*" ) + if ( tag == "*" && ret[i].nodeName.toLowerCase() == "object" ) tag = "param"; - jQuery.merge( r, - m[1] != "" && ret.length != 1 ? - jQuery.getAll( this, [], m[1], m[2], rec ) : - this.getElementsByTagName( tag ) - ); - }); + r = jQuery.merge( r, ret[i].getElementsByTagName( tag )); + } // It's faster to filter by class and be done with it - if ( m[1] == "." && ret.length == 1 ) + if ( m[1] == "." ) r = jQuery.grep( r, function(e) { return rec.test(e.className); }); // Same with ID filtering - if ( m[1] == "#" && ret.length == 1 ) { + if ( m[1] == "#" ) { // Remember, then wipe out, the result set var tmp = r; r = []; @@ -293,7 +296,7 @@ jQuery.extend({ if ( ret && ret[0] == context ) ret.shift(); // And combine the results - jQuery.merge( done, ret ); + done = jQuery.merge( done, ret ); return done; }, @@ -356,28 +359,6 @@ jQuery.extend({ // and the modified expression string (t) return { r: r, t: t }; }, - - getAll: function( o, r, token, name, re ) { - for ( var s = o.firstChild; s; s = s.nextSibling ) - if ( s.nodeType == 1 ) { - var add = true; - - if ( token == "." ) - add = s.className && re.test(s.className); - else if ( token == "#" ) - add = s.getAttribute("id") == name; - - if ( add ) - r.push( s ); - - if ( token == "#" && r.length ) break; - - if ( s.firstChild ) - jQuery.getAll( s, r, token, name, re ); - } - - return r; - }, /** * All ancestors of a given element.