Class: ImageCheck

Inherits:
HTMLProofer::Check show all
Defined in:
lib/html-proofer/check/images.rb

Constant Summary collapse

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

Instance Attribute Summary

Attributes inherited from HTMLProofer::Check

#element, #external_urls, #html, #issues, #node, #options, #path, #src

Instance Method Summary collapse

Methods inherited from HTMLProofer::Check

#add_issue, #add_path_for_url, #add_to_external_urls, #blank?, #create_element, #initialize, subchecks

Constructor Details

This class inherits a constructor from HTMLProofer::Check

Instance Method Details

#empty_alt_tag?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/html-proofer/check/images.rb', line 6

def empty_alt_tag?
  @img.alt.nil? || @img.alt.strip.empty?
end

#missing_src?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/html-proofer/check/images.rb', line 14

def missing_src?
  blank?(@img.url)
end

#runObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/html-proofer/check/images.rb', line 18

def run
  @html.css('img').each do |node|
    @img = create_element(node)
    line = node.line
    content = node.content

    next if @img.ignore?

    # screenshot filenames should return because of terrible names
    if terrible_filename?
      add_issue("image has a terrible filename (#{@img.url})", line: line, content: content)
      next
    end

    # does the image exist?
    if missing_src?
      add_issue('image has no src or srcset attribute', line: line, content: content)
    elsif @img.remote?
      add_to_external_urls(@img.url)
    elsif !@img.exists?
      add_issue("internal image #{@img.url} does not exist", line: line, content: content)
    end

    add_issue("image #{@img.url} does not have an alt attribute", line: line, content: content) if empty_alt_tag? && !@img.ignore_empty_alt? && !@img.ignore_alt?

    add_issue("image #{@img.url} uses the http scheme", line: line, content: content) if @img.check_img_http? && @img.scheme == 'http'
  end

  external_urls
end

#terrible_filename?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/html-proofer/check/images.rb', line 10

def terrible_filename?
  @img.url =~ SCREEN_SHOT_REGEX
end