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 describe '#truncate' do context 'when the word_boundry option is set to false' do it 'truncates to the exact length specified' do truncate('
123456789
', :length => 5, :omission => '', :word_boundry => false).should == '
12345
' 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 '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 context 'the input html contains a script tag' do before(:each) do @input_html = "

I have a script and more text

" @expected_out = "

I have a script and...

" end it 'treats the script tag as lengthless string' do truncate(@input_html, :length => 23).should == @expected_out end end context 'truncating in the middle of a link' do before(:each) do @html = '
' end it 'truncates, and closes the , and closes any remaining open tags' do truncate(@html, :length => 14).should == '
' end end %w(! @ # $ % ^ & * \( \) - _ + = [ ] { } \ | , . / ?).each do |char| context "when the html has a #{char} character after a closing tag" do before(:each) do @html = "

Look at this#{char} More words here

" end it 'places the punctuation after the tag without any whitespace' do truncate(@html, :length => 19).should == "

Look at this#{char} More...

" end end end context 'when the html has a non punctuation character after a closing tag' do before(:each) do @html = '

Look at this link for randomness

' end it 'leaves a whitespace between the closing tag and the following word character' do truncate(@html, :length => 21).should == '

Look at this link...

' end end #unusual, but just covering my ass context 'when the HTML tags are multiline' do before(:each) do @html = <<-END_HTML
This is ugly html.
END_HTML end it 'recognizes the multiline html properly' do truncate(@html, :length => 12).should == '
This is...
' end 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 before(:each) do @html = "
Some before. <#{unpaired_tag}>and some after
" @html_caps = "
Some before. <#{unpaired_tag.capitalize}>and some after
" end it "does not close the #{unpaired_tag} tag" do 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 before(:each) do @html = "
Some before. <#{unpaired_tag} />and some after
" @html_caps = "
Some before. <#{unpaired_tag.capitalize} />and some after
" end it "does not close the #{unpaired_tag} tag" do 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 end end