From 7be19db42fd51da23b1ef658263897213b624500 Mon Sep 17 00:00:00 2001 From: Alfredo Sumaran Date: Wed, 8 Jun 2016 02:02:55 -0500 Subject: [PATCH 1/4] Set target="_blank" for external links --- lib/banzai/filter/external_link_filter.rb | 3 +++ spec/features/markdown_spec.rb | 10 ++++++++++ spec/fixtures/markdown.md.erb | 2 +- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/banzai/filter/external_link_filter.rb b/lib/banzai/filter/external_link_filter.rb index 38c4219518..27ca38e6d4 100644 --- a/lib/banzai/filter/external_link_filter.rb +++ b/lib/banzai/filter/external_link_filter.rb @@ -15,6 +15,9 @@ module Banzai next if link.start_with?(internal_url) node.set_attribute('rel', 'nofollow noreferrer') + + # Open external links on a new tab + node.set_attribute('target', '_blank') end doc diff --git a/spec/features/markdown_spec.rb b/spec/features/markdown_spec.rb index 7663d19335..cabccdacf8 100644 --- a/spec/features/markdown_spec.rb +++ b/spec/features/markdown_spec.rb @@ -173,10 +173,20 @@ describe 'GitLab Markdown', feature: true do expect(link.attr('rel')).to include('noreferrer') end + it 'adds _blank to target attribute for external links' do + link = doc.at_css('a:contains("Google")') + expect(link.attr('target')).to match('_blank') + end + it 'ignores internal link' do link = doc.at_css('a:contains("GitLab Root")') expect(link.attr('rel')).not_to match 'nofollow' end + + it 'does not set _blank to target attribute for internal links' do + link = doc.at_css('a:contains("GitLab Root")') + expect(link.attr('target')).not_to match '_blank' + end end end diff --git a/spec/fixtures/markdown.md.erb b/spec/fixtures/markdown.md.erb index 34ce7c4f03..c75d28d980 100644 --- a/spec/fixtures/markdown.md.erb +++ b/spec/fixtures/markdown.md.erb @@ -136,7 +136,7 @@ But it shouldn't autolink text inside certain tags: ### ExternalLinkFilter -External links get a `rel="nofollow"` attribute: +External links get a `rel="nofollow noreferrer"` and `target="_blank"` attributes: - [Google](https://google.com/) - [GitLab Root](<%= Gitlab.config.gitlab.url %>) From 064cff13fd7d02672695b4ac1722bbd6df35f1d4 Mon Sep 17 00:00:00 2001 From: Alfredo Sumaran Date: Wed, 8 Jun 2016 12:14:56 -0500 Subject: [PATCH 2/4] Remove obvious comment and extra line --- lib/banzai/filter/external_link_filter.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/banzai/filter/external_link_filter.rb b/lib/banzai/filter/external_link_filter.rb index 27ca38e6d4..f73ecfc941 100644 --- a/lib/banzai/filter/external_link_filter.rb +++ b/lib/banzai/filter/external_link_filter.rb @@ -15,8 +15,6 @@ module Banzai next if link.start_with?(internal_url) node.set_attribute('rel', 'nofollow noreferrer') - - # Open external links on a new tab node.set_attribute('target', '_blank') end From 27ada5aa4690affd04ada606d15a44a598184c57 Mon Sep 17 00:00:00 2001 From: Alfredo Sumaran Date: Wed, 8 Jun 2016 12:20:24 -0500 Subject: [PATCH 3/4] Combine tests for internal links --- spec/features/markdown_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/features/markdown_spec.rb b/spec/features/markdown_spec.rb index cabccdacf8..1193cae5a2 100644 --- a/spec/features/markdown_spec.rb +++ b/spec/features/markdown_spec.rb @@ -165,26 +165,26 @@ describe 'GitLab Markdown', feature: true do describe 'ExternalLinkFilter' do it 'adds nofollow to external link' do link = doc.at_css('a:contains("Google")') + expect(link.attr('rel')).to include('nofollow') end it 'adds noreferrer to external link' do link = doc.at_css('a:contains("Google")') + expect(link.attr('rel')).to include('noreferrer') end it 'adds _blank to target attribute for external links' do link = doc.at_css('a:contains("Google")') + expect(link.attr('target')).to match('_blank') end it 'ignores internal link' do link = doc.at_css('a:contains("GitLab Root")') - expect(link.attr('rel')).not_to match 'nofollow' - end - it 'does not set _blank to target attribute for internal links' do - link = doc.at_css('a:contains("GitLab Root")') + expect(link.attr('rel')).not_to match 'nofollow' expect(link.attr('target')).not_to match '_blank' end end From 26d4e633413d359c278262c9d9b2d3b94b0792e2 Mon Sep 17 00:00:00 2001 From: Alfredo Sumaran Date: Wed, 8 Jun 2016 13:02:32 -0500 Subject: [PATCH 4/4] Update CHANGELOG --- CHANGELOG | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG b/CHANGELOG index 657c6f4833..3ae881da36 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -50,6 +50,7 @@ v 8.9.0 (unreleased) - An indicator is now displayed at the top of the comment field for confidential issues. - RepositoryCheck::SingleRepositoryWorker public and private methods are now instrumented - Improve issuables APIs performance when accessing notes !4471 + - External links now open in a new tab v 8.8.4 (unreleased) - Ensure branch cleanup regardless of whether the GitHub import process succeeds