From 87d01ff41538dab7c381c8255a1688b8bcad17ba 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 | 24 +++++++++++++++++++---- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/lib/truncate_html/html_truncator.rb b/lib/truncate_html/html_truncator.rb index 52b707f..ded2128 100644 --- a/lib/truncate_html/html_truncator.rb +++ b/lib/truncate_html/html_truncator.rb @@ -14,7 +14,7 @@ def initialize(original_html, options = {}) 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..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. 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,20 @@ def truncate(html, opts = {}) to eq '

hello and ...

' end end + + context 'when truncating by break_token and using a omission' do + it 'includes the default omission after the truncation' do + expect(truncate('This is the time to truncate this. Do it properly!', + length: 50, break_token: 'truncate')). + to eq 'This is the time to...' + end + + 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