Commit 78c6f914 authored by John Hope's avatar John Hope

Add negative lookahead to avoid matching incorrect commands

parent 5b5b4d87
...@@ -10,14 +10,14 @@ module Gitlab ...@@ -10,14 +10,14 @@ module Gitlab
end end
def match(content) def match(content)
content.match %r{^/#{all_names.join('|')} ?(.*)$} content.match %r{^/#{all_names.join('|')}(?![\S]) ?(.*)$}
end end
def perform_substitution(context, content) def perform_substitution(context, content)
return unless content return unless content
all_names.each do |a_name| all_names.each do |a_name|
content = content.gsub(%r{/#{a_name} ?(.*)$}i, execute_block(action_block, context, '\1')) content = content.gsub(%r{/#{a_name}(?![\S]) ?(.*)$}i, execute_block(action_block, context, '\1'))
end end
content content
......
...@@ -19,11 +19,29 @@ EOF ...@@ -19,11 +19,29 @@ EOF
expect(subject.perform_substitution(self, nil)).to be_nil expect(subject.perform_substitution(self, nil)).to be_nil
end end
it 'performs the substitution by default' do context 'when content contains command name' do
expect(subject.perform_substitution(self, content)).to eq <<EOF it 'performs the substitution by default' do
expect(subject.perform_substitution(self, content)).to eq <<EOF
Hello! Let's do this! Hello! Let's do this!
I like this stuff foo I like this stuff foo
EOF EOF
end
end
context 'when content contains command name in word' do
let(:content) do
<<EOF
Hello! Let's do this!
`/sub_names` I like this stuff
EOF
end
it 'does not perform the substitution' do
expect(subject.perform_substitution(self, content)).to eq <<EOF
Hello! Let's do this!
`/sub_names` I like this stuff
EOF
end
end end
end end
...@@ -41,5 +59,9 @@ EOF ...@@ -41,5 +59,9 @@ EOF
it 'is nil if content does not have the command' do it 'is nil if content does not have the command' do
expect(subject.match('blah')).to be_falsey expect(subject.match('blah')).to be_falsey
end end
it 'is nil if content contains the command as prefix' do
expect(subject.match('/sub_namex')).to be_falsey
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