Skip to content

Commit

Permalink
Set width and height attributes to the image tag if values exist (#…
Browse files Browse the repository at this point in the history
…13)

* Set `width` and `height` attributes to the image tag if values exist

* break lines
  • Loading branch information
frostmark authored Jul 19, 2022
1 parent 10b83ed commit d0389f2
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CHANGELOG

== 0.4.0 (2022-07-18)

- Set `width` and `height` attributes to the image tag if values exist
1 change: 1 addition & 0 deletions carrierwave-cloudflare.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rack", "~> 2.0"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
spec.add_development_dependency "rubocop"
end
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def hidpi_image_tag(url, dprs: nil, **options)
image_tag(
cdn_transformed(url, **transform),
srcset: hidpi_image_srcset(url, dprs: dprs, **transform),
width: transform[:width],
height: transform[:height],
**rest
)
end
Expand All @@ -73,7 +75,7 @@ def responsive_image_tag(url, width:, sizes: nil, dprs: [1, 2], **options)
url = url.url if url.is_a?(CarrierWave::Uploader)

if sizes.nil?
return hidpi_image_tag(url, **options)
return hidpi_image_tag(url, width: width, **options)
end

sizes[:default] = width
Expand Down Expand Up @@ -108,6 +110,8 @@ def responsive_image_tag(url, width:, sizes: nil, dprs: [1, 2], **options)
base_version,
srcset: srcset,
sizes: sizes_attr,
width: width,
height: transform[:height],
**rest
)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/carrierwave/cloudflare/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module CarrierWave
module Cloudflare
VERSION = "0.3.2"
VERSION = "0.4.0"
end
end
46 changes: 42 additions & 4 deletions spec/carrierwave/cloudflare/responsive_images_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,21 @@ class DummyView

it "generates image tag with srcset" do
img_tag = view.hidpi_image_tag("/img.jpg", width: 400, dprs: [1, 2, 3])
expected_img_tag = "<img srcset=\"/cdn-cgi/image/width=400,dpr=1/img.jpg 1x, /cdn-cgi/image/width=400,dpr=2/img.jpg 2x, /cdn-cgi/image/width=400,dpr=3/img.jpg 3x\" src=\"/cdn-cgi/image/width=400/img.jpg\" />"
expected_img_tag = "<img srcset=\"/cdn-cgi/image/width=400,dpr=1/img.jpg 1x,\
/cdn-cgi/image/width=400,dpr=2/img.jpg 2x,\
/cdn-cgi/image/width=400,dpr=3/img.jpg 3x\"\
width=\"400\" src=\"/cdn-cgi/image/width=400/img.jpg\" />"

expect(img_tag).to eql(expected_img_tag)
end

it "generates image tag with width and height" do
img_tag = view.hidpi_image_tag("/img.jpg", width: 400, height: 300, dprs: [1, 2, 3])

expected_img_tag = "<img srcset=\"/cdn-cgi/image/width=400,height=300,dpr=1/img.jpg 1x,\
/cdn-cgi/image/width=400,height=300,dpr=2/img.jpg 2x,\
/cdn-cgi/image/width=400,height=300,dpr=3/img.jpg 3x\"\
width=\"400\" height=\"300\" src=\"/cdn-cgi/image/width=400,height=300/img.jpg\" />"

expect(img_tag).to eql(expected_img_tag)
end
Expand All @@ -67,14 +81,16 @@ class DummyView

it "generates image srcset" do
img_srcset = view.hidpi_image_srcset("/img.jpg", width: 400, dprs: [1, 2, 3])
expected_img_srcset = "/cdn-cgi/image/width=400,dpr=1/img.jpg 1x, /cdn-cgi/image/width=400,dpr=2/img.jpg 2x, /cdn-cgi/image/width=400,dpr=3/img.jpg 3x"
expected_img_srcset = "/cdn-cgi/image/width=400,dpr=1/img.jpg 1x,\
/cdn-cgi/image/width=400,dpr=2/img.jpg 2x, /cdn-cgi/image/width=400,dpr=3/img.jpg 3x"

expect(img_srcset).to eql(expected_img_srcset)
end

it "generates image srcset (uses default drps)" do
img_srcset = view.hidpi_image_srcset("/img.jpg", width: 400)
expected_img_srcset = "/cdn-cgi/image/width=400,dpr=1/img.jpg 1x, /cdn-cgi/image/width=400,dpr=2/img.jpg 2x"
expected_img_srcset = "/cdn-cgi/image/width=400,dpr=1/img.jpg 1x,\
/cdn-cgi/image/width=400,dpr=2/img.jpg 2x"

expect(img_srcset).to eql(expected_img_srcset)
end
Expand All @@ -90,7 +106,29 @@ class DummyView
it "generates image responsive image tag" do
img_tag = view.responsive_image_tag('/bird.jpg', width: 1200, sizes: { phone: 600, tablet: 800 })

expected_img_tag = "<img srcset=\"/cdn-cgi/image/width=1200,dpr=0.5/bird.jpg 600w, /cdn-cgi/image/width=1200,dpr=1.0/bird.jpg 1200w, /cdn-cgi/image/width=1200,dpr=0.67/bird.jpg 800w, /cdn-cgi/image/width=1200,dpr=1.33/bird.jpg 1600w, /cdn-cgi/image/width=1200,dpr=2.0/bird.jpg 2400w\" sizes=\"(max-width: 767px) 600px, (max-width: 1023px) 800px, 1200px\" src=\"/cdn-cgi/image/width=1200/bird.jpg\" />"
expected_img_tag = "<img srcset=\"/cdn-cgi/image/width=1200,dpr=0.5/bird.jpg 600w,\
/cdn-cgi/image/width=1200,dpr=1.0/bird.jpg 1200w,\
/cdn-cgi/image/width=1200,dpr=0.67/bird.jpg 800w,\
/cdn-cgi/image/width=1200,dpr=1.33/bird.jpg 1600w,\
/cdn-cgi/image/width=1200,dpr=2.0/bird.jpg 2400w\"\
sizes=\"(max-width: 767px) 600px, (max-width: 1023px) 800px,\
1200px\" width=\"1200\" src=\"/cdn-cgi/image/width=1200/bird.jpg\" />"


expect(img_tag).to eql(expected_img_tag)
end

it "generates image responsive image tag with height" do
img_tag = view.responsive_image_tag('/bird.jpg', width: 200, height: 1200, sizes: { phone: 600, tablet: 800 })

expected_img_tag = "<img srcset=\"/cdn-cgi/image/width=200,height=1200,dpr=3.0/bird.jpg 600w,\
/cdn-cgi/image/width=200,height=1200,dpr=6.0/bird.jpg 1200w,\
/cdn-cgi/image/width=200,height=1200,dpr=4.0/bird.jpg 800w,\
/cdn-cgi/image/width=200,height=1200,dpr=8.0/bird.jpg 1600w,\
/cdn-cgi/image/width=200,height=1200,dpr=1.0/bird.jpg 200w,\
/cdn-cgi/image/width=200,height=1200,dpr=2.0/bird.jpg 400w\"\
sizes=\"(max-width: 767px) 600px, (max-width: 1023px) 800px, 200px\"\
width=\"200\" height=\"1200\" src=\"/cdn-cgi/image/width=200,height=1200/bird.jpg\" />"


expect(img_tag).to eql(expected_img_tag)
Expand Down

0 comments on commit d0389f2

Please sign in to comment.