FIX: InlineUploads raises an error when img tag is invalid.

This commit is contained in:
Guo Xiang Tan 2019-06-12 10:31:00 +08:00
parent d49c193289
commit ff48fbdfda
2 changed files with 16 additions and 2 deletions

View File

@ -195,9 +195,9 @@ class InlineUploads
def self.match_img(markdown, external_src: false)
markdown.scan(/(<(?!img)[^<>]+\/?>)?(\n*)(([ ]*)<img ([^<>]+)>([ ]*))(\n*)/) do |match|
node = Nokogiri::HTML::fragment(match[2].strip).children[0]
src = node.attributes["src"].value
src = node.attributes["src"]&.value
if matched_uploads(src).present? || external_src
if src && (matched_uploads(src).present? || external_src)
text = node.attributes["alt"]&.value
width = node.attributes["width"]&.value
height = node.attributes["height"]&.value

View File

@ -45,6 +45,20 @@ RSpec.describe InlineUploads do
expect(InlineUploads.process(md)).to eq(md)
end
it "should work with invalid img tags" do
md = <<~MD
<img src="#{upload.url}">
This is an invalid `<img ...>` tag
MD
expect(InlineUploads.process(md)).to eq(<<~MD)
![](#{upload.short_url})
This is an invalid `<img ...>` tag
MD
end
it "should not correct code blocks" do
md = "`<a class=\"attachment\" href=\"#{upload2.url}\">In Code Block</a>`"