Commit 25eea058 authored by Tim Zallmann's avatar Tim Zallmann

Icon Sprite URL is also local even if asset_host is set

parent 20ac30a7
module IconsHelper module IconsHelper
extend self extend self
include Gitlab::CurrentSettings
include FontAwesome::Rails::IconHelper include FontAwesome::Rails::IconHelper
# Creates an icon tag given icon name(s) and possible icon modifiers. # Creates an icon tag given icon name(s) and possible icon modifiers.
...@@ -23,10 +24,17 @@ module IconsHelper ...@@ -23,10 +24,17 @@ module IconsHelper
render "shared/icons/#{icon_name}.svg", size: size render "shared/icons/#{icon_name}.svg", size: size
end end
def sprite_icon_path
# SVG Sprites currently don't work across domains, so in the case of a CDN
# we have to set the current path deliberately to prevent addition of asset_host
sprite_base_url = ActionController::Base.asset_host.present? ? Gitlab.config.gitlab.url : nil
ActionController::Base.helpers.image_path('icons.svg', host: sprite_base_url)
end
def sprite_icon(icon_name, size: nil, css_class: nil) def sprite_icon(icon_name, size: nil, css_class: nil)
css_classes = size ? "s#{size}" : "" css_classes = size ? "s#{size}" : ""
css_classes << " #{css_class}" unless css_class.blank? css_classes << " #{css_class}" unless css_class.blank?
content_tag(:svg, content_tag(:use, "", { "xlink:href" => "#{image_path('icons.svg')}##{icon_name}" } ), class: css_classes.empty? ? nil : css_classes) content_tag(:svg, content_tag(:use, "", { "xlink:href" => "#{sprite_icon_path()}##{icon_name}" } ), class: css_classes.empty? ? nil : css_classes)
end end
def audit_icon(names, options = {}) def audit_icon(names, options = {})
......
...@@ -20,7 +20,7 @@ module Gitlab ...@@ -20,7 +20,7 @@ module Gitlab
gon.gitlab_url = Gitlab.config.gitlab.url gon.gitlab_url = Gitlab.config.gitlab.url
gon.revision = Gitlab::REVISION gon.revision = Gitlab::REVISION
gon.gitlab_logo = ActionController::Base.helpers.asset_path('gitlab_logo.png') gon.gitlab_logo = ActionController::Base.helpers.asset_path('gitlab_logo.png')
gon.sprite_icons = ActionController::Base.helpers.asset_path('icons.svg') gon.sprite_icons = IconsHelper.sprite_icon_path
if current_user if current_user
gon.current_user_id = current_user.id gon.current_user_id = current_user.id
......
...@@ -16,6 +16,27 @@ describe IconsHelper do ...@@ -16,6 +16,27 @@ describe IconsHelper do
end end
end end
describe 'sprite_icon' do
it 'returns relative path' do
expect(sprite_icon_path())
.to eq "/image/icons.svg"
end
context 'when an asset_host is set in the config it will return an absolute local URL' do
let(:asset_host) { 'http://assets' }
before do
allow(ActionController::Base).to receive(:asset_host).and_return(asset_host)
end
it 'returns an absolute URL on that asset host' do
gitlab_url = Gitlab.config.gitlab.url
expect(sprite_icon_path())
.to eq "#{gitlab_url}/image/icons.svg"
end
end
end
describe 'sprite_icon' do describe 'sprite_icon' do
icon_name = 'clock' icon_name = 'clock'
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment