The design language that makes your AI harness better at design.
JavaScript
Full-page shots and scroll restores see through shadow roots
Paul Bakaus11d35bdb7886
diff --git a/crates/browser/src/fullpage.rs b/crates/browser/src/fullpage.rsindex 8f653a5bf..f27c3ba45 100644--- a/crates/browser/src/fullpage.rs+++ b/crates/browser/src/fullpage.rs@@ -241,10 +241,30 @@ const SCROLL_TO_JS: &str = r#"(async ({ x, y, useScroller, hideFixed }) => {y: el ? el.scrollTop : window.scrollY,maxY: el ? el.scrollHeight - el.clientHeight : se.scrollHeight - window.innerHeight,};+ // Every element under body, open shadow trees included: chrome a widget+ // renders into a shadow root repeats on tiles like any other.+ const deepAll = () => {+ const all = [];+ const visit = scope => {+ for (const node of scope.querySelectorAll('*')) {+ all.push(node);+ if (node.shadowRoot) visit(node.shadowRoot);+ }+ };+ if (document.body) visit(document.body);+ return all;+ };+ // The flat-tree parent: a slotted node's slot, else its parent, else the+ // host of the shadow root it sits at the top of.+ const up = n => n.assignedSlot || n.parentElement || (n.parentNode && n.parentNode.host) || null;+ const holds = (node, target) => {+ for (let n = target; n; n = up(n)) if (n === node) return true;+ return false;+ };// The first tile keeps where every positioned box sits in the viewport.if (!hideFixed && !window.__impeccableShotAnchors) {const boxes = [];- for (const node of document.querySelectorAll('body *')) {+ for (const node of deepAll()) {const position = getComputedStyle(node).position;if (position !== 'absolute' && position !== 'sticky') continue;const r = node.getBoundingClientRect();@@ -262,13 +282,14 @@ const SCROLL_TO_JS: &str = r#"(async ({ x, y, useScroller, hideFixed }) => {hidden.push([node, node.style.getPropertyValue('opacity'), node.style.getPropertyPriority('opacity')]);node.style.setProperty('opacity', '0', 'important');};- for (const node of document.querySelectorAll('body *')) {+ for (const node of deepAll()) {if (getComputedStyle(node).position !== 'fixed') continue;- // A page can scroll a fixed frame: that frame is the content.- if (el && node.contains(el)) continue;+ // A page can scroll a fixed frame: that frame is the content, even+ // when the frame sits in the fixed box's shadow tree.+ if (el && holds(node, el)) continue;// Under a transformed or filtered ancestor a fixed box scrolls along.let contained = false;- for (let p = node.parentElement; p && !contained; p = p.parentElement) {+ for (let p = up(node); p && !contained; p = up(p)) {const cs = getComputedStyle(p);contained = cs.transform !== 'none' || cs.filter !== 'none' || cs.perspective !== 'none'|| cs.willChange.includes('transform') || cs.contain.includes('paint');@@ -281,7 +302,7 @@ const SCROLL_TO_JS: &str = r#"(async ({ x, y, useScroller, hideFixed }) => {const anchors = window.__impeccableShotAnchors;if (anchors && Math.abs(pos.x - anchors.x) + Math.abs(pos.y - anchors.y) >= 1) {for (const [node, left, top] of anchors.boxes) {- if (!node.isConnected || (el && node.contains(el))) continue;+ if (!node.isConnected || (el && holds(node, el))) continue;const r = node.getBoundingClientRect();if (r.width < 1 || r.height < 1) continue;if (Math.abs(r.left - left) + Math.abs(r.top - top) < 0.5) hide(node);@@ -328,8 +349,11 @@ const ELEMENT_INTO_VIEW_JS: &str = r#"(async (resolve, { selector, identity, reureturn null;}if (!el) return null;+ // Ancestors through slots and shadow roots: a slotted element scrolls+ // inside its host's shadow frame.+ const up = n => n.assignedSlot || n.parentElement || (n.parentNode && n.parentNode.host) || null;const chain = [];- for (let p = el.parentElement; p; p = p.parentElement) chain.push(p);+ for (let p = up(el); p; p = up(p)) chain.push(p);// How much of the element shows: its box cut by the viewport and by every// ancestor that clips overflow, as a share of its own area.const showing = () => {diff --git a/crates/browser/src/screenshot_contrast.rs b/crates/browser/src/screenshot_contrast.rsindex e0ddbb10d..a3de9decd 100644--- a/crates/browser/src/screenshot_contrast.rs+++ b/crates/browser/src/screenshot_contrast.rs@@ -292,7 +292,10 @@ pub(crate) const PICK_CANDIDATE_JS: &str = r#"((selector, match) => {const BRING_INTO_VIEW_JS: &str = r#"(async (el) => {if (!el) return null;const saved = [];- for (let p = el.parentElement; p; p = p.parentElement) saved.push([p, p.scrollTop, p.scrollLeft]);+ // Ancestors through slots and shadow roots, so a shadow frame the scroll+ // moves is put back too.+ const up = n => n.assignedSlot || n.parentElement || (n.parentNode && n.parentNode.host) || null;+ for (let p = up(el); p; p = up(p)) saved.push([p, p.scrollTop, p.scrollLeft]);const sx = window.scrollX;const sy = window.scrollY;el.scrollIntoView({ block: 'center', inline: 'nearest', behavior: 'instant' });@@ -541,6 +544,34 @@ mod tests {use super::*;use impeccable_core::js::to_fixed;+ /// A slotted element scrolls inside its host's shadow frame: bringing it+ /// into view records that frame, and the restore puts it back.+ #[test]+ fn a_shadow_frame_the_scroll_moves_is_put_back() {+ let env: std::collections::HashMap<String, String> = std::env::vars().collect();+ let Ok(exe) = crate::discovery::find_browser(&env) else { return };+ let Ok(mut browser) = crate::cdp::Browser::launch(&exe, &[], false) else { return };+ let mut page = browser.new_page().unwrap();+ page.goto("about:blank", "load", std::time::Duration::from_secs(15)).unwrap();+ let setup = r#"(() => {+ document.body.innerHTML = '<x-frame><p id="deep">Deep copy</p></x-frame>';+ document.querySelector('x-frame').attachShadow({ mode: 'open' }).innerHTML =+ '<div id="frame" style="height:300px;overflow:auto"><div style="height:3000px"></div><slot></slot></div>';+ return true;+})()"#;+ page.evaluate_value(setup).unwrap();+ let moved = page+ .evaluate_value(&format!("({BRING_INTO_VIEW_JS})(document.getElementById('deep'))"))+ .unwrap();+ assert_eq!(moved.get("moved").and_then(Value::as_bool), Some(true), "{moved}");+ let frame = "document.querySelector('x-frame').shadowRoot.getElementById('frame').scrollTop";+ assert!(page.evaluate_value(frame).unwrap().as_f64().unwrap() > 0.0);+ page.evaluate_value(RESTORE_SCROLL_JS).unwrap();+ assert_eq!(page.evaluate_value(frame).unwrap().as_f64(), Some(0.0));+ page.close();+ browser.close();+ }+#[test]fn pixel_snippet_never_prints_a_failing_verdict_as_the_bar() {assert_eq!(diff --git a/crates/browser/src/snapshot_engine.rs b/crates/browser/src/snapshot_engine.rsindex 4f0a238fc..2b82123fb 100644--- a/crates/browser/src/snapshot_engine.rs+++ b/crates/browser/src/snapshot_engine.rs@@ -297,7 +297,7 @@ fn scroll_into_view(page: &mut Page<'_>, candidate: &Value) -> CdpResult<bool> {// it moves (a page that scrolls inside its body or an app shell's main) can// be put back: `window.scrollTo` never reaches those.let expr = format!(- "(function(){{ const el = ({})({}, {}); if (!el || typeof el.scrollIntoView !== 'function') return false; const saved = window.__impeccableVisualScrollSaved || (window.__impeccableVisualScrollSaved = new Map()); for (let p = el.parentElement; p; p = p.parentElement) {{ if (!saved.has(p)) saved.set(p, [p.scrollTop, p.scrollLeft]); }} el.scrollIntoView({{ block: 'center', inline: 'nearest', behavior: 'instant' }}); return true; }})()",+ "(function(){{ const el = ({})({}, {}); if (!el || typeof el.scrollIntoView !== 'function') return false; const saved = window.__impeccableVisualScrollSaved || (window.__impeccableVisualScrollSaved = new Map()); const up = n => n.assignedSlot || n.parentElement || (n.parentNode && n.parentNode.host) || null; for (let p = up(el); p; p = up(p)) {{ if (!saved.has(p)) saved.set(p, [p.scrollTop, p.scrollLeft]); }} el.scrollIntoView({{ block: 'center', inline: 'nearest', behavior: 'instant' }}); return true; }})()",crate::screenshot_contrast::PICK_CANDIDATE_JS,json!(selector),identitydiff --git a/crates/browser/tests/shadow_trees.rs b/crates/browser/tests/shadow_trees.rsindex da9492aba..449fab0af 100644--- a/crates/browser/tests/shadow_trees.rs+++ b/crates/browser/tests/shadow_trees.rs@@ -17,6 +17,7 @@ use impeccable_browser::{EvidenceRequest,};use impeccable_detect::engines::ScanOptions;+use base64::Engine as _;use serde_json::Value;fn serve(body: &'static str) -> u16 {@@ -149,3 +150,64 @@ fn a_shadow_root_scroll_frame_is_captured_by_scrolling_the_frame() {// Twelve 400px rows: the frame's whole height, not one viewport.assert!(shot.height >= 4800.0, "{}", shot.height);}++const FIXED_SHADOW_SHELL_PAGE: &str = r#"<!doctype html><html><head><style>+html, body { margin: 0; height: 100%; overflow: hidden; }+app-shell { position: fixed; inset: 0; display: block; }+</style></head><body>+<app-shell></app-shell>+<chat-widget></chat-widget>+<script>+ const root = document.querySelector('app-shell').attachShadow({ mode: 'open' });+ root.innerHTML = `<style>+ .frame { height: 100vh; overflow: auto; }+ .row { height: 400px; display: flex; align-items: center; padding: 0 40px; font: 32px sans-serif; }+ .row:nth-child(odd) { background: #1d4ed8; color: #fff; }+ .row:nth-child(even) { background: #f59e0b; color: #111; }+ </style><div class="frame">${Array.from({ length: 12 }, (_, i) => `<div class="row">Row ${i + 1}</div>`).join('')}</div>`;+ document.querySelector('chat-widget').attachShadow({ mode: 'open' }).innerHTML =+ '<div style="position:fixed;right:24px;bottom:24px;width:160px;height:160px;background:#16a34a;z-index:10"></div>';+</script>+</body></html>"#;++#[test]+fn a_fixed_shadow_shell_keeps_its_frame_and_drops_repeated_shadow_chrome() {+ let env: HashMap<String, String> = std::env::vars().collect();+ if discovery::find_browser(&env).is_err() {+ eprintln!("skip: no installed browser found");+ return;+ }+ let port = serve(FIXED_SHADOW_SHELL_PAGE);+ let engine = BrowserEngine::new(env);+ let mut browser = engine.launch().expect("launch");+ let options = ScanOptions { viewport: Some((1280, 800)), ..Default::default() };+ let (_, evidence) = detect_url_evidence(+ &mut browser,+ &format!("http://127.0.0.1:{port}/"),+ &options,+ "load",+ 100,+ &EvidenceRequest::default(),+ )+ .expect("evidence scan");+ browser.close();+ let shot = evidence.screenshot.as_ref().expect("screenshot");+ assert_eq!(shot.method, fullpage::method::STITCHED);+ let bytes = base64::engine::general_purpose::STANDARD.decode(&shot.jpeg_base64).expect("base64");+ let img = image::load_from_memory_with_format(&bytes, image::ImageFormat::Jpeg).expect("jpeg").to_rgb8();+ // The fixed host holds the frame being scrolled, so later tiles keep it.+ let (_, blank) = fullpage::longest_uniform_band(&img);+ assert!(blank < 800, "a blank band of {blank} rows");+ // The widget in a shadow root paints once, in the first viewport.+ let green = |y0: u32, y1: u32| {+ (y0..y1.min(img.height()))+ .flat_map(|y| (0..img.width()).map(move |x| (x, y)))+ .filter(|&(x, y)| {+ let p = img.get_pixel(x, y).0;+ p[1] > 120 && p[0] < 60 && p[2] < 110+ })+ .count()+ };+ assert!(green(0, 800) > 5000, "the widget is missing from the first viewport");+ assert_eq!(green(800, img.height()), 0, "the widget repeats below the first viewport");+}