Commit 3f6d91c5 authored by Douwe Maan's avatar Douwe Maan

Add Gitlab::StringRegexMarker

parent e1797078
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
......@@ -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
......
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
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