diff --git a/lib/truncate_html/html_truncator.rb b/lib/truncate_html/html_truncator.rb
index 07dd808..5c2898c 100644
--- a/lib/truncate_html/html_truncator.rb
+++ b/lib/truncate_html/html_truncator.rb
@@ -16,7 +16,7 @@ def truncate
return @omission if @chars_remaining < 0
@original_html.html_tokens.each do |token|
- if @chars_remaining <= 0 || truncate_token?(token)
+ if @chars_remaining <= 0
close_open_tags
break
else
@@ -51,8 +51,10 @@ def build_output
end
def process_token(token)
- append_to_result(token)
- if token.html_tag?
+ append_to_result(token) if !truncate_token?(token)
+ if truncate_token?(token)
+ @chars_remaining = 0
+ elsif token.html_tag?
if token.open_tag?
@open_tags << token
else
@@ -60,9 +62,10 @@ def process_token(token)
end
elsif !token.html_comment?
@chars_remaining -= (@word_boundary ? token.length : token[0, @chars_remaining].length)
- if @chars_remaining <= 0
- @truncated_html[-1] = @truncated_html[-1].rstrip + @omission
- end
+ end
+
+ if @chars_remaining <= 0
+ @truncated_html[-1] = @truncated_html[-1].rstrip + @omission
end
end
diff --git a/spec/truncate_html/html_truncator_spec.rb b/spec/truncate_html/html_truncator_spec.rb
index d745d2b..0becdd5 100644
--- a/spec/truncate_html/html_truncator_spec.rb
+++ b/spec/truncate_html/html_truncator_spec.rb
@@ -177,7 +177,7 @@ def truncate(html, opts = {})
it 'truncates before the length param if the break_token is before the token at "length"' do
expect(truncate('This is line one. This is line two.',
length: 30, break_token: '')).
- to eq 'This is line one.'
+ to eq 'This is line one....'
end
end
@@ -195,7 +195,7 @@ def truncate(html, opts = {})
it 'truncates before the length param if the break_token is before the token at "length"' do
expect(truncate('This is line one. This is line two.',
length: 30, break_token: '')).
- to eq 'This is line one.'
+ to eq 'This is line one....'
end
end
@@ -213,7 +213,7 @@ def truncate(html, opts = {})
it 'truncates before the length param if the break_token is before the token at "length"' do
expect(truncate('This is line one.