Display line breaks in order confirmation email distributor info

This commit is contained in:
Rohan Mitchell
2014-03-12 13:34:34 +11:00
parent a8373b7bef
commit 011668c0b6
2 changed files with 42 additions and 2 deletions

View File

@@ -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[^>]>|<br[^>]*>|<\/p>|<\/div>/, "\\1\n")
end
def squeeze_linebreaks(html)
html.andand.squeeze "\n"
end
end

View File

@@ -3,7 +3,7 @@ require 'spec_helper'
describe HtmlHelper do
describe "stripping html from a string" do
it "strips tags" do
helper.strip_html('<p><b>Hello</b> <em>world</em>!</p>').should == 'Hello world!'
helper.strip_html('<p><b>Hello</b> <em>world</em>!</p>').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("<h1>foo</h1>").should == "foo\n";
helper.strip_html("<h2>foo</h2>").should == "foo\n";
end
it "adds a line break after br tags" do
helper.strip_html("foo<br>").should == "foo\n";
helper.strip_html("foo<br/>").should == "foo\n";
helper.strip_html("foo<br />").should == "foo\n";
end
it "adds a line break after p tags" do
helper.strip_html("<p>foo</p>").should == "foo\n";
end
it "adds a line break after div tags" do
helper.strip_html("<div>foo</div>").should == "foo\n";
end
it "squeezes multiple line breaks" do
helper.strip_html("<p>foo</p><br /><br>bar").should == "foo\nbar";
end
end
end
end