FIX: Handle cases where `alt` and `title` tag is blank when parsing excerpt.

This commit is contained in:
Guo Xiang Tan 2017-04-11 12:12:51 +08:00
parent 2d9b31b147
commit 0a4c30bce3
2 changed files with 22 additions and 6 deletions

View File

@ -68,9 +68,9 @@ class ExcerptParser < Nokogiri::XML::SAX::Document
# If include_images is set, include the image in markdown
characters("!") if @markdown_images
if attributes["alt"]
if !attributes["alt"].blank?
characters("[#{attributes["alt"]}]")
elsif attributes["title"]
elsif !attributes["title"].blank?
characters("[#{attributes["title"]}]")
else
characters("[#{I18n.t 'excerpt_image'}]")

View File

@ -124,12 +124,28 @@ HTML
expect(PrettyText.excerpt("<img src='http://cnn.com/a.gif'>",100)).to eq("[image]")
end
it "should keep alt tags" do
expect(PrettyText.excerpt("<img src='http://cnn.com/a.gif' alt='car' title='my big car'>",100)).to eq("[car]")
context 'alt tags' do
it "should keep alt tags" do
expect(PrettyText.excerpt("<img src='http://cnn.com/a.gif' alt='car' title='my big car'>", 100)).to eq("[car]")
end
describe 'when alt tag is empty' do
it "should not keep alt tags" do
expect(PrettyText.excerpt("<img src='http://cnn.com/a.gif' alt>", 100)).to eq("[#{I18n.t('excerpt_image')}]")
end
end
end
it "should keep title tags" do
expect(PrettyText.excerpt("<img src='http://cnn.com/a.gif' title='car'>",100)).to eq("[car]")
context 'title tags' do
it "should keep title tags" do
expect(PrettyText.excerpt("<img src='http://cnn.com/a.gif' title='car'>", 100)).to eq("[car]")
end
describe 'when title tag is empty' do
it "should not keep title tags" do
expect(PrettyText.excerpt("<img src='http://cnn.com/a.gif' title>", 100)).to eq("[#{I18n.t('excerpt_image')}]")
end
end
end
it "should convert images to markdown if the option is set" do