paste: More robust check to verify if clipboard data is an image.

The commit f863a9b567 had modified
jquery.filedrop's paste method to exit early if any of the items in the
clipboardData is of the string kind. The early exit was added to prevent pasting
an image thumbnail for text copied from software like MS Word, instead of
pasting the actual copied text content. When copying an image in a (modern?)
Browser, though, the clipboard seems to contain a html `img` tag item, along
with the actual image file. This resulted in pastes being broken.

This commit modifies the condition checked for the early exit. We now actually
look at the html content in the clipboard to see if it is an `img` tag, in which
case we upload the image, instead of exiting early.

Closes #7130.
This commit is contained in:
Puneeth Chaganti
2019-01-19 22:45:17 +05:30
committed by Tim Abbott
parent 8cf5810fc0
commit 9c377a05f3
3 changed files with 88 additions and 12 deletions

View File

@@ -235,8 +235,16 @@ exports.paste_handler = function (event) {
if (clipboardData.getData) {
var paste_html = clipboardData.getData('text/html');
if (paste_html && page_params.development_environment) {
event.preventDefault();
var text = exports.paste_handler_converter(paste_html);
var mdImageRegex = /^!\[.*\]\(.*\)$/;
if (text.match(mdImageRegex)) {
// This block catches cases where we are pasting an
// image into Zulip, which should be handled by the
// jQuery filedrop library, not this code path.
return;
}
event.preventDefault();
event.stopPropagation();
compose_ui.insert_syntax_and_focus(text);
}
}