From 7e4192870f10c72542e13966ec6db007cdc0ce79 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Thu, 19 Nov 2026 17:34:46 +0100 Subject: [PATCH] fix: draw the search shortcut hint from CSS The search button is named "Search" and showed "Ctrl K" as text in its content, so Lighthouse flagged label-content-name-mismatch. The keys are drawn from data-key with ::before, and aria-keyshortcuts still announces the shortcut. --- tests/test_site_components.py | 17 +++++++++++++++++ website/src/components/Search.astro | 14 ++++++++++---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/tests/test_site_components.py b/tests/test_site_components.py index 28c39a411..da52e0dde 100644 --- a/tests/test_site_components.py +++ b/tests/test_site_components.py @@ -984,6 +984,23 @@ class SearchLoadsOnFirstUse(unittest.TestCase): self.assertNotIn("TODO", search) +class SearchButtonShowsOnlyItsName(unittest.TestCase): + """Lighthouse flagged label-content-name-mismatch: the button is named + "Search" and showed "Ctrl K" as text. The hint is drawn from data-key, + the shortcut is announced by aria-keyshortcuts.""" + + def test_no_text_in_the_button_but_its_name(self): + search = _read(COMPONENTS / "Search.astro") + start = search.index("", start)] + self.assertIn("aria-label={Astro.locals.t('search.label')}", button) + self.assertIn('aria-keyshortcuts="Control+K"', button) + text = re.sub(r"<[^>]*>", "", button[button.index(">", button.index("aria-keyshortcuts")) + 1:]) + self.assertEqual(text.split(), ["{Astro.locals.t('search.label')}"]) + self.assertIn("kbd[data-key]::before", search) + self.assertIn("platformKey.dataset.key = '⌘';", search) + + class LogosAtTheirWeight(unittest.TestCase): """2.7: two logos were Inkscape files, one with a 441 x 334 PNG inside.""" diff --git a/website/src/components/Search.astro b/website/src/components/Search.astro index 0a350f64f..056b347ff 100644 --- a/website/src/components/Search.astro +++ b/website/src/components/Search.astro @@ -1,9 +1,12 @@ --- // Starlight's search (@astrojs/starlight 0.42.6, components/Search.astro), -// with one change: the Pagefind UI (ui-core.js, 26 KiB) loads when the +// with two changes. The Pagefind UI (ui-core.js, 26 KiB) loads when the // reader reaches for search, a pointer on the button, focus on it, a tap, // Ctrl+K, and not on idle on every page. It was a third of the home page's -// weight. The markup and the styles are Starlight's own. +// weight. The Ctrl K hint is drawn from data-key, not written as text: the +// button is named "Search", the shortcut is in aria-keyshortcuts, and a +// visible "Ctrl K" in its content made the name differ from its label +// (WCAG 2.5.3). The rest of the markup and the styles are Starlight's own. import { Icon } from '@astrojs/starlight/components'; import project from 'virtual:starlight/project-context'; @@ -30,7 +33,7 @@ if (project.trailingSlash === 'never') dataAttributes['data-strip-trailing-slash @@ -68,7 +71,7 @@ if (project.trailingSlash === 'never') dataAttributes['data-strip-trailing-slash if (!openBtn || !(shortcut instanceof HTMLElement)) return; const platformKey = shortcut.querySelector('kbd'); if (platformKey && /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform)) { - platformKey.textContent = '⌘'; + platformKey.dataset.key = '⌘'; openBtn.setAttribute('aria-keyshortcuts', 'Meta+K'); } shortcut.style.display = ''; @@ -221,6 +224,9 @@ if (project.trailingSlash === 'never') dataAttributes['data-strip-trailing-slash kbd { font-family: var(--__sl-font); } + kbd[data-key]::before { + content: attr(data-key); + } dialog { margin: 0;