diff --git a/design-system/components/tables.css b/design-system/components/tables.css index 9db1f77..ddc5a59 100644 --- a/design-system/components/tables.css +++ b/design-system/components/tables.css @@ -10,6 +10,17 @@ border: 1px solid var(--color-border); } +/* Clickable rows (; static/js/interactive.js + makes them focusable and opens them on Enter). A visible outline when + reached with the keyboard; the global :focus-visible box-shadow doesn't + render reliably on table rows, so use an inset outline instead. */ +tr.run-row:focus-visible { + outline: 2px solid var(--color-primary-500); + outline-offset: -2px; + box-shadow: none; + border-radius: 0; +} + .table { width: 100%; border-collapse: collapse; diff --git a/src/secfoo/web/static/design-system/components/tables.css b/src/secfoo/web/static/design-system/components/tables.css index 9db1f77..ddc5a59 100644 --- a/src/secfoo/web/static/design-system/components/tables.css +++ b/src/secfoo/web/static/design-system/components/tables.css @@ -10,6 +10,17 @@ border: 1px solid var(--color-border); } +/* Clickable rows (; static/js/interactive.js + makes them focusable and opens them on Enter). A visible outline when + reached with the keyboard; the global :focus-visible box-shadow doesn't + render reliably on table rows, so use an inset outline instead. */ +tr.run-row:focus-visible { + outline: 2px solid var(--color-primary-500); + outline-offset: -2px; + box-shadow: none; + border-radius: 0; +} + .table { width: 100%; border-collapse: collapse; diff --git a/src/secfoo/web/static/js/interactive.js b/src/secfoo/web/static/js/interactive.js index ceb7e8d..5cfcc37 100644 --- a/src/secfoo/web/static/js/interactive.js +++ b/src/secfoo/web/static/js/interactive.js @@ -48,3 +48,22 @@ document.addEventListener("click", function (event) { window.location.href = row.dataset.href; } }); + +// Keyboard access for clickable rows. A isn't focusable, so without +// this Tab skips every .run-row and keyboard-only users can't open them. +// Done here rather than with tabindex="0" in each template: the rows only +// work through this script, so they only become focusable when it runs, +// and any new .run-row is covered automatically. +document.querySelectorAll(".run-row[data-href]").forEach(function (row) { + if (!row.hasAttribute("tabindex")) row.tabIndex = 0; +}); + +document.addEventListener("keydown", function (event) { + if (event.key !== "Enter") return; + // Only when the row itself is focused -- Enter on a link or button + // inside the row keeps that element's own behavior. + const row = event.target; + if (row.matches && row.matches(".run-row[data-href]")) { + window.location.href = row.dataset.href; + } +}); diff --git a/tests/test_web/test_keyboard_access.py b/tests/test_web/test_keyboard_access.py new file mode 100644 index 0000000..291a3c3 --- /dev/null +++ b/tests/test_web/test_keyboard_access.py @@ -0,0 +1,34 @@ +"""Clickable table rows must be usable without a mouse. + +`` rows are opened by static/js/ +interactive.js. A isn't focusable by default, so the script gives +each such row tabindex=0 and opens it on Enter. These checks guard both +halves: every clickable row carries data-href (what the script looks +for), and the script still sets up focus and Enter handling. +""" + +from __future__ import annotations + +import re +from pathlib import Path + +WEB = Path(__file__).parents[2] / "src" / "secfoo" / "web" +_RUN_ROW_RE = re.compile(r"]*\bclass=\"[^\"]*\brun-row\b[^>]*>") + + +def test_every_clickable_row_has_a_data_href(): + rows = [ + (str(f), m.group(0)) + for f in (WEB / "templates").rglob("*.html") + for m in _RUN_ROW_RE.finditer(f.read_text()) + ] + assert rows, "expected at least one clickable run-row in the templates" + missing = [(f, tag) for f, tag in rows if "data-href=" not in tag] + assert not missing, f"run-row without data-href (not keyboard/clickable): {missing}" + + +def test_interactive_js_makes_rows_focusable_and_opens_them_on_enter(): + js = (WEB / "static" / "js" / "interactive.js").read_text() + assert '.run-row[data-href]' in js + assert "tabIndex = 0" in js + assert '"keydown"' in js and '"Enter"' in js