Add setting to allow for empty alt text in images

This commit is contained in:
Eric Dunham 2021-03-01 10:41:47 -06:00
commit ed4dd50864
7 changed files with 31 additions and 2 deletions

3
RELEASE.md Normal file
View file

@ -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/.

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -0,0 +1,9 @@
<html>
<head>
</head>
<body>
Images
<img alt="" src="test-image.png" width="300px" />
<img src="test-image.png" width="300px" alt="" />
</body>
</html>

View file

@ -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"""

View file

@ -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):