Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Omission included when truncating by break_token. #57

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions lib/truncate_html/html_truncator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -50,22 +50,25 @@ def build_output

def process_token(token)
append_to_result(token)
if token.html_tag?
if token.html_tag? && @chars_remaining > 0
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

def append_to_result(token)
if token.html_tag? || token.html_comment?
if truncate_token?(token)
@chars_remaining = 0
elsif token.html_tag? || token.html_comment?
@truncated_html << token
elsif @word_boundary
@truncated_html << token if (@chars_remaining - token.length) >= 0
Expand Down
14 changes: 10 additions & 4 deletions spec/truncate_html/html_truncator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def truncate(html, opts = {})
truncate('This is line one. This is line <!-- truncate --> two.', :length => 30, :break_token => '<!-- truncate -->').should == 'This is line one. This is...'
end
it 'truncates before the length param if the break_token is before the token at "length"' do
truncate('This is line one. <!-- truncate --> This is line two.', :length => 30, :break_token => '<!-- truncate -->').should == 'This is line one.'
truncate('This is line one. <!-- truncate --> This is line two.', :length => 30, :break_token => '<!-- truncate -->').should == 'This is line one....'
end
end

Expand All @@ -170,7 +170,7 @@ def truncate(html, opts = {})
truncate('This is line one. This is line <!-- break --> two.', :length => 30, :break_token => '<!-- break -->').should == 'This is line one. This is...'
end
it 'truncates before the length param if the break_token is before the token at "length"' do
truncate('This is line one. <!-- break --> This is line two.', :length => 30, :break_token => '<!-- break -->').should == 'This is line one.'
truncate('This is line one. <!-- break --> This is line two.', :length => 30, :break_token => '<!-- break -->').should == 'This is line one....'
end
end

Expand All @@ -182,7 +182,7 @@ def truncate(html, opts = {})
truncate('This is line one. This is line <break /> two.', :length => 30, :break_token => '<break />').should == 'This is line one. This is...'
end
it 'truncates before the length param if the break_token is before the token at "length"' do
truncate('This is line one. <break /> This is line two.', :length => 30, :break_token => '<break />').should == 'This is line one.'
truncate('This is line one. <break /> This is line two.', :length => 30, :break_token => '<break />').should == 'This is line one....'
end
end

Expand All @@ -194,7 +194,7 @@ def truncate(html, opts = {})
truncate('This is line one. This is line foobar two.', :length => 30, :break_token => 'foobar').should == 'This is line one. This is...'
end
it 'truncates before the length param if the break_token is before the token at "length"' do
truncate('This is line one. foobar This is line two.', :length => 30, :break_token => 'foobar').should == 'This is line one.'
truncate('This is line one. foobar This is line two.', :length => 30, :break_token => 'foobar').should == 'This is line one....'
end
end

Expand All @@ -204,4 +204,10 @@ def truncate(html, opts = {})
'<h1>hello <!-- stuff --> and <!-- la -->...</h1>'
end
end

context 'when truncating by break_token and using a omission' do
it 'includes the omission after the truncation' do
truncate('This is the time to truncate this. Do it properly!', :length => 50, :break_token => 'truncate').should == 'This is the time to...'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You haven't specified an omission here. You've already tested that the default omission of ... works with all the other tests. You should probably do something like:

truncate('This is the time to truncate this. Do it properly!', :length => 50, :break_token => 'truncate', :omission => link_to(' MORE', 'path')).should == 'This is the time to <a href="path">MORE</a>'

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @gwagener! It was a good idea to include a test with a custom omission.

end
end
end