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.

copy: Append/prepend ellipsis when partially selecting messages.

apoorvapendseb4dae70aaf46
diff --git a/web/e2e-tests/copy-messages.test.ts b/web/e2e-tests/copy-messages.test.ts
index 8b420dabe63..4bf615babe7 100644
--- a/web/e2e-tests/copy-messages.test.ts
+++ b/web/e2e-tests/copy-messages.test.ts
@@ -245,7 +245,7 @@ async function test_multiple_message_selection_with_partially_selected_bookend_m
"Verona > copy-paste-topic #1 | Today",
"Desdemona:",
// w/o partial selection: "copy paste test B",
- "paste test B",
+ "...paste test B",
"Verona > copy-paste-topic #2 | Today",
"Desdemona:",
"copy paste test C",
@@ -256,7 +256,7 @@ async function test_multiple_message_selection_with_partially_selected_bookend_m
"Verona > copy-paste-topic #3 | Today",
"Desdemona:",
// w/o partial selection: "copy paste test F",
- "copy paste",
+ "copy paste...",
];
assert.deepStrictEqual(actual_copied_lines, expected_copied_lines);
}
diff --git a/web/src/copy_messages.ts b/web/src/copy_messages.ts
index 8066c63f361..294f18c56e3 100644
--- a/web/src/copy_messages.ts
+++ b/web/src/copy_messages.ts
@@ -75,6 +75,8 @@ function get_selected_message_content_elements(): NodeListOf<HTMLElement> | unde
// from a selection range, as the intermediate selected messages
// anyways contain the entire `.message_content` HTML.
function get_html_for_bookend_message_content(
+ type: RangeContainer,
+ original_message_content_element: Element,
selected_message_content_element: Node | undefined,
): string {
assert(window.getSelection()?.rangeCount === 1);
@@ -90,6 +92,38 @@ function get_html_for_bookend_message_content(
if (selected_message_content_element.classList.contains("status-message")) {
return `<div>` + selected_message_content_element.outerHTML + `</div>`;
}
+
+ // If the selected `.message_content` HTML is same as the complete `.message_content` HTML,
+ // we return early and don't append/prepend ellipsis text.
+ if (
+ selected_message_content_element.innerHTML.trim() ===
+ original_message_content_element.innerHTML.trim()
+ ) {
+ return selected_message_content_element.innerHTML;
+ }
+
+ // The ellipsis marks where the partial selection was truncated, so it
+ // belongs within the text flow of the truncated paragraph. Inserting it
+ // inside the first/last paragraph (rather than as a sibling of it) keeps
+ // turndown from rendering it on its own line, separated from the text by
+ // a blank line.
+ const $ellipsis_span = $("<span>").text("...");
+ const $content_children = $(selected_message_content_element).children();
+ if (type === "start") {
+ const $first_child = $content_children.first();
+ if ($first_child.is("p")) {
+ the($first_child).prepend(the($ellipsis_span));
+ } else {
+ selected_message_content_element.prepend(the($ellipsis_span));
+ }
+ } else {
+ const $last_child = $content_children.last();
+ if ($last_child.is("p")) {
+ the($last_child).append(the($ellipsis_span));
+ } else {
+ selected_message_content_element.append(the($ellipsis_span));
+ }
+ }
return selected_message_content_element.innerHTML;
}
@@ -199,7 +233,11 @@ function construct_copy_div($div: JQuery, start_id: number, end_id: number): voi
);
assert(first_selected_message_content_element && last_selected_message_content_element);
$first_message_element = $(
- get_html_for_bookend_message_content(selected_message_content_elements[0]),
+ get_html_for_bookend_message_content(
+ "start",
+ first_selected_message_content_element,
+ selected_message_content_elements[0],
+ ),
);
// We don't want to append the same content as the first message in the selection
@@ -208,7 +246,11 @@ function construct_copy_div($div: JQuery, start_id: number, end_id: number): voi
if (selected_message_content_elements.length > 1) {
const len = selected_message_content_elements.length;
$last_message_element = $(
- get_html_for_bookend_message_content(selected_message_content_elements[len - 1]),
+ get_html_for_bookend_message_content(
+ "end",
+ last_selected_message_content_element,
+ selected_message_content_elements[len - 1],
+ ),
);
}
}
diff --git a/web/tests/compose_paste.test.cjs b/web/tests/compose_paste.test.cjs
index 39a7d024a19..5f955b1a9b1 100644
--- a/web/tests/compose_paste.test.cjs
+++ b/web/tests/compose_paste.test.cjs
@@ -564,6 +564,15 @@ run_test("paste_handler_converter", () => {
"**Desdemona:**\nintro text\n* item one\n* item two\n* item three",
);
+ // Partially selecting the first message inserts the "..." ellipsis inside
+ // the leading paragraph, so it stays glued to the start of the truncated
+ // text (on the same line) rather than landing on its own line.
+ input = `<html><body><!--StartFragment--><b>Iago: </b><div><span>...</span>of Nephelococcygia</div>\n<ul>\n<li>Myrmica ruginodis</li>\n<li>Myrmica kotokui</li>\n</ul><!--EndFragment--></body></html>`;
+ assert.equal(
+ compose_paste.paste_handler_converter(input),
+ "**Iago:**\n...of Nephelococcygia\n* Myrmica ruginodis\n* Myrmica kotokui",
+ );
+
// This contains three child elements inside the body tag, pasted
// from LibreOffice Writer, which is correctly classified as not an image.
input = `<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"/><title></title><meta name="generator" content="LibreOffice 25.2.3.2 (Windows)"/><style type="text/css">@page { size: 8.5in 11in; margin: 0.79in }td p { orphans: 0; widows: 0; background: transparent }p { line-height: 115%; margin-bottom: 0.1in; background: transparent }</style></head><body lang="en-US" link="#000080" vlink="#800000" dir="ltr"><p style="line-height: 100%; margin-bottom: 0in">ello world</p><table width="100%" cellpadding="0" cellspacing="0"><col width="51*"/><col width="51*"/><col width="51*"/><col width="51*"/><col width="51*"/><tr valign="top"><td width="20%" style="border: none; padding: 0in"><p>X</p></td><td width="20%" style="border: none; padding: 0in"><p>as</p></td><td width="20%" style="border: none; padding: 0in"><p>Jak</p></td><td width="20%" style="border: none; padding: 0in"><p>J</p></td><td width="20%" style="border: none; padding: 0in"><p>Nm</p></td></tr><tr valign="top"><td width="20%" style="border: none; padding: 0in"><p>,mn</p></td><td width="20%" style="border: none; padding: 0in"><p>,nnf</p></td><td width="20%" style="border: none; padding: 0in"><p>Adlk</p></td><td width="20%" style="border: none; padding: 0in"><p>Asn</p></td><td width="20%" style="border: none; padding: 0in"><p>,amns</p></td></tr><tr valign="top"><td width="20%" style="border: none; padding: 0in"><p>Nm</p></td><td width="20%" style="border: none; padding: 0in"><p>Oi</p></td><td width="20%" style="border: none; padding: 0in"><p>Poi</p></td><td width="20%" style="border: none; padding: 0in"><p>B</p></td><td width="20%" style="border: none; padding: 0in"><p>Ijo</p></td></tr><tr valign="top"><td width="20%" style="border: none; padding: 0in"><p>,mn,</p></td><td width="20%" style="border: none; padding: 0in"><p>;ih</p></td><td width="20%" style="border: none; padding: 0in"><p>Oug</p></td><td width="20%" style="border: none; padding: 0in"><p>Iu</p></td><td width="20%" style="border: none; padding: 0in"><p>G</p></td></tr><tr valign="top"><td width="20%" style="border: none; padding: 0in"><p>Ug</p></td><td width="20%" style="border: none; padding: 0in"><p>Bkjb</p></td><td width="20%" style="border: none; padding: 0in"><p>Kjbk</p></td><td width="20%" style="border: none; padding: 0in"><p>;jbj</p></td><td width="20%" style="border: none; padding: 0in"><p>;jb;</p></td></tr><tr valign="top"><td width="20%" style="border: none; padding: 0in"><p>Bkjb</p></td><td width="20%" style="border: none; padding: 0in"><p>Ugug</p></td><td width="20%" style="border: none; padding: 0in"><p>I9</p></td><td width="20%" style="border: none; padding: 0in"><p>68</p></td><td width="20%" style="border: none; padding: 0in"><p>0</p></td></tr><tr valign="top"><td width="20%" style="border: none; padding: 0in"><p>90kjb</p></td><td width="20%" style="border: none; padding: 0in"><p>,bnbiu</p></td><td width="20%" style="border: none; padding: 0in"><p>Ofif</p></td><td width="20%" style="border: none; padding: 0in"><p>P8gp</p></td><td width="20%" style="border: none; padding: 0in"><p>pugp</p></td></tr></table><p style="line-height: 100%; margin-bottom: 0in"><br/></p></body></html>`;