Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

postprocess: Conditionally structure galleries in spans or divs.

Karl Stolley381fe5696004
diff --git a/web/src/postprocess_content.ts b/web/src/postprocess_content.ts
index 3685d5140f5..8c082728b83 100644
--- a/web/src/postprocess_content.ts
+++ b/web/src/postprocess_content.ts
@@ -337,9 +337,25 @@ export function postprocess_content(html: string): string {
// content (or is the first in the message) and need to create a
// gallery for it, and perhaps other adjacent sibling media elements,
// if they exist.
- gallery_element = inertDocument.createElement("div");
+ if (elt.classList.contains("message-media-gallery-image")) {
+ // Because inline images may be presented in galleries in the middle
+ // of a paragraph, we create those as `<span>` elements. That prevents
+ // the client-side markdown from doing a slipshod job of inserting
+ // empty `<p>` elements or leaving orphaned text nodes around a `<div>`,
+ // which isn't allowed to appear inside of a `<p>`.
+ gallery_element = inertDocument.createElement("span");
+ } else {
+ // However, for legacy galleries that always appear after a paragraph,
+ // we create a `<div>` element.
+ gallery_element = inertDocument.createElement("div");
+ }
+
+ // Regardless of what element the gallery is, we add the
+ // .message-thumbnail-gallery class, whose CSS selectors
+ // will style this as a flexbox regardless.
gallery_element.classList.add("message-thumbnail-gallery");
- // We insert the gallery just before the media element we've found
+
+ // We insert a new gallery just before the media element we've found
elt.before(gallery_element);
}
diff --git a/web/tests/postprocess_content.test.cjs b/web/tests/postprocess_content.test.cjs
index 6132a098839..fe3ad06aa0d 100644
--- a/web/tests/postprocess_content.test.cjs
+++ b/web/tests/postprocess_content.test.cjs
@@ -193,7 +193,7 @@ run_test("inline_image_galleries", ({override}) => {
"</span>" +
"</p>" +
"<p>" +
- '<div class="message-thumbnail-gallery">' +
+ '<span class="message-thumbnail-gallery">' +
'<span class="message-media-inline-image message-media-gallery-image">' +
'<a href="/user_uploads/path/to/image-wide.png" target="_blank" rel="noopener noreferrer" class="media-anchor-element" aria-label="image">' +
'<img alt="image" class="inline-image media-image-element landscape-thumbnail" data-original-content-type="image/png" data-original-dimensions="900x600" data-original-src="/user_uploads/path/to/image-wide.png" src="/user_uploads/thumbnail/path/to/image.png/840x560.webp" loading="lazy" width="900" height="600" style="width: 15em;">' +
@@ -204,7 +204,7 @@ run_test("inline_image_galleries", ({override}) => {
'<img alt="image" class="inline-image media-image-element portrait-thumbnail" data-original-content-type="image/png" data-original-dimensions="600x900" data-original-src="/user_uploads/path/to/image-tall.png" src="/user_uploads/thumbnail/path/to/image.png/840x560.webp" loading="lazy" width="600" height="900" style="width: 6.666666666666667em;">' +
"</a>" +
"</span>" +
- "</div>" +
+ "</span>" +
"</p>",
);
});