fix: pre-encode XML to UTF-8 to avoid surrogate pair corruption in JSZip#3329
Open
Yuof wants to merge 2 commits intodolanmiu:masterfrom
Open
fix: pre-encode XML to UTF-8 to avoid surrogate pair corruption in JSZip#3329Yuof wants to merge 2 commits intodolanmiu:masterfrom
Yuof wants to merge 2 commits intodolanmiu:masterfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses a bug in JSZip where string chunking can split UTF-16 surrogate pairs when processing large XML content (> 16KB), resulting in invalid CESU-8 encoding instead of proper UTF-8. The fix pre-encodes XML strings to UTF-8 using TextEncoder before passing them to JSZip, bypassing the problematic chunking behavior.
Changes:
- Added
encodeUtf8()helper function to pre-encode strings to UTF-8 bytes - Modified all
zip.file()calls for XML content to use the pre-encoded UTF-8 bytes - Binary content (images, fonts) continues to be passed directly without encoding
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| src/patcher/from-docx.ts | Added encodeUtf8() helper and applied it to XML content in the patching workflow |
| src/export/packer/next-compiler.ts | Added encodeUtf8() helper and applied it to all XML file generation in the compiler |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When JSZip processes large XML content (> 16KB), it chunks strings using \substring()\ which operates on UTF-16 code units. This can split surrogate pairs for characters above U+FFFF (like emoji or Material Design Icons).
Each surrogate then gets encoded as a separate 3-byte UTF-8 sequence, producing invalid CESU-8 instead of proper UTF-8. This corrupts the docx file and causes XML parsing errors.
Root Cause
JSZip's \DataWorker\ uses:
\\javascript
DEFAULT_BLOCK_SIZE = 16 * 1024
data.substring(index, nextIndex)
\\
The \Utf8EncodeWorker\ then processes each chunk independently, without handling split surrogates. I've opened a fix PR to JSZip: Stuk/jszip#963
Solution
This PR works around the issue by pre-encoding strings to UTF-8 using \TextEncoder\ before passing to \zip.file(). Since we pass \Uint8Array\ instead of strings, JSZip skips the problematic \Utf8EncodeWorker\ entirely.
Changes
ext-compiler.ts\ and \rom-docx.ts\
Testing