Skip to content
This repository
Browse code

Merge pull request #7754 from NARKOZ/image_tag-size

Feature: set the same width and height for image if size attribute is a number
  • Loading branch information...
commit 9fec0c8d3522c6b027dbe2df70c49953b2d9bbaa 2 parents c96b20f + 3164b0a
Rafael Mendonça França authored September 25, 2012
5  actionpack/CHANGELOG.md
Source Rendered
... ...
@@ -1,5 +1,10 @@
1 1
 ## Rails 4.0.0 (unreleased) ##
2 2
 
  3
+*   `image_tag` will set the same width and height for image if numerical value
  4
+    passed to `size` option.
  5
+
  6
+    *Nihad Abbasov*
  7
+
3 8
 *   Deprecate Mime::Type#verify_request? and Mime::Type.browser_generated_types,
4 9
     since they are no longer used inside of Rails, they will be removed in Rails 4.1
5 10
 
11  actionpack/lib/action_view/helpers/asset_tag_helper.rb
@@ -364,9 +364,9 @@ def font_url(source)
364 364
       #
365 365
       # * <tt>:alt</tt>  - If no alt text is given, the file name part of the
366 366
       #   +source+ is used (capitalized and without the extension)
367  
-      # * <tt>:size</tt> - Supplied as "{Width}x{Height}", so "30x45" becomes
368  
-      #   width="30" and height="45". <tt>:size</tt> will be ignored if the
369  
-      #   value is not in the correct format.
  367
+      # * <tt>:size</tt> - Supplied as "{Width}x{Height}" or "{Number}", so "30x45" becomes
  368
+      #   width="30" and height="45", and "50" becomes width="50" and height="50".
  369
+      #   <tt>:size</tt> will be ignored if the value is not in the correct format.
370 370
       #
371 371
       #  image_tag("icon")
372 372
       #  # => <img src="/assets/icon" alt="Icon" />
@@ -374,7 +374,7 @@ def font_url(source)
374 374
       #  # => <img src="/assets/icon.png" alt="Icon" />
375 375
       #  image_tag("icon.png", :size => "16x10", :alt => "Edit Entry")
376 376
       #  # => <img src="/assets/icon.png" width="16" height="10" alt="Edit Entry" />
377  
-      #  image_tag("/icons/icon.gif", :size => "16x16")
  377
+      #  image_tag("/icons/icon.gif", :size => "16")
378 378
       #  # => <img src="/icons/icon.gif" width="16" height="16" alt="Icon" />
379 379
       #  image_tag("/icons/icon.gif", :height => '32', :width => '32')
380 380
       #  # => <img alt="Icon" height="32" src="/icons/icon.gif" width="32" />
@@ -390,7 +390,8 @@ def image_tag(source, options={})
390 390
         end
391 391
 
392 392
         if size = options.delete(:size)
393  
-          options[:width], options[:height] = size.split("x") if size =~ %r{^\d+x\d+$}
  393
+          options[:width], options[:height] = size.split("x") if size =~ %r{\A\d+x\d+\z}
  394
+          options[:width] = options[:height] = size if size =~ %r{\A\d+\z}
394 395
         end
395 396
 
396 397
         tag("img", options)
2  actionpack/test/template/asset_tag_helper_test.rb
@@ -192,9 +192,9 @@ def teardown
192 192
   ImageLinkToTag = {
193 193
     %(image_tag("xml.png")) => %(<img alt="Xml" src="/images/xml.png" />),
194 194
     %(image_tag("rss.gif", :alt => "rss syndication")) => %(<img alt="rss syndication" src="/images/rss.gif" />),
  195
+    %(image_tag("gold.png", :size => "20")) => %(<img alt="Gold" height="20" src="/images/gold.png" width="20" />),
195 196
     %(image_tag("gold.png", :size => "45x70")) => %(<img alt="Gold" height="70" src="/images/gold.png" width="45" />),
196 197
     %(image_tag("gold.png", "size" => "45x70")) => %(<img alt="Gold" height="70" src="/images/gold.png" width="45" />),
197  
-    %(image_tag("error.png", "size" => "45")) => %(<img alt="Error" src="/images/error.png" />),
198 198
     %(image_tag("error.png", "size" => "45 x 70")) => %(<img alt="Error" src="/images/error.png" />),
199 199
     %(image_tag("error.png", "size" => "x")) => %(<img alt="Error" src="/images/error.png" />),
200 200
     %(image_tag("google.com.png")) => %(<img alt="Google.com" src="/images/google.com.png" />),

0 notes on commit 9fec0c8

Please sign in to comment.
Something went wrong with that request. Please try again.