Skip to content

fix(traversing): return prevAll/prevUntil results in reverse document order - #5677

Open
Enesuygurs wants to merge 1 commit into
cheeriojs:mainfrom
Enesuygurs:fix/prev-all-order
Open

Enesuygurs wants to merge 1 commit into
cheeriojs:mainfrom
Enesuygurs:fix/prev-all-order

Conversation

@Enesuygurs

@Enesuygurs Enesuygurs commented Oct 23, 2026 •

Copy link
Copy Markdown

Fixes #5676

When .prevAll() or .prevUntil() is called on more than one element, the results were only de-duplicated, not ordered. They ended up in neither document order nor reverse document order:

const $ = load('<ul><li id=a></li><li id=b></li><li id=c></li><li id=d></li></ul>');
$('#c, #d').prevAll();        // cheerio: b, a, c   jQuery: c, b, a
$('#c, #d').prevUntil('#a');  // cheerio: b, c      jQuery: c, b

For parents* and prev*, jQuery sorts the results of a multi-element selection and then reverses them ("in order beginning with the closest sibling"). Cheerio already does this for parents and parentsUntil. This PR applies the same post-processing to prevAll and prevUntil. Single-element calls are unaffected and skip the sort.

The existing prevUntil test "(function) : should reset the index for each starting element" asserted the old order (orange, carrot). It now expects carrot, orange, which is what jQuery returns for the same selection. The index-reset behaviour it covers is unchanged.

Tests: added ordering tests for prevAll (same parent and different parents) and prevUntil. They fail on main and pass with the change. I checked the outputs against jQuery 4.0.0 on jsdom. npm run test:vi passes (838 tests), as do npm run lint:ts and npm run lint:biome.

View guided diff

… order

prevAll and prevUntil only removed duplicates when called on more than
one element, so the result was each starting element's siblings
concatenated, e.g. `$('#c, #d').prevAll()` gave `b, a, c`.

jQuery sorts these results and then reverses them (closest sibling
first), as cheerio already does for parents and parentsUntil. Use the
same post-processing for prevAll and prevUntil.
@coderabbitai

coderabbitai Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 686f6f42-1f22-47da-96c1-fd84339d9f45
📥 Commits

Reviewing files that changed from the base of the PR and between f222764 and aa42aec.

📒 Files selected for processing: 2
  • src/api/traversing.spec.ts
  • src/api/traversing.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

📝 Summary

Summary by CodeRabbit

  • Bug Fixes
    • Corrected the ordering of results from prevAll and prevUntil when called with multiple starting elements. Preceding siblings now appear in consistent traversal order, including when the elements come from different parents.
    • Updated the expected order for predicate-based prevUntil results.

prevAll and prevUntil now sort matches and reverse the results. Tests cover ordering for multiple starting elements, including selections across different parents.

Changes: Preceding sibling ordering

Layer / File(s) Summary
Sort and reverse preceding sibling results
src/api/traversing.ts, src/api/traversing.spec.ts
prevAll and prevUntil sort their matches and reverse the results. Tests check ordering for multiple starting elements and update the expected predicate-index order.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: fb55

Merge Risk: ⚪ Minimal · up to aa42a

No merge-blocking issue is identified; merge after normal checks.

Pre-merge checks: Passed 5
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: returning multi-element prevAll and prevUntil results in reverse document order.
Description check Passed The description directly explains the ordering bug, the implementation, the tests, and the reported validation results.
Linked Issues check Passed Issue #5676 requires multi-element prevAll() and prevUntil() results in reverse document order, with duplicate removal. At the reviewed head, src/api/traversing.ts applies uniqueSort and then …
Out of Scope Changes check Passed The reported changes are limited to src/api/traversing.ts and src/api/traversing.spec.ts. The source change implements issue #5676. The test additions and expectation update verify the required or…
Docstring Coverage Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2…
Full details: Linked Issues check

Explanation

Issue #5676 requires multi-element prevAll() and prevUntil() results in reverse document order, with duplicate removal. At the reviewed head, src/api/traversing.ts applies uniqueSort and then reverse() to both methods. The shared matcher applies post-processing only when the selection has more than one element, so single-element calls remain unchanged. The supplied change summary reports ordering tests for same-parent and different-parent selections and updated prevUntil coverage.

Full details: Out of Scope Changes check

Explanation

The reported changes are limited to src/api/traversing.ts and src/api/traversing.spec.ts. The source change implements issue #5676. The test additions and expectation update verify the required ordering and preserve index-reset coverage. No unrelated change is identified.

Full details: Docstring Coverage

Explanation

No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2 files.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-23T06:41:45.742321Z aa42aec PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

View guided diff | Re-trigger cubic

@greptile-apps

greptile-apps Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge.

Summary

Updates prevAll and prevUntil to return results in reverse document order when called on multiple elements.

  • .prevAll() and .prevUntil() return combined siblings in reverse document order.

Reviews (1) · Last reviewed commit: "fix(traversing): return prevAll/prevUnti..." · Reviewed by Greptile

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

prevAll() and prevUntil() return results out of order for multi-element selections

1 participant