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

approved
#1

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

diff --git a/app/services/inline_uploads.rb b/app/services/inline_uploads.rb
index 70cbbf9..42f410d 100644
--- a/app/services/inline_uploads.rb
+++ b/app/services/inline_uploads.rb
@@ -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
diff --git a/spec/services/inline_uploads_spec.rb b/spec/services/inline_uploads_spec.rb
index 0ec00d0..4eb1495 100644
--- a/spec/services/inline_uploads_spec.rb
+++ b/spec/services/inline_uploads_spec.rb
@@ -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>`"

GitHub sha: ff48fbdf

1 Like
Approved #2