Sizzle duplicate selections when using POS filters
|Reported by:||manixrock||Owned by:|
In the following HTML:
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.
Change History (6)
comment:2 Changed 5 years ago by SlexAxton
- Keywords even added
- Milestone changed from 1.4.3 to 1.5
- Priority set to low
- Status changed from new to open
comment:5 Changed 5 years ago by john
- Resolution set to duplicate
- Status changed from open to closed