Show two linebreaks for end of block level tags, strip whitespace from end of string

This commit is contained in:
Rohan Mitchell
2014-03-12 13:52:13 +11:00
parent 011668c0b6
commit f7d8d866eb
2 changed files with 23 additions and 22 deletions

View File

@@ -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[^>]>|<br[^>]*>|<\/p>|<\/div>/, "\\1\n")
html.
andand.gsub(/<\/h[^>]>|<\/p>|<\/div>/, "\\1\n\n").
andand.gsub(/<br[^>]*>/, "\\1\n")
end
def squeeze_linebreaks(html)
html.andand.squeeze "\n"
def strip_surrounding_whitespace(html)
html.andand.strip
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!\n"
helper.strip_html('<p><b>Hello</b> <em>world</em>!</p>').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("<h1>foo</h1>").should == "foo\n";
helper.strip_html("<h2>foo</h2>").should == "foo\n";
it "adds two line breaks after heading tags" do
helper.strip_html("<h1>foo</h1>bar").should == "foo\n\nbar";
helper.strip_html("<h2>foo</h2>bar").should == "foo\n\nbar";
end
it "adds two line breaks after p tags" do
helper.strip_html("<p>foo</p>bar").should == "foo\n\nbar";
end
it "adds two line breaks after div tags" do
helper.strip_html("<div>foo</div>bar").should == "foo\n\nbar";
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";
helper.strip_html("foo<br>bar").should == "foo\nbar";
helper.strip_html("foo<br/>bar").should == "foo\nbar";
helper.strip_html("foo<br />bar").should == "foo\nbar";
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";
it "strips line breaks at the end of the string" do
helper.strip_html("<div>foo</div><br />").should == "foo";
end
end
end