Skip to content

The design language that makes your AI harness better at design.

JavaScript

Seed the topmost already-open modal by hit test; test the chrome-free clone

Abdul Wahab6d1d354d5c4c
diff --git a/skill/scripts/live-browser-dom.js b/skill/scripts/live-browser-dom.js
index c8fb8f96a..71ae5d3b5 100644
--- a/skill/scripts/live-browser-dom.js
+++ b/skill/scripts/live-browser-dom.js
@@ -157,7 +157,12 @@
// Parked in the page's dialog, chrome clicks would bubble into its own
// handlers, such as a click-outside-the-box close. Live listens in capture.
topLayerHost.addEventListener('click', (e) => e.stopPropagation());
- openModals.push(...doc.querySelectorAll('dialog:modal'));
+ // Modals already open have no readable top-layer order, but the topmost
+ // one's backdrop covers the viewport, so a hit test lands in it. It goes
+ // last; the rest keep document order.
+ const top = doc.elementFromPoint(0, 0)?.closest('dialog:modal');
+ const open = [...doc.querySelectorAll('dialog:modal')];
+ openModals.push(...open.filter((m) => m !== top), ...open.filter((m) => m === top));
const observer = new MutationObserver(syncTopLayerHost);
observer.observe(doc, { subtree: true, childList: true, attributes: true, attributeOldValue: true, attributeFilter: ['open'] });
syncTopLayerHost();
@@ -168,6 +173,14 @@
};
}
+ // A copy of a page element without the chrome parked inside it, which a
+ // picked modal dialog contains.
+ function cloneWithoutChrome(el) {
+ const clone = el.cloneNode(true);
+ if (el.contains(topLayerHost)) clone.querySelector('#' + cssId(topLayerHost.id)).remove();
+ return clone;
+ }
+
// Mount a chrome node on the page itself: <body>, or the top-layer host
// while the chrome is parked in a modal.
function uiAppendToPage(el) {
@@ -236,6 +249,7 @@
uiAppendToPage,
topLayerHost,
watchModalDialogs,
+ cloneWithoutChrome,
uiAppendStyle,
uiGetById,
activeElementDeep,
diff --git a/skill/scripts/live-browser.js b/skill/scripts/live-browser.js
index 99d013da2..8a0318ccb 100644
--- a/skill/scripts/live-browser.js
+++ b/skill/scripts/live-browser.js
@@ -295,6 +295,7 @@
uiAppendToPage,
topLayerHost,
watchModalDialogs,
+ cloneWithoutChrome,
uiAppendStyle,
uiGetById,
activeElementDeep,
@@ -971,12 +972,10 @@
}
}
- // The element as the agent reads it, text and markup alike. The chrome
- // parks inside an open modal <dialog> (live-browser-dom.js), so a picked
- // dialog contains it; the clone must not.
+ // The element as the agent reads it, text and markup alike: no parked
+ // chrome (a picked modal dialog contains it) and no edit runtime state.
function sanitizedContextClone(el) {
- const clone = el.cloneNode(true);
- if (el.contains(topLayerHost)) clone.querySelector('#' + topLayerHost.id).remove();
+ const clone = cloneWithoutChrome(el);
stripManualEditRuntimeState(clone);
return clone;
}
diff --git a/tests/live-browser-source.test.mjs b/tests/live-browser-source.test.mjs
index 9af6d5c97..c38ccacdb 100644
--- a/tests/live-browser-source.test.mjs
+++ b/tests/live-browser-source.test.mjs
@@ -311,8 +311,13 @@ describe('live-browser source contracts', () => {
);
assert.match(
SOURCE,
- /function sanitizedContextClone\(el\)[\s\S]*?stripManualEditRuntimeState\(clone\);/,
- 'manual copy edit prompt context should strip browser-only edit markers before staging HTML',
+ /function sanitizedContextClone\(el\) \{\s*const clone = cloneWithoutChrome\(el\);\s*stripManualEditRuntimeState\(clone\);/,
+ 'staged context should drop chrome parked in a picked modal dialog and strip browser-only edit markers',
+ );
+ assert.match(
+ SOURCE,
+ /filter: \(node\) => node !== topLayerHost,/,
+ 'element captures should leave out chrome parked in a picked modal dialog',
);
assert.match(
SOURCE,
diff --git a/tests/live-top-layer.test.mjs b/tests/live-top-layer.test.mjs
index af1f3d63c..4b36bed98 100644
--- a/tests/live-top-layer.test.mjs
+++ b/tests/live-top-layer.test.mjs
@@ -26,6 +26,8 @@ const PAGE = `
<button id="close" onclick="d.close()">Close</button>
<dialog id="d2"><p>Nested</p></dialog>
</dialog>
+ <dialog id="e1"><p>First</p></dialog>
+ <dialog id="e2"><p>Second</p></dialog>
<div style="height: 2000px"></div>`;
let browser;
@@ -54,10 +56,11 @@ beforeEach(async () => {
const bar = document.createElement('div');
bar.id = 'impeccable-live-bar';
bar.style.cssText = 'position: fixed; left: 20px; bottom: 20px; width: 160px; height: 40px; z-index: 100005';
+ bar.textContent = 'Live bar';
bar.onclick = () => { window.clicks = (window.clicks || 0) + 1; };
h.uiAppend(bar);
window.live = h;
- h.watchModalDialogs();
+ window.stopWatch = h.watchModalDialogs();
});
});
@@ -140,6 +143,26 @@ describe('live chrome under a modal dialog', () => {
assert.equal(await page.evaluate(() => d.open), true);
});
+ it('parks in the topmost of modals already open when watching starts', async () => {
+ // Opened against document order, so document order would pick e2.
+ await page.evaluate(() => { window.stopWatch(); e2.showModal(); e1.showModal(); window.live.watchModalDialogs(); });
+ assert.equal(await parentOf(HOST), 'e1');
+ await click('#impeccable-live-bar');
+ assert.equal(await clicks(), 1);
+ });
+
+ it('leaves parked chrome out of a copy of the picked dialog', async () => {
+ await page.evaluate(() => d.showModal());
+ const copy = await page.evaluate(() => {
+ const clone = window.live.cloneWithoutChrome(d);
+ return { host: d.contains(window.live.topLayerHost), parked: !!clone.querySelector('[id^="impeccable-live"]'), text: clone.textContent };
+ });
+ assert.equal(copy.host, true);
+ assert.equal(copy.parked, false);
+ assert.doesNotMatch(copy.text, /Live bar/);
+ assert.match(copy.text, /Inside/);
+ });
+
it('puts the chrome back when an open modal is removed from the document', async () => {
await page.evaluate(() => d.showModal());
assert.equal(await parentOf(HOST), 'd');