Sha256: 84c5486c787d74668c1ed618c3238892dfecf4ac2936210c6fc9136584288cb0

Contents?: true

Size: 1.16 KB

Versions: 8

Compression:

Stored size: 1.16 KB

Contents

# encoding: utf-8

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.empty?
  end

  def terrible_filename?
    src =~ SCREEN_SHOT_REGEX
  end

  def src
    real_attr(@src) || real_attr(@srcset)
  end

  def missing_src?
    !src
  end

end

class ImageCheck < ::HTML::Proofer::CheckRunner
  def run
    @html.css('img').each do |i|
      img = ImageCheckable.new i, self

      next if img.ignore?

      # screenshot filenames should return because of terrible names
      next add_issue("image has a terrible filename (#{img.src})", i.line) if img.terrible_filename?

      # does the image exist?
      if img.missing_src?
        add_issue('image has no src or srcset attribute', i.line)
      else
        if img.remote?
          add_to_external_urls img.src
        else
          add_issue("internal image #{img.src} does not exist", i.line) unless img.exists?
        end
      end

      # check alt tag
      add_issue("image #{img.src} does not have an alt attribute", i.line) unless img.valid_alt_tag?
    end

    external_urls
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
html-proofer-2.1.0 lib/html/proofer/checks/images.rb
html-proofer-2.0.6 lib/html/proofer/checks/images.rb
html-proofer-2.0.5 lib/html/proofer/checks/images.rb
html-proofer-2.0.4 lib/html/proofer/checks/images.rb
html-proofer-2.0.3 lib/html/proofer/checks/images.rb
html-proofer-2.0.2 lib/html/proofer/checks/images.rb
html-proofer-2.0.1 lib/html/proofer/checks/images.rb
html-proofer-2.0.0 lib/html/proofer/checks/images.rb