Skip to content

Commit 8dbbc1d

Browse files
fix(registry): caption keywords, emoji and aftershock work for words outside a-z (#7804)
caption-emoji-pop and caption-neon-accent look a word up in KEYWORDS and WORD_EMOJI after stripping everything outside a-z, and caption-glitch-rgb counts a word's \w characters to decide on the aftershock. A Korean, Cyrillic or accented word comes out empty or cut, so a keyword written in that script never matches and a long word never pulses. The three components now strip by Unicode letter (\p{L}) instead, which is the same set for a lowercased English transcript. Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 28d4ba5 commit 8dbbc1d

7 files changed

Lines changed: 98 additions & 10 deletions

File tree

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { readFileSync } from "node:fs";
2+
import vm from "node:vm";
3+
import { Window } from "happy-dom";
4+
import { describe, expect, it } from "vitest";
5+
6+
const LATIN = ["bright", "camera", "moment", "simple", "render", "motion"];
7+
const KOREAN = ["오늘", "영상", "처음", "공개", "끝까지", "감사"];
8+
const CYRILLIC = ["сегодня", "невероятно", "показываем", "результат", "спасибо", "друзья"];
9+
const LATIN_LONG = ["segodnya", "neveroyatn", "pokazyvaem", "rezultatt", "spasibo", "druzyaa"];
10+
11+
function source(name: string): string {
12+
const url = new URL(`../../../../registry/components/${name}/${name}.html`, import.meta.url);
13+
return readFileSync(url, "utf8");
14+
}
15+
16+
function withTranscript(html: string, words: string[], step = 0.5): string {
17+
const list = words.map(
18+
(text, i) => `{ text: "${text}", start: ${i * step}, end: ${i * step + 0.2} }`,
19+
);
20+
return html.replace(/var TRANSCRIPT = \[[\s\S]*?\];/, `var TRANSCRIPT = [${list.join(",")}];`);
21+
}
22+
23+
function withKeywords(html: string, words: string[]): string {
24+
return html.replace(
25+
/var KEYWORDS = new Set\(\[[^\]]*\]\);/,
26+
`var KEYWORDS = new Set(${JSON.stringify(words)});`,
27+
);
28+
}
29+
30+
/** Runs the component's inline script; returns its document and how many tweens it scheduled. */
31+
function run(html: string): { document: Window["document"]; tweens: number } {
32+
const window = new Window({
33+
settings: { disableJavaScriptEvaluation: true, disableJavaScriptFileLoading: true },
34+
});
35+
window.document.write(html);
36+
const scripts = [...window.document.querySelectorAll("script")];
37+
const script = scripts.filter((el) => !el.hasAttribute("src")).at(-1)!.textContent;
38+
for (const el of scripts) el.remove();
39+
const canvasContext = { font: "", measureText: () => ({ width: 0 }) };
40+
window.HTMLCanvasElement.prototype.getContext = (() =>
41+
canvasContext) as unknown as typeof window.HTMLCanvasElement.prototype.getContext;
42+
let tweens = 0;
43+
const tl: Record<string, () => unknown> = {};
44+
for (const method of ["set", "seek", "add", "call"]) tl[method] = () => tl;
45+
for (const method of ["to", "from", "fromTo"]) {
46+
tl[method] = () => {
47+
tweens += 1;
48+
return tl;
49+
};
50+
}
51+
const gsap = { timeline: () => tl, set: () => undefined, to: () => undefined };
52+
vm.runInNewContext(script, { window, document: window.document, gsap, console });
53+
return { document: window.document, tweens };
54+
}
55+
56+
function wordColors(html: string): string[] {
57+
const words = [...run(html).document.querySelectorAll(".caption-word")];
58+
return words.map((el) => (el as unknown as { style: { color: string } }).style.color);
59+
}
60+
61+
describe("caption components with a transcript that is not in Latin letters", () => {
62+
it("caption-emoji-pop shows the emoji mapped to a Korean word", () => {
63+
const html = withTranscript(source("caption-emoji-pop"), KOREAN).replace(
64+
/var WORD_EMOJI = \{[\s\S]*?\};/,
65+
'var WORD_EMOJI = { 영상: "📹" };',
66+
);
67+
const emoji = [...run(html).document.querySelectorAll(".caption-emoji")];
68+
expect(emoji.map((el) => el.textContent)).toEqual(["📹"]);
69+
});
70+
71+
it.each(["caption-emoji-pop", "caption-neon-accent"])(
72+
"%s accents Korean keywords the way it accents Latin ones",
73+
(name) => {
74+
const latin = wordColors(withKeywords(withTranscript(source(name), LATIN), LATIN));
75+
const korean = wordColors(withKeywords(withTranscript(source(name), KOREAN), KOREAN));
76+
expect(latin).toHaveLength(LATIN.length);
77+
expect(korean).toEqual(latin);
78+
},
79+
);
80+
81+
it("caption-glitch-rgb gives a long Cyrillic word the aftershock a long Latin word gets", () => {
82+
const tweens = (words: string[]) =>
83+
run(withTranscript(source("caption-glitch-rgb"), words, 0.47)).tweens;
84+
const [short, latin, cyrillic] = [tweens(LATIN), tweens(LATIN_LONG), tweens(CYRILLIC)];
85+
expect(latin).toBeGreaterThan(short);
86+
expect(cyrillic).toBe(latin);
87+
});
88+
});

