Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
gitlab-ce
Commits
ae6a54f7
Commit
ae6a54f7
authored
Jun 16, 2016
by
Paco Guzman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Banzai::Filter::ExternalLinkFilter use XPath
parent
c369cc8b
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
26 additions
and
22 deletions
+26
-22
CHANGELOG
CHANGELOG
+1
-0
lib/banzai/filter/external_link_filter.rb
lib/banzai/filter/external_link_filter.rb
+2
-11
spec/lib/banzai/filter/external_link_filter_spec.rb
spec/lib/banzai/filter/external_link_filter_spec.rb
+23
-11
No files found.
CHANGELOG
View file @
ae6a54f7
...
@@ -119,6 +119,7 @@ v 8.8.5
...
@@ -119,6 +119,7 @@ v 8.8.5
- Forbid scripting for wiki files
- Forbid scripting for wiki files
- Only show notes through JSON on confidential issues that the user has access to
- Only show notes through JSON on confidential issues that the user has access to
- Banzai::Filter::UploadLinkFilter use XPath instead CSS expressions
- Banzai::Filter::UploadLinkFilter use XPath instead CSS expressions
- Banzai::Filter::ExternalLinkFilter use XPath instead CSS expressions
v 8.8.4
v 8.8.4
- Fix LDAP-based login for users with 2FA enabled. !4493
- Fix LDAP-based login for users with 2FA enabled. !4493
...
...
lib/banzai/filter/external_link_filter.rb
View file @
ae6a54f7
...
@@ -3,17 +3,8 @@ module Banzai
...
@@ -3,17 +3,8 @@ module Banzai
# HTML Filter to modify the attributes of external links
# HTML Filter to modify the attributes of external links
class
ExternalLinkFilter
<
HTML
::
Pipeline
::
Filter
class
ExternalLinkFilter
<
HTML
::
Pipeline
::
Filter
def
call
def
call
doc
.
search
(
'a'
).
each
do
|
node
|
# Skip non-HTTP(S) links and internal links
link
=
node
.
attr
(
'href'
)
doc
.
xpath
(
"descendant-or-self::a[starts-with(@href, 'http') and not(starts-with(@href, '
#{
internal_url
}
'))]"
).
each
do
|
node
|
next
unless
link
# Skip non-HTTP(S) links
next
unless
link
.
start_with?
(
'http'
)
# Skip internal links
next
if
link
.
start_with?
(
internal_url
)
node
.
set_attribute
(
'rel'
,
'nofollow noreferrer'
)
node
.
set_attribute
(
'rel'
,
'nofollow noreferrer'
)
node
.
set_attribute
(
'target'
,
'_blank'
)
node
.
set_attribute
(
'target'
,
'_blank'
)
end
end
...
...
spec/lib/banzai/filter/external_link_filter_spec.rb
View file @
ae6a54f7
...
@@ -19,19 +19,31 @@ describe Banzai::Filter::ExternalLinkFilter, lib: true do
...
@@ -19,19 +19,31 @@ describe Banzai::Filter::ExternalLinkFilter, lib: true do
expect
(
filter
(
act
).
to_html
).
to
eq
exp
expect
(
filter
(
act
).
to_html
).
to
eq
exp
end
end
it
'adds rel="nofollow" to external links'
do
context
'for root links on document'
do
act
=
%q(<a href="https://google.com/">Google</a>)
let
(
:doc
)
{
filter
%q(<a href="https://google.com/">Google</a>)
}
doc
=
filter
(
act
)
it
'adds rel="nofollow" to external links'
do
expect
(
doc
.
at_css
(
'a'
)).
to
have_attribute
(
'rel'
)
expect
(
doc
.
at_css
(
'a'
)).
to
have_attribute
(
'rel'
)
expect
(
doc
.
at_css
(
'a'
)[
'rel'
]).
to
include
'nofollow'
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
it
'adds rel="noreferrer" to external links'
do
context
'for nested links on document'
do
act
=
%q(<a href="https://google.com/">Google</a>)
let
(
:doc
)
{
filter
%q(<p><a href="https://google.com/">Google</a></p>)
}
doc
=
filter
(
act
)
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
expect
(
doc
.
at_css
(
'a'
)).
to
have_attribute
(
'rel'
)
it
'adds rel="noreferrer" to external links'
do
expect
(
doc
.
at_css
(
'a'
)[
'rel'
]).
to
include
'noreferrer'
expect
(
doc
.
at_css
(
'a'
)).
to
have_attribute
(
'rel'
)
expect
(
doc
.
at_css
(
'a'
)[
'rel'
]).
to
include
'noreferrer'
end
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment