# Encoding: UTF-8 require File.join(File.dirname(__FILE__), '..', 'spec_helper') describe TruncateHtml::HtmlTruncator do def truncate(html, opts = {}) html_string = TruncateHtml::HtmlString.new(html) TruncateHtml::HtmlTruncator.new(html_string).truncate(opts) end context 'when the word_boundary option is set to false' do it 'truncates to the exact length specified' do truncate('
I have a script and more text
" expected_out = "I have a script and...
" truncate(input_html, :length => 23).should == expected_out end it 'in the middle of a link, truncates and closes the , and closes any remaining open tags' do html = 'Look at this#{char} More words here
" expected = "Look at this#{char}...
" truncate(html, :length => 19).should == expected end end end context 'when the html has a non punctuation character after a closing tag' do it 'leaves a whitespace between the closing tag and the following word character' do html = 'Look at this link for randomness
' expected = 'Look at this link...
' truncate(html, :length => 21).should == expected end end it 'handles multibyte characters and leaves them in the result' do html = 'Look at our multibyte characters ā ž this link for randomness ā ž
' truncate(html, :length => html.length).should == html end #unusual, but just covering my ass it 'recognizes the multiline html properly' do html = <<-END_HTML END_HTML truncate(html, :length => 12).should == ' ' end %w(br hr img).each do |unpaired_tag| context "when the html contains a #{unpaired_tag} tag" do context "and the #{unpaired_tag} does not have the closing slash" do it "does not close the #{unpaired_tag} tag" do html = "“我现在使用的是中文的拼音。”
测试一下具体的truncatehtml功能。
“我现在使用的是中文的拼音。”
测试一下具体的truncatehtml功能。
“我现在使用的是中文的拼音。”
测试一下具体的truncatehtml功能。
“我现在使用的是中文的拼音。”
测试一下具体的truncatehtml功能。
“我现在使用的是中文的拼音。”
"
end
context 'when the break_token option is set as ' 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 => '').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 two.', :length => 30, :break_token => '').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. This is line two.', :length => 30, :break_token => '').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 => '').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 two.', :length => 30, :break_token => '').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. This is line two.', :length => 30, :break_token => '').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 => '