Skip to content

Open-source AI browser agent for Chrome and Firefox (monorepo) 🧠

fix: distinguish pinned and unstuck sticky controls

Barack Sokullu9bacfa531a34
diff --git a/src/chrome/src/content/content.js b/src/chrome/src/content/content.js
index 3fe5a527b..aeba54c94 100644
--- a/src/chrome/src/content/content.js
+++ b/src/chrome/src/content/content.js
@@ -472,8 +472,25 @@
if (!_isFullyVisibleForInteraction(el, { top: 0, bottom: 0 })) return false;
for (let node = el; node; node = _composedParent(node)) {
if (node.nodeType !== Node.ELEMENT_NODE) continue;
- const position = view.getComputedStyle(node).position;
- if (position === 'fixed' || position === 'sticky') return true;
+ const style = view.getComputedStyle(node);
+ if (style.position === 'fixed') return true;
+ if (style.position !== 'sticky') continue;
+ let portTop = 0;
+ let portBottom = view.innerHeight;
+ for (let parent = _composedParent(node); parent; parent = _composedParent(parent)) {
+ if (parent.nodeType !== Node.ELEMENT_NODE) continue;
+ const parentStyle = view.getComputedStyle(parent);
+ if (!/^(?:auto|scroll|hidden)$/.test(parentStyle.overflowY)) continue;
+ const parentRect = parent.getBoundingClientRect();
+ portTop = parentRect.top + parent.clientTop;
+ portBottom = portTop + parent.clientHeight;
+ break;
+ }
+ const rect = node.getBoundingClientRect();
+ const top = parseFloat(style.top);
+ const bottom = parseFloat(style.bottom);
+ if ((Number.isFinite(top) && Math.abs(rect.top - portTop - top) <= 2)
+ || (Number.isFinite(bottom) && Math.abs(rect.bottom - portBottom + bottom) <= 2)) return true;
}
return false;
}
diff --git a/src/firefox/src/content/content.js b/src/firefox/src/content/content.js
index d01f19439..b035a0c8f 100644
--- a/src/firefox/src/content/content.js
+++ b/src/firefox/src/content/content.js
@@ -731,8 +731,25 @@
if (!_isFullyVisibleForInteraction(el, { top: 0, bottom: 0 })) return false;
for (let node = el; node; node = _composedParent(node)) {
if (node.nodeType !== Node.ELEMENT_NODE) continue;
- const position = view.getComputedStyle(node).position;
- if (position === 'fixed' || position === 'sticky') return true;
+ const style = view.getComputedStyle(node);
+ if (style.position === 'fixed') return true;
+ if (style.position !== 'sticky') continue;
+ let portTop = 0;
+ let portBottom = view.innerHeight;
+ for (let parent = _composedParent(node); parent; parent = _composedParent(parent)) {
+ if (parent.nodeType !== Node.ELEMENT_NODE) continue;
+ const parentStyle = view.getComputedStyle(parent);
+ if (!/^(?:auto|scroll|hidden)$/.test(parentStyle.overflowY)) continue;
+ const parentRect = parent.getBoundingClientRect();
+ portTop = parentRect.top + parent.clientTop;
+ portBottom = portTop + parent.clientHeight;
+ break;
+ }
+ const rect = node.getBoundingClientRect();
+ const top = parseFloat(style.top);
+ const bottom = parseFloat(style.bottom);
+ if ((Number.isFinite(top) && Math.abs(rect.top - portTop - top) <= 2)
+ || (Number.isFinite(bottom) && Math.abs(rect.bottom - portBottom + bottom) <= 2)) return true;
}
return false;
}
diff --git a/test/fixtures/run.mjs b/test/fixtures/run.mjs
index f4d08e816..e41dc67d4 100644
--- a/test/fixtures/run.mjs
+++ b/test/fixtures/run.mjs
@@ -2438,6 +2438,44 @@ test('Chrome: controls inside a fixed side rail keep document scroll position',
firefoxTest('Firefox: controls inside a fixed side rail keep document scroll position', page =>
assertFixedSideRailControlsKeepScroll(page, 'firefox'));
+async function assertUnstuckStickyTargetClearsHeader(page, browserKind) {
+ await setupContentHtml(page, `<!doctype html>
+ <style>
+ body { margin: 0; height: 1700px; }
+ header { position: fixed; top: 0; width: 100%; height: 80px; z-index: 20; background: white; }
+ #spacer { height: 350px; }
+ #sticky-row { position: sticky; top: 0; height: 40px; z-index: 10; }
+ </style>
+ <header>Fixed header</header>
+ <div id="spacer"></div>
+ <div id="sticky-row"><button id="target" onclick="window.__targetClicked = true">Unstuck sticky action</button></div>
+ `, browserKind);
+ const before = await page.evaluate(() => {
+ window.scrollTo(0, 300);
+ return {
+ scrollY: window.scrollY,
+ rowTop: document.getElementById('sticky-row').getBoundingClientRect().top,
+ };
+ });
+ if (before.scrollY !== 300 || before.rowTop <= 0 || before.rowTop >= 80) {
+ throw new Error(`${browserKind}: sticky fixture must be unstuck under the header: ${JSON.stringify(before)}`);
+ }
+ const click = await call(page, 'click', { selector: '#target' });
+ const after = await page.evaluate(() => ({
+ clicked: window.__targetClicked === true,
+ scrollY: window.scrollY,
+ top: document.getElementById('target').getBoundingClientRect().top,
+ }));
+ if (!click?.success || !after.clicked || after.scrollY >= 300 || after.top < 80) {
+ throw new Error(`${browserKind}: unstuck sticky target was not cleared: ${JSON.stringify({ before, click, after })}`);
+ }
+}
+
+test('Chrome: an unstuck sticky control clears a fixed header', page =>
+ assertUnstuckStickyTargetClearsHeader(page, 'chrome'));
+firefoxTest('Firefox: an unstuck sticky control clears a fixed header', page =>
+ assertUnstuckStickyTargetClearsHeader(page, 'firefox'));
+
async function assertStackedDockedBars(page, browserKind) {
await setupContentHtml(page, `<!doctype html>
<style>