Repository navigation
fix(filters): truncate and slice must not split surrogate pairs - #981
Merged
harttle merged 2 commits intoOct 28, 2026
Merged
Conversation
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.
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 |
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).
Owner
|
@all-contributors please add @OsamaAnsar for code. |
Contributor
|
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))
|
🎉 This PR is included in version 10.30.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
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.
truncateandslicecut 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:{{ "😀😀😀😀" | 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 insrc/util/underscore.ts. Ifstartfalls on the second half of a pair it moves forward by one unit, and ifendfalls between the two halves it moves back by one unit; then it slices.truncateandslice(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 handledstrip/lstrip/rstripcustom 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
slicedoes) 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.tsandarray.spec.ts. With only the threesrc/files reverted, 4 of the 6 new tests fail (the twotruncateand twoslicecases 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) andnpm run lintpass. I also compared 20,000 random BMP-only strings and arguments against the oldsubstring/sliceexpressions with no differences, and ran 50,000 random strings made ofa,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:
truncatewordsis not affected (it splits on whitespace only).{{ "😀" | first }}returns"\ud83d". I left those alone to keep this change small.