-
-
Notifications
You must be signed in to change notification settings - Fork 192
/
images.rb
102 lines (83 loc) · 3.41 KB
/
images.rb
1
2
3
4
5
6
7
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# frozen_string_literal: true
module HTMLProofer
class Check
class Images < HTMLProofer::Check
SCREEN_SHOT_REGEX = /Screen(?: |%20)Shot(?: |%20)\d+-\d+-\d+(?: |%20)at(?: |%20)\d+.\d+.\d+/.freeze
def run
@html.css("img").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})", line: @img.line,
content: @img.content) if terrible_filename?
# does the image exist?
if missing_src?
add_failure("image has no src or srcset attribute", line: @img.line, content: @img.content)
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", line: @img.line,
content: @img.content)
elsif @img.multiple_srcsets?
@img.srcsets.each do |srcset|
srcset_url = HTMLProofer::Attribute::Url.new(@runner, srcset, base_url: @img.base_url)
if srcset_url.remote?
add_to_external_urls(srcset_url.url, @img.line)
elsif !srcset_url.exists?
add_failure("internal image #{srcset} does not exist", line: @img.line, content: @img.content)
end
end
elsif @img.multiple_sizes?
@img.srcsets_wo_sizes.each do |srcset|
srcset_url = HTMLProofer::Attribute::Url.new(@runner, srcset, base_url: @img.base_url)
if srcset_url.remote?
add_to_external_urls(srcset_url.url, @img.line)
elsif !srcset_url.exists?
add_failure("internal image #{srcset} does not exist", line: @img.line, content: @img.content)
end
end
end
unless ignore_element?
if missing_alt_tag? && !ignore_missing_alt?
add_failure("image #{@img.url.raw_attribute} does not have an alt attribute", line: @img.line,
content: @img.content)
elsif (empty_alt_tag? || alt_all_spaces?) && !ignore_empty_alt?
add_failure("image #{@img.url.raw_attribute} has an alt attribute, but no content", line: @img.line,
content: @img.content)
end
end
add_failure("image #{@img.url.raw_attribute} uses the http scheme", line: @img.line,
content: @img.content) if @runner.enforce_https? && @img.url.http?
end
external_urls
end
def ignore_missing_alt?
@runner.options[:ignore_missing_alt]
end
def ignore_empty_alt?
@runner.options[:ignore_empty_alt]
end
def ignore_element?
@img.url.ignore? || @img.aria_hidden?
end
def missing_alt_tag?
@img.node["alt"].nil?
end
def empty_alt_tag?
!missing_alt_tag? && @img.node["alt"].empty?
end
def empty_whitespace_alt_tag?
!missing_alt_tag? && @img.node["alt"].strip.empty?
end
def alt_all_spaces?
!missing_alt_tag? && @img.node["alt"].split.all?(" ")
end
def terrible_filename?
@img.url.to_s =~ SCREEN_SHOT_REGEX
end
def missing_src?
blank?(@img.url.to_s)
end
end
end
end