Added support for a new :header psuedo-selector (only selects H1-H6 elements).
[jquery.git] / src / selector / selector.js
index 4a90bb9..f75bae3 100644 (file)
@@ -1,3 +1,11 @@
+
+var chars = jQuery.browser.safari && parseInt(jQuery.browser.version) < 417 ?
+               "(?:[\\w*_-]|\\\\.)" :
+               "(?:[\\w\u0128-\uFFFF*_-]|\\\\.)",
+       quickChild = new RegExp("^[/>]\\s*(" + chars + "+)"),
+       quickID = new RegExp("^(" + chars + "+)(#)(" + chars + "+)"),
+       quickClass = new RegExp("^([#.]?)(" + chars + "*)");
+
 jQuery.extend({
        expr: {
                "": "m[2]=='*'||jQuery.nodeName(a,m[2])",
@@ -45,8 +53,15 @@ jQuery.extend({
                        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)"
+                       input: "/input|select|textarea|button/i.test(a.nodeName)",
+
+                       // :has()
+                       has: "jQuery.find(m[3],a).length",
+
+                       // :header
+                       header: "/h\\d/i.test(a.nodeName)"
                },
+               // DEPRECATED
                "[": "jQuery.find(m[2],a).length"
        },
        
@@ -55,6 +70,7 @@ jQuery.extend({
                // Match: [@value='test'], [@foo]
                /^\[ *(@)([\w-]+) *([!*$^~=]*) *('?"?)(.*?)\4 *\]/,
 
+               // DEPRECATED
                // Match: [div], [div p]
                /^(\[)\s*(.*?(\[.*?\])?[^[]*?)\s*\]/,
 
@@ -62,8 +78,7 @@ jQuery.extend({
                /^(:)([\w-]+)\("?'?(.*?(\(.*?\))?[^(]*?)"?'?\)/,
 
                // Match: :even, :last-chlid, #id, .class
-               new RegExp("^([:.#]*)(" + 
-                       ( jQuery.chars = jQuery.browser.safari && jQuery.browser.version < "3.0.0" ? "\\w" : "(?:[\\w\u0128-\uFFFF*_-]|\\\\.)" ) + "+)")
+               new RegExp("^([:.#]*)(" + chars + "+)")
        ],
 
        multiFilter: function( expr, elems, not ) {
@@ -97,11 +112,13 @@ jQuery.extend({
                // Set the correct context (if none is provided)
                context = context || document;
 
+               // DEPRECATED
                // Handle the common XPath // expression
                if ( !t.indexOf("//") ) {
-                       context = context.documentElement;
+                       //context = context.documentElement;
                        t = t.substr(2,t.length);
 
+               // DEPRECATED
                // And the / root expression
                } else if ( !t.indexOf("/") && !context.ownerDocument ) {
                        context = context.documentElement;
@@ -119,13 +136,14 @@ jQuery.extend({
                        var r = [];
                        last = t;
 
+                       // DEPRECATED
                        t = jQuery.trim(t).replace( /^\/\//, "" );
 
                        var foundToken = false;
 
                        // An attempt at speeding up child selectors that
                        // point to a specific element tag
-                       var re = new RegExp("^[/>]\\s*(" + jQuery.chars + "+)");
+                       var re = quickChild;
                        var m = re.exec(t);
 
                        if ( m ) {
@@ -142,7 +160,8 @@ jQuery.extend({
                                if ( t.indexOf(" ") == 0 ) continue;
                                foundToken = true;
                        } else {
-                               re = /^((\/?\.\.)|([>\/+~]))\s*([a-z]*)/i;
+                               // (.. and /) DEPRECATED
+                               re = /^((\/?\.\.)|([>\/+~]))\s*(\w*)/i;
 
                                if ( (m = re.exec(t)) != null ) {
                                        r = [];
@@ -164,6 +183,7 @@ jQuery.extend({
                                                                        
                                                                        if ( m == "+" ) break;
                                                                }
+                                               // DEPRECATED
                                                } else
                                                        r.push( ret[j].parentNode );
 
@@ -194,7 +214,7 @@ jQuery.extend({
 
                                } else {
                                        // Optimize for the case nodeName#idName
-                                       var re2 = new RegExp("^(" + jQuery.chars + "+)(#)(" + jQuery.chars + "+)");
+                                       var re2 = quickID;
                                        var m = re2.exec(t);
                                        
                                        // Re-organize the results, so that they're consistent
@@ -204,7 +224,7 @@ jQuery.extend({
                                        } else {
                                                // Otherwise, do a traditional filter check for
                                                // ID, class, and element selectors
-                                               re2 = new RegExp("^([#.]?)(" + jQuery.chars + "*)");
+                                               re2 = quickClass;
                                                m = re2.exec(t);
                                        }
 
@@ -213,7 +233,7 @@ jQuery.extend({
                                        var elem = ret[ret.length-1];
 
                                        // Try to do a global search by ID, where we can
-                                       if ( m[1] == "#" && elem && elem.getElementById ) {
+                                       if ( m[1] == "#" && elem && elem.getElementById && !jQuery.isXMLDoc(elem) ) {
                                                // Optimization for HTML document case
                                                var oid = elem.getElementById(m[2]);
                                                
@@ -230,7 +250,7 @@ jQuery.extend({
                                                // 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];
+                                                       var tag = m[1] == "#" && m[3] ? m[3] : m[1] != "" || m[0] == "" ? "*" : m[2];
 
                                                        // Handle IE7 being really dumb about <object>s
                                                        if ( tag == "*" && ret[i].nodeName.toLowerCase() == "object" )
@@ -339,7 +359,7 @@ jQuery.extend({
                                for ( var i = 0, rl = r.length; i < rl; i++ ) {
                                        var a = r[i], z = a[ jQuery.props[m[2]] || m[2] ];
                                        
-                                       if ( z == null || /href|src/.test(m[2]) )
+                                       if ( z == null || /href|src|selected/.test(m[2]) )
                                                z = jQuery.attr(a,m[2]) || '';
 
                                        if ( (type == "" && !!z ||
@@ -395,7 +415,7 @@ jQuery.extend({
                                        f = jQuery.expr[m[1]][m[2]];
 
                                // Build a custom macro to enclose it
-                               eval("f = function(a,i){return " + f + "}");
+                               f = eval("false||function(a,i){return " + f + "}");
 
                                // Execute it against the current filter
                                r = jQuery.grep( r, f, not );