Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions packages/cli/src/registry/captionWordGroups.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { readFileSync } from "node:fs";
import vm from "node:vm";
import { Window } from "happy-dom";
import { describe, expect, it } from "vitest";

const COMPONENTS = ["caption-highlight", "caption-clip-wipe", "caption-particle-burst"];
const DEMO_GROUP_SIZES = [4, 4, 1, 4, 3, 1, 3, 3, 2, 3];
const KOREAN = [
...["오늘은", "정말", "특별한", "날이에요.", "우리가", "함께", "만든", "영상을"],
...["처음으로", "세상에", "공개합니다.", "끝까지", "봐", "주세요.", "감사합니다."],
];

function source(name: string): string {
const url = new URL(`../../../../registry/components/${name}/${name}.html`, import.meta.url);
return readFileSync(url, "utf8");
}

function withWords(html: string, words: string[]): string {
const list = words.map(
(text, i) => `{ text: "${text}", start: ${i * 0.5}, end: ${i * 0.5 + 0.45} }`,
);
return html.replace(/var WORDS = \[[\s\S]*?\];/, `var WORDS = [${list.join(",")}];`);
}

function fakeTimeline(): Record<string, () => unknown> {
const tl: Record<string, () => unknown> = {};
for (const method of ["set", "to", "from", "fromTo", "seek", "add", "call"])
tl[method] = () => tl;
return tl;
}

/** Runs the component's inline script and returns how many words each caption group shows. */
function groupSizes(html: string): number[] {
const window = new Window();
window.document.write(html.replace(/<script\b[\s\S]*?<\/script>/gi, ""));
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
const canvasContext = { font: "", measureText: () => ({ width: 0 }) };
window.HTMLCanvasElement.prototype.getContext = (() =>
canvasContext) as unknown as typeof window.HTMLCanvasElement.prototype.getContext;
const script = [...html.matchAll(/<script>([\s\S]*?)<\/script>/gi)].at(-1)![1]!;
const gsap = { timeline: fakeTimeline, set: () => undefined, to: () => undefined };
vm.runInNewContext(script, { window, document: window.document, gsap, console });
const container = window.document.querySelector('[id$="-container"]')!;
return [...container.children]
.filter((child) => child.className.endsWith("-group"))
.map((group) => group.querySelectorAll('[class$="-word"]').length);
}

describe("caption components with hand-tuned word groups", () => {
it.each(COMPONENTS)("%s keeps the demo transcript's groups", (name) => {
expect(groupSizes(source(name))).toEqual(DEMO_GROUP_SIZES);
});

it.each(COMPONENTS)("%s groups a replaced 15-word transcript instead of crashing", (name) => {
const sizes = groupSizes(withWords(source(name), KOREAN));
expect(sizes.reduce((sum, n) => sum + n, 0)).toBe(KOREAN.length);
});
});
32 changes: 31 additions & 1 deletion registry/components/caption-clip-wipe/caption-clip-wipe.html
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@

var KEYWORDS = new Set([8, 12, 15, 24, 27]); // HyperFrames, HTML, professional, code, cinema.

var RAW_GROUPS = [
// Hand-tuned for the demo WORDS; a replaced transcript is grouped by buildGroups instead.
var DEMO_GROUPS = [
[0, 3],
[4, 7],
[8, 8],
Expand All @@ -150,6 +151,35 @@
[23, 24],
[25, 27],
];

function buildGroups(words) {
var groups = [];
var start = 0;
var chars = 0;
for (var i = 0; i < words.length; i++) {
chars += words[i].text.length + 1;
var endsSentence = /[.!?]$/.test(words[i].text);
var nextGap = i + 1 < words.length ? words[i + 1].start - words[i].end : 0;
var full = i - start >= 3 || chars >= 21;
if (endsSentence || nextGap > 0.28 || full || i === words.length - 1) {
groups.push([start, i]);
start = i + 1;
chars = 0;
}
}
return groups;
}

function tilesWords(groups, words) {
var next = 0;
for (var i = 0; i < groups.length; i++) {
if (groups[i][0] !== next || groups[i][1] < next) return false;
next = groups[i][1] + 1;
}
return next === words.length;
}

var RAW_GROUPS = tilesWords(DEMO_GROUPS, WORDS) ? DEMO_GROUPS : buildGroups(WORDS);
var GROUPS = RAW_GROUPS.map(function (pair, gi) {
var ws = pair[0],
we = pair[1];
Expand Down
32 changes: 31 additions & 1 deletion registry/components/caption-highlight/caption-highlight.html
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@
{ text: "cinema.", start: 7.3, end: 7.7 },
];

var RAW_GROUPS = [
// Hand-tuned for the demo WORDS; a replaced transcript is grouped by buildGroups instead.
var DEMO_GROUPS = [
[0, 3],
[4, 7],
[8, 8],
Expand All @@ -175,6 +176,35 @@
[23, 24],
[25, 27],
];

function buildGroups(words) {
var groups = [];
var start = 0;
var chars = 0;
for (var i = 0; i < words.length; i++) {
chars += words[i].text.length + 1;
var endsSentence = /[.!?]$/.test(words[i].text);
var nextGap = i + 1 < words.length ? words[i + 1].start - words[i].end : 0;
var full = i - start >= 3 || chars >= 21;
if (endsSentence || nextGap > 0.28 || full || i === words.length - 1) {
groups.push([start, i]);
start = i + 1;
chars = 0;
}
}
return groups;
}

function tilesWords(groups, words) {
var next = 0;
for (var i = 0; i < groups.length; i++) {
if (groups[i][0] !== next || groups[i][1] < next) return false;
next = groups[i][1] + 1;
}
return next === words.length;
}

var RAW_GROUPS = tilesWords(DEMO_GROUPS, WORDS) ? DEMO_GROUPS : buildGroups(WORDS);
var GROUPS = RAW_GROUPS.map(function (pair, gi) {
var ws = pair[0],
we = pair[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@
// Keywords get particle burst + gold color
var KEYWORDS = new Set([8, 12, 15, 24, 27]); // HyperFrames, HTML, professional, code, cinema.

var RAW_GROUPS = [
// Hand-tuned for the demo WORDS; a replaced transcript is grouped by buildGroups instead.
var DEMO_GROUPS = [
[0, 3],
[4, 7],
[8, 8],
Expand All @@ -171,6 +172,35 @@
[23, 24],
[25, 27],
];

function buildGroups(words) {
var groups = [];
var start = 0;
var chars = 0;
for (var i = 0; i < words.length; i++) {
chars += words[i].text.length + 1;
var endsSentence = /[.!?]$/.test(words[i].text);
var nextGap = i + 1 < words.length ? words[i + 1].start - words[i].end : 0;
var full = i - start >= 3 || chars >= 21;
if (endsSentence || nextGap > 0.28 || full || i === words.length - 1) {
groups.push([start, i]);
start = i + 1;
chars = 0;
}
}
return groups;
}

function tilesWords(groups, words) {
var next = 0;
for (var i = 0; i < groups.length; i++) {
if (groups[i][0] !== next || groups[i][1] < next) return false;
next = groups[i][1] + 1;
}
return next === words.length;
}

var RAW_GROUPS = tilesWords(DEMO_GROUPS, WORDS) ? DEMO_GROUPS : buildGroups(WORDS);
var GROUPS = RAW_GROUPS.map(function (pair, gi) {
var ws = pair[0],
we = pair[1];
Expand Down
Loading