Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
postprocess: Handle galleries for line-broken images.
Karl Stolleycd54eaca976c
diff --git a/web/src/postprocess_content.ts b/web/src/postprocess_content.tsindex 8c082728b83..c30f5f8e347 100644--- a/web/src/postprocess_content.ts+++ b/web/src/postprocess_content.ts@@ -123,23 +123,36 @@ export function postprocess_content(html: string): string {const media_wrapper = inertDocument.createElement("span");media_wrapper.classList.add("message-media-inline-image");- // If one or more inline images sit in a paragraph containing no- // other text content, we will include it in a gallery via the- // logic further down in this file.+ // If one or more inline images sit in a paragraph in isolation,+ // or are separated only by line breaks, we will include those+ // images in a gallery via the logic further down in this file.const inline_img_parent_elt = inline_img_elt.parentElement;// We want to determine the length after trimming out the spaces// from line breaks; this value will be precisely zero if the// containing paragraph has no text content, including things// that might be tucked in a link or a bold tag, etc.- const inline_img_parent_elt_text_length = inline_img_parent_elt?.textContent?.trim().length;const inline_img_parent_elt_name = inline_img_parent_elt?.tagName.toLowerCase();- if (inline_img_parent_elt_name === "p" && inline_img_parent_elt_text_length === 0) {+ if (inline_img_parent_elt_name === "p" && !is_media_run_inline_with_text(inline_img_elt)) {media_wrapper.classList.add("message-media-gallery-image");- // Multiple images will be separated by break tags, which will- // be unnecessary (and make trouble for correctly placing+ // Multiple images may be separated by break tags, which will+ // be unnecessary and make trouble for correctly placing// adjacent images into a single gallery, when we process them.- inline_img_parent_elt?.querySelector("br")?.remove();+ // However, in a message with deliberate line breaks elsewhere,+ // like between lines of text, we need to be careful to preserve+ // those and instead just remove those that precede the+ // inline_img_elt we're working with.+ const image_elt_prev_element_sibling = inline_img_elt.previousElementSibling;++ // We remove any previous element-sibling break tags, but leave+ // the any trailing break tags to properly detect other images+ // that may need to be included in a gallery. Any trailing break+ // tags are removed at the point that the gallery gets inserted+ // into the DOM (at which point they will be trailing the gallery+ // itself).+ if (image_elt_prev_element_sibling?.tagName?.toLowerCase() === "br") {+ image_elt_prev_element_sibling.remove();+ }}const media_link = inertDocument.createElement("a");@@ -359,13 +372,55 @@ export function postprocess_content(html: string): string {elt.before(gallery_element);}- // Finally, the media element gets moved into the current gallery+ // Move the media element into the current gallerygallery_element?.append(elt);++ // Delete any trailing <br> tag after new gallery element; this can+ // happen when there's an image trailed by a break and more text.+ if (gallery_element?.nextElementSibling?.tagName.toLowerCase() === "br") {+ gallery_element.nextElementSibling.remove();+ }}return template.innerHTML;}+// If an image is run inline with text--that is, there are non-whitespace+// text nodes adjacent the image--we will not put it into a gallery.+function is_media_run_inline_with_text(media_elt: Element): boolean {+ const media_elt_previous_sibling_node = media_elt.previousSibling;+ const media_elt_next_sibling_node = media_elt.nextSibling;++ // A standalone image in its own paragraph will have no sibling nodes+ if (media_elt_previous_sibling_node === null && media_elt_next_sibling_node === null) {+ return false;+ }++ // For images that have text nodes, we need to consider the nodeValue;+ // these will be `null` for element nodes. We do not want to trim these+ // values, because that would wipe out newlines, "\n", which we are+ // interested in detecting.+ const previous_sibling_node_value = media_elt_previous_sibling_node?.nodeValue;+ const next_sibling_node_value = media_elt_next_sibling_node?.nodeValue;++ // For images that have adjacent element nodes, we examine the nodeName.+ const previous_sibling_node_name = media_elt_previous_sibling_node?.nodeName?.toLowerCase();+ const next_sibling_node_name = media_elt_next_sibling_node?.nodeName?.toLowerCase();++ // Any adjacent newlines or break tags mean that this image not run+ // inline with text.+ if (+ previous_sibling_node_value === "\n" ||+ next_sibling_node_value === "\n" ||+ previous_sibling_node_name === "br" ||+ next_sibling_node_name === "br"+ ) {+ return false;+ }++ return true;+}+// Process single-paragraph messages that contain only emoji.function process_emoji_only_message(content: DocumentFragment): void {// Exit as quickly as possible when more than one child elementdiff --git a/web/tests/postprocess_content.test.cjs b/web/tests/postprocess_content.test.cjsindex fe3ad06aa0d..7096da72b7e 100644--- a/web/tests/postprocess_content.test.cjs+++ b/web/tests/postprocess_content.test.cjs@@ -154,9 +154,11 @@ run_test("inline_image_galleries", ({override}) => {"</p>" +"<p>" +'<img alt="image" class="inline-image" 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/900x600.webp">' +- "<br>" ++ "<br>\n" +'<img alt="image" class="inline-image" 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/600x900.webp">' +- "</p>",+ "</p>" ++ '<p>And here is a gallery in the inline style, with text before the images...<br>\n<img alt="image-01.png" class="inline-image" data-original-content-type="image/png" data-original-dimensions="800x800" data-original-src="/user_uploads/2/17/k0c4iGRefC2aCr4Jxf6NQdfH/image-01.png" src="/user_uploads/thumbnail/2/17/k0c4iGRefC2aCr4Jxf6NQdfH/image-01.png/840x560.webp"><br>\n<img alt="image-02.png" class="inline-image" data-original-content-type="image/png" data-original-dimensions="800x800" data-original-src="/user_uploads/2/3f/B0vUyCSpixMgDLG29fKeUkk6/image-02.png" src="/user_uploads/thumbnail/2/3f/B0vUyCSpixMgDLG29fKeUkk6/image-02.png/840x560.webp"><br>\n...and text after (again, line breaks only, no new paragraphs).</p>' ++ '<p><img alt="square.png" class="inline-image" data-original-content-type="image/png" data-original-dimensions="800x800" data-original-src="/user_uploads/2/48/mo114lAto6fft973UYWtik2T/square.png" src="/user_uploads/thumbnail/2/48/mo114lAto6fft973UYWtik2T/square.png/840x560.webp"></p>',),"<p>Message text</p>" +'<div class="message-thumbnail-gallery">' +@@ -204,8 +206,13 @@ 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>" +- "</span>" +- "</p>",+ "</span>\n" ++ "</p>" ++ "<p>And here is a gallery in the inline style, with text before the images...\n" ++ '<span class="message-thumbnail-gallery"><span class="message-media-inline-image message-media-gallery-image"><a href="/user_uploads/2/17/k0c4iGRefC2aCr4Jxf6NQdfH/image-01.png" target="_blank" rel="noopener noreferrer" class="media-anchor-element" aria-label="image-01.png"><img alt="image-01.png" class="inline-image media-image-element portrait-thumbnail" data-original-content-type="image/png" data-original-dimensions="800x800" data-original-src="/user_uploads/2/17/k0c4iGRefC2aCr4Jxf6NQdfH/image-01.png" src="/user_uploads/thumbnail/2/17/k0c4iGRefC2aCr4Jxf6NQdfH/image-01.png/840x560.webp" loading="lazy" width="800" height="800" style="width: 10em;"></a></span><span class="message-media-inline-image message-media-gallery-image"><a href="/user_uploads/2/3f/B0vUyCSpixMgDLG29fKeUkk6/image-02.png" target="_blank" rel="noopener noreferrer" class="media-anchor-element" aria-label="image-02.png"><img alt="image-02.png" class="inline-image media-image-element portrait-thumbnail" data-original-content-type="image/png" data-original-dimensions="800x800" data-original-src="/user_uploads/2/3f/B0vUyCSpixMgDLG29fKeUkk6/image-02.png" src="/user_uploads/thumbnail/2/3f/B0vUyCSpixMgDLG29fKeUkk6/image-02.png/840x560.webp" loading="lazy" width="800" height="800" style="width: 10em;"></a></span></span>\n' ++ "\n" ++ "...and text after (again, line breaks only, no new paragraphs).</p>" ++ '<p><span class="message-thumbnail-gallery"><span class="message-media-inline-image message-media-gallery-image"><a href="/user_uploads/2/48/mo114lAto6fft973UYWtik2T/square.png" target="_blank" rel="noopener noreferrer" class="media-anchor-element" aria-label="square.png"><img alt="square.png" class="inline-image media-image-element portrait-thumbnail" data-original-content-type="image/png" data-original-dimensions="800x800" data-original-src="/user_uploads/2/48/mo114lAto6fft973UYWtik2T/square.png" src="/user_uploads/thumbnail/2/48/mo114lAto6fft973UYWtik2T/square.png/840x560.webp" loading="lazy" width="800" height="800" style="width: 10em;"></a></span></span></p>',);});