diff --git a/lib/truncate_html/html_string.rb b/lib/truncate_html/html_string.rb
index 76d82e9..1b31fdf 100644
--- a/lib/truncate_html/html_string.rb
+++ b/lib/truncate_html/html_string.rb
@@ -3,7 +3,8 @@ module TruncateHtml
class HtmlString < String
UNPAIRED_TAGS = %w(br hr img).freeze
- REGEX = /(?:.*<\/script>)+|<\/?[^>]+>|[[[:alpha:]][0-9]\|`~!@#\$%^&*\(\)\-_\+=\[\]{}:;'²³§",\.\/?]+|\s+|[[:punct:]]/.freeze
+ REGEX = /(?:.*<\/script>)+|<\/?[^>]+>|[[[:alpha:]][0-9]\|`~!@#\$%^&*\(\)\-_\+=\[\]{}:;'²³§",\.\/?]+|\s+|[[:punct:]]/.freeze
+ HTMLTAGS = / This will show
'
+ expected = 'This is bold. This will show'
+ html_string(html).clean_html.should == expected
+ end
+
+ it 'returns the html string without any comments' do
+ html = 'This is bold. And this will show'
+ expected = 'This is bold. And this will show'
+ html_string(html).clean_html.should == expected
+ end
+ end
end
diff --git a/spec/truncate_html/html_truncator_spec.rb b/spec/truncate_html/html_truncator_spec.rb
index 0da14d6..faf3f82 100644
--- a/spec/truncate_html/html_truncator_spec.rb
+++ b/spec/truncate_html/html_truncator_spec.rb
@@ -15,12 +15,12 @@ def truncate(html, opts = {})
it 'retains the tags within the text' do
html = 'some text CAPS some text'
- truncate(html, :length => 25, :word_boundary => false).should == 'some text CAPS some te...'
+ truncate(html, :length => 19, :word_boundary => false).should == 'some text CAPS s...'
end
context 'and a custom omission value is passed' do
it 'retains the omission text' do
- truncate("testtest", :length => 10, :omission => '..', :word_boundary => false).should == 'testtest..'
+ truncate("testtest", :length => 7, :omission => '..', :word_boundary => false).should == 'testt..'
end
it 'handles multibyte characters' do
@@ -204,4 +204,34 @@ def truncate(html, opts = {})
'hello and ...
'
end
end
+
+ context 'when the clean string length is the same than the length param' do
+ it 'does not truncate the string' do
+ html = 'exact string length'
+ truncate(html, length: 19).should == html
+ end
+
+ it 'does not truncate the string even if it contains html tags' do
+ html = 'exact string length'
+ truncate(html, length: 19).should == html
+ end
+
+ it 'does not truncate the string even if it contains html comments' do
+ html = 'exact string length'
+ truncate(html, length: 19).should == html
+ end
+
+ context 'when the break_token is set' do
+ it 'truncates before the break_token if included in the string' do
+ html = 'exact string length'
+ expected = 'exact string'
+ truncate(html, length: 19, break_token: 'length').should == expected
+ end
+
+ it 'does not truncate before if break token is not in the string' do
+ html = 'exact string length'
+ truncate(html, length: 19, break_token: 'nothere').should == html
+ end
+ end
+ end
end