Skip to content

Commit

Permalink
Add empty image alt tag if alt text not specified (#2323)
Browse files Browse the repository at this point in the history
Middleman's image_tag helper wraps the Padrino image_tag helper.
By default, the Padrino image_tag helper adds an alt tag based on the
image filename when one isn't explicitly set.

Alt text based on the filename is not helpful to users and therefore
bad for accessibility.

To avoid this, explicitly set an empty alt tag value before calling
Padrino's image_tag if the user hasn't specified a value.

Co-Authored-By: vixus0 <vixus0@gmail.com>

Co-authored-by: vixus0 <vixus0@gmail.com>
  • Loading branch information
bravegrape and vixus0 committed May 19, 2020
1 parent e8cf86f commit 5d3b695
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 2 deletions.
11 changes: 11 additions & 0 deletions middleman-core/features/default_alt_tag.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Feature: Add an empty alt tag for images that don't have alt tags specified

Scenario: Image does not have alt tag specified
Given the Server is running at "default-alt-tags-app"
When I go to "/empty-alt-tag.html"
Then I should see 'alt=""'

Scenario: Image has alt tag specified
Given the Server is running at "default-alt-tags-app"
When I go to "/meaningful-alt-tag.html"
Then I should see 'alt="Meaningful alt text"'
2 changes: 1 addition & 1 deletion middleman-core/features/relative_assets.feature
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ Feature: Relative Assets
"""
And the Server is running at "relative-assets-app"
When I go to "/sub/image_tag.html"
Then I should see '<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" />'
Then I should see '<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" alt="" />'

Scenario: URLs are not rewritten for rewrite ignored paths
Given a fixture app "relative-assets-app"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Feature: Relative Assets (Helpers Only)
"""
And the Server is running at "relative-assets-app"
When I go to "/sub/image_tag.html"
Then I should see '<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" />'
Then I should see '<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" alt="" />'

@wip
Scenario: URLs are not rewritten for rewrite ignored paths
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= image_tag "blank.gif" %>
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= image_tag "blank.gif", alt: "Meaningful alt text" %>
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ def image_tag(path, params={})
params[:srcset] = images_sources.join(', ')
end

params[:alt] ||= ''

super(path, params)
end

Expand Down

0 comments on commit 5d3b695

Please sign in to comment.