Skip to content

[Bug] emoji导致的渲染进程错误 #4926

Closed

Description

@ACodeCopyer

Description

将其他地方(比如AI对话的回答中)复制的emoji表情复制到使用MarkText打开的md文档中再删除后会报错:意外的渲染进程错误

Steps to Reproduce

  1. 将emoji表情复制到使用MarkText打开的md文档中
  2. 在默认的编辑模式下删除emoji表情
  3. MarkText报错意外的渲染进程错误

Expected Behavior

删除emoji后正常渲染无报错

Actual Behavior

报错意外的渲染进程错误

Screenshots / Logs

意外的渲染进程错误
Error: Invalid offset - splits unicode bytes
at unicount.strPosToUni (file:///C:/Users/x/AppData/Local/Programs/marktext/resources/app.asar/out/renderer/assets/index-D8Yzs57g.js:99249:13)
at exports2.dlen (file:///C:/Users/x/AppData/Local/Programs/marktext/resources/app.asar/out/renderer/assets/index-D8Yzs57g.js:99332:70)
at checkOp (file:///C:/Users/x/AppData/Local/Programs/marktext/resources/app.asar/out/renderer/assets/index-D8Yzs57g.js:99281:26)
at Object.apply (file:///C:/Users/x/AppData/Local/Programs/marktext/resources/app.asar/out/renderer/assets/index-D8Yzs57g.js:99583:11)
at drop2 (file:///C:/Users/x/AppData/Local/Programs/marktext/resources/app.asar/out/renderer/assets/index-D8Yzs57g.js:146616:57)
at drop2 (file:///C:/Users/x/AppData/Local/Programs/marktext/resources/app.asar/out/renderer/assets/index-D8Yzs57g.js:146611:27)
at Object.apply2 [as apply] (file:///C:/Users/x/AppData/Local/Programs/marktext/resources/app.asar/out/renderer/assets/index-D8Yzs57g.js:146621:9)
at JSONState._apply (file:///C:/Users/x/AppData/Local/Programs/marktext/resources/app.asar/out/renderer/assets/index-D8Yzs57g.js:147289:44)
at JSONState._flushOperationCache (file:///C:/Users/x/AppData/Local/Programs/marktext/resources/app.asar/out/renderer/assets/index-D8Yzs57g.js:147442:10)
at file:///C:/Users/x/AppData/Local/Programs/marktext/resources/app.asar/out/renderer/assets/index-D8Yzs57g.js:147422:12

Operating System

Windows

MarkText Version

v0.20.0-rc.1

OS Version

Windows 11

Reproducibility

  • I can reliably reproduce this issue

Activity

  1. Renakoni commented on Jul 15, 2026

    @Renakoni
    Contributor

    Does this issue happen with all emojis? Could you give me some specific examples? That way, I can add concrete tests for them.

  2. ACodeCopyer commented on Jul 15, 2026

    @ACodeCopyer
    Author

    Does this issue happen with all emojis? Could you give me some specific examples? That way, I can add concrete tests for them.

    I am not sure if this issue happen with all emojis, but when I input 🙂(微笑)😀(大笑) , it will happen.

  3. Renakoni commented on Jul 15, 2026

    @Renakoni
    Contributor

    Thanks for the example! 🙏 That 🙂(微笑)😀(大笑) case was really helpful — I've used it directly in the tests.

    It turned out to be more interesting than it looks, so a quick explanation:

    Emoji like 🙂 and 😀 aren't single characters under the hood — each is stored as two UTF‑16 "code units" (a surrogate pair). The crash happens when the caret ends up between those two halves and you press Backspace/Delete: the editor removes only one half and leaves a "lone surrogate," which the underlying text engine (ot-text-unicode) can't encode — that's the Invalid offset - splits unicode bytes error you saw.

    Two things worth clarifying:

    • It's not specific to these emoji — essentially any emoji that's a surrogate pair (most modern emoji) can trigger it, whenever a delete lands on that split point. The surrounding (微笑) / (大笑) text is fine; it's the 🙂 / 😀 themselves.
    • It's also not really a copy‑paste bug. Pasting is just one easy way to get the emoji into the document and leave the caret mid‑character — the real problem is in how deletion handles the surrogate pair. The fix doesn't touch the clipboard/paste path at all.

    The fix (#4931) makes deletion surrogate‑aware: it snaps the caret to a whole‑character boundary before deleting, then removes the emoji as a single code point. Unit + end‑to‑end tests are included, using your exact 🙂(微笑)😀(大笑) example.

    这个问题比想象中有意思 简单说:像 🙂 😀 这类 emoji 在底层其实是两个 UTF‑16 编码单元拼起来的(代理对)。当光标正好卡在这两半中间时按删除键,编辑器只删掉了其中一半,留下“半个字符”,底层文本引擎没法处理,就崩了(也就是你看到的 Invalid offset - splits unicode bytes)。

  4. ACodeCopyer commented on Jul 15, 2026

    @ACodeCopyer
    Author

    Oh, that explains everything. Thanks for fix it!🙏

  5. added a commit that references this issue on Sep 14, 2026
    911dd45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions