Bug 12359

Summary: XPathEvaluator may return some nodes more than once in a result set
Product: WebKit Reporter: Andrew Dupont <webkit>
Component: XMLAssignee: Alexey Proskuryakov <ap>
Status: RESOLVED FIXED    
Severity: Normal CC: ap, webkit
Priority: P2    
Version: 420+   
Hardware: Mac   
OS: OS X 10.4   
URL: http://andrewdupont.net/test/double-dollar/
Attachments:
Description Flags
Reduced test case
none
proposed fix darin: review+

Andrew Dupont
Reported 2007-01-21 20:22:46 PST
While working on routing CSS selector queries through XPath for performance, I noticed that WebKit's version of document.evaluate doesn't ensure uniqueness of the result set. At the test URL, you'll notice that some queries -- like "div div" (or in XPath: ".//div//div" -- fail: the length of the result set is far larger than it should be because a DIV with two DIV ancestors would occur twice. This is different from the behavior of Firefox or Opera. Filtering for uniqueness in my own code solves this problem. Discovered using document.evaluate with result set of type ORDERED_NODE_SNAPSHOT_TYPE.
Attachments
Reduced test case (799 bytes, text/html)
2007-01-21 20:43 PST, Andrew Dupont
no flags
proposed fix (5.99 KB, patch)
2007-01-28 11:27 PST, Alexey Proskuryakov
darin: review+
David Kilzer (:ddkilzer)
Comment 1 2007-01-21 20:29:31 PST
Andrew, could you upload a reduced test case? This would be very helpful in resolving this bug. Thanks!
Andrew Dupont
Comment 2 2007-01-21 20:43:54 PST
Created attachment 12597 [details] Reduced test case Illustrates the example in the above comment. Firefox returns 2 results; WebKit returns 3 (counting the innermost DIV twice).
Alexey Proskuryakov
Comment 3 2007-01-28 11:27:12 PST
Created attachment 12729 [details] proposed fix
Darin Adler
Comment 4 2007-01-28 18:27:37 PST
Comment on attachment 12729 [details] proposed fix + if (!outDOMNodesSet.contains(node)) { + outDOMNodes.append(node); + outDOMNodesSet.add(node); + } This can be done more efficiently by taking advantage of the return value from add. Like this: if (outDOMNodesSet.add(node).second) outDOMNodes.append(node); I know it's a little ugly, but I love the fact that it cuts the number of hash table lookups roughly in half, so please consider this idiom. r=me
Alexey Proskuryakov
Comment 5 2007-01-29 10:25:04 PST
Committed revision 19227. (In reply to comment #4) > I know it's a little ugly, but I love the fact that it cuts the number of hash > table lookups roughly in half, so please consider this idiom. Oops - corrected! I should be more critical of idioms used in existing code :)
Note You need to log in before you can comment on or make changes to this bug.