fix(editor): overwrite a whole emoji instead of leaving half a surrogate pair (#7220)

In overwrite mode, typing over an emoji replaced only its high surrogate and left a broken low half. A replaced range that ends inside a surrogate pair now takes the whole pair.

Co-authored-by: audichuang <[email protected]>
Цей коміт міститься в:
audichuang authored and GitHub committed 2026-12-01 18:57:01 +08:00
1 джерело 9ba3150998
коміт 54da9c2c12
2 files changed
+25

No files matched your search

+21
Переглянути файл
@@ -457,6 +457,27 @@ describe("overwrite mode", () => {
type("XYZ");
expect(show(v)).toBe("abXYZ|\nx");
});
it("overwrites a whole surrogate pair after the caret", () => {
for (const [text, typed, after] of [
["|😀b", "A", "A|b"],
["|ab😀c", "XYZ", "XYZ|c"],
["|是\u200D全角", "非", "非|\u200D全角"],
// A DOM change from 😀 to 🨀 arrives as the new high surrogate alone.
["|😀b", "\ud83e", "\ud83e|\ude00b"],
]) {
const v = make(text!, overwriteField);
toggleOverwrite(v);
v.state.facet(EditorView.inputHandler).some((h) => h(v, 0, 0, typed!, () => v.state.update({})));
expect(show(v)).toBe(after);
}
// A caret between the halves of a pair (Go to Line:Column can put it there) replaces nothing on its own.
const v = make("|😀b", overwriteField);
toggleOverwrite(v);
v.dispatch({ selection: { anchor: 1 } });
v.state.facet(EditorView.inputHandler).some((h) => h(v, 1, 1, "", () => v.state.update({})));
expect(v.state.doc.toString()).toBe("😀b");
});
});
describe("smooth caret", () => {
+4
Переглянути файл
@@ -24,6 +24,10 @@ const overwriteInput = EditorView.inputHandler.of((view, _from, _to, text) => {
if (r.empty) {
const line = state.doc.lineAt(r.head);
to = Math.min(line.to, r.head + text.length);
// A range that ends between the halves of a surrogate pair takes the low half too, unless the
// typed text ends with a high surrogate that pairs with it.
const pair = state.doc.sliceString(to - 1, to + 1);
if (to > r.head && /^[\ud800-\udbff][\udc00-\udfff]$/.test(pair) && !/[\ud800-\udbff]$/.test(text)) to++;
}
return { changes: { from: r.from, to, insert: text }, range: EditorSelection.cursor(r.from + text.length) };
});