diff --git a/packages/cli/src/registry/captionNonLatinKeywords.test.ts b/packages/cli/src/registry/captionNonLatinKeywords.test.ts new file mode 100644 index 0000000000..f1ee0032b9 --- /dev/null +++ b/packages/cli/src/registry/captionNonLatinKeywords.test.ts @@ -0,0 +1,88 @@ +import { readFileSync } from "node:fs"; +import vm from "node:vm"; +import { Window } from "happy-dom"; +import { describe, expect, it } from "vitest"; + +const LATIN = ["bright", "camera", "moment", "simple", "render", "motion"]; +const KOREAN = ["오늘", "영상", "처음", "공개", "끝까지", "감사"]; +const CYRILLIC = ["сегодня", "невероятно", "показываем", "результат", "спасибо", "друзья"]; +const LATIN_LONG = ["segodnya", "neveroyatn", "pokazyvaem", "rezultatt", "spasibo", "druzyaa"]; + +function source(name: string): string { + const url = new URL(`../../../../registry/components/${name}/${name}.html`, import.meta.url); + return readFileSync(url, "utf8"); +} + +function withTranscript(html: string, words: string[], step = 0.5): string { + const list = words.map( + (text, i) => `{ text: "${text}", start: ${i * step}, end: ${i * step + 0.2} }`, + ); + return html.replace(/var TRANSCRIPT = \[[\s\S]*?\];/, `var TRANSCRIPT = [${list.join(",")}];`); +} + +function withKeywords(html: string, words: string[]): string { + return html.replace( + /var KEYWORDS = new Set\(\[[^\]]*\]\);/, + `var KEYWORDS = new Set(${JSON.stringify(words)});`, + ); +} + +/** Runs the component's inline script; returns its document and how many tweens it scheduled. */ +function run(html: string): { document: Window["document"]; tweens: number } { + const window = new Window({ + settings: { disableJavaScriptEvaluation: true, disableJavaScriptFileLoading: true }, + }); + window.document.write(html); + const scripts = [...window.document.querySelectorAll("script")]; + const script = scripts.filter((el) => !el.hasAttribute("src")).at(-1)!.textContent; + for (const el of scripts) el.remove(); + const canvasContext = { font: "", measureText: () => ({ width: 0 }) }; + window.HTMLCanvasElement.prototype.getContext = (() => + canvasContext) as unknown as typeof window.HTMLCanvasElement.prototype.getContext; + let tweens = 0; + const tl: Record unknown> = {}; + for (const method of ["set", "seek", "add", "call"]) tl[method] = () => tl; + for (const method of ["to", "from", "fromTo"]) { + tl[method] = () => { + tweens += 1; + return tl; + }; + } + const gsap = { timeline: () => tl, set: () => undefined, to: () => undefined }; + vm.runInNewContext(script, { window, document: window.document, gsap, console }); + return { document: window.document, tweens }; +} + +function wordColors(html: string): string[] { + const words = [...run(html).document.querySelectorAll(".caption-word")]; + return words.map((el) => (el as unknown as { style: { color: string } }).style.color); +} + +describe("caption components with a transcript that is not in Latin letters", () => { + it("caption-emoji-pop shows the emoji mapped to a Korean word", () => { + const html = withTranscript(source("caption-emoji-pop"), KOREAN).replace( + /var WORD_EMOJI = \{[\s\S]*?\};/, + 'var WORD_EMOJI = { 영상: "📹" };', + ); + const emoji = [...run(html).document.querySelectorAll(".caption-emoji")]; + expect(emoji.map((el) => el.textContent)).toEqual(["📹"]); + }); + + it.each(["caption-emoji-pop", "caption-neon-accent"])( + "%s accents Korean keywords the way it accents Latin ones", + (name) => { + const latin = wordColors(withKeywords(withTranscript(source(name), LATIN), LATIN)); + const korean = wordColors(withKeywords(withTranscript(source(name), KOREAN), KOREAN)); + expect(latin).toHaveLength(LATIN.length); + expect(korean).toEqual(latin); + }, + ); + + it("caption-glitch-rgb gives a long Cyrillic word the aftershock a long Latin word gets", () => { + const tweens = (words: string[]) => + run(withTranscript(source("caption-glitch-rgb"), words, 0.47)).tweens; + const [short, latin, cyrillic] = [tweens(LATIN), tweens(LATIN_LONG), tweens(CYRILLIC)]; + expect(latin).toBeGreaterThan(short); + expect(cyrillic).toBe(latin); + }); +}); diff --git a/registry/components/caption-emoji-pop/caption-emoji-pop.html b/registry/components/caption-emoji-pop/caption-emoji-pop.html index f8a863d2e8..1c1ee39657 100644 --- a/registry/components/caption-emoji-pop/caption-emoji-pop.html +++ b/registry/components/caption-emoji-pop/caption-emoji-pop.html @@ -241,7 +241,7 @@ return rawWords .map(function (item) { var text = String(item.word || item.text || "").trim(); - var clean = text.toLowerCase().replace(/[^a-z]/g, ""); + var clean = text.toLowerCase().replace(/[^\p{L}]/gu, ""); return { text: text, start: Math.max(0, Number(item.start) || 0), @@ -261,7 +261,7 @@ function assignWordColors(words, groupIndex) { return words.map(function (word, index) { - var clean = word.text.toLowerCase().replace(/[^a-z]/g, ""); + var clean = word.text.toLowerCase().replace(/[^\p{L}]/gu, ""); if (KEYWORDS.has(clean)) { var ci = Math.floor(seededRandom(groupIndex * 23 + index * 31) * ACCENT_COLORS.length); return ACCENT_COLORS[ci]; diff --git a/registry/components/caption-emoji-pop/demo.html b/registry/components/caption-emoji-pop/demo.html index f488b6a6bf..c3cfc25207 100644 --- a/registry/components/caption-emoji-pop/demo.html +++ b/registry/components/caption-emoji-pop/demo.html @@ -239,7 +239,7 @@ return rawWords .map(function (item) { var text = String(item.word || item.text || "").trim(); - var clean = text.toLowerCase().replace(/[^a-z]/g, ""); + var clean = text.toLowerCase().replace(/[^\p{L}]/gu, ""); return { text: text, start: Math.max(0, Number(item.start) || 0), @@ -259,7 +259,7 @@ function assignWordColors(words, groupIndex) { return words.map(function (word, index) { - var clean = word.text.toLowerCase().replace(/[^a-z]/g, ""); + var clean = word.text.toLowerCase().replace(/[^\p{L}]/gu, ""); if (KEYWORDS.has(clean)) { var ci = Math.floor(seededRandom(groupIndex * 23 + index * 31) * ACCENT_COLORS.length); return ACCENT_COLORS[ci]; diff --git a/registry/components/caption-glitch-rgb/caption-glitch-rgb.html b/registry/components/caption-glitch-rgb/caption-glitch-rgb.html index fe212755ec..845727fa6e 100644 --- a/registry/components/caption-glitch-rgb/caption-glitch-rgb.html +++ b/registry/components/caption-glitch-rgb/caption-glitch-rgb.html @@ -196,7 +196,7 @@ } function shouldAftershock(word) { - return /\d|\$|%/.test(word.text) || word.text.replace(/[^\w]/g, "").length >= 7; + return /\d|\$|%/.test(word.text) || word.text.replace(/[^\p{L}\p{N}_]/gu, "").length >= 7; } var RAW_GROUPS = buildGroups(TRANSCRIPT); diff --git a/registry/components/caption-glitch-rgb/demo.html b/registry/components/caption-glitch-rgb/demo.html index c7cf5b9d77..a262271cc4 100644 --- a/registry/components/caption-glitch-rgb/demo.html +++ b/registry/components/caption-glitch-rgb/demo.html @@ -194,7 +194,7 @@ } function shouldAftershock(word) { - return /\d|\$|%/.test(word.text) || word.text.replace(/[^\w]/g, "").length >= 7; + return /\d|\$|%/.test(word.text) || word.text.replace(/[^\p{L}\p{N}_]/gu, "").length >= 7; } var RAW_GROUPS = buildGroups(TRANSCRIPT); diff --git a/registry/components/caption-neon-accent/caption-neon-accent.html b/registry/components/caption-neon-accent/caption-neon-accent.html index 1313eeb8e3..fdc48e8ab1 100644 --- a/registry/components/caption-neon-accent/caption-neon-accent.html +++ b/registry/components/caption-neon-accent/caption-neon-accent.html @@ -272,7 +272,7 @@ } function isKeyword(text) { - return KEYWORDS.has(text.toLowerCase().replace(/[^a-z]/g, "")); + return KEYWORDS.has(text.toLowerCase().replace(/[^\p{L}]/gu, "")); } function assignWordColors(words, groupIndex) { @@ -281,7 +281,7 @@ var colorIndex = Math.floor(seededRandom(groupIndex * 23 + 7) * ACCENT_COLORS.length); return ACCENT_COLORS[colorIndex]; } - var clean = word.text.toLowerCase().replace(/[^a-z']/g, ""); + var clean = word.text.toLowerCase().replace(/[^\p{L}']/gu, ""); if (TRANSITION_WORDS.has(clean)) return PRIMARY_COLOR; var rand = seededRandom(groupIndex * 7 + words.indexOf(word)); if (rand > 0.6) { diff --git a/registry/components/caption-neon-accent/demo.html b/registry/components/caption-neon-accent/demo.html index 90f45018e8..fa9448fa35 100644 --- a/registry/components/caption-neon-accent/demo.html +++ b/registry/components/caption-neon-accent/demo.html @@ -270,7 +270,7 @@ } function isKeyword(text) { - return KEYWORDS.has(text.toLowerCase().replace(/[^a-z]/g, "")); + return KEYWORDS.has(text.toLowerCase().replace(/[^\p{L}]/gu, "")); } function assignWordColors(words, groupIndex) { @@ -279,7 +279,7 @@ var colorIndex = Math.floor(seededRandom(groupIndex * 23 + 7) * ACCENT_COLORS.length); return ACCENT_COLORS[colorIndex]; } - var clean = word.text.toLowerCase().replace(/[^a-z']/g, ""); + var clean = word.text.toLowerCase().replace(/[^\p{L}']/gu, ""); if (TRANSITION_WORDS.has(clean)) return PRIMARY_COLOR; var rand = seededRandom(groupIndex * 7 + words.indexOf(word)); if (rand > 0.6) {