summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDouwe Maan <douwe@selenight.nl>2016-07-10 15:17:20 -0500
committerDouwe Maan <douwe@selenight.nl>2017-05-10 08:26:21 -0500
commit3f6d91c53c1bf99ce2e7dfeb9c25ef5f8149b72e (patch)
tree6aa3c7cc777857ed6a35a50612cf9fec83c94493
parente179707844b043fac6f81a30f82e5f4ba446abde (diff)
downloadgitlab-ce-3f6d91c53c1bf99ce2e7dfeb9c25ef5f8149b72e.tar.gz
Add Gitlab::StringRegexMarker
-rw-r--r--lib/gitlab/string_regex_marker.rb13
-rw-r--r--spec/lib/gitlab/string_range_marker_spec.rb4
-rw-r--r--spec/lib/gitlab/string_regex_marker_spec.rb18
3 files changed, 33 insertions, 2 deletions
diff --git a/lib/gitlab/string_regex_marker.rb b/lib/gitlab/string_regex_marker.rb
new file mode 100644
index 00000000000..7ebf1c0428c
--- /dev/null
+++ b/lib/gitlab/string_regex_marker.rb
@@ -0,0 +1,13 @@
+module Gitlab
+ class StringRegexMarker < StringRangeMarker
+ def mark(regex, group: 0, &block)
+ regex_match = raw_line.match(regex)
+ return rich_line unless regex_match
+
+ begin_index, end_index = regex_match.offset(group)
+ name_range = begin_index..(end_index - 1)
+
+ super([name_range], &block)
+ end
+ end
+end
diff --git a/spec/lib/gitlab/string_range_marker_spec.rb b/spec/lib/gitlab/string_range_marker_spec.rb
index 45b4d4af206..7c77772b3f6 100644
--- a/spec/lib/gitlab/string_range_marker_spec.rb
+++ b/spec/lib/gitlab/string_range_marker_spec.rb
@@ -6,7 +6,7 @@ describe Gitlab::StringRangeMarker, lib: true do
let(:raw) { "abc <def>" }
let(:rich) { %{<span class="abc">abc</span><span class="space"> </span><span class="def">&lt;def&gt;</span>}.html_safe }
let(:inline_diffs) { [2..5] }
- let(:subject) do
+ subject do
described_class.new(raw, rich).mark(inline_diffs) do |text, left:, right:|
"LEFT#{text}RIGHT"
end
@@ -21,7 +21,7 @@ describe Gitlab::StringRangeMarker, lib: true do
context "when the rich text is not html safe" do
let(:raw) { "abc <def>" }
let(:inline_diffs) { [2..5] }
- let(:subject) do
+ subject do
described_class.new(raw).mark(inline_diffs) do |text, left:, right:|
"LEFT#{text}RIGHT"
end
diff --git a/spec/lib/gitlab/string_regex_marker_spec.rb b/spec/lib/gitlab/string_regex_marker_spec.rb
new file mode 100644
index 00000000000..2f5cf6c6e3b
--- /dev/null
+++ b/spec/lib/gitlab/string_regex_marker_spec.rb
@@ -0,0 +1,18 @@
+require 'spec_helper'
+
+describe Gitlab::StringRegexMarker, lib: true do
+ describe '#mark' do
+ let(:raw) { %{"name": "AFNetworking"} }
+ let(:rich) { %{<span class="key">"name"</span><span class="punctuation">: </span><span class="value">"AFNetworking"</span>}.html_safe }
+ subject do
+ described_class.new(raw, rich).mark(/"[^"]+":\s*"(?<name>[^"]+)"/, group: :name) do |text, left:, right:|
+ %{<a href="#">#{text}</a>}
+ end
+ end
+
+ it 'marks the inline diffs' do
+ expect(subject).to eq(%{<span class="key">"name"</span><span class="punctuation">: </span><span class="value">"<a href="#">AFNetworking</a>"</span>})
+ expect(subject).to be_html_safe
+ end
+ end
+end