Skip to content

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

Open
hobostay wants to merge 1 commit into
heygen-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 into
heygen-com:mainfrom
hobostay:fix/caption-generator-string-escaping

Conversation

@hobostay
Copy link
Copy Markdown
Contributor

@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.

Copy link
Copy Markdown
Collaborator

@vanceingalls vanceingalls left a comment

Choose a reason for hiding this comment

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

Additive review at 49d4dbba@jrusso1020 already verified and approved.

Audited

  • packages/studio/src/captions/generator.ts:306-313 (the JSON.stringify replacement)
  • CI: all required green ✓

Strength

James covered the correctness — JSON.stringify produces a complete double-quoted JS string literal that handles control chars, U+2028/U+2029, embedded quotes, and backslashes correctly. The two failure modes the old manual regex missed (literal newlines, line separators) are both fixed. ✓

Important — supersedes the caption portion of #794 and #795

Both #794 (Jefsky) and #795 (Jefsky) ship the same caption fix (JSON.stringify(seg.text)) but bundle it with broken LayersPanel.tsx new-file additions that would clobber existing code on merge. Recommend merging this single-purpose PR and closing #794 + the caption portion of #795 to keep the surface clean. I've flagged the same in those PRs.

Nit

  • A positive-pin test for the contract would be small and high-value here — expect(generateCaptionJs("line1\nline2")).toContain('"line1\\nline2"') pins the JSON.stringify behavior against a future "let's roll our own escaping again" regression. Out of scope for this PR, but worth a follow-up.

Verdict

Verdict: COMMENT
Reasoning: Fix is correct (James verified); single-purpose diff, CI green. Vance check before merging — this is external-contributor code but it's the canonical one of three competing PRs for this bug. Recommend landing this and closing #794 + the caption portion of #795.

— Vai

Copy link
Copy Markdown
Collaborator

@vanceingalls vanceingalls left a comment

Choose a reason for hiding this comment

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

hobostay's caption JSON.stringify escape fix. @jrusso1020 already APPROVED on this SHA. CLEAN merge state. (Note: there's a duplicate-shape #794 from Jefsky against a broken bundle — close that one in favor of this.) Vance authorized the stamp.

Verdict: APPROVE
Reasoning: James verified, correct fix shape, CLEAN.

— Vai

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.

3 participants