X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fselector%2Fselector.js;h=3bd6b3439bc43df8531428a4dfad66b276a65194;hb=ec2b688920cf38f5bb2487b8a1897f7eb7557248;hp=1bad21b635e1f2aeac63274704d1b24d6b606ffd;hpb=f2ff0db0320cfab0d856ff68d012cb843c97b600;p=jquery.git diff --git a/src/selector/selector.js b/src/selector/selector.js index 1bad21b..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 @@ -14,7 +14,7 @@ jQuery.extend({ odd: "i%2", // Child Checks - "nth-child": "jQuery.nth(a.parentNode.firstChild,m[3],'nextSibling')==a", + "nth-child": "jQuery.nth(a.parentNode.firstChild,m[3],'nextSibling',a)==a", "first-child": "jQuery.nth(a.parentNode.firstChild,1,'nextSibling')==a", "last-child": "jQuery.nth(a.parentNode.lastChild,1,'previousSibling')==a", "only-child": "jQuery.sibling(a.parentNode.firstChild).length==1", @@ -27,14 +27,14 @@ 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: '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"', // Form attributes enabled: "!a.disabled", disabled: "a.disabled", checked: "a.checked", - selected: "a.selected || jQuery.attr(a, 'selected')", + selected: "a.selected||jQuery.attr(a,'selected')", // Form elements text: "a.type=='text'", @@ -45,21 +45,21 @@ 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])", "@": { "=": "z==m[4]", "!=": "z!=m[4]", - "^=": "z && !z.indexOf(m[4])", - "$=": "z && z.substr(z.length - m[4].length,m[4].length)==m[4]", - "*=": "z && z.indexOf(m[4])>=0", + "^=": "z&&!z.indexOf(m[4])", + "$=": "z&&z.substr(z.length - m[4].length,m[4].length)==m[4]", + "*=": "z&&z.indexOf(m[4])>=0", "": "z", _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" }, @@ -67,25 +67,25 @@ jQuery.extend({ // The regular expressions that power the parsing engine parse: [ // Match: [@value='test'], [@foo] - "\\[ *(@)S *([!*$^=]*) *('?\"?)(.*?)\\4 *\\]", + /^\[ *(@)([a-z0-9_-]*) *([!*$^=]*) *('?"?)(.*?)\4 *\]/i, // Match: [div], [div p] - "(\\[)\\s*(.*?(\\[.*?\\])?[^[]*?)\\s*\\]", + /^(\[)\s*(.*?(\[.*?\])?[^[]*?)\s*\]/, // Match: :contains('foo') - "(:)S\\(\"?'?(.*?(\\(.*?\\))?[^(]*?)\"?'?\\)", + /^(:)([a-z0-9_-]*)\("?'?(.*?(\(.*?\))?[^(]*?)"?'?\)/i, // Match: :even, :last-chlid - "([:.#]*)S" + /^([:.#]*)([a-z0-9_*-]*)/i ], token: [ - "\\.\\.|/\\.\\.", "a.parentNode", - ">|/", "jQuery.sibling(a.firstChild)", - "\\+", "jQuery.nth(a,2,'nextSibling')", - "~", function(a){ + /^(\/?\.\.)/, "a.parentNode", + /^(>|\/)/, "jQuery.sibling(a.firstChild)", + /^(\+)/, "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); } ], @@ -93,16 +93,11 @@ jQuery.extend({ var old, cur = []; while ( expr && expr != old ) { -console.log( cur, expr, elems, not ); + old = expr; var f = jQuery.filter( expr, elems, not ); expr = f.t.replace(/^\s*,\s*/, "" ); - - if ( not ) - cur = elems = f.r; - else - cur = jQuery.merge( cur, f.r ); + cur = not ? elems = f.r : jQuery.merge( cur, f.r ); } -console.log( "DONE", cur, expr, elems, not ); return cur; }, @@ -131,7 +126,7 @@ console.log( "DONE", cur, expr, elems, not ); 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 ) @@ -158,26 +153,28 @@ console.log( "DONE", cur, expr, elems, not ); if ( m ) { // Perform our own iteration and filter - for ( var i = 0, rl = ret.length; i < rl; i++ ) - for ( var c = ret[i].firstChild; c; c = c.nextSibling ) - if ( c.nodeType == 1 && ( c.nodeName == m[1].toUpperCase() || m[1] == "*" ) ) + jQuery.each( ret, function(){ + for ( var c = this.firstChild; c; c = c.nextSibling ) + 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 for ( var i = 0; i < jQuery.token.length; i += 2 ) { // Attempt to match each, individual, token in // the specified order - var re = new RegExp("^(" + jQuery.token[i] + ")"); + var re = jQuery.token[i]; 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.token[i+1].constructor == Function ? + r = ret = jQuery.map( ret, jQuery.isFunction( jQuery.token[i+1] ) ? jQuery.token[i+1] : function(a){ return eval(jQuery.token[i+1]); }); @@ -193,12 +190,12 @@ console.log( "DONE", cur, expr, elems, not ); // matched a token if ( t && !foundToken ) { // Handle multiple expressions - if ( !t.indexOf(",") || !t.indexOf("|") ) { - // Clean teh result set + if ( !t.indexOf(",") ) { + // Clean the result set 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]; @@ -222,15 +219,21 @@ console.log( "DONE", cur, expr, elems, not ); 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 @@ -240,31 +243,36 @@ console.log( "DONE", cur, expr, elems, not ); // 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++ ) - jQuery.merge( r, - m[1] != "" && ret.length != 1 ? - jQuery.getAll( ret[i], [], m[1], m[2], rec ) : - ret[i].getElementsByTagName( m[1] != "" || m[0] == "" ? "*" : m[2] ) - ); + 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 ( tag == "*" && ret[i].nodeName.toLowerCase() == "object" ) + tag = "param"; + + 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 = []; // Then try to find the element with the ID - for ( var i = 0, tl = tmp.length; i < tl; i++ ) - if ( tmp[i].getAttribute("id") == m[2] ) { - r = [ tmp[i] ]; - break; + jQuery.each( tmp, function(){ + if ( this.getAttribute("id") == m[2] ) { + r = [ this ]; + return false; } + }); } ret = r; @@ -288,7 +296,7 @@ console.log( "DONE", cur, expr, elems, not ); if ( ret && ret[0] == context ) ret.shift(); // And combine the results - jQuery.merge( done, ret ); + done = jQuery.merge( done, ret ); return done; }, @@ -297,28 +305,25 @@ console.log( "DONE", cur, expr, elems, not ); // Look for common filter expressions while ( t && /^[a-z[({<*:.#]/i.test(t) ) { - var p = jQuery.parse; + var p = jQuery.parse, m; - for ( var i = 0, pl = p.length; i < pl; i++ ) { + jQuery.each( p, function(i,re){ // Look for, and replace, string-like sequences // and finally build a regexp out of it - var re = new RegExp( - "^" + p[i].replace("S", "([a-z*_-][a-z0-9_-]*)"), "i" ); - - var m = re.exec( t ); + m = re.exec( t ); if ( m ) { + // Remove what we just matched + t = t.substring( m[0].length ); + // Re-organize the first match if ( jQuery.expr[ m[1] ]._resort ) m = jQuery.expr[ m[1] ]._resort( m ); - // Remove what we just matched - t = t.replace( re, "" ); - - break; + return false; } - } + }); // :not() is a special case that can be optimized by // keeping it out of the expression list @@ -331,7 +336,7 @@ console.log( "DONE", cur, expr, elems, not ); var re = new RegExp("(^|\\s)" + m[2] + "(\\s|$)"); r = jQuery.grep( r, function(e){ - return re.test(e.className || ''); + return re.test(e.className || ""); }, not); // Otherwise, find the expression to execute @@ -354,28 +359,6 @@ console.log( "DONE", cur, expr, elems, not ); // 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. @@ -404,17 +387,17 @@ console.log( "DONE", cur, expr, elems, not ); * @name $.nth * @type DOMElement * @param DOMElement cur The element to search from. - * @param Number|String num The Nth result to match. Can be a number or a string (like 'even' or 'odd'). + * @param String|Number num The Nth result to match. Can be a number or a string (like 'even' or 'odd'). * @param String dir The direction to move in (pass in something like 'previousSibling' or 'nextSibling'). * @cat DOM/Traversing */ - nth: function(cur,result,dir){ + nth: function(cur,result,dir,elem){ result = result || 1; var num = 0; for ( ; cur; cur = cur[dir] ) { if ( cur.nodeType == 1 ) num++; - if ( num == result || result == "even" && num % 2 == 0 && num > 1 || - result == "odd" && num % 2 == 1 ) return cur; + if ( num == result || result == "even" && num % 2 == 0 && num > 1 && cur == elem || + result == "odd" && num % 2 == 1 && cur == elem ) return cur; } },