summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorRobert Speicher <rspeicher@gmail.com>2015-04-12 17:34:05 -0400
committerRobert Speicher <rspeicher@gmail.com>2015-04-12 17:48:28 -0400
commit975f1e6ec8df4b50118fb3a757c2a4a50a12e98d (patch)
tree041364768ef0d26cf5830b38b9546456320142fb /app
parent8d9d63b25056e889a072bfcb55eb485beb41389d (diff)
downloadgitlab-ce-975f1e6ec8df4b50118fb3a757c2a4a50a12e98d.tar.gz
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.
Diffstat (limited to 'app')
-rw-r--r--app/helpers/application_helper.rb44
1 files changed, 23 insertions, 21 deletions
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index b5b0015542c..6aef82354ab 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -236,33 +236,35 @@ module ApplicationHelper
Gitlab::MarkdownHelper.gitlab_markdown?(filename)
end
- def link_to(name = nil, options = nil, html_options = nil, &block)
- begin
- uri = URI(options)
- host = uri.host
- absolute_uri = uri.absolute?
- rescue URI::InvalidURIError, ArgumentError
- 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'
+ # Overrides ActionView::Helpers::UrlHelper#link_to to add `rel="nofollow"` to
+ # external links
+ def link_to(name = nil, options = nil, html_options = {})
+ if options.kind_of?(String)
+ if options[0] != '/' && options[0] != '#'
+ html_options = add_nofollow(options, html_options)
end
end
super
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)
auto_link ERB::Util.html_escape(text), link: :urls
end