Файли
openidea/src/platform/webkitCmdInput.test.ts
T
Onur Ugur 0f0cdd4672 fix(macos): route Cmd+` natively and drop spurious Cmd text insertion (#7436)
On macOS, Cmd+/ and Cmd+` left a character at the caret (`/` and a backtick under ABC, `、` and
`·` under Pinyin), and Cmd+` did not switch project windows: WKWebView's performKeyEquivalent
routed the key through NSTextInputContext before NSMenu. WryWebView's performKeyEquivalent now
dispatches window.next / window.previous for Cmd+` / Cmd+Shift+`, and insertText is dropped for a
Command key-down without marked text. A frontend guard drops text beforeinput events right after
a Command key-down as a fallback.

Closes #7334

Co-authored-by: OnurrUgur <[email protected]>
2026-12-02 23:02:11 +08:00

137 рядки
5.7 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { CMD_BEFOREINPUT_WINDOW_MS, installMacCmdInputGuard } from "./webkitCmdInput";
describe("installMacCmdInputGuard", () => {
let now = 1000;
let uninstall: () => void;
let editor: HTMLElement;
let input: HTMLInputElement;
const fireKeyDown = (el: Element, init: KeyboardEventInit) => {
const event = new KeyboardEvent("keydown", { bubbles: true, cancelable: true, ...init });
el.dispatchEvent(event);
return event;
};
const fireBeforeInput = (el: Element, inputType: string, data: string | null = null) => {
const event = new InputEvent("beforeinput", { bubbles: true, cancelable: true, inputType, data });
el.dispatchEvent(event);
return event;
};
beforeEach(() => {
now = 1000;
document.body.innerHTML = `
<div class="cm-editor"><div class="cm-content" contenteditable="true"></div></div>
<input id="field" />`;
editor = document.querySelector(".cm-content")!;
input = document.getElementById("field") as HTMLInputElement;
uninstall = installMacCmdInputGuard({ target: window, now: () => now, isTrusted: () => true });
});
afterEach(() => {
uninstall();
document.body.innerHTML = "";
});
it("drops stray ABC and Pinyin beforeinput text after Cmd+/ in the editor", () => {
const seen = vi.fn();
editor.addEventListener("beforeinput", seen);
fireKeyDown(editor, { key: "/", code: "Slash", metaKey: true });
const abc = fireBeforeInput(editor, "insertText", "/");
expect(abc.defaultPrevented).toBe(true);
now += 50;
fireKeyDown(editor, { key: "、", code: "Slash", metaKey: true });
const pinyin = fireBeforeInput(editor, "insertText", "、");
expect(pinyin.defaultPrevented).toBe(true);
expect(seen).not.toHaveBeenCalled();
});
it("drops stray ABC and Pinyin beforeinput text (including composition commit) after Cmd+` and Cmd+Shift+`", () => {
const seen = vi.fn();
editor.addEventListener("beforeinput", seen);
fireKeyDown(editor, { key: "`", code: "Backquote", metaKey: true });
expect(fireBeforeInput(editor, "insertText", "`").defaultPrevented).toBe(true);
now += 50;
fireKeyDown(editor, { key: "·", code: "Backquote", metaKey: true });
expect(fireBeforeInput(editor, "insertCompositionText", "·").defaultPrevented).toBe(true);
now += 50;
fireKeyDown(editor, { key: "~", code: "Backquote", metaKey: true, shiftKey: true });
expect(fireBeforeInput(editor, "insertText", "~").defaultPrevented).toBe(true);
expect(seen).not.toHaveBeenCalled();
});
it("keeps native paste, undo, redo and cut beforeinput events intact", () => {
const seen: string[] = [];
editor.addEventListener("beforeinput", (e) => seen.push((e as InputEvent).inputType));
for (const [key, code, inputType] of [
["v", "KeyV", "insertFromPaste"],
["z", "KeyZ", "historyUndo"],
["z", "KeyZ", "historyRedo"],
["x", "KeyX", "deleteByCut"],
] as const) {
fireKeyDown(editor, { key, code, metaKey: true });
const event = fireBeforeInput(editor, inputType, null);
expect(event.defaultPrevented).toBe(false);
}
expect(seen).toEqual(["insertFromPaste", "historyUndo", "historyRedo", "deleteByCut"]);
});
it("does not block normal typing or Option-key typing immediately after Cmd+/", () => {
const seen: string[] = [];
editor.addEventListener("beforeinput", (e) => seen.push((e as InputEvent).data ?? ""));
fireKeyDown(editor, { key: "/", code: "Slash", metaKey: true });
expect(fireBeforeInput(editor, "insertText", "/").defaultPrevented).toBe(true);
// Next normal keystroke 5 ms later clears the pending Cmd state immediately.
now += 5;
fireKeyDown(editor, { key: "/", code: "Slash" });
expect(fireBeforeInput(editor, "insertText", "/").defaultPrevented).toBe(false);
now += 5;
fireKeyDown(editor, { key: "#", code: "Digit3", altKey: true });
expect(fireBeforeInput(editor, "insertText", "#").defaultPrevented).toBe(false);
expect(seen).toEqual(["/", "#"]);
});
it("expires after the time window and clears on pointerdown or blur", () => {
fireKeyDown(input, { key: "/", code: "Slash", metaKey: true });
now += CMD_BEFOREINPUT_WINDOW_MS + 1;
expect(fireBeforeInput(input, "insertText", "/").defaultPrevented).toBe(false);
fireKeyDown(input, { key: "/", code: "Slash", metaKey: true });
input.dispatchEvent(new Event("pointerdown", { bubbles: true }));
expect(fireBeforeInput(input, "insertText", "/").defaultPrevented).toBe(false);
fireKeyDown(input, { key: "/", code: "Slash", metaKey: true });
window.dispatchEvent(new Event("blur"));
expect(fireBeforeInput(input, "insertText", "/").defaultPrevented).toBe(false);
});
it("leaves in-progress IME composition flushes and untrusted synthetic events alone", () => {
fireKeyDown(editor, { key: "s", code: "KeyS", metaKey: true, isComposing: true });
expect(fireBeforeInput(editor, "insertCompositionText", "nihao").defaultPrevented).toBe(false);
uninstall();
uninstall = installMacCmdInputGuard({ target: window, now: () => now });
fireKeyDown(input, { key: "v", code: "KeyV", metaKey: true });
// Default isTrusted checks event.isTrusted, which is false for synthetic / execCommand events.
expect(fireBeforeInput(input, "insertText", "pasted").defaultPrevented).toBe(false);
});
it("stops listening once uninstalled", () => {
uninstall();
fireKeyDown(editor, { key: "/", code: "Slash", metaKey: true });
expect(fireBeforeInput(editor, "insertText", "/").defaultPrevented).toBe(false);
uninstall = installMacCmdInputGuard({ target: window, now: () => now, isTrusted: () => true });
});
});