Skip to content

Commit

Permalink
Fix missing omission when break_token exists
Browse files Browse the repository at this point in the history
When using break_token, omissions was not added in the truncated html.
Fix based on hgmnz/truncate_html#57.
Issue described here hgmnz/truncate_html#52
  • Loading branch information
Xenofon Deligiannis committed Apr 19, 2019
1 parent f9da9c0 commit d7e6d3f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
15 changes: 9 additions & 6 deletions lib/truncate_html/html_truncator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -49,18 +49,21 @@ 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
remove_latest_open_tag(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

Expand Down
24 changes: 20 additions & 4 deletions spec/truncate_html/html_truncator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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. <!-- truncate --> This is line two.',
length: 30, break_token: '<!-- truncate -->')).
to eq 'This is line one.'
to eq 'This is line one....'
end
end

Expand All @@ -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. <!-- break --> This is line two.',
length: 30, break_token: '<!-- break -->')).
to eq 'This is line one.'
to eq 'This is line one....'
end
end

Expand All @@ -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. <break /> This is line two.',
length: 30, break_token: '<break />')).
to eq 'This is line one.'
to eq 'This is line one....'
end
end

Expand All @@ -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

Expand All @@ -242,4 +242,20 @@ def truncate(html, opts = {})
to eq '<h1>hello <!-- stuff --> and <!-- la -->...</h1>'
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: ' <a href="path">MORE</a>')).
to eq 'This is the time to <a href="path">MORE</a>'
end
end
end

0 comments on commit d7e6d3f

Please sign in to comment.