// One responsibility: the in-page half of `pointerClick` — the actionability wait. Scroll the // target to the middle of the viewport, then hold until its rect is the same on two consecutive // frames and `elementFromPoint` at a point of it is the target or inside it, so the press lands // where the target is NOW and on the target, not on whatever a late reflow or an overlay put there. // // What it handles: a target that starts hidden, is replaced, is pushed out of the window again and // again, wraps over several lines, is taller than the window, sits in a scroll pane that clips it, // or is only partly hit-testable (a clip-path, a transform's empty corner): each is judged by // hit-testing points of the visible part. What it does not do, and says so: search inside an iframe // or a shadow root (a selector reaches the top document), or press a target that never stops moving. /** What the probe answers, by value: a point to press, no such element, or what never cleared. */ export type PointerProbe = | { readonly at: readonly [number, number] } | { readonly missing: true; readonly unsearched?: readonly string[] } | { readonly blocked: string }; /** Frames wait on `requestAnimationFrame`, and on a timer too: a hidden tab paints no frames. */ const SOURCE = `(async (selector, timeoutMs, prefer) => { const find = () => document.querySelector(selector); let el = find(); if (!el) { // A selector reaches the top document only: say what this page holds that it cannot see. const count = (selector) => document.querySelectorAll ? document.querySelectorAll(selector) : []; const unsearched = []; if (count('iframe, frame').length > 0) unsearched.push('an iframe'); if (Array.from(count('*')).some((n) => n.shadowRoot)) unsearched.push('an open shadow root'); return unsearched.length > 0 ? { missing: true, unsearched } : { missing: true }; } const reveal = (node) => node.scrollIntoView({ block: 'center', inline: 'center', behavior: 'instant' }); reveal(el); // A scroll does nothing to a node with no layout box (display: none): it is repeated the first // time the node has one, unless a reveal has only just happened with it. let revealedBoxed = false; // The position a scroll was last aimed from while the target was fully offscreen: a layout shift // can push it out of view after the first sample, and one that cannot be scrolled to is not // scrolled at again from the same place. let offscreenAt = ''; const vw = typeof innerWidth === 'number' ? innerWidth : Infinity; const vh = typeof innerHeight === 'number' ? innerHeight : Infinity; let justRevealed = true; const frame = () => new Promise((done) => { const timer = setTimeout(done, 50); requestAnimationFrame(() => { clearTimeout(timer); done(undefined); }); }); const describe = (node) => { if (!node) return 'nothing (the point is outside the viewport)'; const id = node.id ? '#' + node.id : ''; const cls = typeof node.className === 'string' && node.className.trim() ? '.' + node.className.trim().split(/\\s+/).join('.') : ''; const named = '<' + node.localName + id + cls + '>'; return node.localName === 'iframe' || node.localName === 'frame' ? named + ' (an iframe: its content is not searched)' : named; }; // The bounds a press can land in, in viewport coordinates: the window, narrowed by every ancestor // on each axis it clips (overflow other than visible). A scrolled pane shows a strip of a tall // target, and the middle of the target can be far below it. Read once per poll, not per fragment. // An ancestor's client box is scaled by how much its border box is (a transformed pane), and over- // clipping is safe: a point is only pressed if the hit test lands on the target. const boundsNow = () => { const b = { left: 0, top: 0, right: vw, bottom: vh }; if (typeof getComputedStyle !== 'function') return b; // With a root element whose own overflow is visible, the body's overflow belongs to the window // and does not clip the body's box: that ancestor is skipped, and the ones inside it still clip. const rootCss = document.documentElement ? getComputedStyle(document.documentElement) : null; // Any containment on the root or on the body is taken as stopping the hand-over: some keywords // (paint, layout) do, and over-keeping a clip is safe, since the window-only pass rescues it. const contained = (css) => Boolean(css.contain) && css.contain !== 'none'; const bodyIsWindow = rootCss !== null && rootCss.overflowX === 'visible' && rootCss.overflowY === 'visible' && !contained(rootCss); for (let a = el.parentElement; a; a = a.parentElement) { const css = getComputedStyle(a); if (bodyIsWindow && a === document.body && !contained(css)) continue; const clipX = css.overflowX !== 'visible'; const clipY = css.overflowY !== 'visible'; if (!clipX && !clipY) continue; const ar = a.getBoundingClientRect(); const sx = a.offsetWidth ? ar.width / a.offsetWidth : 1; const sy = a.offsetHeight ? ar.height / a.offsetHeight : 1; const left = ar.left + (a.clientLeft || 0) * sx; const top = ar.top + (a.clientTop || 0) * sy; if (clipX) { b.left = Math.max(b.left, left); b.right = Math.min(b.right, left + a.clientWidth * sx); } if (clipY) { b.top = Math.max(b.top, top); b.bottom = Math.min(b.bottom, top + a.clientHeight * sy); } } return b; }; const clipped = (box, b) => { const left = Math.max(box.left, b.left); const top = Math.max(box.top, b.top); const right = Math.min(box.left + box.width, b.right); const bottom = Math.min(box.top + box.height, b.bottom); return right > left && bottom > top ? { left, top, width: right - left, height: bottom - top } : null; }; // The points of a part tried in turn: its middle, then the middle of each quarter. A clip-path or // a rotated box leaves its bounding box's middle empty while the target is plainly there. const points = (part) => [[0.5, 0.5], [0.25, 0.25], [0.75, 0.25], [0.25, 0.75], [0.75, 0.75]] .map(([fx, fy]) => [part.left + part.width * fx, part.top + part.height * fy]); const deadline = performance.now() + timeoutMs; let last = ''; const MOVING = 'it kept moving: its rect never held still for two frames in a row (an animation that does not end: wait for it, or turn it off in the test)'; let why = MOVING; for (;;) { // The selector is resolved again every sample: a render that replaced the node leaves the old // one detached and still, and measuring it would time out beside a visible, stable target. const now = find(); if (!now) { el = null; last = ''; why = 'it left the page'; } else { if (now !== el) { el = now; last = ''; reveal(el); revealedBoxed = false; justRevealed = true; offscreenAt = ''; } const r = el.getBoundingClientRect(); const boxed = r.width > 0 || r.height > 0; // Whenever the node loses its box, the next time it has one it is scrolled to again: it may // come back somewhere else (display: none, then shown below the fold). if (!boxed) revealedBoxed = false; if (boxed && !revealedBoxed) { revealedBoxed = true; if (!justRevealed) reveal(el); } justRevealed = false; if (boxed && (r.top + r.height <= 0 || r.top >= vh || r.left + r.width <= 0 || r.left >= vw)) { const where = [r.left, r.top, r.width, r.height].join(); if (where !== offscreenAt) { offscreenAt = where; reveal(el); last = ''; } } else { // Back in the window: the next time it is pushed out, even to the same place, it is a new shift. offscreenAt = ''; } // A wrapped inline element is several boxes, and the centre of the box around them can be // empty space: each fragment's own centre is tried, the first that lands on the target wins. const pieces = typeof el.getClientRects === 'function' ? Array.from(el.getClientRects()).filter((p) => p.width > 0 || p.height > 0) : []; const fragments = pieces.length > 0 ? pieces : [r]; const rect = [r.left, r.top, r.width, r.height] .concat(...fragments.map((p) => [p.left, p.top, p.width, p.height])).join(); const still = rect === last; last = rect; // The reason is the latest sample's: an earlier hidden or covered state must not mask a target // that is now boxed and moving. if (!boxed) why = 'it has no layout box (display: none, or not yet rendered)'; else if (!still) why = MOVING; if (still && boxed) { // Each fragment's visible part, none on screen keeping the whole fragment so the answer // still names the point as outside the viewport; then its points in turn, the first that // lands on the target winning. const bounds = boundsNow(); const shown = fragments.map((f) => clipped(f, bounds)).filter(Boolean); // A press lands when the hit is the target or inside it, but never on an iframe, held by the // target or the target itself: the click goes into the frame's document. const lands = (top) => Boolean(top) && top.localName !== 'iframe' && top.localName !== 'frame' && (top === el || el.contains(top)); // Where the pointer already is wins when it still hits the target: a hover handler can // cover the middle when the pointer reaches it, and picking a new point each time would // chase the page's own response until the timeout. let covering; if (prefer) { const top = document.elementFromPoint(prefer[0], prefer[1]); if (lands(top)) return { at: [prefer[0], prefer[1]] }; } // The parts are tried clipped by the ancestors first. That clip is an estimate (a body's // overflow belongs to the window, a border box can be transformed, overflow-clip-margin // shows more), so when every point of it misses, the parts clipped by the window alone are // tried too: the hit test, not the estimate, decides what is pressed. const inWindow = fragments.map((f) => clipped(f, { left: 0, top: 0, right: vw, bottom: vh })).filter(Boolean); const passes = shown.length > 0 ? [shown, inWindow] : [inWindow.length > 0 ? inWindow : fragments]; for (const parts of passes) { for (const part of parts) { for (const [x, y] of points(part)) { const top = document.elementFromPoint(x, y); if (lands(top)) return { at: [x, y] }; if (covering === undefined) covering = top; } } } why = 'covered by ' + describe(covering); } } if (performance.now() >= deadline) return { blocked: why }; await frame(); } })`; /** * The expression `Runtime.evaluate` runs; `awaitPromise` settles it. `prefer` is the point the * pointer is already at: answered as it is when it still hits the target. */ export const pointerProbe = ( selector: string, timeoutMs: number, prefer: readonly [number, number] | null = null, ): string => `${SOURCE}(${JSON.stringify(selector)}, ${String(timeoutMs)}, ${JSON.stringify(prefer)})`;