Skip to content

fix(for, tablerow): treat a negative offset or limit like Ruby Liquid - #1012

Merged
harttle merged 2 commits into
harttle:masterfrom
zzoraemon043:fix/negative-offset-limit
Nov 25, 2026
Merged

harttle merged 2 commits into
harttle:masterfrom
zzoraemon043:fix/negative-offset-limit

Conversation

@zzoraemon043

Copy link
Copy Markdown
Contributor

Array.prototype.slice counts a negative index from the end, so a negative offset or limit on {% for %} and {% tablerow %} dropped items from the wrong end:

{% for i in (0..3) offset: -2 %}{{ i }}{% endfor %}            → "23"  (Ruby Liquid: "0123")
{% for i in (0..3) offset: -2 limit: 3 %}{{ i }}{% endfor %}   → "23"  (Ruby Liquid: "0")
{% for i in (0..5) offset: 1 limit: -1 %}{{ i }}{% endfor %}   → "12"  (Ruby Liquid: "")
{% tablerow i in (1..3) offset: -1 %}{{ i }}{% endtablerow %}  → one cell "3"  (Ruby Liquid: 1, 2, 3)

Ruby Liquid keeps the items whose index is in [offset, offset + limit) (Utils.slice_collection), so a negative offset skips nothing but still shortens the limit, and a negative limit leaves nothing. Both tags now clamp the slice bounds at 0 and count the limit from the raw offset. offset: continue after a negative offset resumes at offset + length, same as before and same as Ruby.

These are the five tags/for negative offset/limit cases from jg-rp/golden-liquid listed in #1004 (range loop, negative offset, ..., continue, ..., limit, offset, negative limit, negative offset, negative limit); they pass now and the other golden for/tablerow results are unchanged. The two tablerow golden cases still differ only by the newlines Ruby writes after <tr>.

Tests: 7 new cases in for.spec.ts and tablerow.spec.ts fail on master and pass now. npm test (1782) passes with TZ Etc/GMT, Asia/Shanghai and America/New_York; npm run lint passes.

Comment thread src/tags/for.ts Resolved
@harttle
harttle merged commit 4ba4068 into harttle:master Nov 25, 2026
13 checks passed
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.

2 participants