Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions lib/truncate_html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
config.length = 100
config.omission = '...'
config.word_boundary = /\S/
config.break_tokens = []
end


Expand Down
2 changes: 1 addition & 1 deletion lib/truncate_html/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module TruncateHtml
class Configuration
attr_accessor :length, :omission, :word_boundary, :break_token
attr_accessor :length, :omission, :word_boundary, :break_token, :break_tokens
end

class << self
Expand Down
11 changes: 10 additions & 1 deletion lib/truncate_html/html_truncator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@ def initialize(original_html, options = {})
@omission = options[:omission] || TruncateHtml.configuration.omission
@word_boundary = (options.has_key?(:word_boundary) ? options[:word_boundary] : TruncateHtml.configuration.word_boundary)
@break_token = options[:break_token] || TruncateHtml.configuration.break_token || nil
@break_tokens = options[:break_tokens] || TruncateHtml.configuration.break_tokens || []
@break_tokens << @break_token
Copy link

Choose a reason for hiding this comment

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

since @break_token can be set to nil on line 9, you may want to add a protection here so you're not shoveling a nil value into your @break_tokens array. maybe something like @break_tokens << @break_token if @break_token.

if there are any nil values in @break_tokens, the code will throw an error at line 83:
NoMethodError - undefined method '<<' for nil:NilClass

@chars_remaining = length - @omission.length
@open_tags, @truncated_html = [], ['']
end

def truncate
unless @break_token.nil?
ActiveSupport::Deprecation.warn(
"You try to use attribute break_token, this attribute is deprecated. " \
"Please use break_tokens."
)
end

return @omission if @chars_remaining < 0
@original_html.html_tokens.each do |token|
if @chars_remaining <= 0 || truncate_token?(token)
Expand Down Expand Up @@ -85,7 +94,7 @@ def remove_latest_open_tag(close_tag)
end

def truncate_token?(token)
@break_token and token == @break_token
@break_tokens.include?(token)
end
end
end
58 changes: 29 additions & 29 deletions spec/truncate_html/html_truncator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,51 +146,51 @@ def truncate(html, opts = {})
result.should include "<p>“我现在使用的是中文的拼音。”<br>"
end

context 'when the break_token option is set as <!-- truncate -->' do
it 'does not truncate abnormally if the break_token is not present' do
truncate('This is line one. This is line two.', :length => 30, :break_token => '<!-- truncate -->').should == 'This is line one. This is...'
context 'when the break_tokens option is set as <!-- truncate -->' do
it 'does not truncate abnormally if the break_tokens is not present' do
truncate('This is line one. This is line two.', :length => 30, :break_tokens => ['<!-- truncate -->']).should == 'This is line one. This is...'
end
it 'does not truncate abnormally if the break_token is present, but beyond the length param' do
truncate('This is line one. This is line <!-- truncate --> two.', :length => 30, :break_token => '<!-- truncate -->').should == 'This is line one. This is...'
it 'does not truncate abnormally if the break_tokens is present, but beyond the length param' do
truncate('This is line one. This is line <!-- truncate --> two.', :length => 30, :break_tokens => ['<!-- 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.'
it 'truncates before the length param if the break_tokens is before the token at "length"' do
truncate('This is line one. <!-- truncate --> This is line two.', :length => 30, :break_tokens => ['<!-- truncate -->']).should == 'This is line one.'
end
end

context 'when the break_token option is customized as a comment' do
it 'does not truncate abnormally if the break_token is not present' do
truncate('This is line one. This is line two.', :length => 30, :break_token => '<!-- break -->').should == 'This is line one. This is...'
context 'when the break_tokens option is customized as a comment' do
it 'does not truncate abnormally if the break_tokens is not present' do
truncate('This is line one. This is line two.', :length => 30, :break_tokens => ['<!-- break -->']).should == 'This is line one. This is...'
end
it 'does not truncate abnormally if the break_token is present, but beyond the length param' do
truncate('This is line one. This is line <!-- break --> two.', :length => 30, :break_token => '<!-- break -->').should == 'This is line one. This is...'
it 'does not truncate abnormally if the break_tokens is present, but beyond the length param' do
truncate('This is line one. This is line <!-- break --> two.', :length => 30, :break_tokens => ['<!-- 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.'
it 'truncates before the length param if the break_tokens is before the token at "length"' do
truncate('This is line one. <!-- break --> This is line two.', :length => 30, :break_tokens => ['<!-- break -->']).should == 'This is line one.'
end
end

context 'when the break_token option is customized as an html tag' do
it 'does not truncate abnormally if the break_token is not present' do
truncate('This is line one. This is line two.', :length => 30, :break_token => '<break />').should == 'This is line one. This is...'
context 'when the break_tokens option is customized as an html tag' do
it 'does not truncate abnormally if the break_tokens is not present' do
truncate('This is line one. This is line two.', :length => 30, :break_tokens => ['<break />']).should == 'This is line one. This is...'
end
it 'does not truncate abnormally if the break_token is present, but beyond the length param' do
truncate('This is line one. This is line <break /> two.', :length => 30, :break_token => '<break />').should == 'This is line one. This is...'
it 'does not truncate abnormally if the break_tokens is present, but beyond the length param' do
truncate('This is line one. This is line <break /> two.', :length => 30, :break_tokens => ['<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.'
it 'truncates before the length param if the break_tokens is before the token at "length"' do
truncate('This is line one. <break /> This is line two.', :length => 30, :break_tokens => ['<break />']).should == 'This is line one.'
end
end

context 'when the break_token option is customized as a word' do
it 'does not truncate abnormally if the break_token is not present' do
truncate('This is line one. This is line two.', :length => 30, :break_token => 'foobar').should == 'This is line one. This is...'
context 'when the break_tokens option is customized as a word' do
it 'does not truncate abnormally if the break_tokens is not present' do
truncate('This is line one. This is line two.', :length => 30, :break_tokens => ['foobar']).should == 'This is line one. This is...'
end
it 'does not truncate abnormally if the break_token is present, but beyond the length param' do
truncate('This is line one. This is line foobar two.', :length => 30, :break_token => 'foobar').should == 'This is line one. This is...'
it 'does not truncate abnormally if the break_tokens is present, but beyond the length param' do
truncate('This is line one. This is line foobar two.', :length => 30, :break_tokens => ['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.'
it 'truncates before the length param if the break_tokens is before the token at "length"' do
truncate('This is line one. foobar This is line two.', :length => 30, :break_tokens => ['foobar']).should == 'This is line one.'
end
end

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