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

Add --ignore-empty-alt #207

Merged
merged 7 commits into from
Apr 21, 2015
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ The `HTML::Proofer` constructor takes an optional hash of additional options:
| Option | Description | Default |
| :----- | :---------- | :------ |
| `alt_ignore` | An array of Strings or RegExps containing `img`s whose missing `alt` tags are safe to ignore. | `[]` |
| `empty_alt_ignore` | If `true`, ignores images with empty alt tags. | `false` |
| `check_external_hash` | Checks whether external hashes exist (even if the website exists). This slows the checker down. | `false` |
|`checks_to_ignore`| An array of Strings indicating which checks you'd like to not perform. | `[]`
| `directory_index_file` | Sets the file to look for when a link refers to a directory. | `index.html` |
Expand Down
1 change: 1 addition & 0 deletions bin/htmlproof
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Mercenary.program(:htmlproof) do |p|

p.option 'as_links', '--as-links', 'Assumes that `PATH` is a comma-separated array of links to check.'
p.option 'alt_ignore', '--alt-ignore image1,[image2,...]', Array, 'Comma-separated list of Strings or RegExps containing `img`s whose missing `alt` tags are safe to ignore'
p.option 'empty_alt_ignore', '--empty-alt-ignore', 'Ignores images with empty alt tags.'
p.option 'checks_to_ignore', '--checks-to-ignore check1,[check2,...]', Array, ' An array of Strings indicating which checks you\'d like to not perform.'
p.option 'check_external_hash', '--check-external-hash', 'Checks whether external hashes exist (even if the website exists). This slows the checker down (default: `false`).'
p.option 'directory_index_file', '--directory-index-file', String, 'Sets the file to look for when a link refers to a directory. (default: `index.html`)'
Expand Down
1 change: 1 addition & 0 deletions lib/html/proofer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def initialize(src, opts = {})
:file_ignore => [],
:check_external_hash => false,
:alt_ignore => [],
:empty_alt_ignore => false,
:disable_external => false,
:verbose => false,
:only_4xx => false,
Expand Down
3 changes: 2 additions & 1 deletion lib/html/proofer/check_runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Proofer
class CheckRunner

attr_reader :issues, :src, :path, :options, :typhoeus_opts, :hydra_opts, :parallel_opts, \
:external_urls, :href_ignores, :alt_ignores
:external_urls, :href_ignores, :alt_ignores, :empty_alt_ignore

def initialize(src, path, html, options, typhoeus_opts, hydra_opts, parallel_opts)
@src = src
Expand All @@ -19,6 +19,7 @@ def initialize(src, path, html, options, typhoeus_opts, hydra_opts, parallel_opt
@issues = []
@href_ignores = @options[:href_ignore]
@alt_ignores = @options[:alt_ignore]
@empty_alt_ignore = @options[:empty_alt_ignore]
@external_urls = {}
end

Expand Down
4 changes: 4 additions & 0 deletions lib/html/proofer/checkable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ def ignore?
end
end

def ignore_empty_alt?
@check.empty_alt_ignore
end

# path is external to the file
def external?
!internal?
Expand Down
13 changes: 9 additions & 4 deletions lib/html/proofer/checks/images.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ class ImageCheckable < ::HTML::Proofer::Checkable

SCREEN_SHOT_REGEX = /Screen(?: |%20)Shot(?: |%20)\d+-\d+-\d+(?: |%20)at(?: |%20)\d+.\d+.\d+/

def valid_alt_tag?
@alt && !@alt.strip.empty?
def alt
@alt
end

def empty_alt_tag?
alt.strip.empty?
end

def terrible_filename?
Expand Down Expand Up @@ -43,8 +47,9 @@ def run
end
end

# check alt tag
add_issue("image #{img.src} does not have an alt attribute", i.line) unless img.valid_alt_tag?
if img.alt.nil? || (img.empty_alt_tag? && !img.ignore_empty_alt?)
add_issue("image #{img.src} does not have an alt attribute", i.line)
end
end

external_urls
Expand Down
6 changes: 6 additions & 0 deletions spec/html/proofer/command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,10 @@
output = make_bin('--check-html', broken)
expect(output).to match('1 failure')
end

it 'works with empty-alt-ignore' do
broken = "#{FIXTURES_DIR}/html/emptyImageAltText.html"
output = make_bin('--empty-alt-ignore', broken)
expect(output).to match('successfully')
end
end
3 changes: 2 additions & 1 deletion spec/html/proofer/fixtures/images/emptyImageAltText.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<body>

<p>Blah blah blah. <img src="./gpl.png" alt=""/> </p>
<p>Blah blah blah. <img src="./gpl.png" alt=" "/> </p>
<p>Blah blah blah. <img src="./gpl.png" alt=" "/> </p>
<p>Blah blah blah. <img src="./gpl.png" alt/> </p>
</body>

</html>
14 changes: 13 additions & 1 deletion spec/html/proofer/images_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
emptyAltFilepath = "#{FIXTURES_DIR}/images/emptyImageAltText.html"
proofer = run_proofer(emptyAltFilepath)
expect(proofer.failed_tests.first).to match(/gpl.png does not have an alt attribute/)
expect(proofer.failed_tests.length).to equal(2)
expect(proofer.failed_tests.length).to equal(3)
end

it 'passes when ignoring image with nothing but spaces in alt attribute' do
Expand Down Expand Up @@ -109,6 +109,18 @@
expect(proofer.failed_tests.first).to_not match /does not have an alt attribute/
end

it 'properly ignores empty alt attribute when empty_alt_ignore set' do
missingAltFilepath = "#{FIXTURES_DIR}/images/emptyImageAltText.html"
proofer = run_proofer(missingAltFilepath, {:empty_alt_ignore => true})
expect(proofer.failed_tests).to eq []
end

it 'properly ignores empty alt attributes, but not missing alt attributes, when empty_alt_ignore set' do
missingAltFilepath = "#{FIXTURES_DIR}/images/missingImageAlt.html"
proofer = run_proofer(missingAltFilepath, {:empty_alt_ignore => true})
expect(proofer.failed_tests.first).to match(/gpl.png does not have an alt attribute/)
end

it 'works for images with a srcset' do
srcSetCheck = "#{FIXTURES_DIR}/images/srcSetCheck.html"
proofer = run_proofer(srcSetCheck)
Expand Down