fix(web-runtime): focus follows the caret to the field it types into

Mirrors the driver, so the two hosts agree about focus on a Compose page.
The harness inherits custom properties down the parent chain the way CSS
does, so an implementation matching the inline style attribute fails.
This commit is contained in:
pj committed 2026-08-18 00:16:52 +05:30
1 parent 04327e63b6
commit ab4c42601d
3 files changed
+122 -4

No files matched your search

+19
View File
@@ -54,6 +54,12 @@ export interface FakeElementSpec {
// host decides an element is scrollable.
overflows?: boolean;
focused?: boolean;
// customProperties are DECLARED on this element and INHERITED by everything
// under it, which is the whole reason the runtime reads a computed style and
// not the style attribute: Compose declares the caret's box on the container
// that positions its backing input, and only the input's COMPUTED style
// carries it.
customProperties?: Record<string, string>;
children?: FakeElementSpec[];
shadow?: FakeElementSpec[];
}
@@ -163,6 +169,14 @@ function activeElementIn(nodes: FakeElement[]): FakeElement | null {
return null;
}
function inheritedProperty(element: FakeElement, name: string): string {
for (let node: FakeElement | null = element; node; node = node.parentElement) {
const declared = node.customProperties?.[name];
if (declared !== undefined) return declared;
}
return "";
}
function queryScope(scope: { children: FakeElement[] }, selector: string): FakeElement[] {
const found: FakeElement[] = [];
const walk = (nodes: FakeElement[]): void => {
@@ -303,9 +317,13 @@ export function withFakeDocument(elements: FakeElement[], run: () => void): void
const global = globalThis as Record<string, unknown>;
const originalDocument = global.document;
const originalWindow = global.window;
const originalComputedStyle = global.getComputedStyle;
const document: FakeRoot = fakeRoot(elements);
global.document = document;
global.window = {};
global.getComputedStyle = (element: FakeElement) => ({
getPropertyValue: (name: string) => inheritedProperty(element, name),
});
__testing__.resetTargetCache();
try {
run();
@@ -313,5 +331,6 @@ export function withFakeDocument(elements: FakeElement[], run: () => void): void
__testing__.resetTargetCache();
global.document = originalDocument;
global.window = originalWindow;
global.getComputedStyle = originalComputedStyle;
}
}