Skip to content

A platform for community discussion. Free, open, simple.

FIX: add data attrs to quoted images (#35455)

David Battersby9fa647390cc7
diff --git a/plugins/chat/lib/chat/message_processor.rb b/plugins/chat/lib/chat/message_processor.rb
index cfa5a449adb..17d7d3ac85c 100644
--- a/plugins/chat/lib/chat/message_processor.rb
+++ b/plugins/chat/lib/chat/message_processor.rb
@@ -60,8 +60,19 @@ module Chat
@doc
.css("img")
.each do |img|
- next if img["class"]&.include?("emoji") || img["class"]&.include?("avatar")
- img["class"] = "#{img["class"]} lightbox".strip
+ if img["class"]&.include?("emoji") || img["class"]&.include?("avatar") ||
+ img["data-base62-sha1"].blank?
+ next
+ end
+
+ sha1 = Upload.sha1_from_base62_encoded(img["data-base62-sha1"])
+ if upload = Upload.find_by(sha1: sha1)
+ img["data-large-src"] = upload.url
+ img["data-download-href"] = upload.short_path
+ img["data-target-width"] = upload.width
+ img["data-target-height"] = upload.height
+ img["class"] = "#{img["class"]} lightbox".strip
+ end
end
end
diff --git a/plugins/chat/spec/lib/chat/message_processor_spec.rb b/plugins/chat/spec/lib/chat/message_processor_spec.rb
index 418b9c61183..85c11972802 100644
--- a/plugins/chat/spec/lib/chat/message_processor_spec.rb
+++ b/plugins/chat/spec/lib/chat/message_processor_spec.rb
@@ -26,10 +26,13 @@ RSpec.describe Chat::MessageProcessor do
end
describe "#add_lightbox_to_images" do
+ fab!(:upload) { Fabricate(:upload, width: 800, height: 600) }
+ let(:base62) { Upload.base62_sha1(upload.sha1) }
+
it "adds lightbox class to quoted images" do
cooked_html = <<~HTML
<blockquote>
- <img src="https://example.com/image.jpg" width="500" height="300">
+ <img src="#{upload.url}" width="500" height="300" data-base62-sha1="#{base62}">
</blockquote>
HTML
@@ -42,6 +45,10 @@ RSpec.describe Chat::MessageProcessor do
img = doc.at_css("img")
expect(img["class"]).to include("lightbox")
+ expect(img["data-large-src"]).to eq(upload.url)
+ expect(img["data-download-href"]).to eq(upload.short_path)
+ expect(img["data-target-width"]).to eq(upload.width.to_s)
+ expect(img["data-target-height"]).to eq(upload.height.to_s)
end
end
end