Repository navigation
[lexical-playground] Bug Fix: exporting an equation with non Latin-1 characters no longer throws - #9019
Closed
LeSingh1 wants to merge 1 commit into
Closed
[lexical-playground] Bug Fix: exporting an equation with non Latin-1 characters no longer throws#9019LeSingh1 wants to merge 1 commit into
LeSingh1 wants to merge 1 commit into
Conversation
…characters no longer throws
## Description
`EquationNode.exportDOM` base64-encodes the LaTeX with `btoa`:
```ts
// Encode the equation as base64 to avoid issues with special characters
const equation = btoa(this.__equation);
element.setAttribute('data-lexical-equation', equation);
```
`btoa` only accepts Latin-1: it throws `InvalidCharacterError` on any code point above U+00FF. An equation is free-form LaTeX typed into a textarea, so those are ordinary content — `\text{α}`, CJK inside `\text{}`, an emoji. Insert one and then copy it, or use Export HTML, and `$generateHtmlFromNodes` → `exportDOM` throws and the whole copy/export fails. The comment one line above says the encoding is there "to avoid issues with special characters", which is precisely the input it breaks on.
The importer is the mirror image and has the same limit:
```ts
const equation = atob(encoded);
```
Even if `btoa` had not thrown, decoding through Latin-1 would mangle multi-byte content.
Both sides now go through the UTF-8 bytes, which is the same approach `utils/docSerialization.ts` already uses in this package for the shareable-document hash (`new TextEncoder().encode(...)` → `String.fromCharCode` → `btoa`, and the inverse). The helpers live next to the node and are shared with the import rule so the two cannot drift apart again.
Backwards compatible: for pure ASCII the UTF-8 bytes are identical to the Latin-1 bytes, so anything an older build exported decodes unchanged. Nothing else could have been exported, because `btoa` threw. `exportJSON` is untouched — it has always carried the raw string — so this is an HTML-only change with no serialization change.
## Test plan
New unit test `packages/lexical-playground/__tests__/unit/EquationNodeHTML.test.ts`, using the same export/re-import harness as the existing `PlaygroundNodeImporters` round-trip tests. The last case pins the backwards compatibility claim and passes before and after.
### Before
```
❯ packages/lexical-playground/__tests__/unit/EquationNodeHTML.test.ts (4 tests | 3 failed)
× exports an equation with non Latin-1 characters without throwing
AssertionError: expected [Function] to not throw an error but 'InvalidCharacterError: Invalid charac…' was thrown
× round trips an equation with non Latin-1 characters
InvalidCharacterError: Invalid character
× round trips an equation outside the basic multilingual plane
InvalidCharacterError: Invalid character
✓ still decodes an equation exported before the encoding change
Test Files 1 failed (1)
Tests 3 failed | 1 passed (4)
```
### After
```
Test Files 1 passed (1)
Tests 4 passed (4)
```
Package suite is unchanged:
```
$ npx vitest run packages/lexical-playground
Test Files 18 passed (18)
Tests 275 passed (275)
```
LeSingh1
requested review from
acywatson,
etrepum,
fantactuka,
ivailop7,
potatowagon and
zurfyx
as code owners
August 9, 2026 05:28
|
@LeSingh1 is attempting to deploy a commit to the Meta Open Source Team on Vercel. A member of the Team first needs to authorize it. |
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.
Description
EquationNode.exportDOMbase64-encodes the LaTeX withbtoa:btoaonly accepts Latin-1: it throwsInvalidCharacterErroron any code point above U+00FF. An equation is free-form LaTeX typed into a textarea, so those are ordinary content —\text{α}, CJK inside\text{}, an emoji. Insert one and then copy it, or use Export HTML, and$generateHtmlFromNodes→exportDOMthrows and the whole copy/export fails. The comment one line above says the encoding is there "to avoid issues with special characters", which is precisely the input it breaks on.The importer is the mirror image and has the same limit:
Even if
btoahad not thrown, decoding through Latin-1 would mangle multi-byte content.Both sides now go through the UTF-8 bytes, which is the same approach
utils/docSerialization.tsalready uses in this package for the shareable-document hash (new TextEncoder().encode(...)→String.fromCharCode→btoa, and the inverse). The helpers live next to the node and are shared with the import rule so the two cannot drift apart again.Backwards compatible: for pure ASCII the UTF-8 bytes are identical to the Latin-1 bytes, so anything an older build exported decodes unchanged. Nothing else could have been exported, because
btoathrew.exportJSONis untouched — it has always carried the raw string — so this is an HTML-only change with no serialization change.Test plan
New unit test
packages/lexical-playground/__tests__/unit/EquationNodeHTML.test.ts, using the same export/re-import harness as the existingPlaygroundNodeImportersround-trip tests. The last case pins the backwards compatibility claim and passes before and after.Before
After
Package suite is unchanged: