From 78b0b05fc3786ef440edf95f3e605579eda453f5 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Wed, 11 Oct 2023 17:29:07 +0200 Subject: [PATCH] fix: make active + ancestor links accessible (#1915) --- src/runtime/active_url.ts | 2 ++ src/runtime/entrypoints/main.ts | 3 +++ tests/partials_test.ts | 24 ++++++++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/src/runtime/active_url.ts b/src/runtime/active_url.ts index 60f4418a..5534eed9 100644 --- a/src/runtime/active_url.ts +++ b/src/runtime/active_url.ts @@ -37,8 +37,10 @@ export function setActiveUrl(vnode: VNode, pathname: string): void { const match = matchesUrl(pathname, hrefProp); if (match === UrlMatchKind.Current) { props[DATA_CURRENT] = "true"; + props["aria-current"] = "page"; } else if (match === UrlMatchKind.Ancestor) { props[DATA_ANCESTOR] = "true"; + props["aria-current"] = "true"; } } } diff --git a/src/runtime/entrypoints/main.ts b/src/runtime/entrypoints/main.ts index baa047d8..14d21326 100644 --- a/src/runtime/entrypoints/main.ts +++ b/src/runtime/entrypoints/main.ts @@ -568,13 +568,16 @@ function updateLinks(url: URL) { if (match === UrlMatchKind.Current) { link.setAttribute(DATA_CURRENT, "true"); + link.setAttribute("aria-current", "page"); link.removeAttribute(DATA_ANCESTOR); } else if (match === UrlMatchKind.Ancestor) { link.setAttribute(DATA_ANCESTOR, "true"); + link.setAttribute("aria-current", "true"); link.removeAttribute(DATA_CURRENT); } else { link.removeAttribute(DATA_CURRENT); link.removeAttribute(DATA_ANCESTOR); + link.removeAttribute("aria-current"); } }); } diff --git a/tests/partials_test.ts b/tests/partials_test.ts index c0e0f14c..01b433b3 100644 --- a/tests/partials_test.ts +++ b/tests/partials_test.ts @@ -976,15 +976,21 @@ Deno.test("active links without client nav", async () => { // Current assertNotSelector(doc, "a[href='/active_nav'][data-ancestor]"); assertSelector(doc, "a[href='/active_nav'][data-current]"); + assertSelector(doc, `a[href='/active_nav'][aria-current="page"]`); // Unrelated links assertNotSelector(doc, "a[href='/active_nav/foo'][data-ancestor]"); + assertNotSelector(doc, "a[href='/active_nav/foo'][aria-current]"); assertNotSelector(doc, "a[href='/active_nav/foo/bar'][data-ancestor]"); + assertNotSelector(doc, "a[href='/active_nav/foo/bar'][aria-current]"); doc = await server.getHtml(`/active_nav/foo`); assertSelector(doc, "a[href='/active_nav/foo'][data-current]"); + assertSelector(doc, `a[href='/active_nav/foo'][aria-current="page"]`); assertSelector(doc, "a[href='/active_nav'][data-ancestor]"); + assertSelector(doc, `a[href='/active_nav'][aria-current="true"]`); assertSelector(doc, "a[href='/'][data-ancestor]"); + assertSelector(doc, `a[href='/'][aria-current="true"]`); }, ); }); @@ -1001,22 +1007,40 @@ Deno.test("Updates active links outside of vdom", async () => { // Current assertNotSelector(doc, "a[href='/active_nav_partial'][data-ancestor]"); assertSelector(doc, "a[href='/active_nav_partial'][data-current]"); + assertSelector(doc, `a[href='/active_nav_partial'][aria-current="page"]`); // Unrelated links assertNotSelector( doc, "a[href='/active_nav_partial/foo'][data-ancestor]", ); + assertNotSelector( + doc, + "a[href='/active_nav_partial/foo'][aria-current]", + ); assertNotSelector( doc, "a[href='/active_nav_partial/foo/bar'][data-ancestor]", ); + assertNotSelector( + doc, + "a[href='/active_nav_partial/foo/bar'][aria-current]", + ); await page.goto(`${address}/active_nav_partial/foo`); doc = parseHtml(await page.content()); assertSelector(doc, "a[href='/active_nav_partial/foo'][data-current]"); + assertSelector( + doc, + `a[href='/active_nav_partial/foo'][aria-current="page"]`, + ); assertSelector(doc, "a[href='/active_nav_partial'][data-ancestor]"); + assertSelector( + doc, + `a[href='/active_nav_partial'][data-ancestor][aria-current="true"]`, + ); assertSelector(doc, "a[href='/'][data-ancestor]"); + assertSelector(doc, `a[href='/'][aria-current="true"]`); }, ); });