diff --git a/app/helpers/html_helper.rb b/app/helpers/html_helper.rb index 522760d7d9..95b7e5d0db 100644 --- a/app/helpers/html_helper.rb +++ b/app/helpers/html_helper.rb @@ -1,6 +1,6 @@ module HtmlHelper def strip_html(html) - squeeze_linebreaks substitute_entities strip_tags add_linebreaks html + strip_surrounding_whitespace substitute_entities strip_tags add_linebreaks html end def substitute_entities(html) @@ -10,11 +10,12 @@ module HtmlHelper def add_linebreaks(html) # I know Cthulu is coming for me. Forgive me. # http://stackoverflow.com/a/1732454/2720566 - html.andand.gsub(/<\/h[^>]>|]*>|<\/p>|<\/div>/, "\\1\n") + html. + andand.gsub(/<\/h[^>]>|<\/p>|<\/div>/, "\\1\n\n"). + andand.gsub(/]*>/, "\\1\n") end - def squeeze_linebreaks(html) - html.andand.squeeze "\n" + def strip_surrounding_whitespace(html) + html.andand.strip end - end diff --git a/spec/helpers/html_helper_spec.rb b/spec/helpers/html_helper_spec.rb index 4b3118ea10..3a19ddfce0 100644 --- a/spec/helpers/html_helper_spec.rb +++ b/spec/helpers/html_helper_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe HtmlHelper do describe "stripping html from a string" do it "strips tags" do - helper.strip_html('

Hello world!

').should == "Hello world!\n" + helper.strip_html('

Hello world!

').should == "Hello world!" end it "removes nbsp and amp entities" do @@ -15,27 +15,27 @@ describe HtmlHelper do end describe "line breaks" do - it "adds a line break after heading tags" do - helper.strip_html("

foo

").should == "foo\n"; - helper.strip_html("

foo

").should == "foo\n"; + it "adds two line breaks after heading tags" do + helper.strip_html("

foo

bar").should == "foo\n\nbar"; + helper.strip_html("

foo

bar").should == "foo\n\nbar"; + end + + it "adds two line breaks after p tags" do + helper.strip_html("

foo

bar").should == "foo\n\nbar"; + end + + it "adds two line breaks after div tags" do + helper.strip_html("
foo
bar").should == "foo\n\nbar"; end it "adds a line break after br tags" do - helper.strip_html("foo
").should == "foo\n"; - helper.strip_html("foo
").should == "foo\n"; - helper.strip_html("foo
").should == "foo\n"; + helper.strip_html("foo
bar").should == "foo\nbar"; + helper.strip_html("foo
bar").should == "foo\nbar"; + helper.strip_html("foo
bar").should == "foo\nbar"; end - it "adds a line break after p tags" do - helper.strip_html("

foo

").should == "foo\n"; - end - - it "adds a line break after div tags" do - helper.strip_html("
foo
").should == "foo\n"; - end - - it "squeezes multiple line breaks" do - helper.strip_html("

foo



bar").should == "foo\nbar"; + it "strips line breaks at the end of the string" do + helper.strip_html("
foo

").should == "foo"; end end end