Use Addressable to improve inline url detection and rewriting. Only rewrite relative paths. Closes #1499

This commit is contained in:
Thomas Reynolds 2015-04-22 09:41:03 -07:00
parent 40e01b0b21
commit 4740159a3a
3 changed files with 14 additions and 16 deletions

View file

@ -1,17 +1,4 @@
Feature: Alternate between multiple asset hosts
In order to speed up page loading
Scenario: Set single host globally
Given a fixture app "asset-host-app"
And a file named "config.rb" with:
"""
activate :asset_host, host: "http://assets1.example.com"
"""
And the Server is running
When I go to "/asset_host.html"
Then I should see "http://assets1"
When I go to "/stylesheets/asset_host.css"
Then I should see "http://assets1"
Scenario: Set single host with inline-option
Given a fixture app "asset-host-app"
@ -21,6 +8,7 @@ Feature: Alternate between multiple asset hosts
"""
And the Server is running
When I go to "/asset_host.html"
Then I should see 'src="https://code.jquery.com/jquery-2.1.3.min.js"'
Then I should see content matching %r{http://assets1.example.com/}
Then I should not see content matching %r{http://assets1.example.com//}
When I go to "/stylesheets/asset_host.css"
@ -37,6 +25,7 @@ Feature: Alternate between multiple asset hosts
"""
And the Server is running
When I go to "/asset_host.html"
Then I should see 'src="https://code.jquery.com/jquery-2.1.3.min.js"'
Then I should see content matching %r{http://assets1.example.com/}
Then I should not see content matching %r{http://assets1.example.com//}
When I go to "/stylesheets/asset_host.css"

View file

@ -1,3 +1,5 @@
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<%= image_tag "blank0.gif" %>
<%= image_tag "blank1.gif" %>
<%= image_tag "blank2.gif" %>

View file

@ -15,6 +15,7 @@ require 'rack/mime'
require 'middleman-core/contracts'
# For URI templating
require 'addressable/uri'
require 'addressable/template'
require 'active_support/inflector'
require 'active_support/inflector/transliterate'
@ -323,11 +324,17 @@ module Middleman
opening_character = $1
asset_path = $2
if result = yield(asset_path)
begin
uri = ::Addressable::URI.parse(asset_path)
if uri.relative? && result = yield(asset_path)
"#{opening_character}#{result}"
else
match
end
rescue
match
end
end
end