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
Léo-Paul Géneau
gitlab-ce
Commits
4c8d4c62
Commit
4c8d4c62
authored
Jun 03, 2016
by
Douwe Maan
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'banzai-user-filter-queries'
parents
70499125
01575e99
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
99 additions
and
2 deletions
+99
-2
CHANGELOG
CHANGELOG
+1
-0
lib/banzai/filter/reference_filter.rb
lib/banzai/filter/reference_filter.rb
+7
-0
lib/banzai/filter/user_reference_filter.rb
lib/banzai/filter/user_reference_filter.rb
+27
-2
spec/lib/banzai/filter/reference_filter_spec.rb
spec/lib/banzai/filter/reference_filter_spec.rb
+45
-0
spec/lib/banzai/filter/user_reference_filter_spec.rb
spec/lib/banzai/filter/user_reference_filter_spec.rb
+19
-0
No files found.
CHANGELOG
View file @
4c8d4c62
...
@@ -36,6 +36,7 @@ v 8.9.0 (unreleased)
...
@@ -36,6 +36,7 @@ v 8.9.0 (unreleased)
v 8.8.4
v 8.8.4
- Fix todos page throwing errors when you have a project pending deletion
- Fix todos page throwing errors when you have a project pending deletion
- Reduce number of SQL queries when rendering user references
v 8.8.3
v 8.8.3
- Fix 404 page when viewing TODOs that contain milestones or labels in different projects. !4312
- Fix 404 page when viewing TODOs that contain milestones or labels in different projects. !4312
...
...
lib/banzai/filter/reference_filter.rb
View file @
4c8d4c62
...
@@ -68,6 +68,8 @@ module Banzai
...
@@ -68,6 +68,8 @@ module Banzai
# by `ignore_ancestor_query`. Link tags are not processed if they have a
# by `ignore_ancestor_query`. Link tags are not processed if they have a
# "gfm" class or the "href" attribute is empty.
# "gfm" class or the "href" attribute is empty.
def
each_node
def
each_node
return
to_enum
(
__method__
)
unless
block_given?
query
=
%Q{descendant-or-self::text()[not(
#{
ignore_ancestor_query
}
)]
query
=
%Q{descendant-or-self::text()[not(
#{
ignore_ancestor_query
}
)]
| descendant-or-self::a[
| descendant-or-self::a[
not(contains(concat(" ", @class, " "), " gfm ")) and not(@href = "")
not(contains(concat(" ", @class, " "), " gfm ")) and not(@href = "")
...
@@ -78,6 +80,11 @@ module Banzai
...
@@ -78,6 +80,11 @@ module Banzai
end
end
end
end
# Returns an Array containing all HTML nodes.
def
nodes
@nodes
||=
each_node
.
to_a
end
# Yields the link's URL and text whenever the node is a valid <a> tag.
# Yields the link's URL and text whenever the node is a valid <a> tag.
def
yield_valid_link
(
node
)
def
yield_valid_link
(
node
)
link
=
CGI
.
unescape
(
node
.
attr
(
'href'
).
to_s
)
link
=
CGI
.
unescape
(
node
.
attr
(
'href'
).
to_s
)
...
...
lib/banzai/filter/user_reference_filter.rb
View file @
4c8d4c62
...
@@ -29,7 +29,7 @@ module Banzai
...
@@ -29,7 +29,7 @@ module Banzai
ref_pattern
=
User
.
reference_pattern
ref_pattern
=
User
.
reference_pattern
ref_pattern_start
=
/\A
#{
ref_pattern
}
\z/
ref_pattern_start
=
/\A
#{
ref_pattern
}
\z/
each_node
do
|
node
|
nodes
.
each
do
|
node
|
if
text_node?
(
node
)
if
text_node?
(
node
)
replace_text_when_pattern_matches
(
node
,
ref_pattern
)
do
|
content
|
replace_text_when_pattern_matches
(
node
,
ref_pattern
)
do
|
content
|
user_link_filter
(
content
)
user_link_filter
(
content
)
...
@@ -59,7 +59,7 @@ module Banzai
...
@@ -59,7 +59,7 @@ module Banzai
self
.
class
.
references_in
(
text
)
do
|
match
,
username
|
self
.
class
.
references_in
(
text
)
do
|
match
,
username
|
if
username
==
'all'
if
username
==
'all'
link_to_all
(
link_text:
link_text
)
link_to_all
(
link_text:
link_text
)
elsif
namespace
=
Namespace
.
find_by
(
path:
username
)
elsif
namespace
=
namespaces
[
username
]
link_to_namespace
(
namespace
,
link_text:
link_text
)
||
match
link_to_namespace
(
namespace
,
link_text:
link_text
)
||
match
else
else
match
match
...
@@ -67,6 +67,31 @@ module Banzai
...
@@ -67,6 +67,31 @@ module Banzai
end
end
end
end
# Returns a Hash containing all Namespace objects for the username
# references in the current document.
#
# The keys of this Hash are the namespace paths, the values the
# corresponding Namespace objects.
def
namespaces
@namespaces
||=
Namespace
.
where
(
path:
usernames
).
each_with_object
({})
do
|
row
,
hash
|
hash
[
row
.
path
]
=
row
end
end
# Returns all usernames referenced in the current document.
def
usernames
refs
=
Set
.
new
nodes
.
each
do
|
node
|
node
.
to_html
.
scan
(
User
.
reference_pattern
)
do
refs
<<
$~
[
:user
]
end
end
refs
.
to_a
end
private
private
def
urls
def
urls
...
...
spec/lib/banzai/filter/reference_filter_spec.rb
0 → 100644
View file @
4c8d4c62
require
'spec_helper'
describe
Banzai
::
Filter
::
ReferenceFilter
,
lib:
true
do
let
(
:project
)
{
build
(
:project
)
}
describe
'#each_node'
do
it
'iterates over the nodes in a document'
do
document
=
Nokogiri
::
HTML
.
fragment
(
'<a href="foo">foo</a>'
)
filter
=
described_class
.
new
(
document
,
project:
project
)
expect
{
|
b
|
filter
.
each_node
(
&
b
)
}.
to
yield_with_args
(
an_instance_of
(
Nokogiri
::
XML
::
Element
))
end
it
'returns an Enumerator when no block is given'
do
document
=
Nokogiri
::
HTML
.
fragment
(
'<a href="foo">foo</a>'
)
filter
=
described_class
.
new
(
document
,
project:
project
)
expect
(
filter
.
each_node
).
to
be_an_instance_of
(
Enumerator
)
end
it
'skips links with a "gfm" class'
do
document
=
Nokogiri
::
HTML
.
fragment
(
'<a href="foo" class="gfm">foo</a>'
)
filter
=
described_class
.
new
(
document
,
project:
project
)
expect
{
|
b
|
filter
.
each_node
(
&
b
)
}.
not_to
yield_control
end
it
'skips text nodes in pre elements'
do
document
=
Nokogiri
::
HTML
.
fragment
(
'<pre>foo</pre>'
)
filter
=
described_class
.
new
(
document
,
project:
project
)
expect
{
|
b
|
filter
.
each_node
(
&
b
)
}.
not_to
yield_control
end
end
describe
'#nodes'
do
it
'returns an Array of the HTML nodes'
do
document
=
Nokogiri
::
HTML
.
fragment
(
'<a href="foo">foo</a>'
)
filter
=
described_class
.
new
(
document
,
project:
project
)
expect
(
filter
.
nodes
).
to
eq
([
document
.
children
[
0
]])
end
end
end
spec/lib/banzai/filter/user_reference_filter_spec.rb
View file @
4c8d4c62
...
@@ -136,4 +136,23 @@ describe Banzai::Filter::UserReferenceFilter, lib: true do
...
@@ -136,4 +136,23 @@ describe Banzai::Filter::UserReferenceFilter, lib: true do
expect
(
link
.
attr
(
'data-user'
)).
to
eq
user
.
namespace
.
owner_id
.
to_s
expect
(
link
.
attr
(
'data-user'
)).
to
eq
user
.
namespace
.
owner_id
.
to_s
end
end
end
end
describe
'#namespaces'
do
it
'returns a Hash containing all Namespaces'
do
document
=
Nokogiri
::
HTML
.
fragment
(
"<p>
#{
user
.
to_reference
}
</p>"
)
filter
=
described_class
.
new
(
document
,
project:
project
)
ns
=
user
.
namespace
expect
(
filter
.
namespaces
).
to
eq
({
ns
.
path
=>
ns
})
end
end
describe
'#usernames'
do
it
'returns the usernames mentioned in a document'
do
document
=
Nokogiri
::
HTML
.
fragment
(
"<p>
#{
user
.
to_reference
}
</p>"
)
filter
=
described_class
.
new
(
document
,
project:
project
)
expect
(
filter
.
usernames
).
to
eq
([
user
.
username
])
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