Commit 4632983c authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'rs-link-to-performance' into 'master'

Speed up the overridden `link_to` helper

Only bothers to check the provided link's external status if it's a String that doesn't begin with a path or anchor character.

See merge request !1767
parents 4ab717ea cd590bf1
...@@ -236,33 +236,35 @@ module ApplicationHelper ...@@ -236,33 +236,35 @@ module ApplicationHelper
Gitlab::MarkdownHelper.gitlab_markdown?(filename) Gitlab::MarkdownHelper.gitlab_markdown?(filename)
end end
def link_to(name = nil, options = nil, html_options = nil, &block) # Overrides ActionView::Helpers::UrlHelper#link_to to add `rel="nofollow"` to
begin # external links
uri = URI(options) def link_to(name = nil, options = nil, html_options = {})
host = uri.host if options.kind_of?(String)
absolute_uri = uri.absolute? if !options.start_with?('#', '/')
rescue URI::InvalidURIError, ArgumentError html_options = add_nofollow(options, html_options)
host = nil
absolute_uri = nil
end
# Add 'nofollow' only to external links
if host && host != Gitlab.config.gitlab.host && absolute_uri
if html_options
if html_options[:rel]
html_options[:rel] << ' nofollow'
else
html_options.merge!(rel: 'nofollow')
end
else
html_options = Hash.new
html_options[:rel] = 'nofollow'
end end
end end
super super
end end
# Add `"rel=nofollow"` to external links
#
# link - String link to check
# html_options - Hash of `html_options` passed to `link_to`
#
# Returns `html_options`, adding `rel: nofollow` for external links
def add_nofollow(link, html_options = {})
uri = URI(link)
if uri && uri.absolute? && uri.host != Gitlab.config.gitlab.host
rel = html_options.fetch(:rel, '')
html_options[:rel] = (rel + ' nofollow').strip
end
html_options
end
def escaped_autolink(text) def escaped_autolink(text)
auto_link ERB::Util.html_escape(text), link: :urls auto_link ERB::Util.html_escape(text), link: :urls
end end
......
...@@ -225,25 +225,29 @@ describe ApplicationHelper do ...@@ -225,25 +225,29 @@ describe ApplicationHelper do
end end
describe 'link_to' do describe 'link_to' do
it 'should not include rel=nofollow for internal links' do it 'should not include rel=nofollow for internal links' do
expect(link_to('Home', root_path)).to eq("<a href=\"/\">Home</a>") expect(link_to('Home', root_path)).to eq('<a href="/">Home</a>')
end end
it 'should include rel=nofollow for external links' do it 'should include rel=nofollow for external links' do
expect(link_to('Example', 'http://www.example.com')).to eq("<a href=\"http://www.example.com\" rel=\"nofollow\">Example</a>") expect(link_to('Example', 'http://www.example.com')).
to eq '<a href="http://www.example.com" rel="nofollow">Example</a>'
end
it 'should include rel=nofollow for external links and honor existing html_options' do
expect(link_to('Example', 'http://www.example.com', class: 'toggle', data: {toggle: 'dropdown'}))
.to eq '<a class="toggle" data-toggle="dropdown" href="http://www.example.com" rel="nofollow">Example</a>'
end end
it 'should include re=nofollow for external links and honor existing html_options' do it 'should include rel=nofollow for external links and preserve other rel values' do
expect( expect(link_to('Example', 'http://www.example.com', rel: 'noreferrer'))
link_to('Example', 'http://www.example.com', class: 'toggle', data: {toggle: 'dropdown'}) .to eq '<a href="http://www.example.com" rel="noreferrer nofollow">Example</a>'
).to eq("<a class=\"toggle\" data-toggle=\"dropdown\" href=\"http://www.example.com\" rel=\"nofollow\">Example</a>")
end end
it 'should include rel=nofollow for external links and preserver other rel values' do it 'should not include rel=nofollow for external links on the same host as GitLab' do
expect( expect(Gitlab.config.gitlab).to receive(:host).and_return('example.foo')
link_to('Example', 'http://www.example.com', rel: 'noreferrer') expect(link_to('Example', 'http://example.foo/bar')).
).to eq("<a href=\"http://www.example.com\" rel=\"noreferrer nofollow\">Example</a>") to eq '<a href="http://example.foo/bar">Example</a>'
end end
end end
......
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