From 011668c0b667702ff362ffb593c922b911eb5f03 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Wed, 12 Mar 2014 13:34:34 +1100 Subject: [PATCH] Display line breaks in order confirmation email distributor info --- app/helpers/html_helper.rb | 17 ++++++++++++++++- spec/helpers/html_helper_spec.rb | 27 ++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/app/helpers/html_helper.rb b/app/helpers/html_helper.rb index c648cf3ace..522760d7d9 100644 --- a/app/helpers/html_helper.rb +++ b/app/helpers/html_helper.rb @@ -1,5 +1,20 @@ module HtmlHelper def strip_html(html) - strip_tags(html).andand.gsub(/ /i, ' ').andand.gsub(/&/i, '&') + squeeze_linebreaks substitute_entities strip_tags add_linebreaks html end + + def substitute_entities(html) + html.andand.gsub(/ /i, ' ').andand.gsub(/&/i, '&') + end + + 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") + end + + def squeeze_linebreaks(html) + html.andand.squeeze "\n" + end + end diff --git a/spec/helpers/html_helper_spec.rb b/spec/helpers/html_helper_spec.rb index 729da60d48..4b3118ea10 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!' + helper.strip_html('

Hello world!

').should == "Hello world!\n" end it "removes nbsp and amp entities" do @@ -13,5 +13,30 @@ describe HtmlHelper do it "returns nil for nil input" do helper.strip_html(nil).should be_nil 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"; + 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"; + 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"; + end + end end end