Fix asset_url and relative option bug.

If the relative option is set and the asset is stored in the directory
alongside the resource referencing it, asset_url produces the
wrong path ('../images/image.jpg' rather than just 'image.jpg').
This commit is contained in:
Gregory McIntyre 2016-03-19 13:37:12 +11:00
parent 563da9d45b
commit 0436c211c6
2 changed files with 19 additions and 1 deletions

View file

@ -77,7 +77,11 @@ module Middleman
uri = URI(path)
path = uri.path
result = if resource = app.sitemap.find_resource_by_destination_path(url_for(app, path, options))
# Ensure the url we pass into find_resource_by_destination_path is not a
# relative path, since it only takes absolute url paths.
dest_path = url_for(app, path, options.merge(relative: false))
result = if resource = app.sitemap.find_resource_by_destination_path(dest_path)
resource.url
else
path = ::File.join(prefix, path)

View file

@ -120,6 +120,20 @@ describe Middleman::Util do
relative: true ) ).to eq '../images/blank.gif'
end
context "when the asset is stored in the same directory as current_resource" do
before do
Given.file 'source/a-path/index.html', ''
Given.file 'source/a-path/blank.gif', ''
@mm = Middleman::Application.new
end
it "returns path relative to the provided current_resource" do
current_resource = @mm.sitemap.find_resource_by_path('a-path/index.html')
expect( Middleman::Util.asset_url( @mm, 'blank.gif', 'images', current_resource: current_resource,
relative: true) ).to eq 'blank.gif'
end
end
it "raises error if not given a current_resource" do
expect{
Middleman::Util.asset_url( @mm, 'blank.gif', 'images', relative: true )