Skip to content

Commit a66f1e3

Browse files
committed
Fix nbsphinx color outputs, suppress other notebook output Axe errors
1 parent 11e0411 commit a66f1e3

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js

+3
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,9 @@ function addTabStopsToScrollableElements() {
769769

770770
// On page load (when this function gets called)
771771
updateTabStops();
772+
773+
// Give tests a way to wait for this script to finish adding tab stops
774+
document.body.dataset.pstTestTabStops = "added";
772775
}
773776
function debounce(callback, wait) {
774777
let timeoutId = null;

src/pydata_sphinx_theme/assets/styles/extensions/_notebooks.scss

+22
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,28 @@ div.nblast.container {
5252
margin-bottom: 1rem;
5353
}
5454

55+
// Notebook cell input line number
56+
.nbinput.container .prompt pre {
57+
html[data-theme="light"] & {
58+
color: #005b82;
59+
}
60+
61+
html[data-theme="dark"] & {
62+
color: #00e0e0;
63+
}
64+
}
65+
66+
// Notebook cell output line number
67+
.nboutput.container .prompt pre {
68+
html[data-theme="light"] & {
69+
color: #a12236;
70+
}
71+
72+
html[data-theme="dark"] & {
73+
color: #ffa07a;
74+
}
75+
}
76+
5577
/*******************************************************************************
5678
* myst NB
5779
*/

tests/test_a11y.py

+37-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ def filter_ignored_violations(violations, url_pathname):
3838
]:
3939
filtered = []
4040
for violation in violations:
41-
# TODO: eventually fix this rule violation. See
42-
# https://github.com/pydata/pydata-sphinx-theme/issues/1479.
41+
# TODO: remove this exclusion once the following update to Axe is
42+
# released and we upgrade:
43+
# https://github.com/dequelabs/axe-core/pull/4469
4344
if violation["id"] == "landmark-unique":
4445
# Ignore landmark-unique only for .sidebar targets. Don't ignore
4546
# it for other targets because then the test might fail to catch
@@ -167,6 +168,11 @@ def test_axe_core(
167168
# Wait for CSS transitions (Bootstrap's transitions are 300 ms)
168169
page.wait_for_timeout(301)
169170

171+
# Wait for JavaScript to add tabindex=0 to scrollable regions (code blocks,
172+
# tables, etc.), otherwise the page will fail
173+
# [scrollable-region-focusable](https://dequeuniversity.com/rules/axe/4.9/scrollable-region-focusable)
174+
page.wait_for_function("() => document.body.dataset.pstTestTabStops === 'added'")
175+
170176
# Inject the Axe-core JavaScript library into the page
171177
page.add_script_tag(path="node_modules/axe-core/axe.min.js")
172178

@@ -176,6 +182,35 @@ def test_axe_core(
176182

177183
# Check found violations against known violations that we do not plan to fix
178184
filtered_violations = filter_ignored_violations(results["violations"], url_pathname)
185+
186+
# We expect notebook outputs on the PyData Library Styles page to have color
187+
# contrast failures.
188+
if url_pathname == "/examples/pydata.html":
189+
# All violations should be color contrast violations
190+
for violation in filtered_violations:
191+
assert (
192+
violation["id"] == "color-contrast"
193+
), f"Found {violation['id']} violation (expected color-contrast): {format_violations([violation])}"
194+
195+
# Now check that when we exclude notebook outputs, the page has no violations
196+
197+
results_sans_nbout = page.evaluate(
198+
f"axe.run({{ include: '{selector}', exclude: '.nboutput > .output_area' }})"
199+
)
200+
violations_sans_nbout = filter_ignored_violations(
201+
results_sans_nbout["violations"], url_pathname
202+
)
203+
204+
# No violations on page when excluding notebook outputs
205+
assert len(violations_sans_nbout) == 0, format_violations(violations_sans_nbout)
206+
207+
# TODO: for color contrast issues with common notebook outputs
208+
# (ipywidget tabbed panels, Xarray, etc.), should we override
209+
# third-party CSS with our own CSS or/and work with NbSphinx, MyST-NB,
210+
# ipywidgets, and other third parties to use higher contrast colors in
211+
# their CSS?
212+
pytest.xfail("notebook outputs have color contrast violations")
213+
179214
assert len(filtered_violations) == 0, format_violations(filtered_violations)
180215

181216

0 commit comments

Comments
 (0)