Repository navigation
fix(muya): delete emoji whole in code blocks and drop the caret-inside-cluster handler (#4926) - #5332
Merged
Merged
Conversation
…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
4 of 7 tasks
6 of 9 tasks
This was referenced Sep 14, 2026
Closed
Closed
Closed
Closed
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.
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
developbefore #4931, withabc🙂and the caret at the end:preventDefault)abc🙂→abc\ud83d[3, {d:"🙂"}, "\ud83d"]makeInvertible→strPosToUnithrows (#4892 stack)abc\ud83d→abc[3, {d:"\ud83d"}]apply→checkOp→dlenthrows (#5106 stack); JSON state is stuck atabc\ud83dabc→abcz[3, "z"]compose→xfBoundarythrows (#5270 / #5008 stack)ot-text-unicode'sstrPosToUnitreats every unit in 0xD800–0xDFFF as the start of a pair and skips the next one, so a lone surrogate overruns the offset. Witha🙂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 endCodeBlockContentextendsContent, notFormat, and overridesbackspaceHandlerwith 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/lastGraphemeLengthfrom #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:**, 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.So this removes the handler and
graphemeClusterContaining, plus the tests that forced the caret inside a cluster (graphemeDelete.spec.ts, one case informatBackspace.spec.ts, and the four "inside a pasted emoji / ZWJ sequence" cases in the desktop e2e).graphemeDelete.spec.tsnow drives the token trim throughkeydownHandlerfor 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
Test plan
codeBlockContent/__tests__/enterBackspaceHandler.spec.ts: the two emoji cases fail without commit 1 ('// ship �','// ok 👍�') and pass with it; the ASCII control passes both ways.graphemeDelete.spec.ts: 8 of 11 cases fail against the pre-fix: #4926 prevent emoji deletion from splitting surrogate pairs #4931format.ts; all pass now.vitest run224 files / 1549 tests pass;lint(no new warnings),lint:types,check-circularpass.Invalid offset - splits unicode byteswhen commit 1 is reverted.build:unpack+ Playwright/Electron):issue-4926-emoji-delete.spec.tspasses; paragraphabc🙂,🙂**b**and code block// ship 🚀Backspace + typing produce the expected markdown with no renderer errors.Notes for reviewers [optional]
firstGraphemeLength/lastGraphemeLength/codePointLengthfrom fix: #4926 prevent emoji deletion from splitting surrogate pairs #4931 are unchanged; only the caret-inside-cluster path is removed.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