diff --git a/middleman-core/features/asset_hash.feature b/middleman-core/features/asset_hash.feature index 4e9b293d..78755818 100644 --- a/middleman-core/features/asset_hash.feature +++ b/middleman-core/features/asset_hash.feature @@ -64,9 +64,13 @@ Feature: Assets get file hashes appended to them and references to them are upda When I go to "/" Then I should see 'href="apple-touch-icon.png"' And I should see 'href="stylesheets/site-d2959d87.css"' + And I should see 'href="stylesheets/fragment-a06f0dfc.css"' And I should see 'src="javascripts/application-1d8d5276.js"' And I should see 'src="images/100px-5fd6fb90.jpg"' And I should see 'srcset="images/100px-5fd6fb90.jpg 1x, images/200px-c11eb203.jpg 2x, images/300px-59adce76.jpg 3x"' + And I should see 'src="images/100px-5fd6fb90.jpg?test"' + And I should see 'src="images/100px-5fd6fb90.jpg?#test"' + And I should see 'src="images/100px-5fd6fb90.jpg#test"' When I go to "/subdir/" Then I should see 'href="../stylesheets/site-d2959d87.css"' And I should see 'src="../javascripts/application-1d8d5276.js"' @@ -75,6 +79,9 @@ Feature: Assets get file hashes appended to them and references to them are upda Then I should see 'href="../stylesheets/site-d2959d87.css"' And I should see 'src="../javascripts/application-1d8d5276.js"' And I should see 'src="../images/100px-5fd6fb90.jpg"' + And I should see 'src="../images/100px-5fd6fb90.jpg?test"' + And I should see 'src="../images/100px-5fd6fb90.jpg?#test"' + And I should see 'src="../images/100px-5fd6fb90.jpg#test"' When I go to "/javascripts/application-1d8d5276.js" Then I should see "img.src = '/images/100px-5fd6fb90.jpg'" When I go to "/stylesheets/site-d2959d87.css" @@ -87,6 +94,11 @@ Feature: Assets get file hashes appended to them and references to them are upda Then I should see 'images/100px-5fd6fb90.gif' And I should see 'images/100px-5fd6fb90.jpg' And I should see 'images/100px-1242c368.png' + When I go to "/stylesheets/fragment-a06f0dfc.css" + And I should see 'url("../images/100px-5fd6fb90.jpg")' + And I should see 'url("../images/100px-5fd6fb90.jpg?test")' + And I should see 'url("../images/100px-5fd6fb90.jpg?#test")' + And I should see 'url("../images/100px-5fd6fb90.jpg#test")' Scenario: Hashed assets work with Slim Given the Server is running at "asset-hash-app" @@ -99,13 +111,25 @@ Feature: Assets get file hashes appended to them and references to them are upda Given the Server is running at "asset-hash-host-app" When I go to "/" Then I should see 'href="http://middlemanapp.com/stylesheets/site-e587b659.css"' + Then I should see 'href="http://middlemanapp.com/stylesheets/fragment-7af0b5ab.css"' And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg"' + And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg?test"' + And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg?#test"' + And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg#test"' When I go to "/subdir/" Then I should see 'href="http://middlemanapp.com/stylesheets/site-e587b659.css"' And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg"' When I go to "/other/" Then I should see 'href="http://middlemanapp.com/stylesheets/site-e587b659.css"' And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg"' + And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg?test"' + And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg?#test"' + And I should see 'src="http://middlemanapp.com/images/100px-5fd6fb90.jpg#test"' + When I go to "/stylesheets/fragment-7af0b5ab.css" + And I should see 'url("http://middlemanapp.com/images/100px-5fd6fb90.jpg")' + And I should see 'url("http://middlemanapp.com/images/100px-5fd6fb90.jpg?test")' + And I should see 'url("http://middlemanapp.com/images/100px-5fd6fb90.jpg?#test")' + And I should see 'url("http://middlemanapp.com/images/100px-5fd6fb90.jpg#test")' Scenario: The asset hash should change when a SASS partial changes Given the Server is running at "asset-hash-app" diff --git a/middleman-core/fixtures/asset-hash-app/source/index.html.erb b/middleman-core/fixtures/asset-hash-app/source/index.html.erb index 59a9c54f..de017654 100644 --- a/middleman-core/fixtures/asset-hash-app/source/index.html.erb +++ b/middleman-core/fixtures/asset-hash-app/source/index.html.erb @@ -4,6 +4,9 @@

Image url:

+<%= image_tag('100px.jpg?test') %> +<%= image_tag('100px.jpg?#test') %> +<%= image_tag('100px.jpg#test') %>

Ignored path:

