X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;ds=sidebyside;f=src%2Fselector%2Fselector.js;h=cc37584f57b1e3b9e600cb1ccacb52edec993635;hb=4e7c9b08a0fb6142755e39cfaae2f8330831008c;hp=3bd6b3439bc43df8531428a4dfad66b276a65194;hpb=ec2b688920cf38f5bb2487b8a1897f7eb7557248;p=jquery.git diff --git a/src/selector/selector.js b/src/selector/selector.js index 3bd6b34..cc37584 100644 --- a/src/selector/selector.js +++ b/src/selector/selector.js @@ -61,22 +61,22 @@ 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] - /^\[ *(@)([a-z0-9_-]*) *([!*$^=]*) *('?"?)(.*?)\4 *\]/i, + /^\[ *(@)([\w-]+) *([!*$^=]*) *('?"?)(.*?)\4 *\]/i, // Match: [div], [div p] /^(\[)\s*(.*?(\[.*?\])?[^[]*?)\s*\]/, // Match: :contains('foo') - /^(:)([a-z0-9_-]*)\("?'?(.*?(\(.*?\))?[^(]*?)"?'?\)/i, + /^(:)([\w-]+)\("?'?(.*?(\(.*?\))?[^(]*?)"?'?\)/i, - // Match: :even, :last-chlid - /^([:.#]*)([a-z0-9_*-]*)/i + // Match: :even, :last-chlid, #id, .class + /^([:.#]*)((?:[\w\u0128-\uFFFF*-]|\\.)+)/i ], token: [ @@ -134,7 +134,7 @@ jQuery.extend({ } // Initialize the search - var ret = [context], done = [], last = null; + var ret = [context], done = [], last; // Continue while a selector expression exists, and while // we're no longer looping upon ourselves @@ -142,13 +142,13 @@ jQuery.extend({ var r = []; last = t; - t = jQuery.trim(t).replace( /^\/\//i, "" ); + t = jQuery.trim(t).replace( /^\/\//, "" ); var foundToken = false; // An attempt at speeding up child selectors that // point to a specific element tag - var re = /^[\/>]\s*([a-z0-9*-]+)/i; + var re = /^[\/>]\s*([\w*-]+)/i; var m = re.exec(t); if ( m ) { @@ -205,7 +205,7 @@ jQuery.extend({ } else { // Optomize for the case nodeName#idName - var re2 = /^([a-z0-9_-]+)(#)([a-z0-9\\*_-]*)/i; + var re2 = /^(\w+)(#)((?:[\w\u0128-\uFFFF*-]|\\.)+)/i; var m = re2.exec(t); // Re-organize the results, so that they're consistent @@ -215,31 +215,29 @@ jQuery.extend({ } else { // Otherwise, do a traditional filter check for // ID, class, and element selectors - re2 = /^([#.]?)([a-z0-9\\*_-]*)/i; + re2 = /^([#.]?)((?:[\w\u0128-\uFFFF*-]|\\.)*)/i; m = re2.exec(t); } - var last = ret[ret.length-1]; + m[2] = m[2].replace(/\\/g, ""); + + var elem = ret[ret.length-1]; // Try to do a global search by ID, where we can - if ( m[1] == "#" && last && last.getElementById ) { + if ( m[1] == "#" && elem && elem.getElementById ) { // Optimization for HTML document case - var oid = last.getElementById(m[2]); + var oid = elem.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]; + 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 @@ -257,7 +255,7 @@ 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); + return jQuery.className.has(e, m[2]); }); // Same with ID filtering @@ -292,8 +290,14 @@ jQuery.extend({ } } + // An error occurred with the selector; + // just return an empty set instead + if ( t ) + ret = []; + // Remove the root context - if ( ret && ret[0] == context ) ret.shift(); + if ( ret && ret[0] == context ) + ret.shift(); // And combine the results done = jQuery.merge( done, ret ); @@ -302,8 +306,11 @@ jQuery.extend({ }, filter: function(t,r,not) { + var last; + // Look for common filter expressions - while ( t && /^[a-z[({<*:.#]/i.test(t) ) { + while ( t && t != last ) { + last = t; var p = jQuery.parse, m; @@ -321,26 +328,22 @@ jQuery.extend({ if ( jQuery.expr[ m[1] ]._resort ) m = jQuery.expr[ m[1] ]._resort( m ); + m[2] = m[2].replace(/\\/g, ""); + return false; } }); + if ( !m ) + continue; + // :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); - // 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]];