From: Tom Hughes Date: Sun, 4 Aug 2024 11:35:20 +0000 (+0100) Subject: Merge remote-tracking branch 'upstream/pull/5041' X-Git-Tag: live~805 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/add32a4fe97255a4db0627a8550f4c37fa0ee4ca?hp=3cc045846820cb6a5c06dba16b448487ebbd2c35 Merge remote-tracking branch 'upstream/pull/5041' --- diff --git a/lib/rich_text.rb b/lib/rich_text.rb index f19d3d3a9..edfa535f4 100644 --- a/lib/rich_text.rb +++ b/lib/rich_text.rb @@ -104,13 +104,17 @@ module RichText end def first_image_element(element) - return element if element.type == :img + return element if image?(element) && element.attr["src"].present? element.children.find do |child| nested_image = first_image_element(child) break nested_image if nested_image end end + + def image?(element) + element.type == :img || (element.type == :html_element && element.value == "img") + end end class Text < Base diff --git a/test/lib/rich_text_test.rb b/test/lib/rich_text_test.rb index 8dc9e49b1..e601c36fc 100644 --- a/test/lib/rich_text_test.rb +++ b/test/lib/rich_text_test.rb @@ -275,6 +275,41 @@ class RichTextTest < ActiveSupport::TestCase assert_equal "https://example.com/image1.jpg", r.image end + def test_markdown_image_with_empty_src + r = RichText.new("markdown", "![invalid]()") + assert_nil r.image + end + + def test_markdown_skip_image_with_empty_src + r = RichText.new("markdown", "![invalid]() ![valid](https://example.com/valid.gif)") + assert_equal "https://example.com/valid.gif", r.image + end + + def test_markdown_html_image + r = RichText.new("markdown", "") + assert_equal "https://example.com/img_element.png", r.image + end + + def test_markdown_html_image_with_empty_src + r = RichText.new("markdown", "") + assert_nil r.image + end + + def test_markdown_skip_html_image_with_empty_src + r = RichText.new("markdown", " ") + assert_equal "https://example.com/next_img_element.png", r.image + end + + def test_markdown_html_image_without_src + r = RichText.new("markdown", "") + assert_nil r.image + end + + def test_markdown_skip_html_image_without_src + r = RichText.new("markdown", " ") + assert_equal "https://example.com/next_img_element.png", r.image + end + private def assert_html(richtext, &block)