Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
copy_messages: Let the browser copy selections with no message content.
Apoorva Pendseab4478d20a86
diff --git a/web/e2e-tests/copy-messages.test.ts b/web/e2e-tests/copy-messages.test.tsindex 3d86b7952e8..5450bf6e54b 100644--- a/web/e2e-tests/copy-messages.test.ts+++ b/web/e2e-tests/copy-messages.test.ts@@ -261,6 +261,51 @@ async function test_multiple_message_selection_with_partially_selected_bookend_massert.deepStrictEqual(actual_copied_lines, expected_copied_lines);}+async function test_copying_selection_with_no_message_content(page: Page): Promise<void> {+ // A recipient header plus the first message's sender name has no+ // `.message_content`.+ const result = await page.evaluate(() => {+ const header = document.querySelector(+ '.message-list .message_header[data-topic-name="copy-paste-topic #2"]',+ );+ const sender_name = header+ ?.closest(".recipient_row")+ ?.querySelector(":scope .message_row .sender_name");+ const header_contents = header?.querySelector(".message-header-contents");+ if (!header_contents || !sender_name) {+ throw new Error("Expected topic header and sender name");+ }++ const range = document.createRange();+ range.setStart(header_contents, 0);+ range.setEndAfter(sender_name);+ window.getSelection()!.removeAllRanges();+ window.getSelection()!.addRange(range);++ const clipboard_data = new DataTransfer();+ const copy_event = new ClipboardEvent("copy", {+ bubbles: true,+ cancelable: true,+ clipboardData: clipboard_data,+ });+ document.dispatchEvent(copy_event);++ return {+ default_prevented: copy_event.defaultPrevented,+ copied_html: clipboard_data.getData("text/html"),+ copied_text: clipboard_data.getData("text/plain"),+ selection_text: window.getSelection()!.toString(),+ };+ });++ // Synthetic ClipboardEvent: native copy does not fill clipboard_data.+ // defaultPrevented === false is what shows the handler stepped aside.+ assert.equal(result.default_prevented, false);+ assert.equal(result.copied_html, "");+ assert.equal(result.copied_text, "");+ assert.ok(result.selection_text.includes("Desdemona"));+}+async function copy_paste_test(page: Page): Promise<void> {await common.log_in(page);await common.send_multiple_messages(page, [@@ -307,6 +352,7 @@ async function copy_paste_test(page: Page): Promise<void> {await test_copying_all_from_prev_first_from_next(page);await test_copying_messages_from_several_topics(page);await test_timestamp_clipboard_has_datetime(page);+ await test_copying_selection_with_no_message_content(page);await test_multiple_message_selection_with_partially_selected_bookend_messages(page);}diff --git a/web/src/copy_messages.ts b/web/src/copy_messages.tsindex 02f5c0a8782..6dbba4a9c3a 100644--- a/web/src/copy_messages.ts+++ b/web/src/copy_messages.ts@@ -179,17 +179,18 @@ been even the best workaround back then).https://github.com/zulip/zulip/commit/fc0b7c00f16316a554349f0ad58c6517ebdd7ac4-The idea is that we build a temp div, let jQuery process the-selection, then restore the selection on a zero-second timer back-to the original selection.+The idea is that we build a temp div, which copy_handler then+writes to the clipboard as HTML and plain text. Returning `false`+indicates that we prefer the browser's native copy and have not+manipulated the temp copy div.Do not be afraid to change this code if you understandhow modern browsers deal with copy/paste. Just testyour changes carefully.*/-function construct_copy_div($div: JQuery, start_id: number, end_id: number): void {+function construct_copy_div($div: JQuery, start_id: number, end_id: number): boolean {if (message_lists.current === undefined) {- return;+ return false;}let $first_message_element;let $last_message_element;@@ -223,7 +224,7 @@ function construct_copy_div($div: JQuery, start_id: number, end_id: number): voicopy_rows.splice(-1, 1);if (copy_rows.length === 0) {// In case this just involved selecting the username of a message.- return;+ return false;}}assert(copy_rows[0] && copy_rows.at(-1));@@ -319,6 +320,7 @@ function construct_copy_div($div: JQuery, start_id: number, end_id: number): voiif (should_include_start_recipient_header) {construct_recipient_header($start_row).prependTo($div);}+ return true;}// We want to grab the closest katex span up the tree@@ -603,7 +605,11 @@ export function copy_handler(ev: ClipboardEvent): boolean {// more difficult since we can get a range (start_id and end_id) for// each selection `Range`.const $div = $("<div>");- construct_copy_div($div, start_id, end_id);+ if (!construct_copy_div($div, start_id, end_id)) {+ // No message content in the selection so we let the browser copy+ // the highlighted text natively.+ return false;+ }const html_content = $div.html().trim();const plain_text = $div.text().trim();