Skip to content

Commit bc799c4

Browse files
only21milcodexclaude
authored
fix(web): give non-Latin theme names distinct stable ids (#23162)
Co-authored-by: GPT-6.1 Sol <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 6262c9a commit bc799c4

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

‎apps/web/src/themePalette.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ function contrastRatio(first: string, second: string): number {
6161
}
6262

6363
describe("theme files", () => {
64+
it("gives non-Latin theme names distinct stable ids", () => {
65+
const file = {
66+
version: THEME_FILE_VERSION,
67+
appearance: "dark",
68+
colors: { canvas: "#101820" },
69+
};
70+
const night = parseThemeFile({ ...file, name: "夜" });
71+
expect(parseThemeFile({ ...file, name: "朝" }).id).not.toBe(night.id);
72+
expect(parseThemeFile({ ...file, name: " 夜 " }).id).toBe(night.id);
73+
});
74+
6475
it("keeps built-in and standard search pairs readable and distinct from inline code", () => {
6576
const toOklab = converter("oklab");
6677
const distance = (first: string, second: string) => {

‎apps/web/src/themePalette.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,13 +1220,20 @@ export function getThemePreferenceMode(theme: ThemePreference): ThemeAppearance
12201220
}
12211221

12221222
export function themeIdFromName(name: string): string {
1223-
const normalized = name
1224-
.trim()
1225-
.toLowerCase()
1223+
const label = name.trim().toLowerCase();
1224+
const normalized = label
12261225
.replace(/[^a-z0-9]+/g, "-")
12271226
.replace(/^-+|-+$/g, "")
12281227
.slice(0, 48);
1229-
return normalized || "custom-theme";
1228+
if (normalized || !label) return normalized || "custom-theme";
1229+
1230+
// Names without ASCII letters or digits still need independent, stable ids.
1231+
let hash = 2_166_136_261;
1232+
for (let index = 0; index < label.length; index += 1) {
1233+
hash ^= label.charCodeAt(index);
1234+
hash = Math.imul(hash, 16_777_619);
1235+
}
1236+
return `theme-${(hash >>> 0).toString(36)}`;
12301237
}
12311238

12321239
export class ThemeLibraryStorageError extends Schema.TaggedError<ThemeLibraryStorageError>()(

0 commit comments

Comments
 (0)