test(web): lock the sidebar highlighting behind tests
It has been reported twice and fixed twice by reading the code, which is how it came back. The decision is now a module of its own — the shell only asks it which entry is current — and twelve cases pin it down: a detail page, a query string, every spelling of the home route, and the original regression where home stayed lit everywhere. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
3 files changed
+151
-12
No files matched your search
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* Which navigation entry is the current one (M16-T11).
|
||||
*
|
||||
* Kept apart from the shell so it can be tested: getting this wrong leaves the
|
||||
* sidebar pointing at a page you are not on, which is the kind of bug that
|
||||
* looks like nothing and gets reported three times.
|
||||
*/
|
||||
|
||||
/**
|
||||
* First segment of a hash route, which is what a sidebar entry stands for.
|
||||
* `#/repos/FWS/Arch` and `#/repos` are the same entry; `#/releases?repo=x` too.
|
||||
*/
|
||||
export function activePath(hash = '') {
|
||||
const raw = String(hash).replace(/^#\/?/, '');
|
||||
const [path] = raw.split('?');
|
||||
const [first = ''] = path.split('/').filter(Boolean);
|
||||
return first;
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks the matching link inside `scope` and clears the others.
|
||||
* Returns the active path, so a caller can retitle the page from it.
|
||||
*/
|
||||
export function highlight(scope, hash = '') {
|
||||
const active = activePath(hash);
|
||||
|
||||
for (const link of scope.querySelectorAll('.nav a')) {
|
||||
// Compare the route a link carries, not its href: a trailing slash or a
|
||||
// query string must not make an entry look inactive.
|
||||
const current = (link.dataset.route ?? '') === active;
|
||||
link.classList.toggle('active', current);
|
||||
if (current) link.setAttribute('aria-current', 'page');
|
||||
else link.removeAttribute('aria-current');
|
||||
}
|
||||
return active;
|
||||
}
|
||||
Reference in new issue
Block a user