Skip to content

Commit

Permalink
Merge pull request #728 from gjtorikian/buggies
Browse files Browse the repository at this point in the history
Squash some minor 4.x bugs
  • Loading branch information
gjtorikian committed Jul 15, 2022
2 parents 0f94d78 + 319c193 commit cd7a457
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 4 deletions.
4 changes: 3 additions & 1 deletion bin/htmlproofer
Expand Up @@ -24,7 +24,7 @@ Mercenary.program(:htmlproofer) do |p|
p.option 'check_sri', '--check-sri', 'Check that `<link>` and `<script>` external resources use SRI (default: `false`).'
p.option 'directory_index_file', '--directory-index-file <filename>', String, 'Sets the file to look for when a link refers to a directory. (default: `index.html`)'
p.option 'disable_external', '--disable-external', 'If `true`, does not run the external link checker (default: `false`)'
p.option 'enforce_https', '--enforce-https', 'Fails a link if it\'s not marked as `https` (default: `true`).'
p.option 'enforce_https', '--enforce-https <false>', String, 'Fails a link if it\'s not marked as `https` (default: `true`).'
p.option 'extensions', '--extensions ext1,[ext2,...[', Array, 'A comma-separated list of Strings indicating the file extensions you would like to check (including the dot) (default: `.html`)'
p.option 'ignore_empty_alt', '--ignore-empty-alt', ' If `true`, ignores images with empty/missing alt tags (in other words, `<img alt>` and `<img alt="">` are valid; set this to `false` to flag those)'
p.option 'ignore_files', '--ignore-files file1,[file2,...]', Array, 'A comma-separated list of Strings or RegExps containing file paths that are safe to ignore'
Expand Down Expand Up @@ -67,6 +67,8 @@ Mercenary.program(:htmlproofer) do |p|
end
end

options[:enforce_https] = false if opts['enforce_https'] == "false"

options[:log_level] = opts['log_level'].to_sym unless opts['log_level'].nil?

options[:typhoeus] = HTMLProofer::Configuration.parse_json_option('typhoeus', opts['typhoeus'], symbolize_names: false) unless opts['typhoeus'].nil?
Expand Down
15 changes: 12 additions & 3 deletions lib/html_proofer/check/images.rb
Expand Up @@ -20,12 +20,21 @@ def run
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?
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?
srcsets = @img.srcset.split(",").map(&:strip)
srcsets.each do |srcset|
@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?
Expand Down
22 changes: 22 additions & 0 deletions lib/html_proofer/element.rb
Expand Up @@ -84,6 +84,28 @@ def multiple_srcsets?
!blank?(srcset) && srcset.split(",").size > 1
end

def srcsets
return nil if blank?(srcset)

srcset.split(",").map(&:strip)
end

def multiple_sizes?
return false if blank?(srcsets)

srcsets.any? do |srcset|
!blank?(srcset) && srcset.split(" ").size > 1
end
end

def srcsets_wo_sizes
return nil if blank?(srcsets)

srcsets.map do |srcset|
srcset.split(" ").first
end
end

def ignore?
return true if @node.attributes["data-proofer-ignore"]
return true if ancestors_ignorable?
Expand Down
10 changes: 10 additions & 0 deletions spec/html-proofer/command_spec.rb
Expand Up @@ -100,6 +100,16 @@
expect(output).to(match("successfully"))
end

it "works with enforce-https" do
custom_data_src_check = File.join(FIXTURES_DIR, "images", "src_http.html")
output = make_bin(custom_data_src_check.to_s)
expect(output).to(match("1 failure"))

custom_data_src_check = File.join(FIXTURES_DIR, "images", "src_http.html")
output = make_bin("#{custom_data_src_check} --enforce-https=false")
expect(output).to(match("successfully"))
end

it "has every option for proofer defaults" do
match_command_help(HTMLProofer::Configuration::PROOFER_DEFAULTS)
end
Expand Down
Binary file added spec/html-proofer/fixtures/images/1.webp
Binary file not shown.
5 changes: 5 additions & 0 deletions spec/html-proofer/fixtures/images/srcset-pixel-density.html
@@ -0,0 +1,5 @@
<html>
<body>
<img srcset="1.webp 1.5x" alt="test" width="100" height="150" />
</body>
</html>
@@ -0,0 +1,5 @@
<html>
<body>
<img srcset="foo.webp 1.5x" alt="test" width="100" height="150" />
</body>
</html>
12 changes: 12 additions & 0 deletions spec/html-proofer/images_spec.rb
Expand Up @@ -247,4 +247,16 @@
proofer = run_proofer(custom_data_src_check, :file, swap_attributes: { "img" => [["src", "foobar"]] })
expect(proofer.failed_checks.length).to(eq(1))
end

it "works for images with srcset and pixel density" do
custom_data_src_check = "#{FIXTURES_DIR}/images/srcset-pixel-density.html"
proofer = run_proofer(custom_data_src_check, :file)
expect(proofer.failed_checks).to(eq([]))
end

it "breaks for images with invalid srcset and pixel density" do
custom_data_src_check = "#{FIXTURES_DIR}/images/srcset-pixel-density_broken.html"
proofer = run_proofer(custom_data_src_check, :file)
expect(proofer.failed_checks.first.description).to(match(/foo.webp does not exist/))
end
end

0 comments on commit cd7a457

Please sign in to comment.