Skip to content
This repository
Browse code

Merged pull request #313 from chadk/master.

[patch] Asset helpers should understand scheme-relative URLs
  • Loading branch information...
commit 4ce14fb0809ea8d636be0a1790fa38eafe64a46f 2 parents c57320d + c15108c
David Heinemeier Hansson authored April 26, 2011
4  actionpack/lib/action_view/helpers/asset_paths.rb
@@ -12,7 +12,7 @@ def initialize(config, controller)
12 12
         @controller = controller
13 13
       end
14 14
 
15  
-      # Add the extension +ext+ if not present. Return full URLs otherwise untouched.
  15
+      # Add the extension +ext+ if not present. Return full or scheme-relative URLs otherwise untouched.
16 16
       # Prefix with <tt>/dir/</tt> if lacking a leading +/+. Account for relative URL
17 17
       # roots. Rewrite the asset path for cache-busting asset ids. Include
18 18
       # asset host, if configured, with the correct request protocol.
@@ -33,7 +33,7 @@ def compute_public_path(source, dir, ext = nil, include_host = true)
33 33
       end
34 34
 
35 35
       def is_uri?(path)
36  
-        path =~ %r{^[-a-z]+://|^cid:}
  36
+        path =~ %r{^[-a-z]+://|^cid:|^//}
37 37
       end
38 38
 
39 39
     private
15  actionpack/test/template/asset_tag_helper_test.rb
@@ -66,6 +66,7 @@ def teardown
66 66
     %(auto_discovery_link_tag(:xml)) => %(<link href="http://www.example.com" rel="alternate" title="XML" type="application/xml" />),
67 67
     %(auto_discovery_link_tag(:rss, :action => "feed")) => %(<link href="http://www.example.com" rel="alternate" title="RSS" type="application/rss+xml" />),
68 68
     %(auto_discovery_link_tag(:rss, "http://localhost/feed")) => %(<link href="http://localhost/feed" rel="alternate" title="RSS" type="application/rss+xml" />),
  69
+    %(auto_discovery_link_tag(:rss, "//localhost/feed")) => %(<link href="//localhost/feed" rel="alternate" title="RSS" type="application/rss+xml" />),
69 70
     %(auto_discovery_link_tag(:rss, {:action => "feed"}, {:title => "My RSS"})) => %(<link href="http://www.example.com" rel="alternate" title="My RSS" type="application/rss+xml" />),
70 71
     %(auto_discovery_link_tag(:rss, {}, {:title => "My RSS"})) => %(<link href="http://www.example.com" rel="alternate" title="My RSS" type="application/rss+xml" />),
71 72
     %(auto_discovery_link_tag(nil, {}, {:type => "text/html"})) => %(<link href="http://www.example.com" rel="alternate" title="" type="text/html" />),
@@ -100,6 +101,7 @@ def teardown
100 101
 
101 102
     %(javascript_include_tag("http://example.com/all")) => %(<script src="http://example.com/all" type="text/javascript"></script>),
102 103
     %(javascript_include_tag("http://example.com/all.js")) => %(<script src="http://example.com/all.js" type="text/javascript"></script>),
  104
+    %(javascript_include_tag("//example.com/all.js")) => %(<script src="//example.com/all.js" type="text/javascript"></script>),
103 105
   }
104 106
 
105 107
   StylePathToTag = {
@@ -129,6 +131,7 @@ def teardown
129 131
 
130 132
     %(stylesheet_link_tag("http://www.example.com/styles/style")) => %(<link href="http://www.example.com/styles/style" media="screen" rel="stylesheet" type="text/css" />),
131 133
     %(stylesheet_link_tag("http://www.example.com/styles/style.css")) => %(<link href="http://www.example.com/styles/style.css" media="screen" rel="stylesheet" type="text/css" />),
  134
+    %(stylesheet_link_tag("//www.example.com/styles/style.css")) => %(<link href="//www.example.com/styles/style.css" media="screen" rel="stylesheet" type="text/css" />),
132 135
   }
133 136
 
134 137
   ImagePathToTag = {
@@ -157,6 +160,7 @@ def teardown
157 160
     %(image_tag("slash..png")) => %(<img alt="Slash." src="/images/slash..png" />),
158 161
     %(image_tag(".pdf.png")) => %(<img alt=".pdf" src="/images/.pdf.png" />),
159 162
     %(image_tag("http://www.rubyonrails.com/images/rails.png")) => %(<img alt="Rails" src="http://www.rubyonrails.com/images/rails.png" />),
  163
+    %(image_tag("//www.rubyonrails.com/images/rails.png")) => %(<img alt="Rails" src="//www.rubyonrails.com/images/rails.png" />),
160 164
     %(image_tag("mouse.png", :mouseover => "/images/mouse_over.png")) => %(<img alt="Mouse" onmouseover="this.src='/images/mouse_over.png'" onmouseout="this.src='/images/mouse.png'" src="/images/mouse.png" />),
161 165
     %(image_tag("mouse.png", :mouseover => image_path("mouse_over.png"))) => %(<img alt="Mouse" onmouseover="this.src='/images/mouse_over.png'" onmouseout="this.src='/images/mouse.png'" src="/images/mouse.png" />),
162 166
     %(image_tag("mouse.png", :alt => nil)) => %(<img src="/images/mouse.png" />)
@@ -195,6 +199,7 @@ def teardown
195 199
     %(video_tag("error.avi", "size" => "100 x 100")) => %(<video src="/videos/error.avi" />),
196 200
     %(video_tag("error.avi", "size" => "x")) => %(<video src="/videos/error.avi" />),
197 201
     %(video_tag("http://media.rubyonrails.org/video/rails_blog_2.mov")) => %(<video src="http://media.rubyonrails.org/video/rails_blog_2.mov" />),
  202
+    %(video_tag("//media.rubyonrails.org/video/rails_blog_2.mov")) => %(<video src="//media.rubyonrails.org/video/rails_blog_2.mov" />),
198 203
     %(video_tag(["multiple.ogg", "multiple.avi"])) => %(<video><source src="multiple.ogg" /><source src="multiple.avi" /></video>),
199 204
     %(video_tag(["multiple.ogg", "multiple.avi"], :size => "160x120", :controls => true)) => %(<video controls="controls" height="120" width="160"><source src="multiple.ogg" /><source src="multiple.avi" /></video>)
200 205
   }
@@ -217,6 +222,7 @@ def teardown
217 222
     %(audio_tag("xml.wav")) => %(<audio src="/audios/xml.wav" />),
218 223
     %(audio_tag("rss.wav", :autoplay => true, :controls => true)) => %(<audio autoplay="autoplay" controls="controls" src="/audios/rss.wav" />),
219 224
     %(audio_tag("http://media.rubyonrails.org/audio/rails_blog_2.mov")) => %(<audio src="http://media.rubyonrails.org/audio/rails_blog_2.mov" />),
  225
+    %(audio_tag("//media.rubyonrails.org/audio/rails_blog_2.mov")) => %(<audio src="//media.rubyonrails.org/audio/rails_blog_2.mov" />),
220 226
   }
