From cead592517618e3cdb9047670374c1cd7cf40d4b Mon Sep 17 00:00:00 2001 From: Xenofon Deligiannis Date: Thu, 18 Apr 2019 11:01:15 +0300 Subject: [PATCH] Fix missing omission when break_token exists When using break_token, omissions was not added in the truncated html. Fix based on https://github.com/hgmnz/truncate_html/pull/57. Issue described here https://github.com/hgmnz/truncate_html/issues/52 --- lib/truncate_html/html_truncator.rb | 15 +++++++++------ spec/truncate_html/html_truncator_spec.rb | 18 ++++++++++++++---- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/lib/truncate_html/html_truncator.rb b/lib/truncate_html/html_truncator.rb index 50f2cb1..2e5dd9b 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 @@ -49,8 +49,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 @@ -58,9 +60,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..2daffe9 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. This is line two.', length: 30, break_token: '')). - to eq 'This is line one.' + to eq 'This is line one....' end end @@ -231,7 +231,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. foobar This is line two.', length: 30, break_token: 'foobar')). - to eq 'This is line one.' + to eq 'This is line one....' end end @@ -242,4 +242,14 @@ def truncate(html, opts = {}) to eq '

hello and ...

' end end + + context 'when the break_token and a custom omission options are used' do + it 'includes the custom omission after the truncation' do + expect(truncate('This is the time to truncate this. Do it properly!', + length: 50, + break_token: 'truncate', + omission: ' MORE')). + to eq 'This is the time to MORE' + end + end end