Repository navigation
Return traversal results in jQuery's order - #130
Merged
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #130 +/- ##
============================================
+ Coverage 90.69% 91.14% +0.44%
- Complexity 1424 1434 +10
============================================
Files 25 26 +1
Lines 3170 3172 +2
============================================
+ Hits 2875 2891 +16
+ Misses 295 281 -14 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
gravitypdf-bot
force-pushed
the
fix/issue-117
branch
from
October 26, 2026 04:58
f54c487 to
4f95cbe
Compare
prevAll(), prevUntil(), nextAll(), nextUntil(), siblings(), parent() and closest() grouped a multi-element set's results by starting element. They now sort them as jQuery does, in one place in walk(). Fixes #117. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
inDocumentOrder() and inReverseDocumentOrder() filter, sort and wrap a traversal's results, so each public method is one line and names its order. The test ID helpers move to TestCase. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
gravitypdf-bot
force-pushed
the
fix/issue-117
branch
from
October 26, 2026 05:31
4f95cbe to
56a0b36
Compare
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Behaviour change. On a multi-element set, the traversal methods now return their results in jQuery's order. Before, the results were grouped by starting element.
What jQuery does
I took the rules from jQuery 3.7.1's
traversing.js. The table values come from running each call in jQuery 3.7.1 on jsdom. jQuery sorts only when the set has more than one element:parents,parentsUntil,prevAllandprevUntiluseuniqueSortand thenreverse, so they return reverse document order.next,prev,childrenandcontentsare inguaranteedUnique, so they are never sorted and keep the order of the starting elements.uniqueSort, which gives document order. That coversnextAll,nextUntil,siblingsandparent.closestalso sorts its results withuniqueSort.Document:
<r><i#a/><i#b/><i#c/><i#d/><i#e/></r>, andr > s#s1 > t#t1 > u#u1andr > s#s2 > t#t2 > u#u2. The start sets are built in the order shown, not in document order.[c, d].prevAll()[c, e].prevUntil('#a')[c, a].nextAll()[c, a].nextUntil('#e')[b, d].siblings()[u2, u1].parent()[u2, u1].closest('s')[c, a].next()[u2, u1].parents()Results for a single starting element are unchanged.
What changed
Each traversal method names its order with one of two helpers, which filter by the optional selector, sort, and wrap the result:
inReverseDocumentOrder()parents(),parentsUntil(),prevAll(),prevUntil()inDocumentOrder()nextAll(),nextUntil(),siblings(),parent(),closest()next(),prev(), which keep the order of the starting elementswalk(), the helper from #129, stays order-agnostic.sortReverseDocumentOrder()became the privatesortDocumentOrder($nodes, $reverse)behind the two helpers, and still skips the sort for a single starting element. Filtering before sorting gives the same result as sorting first, because since #128 positional pseudo-classes count in document order whatever order the set is given in, and it sorts the smaller set.Remaining differences from jQuery
$('#e').prevAll(':first')is#d, the nearest sibling. QueryPath's positional pseudo-classes count in document order (jQuery positional pseudo-classes (:eq, :first, :lt, :gt, :odd, :even) index siblings instead of the match set; :eq(0) never matches #66), insideDOMTraverser, soprevAll(':first')is still#a. This PR does not change that, because it needssrc/CSS/changes; Positional selectors in prevAll(), parents() and the *Until() methods count in document order, not jQuery's walk order #134 tracks it. Since the count is done in document order, these results do not depend on the output ordering. Positional selectors in closest(), next() and the *Until() methods see a one-element set #118 pins them.parent($selector)still returns the nearest matching ancestor, where jQuery tests only the immediate parent. That is existing QueryPath behaviour.Tests
tests/Issues/Issue117Test.phpcovers each method on a multi-element set with an out-of-order start, plus the single-start case. Three of its five tests fail onmain.DOMQueryTest::testNextAllAndPrevAllFromOverlappingStarts, two expectations pinned the old grouped order. They now expect jQuery's.selectIds()andidList(), the helpers that build an out-of-order start set and list its IDs, live inQueryPathTests\TestCase, so Count positional *Until() stops across the document, as jQuery does #131's tests share them.vendor/bin/phpunit: 698 tests, 1948 assertions, 0 failures (1 skipped).composer run lintandcomposer run lint:min-phpare clean.Performance: the sort adds about 6 ms to
nextAll()andprevAll()on a 4,000-item list (1.3 ms before, about 7 ms after).Rebased onto #129
Rebased onto the updated #129, which moved ordering out of
walk()and into the public methods; the ordering this PR first put at the end ofwalk()now lives in the two helpers above.Fixes #117.
🤖 Generated with Claude Code