Skip to content

Commit d9af98b

Browse files
authored
fix(desktop-browser): refuse typing behind an unmarked full-screen modal or when no sampled point lands (#10013)
* fix(desktop-browser): refuse typing behind an unmarked full-screen modal or when no sampled point lands Top-page typing through a covering element now also refuses when the cover sits in a fixed layer spanning the viewport that does not hold the field, and when every sampled point hits nothing. The never-settling settle test now asserts the cap. * refactor(desktop-browser): compute the field's ancestors once, dedupe sampled hits, and time the settle cap from its first probe * test(desktop-browser): pass only the element to visible() from map * test(desktop-browser): drop a typing assertion that cannot fail and bound the settle cap from below * fix(desktop-browser): count a full-screen fixed layer as a modal only when it catches the pointer * fix(desktop-browser): detect an unmarked modal by probing the viewport for the layer that covers the field * fix(desktop-browser): treat an element painted above the field and under every viewport corner as a modal backdrop * test(desktop-browser): cover a field painted below its own full-screen container * test(desktop-browser): type the corner stacks as Element arrays
1 parent 06f077d commit d9af98b

3 files changed

Lines changed: 188 additions & 6 deletions

File tree

‎apps/desktop/src/main/browser-agent/driver.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3223,14 +3223,18 @@ describe('credential protection', () => {
32233223
const contents = await openPage()
32243224
const calls: string[] = []
32253225
let probe = 0
3226+
let firstProbeAt = Number.NaN
3227+
let snapshotAt = Number.NaN
32263228
vi.mocked(contents.executeJavaScript).mockImplementation((expression: string) => {
32273229
if (isPageCall(expression, 'readDomQuiet')) {
32283230
calls.push('probe')
3231+
if (Number.isNaN(firstProbeAt)) firstProbeAt = Date.now()
32293232
const quietMs = quietReadings[Math.min(probe++, quietReadings.length - 1)]
32303233
return Promise.resolve({ quietMs, loading: false })
32313234
}
32323235
if (isPageCall(expression, 'collectSnapshot')) {
32333236
calls.push('snapshot')
3237+
snapshotAt = Date.now()
32343238
return Promise.resolve({
32353239
url: 'https://example.com/',
32363240
title: 'Example',
@@ -3256,6 +3260,10 @@ describe('credential protection', () => {
32563260
const probesBeforeSnapshot = calls.indexOf('snapshot')
32573261
expect(probesBeforeSnapshot).toBeGreaterThan(0)
32583262
if (expectedProbes !== undefined) expect(probesBeforeSnapshot).toBe(expectedProbes)
3263+
else {
3264+
expect(snapshotAt - firstProbeAt).toBeGreaterThanOrEqual(1_900)
3265+
expect(snapshotAt - firstProbeAt).toBeLessThanOrEqual(2_000)
3266+
}
32593267
} finally {
32603268
vi.useRealTimers()
32613269
}

‎apps/desktop/src/main/browser-agent/page-functions.test.ts‎

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,148 @@ describe('combobox typing surfaces', () => {
381381
expect(focusElementForTyping(0)).toMatchObject({ error: 'obstructed' })
382382
})
383383

384+
/**
385+
* Answers hit testing with the paint stack (topmost first) over the field's sampled points and
386+
* elsewhere, as `elementsFromPoint` does; `elementFromPoint` is its top.
387+
*/
388+
function stackTo(
389+
overField: Element[],
390+
elsewhere: (x: number, y: number) => Element[],
391+
target: Document | ShadowRoot = document
392+
): void {
393+
const stackAt = (x: number, y: number) =>
394+
x >= 10 && x <= 100 && y <= 20 ? overField : elsewhere(x, y)
395+
Object.defineProperty(target, 'elementsFromPoint', { configurable: true, value: stackAt })
396+
Object.defineProperty(target, 'elementFromPoint', {
397+
configurable: true,
398+
value: (x: number, y: number) => stackAt(x, y)[0] ?? null,
399+
})
400+
}
401+
const page = () => [document.body, document.documentElement]
402+
const byId = <T extends Element = HTMLDivElement>(id: string) =>
403+
document.getElementById(id) as unknown as T
404+
405+
it('refuses a top-page field behind an unmarked backdrop and panel', () => {
406+
document.body.innerHTML = `
407+
<input id="field" type="text" aria-label="Search" />
408+
<div id="backdrop" style="position: fixed"></div>
409+
<div id="panel" style="position: fixed">Subscribe to our newsletter</div>
410+
`
411+
const input = visible(byId<HTMLInputElement>('field'))
412+
register(input)
413+
stackTo([byId('panel'), byId('backdrop'), input, ...page()], () => [
414+
byId('backdrop'),
415+
...page(),
416+
])
417+
418+
expect(focusElementForTyping(0)).toMatchObject({ error: 'obstructed' })
419+
})
420+
421+
it('refuses a field behind an unmarked modal nested in a full-screen app shell', () => {
422+
document.body.innerHTML = `
423+
<div id="shell" style="position: fixed">
424+
<input id="field" type="text" aria-label="Search" />
425+
<div id="backdrop" style="position: fixed"><div id="panel">Confirm your plan</div></div>
426+
</div>
427+
`
428+
const input = visible(byId<HTMLInputElement>('field'))
429+
register(input)
430+
stackTo([byId('panel'), byId('backdrop'), input, byId('shell'), ...page()], () => [
431+
byId('backdrop'),
432+
byId('shell'),
433+
...page(),
434+
])
435+
436+
expect(focusElementForTyping(0)).toMatchObject({ error: 'obstructed' })
437+
})
438+
439+
it('refuses a field behind an unmarked modal inside the same shadow root', () => {
440+
document.body.innerHTML = '<div id="host"></div>'
441+
const shadow = byId('host').attachShadow({ mode: 'open' })
442+
shadow.innerHTML =
443+
'<input type="text" aria-label="Search" /><div id="backdrop" style="position: fixed"><div id="panel">Sign up</div></div>'
444+
const input = visible(shadow.querySelector('input') as HTMLInputElement)
445+
const backdrop = shadow.querySelector('#backdrop') as HTMLDivElement
446+
const panel = shadow.querySelector('#panel') as HTMLDivElement
447+
register(input)
448+
stackTo(
449+
[panel, backdrop, input, byId('host'), ...page()],
450+
() => [backdrop, byId('host'), ...page()],
451+
shadow
452+
)
453+
454+
expect(focusElementForTyping(0)).toMatchObject({ error: 'obstructed' })
455+
})
456+
457+
it('types under covers that do not reach every corner of the viewport', () => {
458+
document.body.innerHTML = `
459+
<div id="map" style="position: fixed">Map</div>
460+
<div id="header" style="position: fixed">Menu</div>
461+
<input id="field" type="text" aria-label="Search" />
462+
<div id="toast">Saved to your list</div>
463+
<div id="banner" style="position: fixed">We use cookies</div>
464+
`
465+
const input = visible(byId<HTMLInputElement>('field'))
466+
register(input)
467+
const topHalf = (y: number) => y < window.innerHeight / 2
468+
for (const [cover, label, elsewhere] of [
469+
// a fixed header over a full-screen fixed map the field also sits above
470+
[
471+
'header',
472+
'Menu',
473+
(_x: number, y: number): Element[] => [
474+
...(topHalf(y) ? [byId('header')] : []),
475+
byId('map'),
476+
...page(),
477+
],
478+
],
479+
// a fixed header with a cookie banner pinned to the bottom corners
480+
[
481+
'header',
482+
'Menu',
483+
(_x: number, y: number): Element[] => [
484+
topHalf(y) ? byId('header') : byId('banner'),
485+
...page(),
486+
],
487+
],
488+
// a toast whose full-screen wrapper lets the pointer through, so the wrapper is in no stack
489+
['toast', 'Saved to your list', (): Element[] => page()],
490+
] as const) {
491+
stackTo([byId(cover), input, byId('map'), ...page()], elsewhere)
492+
expect(focusElementForTyping(0)).toMatchObject({ focused: true, coveredBy: label })
493+
}
494+
})
495+
496+
it('types inside a full-screen fixed app shell that also holds the field', () => {
497+
document.body.innerHTML = `
498+
<div id="shell" style="position: fixed">
499+
<input id="field" type="text" aria-label="Search" /><div id="card">Shop for your home</div>
500+
</div>
501+
`
502+
const input = visible(byId<HTMLInputElement>('field'))
503+
register(input)
504+
// the shell paints below the field, or above it when the field sits at a negative z-index
505+
for (const overField of [
506+
[byId('card'), input, byId('shell'), ...page()],
507+
[byId('card'), byId('shell'), input, ...page()],
508+
]) {
509+
stackTo(overField, () => [byId('shell'), ...page()])
510+
expect(focusElementForTyping(0)).toMatchObject({
511+
focused: true,
512+
coveredBy: 'Shop for your home',
513+
})
514+
}
515+
})
516+
517+
it('refuses a top-page field when no sampled point hits anything', () => {
518+
document.body.innerHTML = '<input type="text" aria-label="Search" />'
519+
const input = visible(document.querySelector('input') as HTMLInputElement)
520+
register(input)
521+
Object.defineProperty(document, 'elementFromPoint', { configurable: true, value: () => null })
522+
523+
expect(focusElementForTyping(0)).toMatchObject({ error: 'obstructed' })
524+
})
525+
384526
it('still refuses a covered field inside a frame', () => {
385527
const frame = document.createElement('iframe')
386528
document.body.append(frame)

‎apps/desktop/src/main/browser-agent/page-functions.ts‎

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2040,11 +2040,12 @@ export function focusElementForTyping(id: number, moveFocus = true): unknown {
20402040
coveredByRelatedPopup = true
20412041
}
20422042
}
2043-
// Top-page typing goes through keyboard focus, so a non-modal overlay does not block it; a modal
2044-
// or dialog cover still refuses, and framed fields keep the check for their mapped point.
2043+
// Top-page typing goes through keyboard focus, so a non-modal overlay does not block it. A modal
2044+
// still does: a dialog, or an unmarked backdrop, which paints above the field and under every
2045+
// corner of the viewport. Framed fields keep the check for their mapped point.
20452046
const topPageField = view === window && window.top === window
2046-
const insideModal = (node: Element | null): boolean => {
2047-
for (let current = node; current; current = composedParent(current)) {
2047+
const insideDialog = (node: Element): boolean => {
2048+
for (let current: Element | null = node; current; current = composedParent(current)) {
20482049
const role = current.getAttribute('role')
20492050
if (
20502051
current.getAttribute('aria-modal') === 'true' ||
@@ -2057,12 +2058,43 @@ export function focusElementForTyping(id: number, moveFocus = true): unknown {
20572058
}
20582059
return false
20592060
}
2061+
const behindBackdrop = (): boolean => {
2062+
const stackRoot = root as ParentNode & {
2063+
elementsFromPoint?: (x: number, y: number) => Element[]
2064+
}
2065+
if (typeof stackRoot.elementsFromPoint !== 'function') return false
2066+
const stackAt = stackRoot.elementsFromPoint.bind(stackRoot)
2067+
const aboveField = new Set<Element>()
2068+
for (const point of blockedPoints) {
2069+
for (const element of stackAt(point.x, point.y)) {
2070+
if (element === editable || editable.contains(element) || element.contains(editable)) break
2071+
aboveField.add(element)
2072+
}
2073+
}
2074+
if (aboveField.size === 0) return false
2075+
const width = editable.ownerDocument.documentElement.clientWidth || view.innerWidth
2076+
const height = editable.ownerDocument.documentElement.clientHeight || view.innerHeight
2077+
const inset = 4
2078+
for (const [x, y] of [
2079+
[inset, inset],
2080+
[width - inset, inset],
2081+
[inset, height - inset],
2082+
[width - inset, height - inset],
2083+
]) {
2084+
const corner = new Set(stackAt(x, y))
2085+
for (const element of aboveField) if (!corner.has(element)) aboveField.delete(element)
2086+
if (aboveField.size === 0) return false
2087+
}
2088+
return true
2089+
}
2090+
const hits = Array.from(new Set(blockedPoints.flatMap((point) => (point.hit ? [point.hit] : []))))
20602091
let coveredBy: string | undefined
20612092
if (
20622093
!chosenPoint &&
20632094
topPageField &&
2064-
blockedPoints.length > 0 &&
2065-
!blockedPoints.some((point) => insideModal(point.hit))
2095+
hits.length > 0 &&
2096+
!hits.some(insideDialog) &&
2097+
!behindBackdrop()
20662098
) {
20672099
chosenPoint = { x: blockedPoints[0].x, y: blockedPoints[0].y }
20682100
coveredBy = blockerLabel(firstBlocker)

0 commit comments

Comments
 (0)