The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 12:15 UTC
fix(server): typing after a click by coordinates no longer hangs a preview tab (#32845)
Entrpi committed
Nov 15, 2026 at 15:39 UTC
f00afc6cda56ff39a3993e15cf45bede9ca5b63e
2 files changed
+141
-39
apps/server/src/preview/ServerBrowserPage.test.ts
+68
@@ -375,6 +375,25 @@ describe("server browser element refs", () => {
375
expect(await page.getByRole("textbox", { name: "parent" }).inputValue()).toBe("");
376
});
377
378
+ it("types after a click by coordinates into a shadow-root field, and refuses a field inside an editable frame", async () => {
379
+ await page.setContent(
380
+ `<div id="host"></div><div contenteditable="true"><iframe srcdoc="<input id=inner>"></iframe></div><script>document.getElementById("host").attachShadow({mode:"open"}).innerHTML = "<input id=field>";</script>`,
381
+ );
382
+ const field = page.locator("#host #field");
383
+ const box = (await field.boundingBox())!;
384
+ await page.mouse.click(box.x + box.width / 2, box.y + box.height / 2);
385
+ await ServerBrowserPage.type(page, { text: "shadow" });
386
+ expect(await field.inputValue()).toBe("shadow");
387
+
388
+ // Refused for now: focus inside a frame cannot be checked from the top document.
389
+ const inner = page.frameLocator("iframe").locator("#inner");
390
+ await inner.click();
391
+ await expect(ServerBrowserPage.type(page, { text: "frame" })).rejects.toMatchObject({
392
+ tag: "PreviewAutomationTargetNotEditableError",
393
+ });
394
+ expect(await inner.inputValue()).toBe("");
395
+ });
396
+
397
it("shows dotted fields as dots wherever their value appears, keeping text values and refs", async () => {
398
await page.setContent(`
399
<table><tr><td>password:</td><td><input id="pw" type="password" aria-label="Password"></td></tr></table>
@@ -932,6 +951,55 @@ describe("server browser evaluate", () => {
951
});
952
});
953
954
+describe("server browser type", () => {
955
+ // As on a desktop tab whose Playwright utility world never answers.
956
+ const stalledPage = (focusAnswer: () => Promise<unknown>, typed: string[]) =>
957
+ ({
958
+ locator: () => ({ last: () => ({ count: () => new Promise<number>(() => {}) }) }),
959
+ evaluate: focusAnswer,
960
+ keyboard: { insertText: async (text: string) => void typed.push(text) },
961
+ }) as unknown as Page;
962
+
963
+ it("types into the focused field when Playwright's utility world never answers", async () => {
964
+ vi.useFakeTimers();
965
+ try {
966
+ const typed: string[] = [];
967
+ let outcome: unknown = "pending";
968
+ const page = stalledPage(async () => true, typed);
969
+ void ServerBrowserPage.type(page, { text: "hello", timeoutMs: 300 }).then(
970
+ () => (outcome = "typed"),
971
+ (error: unknown) => (outcome = error),
972
+ );
973
+ await vi.advanceTimersByTimeAsync(300);
974
+ expect(outcome).toBe("typed");
975
+ expect(typed).toEqual(["hello"]);
976
+ } finally {
977
+ vi.useRealTimers();
978
+ }
979
+ });
980
+
981
+ it("stops at its timeout without typing when the page never answers", async () => {
982
+ vi.useFakeTimers();
983
+ try {
984
+ const typed: string[] = [];
985
+ let outcome: unknown = "pending";
986
+ const page = stalledPage(() => new Promise(() => {}), typed);
987
+ void ServerBrowserPage.type(page, { text: "hello", timeoutMs: 300 }).then(
988
+ () => (outcome = "typed"),
989
+ (error: unknown) => (outcome = error),
990
+ );
991
+ await vi.advanceTimersByTimeAsync(300);
992
+ expect(outcome).toMatchObject({
993
+ tag: "PreviewAutomationTimeoutError",
994
+ detail: { timeoutMs: 300 },
995
+ });
996
+ expect(typed).toEqual([]);
997
+ } finally {
998
+ vi.useRealTimers();
999
+ }
1000
+ });
1001
+});
1002
+
1003
describe("server browser unresponsive script", () => {
1004
it("leaves a page that answers alone", async () => {
1005
const send = vi.fn(async (_method: string) => ({}));
apps/server/src/preview/ServerBrowserPage.ts
+73
-39
@@ -896,6 +896,76 @@ const clickTarget = async (
896
return point;
897
};
898
899
+type FocusCandidate = {
900
+ readonly tagName: string;
901
+ readonly type?: string;
902
+ readonly disabled?: boolean;
903
+ readonly readOnly?: boolean;
904
+ readonly isContentEditable?: boolean;
905
+ readonly shadowRoot?: { readonly activeElement: FocusCandidate | null } | null;
906
+ readonly focus?: () => void;
907
+ getRootNode(): { readonly activeElement?: FocusCandidate | null };
908
+ contains(other: FocusCandidate): boolean;
909
+};
910
+
911
+// Serialized into the page's main world, so it must not reference module scope.
912
+const focusTextTarget = (target: FocusCandidate | null) => {
913
+ let element =
914
+ target ??
915
+ (globalThis as unknown as { document: { activeElement: FocusCandidate | null } }).document
916
+ .activeElement;
917
+ if (target === null) {
918
+ while (element?.shadowRoot?.activeElement) element = element.shadowRoot.activeElement;
919
+ // Focus inside a frame cannot be checked from here, and an editable frame would pass as contentEditable.
920
+ if (element && ["IFRAME", "FRAME", "FENCEDFRAME", "OBJECT", "EMBED"].includes(element.tagName))
921
+ return false;
922
+ }
923
+ if (element == null) return false;
924
+ // Require an enabled, focused text target to avoid losing input or sending it to the previous field.
925
+ const nonText = [
926
+ "button",
927
+ "checkbox",
928
+ "color",
929
+ "file",
930
+ "hidden",
931
+ "image",
932
+ "radio",
933
+ "range",
934
+ "reset",
935
+ "submit",
936
+ ];
937
+ const textControl =
938
+ element.tagName === "TEXTAREA" ||
939
+ (element.tagName === "INPUT" && !nonText.includes(element.type ?? "text"));
940
+ if (!(textControl || element.isContentEditable) || element.disabled || element.readOnly) {
941
+ return false;
942
+ }
943
+ element.focus?.();
944
+ const active = element.getRootNode().activeElement;
945
+ return active != null && (active === element || element.contains(active));
946
+};
947
+
948
+// A `*:focus` locator needs Playwright's utility world, which some desktop tabs never get.
949
+const focusedTextField = async (page: Page, timeout: number) => {
950
+ let timer: ReturnType<typeof setTimeout> | undefined;
951
+ const expired = new Promise<never>((_, reject) => {
952
+ timer = setTimeout(
953
+ () =>
954
+ reject(
955
+ new ServerBrowserOperationError({
956
+ tag: "PreviewAutomationTimeoutError",
957
+ message: `The page did not report its focused element within ${timeout}ms, so no text was typed.`,
958
+ detail: { timeoutMs: timeout },
959
+ }),
960
+ ),
961
+ timeout,
962
+ );
963
+ });
964
+ return await Promise.race([page.evaluate(focusTextTarget, null), expired]).finally(() =>
965
+ clearTimeout(timer),
966
+ );
967
+};
968
+
969
export const type = async (page: Page, input: PreviewAutomationTypeInput) => {
970
const timeout = input.timeoutMs ?? DEFAULT_TIMEOUT_MS;
971
const locator = targetLocator(page, input);
@@ -903,46 +973,10 @@ export const type = async (page: Page, input: PreviewAutomationTypeInput) => {
973
await locator.fill(input.text, { timeout });
974
return;
975
}
906
- // A shadow host also matches :focus; use its innermost focused descendant.
907
- const target = locator ?? page.locator("*:focus").last();
976
const focused =
909
- (locator !== null || (await target.count()) > 0) &&
910
- (await target.evaluate(
911
- (element) => {
912
- // Require an enabled, focused text target to avoid losing input or sending it to the previous field.
913
- const control = element as unknown as {
914
- readonly type?: string;
915
- readonly disabled?: boolean;
916
- readonly readOnly?: boolean;
917
- readonly isContentEditable?: boolean;
918
- readonly focus?: () => void;
919
- };
920
- const nonText = [
921
- "button",
922
- "checkbox",
923
- "color",
924
- "file",
925
- "hidden",
926
- "image",
927
- "radio",
928
- "range",
929
- "reset",
930
- "submit",
931
- ];
932
- const textControl =
933
- element.tagName === "TEXTAREA" ||
934
- (element.tagName === "INPUT" && !nonText.includes(control.type ?? "text"));
935
- if (!(textControl || control.isContentEditable) || control.disabled || control.readOnly) {
936
- return false;
937
- }
938
- control.focus?.();
939
- const root = element.getRootNode() as { readonly activeElement?: typeof element | null };
940
- const active = root.activeElement;
941
- return active != null && (active === element || element.contains(active));
942
- },
943
- undefined,
944
- { timeout },
945
- ));
977
+ locator === null
978
+ ? await focusedTextField(page, timeout)
979
+ : await locator.evaluate(focusTextTarget, undefined, { timeout });
980
if (focused !== true) {
981
throw new ServerBrowserOperationError({
982
tag: "PreviewAutomationTargetNotEditableError",