221 227
 
222 228
   def test_auto_discovery_link_tag
@@ -505,6 +511,10 @@ def test_should_skip_asset_id_on_complete_url
505 511
     assert_equal %(<img alt="Rails" src="http://www.example.com/rails.png" />), image_tag("http://www.example.com/rails.png")
506 512
   end
507 513
 
  514
+  def test_should_skip_asset_id_on_scheme_relative_url
  515
+    assert_equal %(<img alt="Rails" src="//www.example.com/rails.png" />), image_tag("//www.example.com/rails.png")
  516
+  end
  517
+
508 518
   def test_should_use_preset_asset_id
509 519
     ENV["RAILS_ASSET_ID"] = "4500"
510 520
     assert_equal %(<img alt="Rails" src="/images/rails.png?4500" />), image_tag("rails.png")
@@ -1095,6 +1105,11 @@ def test_should_ignore_asset_host_on_complete_url
1095 1105
     assert_dom_equal(%(<link href="http://bar.example.com/stylesheets/style.css" media="screen" rel="stylesheet" type="text/css" />), stylesheet_link_tag("http://bar.example.com/stylesheets/style.css"))
1096 1106
   end
1097 1107
 
  1108
+  def test_should_ignore_asset_host_on_scheme_relative_url
  1109
+    @controller.config.asset_host = "http://assets.example.com"
  1110
+    assert_dom_equal(%(<link href="//bar.example.com/stylesheets/style.css" media="screen" rel="stylesheet" type="text/css" />), stylesheet_link_tag("//bar.example.com/stylesheets/style.css"))
  1111
+  end
  1112
+
1098 1113
   def test_should_wildcard_asset_host_between_zero_and_four
1099 1114
     @controller.config.asset_host = 'http://a%d.example.com'
1100 1115
     assert_match(%r(http://a[0123].example.com/collaboration/hieraki/images/xml.png), image_path('xml.png'))

0 notes on commit 4ce14fb

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