Merge pull request #1079 from sgmap/fix-crash

[Fix #1078] Fix a crasher in HtmlToStringHelper
This commit is contained in:
gregoirenovel 2017-12-12 10:10:55 +01:00 committed by GitHub
commit 74cda1b861
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 5 deletions

View file

@ -1,10 +1,14 @@
module HtmlToStringHelper module HtmlToStringHelper
def html_to_string(html) def html_to_string(html)
string_with_tags = html if html.blank?
.gsub(/<br[ ]?[\/]?>/, "\n") html
.gsub('</p>', "\n") else
.gsub('<p>', '') string_with_tags = html
.gsub(/<br[ ]?[\/]?>/, "\n")
.gsub('</p>', "\n")
.gsub('<p>', '')
strip_tags(string_with_tags) strip_tags(string_with_tags)
end
end end
end end

View file

@ -4,6 +4,11 @@ describe HtmlToStringHelper do
it { expect(helper.html_to_string('text')).to eq('text') } it { expect(helper.html_to_string('text')).to eq('text') }
end end
describe 'deals with empty / nil strings' do
it { expect(helper.html_to_string(nil)).to eq(nil) }
it { expect(helper.html_to_string('')).to eq("") }
end
describe 'deals with <br>' do describe 'deals with <br>' do
it { expect(helper.html_to_string('new<br>line')).to eq("new\nline") } it { expect(helper.html_to_string('new<br>line')).to eq("new\nline") }
it { expect(helper.html_to_string('new<br/>line')).to eq("new\nline") } it { expect(helper.html_to_string('new<br/>line')).to eq("new\nline") }