From 4740159a3a687129a148d27aba28f6741e0fe11d Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Wed, 22 Apr 2015 09:41:03 -0700 Subject: [PATCH] Use Addressable to improve inline url detection and rewriting. Only rewrite relative paths. Closes #1499 --- middleman-core/features/asset_host.feature | 15 ++------------- .../asset-host-app/source/asset_host.html.erb | 2 ++ middleman-core/lib/middleman-core/util.rb | 13 ++++++++++--- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/middleman-core/features/asset_host.feature b/middleman-core/features/asset_host.feature index 19c6b14d..bcd4c06c 100644 --- a/middleman-core/features/asset_host.feature +++ b/middleman-core/features/asset_host.feature @@ -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" diff --git a/middleman-core/fixtures/asset-host-app/source/asset_host.html.erb b/middleman-core/fixtures/asset-host-app/source/asset_host.html.erb index dbef372a..3d7be098 100755 --- a/middleman-core/fixtures/asset-host-app/source/asset_host.html.erb +++ b/middleman-core/fixtures/asset-host-app/source/asset_host.html.erb @@ -1,3 +1,5 @@ + + <%= image_tag "blank0.gif" %> <%= image_tag "blank1.gif" %> <%= image_tag "blank2.gif" %> diff --git a/middleman-core/lib/middleman-core/util.rb b/middleman-core/lib/middleman-core/util.rb index f9710011..679289a0 100644 --- a/middleman-core/lib/middleman-core/util.rb +++ b/middleman-core/lib/middleman-core/util.rb @@ -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,9 +324,15 @@ module Middleman opening_character = $1 asset_path = $2 - if result = yield(asset_path) - "#{opening_character}#{result}" - else + begin + uri = ::Addressable::URI.parse(asset_path) + + if uri.relative? && result = yield(asset_path) + "#{opening_character}#{result}" + else + match + end + rescue match end end