Skip to content

Commit

Permalink
Show a warning if an image has no alternative text (#2024)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
brichet and pre-commit-ci[bot] authored Jul 19, 2023
1 parent fa8223c commit 1cbb0a4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
8 changes: 6 additions & 2 deletions nbconvert/exporters/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def default_config(self):
@validate("language_code")
def _valid_language_code(self, proposal):
if self.language_code not in iso639_1:
self.log.warn(
self.log.warning(
f'"{self.language_code}" is not an ISO 639-1 language code. '
'It has been replaced by the default value "en".'
)
Expand Down Expand Up @@ -259,8 +259,12 @@ def from_notebook_node( # type:ignore
html, resources = super().from_notebook_node(nb, resources, **kw)
soup = BeautifulSoup(html, features="html.parser")
# Add image's alternative text
missing_alt = 0
for elem in soup.select("img:not([alt])"):
elem.attrs["alt"] = "Image"
elem.attrs["alt"] = "No description has been provided for this image"
missing_alt += 1
if missing_alt:
self.log.warning(f"Alternative text is missing on {missing_alt} image(s).")
# Set input and output focusable
for elem in soup.select(".jp-Notebook div.jp-Cell-inputWrapper"):
elem.attrs["tabindex"] = "0"
Expand Down
33 changes: 30 additions & 3 deletions nbconvert/exporters/tests/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,17 @@ def test_png_metadata(self):
"""
Does HTMLExporter with the 'classic' template treat pngs with width/height metadata correctly?
"""
(output, resources) = HTMLExporter(template_name="classic").from_filename(
self._get_notebook(nb_name="pngmetadata.ipynb")
exporter = HTMLExporter(template_name="classic")
with self.assertLogs(exporter.log, level="WARN") as log:
(output, resources) = exporter.from_filename(
self._get_notebook(nb_name="pngmetadata.ipynb")
)
assert len(log.output) == 1
assert "Alternative text is missing on 1 image(s)." in log.output[0]

check_for_png = re.compile(
r'<img alt="No description has been provided for this image"([^>]*?)>'
)
check_for_png = re.compile(r'<img alt="Image"([^>]*?)>')
result = check_for_png.search(output)
assert result
attr_string = result.group(1)
Expand Down Expand Up @@ -198,3 +205,23 @@ def test_javascript_injection(self):
assert "<script>alert('text/markdown output')</script>" not in output
assert "<script>alert('text/html output')</script>" not in output
assert "alert('application/javascript output')" not in output

def test_language_code_not_set(self):
(output, resources) = HTMLExporter(template_name="classic").from_filename(
self._get_notebook()
)
assert '<html lang="en">' in output

def test_set_language_code(self):
exporter = HTMLExporter(template_name="classic", language_code="fr")
(output, resources) = exporter.from_filename(self._get_notebook())
assert '<html lang="fr">' in output

def test_language_code_error(self):
with self.assertLogs(level="WARN") as log:
exporter = HTMLExporter(template_name="classic", language_code="zz")
assert len(log.output) == 1
assert '"zz" is not an ISO 639-1 language code.' in log.output[0]
(output, resources) = exporter.from_filename(self._get_notebook())

assert '<html lang="en">' in output

0 comments on commit 1cbb0a4

Please sign in to comment.