Skip to content

fix(ui): keep truncateWithEndVisible code-point-safe in short-width fallback#9029

Open
greymoth-jp wants to merge 1 commit into
clerk:mainfrom
greymoth-jp:fix/truncate-end-visible-surrogate
Open

fix(ui): keep truncateWithEndVisible code-point-safe in short-width fallback#9029
greymoth-jp wants to merge 1 commit into
clerk:mainfrom
greymoth-jp:fix/truncate-end-visible-surrogate

Conversation

@greymoth-jp

@greymoth-jp greymoth-jp commented Jun 28, 2026

Copy link
Copy Markdown

truncateWithEndVisible keeps the start and end of a string and puts an ellipsis in the middle. The main path builds the result from Array.from(str), so it slices on code points and never breaks a character. The short-width fallback (taken when maxLength <= endChars + 3) instead returns ELLIPSIS + str.slice(-endChars), which slices on UTF-16 code units.

For characters outside the BMP (CJK Extension B kanji, emoji) each character is a surrogate pair of two code units, so str.slice(-endChars) can cut one in half and leave a lone surrogate:

truncateWithEndVisible('𠮷𠮷𠮷𠮷𠮷', 8, 5)
// before: '...\uDFB7𠮷𠮷'  (broken leading character)
// after:  '...𠮷𠮷𠮷𠮷𠮷'

The fallback now slices by code point, the same way the main path already does. ASCII inputs are unaffected and the existing tests still pass. Added a test covering the fallback with astral characters.

Summary by CodeRabbit

  • Bug Fixes
    • Improved text truncation so very short widths no longer split multi-code-point characters.
    • Fixed fallback truncation to preserve emoji and other non-BMP characters, avoiding broken replacement symbols.
    • Added test coverage to verify truncation remains correct for Unicode characters in edge cases.

@changeset-bot

changeset-bot Bot commented Jun 28, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 017122d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@clerk/ui Patch
@clerk/chrome-extension Patch
@clerk/swingset Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel

vercel Bot commented Jun 28, 2026

Copy link
Copy Markdown

@greymoth-jp is attempting to deploy a commit to the Clerk Production Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

truncateWithEndVisible now uses Array.from(str) instead of str.slice(-endChars) in the small-maxLength fallback, making it Unicode code-point safe. A test and changeset accompany the one-line fix.

Surrogate-safe truncation fix

Layer / File(s) Summary
Unicode-safe fallback + test
packages/ui/src/utils/truncateTextWithEndVisible.ts, packages/ui/src/utils/__tests__/truncateTextWithEndVisible.test.ts, .changeset/truncate-surrogate-safe.md
The small-maxLength branch now derives the trailing endChars via Array.from(str) instead of str.slice(-endChars). A new test asserts non-BMP CJK and emoji inputs are not split into replacement characters.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐇 A slice gone wrong split chars in two,
Surrogate pairs turned to ??? — oh no!
Array.from came hopping to save the day,
Each emoji and glyph kept whole on display.
No broken hearts, no broken 𠮷s here to stay!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and accurately summarizes the main change: making truncateWithEndVisible code-point-safe in the short-width fallback.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/ui/src/utils/truncateTextWithEndVisible.ts`:
- Line 23: The fallback in truncateTextWithEndVisible is running before the
code-point-aware fit check, so astral-only strings that already fit still get
truncated. Move the Array.from(str).slice(-endChars) / ELLIPSIS fallback to
execute only after the totalChars <= maxLength guard (or equivalent
code-point-length check) in truncateWithEndVisible, and update the related tests
so they either expect the original string when it fits or use a smaller
maxLength to cover the truncation path.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Repository UI (inherited)

Review profile: CHILL

Plan: Pro Plus

Run ID: 7d33ce58-9c73-4bca-ad18-6e721833bb5f

📥 Commits

Reviewing files that changed from the base of the PR and between de70b28 and 017122d.

📒 Files selected for processing (3)
  • .changeset/truncate-surrogate-safe.md
  • packages/ui/src/utils/__tests__/truncateTextWithEndVisible.test.ts
  • packages/ui/src/utils/truncateTextWithEndVisible.ts


if (maxLength <= endChars + ELLIPSIS_LENGTH) {
return ELLIPSIS + str.slice(-endChars);
return ELLIPSIS + Array.from(str).slice(-endChars).join('');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Hoist the code-point length check before this fallback.

Line 23 still runs before the code-point-aware totalChars <= maxLength guard, so astral-only strings that already fit are still truncated. For example, truncateWithEndVisible('🍣🍣🍣🍣🍣', 8, 5) now returns '...🍣🍣🍣🍣🍣' because Line 18 compares UTF-16 length. That also makes the new assertions at Lines 35-36 validate the wrong behavior; once this is fixed, they should either expect the original string or use a smaller maxLength to exercise the fallback.

Suggested fix
 export function truncateWithEndVisible(str: string, maxLength = 20, endChars = 5): string {
   const ELLIPSIS = '...';
   const ELLIPSIS_LENGTH = ELLIPSIS.length;
 
-  if (!str || str.length <= maxLength) {
+  if (!str) {
     return str;
   }
+
+  const chars = Array.from(str);
+  const totalChars = chars.length;
+
+  if (totalChars <= maxLength) {
+    return str;
+  }
 
   if (maxLength <= endChars + ELLIPSIS_LENGTH) {
-    return ELLIPSIS + Array.from(str).slice(-endChars).join('');
+    return ELLIPSIS + chars.slice(-endChars).join('');
   }
-
-  const chars = Array.from(str);
-  const totalChars = chars.length;
-
-  if (totalChars <= maxLength) {
-    return str;
-  }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
return ELLIPSIS + Array.from(str).slice(-endChars).join('');
export function truncateWithEndVisible(str: string, maxLength = 20, endChars = 5): string {
const ELLIPSIS = '...';
const ELLIPSIS_LENGTH = ELLIPSIS.length;
if (!str) {
return str;
}
const chars = Array.from(str);
const totalChars = chars.length;
if (totalChars <= maxLength) {
return str;
}
if (maxLength <= endChars + ELLIPSIS_LENGTH) {
return ELLIPSIS + chars.slice(-endChars).join('');
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/ui/src/utils/truncateTextWithEndVisible.ts` at line 23, The fallback
in truncateTextWithEndVisible is running before the code-point-aware fit check,
so astral-only strings that already fit still get truncated. Move the
Array.from(str).slice(-endChars) / ELLIPSIS fallback to execute only after the
totalChars <= maxLength guard (or equivalent code-point-length check) in
truncateWithEndVisible, and update the related tests so they either expect the
original string when it fits or use a smaller maxLength to cover the truncation
path.

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.

1 participant