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_paste: Update formating for multi-message paste.

apoorvapendsebdd3629f28cd
diff --git a/web/e2e-tests/copy-messages.test.ts b/web/e2e-tests/copy-messages.test.ts
index 549d8916777..8b420dabe63 100644
--- a/web/e2e-tests/copy-messages.test.ts
+++ b/web/e2e-tests/copy-messages.test.ts
@@ -72,7 +72,11 @@ async function copy_messages(
const parser = new DOMParser();
const doc = parser.parseFromString(copied_html, "text/html");
- return [...doc.body.children].map((el) => el.textContent.trim());
+ // Empty paragraphs are inserted only to separate consecutive
+ // messages with a blank line; skip them when collecting lines.
+ return [...doc.body.children]
+ .map((el) => el.textContent.trim())
+ .filter((line) => line !== "");
},
start_message,
end_message,
@@ -94,16 +98,24 @@ async function test_copying_last_message_from_topic(page: Page): Promise<void> {
async function test_copying_first_two_messages_from_topic(page: Page): Promise<void> {
const actual_copied_lines = await copy_messages(page, "copy paste test C", "copy paste test D");
- const expected_copied_lines = ["Desdemona: copy paste test C", "Desdemona: copy paste test D"];
+ const expected_copied_lines = [
+ "Desdemona:",
+ "copy paste test C",
+ "Desdemona:",
+ "copy paste test D",
+ ];
assert.deepStrictEqual(actual_copied_lines, expected_copied_lines);
}
async function test_copying_all_messages_from_topic(page: Page): Promise<void> {
const actual_copied_lines = await copy_messages(page, "copy paste test C", "copy paste test E");
const expected_copied_lines = [
- "Desdemona: copy paste test C",
- "Desdemona: copy paste test D",
- "Desdemona: copy paste test E",
+ "Desdemona:",
+ "copy paste test C",
+ "Desdemona:",
+ "copy paste test D",
+ "Desdemona:",
+ "copy paste test E",
];
assert.deepStrictEqual(actual_copied_lines, expected_copied_lines);
}
@@ -111,10 +123,12 @@ async function test_copying_all_messages_from_topic(page: Page): Promise<void> {
async function test_copying_last_from_prev_first_from_next(page: Page): Promise<void> {
const actual_copied_lines = await copy_messages(page, "copy paste test B", "copy paste test C");
const expected_copied_lines = [
- "Verona > copy-paste-topic #1 Today",
- "Desdemona: copy paste test B",
- "Verona > copy-paste-topic #2 Today",
- "Desdemona: copy paste test C",
+ "Verona > copy-paste-topic #1 | Today",
+ "Desdemona:",
+ "copy paste test B",
+ "Verona > copy-paste-topic #2 | Today",
+ "Desdemona:",
+ "copy paste test C",
];
assert.deepStrictEqual(actual_copied_lines, expected_copied_lines);
}
@@ -122,12 +136,16 @@ async function test_copying_last_from_prev_first_from_next(page: Page): Promise<
async function test_copying_last_from_prev_all_from_next(page: Page): Promise<void> {
const actual_copied_lines = await copy_messages(page, "copy paste test B", "copy paste test E");
const expected_copied_lines = [
- "Verona > copy-paste-topic #1 Today",
- "Desdemona: copy paste test B",
- "Verona > copy-paste-topic #2 Today",
- "Desdemona: copy paste test C",
- "Desdemona: copy paste test D",
- "Desdemona: copy paste test E",
+ "Verona > copy-paste-topic #1 | Today",
+ "Desdemona:",
+ "copy paste test B",
+ "Verona > copy-paste-topic #2 | Today",
+ "Desdemona:",
+ "copy paste test C",
+ "Desdemona:",
+ "copy paste test D",
+ "Desdemona:",
+ "copy paste test E",
];
assert.deepStrictEqual(actual_copied_lines, expected_copied_lines);
}
@@ -135,11 +153,14 @@ async function test_copying_last_from_prev_all_from_next(page: Page): Promise<vo
async function test_copying_all_from_prev_first_from_next(page: Page): Promise<void> {
const actual_copied_lines = await copy_messages(page, "copy paste test A", "copy paste test C");
const expected_copied_lines = [
- "Verona > copy-paste-topic #1 Today",
- "Desdemona: copy paste test A",
- "Desdemona: copy paste test B",
- "Verona > copy-paste-topic #2 Today",
- "Desdemona: copy paste test C",
+ "Verona > copy-paste-topic #1 | Today",
+ "Desdemona:",
+ "copy paste test A",
+ "Desdemona:",
+ "copy paste test B",
+ "Verona > copy-paste-topic #2 | Today",
+ "Desdemona:",
+ "copy paste test C",
];
assert.deepStrictEqual(actual_copied_lines, expected_copied_lines);
}
@@ -147,14 +168,19 @@ async function test_copying_all_from_prev_first_from_next(page: Page): Promise<v
async function test_copying_messages_from_several_topics(page: Page): Promise<void> {
const actual_copied_lines = await copy_messages(page, "copy paste test B", "copy paste test F");
const expected_copied_lines = [
- "Verona > copy-paste-topic #1 Today",
- "Desdemona: copy paste test B",
- "Verona > copy-paste-topic #2 Today",
- "Desdemona: copy paste test C",
- "Desdemona: copy paste test D",
- "Desdemona: copy paste test E",
- "Verona > copy-paste-topic #3 Today",
- "Desdemona: copy paste test F",
+ "Verona > copy-paste-topic #1 | Today",
+ "Desdemona:",
+ "copy paste test B",
+ "Verona > copy-paste-topic #2 | Today",
+ "Desdemona:",
+ "copy paste test C",
+ "Desdemona:",
+ "copy paste test D",
+ "Desdemona:",
+ "copy paste test E",
+ "Verona > copy-paste-topic #3 | Today",
+ "Desdemona:",
+ "copy paste test F",
];
assert.deepStrictEqual(actual_copied_lines, expected_copied_lines);
}
@@ -216,16 +242,21 @@ async function test_multiple_message_selection_with_partially_selected_bookend_m
},
);
const expected_copied_lines = [
- "Verona > copy-paste-topic #1 Today",
- // w/o partial selection: "Desdemona: copy paste test B",
- "Desdemona: paste test B",
- "Verona > copy-paste-topic #2 Today",
- "Desdemona: copy paste test C",
- "Desdemona: copy paste test D",
- "Desdemona: copy paste test E",
- "Verona > copy-paste-topic #3 Today",
- // w/o partial selection: "Desdemona: copy paste test F",
- "Desdemona: copy paste",
+ "Verona > copy-paste-topic #1 | Today",
+ "Desdemona:",
+ // w/o partial selection: "copy paste test B",
+ "paste test B",
+ "Verona > copy-paste-topic #2 | Today",
+ "Desdemona:",
+ "copy paste test C",
+ "Desdemona:",
+ "copy paste test D",
+ "Desdemona:",
+ "copy paste test E",
+ "Verona > copy-paste-topic #3 | Today",
+ "Desdemona:",
+ // w/o partial selection: "copy paste test F",
+ "copy paste",
];
assert.deepStrictEqual(actual_copied_lines, expected_copied_lines);
}
diff --git a/web/src/compose_paste.ts b/web/src/compose_paste.ts
index 5641d37786b..718f0a0d575 100644
--- a/web/src/compose_paste.ts
+++ b/web/src/compose_paste.ts
@@ -615,6 +615,14 @@ export function paste_handler_converter(
},
});
+ turndownService.addRule("message-header paste output", {
+ filter: (node) =>
+ node.nodeName === "P" && node.getAttribute("data-message-header-paragraph") === "true",
+ replacement(content) {
+ return `\n\n${content}\n`;
+ },
+ });
+
let markdown_text = turndownService.turndown(paste_html);
// Checks for escaped ordered list syntax.
diff --git a/web/src/copy_messages.ts b/web/src/copy_messages.ts
index fefd52479ee..8066c63f361 100644
--- a/web/src/copy_messages.ts
+++ b/web/src/copy_messages.ts
@@ -4,6 +4,8 @@
import $ from "jquery";
import assert from "minimalistic-assert";
+import render_copied_recipient_header from "../templates/copied_recipient_header.hbs";
+
import * as blueslip from "./blueslip.ts";
import * as message_lists from "./message_lists.ts";
import * as rows from "./rows.ts";
@@ -48,13 +50,14 @@ function find_boundary_tr(
}
function construct_recipient_header($message_row: JQuery): JQuery {
- const message_header_content = rows
- .get_message_recipient_header($message_row)
- .text()
- .replaceAll(/\s+/g, " ")
- .replace(/^\s/, "")
- .replace(/\s$/, "");
- return $("<p>").append($("<strong>").text(message_header_content));
+ const $header = rows.get_message_recipient_header($message_row);
+ const date_text = $header.find(".recipient_row_date").text().trim();
+
+ const $header_without_date = $header.clone();
+ $header_without_date.find(".recipient_row_date").remove();
+ const recipient_text = $header_without_date.text().replaceAll(/\s+/g, " ").trim();
+
+ return $(render_copied_recipient_header({recipient_text, date_text}));
}
// Returns the selected `.message_content`s in the current range.
@@ -221,6 +224,7 @@ function construct_copy_div($div: JQuery, start_id: number, end_id: number): voi
const $row = copy_rows[i];
assert($row !== undefined && $row[0] instanceof HTMLElement);
const recipient_row_id = rows.id_for_recipient_row(rows.get_message_recipient_row($row));
+ let added_recipient_header = false;
// if we found a message from another recipient,
// it means that we have messages from several recipients,
// so we have to add new recipient's bar to final copied message
@@ -229,6 +233,7 @@ function construct_copy_div($div: JQuery, start_id: number, end_id: number): voi
construct_recipient_header($row).appendTo($div);
last_recipient_row_id = recipient_row_id;
should_include_start_recipient_header = true;
+ added_recipient_header = true;
}
const message = message_lists.current.get(rows.id($row));
assert(message !== undefined);
@@ -242,11 +247,28 @@ function construct_copy_div($div: JQuery, start_id: number, end_id: number): voi
$content = $(message.content);
}
- $content.first().prepend(
- $("<span>")
- .text(message.sender_full_name + ": ")
- .contents(),
- );
+ // A recipient header already separates itself from the following
+ // sender name with a newline. Between consecutive messages from the
+ // same recipient there is no header, so we insert an empty paragraph
+ // to keep a blank line separating each message's body from the next
+ // message's sender name.
+ if (i > 0 && !added_recipient_header) {
+ $div.append($("<p>"));
+ }
+
+ $div.append($("<b>").text(message.sender_full_name + ": "));
+
+ // A leading paragraph is moved into a div so that the `no extra
+ // newline` turndown rule keeps just a single newline (instead of a
+ // blank line) between the sender name and the message body. We only
+ // unwrap this first paragraph; any following block content, such as a
+ // list, is left untouched so that its structure is preserved.
+ const $first_content_element = $content.first();
+ if ($first_content_element.is("p")) {
+ $div.append($("<div>").append($first_content_element.contents()));
+ $content = $content.slice(1);
+ }
+
$div.append($content);
}
diff --git a/web/templates/copied_recipient_header.hbs b/web/templates/copied_recipient_header.hbs
new file mode 100644
index 00000000000..9b688df477e
--- /dev/null
+++ b/web/templates/copied_recipient_header.hbs
@@ -0,0 +1 @@
+<p data-message-header-paragraph="true"><strong>{{recipient_text}}</strong>{{#if date_text}}<span> | {{date_text}}</span>{{/if}}</p>
diff --git a/web/tests/compose_paste.test.cjs b/web/tests/compose_paste.test.cjs
index 0dc8a151006..39a7d024a19 100644
--- a/web/tests/compose_paste.test.cjs
+++ b/web/tests/compose_paste.test.cjs
@@ -544,6 +544,26 @@ run_test("paste_handler_converter", () => {
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"> body,div,table,thead,tbody,tfoot,tr,th,td,p { font-family:"Arial"; font-size:x-small } a.comment-indicator:hover + comment { background:#ffd; position:absolute; display:block; border:1px solid black; padding:0.5em; } a.comment-indicator { background:red; display:inline-block; border:1px solid black; width:0.5em; height:0.5em; } comment { display:none; } </style></head><body><table cellspacing="0" border="0"><colgroup span="4" width="107"></colgroup><tbody><tr><td height="24" align="left" data-sheets-value="{ &quot;1&quot;: 2, &quot;2&quot;: &quot;Kathleen&quot;}">Kathleen</td><td align="left" data-sheets-value="{ &quot;1&quot;: 2, &quot;2&quot;: &quot;Hanner&quot;}">Hanner</td><td align="left" data-sheets-value="{ &quot;1&quot;: 2, &quot;2&quot;: &quot;Female&quot;}">Female</td><td align="left" data-sheets-value="{ &quot;1&quot;: 2, &quot;2&quot;: &quot;United States&quot;}">United States</td></tr><tr><td height="24" align="left" data-sheets-value="{ &quot;1&quot;: 2, &quot;2&quot;: &quot;Nereida&quot;}">Nereida</td><td align="left" data-sheets-value="{ &quot;1&quot;: 2, &quot;2&quot;: &quot;Magwood&quot;}">Magwood</td><td align="left" data-sheets-value="{ &quot;1&quot;: 2, &quot;2&quot;: &quot;Female&quot;}">Female</td><td align="left" data-sheets-value="{ &quot;1&quot;: 2, &quot;2&quot;: &quot;United States&quot;}">United States</td></tr></tbody></table></body></html>`;
assert.ok(compose_paste.is_single_image(input));
+ // Pastes containing the `data-message-header-paragraph="true"` attribute should
+ // be formatted as per the custom turndown rule. The empty paragraph that
+ // copy_messages inserts between consecutive messages from the same recipient
+ // should render as a blank line separating them.
+ input = `<html><body><!--StartFragment--><p data-message-header-paragraph="true"><strong>test &gt; LAST API has been skipping erratically </strong><span>| Dec 20</span></p><b>Othello, the Moor of Venice: </b><div>When shall we three meet again In thunder, lightning, or in rain?</div><p></p><b>Othello, the Moor of Venice: </b><div>The second consecutive message.</div><p data-message-header-paragraph="true"><strong>test &gt; leased keurig wasn't loading </strong><span>| Dec 21</span></p><b>King Hamlet: </b><div>Sit down awhile; And let us once again assail your ears, That are so fortified against our story What we have two nights seen.</div><!--EndFragment--></body></html>`;
+ assert.equal(
+ compose_paste.paste_handler_converter(input),
+ "**test > LAST API has been skipping erratically** | Dec 20\n**Othello, the Moor of Venice:**\nWhen shall we three meet again In thunder, lightning, or in rain?\n\n**Othello, the Moor of Venice:**\nThe second consecutive message.\n\n**test > leased keurig wasn't loading** | Dec 21\n**King Hamlet:**\nSit down awhile; And let us once again assail your ears, That are so fortified against our story What we have two nights seen.",
+ );
+
+ // Copying a message that is a paragraph followed by a list: copy_messages
+ // unwraps only the leading paragraph into a div (so the sender name is
+ // separated from the body by a single newline), leaving the list intact.
+ // The list must stay on its own line and not get glued onto the paragraph.
+ input = `<html><body><!--StartFragment--><b>Desdemona: </b><div>intro text</div>\n<ul>\n<li>item one</li>\n<li>item two</li>\n<li>item three</li>\n</ul><!--EndFragment--></body></html>`;
+ assert.equal(
+ compose_paste.paste_handler_converter(input),
+ "**Desdemona:**\nintro text\n* item one\n* item two\n* item three",
+ );
+
// 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>`;