fix: active links consider query params and respect existing aria-current (#3755)
## Summary - `matchesUrl()` now considers query parameters — links with query params that don't match the current URL get `Ancestor` instead of `Current` status - Links with a user-set `aria-current` attribute are left untouched (both server and client side), fixing daisyUI tab components and similar use cases - Client-side `updateLinks()` passes search params and respects user-set attributes Closes #3474 --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
895bcacdae
commit
fc0ff66b80
@@ -83,6 +83,86 @@ Deno.test({
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "active links - query params differentiate current vs ancestor",
|
||||
fn: async () => {
|
||||
function TabView() {
|
||||
return (
|
||||
<Doc>
|
||||
<div>
|
||||
<a href="/tabs?sort=name">Sort by name</a>
|
||||
<a href="/tabs?sort=price">Sort by price</a>
|
||||
<a href="/tabs">All</a>
|
||||
</div>
|
||||
</Doc>
|
||||
);
|
||||
}
|
||||
|
||||
const app = testApp()
|
||||
.get("/tabs", (ctx) => {
|
||||
return ctx.render(<TabView />);
|
||||
});
|
||||
|
||||
const server = new FakeServer(app.handler());
|
||||
|
||||
// When visiting /tabs?sort=name, only that link is "current"
|
||||
let res = await server.get("/tabs?sort=name");
|
||||
let doc = parseHtml(await res.text());
|
||||
|
||||
// Exact match including query params
|
||||
assertSelector(doc, `a[href='/tabs?sort=name'][data-current]`);
|
||||
assertSelector(doc, `a[href='/tabs?sort=name'][aria-current="page"]`);
|
||||
|
||||
// Different query params → ancestor, not current
|
||||
assertNotSelector(doc, `a[href='/tabs?sort=price'][data-current]`);
|
||||
assertSelector(doc, `a[href='/tabs?sort=price'][data-ancestor]`);
|
||||
|
||||
// Link without query params matches regardless
|
||||
assertSelector(doc, `a[href='/tabs'][data-current]`);
|
||||
|
||||
// When visiting /tabs (no query), all links on same path are current
|
||||
res = await server.get("/tabs");
|
||||
doc = parseHtml(await res.text());
|
||||
assertSelector(doc, `a[href='/tabs'][data-current]`);
|
||||
// Links with query params → ancestor since current URL has no query
|
||||
assertSelector(doc, `a[href='/tabs?sort=name'][data-ancestor]`);
|
||||
assertSelector(doc, `a[href='/tabs?sort=price'][data-ancestor]`);
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "active links - respects user-set aria-current",
|
||||
fn: async () => {
|
||||
function View() {
|
||||
return (
|
||||
<Doc>
|
||||
<div>
|
||||
<a href="/custom_aria" aria-current="step">Step link</a>
|
||||
<a href="/custom_aria">Auto link</a>
|
||||
</div>
|
||||
</Doc>
|
||||
);
|
||||
}
|
||||
|
||||
const app = testApp()
|
||||
.get("/custom_aria", (ctx) => {
|
||||
return ctx.render(<View />);
|
||||
});
|
||||
|
||||
const server = new FakeServer(app.handler());
|
||||
const res = await server.get("/custom_aria");
|
||||
const doc = parseHtml(await res.text());
|
||||
|
||||
// User-set aria-current should be preserved, not overwritten
|
||||
assertSelector(doc, `a[aria-current="step"]`);
|
||||
assertNotSelector(doc, `a[aria-current="step"][data-current]`);
|
||||
|
||||
// The other link (no user-set aria-current) should get Fresh's attributes
|
||||
assertSelector(doc, `a[href='/custom_aria'][data-current]`);
|
||||
assertSelector(doc, `a[href='/custom_aria'][aria-current="page"]`);
|
||||
},
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "active links - updates outside of vdom",
|
||||
fn: async () => {
|
||||
|
||||
Reference in New Issue
Block a user