Skip to content

fix(filters): truncate and slice must not split surrogate pairs - #981

Merged
harttle merged 2 commits into
harttle:masterfrom
OsamaAnsar:fix/truncate-slice-surrogates
Oct 28, 2026
Merged

harttle merged 2 commits into
harttle:masterfrom
OsamaAnsar:fix/truncate-slice-surrogates

Conversation

@OsamaAnsar

Copy link
Copy Markdown
Contributor

truncate and slice cut strings by UTF-16 code unit, so a cut inside a surrogate pair leaves a lone surrogate in the output. A lone surrogate becomes U+FFFD when the text is encoded as UTF-8.

Output on current master (10.30.0), before this change:

template before after
{{ "😀😀😀😀" | truncate: 6 }} "😀\ud83d..." "😀..."
{{ "😀😀😀" | truncate: 5, "" }} "😀😀\ud83d" "😀😀"
{{ "a😀b" | slice: 1, 1 }} "\ud83d" ""
{{ "a😀b" | slice: 2, 2 }} "\ude00b" "b"
{{ "a😀b" | slice: 1, 2 }} "😀" "😀"

The fix adds a small sliceWholeChars(str, start, end) helper in src/util/underscore.ts. If start falls on the second half of a pair it moves forward by one unit, and if end falls between the two halves it moves back by one unit; then it slices. truncate and slice (strings only) use it. Only a pair that the cut would split is touched, so a lone surrogate already present in the input is left alone, and strings without surrogate pairs give the same output as before. It is in the same spirit as #955 (which handled strip/lstrip/rstrip custom characters).

Lengths are still counted in UTF-16 code units, as before. The visible effect is that the result can be one unit shorter than requested when the cut would have split a pair. Counting by code point (what Ruby's slice does) would be a larger behaviour change, so I left it out; happy to do that instead if you prefer.

Tests are added to test/integration/filters/string.spec.ts and array.spec.ts. With only the three src/ files reverted, 4 of the 6 new tests fail (the two truncate and two slice cases that split a pair) and the other 2 (a pair that ends exactly at the cut) pass both ways; with the change all pass.

Local run: npm run build, npm test (89 suites, 1702 passed, 4 skipped) and npm run lint pass. I also compared 20,000 random BMP-only strings and arguments against the old substring/slice expressions with no differences, and ran 50,000 random strings made of a, b, a CJK character, two astral characters and a space: 12,860 outputs contained a lone surrogate before the change and 0 after.

Not tested or not changed:

  • truncatewords is not affected (it splits on whitespace only).
  • Other filters can still return half of a pair, for example {{ "😀" | first }} returns "\ud83d". I left those alone to keep this change small.
  • I did not run the browser bundles or the docs build.

truncate and slice cut strings by UTF-16 code unit, so a cut that landed
inside an emoji or other astral character left a lone surrogate in the
output, e.g. {{ "a😀b" | slice: 1, 1 }} returned "\ud83d". A lone
surrogate becomes U+FFFD when the output is encoded as UTF-8.

Move a cut that falls between the two halves of a pair to the nearest
boundary inside the range. Lengths are still counted in UTF-16 code
units, so output may be shorter than requested by one unit. Strings
without surrogate pairs are unaffected.
@harttle

harttle commented Oct 27, 2026

Copy link
Copy Markdown
Owner

I find the result is still different from Ruby version, which uses code point instead of UTF16 bytes. Fortunately ES6 also supports code points. Is it possible to use something like [...str].slice to also align with the size counting of surrogates?

@harttle harttle added the bug label Oct 27, 2026
Count and cut by Unicode code point instead of UTF-16 code unit, as
Shopify/liquid does (String#length, String#[] and String#slice are
character based in Ruby). This also stops a cut from leaving half of a
surrogate pair behind, e.g. {{ "a😀b" | slice: 1, 1 }} returned "\ud83d"
and now returns "😀".

truncate counts the length of the ellipsis in code points as well.
Offsets are resolved by walking the string once, so a negative slice
offset or a deep truncate costs O(length of the string).
@harttle
harttle merged commit 57016eb into harttle:master Oct 28, 2026
13 checks passed
@harttle

harttle commented Oct 28, 2026

Copy link
Copy Markdown
Owner

@all-contributors please add @OsamaAnsar for code.

@allcontributors

Copy link
Copy Markdown
Contributor

@harttle

I've put up a pull request to add @OsamaAnsar! 🎉

github-actions Bot pushed a commit that referenced this pull request Oct 28, 2026
## [10.30.1](v10.30.0...v10.30.1) (2026-10-28)

### Bug Fixes

* apply outputEscape to {% echo %} ([46ab5d8](46ab5d8))
* **array_to_sentence_string:** coerce non-array input with toArray ([#968](#968)) ([e843466](e843466)), closes [#964](#964)
* **base64_decode:** throw on invalid input in every build ([#971](#971)) ([83bbfe0](83bbfe0)), closes [#965](#965)
* **date_to_rfc822:** use English day and month names ([#974](#974)) ([e94a1c4](e94a1c4)), closes [#973](#973)
* **filters:** round must not alter large or high-precision numbers ([#980](#980)) ([ea22af4](ea22af4)), closes [hi#precision](https://github.com/hi/issues/precision)
* **filters:** truncate and slice must not split surrogate pairs ([#981](#981)) ([57016eb](57016eb)), closes [String#length](https://github.com/String/issues/length) [String#slice](https://github.com/String/issues/slice)
* pad %Y to 4 digits like Ruby ([#978](#978)) ([95a2eed](95a2eed))
* **security:** apply outputEscape to {% cycle %} ([#984](#984)) ([2e1ebe0](2e1ebe0))
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 10.30.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants