fix(web): delete one grapheme per iOS Backspace, keep the recovery fallback (#830)

Count native edits and overlay deletions in grapheme clusters (Intl.Segmenter, code-point fallback), so one Backspace on a multi-code-point emoji sends one DEL.
Toolbar Enter with echo off tells the codex predictor about the CR, clearing its predictions as typed Enter does.
Fall back to the keyCode-229 recovery when the iOS owner refuses to start, in the main pane and tiles.
This commit is contained in:
Hadrien Kerlero committed 2026-11-21 04:13:40 +01:00
1 parent 05fc2f990b
commit 71c7d01d02
8 files changed
+184 -24

No files matched your search

+29
View File
@@ -936,6 +936,35 @@ describe('native main transactions and boundaries', () => {
app._initTerminalInputOwner();
expect(recovery.destroy).toHaveBeenCalledOnce();
});
it('falls back to the recovery when the iOS module is supported but create() refuses', () => {
const { app, wire, context } = harness();
const native = { isSupported: () => true, create: vi.fn(() => null), attachCaretMarker: vi.fn() };
const create = vi.fn((_options: { emitRecovered: (data: string) => void }) => ({ destroy: vi.fn() }));
context.window.CodemanIosNativeInput = native;
context.window.CodemanKeyCode229Recovery = { create };
app._localEchoEnabled = false;
app._initTerminalInputOwner();
expect(native.create).toHaveBeenCalledOnce();
expect(native.attachCaretMarker).not.toHaveBeenCalled();
expect(app._iosNativeInput).toBeNull();
expect(create).toHaveBeenCalledOnce();
create.mock.calls[0][0].emitRecovered('recovered');
expect(wire).toEqual([['old', 'recovered']]);
});
it('toolbar Enter with echo off clears codex predictions like typed Enter', () => {
const { app, wire } = harness();
// The harness stubs the hook; run the real one from terminal-ui.js.
delete app._predictHookOnData;
app._localEchoEnabled = false;
app._localEchoPolicy = 'predict';
app._predictiveEcho = { clearPredictions: vi.fn(), predictChar: vi.fn(), predictBackspace: vi.fn() };
app._submitMainInput('old');
expect(app._predictiveEcho.clearPredictions).toHaveBeenCalledOnce();
expect(wire).toEqual([['old', '\r']]);
});
it('settles and resets native ownership before inserting external text into the overlay', () => {
const { app } = harness();
app._iosNativeInput = {
+49 -4
View File
@@ -159,7 +159,12 @@ type Listener = (event: Event) => void;
const DEL = '\x7f';
const dels = (count: number) => Array(count).fill(DEL);
const project = (value: string) => value.replace(/\r\n|[\r\n]/g, ' ');
const trimPoint = (value: string) => Array.from(value).slice(0, -1).join('');
// The destination (Claude Code, Codex, the local-echo overlay) deletes one grapheme cluster per DEL.
const trimGrapheme = (value: string) =>
[...new Intl.Segmenter(undefined, { granularity: 'grapheme' }).segment(value)]
.slice(0, -1)
.map((part) => part.segment)
.join('');
class FakeNode {
listeners = new Map<string, Set<Listener>>();
@@ -356,8 +361,8 @@ function harness(
const consume = (data: string) => {
sent.push(data);
if (data === DEL) {
if (pending) pending = trimPoint(pending);
else line = trimPoint(line);
if (pending) pending = trimGrapheme(pending);
else line = trimGrapheme(line);
} else if (data === '\r') {
commits.push(line + pending);
line = '';
@@ -421,7 +426,11 @@ function harness(
if (inputType === 'deleteContentBackward') {
if (start === end) {
if (start === 0) return;
start -= Array.from(textarea.value.slice(0, start)).at(-1)!.length;
// WebKit removes one whole grapheme cluster per Backspace.
const clusters = [
...new Intl.Segmenter(undefined, { granularity: 'grapheme' }).segment(textarea.value.slice(0, start)),
];
start -= clusters.at(-1)!.segment.length;
}
textarea.value = textarea.value.slice(0, start) + textarea.value.slice(end);
textarea.setSelectionRange(start, start);
@@ -548,6 +557,17 @@ const replayCases: Array<[string, (h: Harness) => void]> = [
expect(h.api.create({})).toBeNull();
expect(h.api.create({ terminal: h.terminal, sendTransaction() {}, isScreenReaderMode: () => true })).toBeNull();
expect(h.api.editBetween('a👍b', 'aX')).toEqual({ deleted: 2, inserted: 'X' });
// One DEL per grapheme cluster: WebKit removes the whole cluster on one
// Backspace, and Claude Code and Codex delete one grapheme per DEL.
for (const emoji of [
'\u2764\uFE0F',
'\u{1F44D}\u{1F3FD}',
'\u{1F1EB}\u{1F1F7}',
'\u{1F468}\u200D\u{1F469}\u200D\u{1F467}\u200D\u{1F466}',
]) {
expect(h.api.editBetween(`ok${emoji}`, 'ok')).toEqual({ deleted: 1, inserted: '' });
expect(h.api.editBetween(`a${emoji}b`, 'aX')).toEqual({ deleted: 2, inserted: 'X' });
}
for (const ch of 'hel') h.type(ch);
h.type('lo');
h.tick();
@@ -914,6 +934,31 @@ const replayCases: Array<[string, (h: Harness) => void]> = [
expect(h.textarea.value).toBe('hello');
},
],
[
'20b grapheme-cluster deletion sends one DEL',
(h) => {
for (const emoji of [
'\u2764\uFE0F',
'\u{1F44D}\u{1F3FD}',
'\u{1F1EB}\u{1F1F7}',
'\u{1F468}\u200D\u{1F469}\u200D\u{1F467}\u200D\u{1F466}',
]) {
h.type('ok');
h.edit(emoji);
h.clearSent();
h.backspace();
expect(h.textarea.value).toBe('ok');
h.bytes([DEL]);
// The destination keeps the draft: one DEL took the whole emoji, nothing before it.
expect(h.value()).toBe('ok');
h.invariant();
h.backspace();
h.backspace();
expect(h.value()).toBe('');
h.clearSent();
}
},
],
[
'21 destroy restoration and stock behavior',
(h) => {
+9
View File
@@ -1560,6 +1560,15 @@ describe('TerminalTile iOS native input ownership', () => {
expect(term.keyHandler!({ type: 'keydown', key: 'a', code: 'KeyA' })).toBe(true);
});
it('falls back to the 229 recovery when the iOS module refuses to create an owner', async () => {
const native = install();
native.create.mockReturnValueOnce(null);
const { tile } = await connectTile(makeApp());
expect(native.create).toHaveBeenCalledOnce();
expect(native.attachCaretMarker).not.toHaveBeenCalled();
expect((tile as ControllerTile)._keyCode229Recovery).not.toBeNull();
});
it('reserves separate DEL writes and insertion for the tile session in one call', async () => {
const native = install();
const app = makeApp();