Repository navigation
Grapheme-safe text: count characters the way people do - #4776
Conversation
Several user-visible string operations indexed by UTF-16 code units, miscounting or corrupting emoji, flags, ZWJ sequences, and combining marks: - TextArea's character counter (display, over-limit state, aria-invalid, warning icon, and live-region announcements) showed an emoji as 2+ characters. All limit math now counts user-perceived characters, and is skipped entirely when no maxLength is set. - PowerSearch's two truncation helpers could split a surrogate pair or drop a combining mark at the cut point. Both now cut on grapheme boundaries with their exact previous thresholds and ellipses. - Table's auto-generated column headers failed to capitalize astral-plane letters (charAt(0) grabbed half a surrogate pair). Adds shared utils/grapheme.ts (graphemeLength, firstGrapheme, truncateGraphemes) built on a reused Intl.Segmenter with a code-point fallback — extracted from Avatar's merged meta#4750 fix, which now consumes the shared util. For pure-ASCII input every refactored site is byte-identical to the previous behavior at all reachable parameters. Fixes meta#4759
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
PR Analysis Report📚 Storybook PreviewView Storybook for this PR 🧪 Sandbox PreviewView Sandbox for this PR Modified ComponentsAvatar (@astryxdesign/core) · View in Storybook
PowerSearch (@astryxdesign/core) · View in Storybook
Table (@astryxdesign/core) · View in Storybook
TextArea (@astryxdesign/core) · View in Storybook
Bundle Size Summary
Accessibility AuditStatus: 1 accessibility violation(s) found — 1 serious. PowerSearch - 1 issue(s)
Generated by PR Enrichment workflow | Storybook | Sandbox | View full report |
Also memoize TextArea's counter length, harden the combining-mark test fixture against NFC normalization, and point maxLength consumers at graphemeLength for validation parity. Streaming reveal follow-up filed as meta#4779.
nynexman4464
left a comment
There was a problem hiding this comment.
Thanks for taking this on! I have some minor feedback on naming (I think grapheme is a little too technical) but otherwise LGTM
Review feedback on the new public API: `grapheme` is Unicode jargon and reads as too technical for consumers. graphemeLength -> characterCount firstGrapheme -> firstCharacter truncateGraphemes -> truncateCharacters Renames the file, the internal helpers and the doc prose to match, so the word only survives where it is the literal `Intl.Segmenter` granularity value. No behavior change: every function body is unchanged apart from the identifiers.
|
@AKnassa is attempting to deploy a commit to the Meta Open Source Team on Vercel. A member of the Team first needs to authorize it. |
nynexman4464
left a comment
There was a problem hiding this comment.
great thanks for helping to fix!
cixzhang
left a comment
There was a problem hiding this comment.
Thanks I like this consolidation!
What this does
Makes character counting and text shortening treat an emoji, flag, or accented character as one character — the way people count — instead of the way computers store them.
Why
Reported in #4759: several spots indexed text by internal storage units. The TextArea character counter told you one emoji was "2 characters" (and announced the wrong count to screen readers), PowerSearch could slice an emoji in half when shortening a filter token (leaving a broken � character), and auto-generated Table headers couldn't capitalize some scripts.
Fixes #4759
What changed
characterCount,firstCharacter,truncateCharacters— also exported for consumers).How to see it
In Storybook, open TextArea's Default story, set a maxLength, and type
Hi 😀👍🇹🇷👨👩👧👦!— the counter reads 8, not 23. Verified live in Chrome; also fuzz-verified that plain-English behavior is byte-for-byte unchanged everywhere.For reviewers
The two truncation helpers keep their exact thresholds (
maxLength − 1+…and thelimit + 3grace window +...) — one shared function expresses both. The multi-item list gates (joined.length <= maxLength) deliberately stay code-unit-based: they never slice, so no corruption is possible, and changing them would alter when lists collapse to "N items". Two changesets ([fix] + [feat] for the new public utils, both patch per the 0.x rule). A same-family issue inuseStreamingText(reveal ticks can transiently split emoji) surfaced during review and is filed as #4779.A note on ownership
@nynexman4464 self-assigned this issue the same day they filed it. If you're already working on it, happy to defer to your fix or close this one — no hard feelings.