# encoding: utf-8 require 'spec_helper' describe ActionMailer::Text::HtmlToPlainText do it 'converts a fragment' do expect(subject.convert_to_text('

Test

')).to match(/Test/) end it 'converts a body' do expect(subject.convert_to_text(' Ignore me

Test

')).to match(/Test/) end it 'ignores titles' do expect(subject.convert_to_text(' Ignore me

Test

')).not_to match(/Ignore me/) end it 'ignores header links' do expect(subject.convert_to_text('

Test

')).not_to match(/\*/) end it 'ignores header titles' do expect(subject.convert_to_text(' Ignore me

Test

')).not_to match(/Ignore me/) end it 'converts a malformed body' do expect(subject.convert_to_text(' Ignore me

Test ')).to match(/Test/) end it 'dencodes html entities' do expect(subject.convert_to_text(' cédille garçon & à ñ ')).to eq('cédille garçon & à ñ') end it 'strips whitespace' do expect(subject.convert_to_text(" \ttext\ntext\n")).to eq("text\ntext") expect(subject.convert_to_text(" \na \n a \t")).to eq("a\na") expect(subject.convert_to_text(" \na \n\t \n \n a \t")).to eq("a\n\na") expect(subject.convert_to_text('test text ')).to eq('test text') expect(subject.convert_to_text('test text')).to eq('test text') end it 'leaves spaces for spans' do expect(subject.convert_to_text('

Test line 2

')).to match(/Test line 2/) end it 'normalizes line breaks' do expect(subject.convert_to_text("Test text\r\nTest text")).to eq("Test text\nTest text") expect(subject.convert_to_text("Test text\nTest text")).to eq("Test text\nTest text") end it 'formats lists' do expect(subject.convert_to_text("
  • item 1
  • item 2
  • \n")).to eq("* item 1\n* item 2") expect(subject.convert_to_text("
  • item 1
  • \t\n
  • item 2
  • item 3
  • \n")).to eq("* item 1\n* item 2\n* item 3") end it 'strips html' do expect(subject.convert_to_text("

    test text\n")).to eq('test text') end it 'creates line breaks for p and br tags' do expect(subject.convert_to_text('

    Test text

    Test text

    ')).to eq("Test text\n\nTest text") expect(subject.convert_to_text("\n

    Test text

    \n\n\n\t

    Test text

    \n")).to eq("Test text\n\nTest text") expect(subject.convert_to_text("\n

    Test text
    Test text

    \n")).to eq("Test text\nTest text") expect(subject.convert_to_text("\n

    Test text
    \tTest text

    \n")).to eq("Test text\nTest text") expect(subject.convert_to_text('Test text

    Test text')).to eq("Test text\n\nTest text") end it 'converts headings' do expect(subject.convert_to_text('

    Test

    ')).to eq("****\nTest\n****") expect(subject.convert_to_text("\t

    \nTest

    ")).to eq("****\nTest\n****") expect(subject.convert_to_text("\t

    \nTest line 1
    Test 2

    ")).to eq("***********\nTest line 1\nTest 2\n***********") expect(subject.convert_to_text('

    Test

    Test

    ')).to eq("****\nTest\n****\n\n****\nTest\n****") expect(subject.convert_to_text('

    Test

    ')).to eq("----\nTest\n----") expect(subject.convert_to_text("

    Test

    ")).to eq("Test\n----") end it 'wraps lines' do raw = '' 100.times { raw += 'test ' } txt = subject.convert_to_text(raw, 20) lens = [] txt.each_line { |l| lens << l.length } expect(lens.max).to be <= 20 end it 'converts links' do # basic expect(subject.convert_to_text('Link')).to eq('Link ( http://example.com/ )') # nested html expect(subject .convert_to_text('Link')) .to eq('Link ( http://example.com/ )') # complex link expect(subject .convert_to_text('Link')) .to eq('Link ( http://example.com:80/~user?aaa=bb&c=d,e,f#foo )') # attributes expect(subject .convert_to_text('Link')) .to eq('Link ( http://example.com/ )') # spacing expect(subject .convert_to_text(' Link ')) .to eq('Link ( http://example.com/ )') # multiple expect(subject .convert_to_text('Link A Link B')) .to eq('Link A ( http://example.com/a/ ) Link B ( http://example.com/b/ )') # merge links expect(subject.convert_to_text('Link')).to eq('Link ( %%LINK%% )') expect(subject.convert_to_text('Link')).to eq('Link ( [LINK] )') expect(subject.convert_to_text('Link')).to eq('Link ( {LINK} )') # unsubscribe expect(subject.convert_to_text('Link')).to eq('Link ( [[!unsubscribe]] )') end it 'ignores empty links' do expect(subject .convert_to_text('Link A Link C', nil)) .to eq('Link A ( http://example.com/a/ ) Link C ( http://example.com/c/ )') end # see https://github.com/alexdunae/premailer/issues/72 it 'converts multiple links per line' do expect(subject .convert_to_text('

    This is link1 and link2 is next.

    ', nil, 10_000)) .to eq('This is link1 ( http://www.google.com ) and link2 ( http://www.google.com ) is next.') end # see https://github.com/alexdunae/premailer/issues/72 it 'converts links within headings' do expect(subject .convert_to_text("

    Test

    ")) .to eq("****************************\nTest ( http://example.com/ )\n****************************") end end