mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-18 07:50:13 +00:00
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>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
28d4ba50f1
commit
8dbbc1d371
@@ -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<string, () => 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);
|
||||
});
|
||||
});
|
||||
@@ -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];
|
||||
|
||||
+2
-2
@@ -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];
|
||||
|
||||
@@ -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);
|
||||
|
||||
+1
-1
@@ -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);
|
||||
|
||||
@@ -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) {
|
||||
|
||||
+2
-2
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user