Skip to content

fix: use JSON.stringify for caption text escaping in generated JS#625

Open
hobostay wants to merge 1 commit intoheygen-com:mainfrom
hobostay:fix/caption-generator-string-escaping
Open

fix: use JSON.stringify for caption text escaping in generated JS#625
hobostay wants to merge 1 commit intoheygen-com:mainfrom
hobostay:fix/caption-generator-string-escaping

Conversation

@hobostay
Copy link
Copy Markdown

@hobostay hobostay commented May 5, 2026

Summary

  • Replace manual single-quote escaping with JSON.stringify() in caption JS generation
  • Manual escaping only handled backslash and single quotes, missing newlines, carriage returns, and other special characters that break generated JavaScript syntax

Details

Affected file: packages/studio/src/captions/generator.ts (lines 306-313)

A caption segment containing a literal newline (e.g. from a multiline caption) would produce broken JS like:

segVar.textContent = 'line1
line2';  // SyntaxError: Invalid or unexpected token

JSON.stringify handles all special characters correctly and produces a double-quoted string literal that is always valid JS.

Test plan

  • Create captions with special characters (newlines, quotes, backslashes) and verify the generated HTML is valid
  • Verify existing single-line captions still render correctly

🤖 Generated with Claude Code

The generated JavaScript used manual single-quote escaping
(replace(/\\/g, '\\\\').replace(/'/g, \"'\\\\''\")) which missed
newlines, carriage returns, and other special characters. Captions
containing line breaks would produce syntactically broken JS.

Use JSON.stringify which handles all JS string special characters
correctly, including newlines, unicode, and control characters.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Copy link
Copy Markdown
Collaborator

@jrusso1020 jrusso1020 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified — generator.ts:306 only handles \\ and ', so a caption segment with a literal newline (or a U+2028/U+2029) emits broken JS. Switching to JSON.stringify is the right call: it returns a complete double-quoted JS string literal that handles all control chars and line separators, and the surrounding quotes are correctly removed at the call sites. LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants