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.

messages: Show tippy tooltip for inline images.

Austin Riba3df0cacd9e1e
diff --git a/frontend_tests/node_tests/util.js b/frontend_tests/node_tests/util.js
index aaa5204f2d5..4313a2f0096 100644
--- a/frontend_tests/node_tests/util.js
+++ b/frontend_tests/node_tests/util.js
@@ -282,12 +282,18 @@ run_test("clean_user_content_links", () => {
'<a href="http://zulip.zulipdev.com/user_uploads/w/ha/tever/file.png">upload</a> ' +
'<a href="http://localhost:NNNN">invalid</a> ' +
'<a href="javascript:alert(1)">unsafe</a> ' +
- '<a href="/#fragment" target="_blank">fragment</a>',
+ '<a href="/#fragment" target="_blank">fragment</a>' +
+ '<div class="message_inline_image">' +
+ '<a href="http://zulip.zulipdev.com/user_uploads/w/ha/tever/inline.png" title="inline image">upload</a> ' +
+ "</div>",
),
'<a href="http://example.com" target="_blank" rel="noopener noreferrer" title="http://example.com/">good</a> ' +
'<a href="http://zulip.zulipdev.com/user_uploads/w/ha/tever/file.png" target="_blank" rel="noopener noreferrer" title="translated: Download file.png">upload</a> ' +
"<a>invalid</a> " +
"<a>unsafe</a> " +
- '<a href="/#fragment" title="http://zulip.zulipdev.com/#fragment">fragment</a>',
+ '<a href="/#fragment" title="http://zulip.zulipdev.com/#fragment">fragment</a>' +
+ '<div class="message_inline_image">' +
+ '<a href="http://zulip.zulipdev.com/user_uploads/w/ha/tever/inline.png" target="_blank" rel="noopener noreferrer" aria-label="inline image">upload</a> ' +
+ "</div>",
);
});
diff --git a/static/js/tippyjs.js b/static/js/tippyjs.js
index c255b3163a0..94bc8203e09 100644
--- a/static/js/tippyjs.js
+++ b/static/js/tippyjs.js
@@ -1,12 +1,15 @@
import $ from "jquery";
import tippy, {delegate} from "tippy.js";
+import render_message_inline_image_tooltip from "../templates/message_inline_image_tooltip.hbs";
+
import {$t} from "./i18n";
import * as message_lists from "./message_lists";
import * as popover_menus from "./popover_menus";
import * as reactions from "./reactions";
import * as rows from "./rows";
import * as timerender from "./timerender";
+import {parse_html} from "./ui_util";
// For tooltips without data-tippy-content, we use the HTML content of
// a <template> whose id is given by data-tooltip-template-id.
@@ -245,4 +248,23 @@ export function initialize() {
},
appendTo: () => document.body,
});
+
+ delegate("body", {
+ target: ".message_inline_image > a > img",
+ appendTo: () => document.body,
+ // Add a short delay so the user can mouseover several inline images without
+ // tooltips showing and hiding rapidly
+ delay: [300, 20],
+ onShow(instance) {
+ // Some message_inline_images aren't actually images with a title,
+ // for example youtube videos, so we default to the actual href
+ const title =
+ $(instance.reference).parent().attr("aria-label") ||
+ $(instance.reference).parent().attr("href");
+ instance.setContent(parse_html(render_message_inline_image_tooltip({title})));
+ },
+ onHidden(instance) {
+ instance.destroy();
+ },
+ });
}
diff --git a/static/js/util.js b/static/js/util.js
index cee7fa0dd0d..bc20924366b 100644
--- a/static/js/util.js
+++ b/static/js/util.js
@@ -290,26 +290,41 @@ export function clean_user_content_links(html) {
elt.removeAttribute("target");
}
- // Ensure that the title displays the real URL.
- let title;
- let legacy_title;
- 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: url.pathname.slice(url.pathname.lastIndexOf("/") + 1)},
- );
+ const is_inline_image =
+ elt.parentElement && elt.parentElement.classList.contains("message_inline_image");
+ if (is_inline_image) {
+ // For inline images we want to handle the tooltips explicitly, and disable
+ // the browser's built in handling of the title attribute.
+ if (elt.getAttribute("title")) {
+ elt.setAttribute("aria-label", elt.getAttribute("title"));
+ elt.removeAttribute("title");
+ }
} else {
- title = url;
- legacy_title = href;
+ // For non-image user uploads, the following block ensures that the title
+ // attribute always displays the filename as a security measure.
+ let title;
+ let legacy_title;
+ 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: url.pathname.slice(url.pathname.lastIndexOf("/") + 1)},
+ );
+ } else {
+ title = url;
+ legacy_title = href;
+ }
+ elt.setAttribute(
+ "title",
+ ["", legacy_title].includes(elt.title) ? title : `${title}\n${elt.title}`,
+ );
}
- elt.setAttribute(
- "title",
- ["", legacy_title].includes(elt.title) ? title : `${title}\n${elt.title}`,
- );
}
return content.innerHTML;
}
diff --git a/static/templates/message_inline_image_tooltip.hbs b/static/templates/message_inline_image_tooltip.hbs
new file mode 100644
index 00000000000..5782555b191
--- /dev/null
+++ b/static/templates/message_inline_image_tooltip.hbs
@@ -0,0 +1,3 @@
+<strong>{{ title }}</strong>
+<br/>
+{{t 'Click to view or download.'}}