mirror of
https://github.com/wahyd4/gitlabhq.git
synced 2026-08-26 04:56:16 +10:00
Merge branch '18582-banzai-filter-external-link-filter' into 'master'
Banzai::Filter::ExternalLinkFilter use XPath See merge request !4702
This commit is contained in:
committed by
Robert Speicher
parent
f4599c6f7a
commit
c804fef77a
@@ -3,17 +3,8 @@ module Banzai
|
||||
# HTML Filter to modify the attributes of external links
|
||||
class ExternalLinkFilter < HTML::Pipeline::Filter
|
||||
def call
|
||||
doc.search('a').each do |node|
|
||||
link = node.attr('href')
|
||||
|
||||
next unless link
|
||||
|
||||
# Skip non-HTTP(S) links
|
||||
next unless link.start_with?('http')
|
||||
|
||||
# Skip internal links
|
||||
next if link.start_with?(internal_url)
|
||||
|
||||
# Skip non-HTTP(S) links and internal links
|
||||
doc.xpath("descendant-or-self::a[starts-with(@href, 'http') and not(starts-with(@href, '#{internal_url}'))]").each do |node|
|
||||
node.set_attribute('rel', 'nofollow noreferrer')
|
||||
node.set_attribute('target', '_blank')
|
||||
end
|
||||
|
||||
@@ -19,19 +19,31 @@ describe Banzai::Filter::ExternalLinkFilter, lib: true do
|
||||
expect(filter(act).to_html).to eq exp
|
||||
end
|
||||
|
||||
it 'adds rel="nofollow" to external links' do
|
||||
act = %q(<a href="https://google.com/">Google</a>)
|
||||
doc = filter(act)
|
||||
context 'for root links on document' do
|
||||
let(:doc) { filter %q(<a href="https://google.com/">Google</a>) }
|
||||
|
||||
expect(doc.at_css('a')).to have_attribute('rel')
|
||||
expect(doc.at_css('a')['rel']).to include 'nofollow'
|
||||
it 'adds rel="nofollow" to external links' do
|
||||
expect(doc.at_css('a')).to have_attribute('rel')
|
||||
expect(doc.at_css('a')['rel']).to include 'nofollow'
|
||||
end
|
||||
|
||||
it 'adds rel="noreferrer" to external links' do
|
||||
expect(doc.at_css('a')).to have_attribute('rel')
|
||||
expect(doc.at_css('a')['rel']).to include 'noreferrer'
|
||||
end
|
||||
end
|
||||
|
||||
it 'adds rel="noreferrer" to external links' do
|
||||
act = %q(<a href="https://google.com/">Google</a>)
|
||||
doc = filter(act)
|
||||
context 'for nested links on document' do
|
||||
let(:doc) { filter %q(<p><a href="https://google.com/">Google</a></p>) }
|
||||
|
||||
expect(doc.at_css('a')).to have_attribute('rel')
|
||||
expect(doc.at_css('a')['rel']).to include 'noreferrer'
|
||||
it 'adds rel="nofollow" to external links' do
|
||||
expect(doc.at_css('a')).to have_attribute('rel')
|
||||
expect(doc.at_css('a')['rel']).to include 'nofollow'
|
||||
end
|
||||
|
||||
it 'adds rel="noreferrer" to external links' do
|
||||
expect(doc.at_css('a')).to have_attribute('rel')
|
||||
expect(doc.at_css('a')['rel']).to include 'noreferrer'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user