Moved a bunch of methods out of the jQuery-specific Sizzle code into more-appropriate...
[jquery.git] / src / selector.js
index fcab67f..6291d86 100644 (file)
@@ -8,7 +8,8 @@
 
 var chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^[\]]*\]|['"][^'"]*['"]|[^[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?/g,
        done = 0,
-       toString = Object.prototype.toString;
+       toString = Object.prototype.toString,
+       hasDuplicate = false;
 
 var Sizzle = function(selector, context, results, seed) {
        results = results || [];
@@ -17,19 +18,19 @@ var Sizzle = function(selector, context, results, seed) {
        if ( context.nodeType !== 1 && context.nodeType !== 9 ) {
                return [];
        }
-
+       
        if ( !selector || typeof selector !== "string" ) {
                return results;
        }
 
        var parts = [], m, set, checkSet, check, mode, extra, prune = true, contextXML = isXML(context);
-
+       
        // Reset the position of the chunker regexp (start from head)
        chunker.lastIndex = 0;
-
+       
        while ( (m = chunker.exec(selector)) !== null ) {
                parts.push( m[1] );
-
+               
                if ( m[2] ) {
                        extra = RegExp.rightContext;
                        break;
@@ -143,6 +144,8 @@ Sizzle.uniqueSort = function(results){
                        }
                }
        }
+
+       return results;
 };
 
 Sizzle.matches = function(expr, set){
@@ -158,7 +161,7 @@ Sizzle.find = function(expr, context, isXML){
 
        for ( var i = 0, l = Expr.order.length; i < l; i++ ) {
                var type = Expr.order[i], match;
-
+               
                if ( (match = Expr.match[ type ].exec( expr )) ) {
                        var left = RegExp.leftContext;
 
@@ -331,7 +334,7 @@ var Expr = Sizzle.selectors = {
                "": function(checkSet, part, isXML){
                        var doneName = done++, checkFn = dirCheck;
 
-                       if ( !part.match(/\W/) ) {
+                       if ( !/\W/.test(part) ) {
                                var nodeCheck = part = isXML ? part : part.toUpperCase();
                                checkFn = dirNodeCheck;
                        }
@@ -341,7 +344,7 @@ var Expr = Sizzle.selectors = {
                "~": function(checkSet, part, isXML){
                        var doneName = done++, checkFn = dirCheck;
 
-                       if ( typeof part === "string" && !part.match(/\W/) ) {
+                       if ( typeof part === "string" && !/\W/.test(part) ) {
                                var nodeCheck = part = isXML ? part : part.toUpperCase();
                                checkFn = dirNodeCheck;
                        }
@@ -420,7 +423,7 @@ var Expr = Sizzle.selectors = {
                },
                ATTR: function(match, curLoop, inplace, result, not, isXML){
                        var name = match[1].replace(/\\/g, "");
-
+                       
                        if ( !isXML && Expr.attrMap[name] ) {
                                match[1] = Expr.attrMap[name];
                        }
@@ -434,7 +437,7 @@ var Expr = Sizzle.selectors = {
                PSEUDO: function(match, curLoop, inplace, result, not){
                        if ( match[1] === "not" ) {
                                // If we're dealing with a complex expression, or a simple one
-                               if ( match[3].match(chunker).length > 1 || /^\w/.test(match[3]) ) {
+                               if ( chunker.exec(match[3]).length > 1 || /^\w/.test(match[3]) ) {
                                        match[3] = Sizzle(match[3], null, null, curLoop);
                                } else {
                                        var ret = Sizzle.filter(match[3], curLoop, inplace, true ^ not);
@@ -446,7 +449,7 @@ var Expr = Sizzle.selectors = {
                        } else if ( Expr.match.POS.test( match[0] ) || Expr.match.CHILD.test( match[0] ) ) {
                                return true;
                        }
-
+                       
                        return match;
                },
                POS: function(match){
@@ -564,13 +567,13 @@ var Expr = Sizzle.selectors = {
                        switch (type) {
                                case 'only':
                                case 'first':
-                                       while (node = node.previousSibling)  {
+                                       while ( (node = node.previousSibling) )  {
                                                if ( node.nodeType === 1 ) return false;
                                        }
                                        if ( type == 'first') return true;
                                        node = elem;
                                case 'last':
-                                       while (node = node.nextSibling)  {
+                                       while ( (node = node.nextSibling) )  {
                                                if ( node.nodeType === 1 ) return false;
                                        }
                                        return true;
@@ -580,20 +583,20 @@ var Expr = Sizzle.selectors = {
                                        if ( first == 1 && last == 0 ) {
                                                return true;
                                        }
-
+                                       
                                        var doneName = match[0],
                                                parent = elem.parentNode;
-
+       
                                        if ( parent && (parent.sizcache !== doneName || !elem.nodeIndex) ) {
                                                var count = 0;
                                                for ( node = parent.firstChild; node; node = node.nextSibling ) {
                                                        if ( node.nodeType === 1 ) {
                                                                node.nodeIndex = ++count;
                                                        }
-                                               }
+                                               } 
                                                parent.sizcache = doneName;
                                        }
-
+                                       
                                        var diff = elem.nodeIndex - last;
                                        if ( first == 0 ) {
                                                return diff == 0;
@@ -660,20 +663,20 @@ for ( var type in Expr.match ) {
 }
 
 var makeArray = function(array, results) {
-       array = Array.prototype.slice.call( array );
+       array = Array.prototype.slice.call( array, 0 );
 
        if ( results ) {
                results.push.apply( results, array );
                return results;
        }
-
+       
        return array;
 };
 
 // Perform a simple check to determine if the browser is capable of
 // converting a NodeList to an array using builtin methods.
 try {
-       Array.prototype.slice.call( document.documentElement.childNodes );
+       Array.prototype.slice.call( document.documentElement.childNodes, 0 );
 
 // Provide a fallback method if it does not work
 } catch(e){
@@ -702,6 +705,13 @@ var sortOrder;
 
 if ( document.documentElement.compareDocumentPosition ) {
        sortOrder = function( a, b ) {
+               if ( !a.compareDocumentPosition || !b.compareDocumentPosition ) {
+                       if ( a == b ) {
+                               hasDuplicate = true;
+                       }
+                       return 0;
+               }
+
                var ret = a.compareDocumentPosition(b) & 4 ? -1 : a === b ? 0 : 1;
                if ( ret === 0 ) {
                        hasDuplicate = true;
@@ -710,6 +720,13 @@ if ( document.documentElement.compareDocumentPosition ) {
        };
 } else if ( "sourceIndex" in document.documentElement ) {
        sortOrder = function( a, b ) {
+               if ( !a.sourceIndex || !b.sourceIndex ) {
+                       if ( a == b ) {
+                               hasDuplicate = true;
+                       }
+                       return 0;
+               }
+
                var ret = a.sourceIndex - b.sourceIndex;
                if ( ret === 0 ) {
                        hasDuplicate = true;
@@ -718,6 +735,13 @@ if ( document.documentElement.compareDocumentPosition ) {
        };
 } else if ( document.createRange ) {
        sortOrder = function( a, b ) {
+               if ( !a.ownerDocument || !b.ownerDocument ) {
+                       if ( a == b ) {
+                               hasDuplicate = true;
+                       }
+                       return 0;
+               }
+
                var aRange = a.ownerDocument.createRange(), bRange = b.ownerDocument.createRange();
                aRange.selectNode(a);
                aRange.collapse(true);
@@ -760,6 +784,7 @@ if ( document.documentElement.compareDocumentPosition ) {
        }
 
        root.removeChild( form );
+       root = form = null; // release memory in IE
 })();
 
 (function(){
@@ -800,6 +825,8 @@ if ( document.documentElement.compareDocumentPosition ) {
                        return elem.getAttribute("href", 2);
                };
        }
+
+       div = null; // release memory in IE
 })();
 
 if ( document.querySelectorAll ) (function(){
@@ -811,7 +838,7 @@ if ( document.querySelectorAll ) (function(){
        if ( div.querySelectorAll && div.querySelectorAll(".TEST").length === 0 ) {
                return;
        }
-
+       
        Sizzle = function(query, context, extra, seed){
                context = context || document;
 
@@ -822,13 +849,15 @@ if ( document.querySelectorAll ) (function(){
                                return makeArray( context.querySelectorAll(query), extra );
                        } catch(e){}
                }
-
+               
                return oldSizzle(query, context, extra, seed);
        };
 
        for ( var prop in oldSizzle ) {
                Sizzle[ prop ] = oldSizzle[ prop ];
        }
+
+       div = null; // release memory in IE
 })();
 
 if ( document.getElementsByClassName && document.documentElement.getElementsByClassName ) (function(){
@@ -851,6 +880,8 @@ if ( document.getElementsByClassName && document.documentElement.getElementsByCl
                        return context.getElementsByClassName(match[1]);
                }
        };
+
+       div = null; // release memory in IE
 })();
 
 function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
@@ -967,60 +998,7 @@ var posProcess = function(selector, context){
 jQuery.find = Sizzle;
 jQuery.expr = Sizzle.selectors;
 jQuery.expr[":"] = jQuery.expr.filters;
-
-Sizzle.selectors.filters.hidden = function(elem){
-       return elem.offsetWidth === 0 && elem.offsetHeight === 0;
-};
-
-Sizzle.selectors.filters.visible = function(elem){
-       return elem.offsetWidth > 0 || elem.offsetHeight > 0;
-};
-
-Sizzle.selectors.filters.animated = function(elem){
-       return jQuery.grep(jQuery.timers, function(fn){
-               return elem === fn.elem;
-       }).length;
-};
-
-jQuery.filter = jQuery.multiFilter = function( expr, elems, not ) {
-       if ( not ) {
-               expr = ":not(" + expr + ")";
-       }
-
-       return Sizzle.matches(expr, elems);
-};
-
-jQuery.dir = function( elem, dir ){
-       var matched = [], cur = elem[dir];
-       while ( cur && cur != document ) {
-               if ( cur.nodeType == 1 )
-                       matched.push( cur );
-               cur = cur[dir];
-       }
-       return matched;
-};
-
-jQuery.nth = function(cur, result, dir, elem){
-       result = result || 1;
-       var num = 0;
-
-       for ( ; cur; cur = cur[dir] )
-               if ( cur.nodeType == 1 && ++num == result )
-                       break;
-
-       return cur;
-};
-
-jQuery.sibling = function(n, elem){
-       var r = [];
-
-       for ( ; n; n = n.nextSibling ) {
-               if ( n.nodeType == 1 && n != elem )
-                       r.push( n );
-       }
-
-       return r;
-};
+jQuery.unique = Sizzle.uniqueSort;
 
 return;