fix(contracts): avoid Intl.Segmenter in monogram validation (Hermes crash) (#11984)
This commit is contained in:
1 parent
87a12b53fd
commit
6f7aaffe26
1 file changed
+24
-2
@@ -459,11 +459,33 @@ const ProjectLucideIconName = TrimmedNonEmptyString.check(
|
||||
|
||||
const ProjectEmoji = TrimmedNonEmptyString.check(Schema.isMaxLength(32));
|
||||
|
||||
const monogramSegmenter = new Intl.Segmenter(undefined, { granularity: "grapheme" });
|
||||
// Hermes (the Android/iOS JS runtime) has no Intl.Segmenter, and this module
|
||||
// loads at app startup, so `new Intl.Segmenter()` crashes mobile on launch.
|
||||
// Count grapheme clusters with a small approximation instead: a code point
|
||||
// continues the current cluster when it is a combining mark, joiner,
|
||||
// variation selector, or emoji modifier, or follows a ZWJ. This must stay
|
||||
// runtime-independent (not `typeof Intl.Segmenter` feature detection) so the
|
||||
// shared contract validates identically on server, web, and mobile.
|
||||
const GRAPHEME_CONTINUATION = /[\p{M}\u200c\u200d\ufe0f\u{1F3FB}-\u{1F3FF}]/u;
|
||||
|
||||
const countGraphemes = (text: string): number => {
|
||||
let clusters = 0;
|
||||
let prevJoiner = false;
|
||||
for (const char of text) {
|
||||
if (clusters > 0 && (prevJoiner || GRAPHEME_CONTINUATION.test(char))) {
|
||||
prevJoiner = char === "\u200d";
|
||||
continue;
|
||||
}
|
||||
clusters += 1;
|
||||
prevJoiner = char === "\u200d";
|
||||
}
|
||||
return clusters;
|
||||
};
|
||||
|
||||
export const ProjectMonogramText = TrimmedNonEmptyString.check(
|
||||
Schema.isMaxLength(32),
|
||||
Schema.isPattern(/^[\p{L}\p{N}][\p{L}\p{N}\p{M}\u200c\u200d]*$/u),
|
||||
Schema.makeFilter((text) => Array.from(monogramSegmenter.segment(text)).length <= 2),
|
||||
Schema.makeFilter((text) => countGraphemes(text) <= 2),
|
||||
);
|
||||
|
||||
export const ProjectIconOverride = Schema.Union([
|
||||
|
||||
Reference in new issue
Block a user