Skip to content

Commit 4bfcc63

Browse files
author
Xenofon Deligiannis
committed
Fix missing omission when break_token exists
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
1 parent e278c9b commit 4bfcc63

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

lib/truncate_html/html_truncator.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def truncate
1616
return @omission if @chars_remaining < 0
1717

1818
@original_html.html_tokens.each do |token|
19-
if @chars_remaining <= 0 || truncate_token?(token)
19+
if @chars_remaining <= 0
2020
close_open_tags
2121
break
2222
else
@@ -51,18 +51,21 @@ def build_output
5151
end
5252

5353
def process_token(token)
54-
append_to_result(token)
55-
if token.html_tag?
54+
append_to_result(token) if !truncate_token?(token)
55+
if truncate_token?(token)
56+
@chars_remaining = 0
57+
elsif token.html_tag?
5658
if token.open_tag?
5759
@open_tags << token
5860
else
5961
remove_latest_open_tag(token)
6062
end
6163
elsif !token.html_comment?
6264
@chars_remaining -= (@word_boundary ? token.length : token[0, @chars_remaining].length)
63-
if @chars_remaining <= 0
64-
@truncated_html[-1] = @truncated_html[-1].rstrip + @omission
65-
end
65+
end
66+
67+
if @chars_remaining <= 0
68+
@truncated_html[-1] = @truncated_html[-1].rstrip + @omission
6669
end
6770
end
6871

spec/truncate_html/html_truncator_spec.rb

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def truncate(html, opts = {})
177177
it 'truncates before the length param if the break_token is before the token at "length"' do
178178
expect(truncate('This is line one. <!-- truncate --> This is line two.',
179179
length: 30, break_token: '<!-- truncate -->')).
180-
to eq 'This is line one.'
180+
to eq 'This is line one....'
181181
end
182182
end
183183

@@ -195,7 +195,7 @@ def truncate(html, opts = {})
195195
it 'truncates before the length param if the break_token is before the token at "length"' do
196196
expect(truncate('This is line one. <!-- break --> This is line two.',
197197
length: 30, break_token: '<!-- break -->')).
198-
to eq 'This is line one.'
198+
to eq 'This is line one....'
199199
end
200200
end
201201

@@ -213,7 +213,7 @@ def truncate(html, opts = {})
213213
it 'truncates before the length param if the break_token is before the token at "length"' do
214214
expect(truncate('This is line one. <break /> This is line two.',
215215
length: 30, break_token: '<break />')).
216-
to eq 'This is line one.'
216+
to eq 'This is line one....'
217217
end
218218
end
219219

@@ -231,7 +231,7 @@ def truncate(html, opts = {})
231231
it 'truncates before the length param if the break_token is before the token at "length"' do
232232
expect(truncate('This is line one. foobar This is line two.',
233233
length: 30, break_token: 'foobar')).
234-
to eq 'This is line one.'
234+
to eq 'This is line one....'
235235
end
236236
end
237237

@@ -242,4 +242,20 @@ def truncate(html, opts = {})
242242
to eq '<h1>hello <!-- stuff --> and <!-- la -->...</h1>'
243243
end
244244
end
245+
246+
context 'when truncating by break_token and using a omission' do
247+
it 'includes the default omission after the truncation' do
248+
expect(truncate('This is the time to truncate this. Do it properly!',
249+
length: 50, break_token: 'truncate')).
250+
to eq 'This is the time to...'
251+
end
252+
253+
it 'includes the custom omission after the truncation' do
254+
expect(truncate('This is the time to truncate this. Do it properly!',
255+
length: 50,
256+
break_token: 'truncate',
257+
omission: ' <a href="path">MORE</a>')).
258+
to eq 'This is the time to <a href="path">MORE</a>'
259+
end
260+
end
245261
end

0 commit comments

Comments
 (0)