require File.join(File.dirname(__FILE__), '..', 'spec_helper') include TruncateHtmlHelper describe TruncateHtmlHelper do it 'should be included in ActionView::Base' do ActionView::Base.included_modules.should include(TruncateHtmlHelper) end describe '#truncate_html' do context 'truncating in the middle of a link' do before(:each) do @html = '
' end it 'should truncate, and close the , and close any remaining open tags' do truncate_html(@html, :length => 10).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 'should place the punctuation after the tag without any whitespace' do truncate_html(@html, :length => 16).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 'should leave a whitespace between the closing tag and the following word character' do truncate_html(@html, :length => 17).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 'should recognize the multiline html properly' do truncate_html(@html, :length => 8).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 "should not close the #{unpaired_tag} tag" do truncate_html(@html, :length => 15).should == "
Some before. <#{unpaired_tag}>and...
" truncate_html(@html_caps, :length => 15).should == "
Some before. <#{unpaired_tag.capitalize}>and...
" end end 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 "should not close the #{unpaired_tag} tag" do truncate_html(@html, :length => 15).should == "
Some before. <#{unpaired_tag} />and...
" truncate_html(@html_caps, :length => 15).should == "
Some before. <#{unpaired_tag.capitalize} />and...
" end end end end end end