# 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('
123456789
', :length => 5, :omission => '', :word_boundary => false).should == '
12345
' end it 'retains the tags within the text' do html = 'some text CAPS some text' truncate(html, :length => 25, :word_boundary => false).should == 'some text CAPS some te...' end context 'and a custom omission value is passed' do it 'retains the omission text' do truncate_html("testtest", :length => 10, :omission => '..', :word_boundary => false).should == 'testtest..' end it 'handles multibyte characters' do truncate_html("prüfenprüfen", :length => 8, :omission => '..', :word_boundary => false). should == 'prüfen..' end end end context 'when the word_boundary option is a custom value (for splitting on sentences)' do it 'truncates to the end of the nearest sentence' do truncate_html('hello there. or maybe not?', :length => 16, :omission => '', :word_boundary => /\S[\.\?\!]/).should == 'hello there.' end end it "includes the omission text's length in the returned truncated html" do truncate('a b c', :length => 4, :omission => '...').should == 'a...' end it "includes omission even on the edge (issue #18)" do opts = { :word_boundary => false, :length => 12 } truncate('One two three', opts).should == 'One two t...' end it "never returns a string longer than :length" do truncate("test this shit", :length => 10).should == 'test...' end it 'supports omissions longer than the maximum length' do lambda { truncate('', :length => 1, :omission => '...') }.should_not raise_error end it 'returns the omission when the specified length is smaller than the omission' do truncate('a b c', :length => 2, :omission => '...').should == '...' end it 'treats script tags as strings with no length' do input_html = "

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 = '
' expected = '
' truncate(html, :length => 15).should == expected end %w(! @ # $ % ^ & * \( \) - _ + = [ ] { } \ | , . / ?).each do |char| context "when the html has a #{char} character after a closing tag" do it 'places the punctuation after the tag without any whitespace' 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
This is ugly html.
END_HTML truncate(html, :length => 12).should == '
This is...
' 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 = "
Some before. <#{unpaired_tag}>and some after
" html_caps = "
Some before. <#{unpaired_tag.capitalize}>and some after
" truncate(html, :length => 19).should == "
Some before. <#{unpaired_tag}>and...
" truncate(html_caps, :length => 19).should == "
Some before. <#{unpaired_tag.capitalize}>and...
" end end context "and the #{unpaired_tag} does have the closing slash" do it "does not close the #{unpaired_tag} tag" do html = "
Some before. <#{unpaired_tag} />and some after
" html_caps = "
Some before. <#{unpaired_tag.capitalize} />and some after
" truncate(html, :length => 19).should == "
Some before. <#{unpaired_tag} />and...
" truncate(html_caps, :length => 19).should == "
Some before. <#{unpaired_tag.capitalize} />and...
" end end end end it 'does not truncate quotes off when input contains chinese characters' do html = "

“我现在使用的是中文的拼音。”
测试一下具体的truncatehtml功能。
“我现在使用的是中文的拼音。”
测试一下具体的truncate
html功能。
“我现在使用的是中文的拼音。”
测试一下具体的truncatehtml功能。
“我现在使用的是中文的拼音。”
测试一下具体的truncate
html功能。

" result = truncate(html, :omission => "", :length => 50) result.should include "

“我现在使用的是中文的拼音。”
" 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 => '').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 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...' 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...' 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.' end end end