diff options
author | Douwe Maan <douwe@gitlab.com> | 2017-11-10 09:28:15 +0000 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2017-11-10 09:28:15 +0000 |
commit | e482a4d59dae6437a64ae4ccbe6976ffced7b9ba (patch) | |
tree | 807f6a2613cc115e76cd4ea96508c62a451b07ec | |
parent | b488c1616b2a209a9f4b6b37f77cab6582954ebb (diff) | |
parent | fce34b4ba80bd4dfda770ea359e09da115caf075 (diff) | |
download | gitlab-ce-e482a4d59dae6437a64ae4ccbe6976ffced7b9ba.tar.gz |
Merge branch 'tz-no-cdn-for-svg-sprites' into 'master'
Icon Sprite URL needs to be local even if asset_host is set
See merge request gitlab-org/gitlab-ce!15289
-rw-r--r-- | app/helpers/icons_helper.rb | 9 | ||||
-rw-r--r-- | lib/gitlab/gon_helper.rb | 2 | ||||
-rw-r--r-- | spec/helpers/icons_helper_spec.rb | 28 |
3 files changed, 34 insertions, 5 deletions
diff --git a/app/helpers/icons_helper.rb b/app/helpers/icons_helper.rb index ec779c1c447..c6a83f21ceb 100644 --- a/app/helpers/icons_helper.rb +++ b/app/helpers/icons_helper.rb @@ -23,10 +23,17 @@ module IconsHelper render "shared/icons/#{icon_name}.svg", size: size 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 = Gitlab.config.gitlab.url if ActionController::Base.asset_host + ActionController::Base.helpers.image_path('icons.svg', host: sprite_base_url) + end + def sprite_icon(icon_name, size: nil, css_class: nil) css_classes = size ? "s#{size}" : "" 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 def audit_icon(names, options = {}) diff --git a/lib/gitlab/gon_helper.rb b/lib/gitlab/gon_helper.rb index 3a666c2268b..dfcdfc307b6 100644 --- a/lib/gitlab/gon_helper.rb +++ b/lib/gitlab/gon_helper.rb @@ -20,7 +20,7 @@ module Gitlab gon.gitlab_url = Gitlab.config.gitlab.url gon.revision = Gitlab::REVISION 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 gon.current_user_id = current_user.id diff --git a/spec/helpers/icons_helper_spec.rb b/spec/helpers/icons_helper_spec.rb index 3d79dac284f..2f23ed55d99 100644 --- a/spec/helpers/icons_helper_spec.rb +++ b/spec/helpers/icons_helper_spec.rb @@ -1,6 +1,8 @@ require 'spec_helper' describe IconsHelper do + let(:icons_path) { ActionController::Base.helpers.image_path("icons.svg") } + describe 'icon' do it 'returns aria-hidden by default' do star = icon('star') @@ -16,22 +18,42 @@ describe IconsHelper do end end + describe 'sprite_icon_path' do + it 'returns relative path' do + expect(sprite_icon_path) + .to eq icons_path + 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 + expect(sprite_icon_path) + .to eq ActionController::Base.helpers.image_path("icons.svg", host: Gitlab.config.gitlab.url) + end + end + end + describe 'sprite_icon' do icon_name = 'clock' it 'returns svg icon html' do expect(sprite_icon(icon_name).to_s) - .to eq "<svg><use xlink:href=\"/images/icons.svg##{icon_name}\"></use></svg>" + .to eq "<svg><use xlink:href=\"#{icons_path}##{icon_name}\"></use></svg>" end it 'returns svg icon html + size classes' do expect(sprite_icon(icon_name, size: 72).to_s) - .to eq "<svg class=\"s72\"><use xlink:href=\"/images/icons.svg##{icon_name}\"></use></svg>" + .to eq "<svg class=\"s72\"><use xlink:href=\"#{icons_path}##{icon_name}\"></use></svg>" end it 'returns svg icon html + size classes + additional class' do expect(sprite_icon(icon_name, size: 72, css_class: 'icon-danger').to_s) - .to eq "<svg class=\"s72 icon-danger\"><use xlink:href=\"/images/icons.svg##{icon_name}\"></use></svg>" + .to eq "<svg class=\"s72 icon-danger\"><use xlink:href=\"#{icons_path}##{icon_name}\"></use></svg>" end end |