Skip to content

fix(muya): delete emoji whole in code blocks and drop the caret-inside-cluster handler (#4926) - #5332

Merged
Jocs merged 2 commits into
developfrom
fix/4926-emoji-delete-followups
Sep 14, 2026
Merged

Jocs merged 2 commits into
developfrom
fix/4926-emoji-delete-followups

Conversation

@Jocs

@Jocs Jocs commented Sep 14, 2026

Copy link
Copy Markdown
Member

Follow-up to #4931 (#4926). Related crash reports: #5270, #5106, #5008, #5027, #4892 — I'll close them with a note once this lands.

Summary

Root cause of the #4926 crash family

When the caret sits at an inline token boundary, Muya performs Backspace itself (muya#113). That emulation trimmed one UTF-16 code unit, so a token ending in an emoji kept half of its surrogate pair. Traced in Chrome on develop before #4931, with abc🙂 and the caret at the end:

Step Block text Text op Result
Backspace 1 (Muya handles it, preventDefault) abc🙂 → abc\ud83d [3, {d:"🙂"}, "\ud83d"] lone surrogate enters the state; makeInvertible → strPosToUni throws (#4892 stack)
Backspace 2 abc\ud83d → abc [3, {d:"\ud83d"}] apply → checkOp → dlen throws (#5106 stack); JSON state is stuck at abc\ud83d
Any later edit abc → abcz [3, "z"] the failed op is still queued; compose → xfBoundary throws (#5270 / #5008 stack)

ot-text-unicode's strPosToUni treats every unit in 0xD800–0xDFFF as the start of a pair and skips the next one, so a lone surrogate overruns the offset. With a🙂b (caret after the emoji but not at a token end) Muya does not intercept, the browser deletes the whole emoji, and nothing breaks.

#4931 fixed this for Format (paragraphs, headings, table cells). This PR has two commits:

1. fix(muya): delete whole characters on Backspace at a code token end

CodeBlockContent extends Content, not Format, and overrides backspaceHandler with its own Prism-token trim that still removed one code unit (token.content.substring(0, token.length - 1)). In a code block, // ship 🚀 + Backspace at the end still crashed after #4931. The trim now removes a whole grapheme cluster (firstGraphemeLength / lastGraphemeLength from #4931) and moves the caret by the removed length.

2. refactor(muya): drop the caret-inside-cluster delete handler

#4931 also added Content._deleteGraphemeClusterContainingCaret, which deletes a whole cluster when a collapsed caret sits strictly inside one. Measured in real browsers, that state does not occur and the handler is not what fixes the crash:

  • Ordinary interactions never put the caret inside a cluster. In Chrome, after each of 1059 clicks, 94 × each of ←/→/Shift+←/→/Alt+←/→, 176 vertical moves, 351 double clicks, 1767 drags, Backspace/Delete walks with undo, paste and Enter at every caret stop, and typed combining marks — 4736 checks of both the DOM selection and Muya's cursor over text with 🙂, 👍🏽, 🇨🇳, 👨‍👩‍👧, 1️⃣, combining accents and inline markup — the caret was inside a cluster 0 times and the handler fired 0 times.
  • Disabling it changes nothing. In Chromium and WebKit, 12 scenarios (the reported paths, two Backspaces in one frame, each cluster shape, emoji next to **, headings, Delete) plus Backspace/Delete at all 11 stops of the [Bug] emoji导致的渲染进程错误 #4926 sample behave identically with the handler on or off: no errors, state in sync, 0 hits. Even with the caret forced inside 🙂 through the API, the browser deletes the whole character without the handler.
  • It had a cost. It ran on every Backspace/Delete in every block and, whenever the caret was next to a non-ASCII character (e.g. any CJK text), segmented the block text from its start to the caret: about 1.6 ms at 10k characters, 15 ms at 100k, 76 ms at 500k.

So this removes the handler and graphemeClusterContaining, plus the tests that forced the caret inside a cluster (graphemeDelete.spec.ts, one case in formatBackspace.spec.ts, and the four "inside a pasted emoji / ZWJ sequence" cases in the desktop e2e). graphemeDelete.spec.ts now drives the token trim through keydownHandler for every cluster shape (astral, skin tone, flag, keycap, combining mark, ZWJ), emoji before inline markup, a leading ZWJ sequence, the #113 marker case, and an in-token Backspace that stays with the browser.

Type of change

  • Bug fix (non-breaking, fixes an issue)
  • New feature (non-breaking, adds functionality)
  • Breaking change (causes existing functionality to change)
  • Documentation update

Test plan

  • New tests added
  • muya: vitest run 224 files / 1549 tests pass; lint (no new warnings), lint:types, check-circular pass.
  • Real browsers (muya e2e host, Chromium + WebKit): all post-fix scenarios above pass with the caret position asserted before each key; the code-block cases fail with Invalid offset - splits unicode bytes when commit 1 is reverted.
  • Built desktop app (build:unpack + Playwright/Electron): issue-4926-emoji-delete.spec.ts passes; paragraph abc🙂, 🙂**b** and code block // ship 🚀 Backspace + typing produce the expected markdown with no renderer errors.
  • Manually tested on: macOS

Notes for reviewers [optional]


By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

🤖 Generated with Claude Code

https://claude.ai/code/session_01KGKDSxyPk9WxU2h5DgPcWn

Jocs and others added 2 commits September 14, 2026 11:21
…4926)

The Firefox-compat Backspace path in code blocks trimmed one UTF-16 code
unit from the highlighted token under the caret. A comment ending in an
astral emoji (`// ship 🚀`) lost only the low surrogate; the lone high
surrogate then crashed the next op with "Invalid offset - splits unicode
bytes". Trim a whole grapheme cluster and move the caret by its length,
mirroring the paragraph fix.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01KGKDSxyPk9WxU2h5DgPcWn
#4931 added `_deleteGraphemeClusterContainingCaret` to delete a whole
grapheme cluster when a collapsed caret sits strictly inside one. That state
does not occur in practice: in real Chrome, 4736 caret checks after clicks,
arrow and word navigation, vertical moves, double clicks, drags,
Backspace/Delete walks, undo, paste, Enter and typed combining marks never
left the DOM selection or Muya's cursor inside a cluster, and the handler
never fired. With it disabled, every #4926 crash scenario behaves the same
in Chromium and WebKit, and even a caret forced inside an emoji deletes the
whole character natively without error.

The #4926 crash comes from Muya's own token-boundary Backspace, which the
Format and code block trims now handle by grapheme. The handler only added a
grapheme segmentation of the block text on every Backspace/Delete next to a
non-ASCII character (about 15 ms at 100k characters), so remove it together
with `graphemeClusterContaining` and the tests that forced the caret inside
a cluster. The token-trim tests now go through `keydownHandler` and cover
every cluster shape.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01KGKDSxyPk9WxU2h5DgPcWn
@github-actions

Copy link
Copy Markdown
Contributor

Build artifacts for PR #5332:

Run: https://github.com/marktext/marktext/actions/runs/34802929576

Artifact Size Link
marktext-windows-arm64 279.4 MB Download
marktext-windows-x64 287.6 MB Download
marktext-macos-x64 297.6 MB Download
marktext-macos-arm64 287.3 MB Download
marktext-linux 637.8 MB Download

@Jocs
Jocs merged commit 2631d90 into develop Sep 14, 2026
16 checks passed
@Jocs
Jocs deleted the fix/4926-emoji-delete-followups branch September 14, 2026 05:29
@Jocs Jocs mentioned this pull request Nov 4, 2026
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.

1 participant