Skip to content

Count negative indexes from the end in eq(), get() and slice(), and take an end index in slice() - #149

Open
gravitypdf-bot wants to merge 4 commits into
mainfrom
fix/issue-142
Open

gravitypdf-bot wants to merge 4 commits into
mainfrom
fix/issue-142

Conversation

@gravitypdf-bot

Copy link
Copy Markdown
Contributor

Fixes #142 and #144. They share a PR because both rewrite slice().

#142: negative indexes

eq(), get() and slice() count a negative index back from the end, as jQuery does. DOM::getNthMatch() and slice() resolve it with Util::normalizePositionalIndex(), the helper :eq() already uses. That helper is now public. An index still out of range gives an empty set, or NULL from get().

#144: slice($start, $end) — breaking change

The second argument is now an exclusive end index, as in jQuery, instead of a length:

  • slice(1, 3) returns two matches, where it used to return three.
  • A negative $end counts back from the end.
  • An explicit 0 now gives an empty set; it used to mean "to the end".
  • To keep the old result, use slice($start, $start + $length).

Tests

  • tests/Issues/Issue142Test.php and Issue144Test.php cover each issue's table against jQuery 3.7.1.
  • DOMQueryTest::testSlice now passes slice(1, 3) instead of slice(1, 2).

Every PR in this set adds its CHANGELOG line at the end of # Unreleased changes, so each one after the first needs a one-line conflict resolved there.

🤖 Generated with Claude Code

gravitypdf-bot and others added 4 commits October 26, 2026 17:17
getNthMatch() returned nothing for a negative index, so eq(-1) was empty
and get(-1) NULL, and slice() kept every match from a negative start.
As in jQuery, a negative index now counts back from the end of the set.

Fixes #142.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
slice($start, $length) followed array_slice(), with a length of 0
meaning "to the end". It is now slice($start, $end), as in jQuery:
the slice stops before $end, a negative $end counts back from the end,
and an omitted $end runs to the end of the set.

Fixes #144.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Oct 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.25%. Comparing base (6ae6acf) to head (47a9fe7).
⚠️ Report is 19 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main     #149      +/-   ##
============================================
+ Coverage     90.69%   91.25%   +0.56%     
- Complexity     1424     1438      +14     
============================================
  Files            25       26       +1     
  Lines          3170     3180      +10     
============================================
+ Hits           2875     2902      +27     
+ Misses          295      278      -17     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

eq(), get() and slice() ignore negative indexes; slice(-2) returns the whole set

1 participant