When using the querySelectorAll method on an element it doesn't return the correct number of nodes if a pattern with elements in elements are used. For example if this code is executed on an element with 4 divs but only one div withing another div it should return 1 and not 4. document.getElementById('test').querySelectorAll('div div'); HTML: <div id="test"> <div>1</div> <div> <div>2</div> </div> <div>3</div> </div>
Created attachment 19242 [details] Testcase to illustrate the bug. This test case will alert 4 but should alert 1 since only one div is inside another div.
I'd mark this INVALID, but can't. Anyway, selectors are always matched agains the document element. However, for calls on element nodes all nodes not part of the sub tree of that element node are filtered out. This is why you end up with 4 and not 1 or 5.
I shouild have mentioned that we will clarify the specification in due course to make this more explicit.
Marking INVALID per Anne's comments.
Hmm, strange since this is not the way common libraries like jQuery/Prototype etc work. They all return 1 if you scope the search to an specific element so this can't be used as an replacement for those APIs. I could not find anything about this in the W3C specification so I'm wondering what is the correct behavior?
*** Bug 17731 has been marked as a duplicate of this bug. ***