diff --git a/middleman-core/fixtures/asset-hash-app/source/layout.erb b/middleman-core/fixtures/asset-hash-app/source/layout.erb index a21259a3..3a1079a7 100644 --- a/middleman-core/fixtures/asset-hash-app/source/layout.erb +++ b/middleman-core/fixtures/asset-hash-app/source/layout.erb @@ -3,16 +3,16 @@ - <%= stylesheet_link_tag "site" %> + <%= stylesheet_link_tag "site", "fragment" %> <%= javascript_include_tag "application" %> <%= yield_content :head %> - +
<%= yield %>
- + diff --git a/middleman-core/fixtures/asset-hash-app/source/other.html.erb b/middleman-core/fixtures/asset-hash-app/source/other.html.erb index 2e6f276f..cc258936 100644 --- a/middleman-core/fixtures/asset-hash-app/source/other.html.erb +++ b/middleman-core/fixtures/asset-hash-app/source/other.html.erb @@ -1,2 +1,5 @@

Image url:

+ + + diff --git a/middleman-core/fixtures/asset-hash-app/source/stylesheets/fragment.css.scss b/middleman-core/fixtures/asset-hash-app/source/stylesheets/fragment.css.scss new file mode 100644 index 00000000..4e175489 --- /dev/null +++ b/middleman-core/fixtures/asset-hash-app/source/stylesheets/fragment.css.scss @@ -0,0 +1,7 @@ +#main { + padding: 50px; + background-image: image-url('100px.jpg'); + background-image: image-url('100px.jpg?test'); + background-image: image-url('100px.jpg?#test'); + background-image: image-url('100px.jpg#test'); +} diff --git a/middleman-core/fixtures/asset-hash-host-app/source/index.html.erb b/middleman-core/fixtures/asset-hash-host-app/source/index.html.erb index 3616ba64..ea81c790 100644 --- a/middleman-core/fixtures/asset-hash-host-app/source/index.html.erb +++ b/middleman-core/fixtures/asset-hash-host-app/source/index.html.erb @@ -4,3 +4,6 @@

Image url:

<%= image_tag('100px.jpg') %> +<%= image_tag('100px.jpg?test') %> +<%= image_tag('100px.jpg?#test') %> +<%= image_tag('100px.jpg#test') %> diff --git a/middleman-core/fixtures/asset-hash-host-app/source/layout.erb b/middleman-core/fixtures/asset-hash-host-app/source/layout.erb index 443b3645..a6aa4a0c 100644 --- a/middleman-core/fixtures/asset-hash-host-app/source/layout.erb +++ b/middleman-core/fixtures/asset-hash-host-app/source/layout.erb @@ -3,15 +3,15 @@ - <%= stylesheet_link_tag "site" %> + <%= stylesheet_link_tag "site", "fragment" %> <%= yield_content :head %> - +
<%= yield %>
- + diff --git a/middleman-core/fixtures/asset-hash-host-app/source/other.html.erb b/middleman-core/fixtures/asset-hash-host-app/source/other.html.erb index 2e6f276f..cc258936 100644 --- a/middleman-core/fixtures/asset-hash-host-app/source/other.html.erb +++ b/middleman-core/fixtures/asset-hash-host-app/source/other.html.erb @@ -1,2 +1,5 @@

Image url:

+ + + diff --git a/middleman-core/fixtures/asset-hash-host-app/source/stylesheets/fragment.css.scss b/middleman-core/fixtures/asset-hash-host-app/source/stylesheets/fragment.css.scss index b169b432..4e175489 100644 --- a/middleman-core/fixtures/asset-hash-host-app/source/stylesheets/fragment.css.scss +++ b/middleman-core/fixtures/asset-hash-host-app/source/stylesheets/fragment.css.scss @@ -1,4 +1,7 @@ #main { padding: 50px; + background-image: image-url('100px.jpg'); + background-image: image-url('100px.jpg?test'); + background-image: image-url('100px.jpg?#test'); background-image: image-url('100px.jpg#test'); } diff --git a/middleman-core/lib/middleman-core/util.rb b/middleman-core/lib/middleman-core/util.rb index db29cf92..1ea5ace8 100644 --- a/middleman-core/lib/middleman-core/util.rb +++ b/middleman-core/lib/middleman-core/util.rb @@ -201,31 +201,37 @@ module Middleman Contract IsA['Middleman::Application'], String, String, Hash => String def asset_url(app, path, prefix='', options={}) # Don't touch assets which already have a full path - if path.include?('//') || path.start_with?('data:') - path - else # rewrite paths to use their destination path - result = if resource = app.sitemap.find_resource_by_destination_path(url_for(app, path)) + return path if path.include?('//') || path.start_with?('data:') + + uri = URI(path) + path = uri.path + + result = if resource = app.sitemap.find_resource_by_destination_path(url_for(app, path)) + resource.url + else + path = File.join(prefix, path) + if resource = app.sitemap.find_resource_by_path(path) resource.url else - path = File.join(prefix, path) - if resource = app.sitemap.find_resource_by_path(path) - resource.url - else - File.join(app.config[:http_prefix], path) - end - end - - 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 + File.join(app.config[:http_prefix], path) 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 + + result_uri = URI(final_result) + result_uri.query = uri.query + result_uri.fragment = uri.fragment + result_uri.to_s end # Given a source path (referenced either absolutely or relatively)