diff --git a/app/views/helpers/line_break_helper.rb b/app/views/helpers/line_break_helper.rb
index 0ae992007b12..8420ec5ef76b 100644
--- a/app/views/helpers/line_break_helper.rb
+++ b/app/views/helpers/line_break_helper.rb
@@ -3,6 +3,6 @@
class LineBreakHelper
def self.break_lines(text)
escaped_text = ERB::Util.html_escape(text.to_s)
- escaped_text.gsub(/\n/, '
').html_safe # rubocop:disable Rails/OutputSafety
+ escaped_text.split("\n").reject(&:blank?).join('
').html_safe # rubocop:disable Rails/OutputSafety
end
end
diff --git a/spec/views/helpers/line_break_helper_spec.rb b/spec/views/helpers/line_break_helper_spec.rb
index 05cde2d7b6ca..26eafc1c16ab 100644
--- a/spec/views/helpers/line_break_helper_spec.rb
+++ b/spec/views/helpers/line_break_helper_spec.rb
@@ -6,10 +6,22 @@
subject(:helper) { described_class }
describe '.break_lines' do
- it 'replaces \n with
' do
+ it 'replaces \n with
' do
html = helper.break_lines("t\nt")
expect(html).to eq('t
t')
end
+
+ it 'removes all \n at the beginning and the end' do
+ html = helper.break_lines("t\nt\n\n\n")
+
+ expect(html).to eq('t
t')
+ end
+
+ it 'removes double extra \n' do
+ html = helper.break_lines("\n\n\nt\n\n\nt\n\n\n")
+
+ expect(html).to eq('t
t')
+ end
end
end