1
0
Fork 0
mirror of https://github.com/middleman/middleman.git synced 2022-11-09 12:20:27 -05:00

Merge pull request #1721 from bhollis/issue1721-relative-paths

relative URLs don't work in image_tag
This commit is contained in:
Thomas Reynolds 2015-12-30 16:04:09 -08:00
commit 5684caeee6
2 changed files with 16 additions and 15 deletions

View file

@ -203,10 +203,14 @@ module Middleman
# Don't touch assets which already have a full path
return path if path.include?('//') || path.start_with?('data:')
if options[:relative] && !options[:current_resource]
raise ArgumentError, '#asset_url must be run in a context with current_resource if relative: true'
end
uri = URI(path)
path = uri.path
result = if resource = app.sitemap.find_resource_by_destination_path(url_for(app, path))
result = if resource = app.sitemap.find_resource_by_destination_path(url_for(app, path, options))
resource.url
else
path = File.join(prefix, path)
@ -217,16 +221,7 @@ module Middleman
end
end
final_result = if options[:relative] != true
result
else
unless options[:current_resource]
raise ArgumentError, '#asset_url must be run in a context with current_resource if relative: true'
end
current_dir = Pathname('/' + options[:current_resource].destination_path)
Pathname(result).relative_path_from(current_dir.dirname).to_s
end
final_result = URI.encode(relative_path_from_resource(options[:current_resource], result, options[:relative]))
result_uri = URI(final_result)
result_uri.query = uri.query
@ -296,9 +291,6 @@ module Middleman
else
resource_url
end
else
# If they explicitly asked for relative links but we can't find a resource...
raise "No resource exists at #{url}" if relative
end
# Support a :query option that can be a string or hash

View file

@ -98,6 +98,15 @@ describe Middleman::Util do
end
end
it "returns path relative to the provided current_resource" do
Given.fixture 'clean-dir-app'
Given.file 'source/a-path/index.html', ''
Given.file 'source/a-path/images/blank.gif', ''
@mm = Middleman::Application.new
current_resource = @mm.sitemap.find_resource_by_path('a-path/index.html')
expect( Middleman::Util.asset_url( @mm, 'images/blank.gif', 'images', current_resource: current_resource ) ).to eq '/a-path/images/blank.gif'
end
context "when relative is true" do
before(:each) do
@ -106,7 +115,7 @@ describe Middleman::Util do
end
it "returns path relative to the provided current_resource" do
current_resource = instance_double("Middleman::Sitemap::Resource", destination_path: 'a-path/index.html')
current_resource = instance_double("Middleman::Sitemap::Resource", destination_path: 'a-path/index.html', path: 'a-path/index.html')
expect( Middleman::Util.asset_url( @mm, 'blank.gif', 'images', current_resource: current_resource,
relative: true ) ).to eq '../images/blank.gif'
end