From 2b9bce88e12c1e23320a6e4733198d1125939c0d Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Tue, 25 Sep 2012 18:20:13 +0500 Subject: allow to pass numerical value to size option in image_tag This will set image's both width and height attributes to value passed in size option. --- actionpack/CHANGELOG.md | 5 +++++ actionpack/lib/action_view/helpers/asset_tag_helper.rb | 9 +++++---- actionpack/test/template/asset_tag_helper_test.rb | 2 +- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index ceaff1a3f4..248677688f 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,5 +1,10 @@ ## Rails 4.0.0 (unreleased) ## +* `image_tag` will set the same width and height for image if numerical value + passed to `size` option. + + *Nihad Abbasov* + * Deprecate Mime::Type#verify_request? and Mime::Type.browser_generated_types, since they are no longer used inside of Rails, they will be removed in Rails 4.1 diff --git a/actionpack/lib/action_view/helpers/asset_tag_helper.rb b/actionpack/lib/action_view/helpers/asset_tag_helper.rb index db4da6f9c8..08efc98a50 100644 --- a/actionpack/lib/action_view/helpers/asset_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/asset_tag_helper.rb @@ -364,9 +364,9 @@ module ActionView # # * :alt - If no alt text is given, the file name part of the # +source+ is used (capitalized and without the extension) - # * :size - Supplied as "{Width}x{Height}", so "30x45" becomes - # width="30" and height="45". :size will be ignored if the - # value is not in the correct format. + # * :size - Supplied as "{Width}x{Height}" or "{Number}", so "30x45" becomes + # width="30" and height="45", and "50" becomes width="50" and height="50". + # :size will be ignored if the value is not in the correct format. # # image_tag("icon") # # => Icon @@ -374,7 +374,7 @@ module ActionView # # => Icon # image_tag("icon.png", :size => "16x10", :alt => "Edit Entry") # # => Edit Entry - # image_tag("/icons/icon.gif", :size => "16x16") + # image_tag("/icons/icon.gif", :size => "16") # # => Icon # image_tag("/icons/icon.gif", :height => '32', :width => '32') # # => Icon @@ -391,6 +391,7 @@ module ActionView if size = options.delete(:size) options[:width], options[:height] = size.split("x") if size =~ %r{^\d+x\d+$} + options[:width] = options[:height] = size if size =~ %r{\A\d+\z} end tag("img", options) diff --git a/actionpack/test/template/asset_tag_helper_test.rb b/actionpack/test/template/asset_tag_helper_test.rb index 25d85c47c7..a04694714d 100644 --- a/actionpack/test/template/asset_tag_helper_test.rb +++ b/actionpack/test/template/asset_tag_helper_test.rb @@ -192,9 +192,9 @@ class AssetTagHelperTest < ActionView::TestCase ImageLinkToTag = { %(image_tag("xml.png")) => %(Xml), %(image_tag("rss.gif", :alt => "rss syndication")) => %(rss syndication), + %(image_tag("gold.png", :size => "20")) => %(Gold), %(image_tag("gold.png", :size => "45x70")) => %(Gold), %(image_tag("gold.png", "size" => "45x70")) => %(Gold), - %(image_tag("error.png", "size" => "45")) => %(Error), %(image_tag("error.png", "size" => "45 x 70")) => %(Error), %(image_tag("error.png", "size" => "x")) => %(Error), %(image_tag("google.com.png")) => %(Google.com), -- cgit v1.2.3 From 3164b0a2c1e6b000d933263716f2e3f2ff1cca52 Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Tue, 25 Sep 2012 21:30:27 +0500 Subject: change ^ and $ anchors in regexp to \A and \z respectively http://guides.rubyonrails.org/security.html#regular-expressions --- actionpack/lib/action_view/helpers/asset_tag_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/actionpack/lib/action_view/helpers/asset_tag_helper.rb b/actionpack/lib/action_view/helpers/asset_tag_helper.rb index 08efc98a50..27ba57ff58 100644 --- a/actionpack/lib/action_view/helpers/asset_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/asset_tag_helper.rb @@ -390,7 +390,7 @@ module ActionView end if size = options.delete(:size) - options[:width], options[:height] = size.split("x") if size =~ %r{^\d+x\d+$} + options[:width], options[:height] = size.split("x") if size =~ %r{\A\d+x\d+\z} options[:width] = options[:height] = size if size =~ %r{\A\d+\z} end -- cgit v1.2.3