Skip to main content

Bug Tracker

Side navigation

#6384 closed bug (duplicate)

Opened April 01, 2010 01:14PM UTC

Closed July 12, 2011 04:08PM UTC

Last modified July 12, 2011 04:08PM UTC

Sizzle duplicate selections when using POS filters

Reported by: manixrock Owned by:
Priority: high Milestone: 1.next
Component: selector Version: 1.4.2
Keywords: even Cc:
Blocked by: Blocking:
Description

In the following HTML:

<div><div><div><a></a></div></div></div>

the code "Sizzle('div:even a');" selects the same <a> tag twice. The reason for this is because internally Sizzle selects 'div:even', then selects 'a' with each resulting <div> from the previous selection, and since the second <div> is contained within the first, the same <a> gets selected twice.

This problem isn't specific to selectors with descendant-relations, next-sibling type selectors are also affected.

A solution for this that would work for all selector relationship types (descendant, child, sibling, next-sibling), is to add a checkForDuplicates variable that is true when a POS-type filter (:even, :nth, etc.) is detected, and whenever a match is found, if checkForDuplicates is true check that it doesn't already exist in the results.

Another solution is after the first selection of 'div:even' to remove any of the matches that are the child of another match (or if the next selector-relationship is of type next-sibling, remove if they are next-sibling; if child or next-adjacent do nothing). This is because the top-most tag will also search in all it's children and was the cause of the duplication. This should be faster than the first solution, and can probably be optimized a lot.

Attachments (0)
Change History (6)

Changed August 04, 2010 02:43AM UTC by dmethvin comment:1

See also #6700; should this be something Sizzle does, or should all the processing happen and the final set be de-duped?

Changed October 29, 2010 05:54AM UTC by SlexAxton comment:2

keywords: → even
milestone: 1.4.31.5
priority: → low
status: newopen

I thought one of the benefits of sizzle was that it was right-to-left, so it didn't suffer from the duplicate problems... hmm - I guess the :even selector ruins that? Does this hit qSA?

http://jsfiddle.net/ytcbj/

Seems like a bug to me :/

Changed April 17, 2011 05:22PM UTC by john comment:3

component: traversingselector

Changed April 17, 2011 05:56PM UTC by john comment:4

milestone: → 1.next
priority: lowhigh

Changed July 12, 2011 04:08PM UTC by john comment:5

resolution: → duplicate
status: openclosed

Changed July 12, 2011 04:08PM UTC by john comment:6

Duplicate of #9810.