Skip to content

Commit da77507

Browse files
Prithvi-Webclaude
andcommitted
fix(a11y): a sidebar search answered after focus left it repaints its results but does not reopen them around someone elsewhere (review F3.5 of QA third pass D10)
Leaving the search - Tab out (F2.7) or Escape (D10) - closed the panel, but a query still in its 140 ms debounce or in flight then answered, and renderGsearch ended with gsearchOpen: the panel reopened with focus on the Dashboard tab, its up-to-50 hits tabbable ahead of the sidebar's tabs, until a click elsewhere. After an Escape a second Escape no longer reached the box's handler. gsearchOpen now opens the results only while focus is inside the search. The answer still repaints the panel, so entering the box again opens it on the answer to what the box says. Cancelling the search on leave (the review's suggestion) was not taken: the panel would keep the older answer behind the newer text, for the focus handler to reopen and Enter to open. Test: frontendContract's search harness now holds the debounce timers and each request for the test to run and answer (counted steps, no clock), and starts the panel hidden as the page does. The test types and Tabs out inside the debounce, Escapes inside it, Tabs out with a request in flight, and has a failed request answer after Escape: the panel stays closed, aria-expanded stays false and focus stays put; entering the box again opens it on "foo"'s answer, not "fo"'s. Eight hand mutants (no guard, the review's cancel shape, a cancel kept beside the guard, the failure path opening directly, aria-expanded set before the guard, a guard that pulls focus back, the focus handler not reopening, a guard that never opens), all RED, each file restored by SHA-256. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 01bfc6b commit da77507

3 files changed

Lines changed: 102 additions & 3 deletions

File tree

‎public/index.html‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35591,7 +35591,17 @@ <h2><span style="display:inline-flex;">${icon('diff', 13)}</span>${cmpWhenPair(r
3559135591
if (gsearch.hits.length) gsearchOpen();
3559235592
}
3559335593

35594+
/**
35595+
* Open the results, for someone in the search. A search typed and then left —
35596+
* Tab or Escape inside the debounce, or while its request was out — still
35597+
* answers and repaints the panel, so coming back shows the answer to what the
35598+
* box says; it no longer reopens the panel around a person who is elsewhere,
35599+
* its hits tabbable ahead of the sidebar's tabs (review F3.5 of QA third pass
35600+
* D10). Cancelling the search instead would leave the older answer behind the
35601+
* newer text, for Enter to open.
35602+
*/
3559435603
function gsearchOpen() {
35604+
if (!$('gsearchWrap').contains(document.activeElement)) return;
3559535605
gsearch.open = true;
3559635606
$('gsearchResults').hidden = false;
3559735607
$('gsearch').setAttribute('aria-expanded', 'true');

‎src/ui/app/225-global-search.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,17 @@ function summonGlobalSearch() {
5757
if (gsearch.hits.length) gsearchOpen();
5858
}
5959

60+
/**
61+
* Open the results, for someone in the search. A search typed and then left —
62+
* Tab or Escape inside the debounce, or while its request was out — still
63+
* answers and repaints the panel, so coming back shows the answer to what the
64+
* box says; it no longer reopens the panel around a person who is elsewhere,
65+
* its hits tabbable ahead of the sidebar's tabs (review F3.5 of QA third pass
66+
* D10). Cancelling the search instead would leave the older answer behind the
67+
* newer text, for Enter to open.
68+
*/
6069
function gsearchOpen() {
70+
if (!$('gsearchWrap').contains(document.activeElement)) return;
6171
gsearch.open = true;
6272
$('gsearchResults').hidden = false;
6373
$('gsearch').setAttribute('aria-expanded', 'true');

‎tests/frontendContract.test.ts‎

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,7 @@ function gsearchHarness(opts: { viewControl: boolean }) {
12301230
};
12311231
const els: Record<string, El> = {};
12321232
for (const id of ['gsearch', 'gsearchResults', 'gsearchWrap', 'sideNav', 'pathInput', 'tmSearch']) els[id] = mk(id);
1233+
els.gsearchResults.hidden = true; // as the page's markup starts it
12331234
/** Who holds whom, for an event to bubble through as a browser's does. */
12341235
const parents = new Map<El, El>();
12351236
const adopt = (parent: El, child: El) => { parent.kids.push(child); parents.set(child, parent); };
@@ -1248,6 +1249,11 @@ function gsearchHarness(opts: { viewControl: boolean }) {
12481249
const tab = mk('tab-dashboard');
12491250
const src = INDEX.slice(INDEX.indexOf('/* ─────────────────────── Global search (A4)'), INDEX.indexOf('/* ───────────────────── Allocation diagnostic (A2)'));
12501251
assert.ok(src.length > 1000, 'the global-search section was found');
1252+
/** The debounce timers the search has set and not cleared, run by hand: a count of steps, never a clock. */
1253+
const timers = new Map<number, () => void>();
1254+
let nextTimer = 1;
1255+
/** The searches sent, in order, each held until the test answers it. */
1256+
const asked: Array<{ url: string; settle(r: unknown): void; fail(e: Error): void }> = [];
12511257
const deps: Record<string, unknown> = {
12521258
$: (id: string) => els[id] ?? assert.fail(`the search touched #${id}`),
12531259
document: {
@@ -1256,8 +1262,10 @@ function gsearchHarness(opts: { viewControl: boolean }) {
12561262
querySelector: (sel: string) => (sel === '.tabbar button[aria-selected="true"]' ? tab : assert.fail(`unexpected query ${sel}`)),
12571263
},
12581264
activeViewControl: () => (opts.viewControl ? viewControl : null),
1259-
applySideNav: () => {}, api: async () => ({ hits: [] }), escapeHtml: String, formatBytes: String, formatCount: String,
1260-
chipFor: () => '', toast: () => {}, state: {}, setTimeout: () => 0, clearTimeout: () => {},
1265+
applySideNav: () => {}, escapeHtml: String, formatBytes: String, formatCount: String, chipFor: () => '', toast: () => {}, state: {},
1266+
api: (url: string) => new Promise((settle, fail) => { asked.push({ url, settle, fail }); }),
1267+
setTimeout: (f: () => void) => { timers.set(nextTimer, f); return nextTimer++; },
1268+
clearTimeout: (id: number) => { timers.delete(id); },
12611269
};
12621270
const keys = Object.keys(deps);
12631271
// eslint-disable-next-line @typescript-eslint/no-implied-eval
@@ -1283,7 +1291,21 @@ function gsearchHarness(opts: { viewControl: boolean }) {
12831291
if (!ev.stopped) for (const f of docListeners[type] || []) f(ev);
12841292
return ev;
12851293
};
1286-
return { els, doc, elsewhere, viewControl, tab, hit, focusBox, escape, fire, pageKeys };
1294+
/** Typing `text` into the box: its value, then the input event the page debounces. */
1295+
const type = (text: string) => { (els.gsearch as El & { value?: string }).value = text; fire(els.gsearch, 'input', {}); };
1296+
/** The debounce running out: every timer still set fires, once. */
1297+
const elapse = () => { for (const [id, f] of [...timers]) { timers.delete(id); f(); } };
1298+
/** The server answering search `i` with files named `names`, and the page's continuations running (turns, not time). */
1299+
const answer = async (i: number, names: string[]) => {
1300+
asked[i].settle({ hits: names.map((name) => ({ name, path: `/r/${name}`, parentPath: '/r', size: 1, type: 'file' })), total: names.length, roots: ['/r'] });
1301+
for (let n = 0; n < 3; n++) await new Promise((r) => setImmediate(r));
1302+
};
1303+
/** Search `i` failing, likewise. */
1304+
const refuse = async (i: number) => {
1305+
asked[i].fail(new Error('Search is not available'));
1306+
for (let n = 0; n < 3; n++) await new Promise((r) => setImmediate(r));
1307+
};
1308+
return { els, doc, elsewhere, viewControl, tab, hit, focusBox, escape, fire, pageKeys, type, elapse, answer, refuse, asked };
12871309
}
12881310

12891311
test('Escape in the sidebar search gives focus back to where it came from — never to <body> (QA third pass D10)', () => {
@@ -1380,6 +1402,63 @@ test('Escape on a sidebar search result leaves the search as Escape in the box d
13801402
assert.equal(t.els.gsearchResults.hidden, false, 'focus going nowhere on the page — another window — leaves them as they were');
13811403
});
13821404

1405+
test('a search answered after focus left the sidebar search does not reopen its results around someone elsewhere — and coming back shows that answer (review F3.5 of QA third pass D10)', async () => {
1406+
// Type "foo" and Tab out within the 140 ms debounce: the focusout closed the
1407+
// panel, then the timer fired, the answer came back, and renderGsearch
1408+
// reopened it — with focus on the Dashboard tab, its hits tabbable ahead of
1409+
// the tab list, until a click elsewhere. Escape inside the debounce, and a
1410+
// Tab out while a request was in flight, did the same.
1411+
const t = gsearchHarness({ viewControl: true });
1412+
t.focusBox(t.elsewhere);
1413+
t.type('foo');
1414+
t.viewControl.focus();
1415+
t.fire(t.els.gsearch, 'focusout', { relatedTarget: t.viewControl }); // Tab: out of the search, before the debounce ran out
1416+
assert.equal(t.els.gsearchResults.hidden, true, 'control: leaving closed the results');
1417+
t.elapse();
1418+
assert.equal(t.asked.length, 1, 'control: the search still ran');
1419+
await t.answer(0, ['foo.zip']);
1420+
assert.equal(t.els.gsearchResults.hidden, true, 'its answer does not reopen the results around a person who has left');
1421+
assert.equal(t.els.gsearch.attrs['aria-expanded'], 'false', 'and the box does not say they are open');
1422+
1423+
// Escape inside the debounce: focus goes back where it came from, and the answer leaves it there.
1424+
const e = gsearchHarness({ viewControl: true });
1425+
e.focusBox(e.elsewhere);
1426+
e.type('foo');
1427+
e.escape();
1428+
e.elapse();
1429+
await e.answer(0, ['foo.zip']);
1430+
assert.equal(e.els.gsearchResults.hidden, true, 'Escape left the search, and the late answer does not reopen it');
1431+
assert.equal(e.doc.active, e.elsewhere, 'focus stays where Escape put it');
1432+
1433+
// Results showing, one more letter, Tab out while that search is in flight.
1434+
const f = gsearchHarness({ viewControl: true });
1435+
f.focusBox(f.elsewhere);
1436+
f.type('fo');
1437+
f.elapse();
1438+
await f.answer(0, ['fo.txt']);
1439+
assert.equal(f.els.gsearchResults.hidden, false, 'control: an answer for someone in the box opens the results');
1440+
f.type('foo');
1441+
f.elapse();
1442+
f.viewControl.focus();
1443+
f.fire(f.els.gsearch, 'focusout', { relatedTarget: f.viewControl });
1444+
await f.answer(1, ['foo.zip']);
1445+
assert.equal(f.els.gsearchResults.hidden, true, 'the answer in flight when focus left does not reopen the results');
1446+
// Back into the box: the results open on the answer to what the box says, not on the older one.
1447+
f.focusBox(f.viewControl);
1448+
assert.equal(f.els.gsearchResults.hidden, false, 'coming back opens the results');
1449+
assert.match(f.els.gsearchResults.innerHTML, /foo\.zip/, 'showing the answer to "foo"');
1450+
assert.doesNotMatch(f.els.gsearchResults.innerHTML, /fo\.txt/, 'not the one to "fo"');
1451+
1452+
// A search that fails after focus left says so the next time the box is entered, not around someone elsewhere.
1453+
const r = gsearchHarness({ viewControl: true });
1454+
r.focusBox(r.elsewhere);
1455+
r.type('foo');
1456+
r.escape();
1457+
r.elapse();
1458+
await r.refuse(0);
1459+
assert.equal(r.els.gsearchResults.hidden, true, 'a failed search answered after Escape does not reopen the results');
1460+
});
1461+
13831462
test('the keyboard shortcut works even when the sidebar is collapsed to the rail', () => {
13841463
// Focusing a `display: none` element does nothing at all, silently — the
13851464
// rail hides the input, so the shortcut must open the sidebar first.

0 commit comments

Comments
 (0)