mirror of
https://github.com/discourse/discourse.git
synced 2025-02-17 09:22:44 +08:00
FIX: InlineUploads
raises an error when img tag is invalid.
This commit is contained in:
parent
d49c193289
commit
ff48fbdfda
|
@ -195,9 +195,9 @@ class InlineUploads
|
||||||
def self.match_img(markdown, external_src: false)
|
def self.match_img(markdown, external_src: false)
|
||||||
markdown.scan(/(<(?!img)[^<>]+\/?>)?(\n*)(([ ]*)<img ([^<>]+)>([ ]*))(\n*)/) do |match|
|
markdown.scan(/(<(?!img)[^<>]+\/?>)?(\n*)(([ ]*)<img ([^<>]+)>([ ]*))(\n*)/) do |match|
|
||||||
node = Nokogiri::HTML::fragment(match[2].strip).children[0]
|
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
|
text = node.attributes["alt"]&.value
|
||||||
width = node.attributes["width"]&.value
|
width = node.attributes["width"]&.value
|
||||||
height = node.attributes["height"]&.value
|
height = node.attributes["height"]&.value
|
||||||
|
|
|
@ -45,6 +45,20 @@ RSpec.describe InlineUploads do
|
||||||
expect(InlineUploads.process(md)).to eq(md)
|
expect(InlineUploads.process(md)).to eq(md)
|
||||||
end
|
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
|
it "should not correct code blocks" do
|
||||||
md = "`<a class=\"attachment\" href=\"#{upload2.url}\">In Code Block</a>`"
|
md = "`<a class=\"attachment\" href=\"#{upload2.url}\">In Code Block</a>`"
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user