Merging a bunch of changes back from Sizzle, by LosTFx.

This commit is contained in:
John Resig 2009-02-10 00:28:38 +00:00
parent 9aa0c69c43
commit 831b15b06c

View file

@ -249,21 +249,19 @@ var Expr = Sizzle.selectors = {
} }
}, },
relative: { relative: {
"+": function(checkSet, part){ "+": function(checkSet, part, isXML){
for ( var i = 0, l = checkSet.length; i < l; i++ ) { var isPartStr = typeof part === "string",
var elem = checkSet[i]; isTag = isPartStr && !/\W/.test(part),
if ( elem ) { isPartStrNotTag = isPartStr && !isTag;
var cur = elem.previousSibling; if ( isTag && !isXML ) part = part.toUpperCase();
while ( cur && cur.nodeType !== 1 ) { for ( var i = 0, l = checkSet.length, elem; i < l; i++ ) {
cur = cur.previousSibling; if ( elem = checkSet[i] ) {
} while ( (elem = elem.previousSibling) && elem.nodeType !== 1 ) {};
checkSet[i] = typeof part === "string" ? checkSet[i] = isPartStrNotTag || elem && elem.nodeName === part ?
cur || false : elem : elem === part;
cur === part;
} }
} }
if (isPartStrNotTag) {
if ( typeof part === "string" ) {
Sizzle.filter( part, checkSet, true ); Sizzle.filter( part, checkSet, true );
} }
}, },
@ -294,7 +292,7 @@ var Expr = Sizzle.selectors = {
} }
}, },
"": function(checkSet, part, isXML){ "": function(checkSet, part, isXML){
var doneName = "done" + (done++), checkFn = dirCheck; var doneName = done++, checkFn = dirCheck;
if ( !part.match(/\W/) ) { if ( !part.match(/\W/) ) {
var nodeCheck = part = isXML ? part : part.toUpperCase(); var nodeCheck = part = isXML ? part : part.toUpperCase();
@ -304,7 +302,7 @@ var Expr = Sizzle.selectors = {
checkFn("parentNode", part, doneName, checkSet, nodeCheck, isXML); checkFn("parentNode", part, doneName, checkSet, nodeCheck, isXML);
}, },
"~": function(checkSet, part, isXML){ "~": function(checkSet, part, isXML){
var doneName = "done" + (done++), checkFn = dirCheck; var doneName = done++, checkFn = dirCheck;
if ( typeof part === "string" && !part.match(/\W/) ) { if ( typeof part === "string" && !part.match(/\W/) ) {
var nodeCheck = part = isXML ? part : part.toUpperCase(); var nodeCheck = part = isXML ? part : part.toUpperCase();
@ -334,10 +332,9 @@ var Expr = Sizzle.selectors = {
CLASS: function(match, curLoop, inplace, result, not){ CLASS: function(match, curLoop, inplace, result, not){
match = " " + match[1].replace(/\\/g, "") + " "; match = " " + match[1].replace(/\\/g, "") + " ";
var elem; for ( var i = 0, elem; (elem = curLoop[i]) != null; i++ ) {
for ( var i = 0; (elem = curLoop[i]) != null; i++ ) {
if ( elem ) { if ( elem ) {
if ( not ^ (" " + elem.className + " ").indexOf(match) >= 0 ) { if ( not ^ (elem.className && (" " + elem.className + " ").indexOf(match) >= 0) ) {
if ( !inplace ) if ( !inplace )
result.push( elem ); result.push( elem );
} else if ( inplace ) { } else if ( inplace ) {
@ -368,7 +365,7 @@ var Expr = Sizzle.selectors = {
} }
// TODO: Move to normal caching system // TODO: Move to normal caching system
match[0] = "done" + (done++); match[0] = done++;
return match; return match;
}, },
@ -495,44 +492,46 @@ var Expr = Sizzle.selectors = {
}, },
filter: { filter: {
CHILD: function(elem, match){ CHILD: function(elem, match){
var type = match[1], parent = elem.parentNode; var type = match[1], node = elem;
switch (type) {
var doneName = match[0]; case 'only':
case 'first':
if ( parent && (!parent[ doneName ] || !elem.nodeIndex) ) { while (node = node.previousSibling) {
var count = 1; if ( node.nodeType === 1 ) return false;
for ( var node = parent.firstChild; node; node = node.nextSibling ) {
if ( node.nodeType == 1 ) {
node.nodeIndex = count++;
} }
if ( type == 'first') return true;
node = elem;
case 'last':
while (node = node.nextSibling) {
if ( node.nodeType === 1 ) return false;
} }
return true;
parent[ doneName ] = count - 1; case 'nth':
} var first = match[2], last = match[3];
if ( type == "first" ) {
return elem.nodeIndex == 1;
} else if ( type == "last" ) {
return elem.nodeIndex == parent[ doneName ];
} else if ( type == "only" ) {
return parent[ doneName ] == 1;
} else if ( type == "nth" ) {
var add = false, first = match[2], last = match[3];
if ( first == 1 && last == 0 ) { if ( first == 1 && last == 0 ) {
return true; return true;
} }
if ( first == 0 ) { var doneName = match[0],
if ( elem.nodeIndex == last ) { parent = elem.parentNode;
add = true;
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;
} }
} else if ( (elem.nodeIndex - last) % first == 0 && (elem.nodeIndex - last) / first >= 0 ) { }
add = true; parent.sizcache = doneName;
} }
return add; var diff = elem.nodeIndex - last;
if ( first == 0 ) {
return diff == 0;
} else {
return ( diff % first == 0 && diff / first >= 0 );
}
} }
}, },
PSEUDO: function(elem, match, i, array){ PSEUDO: function(elem, match, i, array){
@ -771,21 +770,27 @@ if ( document.getElementsByClassName && document.documentElement.getElementsByCl
})(); })();
function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) { function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
var sibDir = dir == "previousSibling" && !isXML;
for ( var i = 0, l = checkSet.length; i < l; i++ ) { for ( var i = 0, l = checkSet.length; i < l; i++ ) {
var elem = checkSet[i]; var elem = checkSet[i];
if ( elem ) { if ( elem ) {
if ( sibDir && elem.nodeType === 1 ){
elem.sizcache = doneName;
elem.sizset = i;
}
elem = elem[dir]; elem = elem[dir];
var match = false; var match = false;
while ( elem && elem.nodeType ) { while ( elem ) {
var done = elem[doneName]; if ( elem.sizcache === doneName ) {
if ( done ) { match = checkSet[elem.sizset];
match = checkSet[ done ];
break; break;
} }
if ( elem.nodeType === 1 && !isXML ) if ( elem.nodeType === 1 && !isXML ){
elem[doneName] = i; elem.sizcache = doneName;
elem.sizset = i;
}
if ( elem.nodeName === cur ) { if ( elem.nodeName === cur ) {
match = elem; match = elem;
@ -801,22 +806,28 @@ function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
} }
function dirCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) { function dirCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
var sibDir = dir == "previousSibling" && !isXML;
for ( var i = 0, l = checkSet.length; i < l; i++ ) { for ( var i = 0, l = checkSet.length; i < l; i++ ) {
var elem = checkSet[i]; var elem = checkSet[i];
if ( elem ) { if ( elem ) {
if ( sibDir && elem.nodeType === 1 ) {
elem.sizcache = doneName;
elem.sizset = i;
}
elem = elem[dir]; elem = elem[dir];
var match = false; var match = false;
while ( elem && elem.nodeType ) { while ( elem ) {
if ( elem[doneName] ) { if ( elem.sizcache === doneName ) {
match = checkSet[ elem[doneName] ]; match = checkSet[elem.sizset];
break; break;
} }
if ( elem.nodeType === 1 ) { if ( elem.nodeType === 1 ) {
if ( !isXML ) if ( !isXML ) {
elem[doneName] = i; elem.sizcache = doneName;
elem.sizset = i;
}
if ( typeof cur !== "string" ) { if ( typeof cur !== "string" ) {
if ( elem === cur ) { if ( elem === cur ) {
match = true; match = true;