Skip to content

Commit

Permalink
Make more extensive use of tokens in handlePOS. Fixes false matches f…
Browse files Browse the repository at this point in the history
…or positional selectors and jQuery bug #12303.
  • Loading branch information
timmywil committed Aug 29, 2012
1 parent 15e840c commit af8206f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 40 deletions.
86 changes: 48 additions & 38 deletions sizzle.js
Original file line number Diff line number Diff line change
Expand Up @@ -1202,8 +1202,10 @@ function handlePOSGroup( selector, posfilter, argument, contexts, seed, not ) {
return results.length > 0 ? fn( results, argument, not ) : [];
}

function handlePOS( selector, context, results, seed, groups ) {
var match, not, anchor, ret, elements, currentContexts, part, lastIndex,
function handlePOS( groups, context, results, seed ) {
var group, part, j, groupLen, token, selector,
anchor, elements, match, matched,
lastIndex, currentContexts, not,
i = 0,
len = groups.length,
rpos = matchExpr["POS"],
Expand All @@ -1222,52 +1224,60 @@ function handlePOS( selector, context, results, seed, groups ) {
};

for ( ; i < len; i++ ) {
// Reset regex index to 0
rpos.exec("");
selector = groups[i].selector;
ret = [];
anchor = 0;
group = groups[i];
part = "";
elements = seed;
while ( (match = rpos.exec( selector )) ) {
lastIndex = rpos.lastIndex = match.index + match[0].length;
if ( lastIndex > anchor ) {
part = selector.slice( anchor, match.index );
anchor = lastIndex;
currentContexts = [ context ];

if ( rcombinators.test(part) ) {
if ( elements ) {
currentContexts = elements;
for ( j = 0, groupLen = group.length; j < groupLen; j++ ) {
token = group[j];
selector = token.string;
if ( token.part === "PSEUDO" ) {
// Reset regex index to 0
rpos.exec("");
anchor = 0;
while ( (match = rpos.exec( selector )) ) {
matched = true;
lastIndex = rpos.lastIndex = match.index + match[0].length;
if ( lastIndex > anchor ) {
part += selector.slice( anchor, match.index );
anchor = lastIndex;
currentContexts = [ context ];

if ( rcombinators.test(part) ) {
if ( elements ) {
currentContexts = elements;
}
elements = seed;
}

if ( (not = rendsWithNot.test( part )) ) {
part = part.slice( 0, -5 ).replace( rcombinators, "$&*" );
anchor++;
}

if ( match.length > 1 ) {
match[0].replace( rposgroups, setUndefined );
}
elements = handlePOSGroup( part, match[1], match[2], currentContexts, elements, not );
}
elements = seed;
part = "";
}

if ( (not = rendsWithNot.test( part )) ) {
part = part.slice( 0, -5 ).replace( rcombinators, "$&*" );
anchor++;
}
}

if ( match.length > 1 ) {
match[0].replace( rposgroups, setUndefined );
}
elements = handlePOSGroup( part, match[1], match[2], currentContexts, elements, not );
if ( !matched ) {
part += selector;
}
matched = false;
}

if ( elements ) {
ret = ret.concat( elements );

if ( (part = selector.slice( anchor )) ) {
if ( rcombinators.test(part) ) {
multipleContexts( part, ret, results, seed );
} else {
Sizzle( part, context, results, seed ? seed.concat(elements) : elements );
}
if ( part ) {
if ( rcombinators.test(part) ) {
multipleContexts( part, elements || [ context ], results, seed );
} else {
push.apply( results, ret );
Sizzle( part, context, results, seed ? seed.concat(elements) : elements );
}
} else {
Sizzle( selector, context, results, seed );
push.apply( results, elements );
}
}

Expand All @@ -1285,7 +1295,7 @@ function select( selector, context, results, seed, xml ) {

// POS handling
if ( matchExpr["POS"].test(selector) ) {
return handlePOS( selector, context, results, seed, match );
return handlePOS( match, context, results, seed );
}

if ( seed ) {
Expand Down
9 changes: 7 additions & 2 deletions test/unit/selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ test("child and adjacent", function() {
});

test("attributes", function() {
expect( 59 );
expect( 61 );

t( "Attribute Exists", "#qunit-fixture a[title]", ["google"] );
t( "Attribute Exists (case-insensitive)", "#qunit-fixture a[TITLE]", ["google"] );
Expand Down Expand Up @@ -452,7 +452,7 @@ test("attributes", function() {
var opt = document.getElementById("option1a"),
match = Sizzle.matchesSelector;

opt.setAttribute("test", "");
opt.setAttribute( "test", "" );

ok( match( opt, "[id*=option1][type!=checkbox]" ), "Attribute Is Not Equal Matches" );
ok( match( opt, "[id*=option1]" ), "Attribute With No Quotes Contains Matches" );
Expand Down Expand Up @@ -481,6 +481,11 @@ test("attributes", function() {
// check2.checked = true;
// ok( !Sizzle.matches("[checked]", [ check2 ] ), "Dynamic boolean attributes match when they should with Sizzle.matches (#11115)" );

a.setAttribute( "data-pos", ":first" );
ok( match( a, "a[data-pos=\\:first]"), "POS within attribute value is treated as an attribute value" );
ok( match( a, "a[data-pos=':first']"), "POS within attribute value is treated as an attribute value" );
a.removeAttribute("data-pos");

// Make sure attribute value quoting works correctly. See: #6093
var attrbad = jQuery("<input type=\"hidden\" value=\"2\" name=\"foo.baz\" id=\"attrbad1\"/><input type=\"hidden\" value=\"2\" name=\"foo[baz]\" id=\"attrbad2\"/><input type=\"hidden\" data-attr=\"foo_baz']\" id=\"attrbad3\"/>").appendTo("body");

Expand Down

0 comments on commit af8206f

Please sign in to comment.