Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix punctuation removal for strings with new line characters #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/html_truncator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def inner_truncate(max, opts)
inner += txt
next if remaining >= 0
if ellipsable?
r = %r/[\s#{HTML_Truncator.punctuation_chars.join}]+$/
r = %r/[\s#{HTML_Truncator.punctuation_chars.join}]+\Z/
inner = inner.sub(r, '') + opts[:ellipsis]
opts[:ellipsis] = ""
opts[:was_truncated] = true
Expand Down
2 changes: 2 additions & 0 deletions spec/html_truncator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
let(:short_text) { "<p>Foo! <b>Bar</b> Baz</p>" }
let(:long_text) { "<p>Foo " + ("<b>Bar Baz</b> " * 100) + "Quux</p>" }
let(:list_text) { "<p>Foo:</p><ul>" + ("<li>Bar Baz</li>\n" * 100) + "</ul>" }
let(:text_with_new_lines) { "<p>Foo.\n Bar.\n Baz.</p>" }

it "should not modify short text" do
HTML_Truncator.truncate(short_text, 10).should == short_text
Expand Down Expand Up @@ -162,6 +163,7 @@

it "should remove punctuation before the ellipsis" do
HTML_Truncator.truncate(short_text, 1).should == "<p>Foo…</p>"
HTML_Truncator.truncate(text_with_new_lines, 12, :length_in_chars => true).should == "<p>Foo.\n Bar…</p>"
end

it "should behave correctly with html entities" do
Expand Down