Skip to content

Commit

Permalink
Merge pull request #851 from paulroub/srcset-sizes-fail
Browse files Browse the repository at this point in the history
Handle multiple srcsets when first is remote (fixes #850)
  • Loading branch information
gjtorikian authored Feb 21, 2025
2 parents 5903360 + f44aa4c commit 3ecb630
Show file tree
Hide file tree
Showing 21 changed files with 353 additions and 24 deletions.
9 changes: 6 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ inherit_gem:
rubocop-standard:
- config/default.yml

require:
plugins:
- rubocop-rspec

Naming/FileName:
Expand All @@ -14,8 +14,11 @@ RSpec/AnyInstance:
RSpec/ExampleLength:
Enabled: false

RSpec/FilePath:
Enabled: false
# RSpec/SpecFilePathFormat:
# Enabled: false

# RSpec/SpecFilePathSuffix:
# Enabled: false

RSpec/MultipleExpectations:
Enabled: false
Expand Down
2 changes: 1 addition & 1 deletion lib/html_proofer/attribute/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def without_hash
else
url
end
rescue Addressable::URI::InvalidURIError # rubocop:disable Lint/SuppressedException; error will be reported at check time
rescue Addressable::URI::InvalidURIError # rubocop:disable Lint/SuppressedException -- error will be reported at check time
end

private def swap_urls!
Expand Down
24 changes: 12 additions & 12 deletions lib/html_proofer/check/images.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,6 @@ def run
# 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)
Expand All @@ -47,6 +35,18 @@ def run
add_failure("internal image #{srcset} does not exist", element: @img)
end
end
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,
)
end

# if this is an img element, check that the alt attribute is present
Expand Down
2 changes: 1 addition & 1 deletion lib/html_proofer/log.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Log
def initialize(log_level)
@logger = Yell.new(
format: false,
name: "HTMLProofer", \
name: "HTMLProofer",
level: "gte.#{log_level}",
) do |l|
l.adapter(:stdout, level: "lte.warn")
Expand Down
2 changes: 1 addition & 1 deletion lib/html_proofer/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module HTMLProofer
VERSION = "5.0.9"
VERSION = "5.0.10"
end
2 changes: 1 addition & 1 deletion spec/html-proofer/cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ def read_cache(cache_filename)
external_link = cache["external"]["https://github.com/gjtorikian/html-proofer"]
expect(external_link["found"]).to(equal(true))
expect(external_link["status_code"]).to(equal(200))
expect(external_link["message"]).equal?("OK")
expect(external_link["message"]).to(match("OK"))

File.delete(cache_filepath)
end
Expand Down
6 changes: 6 additions & 0 deletions spec/html-proofer/check/images_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,12 @@
expect(proofer.failed_checks).to(eq([]))
end

it "works for picture elements with multiple remote sizes" do
custom_data_src_check = "#{FIXTURES_DIR}/images/srcset_picture_remote_sizes.html"
proofer = run_proofer(custom_data_src_check, :file)
expect(proofer.failed_checks).to(eq([]))
end

it "breaks for picture elements with multiple srcsets and pixel densities" do
custom_data_src_check = "#{FIXTURES_DIR}/images/srcset_picture_pixel-density_broken.html"
proofer = run_proofer(custom_data_src_check, :file)
Expand Down
2 changes: 1 addition & 1 deletion spec/html-proofer/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@
it "has only one UA" do
http = make_bin(%|--typhoeus='{"verbose":true,"headers":{"User-Agent":"Mozilla/5.0 (Macintosh; My New User-Agent)"}}' --as-links https://linkedin.com|)
expect(http.scan("User-Agent: Typhoeus").count).to(eq(0))
expect(http.scan(%r{User-Agent: Mozilla/5.0 \(Macintosh; My New User-Agent\)}i).count).to(eq(2))
expect(http.match(%r{User-Agent: Mozilla/5.0 \(Macintosh; My New User-Agent\)}i)).not_to(be_nil)
end

it "supports hydra" do
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":2,"internal":{"/somewhere.html":{"time":"2022-01-06 12:00:00 -0700","metadata":[{"source":"spec/html-proofer/fixtures/cache/internal_and_external_example.html","filename":"spec/html-proofer/fixtures/cache/internal_and_external_example.html","line":11,"base_url":"","found":false}]}},"external":{"https://github.com/gjtorikian/html-proofer":{"time":"2022-01-06 12:00:00 -0700","found":true,"status_code":200,"message":"OK","metadata":[{"filename":"spec/html-proofer/fixtures/cache/internal_and_external_example.html","line":7}]}}}
{"version":2,"internal":{"/somewhere.html":{"time":"2022-01-06 12:00:00 -0500","metadata":[{"source":"spec/html-proofer/fixtures/cache/internal_and_external_example.html","filename":"spec/html-proofer/fixtures/cache/internal_and_external_example.html","line":11,"base_url":"","found":false}]}},"external":{"https://github.com/gjtorikian/html-proofer":{"time":"2022-01-06 12:00:00 -0500","found":true,"status_code":200,"message":"OK","metadata":[{"filename":"spec/html-proofer/fixtures/cache/internal_and_external_example.html","line":7}]}}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":2,"internal":{},"external":{"https://github.com/gjtorikian/html-proofer":{"time":"2022-01-06 12:00:00 -0700","found":true,"status_code":200,"message":"OK","metadata":[{"filename":"spec/html-proofer/fixtures/cache/external_example.html","line":7}]}}}
{"version":2,"internal":{},"external":{"https://github.com/gjtorikian/html-proofer":{"time":"2022-01-06 12:00:00 -0500","found":true,"status_code":200,"message":"OK","metadata":[{"filename":"spec/html-proofer/fixtures/cache/external_example.html","line":7}]}}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":2,"internal":{},"external":{"https://github.com/gjtorikian":{"time":"2022-01-06 12:00:00 -0700","found":true,"status_code":200,"message":"OK","metadata":[{"filename":"spec/html-proofer/fixtures/cache/some_link.html","line":7}]}}}
{"version":2,"internal":{},"external":{"https://github.com/gjtorikian":{"time":"2022-01-06 12:00:00 -0500","found":true,"status_code":200,"message":"OK","metadata":[{"filename":"spec/html-proofer/fixtures/cache/some_link.html","line":7}]}}}
2 changes: 1 addition & 1 deletion spec/html-proofer/fixtures/cache/version_2/.runner.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":2,"internal":{},"external":{"https://www.github.com":{"time":"2022-02-17 12:00:00 -0700","found":true,"status_code":200,"message":"OK","metadata":[{"filename":"spec/html-proofer/fixtures/links/_site/folder.html/index.html","line":4}]}}}
{"version":2,"internal":{},"external":{"https://www.github.com":{"time":"2022-02-17 12:00:00 -0500","found":true,"status_code":200,"message":"OK","metadata":[{"filename":"spec/html-proofer/fixtures/links/_site/folder.html/index.html","line":4}]}}}
12 changes: 12 additions & 0 deletions spec/html-proofer/fixtures/images/srcset_picture_remote_sizes.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<picture>
<!--[if IE 9]><video style="display: none;"><![endif]-->
<source
srcset="
https://upload.wikimedia.org/wikipedia/en/thumb/2/22/Heckert_GNU_white.svg/256px-Heckert_GNU_white.svg.png 2x,
https://upload.wikimedia.org/wikipedia/en/2/22/Heckert_GNU_white.svg 1x
"
type="image/webp"
/>
<!--[if IE 9]></video><![endif]-->
<img srcset="gpl.png" alt="test" />
</picture>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3ecb630

Please sign in to comment.