Skip to content

[lexical-playground] Bug Fix: exporting an equation with non Latin-1 characters no longer throws - #9019

Closed
LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:fix/equation-unicode-export
Closed

LeSingh1 wants to merge 1 commit into
meta:mainfrom
LeSingh1:fix/equation-unicode-export

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Description

EquationNode.exportDOM base64-encodes the LaTeX with btoa:

// 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:

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)

…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)
```
@vercel

vercel Bot commented Aug 9, 2026

Copy link
Copy Markdown

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

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Aug 9, 2026
@LeSingh1

Copy link
Copy Markdown
Contributor Author

Consolidated into #9060 with the other PRs that share this defect, per @etrepum's note on #9027 and @mayrang's on #9035. Same fix and same tests, one review.

@LeSingh1 LeSingh1 closed this Aug 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant