Merge pull request #327 from dignfei/fix/terminal-ime-punctuation

fix(terminal): preserve IME punctuation input
This commit is contained in:
Ark0N
2026-08-21 20:23:26 +02:00
committed by GitHub
4 changed files with 145 additions and 107 deletions
+42 -11
View File
@@ -5,7 +5,7 @@
* composition/keydown/input event sequences against a stub textarea.
* Focus: the intermittent "Chinese characters silently lost" failure modes —
* stuck composition state, deferred flush racing the next composition, and
* the keydown-echo suppression window swallowing a real IME commit.
* physical-key punctuation transformed into full-width IME output.
*/
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
@@ -116,6 +116,36 @@ describe('CJK input module', () => {
expect(textarea.value).toBe(PHANTOM);
});
it('lets the IME transform printable keys before sending full-width punctuation', () => {
const { textarea, sent } = loadCjkHarness();
const committed = Array.from(',。!?;:“”、《》、()');
const physicalKeys = [',', '.', '!', '?', ';', ':', '"', '"', '\\', '<', '>', '\\', '(', ')'];
textarea.fire('compositionstart');
textarea.value = PHANTOM + '中文';
textarea.fire('input', { isComposing: true, inputType: 'insertCompositionText' });
textarea.fire('compositionend');
vi.advanceTimersByTime(10);
vi.advanceTimersByTime(1000);
for (const [index, punctuation] of committed.entries()) {
const preventDefault = vi.fn();
textarea.fire('keydown', {
key: physicalKeys[index],
ctrlKey: false,
altKey: false,
metaKey: false,
preventDefault,
});
expect(preventDefault).not.toHaveBeenCalled();
textarea.value = PHANTOM + punctuation;
textarea.fire('input', { isComposing: false, inputType: 'insertText' });
}
expect(sent).toEqual(['中文', ...committed]);
});
it('recovers committed text when compositionend never fires (stuck composition)', () => {
const { textarea, sent } = loadCjkHarness();
@@ -124,6 +154,7 @@ describe('CJK input module', () => {
textarea.value = PHANTOM + '你好';
// The commit arrives as a plain input event outside composition.
textarea.fire('input', { isComposing: false, inputType: 'insertText' });
expect(sent).toEqual([]);
vi.advanceTimersByTime(200);
expect(sent).toEqual(['你好']);
@@ -151,30 +182,28 @@ describe('CJK input module', () => {
expect(sent).toEqual(['你好世界']);
});
it('does not discard an IME commit landing inside the keydown echo window', () => {
it('sends the transformed IME commit that follows a printable keydown', () => {
const { textarea, sent } = loadCjkHarness();
// English char goes out immediately via keydown.
vi.advanceTimersByTime(1000);
// The physical key is not committed text and must not be sent by itself.
textarea.fire('keydown', { key: 'a', ctrlKey: false, altKey: false, metaKey: false });
expect(sent).toEqual(['a']);
expect(sent).toEqual([]);
// Within 100ms the IME commits Chinese via a bare input event.
// The browser/IME supplies the canonical text in the following input.
vi.advanceTimersByTime(50);
textarea.value = PHANTOM + '你好';
textarea.fire('input', { isComposing: false, inputType: 'insertText' });
vi.advanceTimersByTime(200);
expect(sent).toEqual(['a', '你好']);
expect(sent).toEqual(['你好']);
});
it('still suppresses the true textarea echo of a keydown-sent character', () => {
it('sends a printable physical key exactly once after its input event', () => {
const { textarea, sent } = loadCjkHarness();
textarea.fire('keydown', { key: 'a', ctrlKey: false, altKey: false, metaKey: false });
expect(sent).toEqual(['a']);
expect(sent).toEqual([]);
// Third-party IME ignored preventDefault — the same char echoes into
// the textarea. It must be dropped, not sent twice.
vi.advanceTimersByTime(10);
textarea.value = PHANTOM + 'a';
textarea.fire('input', { isComposing: false, inputType: 'insertText' });
@@ -242,6 +271,8 @@ describe('CJK input module', () => {
vi.advanceTimersByTime(10);
textarea.fire('keydown', { key: '囍', ctrlKey: false, altKey: false, metaKey: false });
textarea.value = PHANTOM + '囍';
textarea.fire('input', { isComposing: false, inputType: 'insertText' });
textarea.value = PHANTOM + '秘密';
textarea.fire('blur');
expect(sent).toEqual(['秘密口令', '囍']);
+71 -1
View File
@@ -12,7 +12,7 @@
* emitted through onData (the bytes that would reach the PTY).
*
* Browser-driven, so it is excluded from `npm run test:ci` like the other
* Playwright suites. Run locally: npm test -- test/terminal-copy-shortcut.test.ts
* Playwright suites. Run locally: npm run test:browser -- test/terminal-copy-shortcut.test.ts
*
* Port: 3174 (per MEMORY.md, ports 3150+ for tests)
*/
@@ -23,6 +23,7 @@ import { WebServer } from '../src/web/server.js';
const PORT = 3174;
const BASE_URL = `http://localhost:${PORT}`;
const IME_PUNCTUATION = ',。!?;:“”、《》、()';
describe('terminal Ctrl+C smart copy', () => {
let server: WebServer;
@@ -102,6 +103,59 @@ describe('terminal Ctrl+C smart copy', () => {
}));
}
async function captureImeInput(targetPage: Page) {
await targetPage.waitForFunction(() => (window as any).app?.terminal, null, { timeout: 30000 });
await targetPage.evaluate(() => {
const term = (window as any).app.terminal;
(window as any).__data = [];
if (!(window as any).__dataHooked) {
term.onData((d: string) => (window as any).__data.push(d));
(window as any).__dataHooked = true;
}
(document.querySelector('.xterm-helper-textarea') as HTMLElement).focus();
});
const cdp = await targetPage.context().newCDPSession(targetPage);
await cdp.send('Input.imeSetComposition', { text: '中文', selectionStart: 2, selectionEnd: 2 });
await cdp.send('Input.insertText', { text: '中文' });
await targetPage.waitForFunction(() => (window as any).__data.join('') === '中文', null, { polling: 10 });
let expected = '中文';
for (const punctuation of Array.from(IME_PUNCTUATION)) {
// Keep keydown -> DOM mutation -> input in one browser task, as a
// native key default action does. Separate CDP calls can let xterm's
// zero-delay textarea diff run before Input.insertText reaches the page.
await targetPage.evaluate((text) => {
const textarea = document.querySelector('.xterm-helper-textarea') as HTMLTextAreaElement;
const down = new KeyboardEvent('keydown', {
key: 'Process',
code: 'Unidentified',
bubbles: true,
cancelable: true,
composed: true,
});
Object.defineProperties(down, { keyCode: { value: 229 }, which: { value: 229 } });
textarea.dispatchEvent(down);
if (!document.execCommand('insertText', false, text)) throw new Error('browser rejected insertText');
const up = new KeyboardEvent('keyup', {
key: 'Process',
code: 'Unidentified',
bubbles: true,
cancelable: true,
composed: true,
});
Object.defineProperties(up, { keyCode: { value: 229 }, which: { value: 229 } });
textarea.dispatchEvent(up);
}, punctuation);
expected += punctuation;
await targetPage.waitForFunction((text) => (window as any).__data.join('') === text, expected, {
polling: 10,
});
}
return targetPage.evaluate(() => (window as any).__data as string[]);
}
it('copies the selection and sends nothing to the PTY', async () => {
await setup('COPY-CASE-SELECTED', true);
await page.keyboard.press('Control+c');
@@ -163,4 +217,20 @@ describe('terminal Ctrl+C smart copy', () => {
expect(res.data.join('')).toContain('SENTINEL'); // pasted text, not ^V
expect(res.data.join('')).not.toContain('\x16');
});
it('forwards full-width punctuation after a Chinese IME composition', async () => {
await setup('IME-PUNCTUATION', false);
const desktopChunks = await captureImeInput(page);
expect(desktopChunks.join('')).toBe('中文' + IME_PUNCTUATION);
const touchContext = await browser.newContext({ hasTouch: true });
try {
const touchPage = await touchContext.newPage();
await touchPage.goto(BASE_URL, { waitUntil: 'domcontentloaded' });
const touchChunks = await captureImeInput(touchPage);
expect(touchChunks.join('')).toBe('中文' + IME_PUNCTUATION);
} finally {
await touchContext.close();
}
});
});