X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fselector.js;h=6291d862187b72495b6221d3f83b3a54359ef23f;hb=bbffc99f7c60d42d4286786dfd6a43aa31d62ae2;hp=42e7e415e1461c3559b6f187445b40026168e83b;hpb=f0189d618181409ac628b9bbdecd77338127b9b1;p=jquery.git diff --git a/src/selector.js b/src/selector.js index 42e7e41..6291d86 100644 --- a/src/selector.js +++ b/src/selector.js @@ -1,5 +1,5 @@ /*! - * Sizzle CSS Selector Engine - v0.9.3 + * Sizzle CSS Selector Engine - v1.0 * Copyright 2009, The Dojo Foundation * Released under the MIT, BSD, and GPL Licenses. * More information: http://sizzlejs.com/ @@ -8,20 +8,22 @@ 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 || []; - context = context || document; + var origContext = context = context || document; - if ( context.nodeType !== 1 && context.nodeType !== 9 ) + 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; + 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; @@ -53,31 +55,43 @@ var Sizzle = function(selector, context, results, seed) { } } } else { - var ret = seed ? - { expr: parts.pop(), set: makeArray(seed) } : - Sizzle.find( parts.pop(), parts.length === 1 && context.parentNode ? context.parentNode : context, isXML(context) ); - set = Sizzle.filter( ret.expr, ret.set ); - - if ( parts.length > 0 ) { - checkSet = makeArray(set); - } else { - prune = false; + // Take a shortcut and set the context if the root selector is an ID + // (but not if it'll be faster if the inner selector is an ID) + if ( !seed && parts.length > 1 && context.nodeType === 9 && !contextXML && + Expr.match.ID.test(parts[0]) && !Expr.match.ID.test(parts[parts.length - 1]) ) { + var ret = Sizzle.find( parts.shift(), context, contextXML ); + context = ret.expr ? Sizzle.filter( ret.expr, ret.set )[0] : ret.set[0]; } - while ( parts.length ) { - var cur = parts.pop(), pop = cur; + if ( context ) { + var ret = seed ? + { expr: parts.pop(), set: makeArray(seed) } : + Sizzle.find( parts.pop(), parts.length === 1 && (parts[0] === "~" || parts[0] === "+") && context.parentNode ? context.parentNode : context, contextXML ); + set = ret.expr ? Sizzle.filter( ret.expr, ret.set ) : ret.set; - if ( !Expr.relative[ cur ] ) { - cur = ""; + if ( parts.length > 0 ) { + checkSet = makeArray(set); } else { - pop = parts.pop(); + prune = false; } - if ( pop == null ) { - pop = context; - } + while ( parts.length ) { + var cur = parts.pop(), pop = cur; + + if ( !Expr.relative[ cur ] ) { + cur = ""; + } else { + pop = parts.pop(); + } + + if ( pop == null ) { + pop = context; + } - Expr.relative[ cur ]( checkSet, pop, isXML(context) ); + Expr.relative[ cur ]( checkSet, pop, contextXML ); + } + } else { + checkSet = parts = []; } } @@ -92,7 +106,7 @@ var Sizzle = function(selector, context, results, seed) { if ( toString.call(checkSet) === "[object Array]" ) { if ( !prune ) { results.push.apply( results, checkSet ); - } else if ( context.nodeType === 1 ) { + } else if ( context && context.nodeType === 1 ) { for ( var i = 0; checkSet[i] != null; i++ ) { if ( checkSet[i] && (checkSet[i] === true || checkSet[i].nodeType === 1 && contains(context, checkSet[i])) ) { results.push( set[i] ); @@ -110,17 +124,22 @@ var Sizzle = function(selector, context, results, seed) { } if ( extra ) { - Sizzle( extra, context, results, seed ); + Sizzle( extra, origContext, results, seed ); + Sizzle.uniqueSort( results ); + } + + return results; +}; - if ( sortOrder ) { - hasDuplicate = false; - results.sort(sortOrder); +Sizzle.uniqueSort = function(results){ + if ( sortOrder ) { + hasDuplicate = false; + results.sort(sortOrder); - if ( hasDuplicate ) { - for ( var i = 1; i < results.length; i++ ) { - if ( results[i] === results[i-1] ) { - results.splice(i--, 1); - } + if ( hasDuplicate ) { + for ( var i = 1; i < results.length; i++ ) { + if ( results[i] === results[i-1] ) { + results.splice(i--, 1); } } } @@ -165,7 +184,8 @@ Sizzle.find = function(expr, context, isXML){ }; Sizzle.filter = function(expr, set, inplace, not){ - var old = expr, result = [], curLoop = set, match, anyFound; + var old = expr, result = [], curLoop = set, match, anyFound, + isXMLFilter = set && set[0] && isXML(set[0]); while ( expr && set.length ) { for ( var type in Expr.filter ) { @@ -178,7 +198,7 @@ Sizzle.filter = function(expr, set, inplace, not){ } if ( Expr.preFilter[ type ] ) { - match = Expr.preFilter[ type ]( match, curLoop, inplace, result, not ); + match = Expr.preFilter[ type ]( match, curLoop, inplace, result, not, isXMLFilter ); if ( !match ) { anyFound = found = true; @@ -261,23 +281,32 @@ var Expr = Sizzle.selectors = { }, relative: { "+": function(checkSet, part, isXML){ - var isPartStr = typeof part === "string", + var isPartStr = typeof part === "string", isTag = isPartStr && !/\W/.test(part), isPartStrNotTag = isPartStr && !isTag; - if ( isTag && !isXML ) part = part.toUpperCase(); + + if ( isTag && !isXML ) { + part = part.toUpperCase(); + } + for ( var i = 0, l = checkSet.length, elem; i < l; i++ ) { - if ( elem = checkSet[i] ) { - while ( (elem = elem.previousSibling) && elem.nodeType !== 1 ) {}; + if ( (elem = checkSet[i]) ) { + while ( (elem = elem.previousSibling) && elem.nodeType !== 1 ) {} + checkSet[i] = isPartStrNotTag || elem && elem.nodeName === part ? - elem : elem === part; + elem || false : + elem === part; } } - if (isPartStrNotTag) { + + if ( isPartStrNotTag ) { Sizzle.filter( part, checkSet, true ); } }, ">": function(checkSet, part, isXML){ - if ( typeof part === "string" && !/\W/.test(part) ) { + var isPartStr = typeof part === "string"; + + if ( isPartStr && !/\W/.test(part) ) { part = isXML ? part : part.toUpperCase(); for ( var i = 0, l = checkSet.length; i < l; i++ ) { @@ -291,13 +320,13 @@ var Expr = Sizzle.selectors = { for ( var i = 0, l = checkSet.length; i < l; i++ ) { var elem = checkSet[i]; if ( elem ) { - checkSet[i] = typeof part === "string" ? + checkSet[i] = isPartStr ? elem.parentNode : elem.parentNode === part; } } - if ( typeof part === "string" ) { + if ( isPartStr ) { Sizzle.filter( part, checkSet, true ); } } @@ -305,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; } @@ -315,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; } @@ -348,9 +377,13 @@ var Expr = Sizzle.selectors = { } }, preFilter: { - CLASS: function(match, curLoop, inplace, result, not){ + CLASS: function(match, curLoop, inplace, result, not, isXML){ match = " " + match[1].replace(/\\/g, "") + " "; + if ( isXML ) { + return match; + } + for ( var i = 0, elem; (elem = curLoop[i]) != null; i++ ) { if ( elem ) { if ( not ^ (elem.className && (" " + elem.className + " ").indexOf(match) >= 0) ) { @@ -388,10 +421,10 @@ var Expr = Sizzle.selectors = { return match; }, - ATTR: function(match){ + ATTR: function(match, curLoop, inplace, result, not, isXML){ var name = match[1].replace(/\\/g, ""); - if ( Expr.attrMap[name] ) { + if ( !isXML && Expr.attrMap[name] ) { match[1] = Expr.attrMap[name]; } @@ -404,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); @@ -413,7 +446,7 @@ var Expr = Sizzle.selectors = { } return false; } - } else if ( Expr.match.POS.test( match[0] ) ) { + } else if ( Expr.match.POS.test( match[0] ) || Expr.match.CHILD.test( match[0] ) ) { return true; } @@ -510,18 +543,37 @@ var Expr = Sizzle.selectors = { } }, filter: { + PSEUDO: function(elem, match, i, array){ + var name = match[1], filter = Expr.filters[ name ]; + + if ( filter ) { + return filter( elem, i, match, array ); + } else if ( name === "contains" ) { + return (elem.textContent || elem.innerText || "").indexOf(match[3]) >= 0; + } else if ( name === "not" ) { + var not = match[3]; + + for ( var i = 0, l = not.length; i < l; i++ ) { + if ( not[i] === elem ) { + return false; + } + } + + return true; + } + }, CHILD: function(elem, match){ var type = match[1], node = elem; 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; @@ -553,25 +605,6 @@ var Expr = Sizzle.selectors = { } } }, - PSEUDO: function(elem, match, i, array){ - var name = match[1], filter = Expr.filters[ name ]; - - if ( filter ) { - return filter( elem, i, match, array ); - } else if ( name === "contains" ) { - return (elem.textContent || elem.innerText || "").indexOf(match[3]) >= 0; - } else if ( name === "not" ) { - var not = match[3]; - - for ( var i = 0, l = not.length; i < l; i++ ) { - if ( not[i] === elem ) { - return false; - } - } - - return true; - } - }, ID: function(elem, match){ return elem.nodeType === 1 && elem.getAttribute("id") === match; }, @@ -579,7 +612,8 @@ var Expr = Sizzle.selectors = { return (match === "*" && elem.nodeType === 1) || elem.nodeName === match; }, CLASS: function(elem, match){ - return match.test( elem.className ); + return (" " + (elem.className || elem.getAttribute("class")) + " ") + .indexOf( match ) > -1; }, ATTR: function(elem, match){ var name = match[1], @@ -625,11 +659,11 @@ var Expr = Sizzle.selectors = { var origPOS = Expr.match.POS; for ( var type in Expr.match ) { - Expr.match[ type ] = RegExp( Expr.match[ type ].source + /(?![^\[]*\])(?![^\(]*\))/.source ); + Expr.match[ type ] = new RegExp( Expr.match[ type ].source + /(?![^\[]*\])(?![^\(]*\))/.source ); } 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 ); @@ -642,7 +676,7 @@ var makeArray = function(array, results) { // 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){ @@ -669,34 +703,65 @@ try { var sortOrder; -if ( Array.prototype.indexOf ) { - var indexOf = Array.prototype.indexOf, - allSort = document.getElementsByTagName("*"); - +if ( document.documentElement.compareDocumentPosition ) { sortOrder = function( a, b ) { - var ret = indexOf.call( allSort, a ) - indexOf.call( allSort, 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; } return ret; }; -} else if ( document.documentElement.sourceIndex === 1 ) { +} 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; } return ret; }; +} 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); + bRange.selectNode(b); + bRange.collapse(true); + var ret = aRange.compareBoundaryPoints(Range.START_TO_END, bRange); + if ( ret === 0 ) { + hasDuplicate = true; + } + return ret; + }; } // Check to see if the browser returns elements by name when // querying by getElementById (and provide a workaround) (function(){ // We're going to inject a fake input element with a specified name - var form = document.createElement("form"), + var form = document.createElement("div"), id = "script" + (new Date).getTime(); - form.innerHTML = ""; + form.innerHTML = ""; // Inject it into the root element, check its status, and remove it quickly var root = document.documentElement; @@ -719,6 +784,7 @@ if ( Array.prototype.indexOf ) { } root.removeChild( form ); + root = form = null; // release memory in IE })(); (function(){ @@ -759,6 +825,8 @@ if ( Array.prototype.indexOf ) { return elem.getAttribute("href", 2); }; } + + div = null; // release memory in IE })(); if ( document.querySelectorAll ) (function(){ @@ -785,10 +853,11 @@ if ( document.querySelectorAll ) (function(){ return oldSizzle(query, context, extra, seed); }; - Sizzle.find = oldSizzle.find; - Sizzle.filter = oldSizzle.filter; - Sizzle.selectors = oldSizzle.selectors; - Sizzle.matches = oldSizzle.matches; + for ( var prop in oldSizzle ) { + Sizzle[ prop ] = oldSizzle[ prop ]; + } + + div = null; // release memory in IE })(); if ( document.getElementsByClassName && document.documentElement.getElementsByClassName ) (function(){ @@ -806,9 +875,13 @@ if ( document.getElementsByClassName && document.documentElement.getElementsByCl return; Expr.order.splice(1, 0, "CLASS"); - Expr.find.CLASS = function(match, context) { - return context.getElementsByClassName(match[1]); + Expr.find.CLASS = function(match, context, isXML) { + if ( typeof context.getElementsByClassName !== "undefined" && !isXML ) { + return context.getElementsByClassName(match[1]); + } }; + + div = null; // release memory in IE })(); function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) { @@ -898,7 +971,7 @@ var contains = document.compareDocumentPosition ? function(a, b){ var isXML = function(elem){ return elem.nodeType === 9 && elem.documentElement.nodeName !== "HTML" || - !!elem.ownerDocument && isXML( elem.ownerDocument ); + !!elem.ownerDocument && elem.ownerDocument.documentElement.nodeName !== "HTML"; }; var posProcess = function(selector, context){ @@ -923,67 +996,9 @@ var posProcess = function(selector, context){ // EXPOSE jQuery.find = Sizzle; -jQuery.filter = Sizzle.filter; jQuery.expr = Sizzle.selectors; jQuery.expr[":"] = jQuery.expr.filters; - -Sizzle.selectors.filters.hidden = function(elem){ - return "hidden" === elem.type || - jQuery.css(elem, "display") === "none" || - jQuery.css(elem, "visibility") === "hidden"; -}; - -Sizzle.selectors.filters.visible = function(elem){ - return "hidden" !== elem.type && - jQuery.css(elem, "display") !== "none" && - jQuery.css(elem, "visibility") !== "hidden"; -}; - -Sizzle.selectors.filters.animated = function(elem){ - return jQuery.grep(jQuery.timers, function(fn){ - return elem === fn.elem; - }).length; -}; - -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;