From ed4dd508644f577a324af3f7a0adbb8c81bfcc96 Mon Sep 17 00:00:00 2001 From: Eric Dunham Date: Mon, 1 Mar 2021 10:41:47 -0600 Subject: [PATCH] Add setting to allow for empty alt text in images --- RELEASE.md | 3 +++ docs/settings.rst | 4 ++++ pelican/readers.py | 2 +- pelican/settings.py | 1 + pelican/tests/content/article_with_images.html | 9 +++++++++ pelican/tests/test_cache.py | 2 +- pelican/tests/test_readers.py | 12 ++++++++++++ 7 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 RELEASE.md create mode 100644 pelican/tests/content/article_with_images.html diff --git a/RELEASE.md b/RELEASE.md new file mode 100644 index 00000000..f52fafd2 --- /dev/null +++ b/RELEASE.md @@ -0,0 +1,3 @@ +Release type: minor + +Add setting to allow for empty `alt` attributes in images; it defaults to `False`, which is the current behavior. Empty `alt` text can be indicative of an accessibility oversight, but can be intentional and desired, e.g. https://webaim.org/techniques/alttext/, https://www.w3.org/WAI/tutorials/images/decorative/. diff --git a/docs/settings.rst b/docs/settings.rst index c66c42a3..1d5e1b7f 100644 --- a/docs/settings.rst +++ b/docs/settings.rst @@ -364,6 +364,10 @@ Basic settings A list of metadata fields containing reST/Markdown content to be parsed and translated to HTML. +.. data:: IMAGES_ALLOW_EMPTY_ALT_TEXT = False + + If ``True``, warnings will not be emitted when empty ``alt`` attributes for images are found. + .. data:: PORT = 8000 The TCP port to serve content from the output folder via HTTP when pelican diff --git a/pelican/readers.py b/pelican/readers.py index 15d09908..b8cf44e2 100644 --- a/pelican/readers.py +++ b/pelican/readers.py @@ -574,7 +574,7 @@ class Readers(FileStampDataCacher): self.cache_data(path, (content, reader_metadata)) metadata.update(_filter_discardable_metadata(reader_metadata)) - if content: + if not self.settings['IMAGES_ALLOW_EMPTY_ALT_TEXT'] and content: # find images with empty alt find_empty_alt(content, path) diff --git a/pelican/settings.py b/pelican/settings.py index ea3ee8eb..9dd3da35 100644 --- a/pelican/settings.py +++ b/pelican/settings.py @@ -164,6 +164,7 @@ DEFAULT_CONFIG = { 'FORMATTED_FIELDS': ['summary'], 'PORT': 8000, 'BIND': '127.0.0.1', + 'IMAGES_ALLOW_EMPTY_ALT_TEXT': False } PYGMENTS_RST_OPTIONS = None diff --git a/pelican/tests/content/article_with_images.html b/pelican/tests/content/article_with_images.html new file mode 100644 index 00000000..4e2af9e7 --- /dev/null +++ b/pelican/tests/content/article_with_images.html @@ -0,0 +1,9 @@ + + + + + Images + + + + diff --git a/pelican/tests/test_cache.py b/pelican/tests/test_cache.py index 564f1d31..76e228c2 100644 --- a/pelican/tests/test_cache.py +++ b/pelican/tests/test_cache.py @@ -153,7 +153,7 @@ class TestCache(unittest.TestCase): - empty.md - empty_with_bom.md """ - self.assertEqual(generator.readers.read_file.call_count, 6) + self.assertEqual(generator.readers.read_file.call_count, 7) def test_article_reader_content_caching(self): """Test raw article content caching at the reader level""" diff --git a/pelican/tests/test_readers.py b/pelican/tests/test_readers.py index ea5f3bdd..51af582c 100644 --- a/pelican/tests/test_readers.py +++ b/pelican/tests/test_readers.py @@ -134,6 +134,18 @@ class DefaultReaderTest(ReaderTest): 'Other images have empty alt attributes'} ) + @patch('pelican.readers.logger') + def test_read_file_with_images_allow_empty_alt_text_false(self, log_mock): + test_file = 'article_with_images.html' + self.read_file(path=test_file, IMAGES_ALLOW_EMPTY_ALT_TEXT=False) + assert 2 == log_mock.warning.call_count + + @patch('pelican.readers.logger') + def test_read_file_with_images_allow_empty_alt_text_true(self, log_mock): + test_file = 'article_with_images.html' + self.read_file(path=test_file, IMAGES_ALLOW_EMPTY_ALT_TEXT=True) + log_mock.warning.assert_not_called() + class RstReaderTest(ReaderTest):