Skip to content

Commit

Permalink
Merge pull request #740 from gjtorikian/webp-madness
Browse files Browse the repository at this point in the history
adjust for additional srcset/webp logic
  • Loading branch information
gjtorikian authored Jul 26, 2022
2 parents 565bdd9 + 85f3f73 commit 54358c4
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 4 deletions.
6 changes: 4 additions & 2 deletions lib/html_proofer/attribute/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@
module HTMLProofer
class Attribute
class Url < HTMLProofer::Attribute
attr_reader :url
attr_reader :url, :size

REMOTE_SCHEMES = ["http", "https"].freeze

def initialize(runner, link_attribute, base_url: nil)
def initialize(runner, link_attribute, base_url: nil, extract_size: false)
super

if @raw_attribute.nil?
@url = nil
else
@url = @raw_attribute.delete("\u200b").strip
@url, @size = @url.split(/\s+/) if extract_size
@url = Addressable::URI.join(base_url, @url).to_s unless blank?(base_url)
@url = "" if @url.nil?

swap_urls!
clean_url!
Expand Down
4 changes: 2 additions & 2 deletions lib/html_proofer/check/images.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def run
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)
srcset_url = HTMLProofer::Attribute::Url.new(@runner, srcset, base_url: @img.base_url, extract_size: true)

if srcset_url.remote?
add_to_external_urls(srcset_url.url, @img.line)
Expand All @@ -35,7 +35,7 @@ def run
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)
srcset_url = HTMLProofer::Attribute::Url.new(@runner, srcset, base_url: @img.base_url, extract_size: true)

if srcset_url.remote?
add_to_external_urls(srcset_url.url, @img.line)
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
10 changes: 10 additions & 0 deletions spec/html-proofer/fixtures/images/webp_valid.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html>
<body>
<div class="w-100">
<img alt="User::round" srcset="webp/user_400.webp 400w, webp/user_760.webp
760w, webp/user_1200x1200_fit_q75_h2_lanczos.webp 1200w"
src=webp/user_400.webp width="250" height="187" loading="lazy"
data-zoomable>
</div>
</body>
</html>
6 changes: 6 additions & 0 deletions spec/html-proofer/images_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,4 +259,10 @@
proofer = run_proofer(custom_data_src_check, :file)
expect(proofer.failed_checks.first.description).to(match(/foo.webp does not exist/))
end

it "works for various webp images" do
custom_data_src_check = "#{FIXTURES_DIR}/images/webp_valid.html"
proofer = run_proofer(custom_data_src_check, :file)
expect(proofer.failed_checks).to(eq([]))
end
end

0 comments on commit 54358c4

Please sign in to comment.