Share popover: leave for the share page only on an answer, and only while open
The first-load fallback navigated whenever the panel load failed (review on #106, delta): offline, Chromium put chrome-error:// where the editor was, losing what was typed offline; and a popover closed with Escape while loading still navigated when the load failed. Now a failure closes the popover, and Share goes to the share page only when the server answered with something that is not the panel (sign-in, 404) and this opening is still the open one. A request with no answer stays on the document. A load for a closed or reopened popover draws nothing. share-pop.mjs's first-load leg is five: a 404 (share page), offline with words typed (stays, words kept), an aborted request (stays), Escape then a 404 (stays, closed), an expired session (sign-in). The busy leg adds two presses of Invite while the first is held: one request; its route now holds and counts /d/x/share too. Seen to fail on the old fallback, without the open check, without the answered check, and without busy. i2.mjs's short-window list check is guarded by anchor support too.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+105
−31
fixtures/engines/i2.mjs+1 −1fixtures/engines/share-pop.mjs+71 −19internal/docs/brand/share-pop.js+33 −11
CI passed — run details
diff --git a/fixtures/engines/i2.mjs b/fixtures/engines/i2.mjs| index 71b787f..8fecd1f 100644 |
| --- a/fixtures/engines/i2.mjs |
| +++ b/fixtures/engines/i2.mjs |
| @@ -273,7 +273,7 @@ for (const engine of [chromium, webkit]) { |
| const hit = outside && document.elementFromPoint(outside.x, outside.y) |
| return { list: { t: r.top, b: r.bottom, l: r.left, r: r.right, h: r.height }, box: { t: box.top, b: box.bottom, l: box.left, r: box.right }, outside, inList: !!(hit && list.contains(hit)), position: getComputedStyle(list).position } |
| }) |
| - if (!l.outside) fail(`300px tall: the font list (${JSON.stringify(l.list)}) never leaves the inspector (${JSON.stringify(l.box)}): it is clipped by it, or select.js shrank it to the room the inspector leaves (both mean the panel is the list's clipping box)`) |
| + if (!g.anchors) { /* no anchor positioning: the panel does not scroll, and the lists are its own (show.html) */ } else if (!l.outside) fail(`300px tall: the font list (${JSON.stringify(l.list)}) never leaves the inspector (${JSON.stringify(l.box)}): it is clipped by it, or select.js shrank it to the room the inspector leaves (both mean the panel is the list's clipping box)`) |
| else if (!l.inList) fail(`300px tall: the font list is clipped where it leaves the inspector (${JSON.stringify(l)})`) |
| await context.close() |
| } |
diff --git a/fixtures/engines/share-pop.mjs b/fixtures/engines/share-pop.mjs| index ecbfe1a..4e36f97 100644 |
| --- a/fixtures/engines/share-pop.mjs |
| +++ b/fixtures/engines/share-pop.mjs |
| @@ -111,7 +111,7 @@ for (const engine of [chromium, webkit]) { |
| let postAt = 0 |
| page.on('requestfinished', (r) => { if (r.method() === 'POST') finished++ }) |
| page.on('requestfailed', (r) => { if (r.method() === 'POST') finished++ }) |
| - await page.route('**/share/**', async (route) => { |
| + await page.route((u) => u.pathname.includes('/share'), async (route) => { |
| if (route.request().method() !== 'POST') return route.continue() |
| posts++ |
| if (!postAt) postAt = Date.now() |
| @@ -144,6 +144,15 @@ for (const engine of [chromium, webkit]) { |
| if (posts !== 1) fail(`${posts} requests were sent for one switch and one refused flip, want 1`) |
| if ((await checked(1)) !== link) fail('the refused flip took effect after all') |
| + // Invite twice while the first is held: one request. |
| + await page.fill('[docs-share-invite] input[type=email]', 'dee@example.com') |
| + const sent = posts |
| + await page.click('[docs-share-submit]') |
| + await page.click('[docs-share-submit]') |
| + await until(async () => finished === sent + 1, 5000) |
| + await pause(300) |
| + if (posts !== sent + 1) fail(`two presses of Invite while the first was in flight sent ${posts - sent} requests, want 1`) |
| + |
| mode = 'abort' |
| const before = await checked(0) |
| await rung(0).locator('[rst-switch]').click() |
| @@ -152,30 +161,73 @@ for (const engine of [chromium, webkit]) { |
| await context.close() |
| } |
| - // The first load failing (review on #106): nothing is drawn, so Share |
| - // goes to the share page, which handles a lost session and a 404 |
| - // itself. A refused panel request, then an expired session. |
| + // The first load failing (review on #106). An answer that is not the |
| + // panel (a 404, the sign-in page) goes to the share page, which handles |
| + // both itself. A request that never got an answer (offline) stays on |
| + // the document: leaving would put the browser's error page where the |
| + // editor was, and anything typed offline with it. And a popover the |
| + // person has already closed stays closed, whatever comes back. |
| { |
| const context = await browser.newContext({ viewport: { width: 1280, height: 900 }, deviceScaleFactor: 1 }) |
| await context.addCookies(config.cookies.map((c) => ({ name: c.Name, value: c.Value, url: config.origin, httpOnly: c.HttpOnly, sameSite: 'Lax' }))) |
| const page = await context.newPage() |
| const doc = config.docs.simple.shift() |
| - await page.goto(config.origin + '/d/' + doc) |
| - await page.waitForSelector('[docs-text][contenteditable="true"]', { timeout: 30000 }) |
| - await page.route((u) => u.searchParams.get('panel') === '1', (route) => route.abort()) |
| - await Promise.all([ |
| - page.waitForURL(config.origin + '/d/' + doc + '/share', { timeout: 8000 }).catch(() => {}), |
| - page.click('[estilo-bar-actions] a[href$="/share"]'), |
| - ]) |
| - if (page.url() !== config.origin + '/d/' + doc + '/share') fail(`a failed first load left the page at ${page.url()}, want the share page`) |
| - await page.unroute((u) => u.searchParams.get('panel') === '1') |
| - await page.goto(config.origin + '/d/' + doc) |
| - await page.waitForSelector('[docs-text][contenteditable="true"]', { timeout: 30000 }) |
| + const docURL = config.origin + '/d/' + doc |
| + const share = () => page.click('[estilo-bar-actions] a[href$="/share"]') |
| + const isPanel = (u) => u.searchParams.get('panel') === '1' |
| + const fresh = async () => { |
| + if (page.url() !== docURL) await page.goto(docURL) |
| + await page.waitForSelector('[docs-text][contenteditable="true"]', { timeout: 30000 }) |
| + } |
| + const popOpen = () => page.evaluate(() => { const d = document.querySelector('[docs-share-pop]'); return !!d && (d.open || d.matches(':popover-open')) }) |
| + const stays = async (what) => { |
| + await pause(1500) |
| + if (page.url() !== docURL) fail(`${what}: Share left the document for ${page.url()}`) |
| + else if (!(await page.locator('[docs-text][contenteditable="true"]').count().catch(() => 0))) fail(`${what}: the editor is gone`) |
| + else if (await popOpen()) fail(`${what}: the empty popover is still open`) |
| + } |
| + |
| + // A 404 answer: the share page. |
| + await fresh() |
| + await page.route(isPanel, (route) => route.fulfill({ status: 404, contentType: 'text/html', body: '<!doctype html><p>Not found</p>' })) |
| + await Promise.all([page.waitForURL(docURL + '/share', { timeout: 8000 }).catch(() => {}), share()]) |
| + if (page.url() !== docURL + '/share') fail(`a 404 for the panel left the page at ${page.url()}, want the share page`) |
| + await page.unroute(isPanel) |
| + |
| + // Offline: stays, with the words typed offline. |
| + await fresh() |
| + await page.click('[docs-text] p') |
| + await page.keyboard.press('End') |
| + await context.setOffline(true) |
| + await page.keyboard.type(' offline words') |
| + await share() |
| + await stays('offline') |
| + if (!(await page.locator('[docs-text]').innerText({ timeout: 2000 }).catch(() => '')).includes('offline words')) fail('offline: the words typed offline are gone') |
| + await context.setOffline(false) |
| + |
| + // A request that fails without an answer: stays. |
| + await fresh() |
| + await page.route(isPanel, (route) => route.abort()) |
| + await share() |
| + await stays('an aborted panel request') |
| + await page.unroute(isPanel) |
| + |
| + // Escape while it loads, then a 404: stays, closed. |
| + await fresh() |
| + await page.route(isPanel, async (route) => { |
| + await pause(800) |
| + await route.fulfill({ status: 404, contentType: 'text/html', body: '<!doctype html><p>Not found</p>' }) |
| + }) |
| + await share() |
| + await pause(100) |
| + await page.keyboard.press('Escape') |
| + await stays('Escape, then a 404') |
| + await page.unroute(isPanel) |
| + |
| + // The session gone: the sign-in page came back, so sign-in. |
| + await fresh() |
| await context.clearCookies() |
| - await Promise.all([ |
| - page.waitForURL((u) => u.pathname.startsWith('/signin'), { timeout: 8000 }).catch(() => {}), |
| - page.click('[estilo-bar-actions] a[href$="/share"]'), |
| - ]) |
| + await Promise.all([page.waitForURL((u) => u.pathname.startsWith('/signin'), { timeout: 8000 }).catch(() => {}), share()]) |
| if (!new URL(page.url()).pathname.startsWith('/signin')) fail(`with the session gone, Share left the page at ${page.url()}, want sign-in`) |
| await context.close() |
| } |
diff --git a/internal/docs/brand/share-pop.js b/internal/docs/brand/share-pop.js| index dee9190..93e5b4e 100644 |
| --- a/internal/docs/brand/share-pop.js |
| +++ b/internal/docs/brand/share-pop.js |
| @@ -39,6 +39,7 @@ |
| var opener = null; |
| var busy = false; |
| var drawn = false; |
| + var opening = 0; |
| var isOpen = function () { |
| return pop.open || (canPopover && pop.hasAttribute("popover") && pop.matches(":popover-open")); |
| @@ -97,8 +98,11 @@ |
| }; |
| // The panel from an answer; anything else (a 404, a sign-in page, a |
| - // proxy's error) is a failure, not something to draw. |
| - var load = function (url, init) { |
| + // proxy's error) is a failure, not something to draw. That failure is |
| + // marked answered: the server said something, which the share page can |
| + // say better. A fetch that never got an answer (offline) rejects |
| + // without the mark. |
| + var load = function (url, init, still) { |
| return fetch(url, Object.assign({ credentials: "same-origin", headers: { Accept: "text/html" } }, init || {})) |
| .then(function (r) { |
| return r.text(); |
| @@ -106,9 +110,16 @@ |
| .then(function (text) { |
| var doc = new DOMParser().parseFromString(text, "text/html"); |
| var panel = doc.querySelector("[docs-share-panel]"); |
| - if (!panel) throw new Error("no panel"); |
| + if (!panel) { |
| + var err = new Error("no panel"); |
| + err.answered = true; |
| + throw err; |
| + } |
| + // A load for a popover since closed, or reopened, draws nothing. |
| + if (still && !still()) return false; |
| pop.replaceChildren(document.importNode(panel, true)); |
| pop.removeAttribute("aria-busy"); |
| + return true; |
| }); |
| }; |
| @@ -142,21 +153,32 @@ |
| pop.replaceChildren(); |
| pop.setAttribute("aria-busy", "true"); |
| show(); |
| - load(href + "?panel=1") |
| - .then(function () { |
| + // This opening's load, and whether it is still the open one: closed |
| + // (Escape, a click outside) or opened again since, it is not. |
| + var mine = ++opening; |
| + var still = function () { return mine === opening && isOpen(); }; |
| + load(href + "?panel=1", null, still) |
| + .then(function (shown) { |
| + if (!shown) return; |
| drawn = true; |
| var email = pop.querySelector('[docs-share-invite] input[type="email"]'); |
| if (email) email.focus({ preventScroll: true }); |
| }) |
| - .catch(function () { |
| + .catch(function (err) { |
| // Nothing drawn yet, so nothing to say a failure in and no close |
| - // button (review on #106): an expired session (the sign-in page |
| - // came back), a 404 or a lost network. Go to the share page, |
| - // which is what Share is with no script, and which handles |
| - // sign-in and 404 itself. failed() is for a redraw. |
| + // button (review on #106). The popover closes and, only if the |
| + // server answered with something that is not the panel (the |
| + // sign-in page for an expired session, a 404) and the person |
| + // still has it open, Share goes to the share page, which is what |
| + // Share is with no script and handles both itself. A request |
| + // that got no answer (offline) stays on the document: leaving |
| + // would put the browser's error page where the editor was, and |
| + // the words typed offline with it. failed() is for a redraw. |
| + var go = !!(err && err.answered) && still(); |
| + if (mine !== opening) return; |
| pop.removeAttribute("aria-busy"); |
| close(false); |
| - location.assign(href); |
| + if (go) location.assign(href); |
| }); |
| }; |