Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show a warning if an image has no alternative text #2024

Merged
merged 4 commits into from
Jul 19, 2023
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
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