Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions design-system/components/tables.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@
border: 1px solid var(--color-border);
}

/* Clickable rows (<tr class="run-row" data-href>; 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;
Expand Down
11 changes: 11 additions & 0 deletions src/secfoo/web/static/design-system/components/tables.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@
border: 1px solid var(--color-border);
}

/* Clickable rows (<tr class="run-row" data-href>; 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;
Expand Down
19 changes: 19 additions & 0 deletions src/secfoo/web/static/js/interactive.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,22 @@ document.addEventListener("click", function (event) {
window.location.href = row.dataset.href;
}
});

// Keyboard access for clickable rows. A <tr> 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;
}
});
34 changes: 34 additions & 0 deletions tests/test_web/test_keyboard_access.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""Clickable table rows must be usable without a mouse.

`<tr class="run-row" data-href=...>` rows are opened by static/js/
interactive.js. A <tr> 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"<tr\b[^>]*\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
Loading