Add relative argument to stylesheet and javascript helpers. Closes #1539

This commit is contained in:
Thomas Reynolds 2015-06-22 10:37:17 -07:00
parent e09f7fadb5
commit 3fca2c6961
8 changed files with 83 additions and 13 deletions

View file

@ -8,6 +8,7 @@ master
* Empower link_to in a i18n context.
* Add retina files support on automatic_image_sizes
* Fix woff/woff2 confusion in asset hashing.
* Support `relative: false` on `stylesheet_link_tag` and `javascript_include_tag`
3.3.12
===

View file

@ -21,6 +21,12 @@ Feature: Relative Assets
Given "relative_assets" feature is "disabled"
And the Server is running at "relative-assets-app"
When I go to "/relative_image.html"
Then I should see '"/stylesheets/relative_assets.css"'
Then I should see '"/javascripts/app.js"'
Then I should see "/images/blank.gif"
When I go to "/absolute_image_relative_css.html"
Then I should see '"stylesheets/relative_assets.css"'
Then I should see '"javascripts/app.js"'
Then I should see "/images/blank.gif"
Scenario: Rendering css with the feature enabled
@ -54,6 +60,11 @@ Feature: Relative Assets
Given "relative_assets" feature is "enabled"
And the Server is running at "relative-assets-app"
When I go to "/relative_image.html"
Then I should see '"stylesheets/relative_assets.css"'
Then I should see '"javascripts/app.js"'
When I go to "/relative_image_absolute_css.html"
Then I should see '"/stylesheets/relative_assets.css"'
Then I should see '"/javascripts/app.js"'
Then I should not see "/images/blank.gif"
And I should see "images/blank.gif"

View file

@ -0,0 +1,9 @@
<html>
<head>
<%= stylesheet_link_tag :relative_assets, relative: true %>
<%= javascript_include_tag :app, relative: true %>
</head>
<body>
<%= image_tag "blank.gif" %>
</body>
</html>

View file

@ -0,0 +1,3 @@
function hello() {
console.log('world');
}

View file

@ -1,6 +1,7 @@
<html>
<head>
<%= stylesheet_link_tag :relative_assets %>
<%= javascript_include_tag :app %>
</head>
<body>
<%= image_tag "blank.gif" %>

View file

@ -0,0 +1,9 @@
<html>
<head>
<%= stylesheet_link_tag :relative_assets, relative: false %>
<%= javascript_include_tag :app, relative: false %>
</head>
<body>
<%= image_tag "blank.gif" %>
</body>
</html>

View file

@ -99,6 +99,36 @@ class Middleman::CoreExtensions::DefaultHelpers < ::Middleman::Extension
end
end
# Override helper to add `relative` opt-out.
def stylesheet_link_tag(*sources)
options = {
:rel => 'stylesheet'
}.update(sources.extract_options!.symbolize_keys)
path_options = {}
path_options[:relative] = options.delete(:relative) if options.key?(:relative)
sources.flatten.inject(ActiveSupport::SafeBuffer.new) do |all,source|
all << tag(:link, {
href: asset_path(:css, source, path_options)
}.update(options))
end
end
# Override helper to add `relative` opt-out.
def javascript_include_tag(*sources)
options = sources.extract_options!.symbolize_keys
path_options = {}
path_options[:relative] = options.delete(:relative) if options.key?(:relative)
sources.flatten.inject(::ActiveSupport::SafeBuffer.new) do |all,source|
all << content_tag(:script, nil, {
src: asset_path(:js, source, path_options)
}.update(options))
end
end
# Output a stylesheet link tag based on the current path
#
# @param [Symbol] asset_ext The type of asset
@ -153,8 +183,9 @@ class Middleman::CoreExtensions::DefaultHelpers < ::Middleman::Extension
#
# @param [Symbol] kind The type of file
# @param [String] source The path to the file
# @param [Hash] options Additional options.
# @return [String]
def asset_path(kind, source)
def asset_path(kind, source, options={})
return source if source.to_s.include?('//') || source.to_s.start_with?('data:')
asset_folder = case kind
when :css
@ -174,29 +205,38 @@ class Middleman::CoreExtensions::DefaultHelpers < ::Middleman::Extension
source << ".#{kind}" unless ignore_extension || source.end_with?(".#{kind}")
asset_folder = '' if source.start_with?('/') # absolute path
asset_url(source, asset_folder)
asset_url(source, asset_folder, options)
end
# Get the URL of an asset given a type/prefix
#
# @param [String] path The path (such as "photo.jpg")
# @param [String] prefix The type prefix (such as "images")
# @param [Hash] options Additional options.
# @return [String] The fully qualified asset url
def asset_url(path, prefix='')
def asset_url(path, prefix='', options={})
# Don't touch assets which already have a full path
if path.include?('//') || path.start_with?('data:')
if path.include?('//') || path.start_with?('data:') || !current_resource
path
else # rewrite paths to use their destination path
if resource = sitemap.find_resource_by_destination_path(url_for(path))
result = if resource = sitemap.find_resource_by_destination_path(url_for(path))
resource.url
else
path = File.join(prefix, path)
if resource = sitemap.find_resource_by_path(path)
resource.url
else
File.join(config[:http_prefix], path)
end
end
if options[:relative] != true
result
else
current_dir = Pathname('/' + current_resource.destination_path)
Pathname(result).relative_path_from(current_dir.dirname).to_s
end
end
end

View file

@ -13,16 +13,12 @@ class Middleman::Extensions::RelativeAssets < ::Middleman::Extension
# asset_url override for relative assets
# @param [String] path
# @param [String] prefix
# @param [Hash] options Additional options.
# @return [String]
def asset_url(path, prefix='')
path = super(path, prefix)
def asset_url(path, prefix='', options={})
options[:relative] = true unless options.key?(:relative)
if path.include?('//') || path.start_with?('data:') || !current_resource
path
else
current_dir = Pathname('/' + current_resource.destination_path)
Pathname(path).relative_path_from(current_dir.dirname).to_s
end
path = super(path, prefix, options)
end
end
end