diff --git a/README.md b/README.md index 2c4a1714..79a9fa53 100644 --- a/README.md +++ b/README.md @@ -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` | diff --git a/bin/htmlproof b/bin/htmlproof index ebf6ce51..f7b3c1c7 100755 --- a/bin/htmlproof +++ b/bin/htmlproof @@ -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`)' diff --git a/lib/html/proofer.rb b/lib/html/proofer.rb index 8df33de0..def8e42e 100644 --- a/lib/html/proofer.rb +++ b/lib/html/proofer.rb @@ -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, diff --git a/lib/html/proofer/check_runner.rb b/lib/html/proofer/check_runner.rb index 0c809eea..fe612c19 100644 --- a/lib/html/proofer/check_runner.rb +++ b/lib/html/proofer/check_runner.rb @@ -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 @@ -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 diff --git a/lib/html/proofer/checkable.rb b/lib/html/proofer/checkable.rb index 2e32cd5e..5959ec91 100644 --- a/lib/html/proofer/checkable.rb +++ b/lib/html/proofer/checkable.rb @@ -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? diff --git a/lib/html/proofer/checks/images.rb b/lib/html/proofer/checks/images.rb index 0f5dc489..4245f28c 100644 --- a/lib/html/proofer/checks/images.rb +++ b/lib/html/proofer/checks/images.rb @@ -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? @@ -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 diff --git a/spec/html/proofer/command_spec.rb b/spec/html/proofer/command_spec.rb index 783c915c..78076689 100644 --- a/spec/html/proofer/command_spec.rb +++ b/spec/html/proofer/command_spec.rb @@ -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 diff --git a/spec/html/proofer/fixtures/images/emptyImageAltText.html b/spec/html/proofer/fixtures/images/emptyImageAltText.html index 69223ef9..add190fb 100644 --- a/spec/html/proofer/fixtures/images/emptyImageAltText.html +++ b/spec/html/proofer/fixtures/images/emptyImageAltText.html @@ -3,7 +3,8 @@
Blah blah blah.
-Blah blah blah.
+Blah blah blah.
+Blah blah blah.