Skip to content

Commit 4d6dfce

Browse files
committed
fix(limrun): trust a fill's identity witness only when it is unique
Two fields that expose neither an identifier nor a label share every part of the identity this witness compares, so an editing twin elsewhere on the screen satisfied the pre-tap set and #1658's misdelivery came back: the fill replaced whichever twin happened to hold focus. An identity the pre-tap tree reports more than once is now left out, so only geometry vouches for a twin and a fill that cannot place its editing element where it aimed is refused.
1 parent 11f8561 commit 4d6dfce

3 files changed

Lines changed: 75 additions & 9 deletions

File tree

‎packages/provider-limrun/src/ios.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,12 @@ class LimrunIosInteractor implements Interactor {
232232
async fill(x: number, y: number, text: string, delayMs?: number): Promise<FillBackendResult> {
233233
// The witness is loaded on the fill path instead of at module evaluation, so this
234234
// provider keeps the import-time closure budget it declares.
235-
const { awaitLimrunTextEntryFocus, readLimrunTapTargetIdentities, readLimrunTextEntryFocus } =
235+
const { awaitLimrunTextEntryFocus, readLimrunUnambiguousTapTargets, readLimrunTextEntryFocus } =
236236
await import('./text-entry-focus.ts');
237237
// The screen is read before the tap so the witness can say what was under this
238238
// point at all: an editing element that appears somewhere else proves nothing
239239
// about this tap (#1658).
240-
const targetsAtPoint = readLimrunTapTargetIdentities(
240+
const targetsAtPoint = readLimrunUnambiguousTapTargets(
241241
await this.session.client.elementTree(),
242242
x,
243243
y,

‎packages/provider-limrun/src/text-entry-focus.test.ts‎

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { expect, test, vi } from 'vitest';
22
import {
33
awaitLimrunTextEntryFocus,
4-
readLimrunTapTargetIdentities,
54
readLimrunTextEntryFocus,
5+
readLimrunUnambiguousTapTargets,
66
} from './text-entry-focus.ts';
77
import type { IosTreeNode } from './snapshot.ts';
88

@@ -41,7 +41,7 @@ function tree(...children: IosTreeNode[]): IosTreeNode {
4141
}
4242

4343
function targetsAt(point: Readonly<{ x: number; y: number }>, ...before: IosTreeNode[]) {
44-
return readLimrunTapTargetIdentities(tree(...before), point.x, point.y);
44+
return readLimrunUnambiguousTapTargets(tree(...before), point.x, point.y);
4545
}
4646

4747
const noSleep = async () => {};
@@ -152,6 +152,58 @@ test('an editing element that was never under the tapped point is refused', asyn
152152
});
153153
});
154154

155+
// Two fields that expose neither an identifier nor a label share one identity, so
156+
// identity alone cannot say which was tapped. The twin that already holds focus is
157+
// the dangerous one: believing its identity would replace its text (#1658).
158+
const TWIN_A_RECT: TreeRect = Object.freeze({ x: 24, y: 142, width: 354, height: 56 });
159+
const TWIN_B_RECT: TreeRect = Object.freeze({ x: 24, y: 262, width: 354, height: 56 });
160+
161+
test('a shared identity does not witness the twin that was not tapped', async () => {
162+
await expect(
163+
awaitLimrunTextEntryFocus({
164+
targetsAtPoint: targetsAt(
165+
{ x: 200, y: 170 },
166+
textField('', { rect: TWIN_A_RECT }),
167+
textField('', { rect: TWIN_B_RECT }),
168+
),
169+
readFocus: async () =>
170+
readLimrunTextEntryFocus(
171+
tree(
172+
textField('', { rect: TWIN_A_RECT }),
173+
textField('', { rect: TWIN_B_RECT, editing: true }),
174+
),
175+
),
176+
sleep: noSleep,
177+
x: 200,
178+
y: 170,
179+
timeoutMs: 0,
180+
}),
181+
).rejects.toMatchObject({
182+
details: { reason: 'text_entry_focus_not_observed', editingElementObserved: true },
183+
});
184+
});
185+
186+
test('geometry still witnesses the twin that was tapped', async () => {
187+
const readiness = await awaitLimrunTextEntryFocus({
188+
targetsAtPoint: targetsAt(
189+
{ x: 200, y: 170 },
190+
textField('', { rect: TWIN_A_RECT }),
191+
textField('', { rect: TWIN_B_RECT }),
192+
),
193+
readFocus: async () =>
194+
readLimrunTextEntryFocus(
195+
tree(
196+
textField('', { rect: TWIN_A_RECT, editing: true }),
197+
textField('', { rect: TWIN_B_RECT }),
198+
),
199+
),
200+
sleep: noSleep,
201+
x: 200,
202+
y: 170,
203+
});
204+
expect(readiness).toBe('focused-element');
205+
});
206+
155207
test('focus that arrives late is waited for instead of typed past', async () => {
156208
let reads = 0;
157209
const readiness = await awaitLimrunTextEntryFocus({

‎packages/provider-limrun/src/text-entry-focus.ts‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,34 @@ export function readLimrunTextEntryFocus(
8282
* editing element among them was under the finger even if focusing it re-laid it
8383
* out or a keyboard scrolled it away, which is what lets a fill vouch for a field
8484
* that moved while refusing one that was never there.
85+
*
86+
* An identity the tree reports more than once is left out rather than trusted. Two
87+
* fields that expose neither an identifier nor a label share every part this
88+
* identity is built from, so trusting it would let a fill replace whichever twin
89+
* holds focus. Geometry still witnesses such a field when the tap lands on it,
90+
* because only geometry can tell two twins apart.
8591
*/
86-
export function readLimrunTapTargetIdentities(
92+
export function readLimrunUnambiguousTapTargets(
8793
tree: IosTreeNode | IosTreeNode[],
8894
x: number,
8995
y: number,
9096
): ReadonlySet<string> {
91-
const identities = new Set<string>();
97+
const framed: Array<Readonly<{ identity: string; rect: Rect }>> = [];
9298
for (const node of flattenIosNodes(tree)) {
9399
const rect = readIosNodeRect(node);
94-
if (isPositiveFiniteRect(rect) && containsPoint(rect, x, y)) {
95-
identities.add(limrunTextEntryIdentity(node));
100+
if (isPositiveFiniteRect(rect)) framed.push({ identity: limrunTextEntryIdentity(node), rect });
101+
}
102+
const sightings = new Map<string, number>();
103+
for (const node of framed) {
104+
sightings.set(node.identity, (sightings.get(node.identity) ?? 0) + 1);
105+
}
106+
const targets = new Set<string>();
107+
for (const node of framed) {
108+
if (sightings.get(node.identity) === 1 && containsPoint(node.rect, x, y)) {
109+
targets.add(node.identity);
96110
}
97111
}
98-
return identities;
112+
return targets;
99113
}
100114

101115
/**

0 commit comments

Comments
 (0)