Skip to content

Commit

Permalink
Avoid cache collisions with other Sizzles. Fixes #8539.
Browse files Browse the repository at this point in the history
  • Loading branch information
timmywil committed Oct 13, 2011
1 parent 3ffd629 commit f56f178
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
30 changes: 17 additions & 13 deletions sizzle.js
Expand Up @@ -7,6 +7,7 @@
(function(){

var chunker = /((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^\[\]]*\]|['"][^'"]*['"]|[^\[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?((?:.|\r|\n)*)/g,
expando = "sizcache" + (Math.random() + '').replace('.', ''),
done = 0,
toString = Object.prototype.toString,
hasDuplicate = false,
Expand Down Expand Up @@ -772,7 +773,10 @@ var Expr = Sizzle.selectors = {
},

CHILD: function( elem, match ) {
var type = match[1],
var first, last,
doneName, parent, cache,
count, diff,
type = match[1],
node = elem;

switch ( type ) {
Expand Down Expand Up @@ -800,29 +804,29 @@ var Expr = Sizzle.selectors = {
return true;

case "nth":
var first = match[2],
last = match[3];
first = match[2];
last = match[3];

if ( first === 1 && last === 0 ) {
return true;
}

var doneName = match[0],
parent = elem.parentNode;
doneName = match[0];
parent = elem.parentNode;

if ( parent && (parent.sizcache !== doneName || !elem.nodeIndex) ) {
var count = 0;
if ( parent && (parent[ expando ] !== doneName || !elem.nodeIndex) ) {
count = 0;

for ( node = parent.firstChild; node; node = node.nextSibling ) {
if ( node.nodeType === 1 ) {
node.nodeIndex = ++count;
}
}

parent.sizcache = doneName;
parent[ expando ] = doneName;
}

var diff = elem.nodeIndex - last;
diff = elem.nodeIndex - last;

if ( first === 0 ) {
return diff === 0;
Expand Down Expand Up @@ -1320,13 +1324,13 @@ function dirNodeCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
elem = elem[dir];

while ( elem ) {
if ( elem.sizcache === doneName ) {
if ( elem[ expando ] === doneName ) {
match = checkSet[elem.sizset];
break;
}

if ( elem.nodeType === 1 && !isXML ){
elem.sizcache = doneName;
elem[ expando ] = doneName;
elem.sizset = i;
}

Expand All @@ -1353,14 +1357,14 @@ function dirCheck( dir, cur, doneName, checkSet, nodeCheck, isXML ) {
elem = elem[dir];

while ( elem ) {
if ( elem.sizcache === doneName ) {
if ( elem[ expando ] === doneName ) {
match = checkSet[elem.sizset];
break;
}

if ( elem.nodeType === 1 ) {
if ( !isXML ) {
elem.sizcache = doneName;
elem[ expando ] = doneName;
elem.sizset = i;
}

Expand Down
2 changes: 1 addition & 1 deletion test/unit/selector.js
Expand Up @@ -197,7 +197,7 @@ test("name", function() {
t( "Name selector non-input", "[name=div]", ["fadein"] );
t( "Name selector non-input", "*[name=iframe]", ["iframe"] );

t( "Name selector for grouped input", "input[name='types[]']", ["types_all", "types_anime", "types_movie"] )
t( "Name selector for grouped input", "input[name='types[]']", ["types_all", "types_anime", "types_movie"] );

same( jQuery("#form").find("input[name=action]").get(), q("text1"), "Name selector within the context of another element" );
same( jQuery("#form").find("input[name='foo[bar]']").get(), q("hidden2"), "Name selector for grouped form element within the context of another element" );
Expand Down

0 comments on commit f56f178

Please sign in to comment.