Skip to content

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

FIX: Lightbox failed to start in components due to null imgEl element. (#36310)

small-lovely-cat1d592aec3e14
diff --git a/frontend/discourse/app/lib/lightbox.js b/frontend/discourse/app/lib/lightbox.js
index e4613347ab8..02f99378fb2 100644
--- a/frontend/discourse/app/lib/lightbox.js
+++ b/frontend/discourse/app/lib/lightbox.js
@@ -291,16 +291,26 @@ export default async function lightbox(
data.thumbCropped = true;
data.src = data.src || el.getAttribute("data-large-src");
- data.origSrc = imgEl.getAttribute("data-orig-src");
- data.title = el.title || imgEl.alt || imgEl.title;
- data.base62SHA1 = imgEl.getAttribute("data-base62-sha1");
+ data.origSrc =
+ imgEl?.getAttribute("data-orig-src") ||
+ el.getAttribute("data-orig-src") ||
+ null;
+ data.title = el.title || imgEl?.alt || imgEl?.title || null;
+ data.base62SHA1 =
+ imgEl?.getAttribute("data-base62-sha1") ||
+ el.getAttribute("data-base62-sha1") ||
+ null;
data.details = imgInfo;
data.w = data.width = width;
data.h = data.height = height;
data.targetWidth =
- el.getAttribute("data-target-width") || imgEl.getAttribute("width");
+ el.getAttribute("data-target-width") ||
+ imgEl?.getAttribute("width") ||
+ null;
data.targetHeight =
- el.getAttribute("data-target-height") || imgEl.getAttribute("height");
+ el.getAttribute("data-target-height") ||
+ imgEl?.getAttribute("height") ||
+ null;
// So we can attach things like a Post model from the caller.
Object.keys(additionalData).forEach((key) => {
diff --git a/spec/system/lightbox_spec.rb b/spec/system/lightbox_spec.rb
index 2ee057dacc6..d1109e60eaf 100644
--- a/spec/system/lightbox_spec.rb
+++ b/spec/system/lightbox_spec.rb
@@ -246,6 +246,9 @@ describe "Lightbox | Photoswipe", type: :system do
topic_page.visit_topic(topic)
lightbox_link = topic_page.post_by_number(1).find("a.lightbox")
+ lightbox_link.click
+ expect(lightbox).to be_visible
+
expect(lightbox_link["data-target-width"]).to eq(upload_1.width.to_s)
expect(lightbox_link["data-target-height"]).to eq(upload_1.height.to_s)
end