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: Move download processing nearer to links.

Karl Stolleyf9b52d4df591
diff --git a/web/src/postprocess_content.ts b/web/src/postprocess_content.ts
index 2fc52f9f1fc..229c62ad614 100644
--- a/web/src/postprocess_content.ts
+++ b/web/src/postprocess_content.ts
@@ -66,6 +66,38 @@ export function postprocess_content(html: string): string {
elt.removeAttribute("target");
}
+ if (!elt.parentElement?.classList.contains("message_inline_image")) {
+ // For non-media (images, video) user uploads, the following block
+ // ensures that the title attribute always displays the filename,
+ // as a security measure.
+ let title: string;
+ let legacy_title: string;
+ if (
+ url.origin === window.location.origin &&
+ url.pathname.startsWith("/user_uploads/")
+ ) {
+ // We add the word "download" to make clear what will
+ // happen when clicking the file. This is particularly
+ // important in the desktop app, where hovering a URL does
+ // not display the URL like it does in the web app.
+ title = legacy_title = $t(
+ {defaultMessage: "Download {filename}"},
+ {
+ filename: decodeURIComponent(
+ url.pathname.slice(url.pathname.lastIndexOf("/") + 1),
+ ),
+ },
+ );
+ } else {
+ title = url.toString();
+ legacy_title = href;
+ }
+ elt.setAttribute(
+ "title",
+ ["", legacy_title].includes(elt.title) ? title : `${title}\n${elt.title}`,
+ );
+ }
+
if (elt.querySelector("img") || elt.querySelector("video")) {
// We want a class to refer to media links
elt.classList.add("media-anchor-element");
@@ -193,35 +225,6 @@ export function postprocess_content(html: string): string {
inline_image.classList.add("landscape-thumbnail");
}
}
- } else {
- // For non-image user uploads, the following block ensures that the title
- // attribute always displays the filename as a security measure.
- let title: string;
- let legacy_title: string;
- if (
- url.origin === window.location.origin &&
- url.pathname.startsWith("/user_uploads/")
- ) {
- // We add the word "download" to make clear what will
- // happen when clicking the file. This is particularly
- // important in the desktop app, where hovering a URL does
- // not display the URL like it does in the web app.
- title = legacy_title = $t(
- {defaultMessage: "Download {filename}"},
- {
- filename: decodeURIComponent(
- url.pathname.slice(url.pathname.lastIndexOf("/") + 1),
- ),
- },
- );
- } else {
- title = url.toString();
- legacy_title = href;
- }
- elt.setAttribute(
- "title",
- ["", legacy_title].includes(elt.title) ? title : `${title}\n${elt.title}`,
- );
}
}
diff --git a/web/tests/postprocess_content.test.cjs b/web/tests/postprocess_content.test.cjs
index 8adab165d73..0623c14fe76 100644
--- a/web/tests/postprocess_content.test.cjs
+++ b/web/tests/postprocess_content.test.cjs
@@ -81,7 +81,7 @@ run_test("postprocess_media_and_embeds", () => {
'<a class="message_embed_image" href="https://example.com/about" style="background-image: url(&quot;https://example.com/preview.jpeg&quot;)" target="_blank" rel="noopener noreferrer" title="https://example.com/about"></a>' +
'<div class="data-container">' +
'<div class="message_embed_title">' +
- '<a href="https://example.com/about" target="_blank" rel="noopener noreferrer" class="message-embed-title-link" title="https://example.com/about">About us</a>' +
+ '<a href="https://example.com/about" target="_blank" rel="noopener noreferrer" title="https://example.com/about" class="message-embed-title-link">About us</a>' +
"</div>" +
'<div class="message_embed_description">All about us.</div>' +
"</div>" +