‎registry/components/caption-emoji-pop/caption-emoji-pop.html‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@
241241
return rawWords
242242
.map(function (item) {
243243
var text = String(item.word || item.text || "").trim();
244-
var clean = text.toLowerCase().replace(/[^a-z]/g, "");
244+
var clean = text.toLowerCase().replace(/[^\p{L}]/gu, "");
245245
return {
246246
text: text,
247247
start: Math.max(0, Number(item.start) || 0),
@@ -261,7 +261,7 @@
261261

262262
function assignWordColors(words, groupIndex) {
263263
return words.map(function (word, index) {
264-
var clean = word.text.toLowerCase().replace(/[^a-z]/g, "");
264+
var clean = word.text.toLowerCase().replace(/[^\p{L}]/gu, "");
265265
if (KEYWORDS.has(clean)) {
266266
var ci = Math.floor(seededRandom(groupIndex * 23 + index * 31) * ACCENT_COLORS.length);
267267
return ACCENT_COLORS[ci];

‎registry/components/caption-emoji-pop/demo.html‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@
239239
return rawWords
240240
.map(function (item) {
241241
var text = String(item.word || item.text || "").trim();
242-
var clean = text.toLowerCase().replace(/[^a-z]/g, "");
242+
var clean = text.toLowerCase().replace(/[^\p{L}]/gu, "");
243243
return {
244244
text: text,
245245
start: Math.max(0, Number(item.start) || 0),
@@ -259,7 +259,7 @@
259259

260260
function assignWordColors(words, groupIndex) {
261261
return words.map(function (word, index) {
262-
var clean = word.text.toLowerCase().replace(/[^a-z]/g, "");
262+
var clean = word.text.toLowerCase().replace(/[^\p{L}]/gu, "");
263263
if (KEYWORDS.has(clean)) {
264264
var ci = Math.floor(seededRandom(groupIndex * 23 + index * 31) * ACCENT_COLORS.length);
265265
return ACCENT_COLORS[ci];

‎registry/components/caption-glitch-rgb/caption-glitch-rgb.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@
196196
}
197197

198198
function shouldAftershock(word) {
199-
return /\d|\$|%/.test(word.text) || word.text.replace(/[^\w]/g, "").length >= 7;
199+
return /\d|\$|%/.test(word.text) || word.text.replace(/[^\p{L}\p{N}_]/gu, "").length >= 7;
200200
}
201201

202202
var RAW_GROUPS = buildGroups(TRANSCRIPT);

‎registry/components/caption-glitch-rgb/demo.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@
194194
}
195195

196196
function shouldAftershock(word) {
197-
return /\d|\$|%/.test(word.text) || word.text.replace(/[^\w]/g, "").length >= 7;
197+
return /\d|\$|%/.test(word.text) || word.text.replace(/[^\p{L}\p{N}_]/gu, "").length >= 7;
198198
}
199199

200200
var RAW_GROUPS = buildGroups(TRANSCRIPT);

‎registry/components/caption-neon-accent/caption-neon-accent.html‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@
272272
}
273273

274274
function isKeyword(text) {
275-
return KEYWORDS.has(text.toLowerCase().replace(/[^a-z]/g, ""));
275+
return KEYWORDS.has(text.toLowerCase().replace(/[^\p{L}]/gu, ""));
276276
}
277277

278278
function assignWordColors(words, groupIndex) {
@@ -281,7 +281,7 @@
281281
var colorIndex = Math.floor(seededRandom(groupIndex * 23 + 7) * ACCENT_COLORS.length);
282282
return ACCENT_COLORS[colorIndex];
283283
}
284-
var clean = word.text.toLowerCase().replace(/[^a-z']/g, "");
284+
var clean = word.text.toLowerCase().replace(/[^\p{L}']/gu, "");
285285
if (TRANSITION_WORDS.has(clean)) return PRIMARY_COLOR;
286286
var rand = seededRandom(groupIndex * 7 + words.indexOf(word));
287287
if (rand > 0.6) {

‎registry/components/caption-neon-accent/demo.html‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@
270270
}
271271

272272
function isKeyword(text) {
273-
return KEYWORDS.has(text.toLowerCase().replace(/[^a-z]/g, ""));
273+
return KEYWORDS.has(text.toLowerCase().replace(/[^\p{L}]/gu, ""));
274274
}
275275

276276
function assignWordColors(words, groupIndex) {
@@ -279,7 +279,7 @@
279279
var colorIndex = Math.floor(seededRandom(groupIndex * 23 + 7) * ACCENT_COLORS.length);
280280
return ACCENT_COLORS[colorIndex];
281281
}
282-
var clean = word.text.toLowerCase().replace(/[^a-z']/g, "");
282+
var clean = word.text.toLowerCase().replace(/[^\p{L}']/gu, "");
283283
if (TRANSITION_WORDS.has(clean)) return PRIMARY_COLOR;
284284
var rand = seededRandom(groupIndex * 7 + words.indexOf(word));
285285
if (rand > 0.6) {

0 commit comments

Comments
 (0)