From 580431ca431ed7a60813b16adec36f12c54f4dec Mon Sep 17 00:00:00 2001 From: David Morrow Date: Wed, 22 Apr 2015 13:09:02 -0700 Subject: [PATCH] Resolves issue #1396 supporting srcset allows you to use image_tag helper and have it build the asset urls for your srcset images, just like it does for your main src. Leaves absolute urls alone, (having // in the path) ``` <%= image_tage 'pic_1980.jpg', srcset: 'pic_640.jpg 2x, pic_1024.jpg 3x' %> => ``` --- middleman-core/features/image_srcset_paths.feature | 7 +++++++ .../features/markdown_kramdown_in_haml.feature | 3 ++- .../image-srcset-paths.html.erb | 1 + .../image-srcset-paths-app/images/blank.gif | Bin 0 -> 43 bytes .../core_extensions/default_helpers.rb | 13 +++++++++++++ 5 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 middleman-core/features/image_srcset_paths.feature create mode 100755 middleman-core/fixtures/image-srcset-paths-app/image-srcset-paths.html.erb create mode 100755 middleman-core/fixtures/image-srcset-paths-app/images/blank.gif diff --git a/middleman-core/features/image_srcset_paths.feature b/middleman-core/features/image_srcset_paths.feature new file mode 100644 index 00000000..b6b1046c --- /dev/null +++ b/middleman-core/features/image_srcset_paths.feature @@ -0,0 +1,7 @@ +Feature: Support srcset property as params for image_tag helper + This lets you specify responsive image sizes + + Scenario: Rendering an image with the feature enabled + Given the Server is running at "image-srcset-paths-app" + When I go to "/image-srcset-paths.html" + Then I should see '//example.com/remote-image.jpg 2x, /images/blank_3x.jpg 3x' \ No newline at end of file diff --git a/middleman-core/features/markdown_kramdown_in_haml.feature b/middleman-core/features/markdown_kramdown_in_haml.feature index 5b5a5871..5980e1b7 100644 --- a/middleman-core/features/markdown_kramdown_in_haml.feature +++ b/middleman-core/features/markdown_kramdown_in_haml.feature @@ -33,9 +33,10 @@ Feature: Markdown support in Haml (Kramdown) :markdown [A link](/link_target.html) - ![image](blank.gif) + ![image](blank.gif){: srcset="image_2x.jpg 2x"} """ Given the Server is running at "markdown-in-haml-app" When I go to "/link_and_image/" Then I should see "/link_target/" + Then I should see "/images/image_2x.jpg 2x" Then I should see 'src="/images/blank.gif"' diff --git a/middleman-core/fixtures/image-srcset-paths-app/image-srcset-paths.html.erb b/middleman-core/fixtures/image-srcset-paths-app/image-srcset-paths.html.erb new file mode 100755 index 00000000..b5fba613 --- /dev/null +++ b/middleman-core/fixtures/image-srcset-paths-app/image-srcset-paths.html.erb @@ -0,0 +1 @@ +<%= image_tag 'blank.jpg', srcset: '//example.com/remote-image.jpg 2x, blank_3x.jpg 3x, http://example.com/remoteimage.jpg 4x' %> \ No newline at end of file diff --git a/middleman-core/fixtures/image-srcset-paths-app/images/blank.gif b/middleman-core/fixtures/image-srcset-paths-app/images/blank.gif new file mode 100755 index 0000000000000000000000000000000000000000..2498f1aac58dab923f0fd99b1c8ee6b8c53c7158 GIT binary patch literal 43 scmZ?wbhEHbWMp7uXkcKtd-pB_1B2pE79h#MpaUX6G7L;iE{qJ;0KEqWk^lez literal 0 HcmV?d00001 diff --git a/middleman-core/lib/middleman-more/core_extensions/default_helpers.rb b/middleman-core/lib/middleman-more/core_extensions/default_helpers.rb index 56daaa28..e711c234 100644 --- a/middleman-core/lib/middleman-more/core_extensions/default_helpers.rb +++ b/middleman-core/lib/middleman-more/core_extensions/default_helpers.rb @@ -255,5 +255,18 @@ class Middleman::CoreExtensions::DefaultHelpers < ::Middleman::Extension url = url_for(url, options) super end + + # Modified Padrino image_tag so that it finds the paths for srcset + # using asset_path for the images listed in the srcset param + def image_tag(path, params={}) + params.symbolize_keys! + + if params.key?(:srcset) + images = params[:srcset].split(',').map {|size| (size.include?('//') ? size : asset_url("images/#{size.strip}")) } + params[:srcset] = images.join(', ') + end + + super(path, params) + end end end