Skip to content

Commit c98d6fb

Browse files
authored
fix: keep caption transcript data inside generated scripts (#3847)
* fix: keep caption transcript data inside generated scripts * test: locate caption data without an HTML filtering regex
1 parent c752b89 commit c98d6fb

7 files changed

Lines changed: 85 additions & 12 deletions

File tree

‎packages/studio/src/captions/generator.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @vitest-environment node
22
import { describe, it, expect } from "vitest";
3+
import { Window } from "happy-dom";
34
import { generateCaptionHtml } from "./generator";
45
import { buildCaptionModel, TranscriptWord } from "./parser";
56

@@ -31,6 +32,35 @@ function buildTestModel(wordsPerGroup = 5) {
3132
// ---------------------------------------------------------------------------
3233

3334
describe("generateCaptionHtml", () => {
35+
it("keeps script-closing text and word IDs as data when HTML is parsed", () => {
36+
const text = '</ScRiPt><img src=x onerror="alert(1)"><!-- <script> & end';
37+
const id = '</script><svg onload="alert(2)">';
38+
const model = buildCaptionModel([{ text, id, start: 0, end: 1 }], {
39+
width: 1920,
40+
height: 1080,
41+
duration: 2,
42+
wordsPerGroup: 5,
43+
});
44+
const window = new Window();
45+
const template = window.document.createElement("template");
46+
template.innerHTML = generateCaptionHtml(model);
47+
const nested = template.content.querySelector("template");
48+
expect(nested).not.toBeNull();
49+
const content = nested!.content;
50+
expect(content.querySelector("img, svg")).toBeNull();
51+
const scripts = content.querySelectorAll("script");
52+
expect(scripts).toHaveLength(1);
53+
const script = scripts[0].textContent;
54+
const transcript = script.match(/const TRANSCRIPT = ([\s\S]*?);/);
55+
expect(transcript).not.toBeNull();
56+
expect(JSON.parse(transcript![1])).toEqual([{ text, id, start: 0, end: 1 }]);
57+
const wordText = script.match(/\.textContent = (.*);/);
58+
const wordId = script.match(/\.id = (.*);/);
59+
expect(JSON.parse(wordText![1])).toBe(text);
60+
expect(JSON.parse(wordId![1])).toBe(id);
61+
window.close();
62+
});
63+
3464
describe("HTML structure", () => {
3565
it("wraps output in a <template id='captions-template'>", () => {
3666
const model = buildTestModel();

‎packages/studio/src/captions/generator.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ function generateJs(model: CaptionModel): string {
277277
}
278278
}
279279

280-
const transcriptJson = JSON.stringify(allSegments, null, 2);
280+
const transcriptJson = JSON.stringify(allSegments, null, 2).replace(/</g, "\\u003c");
281281

282282
const groupBlocks: string[] = [];
283283

@@ -303,9 +303,11 @@ function generateJs(model: CaptionModel): string {
303303

304304
// Build word spans
305305
const wordLines: string[] = groupSegments.map((seg) => {
306-
const escaped = JSON.stringify(seg.text);
306+
const escaped = JSON.stringify(seg.text).replace(/</g, "\\u003c");
307307
const segVar = `w_${seg.id.replace(/[^a-zA-Z0-9_]/g, "_")}`;
308-
const idLine = seg.wordId ? `\n ${segVar}.id = ${JSON.stringify(seg.wordId)};` : "";
308+
const idLine = seg.wordId
309+
? `\n ${segVar}.id = ${JSON.stringify(seg.wordId).replace(/</g, "\\u003c")};`
310+
: "";
309311
return (
310312
` const ${segVar} = document.createElement('span');` +
311313
`\n ${segVar}.className = 'word clip';` +

‎skills-manifest.json‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"files": 142
77
},
88
"faceless-explainer": {
9-
"hash": "986ee23676272292",
9+
"hash": "76162336b4c88963",
1010
"files": 24
1111
},
1212
"figma": {
@@ -62,11 +62,11 @@
6262
"files": 132
6363
},
6464
"pr-to-video": {
65-
"hash": "4905e50c64aab195",
65+
"hash": "265c1f85d732d79c",
6666
"files": 30
6767
},
6868
"product-launch-video": {
69-
"hash": "960e964f32dcb4fa",
69+
"hash": "3e1efc70cad06b58",
7070
"files": 30
7171
},
7272
"remotion-to-hyperframes": {

‎skills/faceless-explainer/scripts/captions.mjs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ function buildFromSkin(skin, groups, total, W, H, tokens, die, faces = "", fonts
263263
out = fillOnce(
264264
out,
265265
/var GROUPS = \[\];/,
266-
`var GROUPS = ${JSON.stringify(groups)};`,
266+
`var GROUPS = ${JSON.stringify(groups).replace(/</g, "\\u003c")};`,
267267
"`var GROUPS = [];` hole",
268268
);
269269
out = fillOnce(out, /var DURATION = 0;/, `var DURATION = ${total};`, "`var DURATION = 0;` hole");
@@ -480,7 +480,7 @@ function buildCaptionsHtml(groups, total, W, H) {
480480
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/gsap.min.js" integrity="sha384-sG0Hv1tP1lZCk9KQmrIbY/XNwi+OY84GQqhMscbnsoBFqAz8KNCil1kvfL3Hbbk2" crossorigin="anonymous"></script>
481481
<script>
482482
(function () {
483-
var GROUPS = ${JSON.stringify(groups)};
483+
var GROUPS = ${JSON.stringify(groups).replace(/</g, "\\u003c")};
484484
var cap = document.getElementById("cap");
485485
var tl = gsap.timeline({ paused: true });
486486
GROUPS.forEach(function (g) {

‎skills/pr-to-video/scripts/captions.mjs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ function buildFromSkin(skin, groups, total, W, H, tokens, die, faces = "", fonts
263263
out = fillOnce(
264264
out,
265265
/var GROUPS = \[\];/,
266-
`var GROUPS = ${JSON.stringify(groups)};`,
266+
`var GROUPS = ${JSON.stringify(groups).replace(/</g, "\\u003c")};`,
267267
"`var GROUPS = [];` hole",
268268
);
269269
out = fillOnce(out, /var DURATION = 0;/, `var DURATION = ${total};`, "`var DURATION = 0;` hole");
@@ -480,7 +480,7 @@ function buildCaptionsHtml(groups, total, W, H) {
480480
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/gsap.min.js" integrity="sha384-sG0Hv1tP1lZCk9KQmrIbY/XNwi+OY84GQqhMscbnsoBFqAz8KNCil1kvfL3Hbbk2" crossorigin="anonymous"></script>
481481
<script>
482482
(function () {
483-
var GROUPS = ${JSON.stringify(groups)};
483+
var GROUPS = ${JSON.stringify(groups).replace(/</g, "\\u003c")};
484484
var cap = document.getElementById("cap");
485485
var tl = gsap.timeline({ paused: true });
486486
GROUPS.forEach(function (g) {

‎skills/pr-to-video/scripts/captions.test.mjs‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import assert from "node:assert/strict";
2+
import { execFileSync } from "node:child_process";
23
import { mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
34
import { tmpdir } from "node:os";
45
import { join } from "node:path";
@@ -217,3 +218,43 @@ test("every build-frame.mjs copy stages the style axis it promises", () => {
217218
);
218219
}
219220
});
221+
222+
for (const workflow of ["pr-to-video", "faceless-explainer", "product-launch-video"]) {
223+
for (const skin of [null, skins[0]]) {
224+
test(`${workflow} ${skin ? "skin" : "default"} keeps transcript markup inside script data`, () => {
225+
const project = mkdtempSync(join(tmpdir(), "caption-script-data-"));
226+
const text = '</ScRiPt><img src=x onerror="alert(1)"><!-- <script> & end';
227+
try {
228+
writeFileSync(
229+
join(project, "STORYBOARD.md"),
230+
"---\nformat: 1920x1080\nmessage: T\n---\n\n## Frame 1 — A\n- duration: 3s\n- src: compositions/frames/01-a.html\n",
231+
);
232+
writeFileSync(
233+
join(project, "audio_meta.json"),
234+
JSON.stringify({
235+
voices: [{ frame: 1, words: [{ text, start: 0, end: 1 }] }],
236+
}),
237+
);
238+
if (skin) writeFileSync(join(project, "caption-skin.html"), skin.source);
239+
const script = fileURLToPath(
240+
new URL(`../../${workflow}/scripts/captions.mjs`, import.meta.url),
241+
);
242+
execFileSync(process.execPath, [script, "build", "--hyperframes", project]);
243+
const html = readFileSync(join(project, "compositions/captions.html"), "utf8");
244+
const start = html.indexOf("var GROUPS =");
245+
assert.ok(start >= 0, "generated caption data exists");
246+
const end = html.toLowerCase().indexOf("</script", start);
247+
assert.ok(end > start, "generated script has a closing tag");
248+
const inline = html.slice(start, end);
249+
assert.ok(!html.includes("<img"), "transcript cannot create an HTML element");
250+
const data = inline.match(/var GROUPS = (.*);/);
251+
assert.ok(data, "entire data assignment remains inside the script element");
252+
const groups = JSON.parse(data[1]);
253+
assert.equal(groups[0].text, text);
254+
assert.equal(groups[0].words[0].text, text);
255+
} finally {
256+
rmSync(project, { recursive: true, force: true });
257+
}
258+
});
259+
}
260+
}

‎skills/product-launch-video/scripts/captions.mjs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ function buildFromSkin(skin, groups, total, W, H, tokens, die, faces = "", fonts
263263
out = fillOnce(
264264
out,
265265
/var GROUPS = \[\];/,
266-
`var GROUPS = ${JSON.stringify(groups)};`,
266+
`var GROUPS = ${JSON.stringify(groups).replace(/</g, "\\u003c")};`,
267267
"`var GROUPS = [];` hole",
268268
);
269269
out = fillOnce(out, /var DURATION = 0;/, `var DURATION = ${total};`, "`var DURATION = 0;` hole");
@@ -480,7 +480,7 @@ function buildCaptionsHtml(groups, total, W, H) {
480480
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/gsap.min.js" integrity="sha384-sG0Hv1tP1lZCk9KQmrIbY/XNwi+OY84GQqhMscbnsoBFqAz8KNCil1kvfL3Hbbk2" crossorigin="anonymous"></script>
481481
<script>
482482
(function () {
483-
var GROUPS = ${JSON.stringify(groups)};
483+
var GROUPS = ${JSON.stringify(groups).replace(/</g, "\\u003c")};
484484
var cap = document.getElementById("cap");
485485
var tl = gsap.timeline({ paused: true });
486486
GROUPS.forEach(function (g) {

0 commit comments

Comments
 (0)