Class: HTMLProofer::Check::Images
- Inherits:
-
HTMLProofer::Check
- Object
- HTMLProofer::Check
- HTMLProofer::Check::Images
- 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+/
Instance Attribute Summary
Attributes inherited from HTMLProofer::Check
#external_urls, #failures, #internal_urls, #options
Instance Method Summary collapse
- #alt_all_spaces? ⇒ Boolean
- #empty_alt_tag? ⇒ Boolean
- #empty_whitespace_alt_tag? ⇒ Boolean
- #ignore_element? ⇒ Boolean
- #ignore_empty_alt? ⇒ Boolean
- #ignore_missing_alt? ⇒ Boolean
- #missing_alt_tag? ⇒ Boolean
- #missing_src? ⇒ Boolean
- #run ⇒ Object
- #terrible_filename? ⇒ Boolean
Methods inherited from HTMLProofer::Check
#add_failure, #add_to_external_urls, #add_to_internal_urls, #create_element, #initialize, #short_name, short_name, subchecks
Methods included from Utils
#blank?, #create_nokogiri, #pluralize
Constructor Details
This class inherits a constructor from HTMLProofer::Check
Instance Method Details
#alt_all_spaces? ⇒ Boolean
100 101 102 |
# File 'lib/html_proofer/check/images.rb', line 100 def alt_all_spaces? !missing_alt_tag? && @img.node["alt"].split.all?(" ") end |
#empty_alt_tag? ⇒ Boolean
92 93 94 |
# File 'lib/html_proofer/check/images.rb', line 92 def empty_alt_tag? !missing_alt_tag? && @img.node["alt"].empty? end |
#empty_whitespace_alt_tag? ⇒ Boolean
96 97 98 |
# File 'lib/html_proofer/check/images.rb', line 96 def empty_whitespace_alt_tag? !missing_alt_tag? && @img.node["alt"].strip.empty? end |
#ignore_element? ⇒ Boolean
84 85 86 |
# File 'lib/html_proofer/check/images.rb', line 84 def ignore_element? @img.url.ignore? || @img.aria_hidden? end |
#ignore_empty_alt? ⇒ Boolean
80 81 82 |
# File 'lib/html_proofer/check/images.rb', line 80 def ignore_empty_alt? @runner.[:ignore_empty_alt] end |
#ignore_missing_alt? ⇒ Boolean
76 77 78 |
# File 'lib/html_proofer/check/images.rb', line 76 def ignore_missing_alt? @runner.[:ignore_missing_alt] end |
#missing_alt_tag? ⇒ Boolean
88 89 90 |
# File 'lib/html_proofer/check/images.rb', line 88 def missing_alt_tag? @img.node["alt"].nil? end |
#missing_src? ⇒ Boolean
108 109 110 |
# File 'lib/html_proofer/check/images.rb', line 108 def missing_src? blank?(@img.url.to_s) end |
#run ⇒ Object
8 9 10 11 12 13 14 15 16 17 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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/html_proofer/check/images.rb', line 8 def run @html.css("img, source").each do |node| @img = create_element(node) next if @img.ignore? # screenshot filenames should return because of terrible names add_failure( "image has a terrible filename (#{@img.url.raw_attribute})", element: @img, ) if terrible_filename? # does the image exist? if missing_src? add_failure("image has no src or srcset attribute", element: @img) elsif @img.url.protocol_relative? add_failure( "image link #{@img.url} is a protocol-relative URL, use explicit https:// instead", element: @img, ) elsif @img.url.remote? add_to_external_urls(@img.url, @img.line) elsif !@img.url.exists? && !@img.multiple_srcsets? && !@img.multiple_sizes? add_failure( "internal image #{@img.url.raw_attribute} does not exist", element: @img, ) elsif @img.multiple_srcsets? || @img.multiple_sizes? @img.srcsets_wo_sizes.each do |srcset| srcset_url = HTMLProofer::Attribute::Url.new(@runner, srcset, base_url: @img.base_url, source: @img.url.source, filename: @img.url.filename, extract_size: true) if srcset_url.protocol_relative? add_failure( "image link #{srcset_url.url} is a protocol-relative URL, use explicit https:// instead", element: @img, ) elsif srcset_url.remote? add_to_external_urls(srcset_url.url, @img.line) elsif !srcset_url.exists? add_failure("internal image #{srcset} does not exist", element: @img) end end end # if this is an img element, check that the alt attribute is present if @img.img_tag? && !ignore_element? if missing_alt_tag? && !ignore_missing_alt? add_failure( "image #{@img.url.raw_attribute} does not have an alt attribute", element: @img, ) elsif (empty_alt_tag? || alt_all_spaces?) && !ignore_empty_alt? add_failure( "image #{@img.url.raw_attribute} has an alt attribute, but no content", element: @img, ) end end add_failure( "image #{@img.url.raw_attribute} uses the http scheme", element: @img, ) if @runner.enforce_https? && @img.url.http? end external_urls end |
#terrible_filename? ⇒ Boolean
104 105 106 |
# File 'lib/html_proofer/check/images.rb', line 104 def terrible_filename? @img.url.to_s =~ SCREEN_SHOT_REGEX end |