Skip to content

Commit 525e52d

Browse files
gabalafoutrallard
andauthored
Ensure code blocks (<pre>) are keyboard focusable (#1636)
* Ensure code blocks (<pre>) are keyboard focusable * Also cover <pre> tags not covered by starttag * Update src/pydata_sphinx_theme/translator.py Co-authored-by: gabalafou <[email protected]> --------- Co-authored-by: Tania Allard <[email protected]>
1 parent 6866546 commit 525e52d

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/pydata_sphinx_theme/translator.py

+26-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import types
44

55
import sphinx
6+
from docutils import nodes
67
from packaging.version import Version
78
from sphinx.application import Sphinx
89
from sphinx.ext.autosummary import autosummary_table
@@ -23,11 +24,35 @@ def __init__(self, *args, **kwds):
2324
self.settings.table_style = "table"
2425

2526
def starttag(self, *args, **kwargs):
26-
"""Ensure an aria-level is set for any heading role."""
27+
"""Perform small modifications to tags.
28+
29+
- ensure aria-level is set for any tag with heading role
30+
- ensure <pre> tags have tabindex="0".
31+
"""
2732
if kwargs.get("ROLE") == "heading" and "ARIA-LEVEL" not in kwargs:
2833
kwargs["ARIA-LEVEL"] = "2"
34+
35+
if "pre" in args:
36+
kwargs["tabindex"] = "0"
37+
2938
return super().starttag(*args, **kwargs)
3039

40+
def visit_literal_block(self, node):
41+
"""Modify literal blocks.
42+
43+
- add tabindex="0" to <pre> tags within the HTML tree of the literal
44+
block
45+
"""
46+
try:
47+
super().visit_literal_block(node)
48+
except nodes.SkipNode:
49+
# If the super method raises nodes.SkipNode, then we know it
50+
# executed successfully and appended to self.body a string of HTML
51+
# representing the code block, which we then modify.
52+
html_string = self.body[-1]
53+
self.body[-1] = html_string.replace("<pre", '<pre tabindex="0"')
54+
raise nodes.SkipNode
55+
3156
def visit_table(self, node):
3257
"""Custom visit table method.
3358

0 commit comments

Comments
 (0)