diff options
author | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-07-13 03:33:25 +0300 |
---|---|---|
committer | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-07-13 17:42:31 +0300 |
commit | f682c0975b3a589d9fc6dae585cf6e36fa9ac348 (patch) | |
tree | f0fa9bcc907e0a8ceac0eeca8e244c9568e9aab9 | |
parent | c88075f46772d3c462969372fafbd7f3886872b8 (diff) |
Fix markdown rendering for label references that begin with a digit
-rw-r--r-- | app/models/label.rb | 5 | ||||
-rw-r--r-- | spec/lib/banzai/filter/label_reference_filter_spec.rb | 48 |
2 files changed, 52 insertions, 1 deletions
diff --git a/app/models/label.rb b/app/models/label.rb index 60c0fff44ce..9b8239d8757 100644 --- a/app/models/label.rb +++ b/app/models/label.rb @@ -52,11 +52,14 @@ class Label < ActiveRecord::Base # This pattern supports cross-project references. # def self.reference_pattern + # NOTE: The id pattern only matches when all characters on the expression + # are digits, so it will match ~2 but not ~2fa because that's probably a + # label name and we want it to be matched as such. @reference_pattern ||= %r{ (#{Project.reference_pattern})? #{Regexp.escape(reference_prefix)} (?: - (?<label_id>\d+) | # Integer-based label ID, or + (?<label_id>\d+(?!\S\w)\b) | # Integer-based label ID, or (?<label_name> [A-Za-z0-9_\-\?&]+ | # String-based single-word label title, or "([^"]*)" # String-based multi-word label surrounded in quotes diff --git a/spec/lib/banzai/filter/label_reference_filter_spec.rb b/spec/lib/banzai/filter/label_reference_filter_spec.rb index b16470718a0..763b8dbc46a 100644 --- a/spec/lib/banzai/filter/label_reference_filter_spec.rb +++ b/spec/lib/banzai/filter/label_reference_filter_spec.rb @@ -104,6 +104,30 @@ describe Banzai::Filter::LabelReferenceFilter, lib: true do end end + context 'String-based single-word references that begin with a digit' do + let(:label) { create(:label, name: '2fa', project: project) } + let(:reference) { "#{Label.reference_prefix}#{label.name}" } + + it 'links to a valid reference' do + doc = reference_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 2fa' + end + + it 'links with adjacent text' do + doc = reference_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.id}#{label.name.reverse}" + + expect(reference_filter(act).to_html).to eq exp + end + end + context 'String-based single-word references with special characters' do let(:label) { create(:label, name: '?gfm&', project: project) } let(:reference) { "#{Label.reference_prefix}#{label.name}" } @@ -153,6 +177,30 @@ describe Banzai::Filter::LabelReferenceFilter, lib: true do end end + context 'String-based multi-word references that begin with a digit' do + let(:label) { create(:label, name: '2 factor authentication', project: project) } + let(:reference) { label.to_reference(format: :name) } + + it 'links to a valid reference' do + doc = reference_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 2 factor authentication' + end + + it 'links with adjacent text' do + doc = reference_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.id}#{label.name.reverse}" + + expect(reference_filter(act).to_html).to eq exp + end + end + context 'String-based multi-word references with special characters in quotes' do let(:label) { create(:label, name: 'gfm & references?', project: project) } let(:reference) { label.to_reference(format: :name) } |