Commit 81a09bc7 authored by Robert Speicher's avatar Robert Speicher

Support only double quotes for multi-word label references

parent 35853033
...@@ -41,16 +41,14 @@ class Label < ActiveRecord::Base ...@@ -41,16 +41,14 @@ class Label < ActiveRecord::Base
end end
# Pattern used to extract label references from text # Pattern used to extract label references from text
#
# TODO (rspeicher): Limit to double quotes (meh) or disallow single quotes in label names (bad).
def self.reference_pattern def self.reference_pattern
%r{ %r{
#{reference_prefix} #{reference_prefix}
(?: (?:
(?<label_id>\d+) | # Integer-based label ID, or (?<label_id>\d+) | # Integer-based label ID, or
(?<label_name> (?<label_name>
[A-Za-z0-9_-]+ | # String-based single-word label title [A-Za-z0-9_-]+ | # String-based single-word label title, or
['"][^&\?,]+['"] # String-based multi-word label surrounded in quotes "[^&\?,]+" # String-based multi-word label surrounded in quotes
) )
) )
}x }x
...@@ -70,7 +68,7 @@ class Label < ActiveRecord::Base ...@@ -70,7 +68,7 @@ class Label < ActiveRecord::Base
# #
# Returns a String # Returns a String
def to_reference(format = :id) def to_reference(format = :id)
if format == :name if format == :name && !name.include?('"')
%(#{self.class.reference_prefix}"#{name}") %(#{self.class.reference_prefix}"#{name}")
else else
"#{self.class.reference_prefix}#{id}" "#{self.class.reference_prefix}#{id}"
......
...@@ -72,8 +72,7 @@ module Gitlab ...@@ -72,8 +72,7 @@ module Gitlab
# Returns a Hash. # Returns a Hash.
def label_params(id, name) def label_params(id, name)
if name if name
# TODO (rspeicher): Don't strip single quotes if we decide to only use double quotes for surrounding. { name: name.tr('"', '') }
{ name: name.tr('\'"', '') }
else else
{ id: id } { id: id }
end end
......
...@@ -100,52 +100,26 @@ module Gitlab::Markdown ...@@ -100,52 +100,26 @@ module Gitlab::Markdown
end end
context 'String-based multi-word references in quotes' do context 'String-based multi-word references in quotes' do
let(:label) { create(:label, name: 'gfm references', project: project) } let(:label) { create(:label, name: 'gfm references', project: project) }
let(:reference) { label.to_reference(:name) }
context 'in single quotes' do it 'links to a valid reference' do
let(:reference) { "#{Label.reference_prefix}'#{label.name}'" } doc = filter("See #{reference}")
it 'links to a valid reference' do
doc = filter("See #{reference}")
expect(doc.css('a').first.attr('href')).to eq urls.
namespace_project_issues_url(project.namespace, project, label_name: label.name)
expect(doc.text).to eq 'See gfm references'
end
it 'links with adjacent text' do
doc = filter("Label (#{reference}.)")
expect(doc.to_html).to match(%r(\(<a.+><span.+>#{label.name}</span></a>\.\)))
end
it 'ignores invalid label names' do
exp = act = "Label #{Label.reference_prefix}'#{label.name.reverse}'"
expect(filter(act).to_html).to eq exp expect(doc.css('a').first.attr('href')).to eq urls.
end namespace_project_issues_url(project.namespace, project, label_name: label.name)
expect(doc.text).to eq 'See gfm references'
end end
context 'in double quotes' do it 'links with adjacent text' do
let(:reference) { %(#{Label.reference_prefix}"#{label.name}") } doc = filter("Label (#{reference}.)")
expect(doc.to_html).to match(%r(\(<a.+><span.+>#{label.name}</span></a>\.\)))
it 'links to a valid reference' do end
doc = filter("See #{reference}")
expect(doc.css('a').first.attr('href')).to eq urls.
namespace_project_issues_url(project.namespace, project, label_name: label.name)
expect(doc.text).to eq 'See gfm references'
end
it 'links with adjacent text' do
doc = filter("Label (#{reference}.)")
expect(doc.to_html).to match(%r(\(<a.+><span.+>#{label.name}</span></a>\.\)))
end
it 'ignores invalid label names' do it 'ignores invalid label names' do
exp = act = %(Label #{Label.reference_prefix}"#{label.name.reverse}") exp = act = %(Label #{Label.reference_prefix}"#{label.name.reverse}")
expect(filter(act).to_html).to eq exp expect(filter(act).to_html).to eq exp
end
end end
end end
......
...@@ -55,13 +55,22 @@ describe Label do ...@@ -55,13 +55,22 @@ describe Label do
end end
describe '#to_reference' do describe '#to_reference' do
it 'returns a String reference to the object' do context 'using id' do
expect(label.to_reference).to eq "~#{label.id}" it 'returns a String reference to the object' do
expect(label.to_reference(double)).to eq "~#{label.id}" expect(label.to_reference).to eq "~#{label.id}"
expect(label.to_reference(double('project'))).to eq "~#{label.id}"
end
end end
it 'returns a String reference to the object using its name' do context 'using name' do
expect(label.to_reference(:name)).to eq %(~"#{label.name}") it 'returns a String reference to the object' do
expect(label.to_reference(:name)).to eq %(~"#{label.name}")
end
it 'uses id when name contains double quote' do
label = create(:label, name: %q{"irony"})
expect(label.to_reference(:name)).to eq "~#{label.id}"
end
end end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment