fix: make active + ancestor links accessible (#1915)
This commit is contained in:
@@ -37,8 +37,10 @@ export function setActiveUrl(vnode: VNode, pathname: string): void {
|
|||||||
const match = matchesUrl(pathname, hrefProp);
|
const match = matchesUrl(pathname, hrefProp);
|
||||||
if (match === UrlMatchKind.Current) {
|
if (match === UrlMatchKind.Current) {
|
||||||
props[DATA_CURRENT] = "true";
|
props[DATA_CURRENT] = "true";
|
||||||
|
props["aria-current"] = "page";
|
||||||
} else if (match === UrlMatchKind.Ancestor) {
|
} else if (match === UrlMatchKind.Ancestor) {
|
||||||
props[DATA_ANCESTOR] = "true";
|
props[DATA_ANCESTOR] = "true";
|
||||||
|
props["aria-current"] = "true";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -568,13 +568,16 @@ function updateLinks(url: URL) {
|
|||||||
|
|
||||||
if (match === UrlMatchKind.Current) {
|
if (match === UrlMatchKind.Current) {
|
||||||
link.setAttribute(DATA_CURRENT, "true");
|
link.setAttribute(DATA_CURRENT, "true");
|
||||||
|
link.setAttribute("aria-current", "page");
|
||||||
link.removeAttribute(DATA_ANCESTOR);
|
link.removeAttribute(DATA_ANCESTOR);
|
||||||
} else if (match === UrlMatchKind.Ancestor) {
|
} else if (match === UrlMatchKind.Ancestor) {
|
||||||
link.setAttribute(DATA_ANCESTOR, "true");
|
link.setAttribute(DATA_ANCESTOR, "true");
|
||||||
|
link.setAttribute("aria-current", "true");
|
||||||
link.removeAttribute(DATA_CURRENT);
|
link.removeAttribute(DATA_CURRENT);
|
||||||
} else {
|
} else {
|
||||||
link.removeAttribute(DATA_CURRENT);
|
link.removeAttribute(DATA_CURRENT);
|
||||||
link.removeAttribute(DATA_ANCESTOR);
|
link.removeAttribute(DATA_ANCESTOR);
|
||||||
|
link.removeAttribute("aria-current");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -976,15 +976,21 @@ Deno.test("active links without client nav", async () => {
|
|||||||
// Current
|
// Current
|
||||||
assertNotSelector(doc, "a[href='/active_nav'][data-ancestor]");
|
assertNotSelector(doc, "a[href='/active_nav'][data-ancestor]");
|
||||||
assertSelector(doc, "a[href='/active_nav'][data-current]");
|
assertSelector(doc, "a[href='/active_nav'][data-current]");
|
||||||
|
assertSelector(doc, `a[href='/active_nav'][aria-current="page"]`);
|
||||||
|
|
||||||
// Unrelated links
|
// Unrelated links
|
||||||
assertNotSelector(doc, "a[href='/active_nav/foo'][data-ancestor]");
|
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'][data-ancestor]");
|
||||||
|
assertNotSelector(doc, "a[href='/active_nav/foo/bar'][aria-current]");
|
||||||
|
|
||||||
doc = await server.getHtml(`/active_nav/foo`);
|
doc = await server.getHtml(`/active_nav/foo`);
|
||||||
assertSelector(doc, "a[href='/active_nav/foo'][data-current]");
|
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'][data-ancestor]");
|
||||||
|
assertSelector(doc, `a[href='/active_nav'][aria-current="true"]`);
|
||||||
assertSelector(doc, "a[href='/'][data-ancestor]");
|
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
|
// Current
|
||||||
assertNotSelector(doc, "a[href='/active_nav_partial'][data-ancestor]");
|
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'][data-current]");
|
||||||
|
assertSelector(doc, `a[href='/active_nav_partial'][aria-current="page"]`);
|
||||||
|
|
||||||
// Unrelated links
|
// Unrelated links
|
||||||
assertNotSelector(
|
assertNotSelector(
|
||||||
doc,
|
doc,
|
||||||
"a[href='/active_nav_partial/foo'][data-ancestor]",
|
"a[href='/active_nav_partial/foo'][data-ancestor]",
|
||||||
);
|
);
|
||||||
|
assertNotSelector(
|
||||||
|
doc,
|
||||||
|
"a[href='/active_nav_partial/foo'][aria-current]",
|
||||||
|
);
|
||||||
assertNotSelector(
|
assertNotSelector(
|
||||||
doc,
|
doc,
|
||||||
"a[href='/active_nav_partial/foo/bar'][data-ancestor]",
|
"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`);
|
await page.goto(`${address}/active_nav_partial/foo`);
|
||||||
doc = parseHtml(await page.content());
|
doc = parseHtml(await page.content());
|
||||||
assertSelector(doc, "a[href='/active_nav_partial/foo'][data-current]");
|
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]");
|
||||||
|
assertSelector(
|
||||||
|
doc,
|
||||||
|
`a[href='/active_nav_partial'][data-ancestor][aria-current="true"]`,
|
||||||
|
);
|
||||||
assertSelector(doc, "a[href='/'][data-ancestor]");
|
assertSelector(doc, "a[href='/'][data-ancestor]");
|
||||||
|
assertSelector(doc, `a[href='/'][aria-current="true"]`);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user