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_messages: Preserve newlines for text/plain.

apoorvapendseb22d1eddb760
diff --git a/web/e2e-tests/copy-messages.test.ts b/web/e2e-tests/copy-messages.test.ts
index 7396e06f68e..d49e4a9f950 100644
--- a/web/e2e-tests/copy-messages.test.ts
+++ b/web/e2e-tests/copy-messages.test.ts
@@ -16,6 +16,7 @@ type CopyMessagesOptions = {
start_message: string;
end_message: string;
partial_selection_config?: PartialSelectionConfig;
+ send_copied_plain_text?: boolean;
};
async function copy_messages({
@@ -23,12 +24,14 @@ async function copy_messages({
start_message,
end_message,
partial_selection_config,
+ send_copied_plain_text,
}: CopyMessagesOptions): Promise<string[]> {
return await page.evaluate(
(
start_message: string,
end_message: string,
partial_selection_config?: PartialSelectionConfig,
+ send_copied_plain_text?: boolean,
) => {
function get_message_node(message: string): Element {
return [...document.querySelectorAll(".message-list .message_content")].find(
@@ -74,6 +77,11 @@ async function copy_messages({
});
document.dispatchEvent(copy_event);
+ if (send_copied_plain_text) {
+ const copied_plain_text = clipboard_data.getData("text/plain");
+ return copied_plain_text.split("\n");
+ }
+
const copied_html = clipboard_data.getData("text/html");
// Convert the copied HTML into separate message strings
@@ -89,6 +97,7 @@ async function copy_messages({
start_message,
end_message,
partial_selection_config,
+ send_copied_plain_text,
);
}
@@ -201,6 +210,42 @@ async function test_copying_all_from_prev_first_from_next(page: Page): Promise<v
assert.deepStrictEqual(actual_copied_lines, expected_copied_lines);
}
+async function test_copying_plain_text(page: Page): Promise<void> {
+ const actual_copied_lines = await copy_messages({
+ page,
+ start_message: "copy paste test A",
+ end_message: "copy paste test B",
+ send_copied_plain_text: true,
+ });
+ const expected_copied_lines = [
+ "Desdemona:",
+ "copy paste test A",
+ "",
+ "Desdemona:",
+ "copy paste test B",
+ ];
+ assert.deepStrictEqual(actual_copied_lines, expected_copied_lines);
+}
+
+async function test_copying_plain_text_from_two_topics(page: Page): Promise<void> {
+ const actual_copied_lines = await copy_messages({
+ page,
+ start_message: "copy paste test B",
+ end_message: "copy paste test C",
+ send_copied_plain_text: true,
+ });
+ 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",
+ ];
+ assert.deepStrictEqual(actual_copied_lines, expected_copied_lines);
+}
+
async function test_copying_messages_from_several_topics(page: Page): Promise<void> {
const actual_copied_lines = await copy_messages({
page,
@@ -688,6 +733,8 @@ async function copy_paste_test(page: Page): Promise<void> {
await test_copying_through_trailing_sender_name(page);
await test_copying_repeated_text_suffix_through_next_sender(page);
await test_multiple_message_selection_with_partially_selected_bookend_messages(page);
+ await test_copying_plain_text(page);
+ await test_copying_plain_text_from_two_topics(page);
}
await common.run_test(copy_paste_test);
diff --git a/web/src/copy_messages.ts b/web/src/copy_messages.ts
index d130ab2df17..ae8cab61e9e 100644
--- a/web/src/copy_messages.ts
+++ b/web/src/copy_messages.ts
@@ -566,6 +566,186 @@ function maybe_update_range_for_code_blocks(range: Range, ev: ClipboardEvent): b
return false;
}
+// Elements that start on a line of their own, as they do on screen.
+const PLAIN_TEXT_BLOCK_TAGS = new Set([
+ "BLOCKQUOTE",
+ "DIV",
+ "H1",
+ "H2",
+ "H3",
+ "H4",
+ "H5",
+ "H6",
+ "HR",
+ "LI",
+ "OL",
+ "PRE",
+ "TABLE",
+ "TR",
+ "UL",
+]);
+
+function get_line_breaks_around(element: Element): {before: number; after: number} {
+ if (element.tagName === "P") {
+ // As with the `message-header paste output` rule in
+ // compose_paste, the recipient header sits on the line
+ // directly above the first sender's name.
+ if (element.getAttribute("data-message-header-paragraph") === "true") {
+ return {before: 2, after: 1};
+ }
+ return {before: 2, after: 2};
+ }
+ if (PLAIN_TEXT_BLOCK_TAGS.has(element.tagName)) {
+ return {before: 1, after: 1};
+ }
+ return {before: 0, after: 0};
+}
+
+// These match the `white-space` rules in rendered_markdown.css.
+function preserves_whitespace(element: Element, parent_preserves_whitespace: boolean): boolean {
+ if (element.classList.contains("code-buttons-container")) {
+ return false;
+ }
+ return (
+ parent_preserves_whitespace ||
+ element.tagName === "PRE" ||
+ element.tagName === "CODE" ||
+ element.classList.contains("stream-topic-inner") ||
+ element.classList.contains("decorated-channel-name")
+ );
+}
+
+// Returns the text a reader sees in the copy div, for `text/plain`.
+// Blocks start on a line of their own, a paragraph is set apart by a
+// blank line, and whitespace is collapsed except where it is displayed
+// as-is.
+export function get_plain_text_for_copy_div(copy_div: Element): string {
+ let plain_text = "";
+ // Line breaks to add before the next text.
+ //
+ // Where several block boundaries meet with no text between them,
+ // this is the most line breaks that any one of them needs.
+ // For example, between two messages from the same recipient,
+ // the copy div has:
+ //
+ // <div>See the docs.</div><p></p><b>Desdemona: </b>
+ //
+ // `</div>` needs 1 line break, and `<p>` and `</p>` need 2 each.
+ // Taking the largest, 2, leaves one blank line before the next
+ // sender's name:
+ //
+ // See the docs.
+ //
+ // Desdemona:
+ //
+ // Adding them up would give 5 line breaks, and so four blank lines.
+ let pending_line_breaks = 0;
+ // Newlines that `plain_text` already ends with, from a `<br>` or
+ // the last line of a code block.
+ let trailing_line_breaks = 0;
+ // Whether `plain_text` ends with the tab between two table cells.
+ // This is tracked, rather than read from the end of `plain_text`,
+ // because checking the end of a string built up with `+=` makes
+ // the browser copy all of it.
+ let ends_with_tab = false;
+ let pending_space = false;
+
+ function is_at_line_start(): boolean {
+ return (
+ plain_text === "" ||
+ pending_line_breaks > 0 ||
+ trailing_line_breaks > 0 ||
+ ends_with_tab
+ );
+ }
+
+ function request_line_breaks(count: number): void {
+ if (count > 0) {
+ pending_line_breaks = Math.max(pending_line_breaks, count);
+ pending_space = false;
+ }
+ }
+
+ function append(text: string): void {
+ if (pending_line_breaks > trailing_line_breaks && plain_text !== "") {
+ plain_text += "\n".repeat(pending_line_breaks - trailing_line_breaks);
+ trailing_line_breaks = pending_line_breaks;
+ } else if (pending_space) {
+ plain_text += " ";
+ trailing_line_breaks = 0;
+ }
+ pending_line_breaks = 0;
+ pending_space = false;
+
+ plain_text += text;
+ ends_with_tab = text.endsWith("\t");
+ let text_trailing_line_breaks = 0;
+ while (text.at(-1 - text_trailing_line_breaks) === "\n") {
+ text_trailing_line_breaks += 1;
+ }
+ if (text_trailing_line_breaks === text.length) {
+ // Text that is only newlines, such as a `<br>`, joins the
+ // newlines already at the end of `plain_text`, so the count
+ // covers the whole run: after two `<br>`s in a row, it is 2.
+ trailing_line_breaks += text_trailing_line_breaks;
+ } else {
+ // Newlines before this text are no longer at the end.
+ trailing_line_breaks = text_trailing_line_breaks;
+ }
+ }
+
+ function append_collapsible_text(text: string): void {
+ let collapsed_text = text.replaceAll(/[\t\n\r ]+/g, " ");
+ if (collapsed_text.startsWith(" ")) {
+ // We ignore whitespace at the start of a line, like the browser does.
+ pending_space ||= !is_at_line_start();
+ collapsed_text = collapsed_text.slice(1);
+ }
+ if (collapsed_text === "") {
+ return;
+ }
+ if (collapsed_text.endsWith(" ")) {
+ append(collapsed_text.slice(0, -1));
+ pending_space = true;
+ } else {
+ append(collapsed_text);
+ }
+ }
+
+ function append_children(parent: Node, parent_preserves_whitespace: boolean): void {
+ for (const child of parent.childNodes) {
+ if (child.nodeType === Node.TEXT_NODE) {
+ const text = child.nodeValue ?? "";
+ if (!parent_preserves_whitespace) {
+ append_collapsible_text(text);
+ } else if (text !== "") {
+ append(text);
+ }
+ } else if (child instanceof Element) {
+ if (child.tagName === "BR") {
+ pending_space = false;
+ append("\n");
+ continue;
+ }
+ const line_breaks = get_line_breaks_around(child);
+ request_line_breaks(line_breaks.before);
+ append_children(child, preserves_whitespace(child, parent_preserves_whitespace));
+ request_line_breaks(line_breaks.after);
+ if (
+ (child.tagName === "TD" || child.tagName === "TH") &&
+ child.nextElementSibling !== null
+ ) {
+ pending_space = false;
+ append("\t");
+ }
+ }
+ }
+ }
+
+ append_children(copy_div, false);
+ return plain_text;
+}
+
export function copy_handler(ev: ClipboardEvent): boolean {
// This is the main handler for copying message content via
// `Ctrl+C` in Zulip (note that this is totally independent of the
@@ -658,7 +838,7 @@ export function copy_handler(ev: ClipboardEvent): boolean {
}
const html_content = $div.html().trim();
- const plain_text = $div.text().trim();
+ const plain_text = get_plain_text_for_copy_div(the($div)).trim();
ev.clipboardData?.setData("text/html", html_content);
ev.clipboardData?.setData("text/plain", plain_text);
diff --git a/web/tests/copy_messages.test.cjs b/web/tests/copy_messages.test.cjs
new file mode 100644
index 00000000000..4ce9728f09e
--- /dev/null
+++ b/web/tests/copy_messages.test.cjs
@@ -0,0 +1,101 @@
+"use strict";
+
+const assert = require("node:assert/strict");
+
+const {JSDOM} = require("jsdom");
+
+const {set_global, zrequire} = require("./lib/namespace.cjs");
+const {run_test} = require("./lib/test.cjs");
+
+const {window} = new JSDOM();
+set_global("Node", window.Node);
+set_global("Element", window.Element);
+
+const copy_messages = zrequire("copy_messages");
+
+function get_plain_text(copy_div_html) {
+ const copy_div = window.document.createElement("div");
+ copy_div.innerHTML = copy_div_html;
+ return copy_messages.get_plain_text_for_copy_div(copy_div);
+}
+
+run_test("messages from one recipient", () => {
+ const copy_div_html =
+ '<b>Desdemona: </b><div>See the <a href="https://zulip.com/help">docs</a>.</div>' +
+ '<p></p><b>Desdemona: </b><div>Hello <span class="user-mention silent" data-user-id="8">Cordelia</span></div>\n' +
+ "<p>Second paragraph</p>";
+ assert.equal(
+ get_plain_text(copy_div_html),
+ "Desdemona:\nSee the docs.\n\nDesdemona:\nHello Cordelia\n\nSecond paragraph",
+ );
+});
+
+run_test("messages from several recipients", () => {
+ const copy_div_html =
+ '<p data-message-header-paragraph="true"><strong>Verona &gt; copy-paste-topic #1</strong><span> | Today</span></p>' +
+ "<b>Desdemona: </b><div>copy paste test B</div>" +
+ '<p data-message-header-paragraph="true"><strong>Verona &gt; copy-paste-topic #2</strong><span> | Today</span></p>' +
+ "<b>Desdemona: </b><div>copy paste test C</div>";
+ assert.equal(
+ get_plain_text(copy_div_html),
+ "Verona > copy-paste-topic #1 | Today\n" +
+ "Desdemona:\ncopy paste test B\n\n" +
+ "Verona > copy-paste-topic #2 | Today\n" +
+ "Desdemona:\ncopy paste test C",
+ );
+});
+
+run_test("blocks within a message", () => {
+ const copy_div_html =
+ "<div>line one<br>\nline two</div>\n" +
+ "<ul>\n<li>item one</li>\n<li>item two</li>\n</ul>\n" +
+ "<blockquote>\n<p>quoted</p>\n</blockquote>\n" +
+ "<h1>Heading</h1>\n" +
+ "<p>After the heading</p>";
+ assert.equal(
+ get_plain_text(copy_div_html),
+ "line one\nline two\nitem one\nitem two\n\nquoted\n\nHeading\n\nAfter the heading",
+ );
+});
+
+run_test("whitespace", () => {
+ const copy_div_html =
+ "<p>collapsed spaces\nand <code>inline code</code> and " +
+ '<a class="stream-topic" href="#narrow/channel/9-two-spaces">' +
+ '<span class="decorated-channel-name-wrapper inline-decorated-channel-name">' +
+ '<span class="channel-privacy-type-icon"><i class="zulip-icon zulip-icon-hashtag"></i></span>' +
+ '<span class="decorated-channel-name">two spaces</span></span></a></p>';
+ assert.equal(
+ get_plain_text(copy_div_html),
+ "collapsed spaces and inline code and two spaces",
+ );
+});
+
+run_test("code blocks", () => {
+ // The code buttons are added to the `<pre>` of a rendered message,
+ // so they appear in a partially selected message.
+ const copy_div_html =
+ "<p>Hamlet once said</p>\n" +
+ '<div class="codehilite" data-code-language="Python"><pre>' +
+ '<div class="code-buttons-container">\n' +
+ ' <span class="copy_codeblock copy-button copy-button-square" role="button">\n' +
+ ' <i class="zulip-icon zulip-icon-copy" aria-hidden="true"></i>\n' +
+ " </span></div>" +
+ '<span></span><code><span class="k">def</span> <span class="nf">func</span><span class="p">():</span>\n' +
+ ' <span class="n">x</span> <span class="o">=</span> <span class="mi">1</span>\n' +
+ "\n" +
+ ' <span class="n">y</span> <span class="o">=</span> <span class="mi">2</span>\n' +
+ "</code></pre></div>\n" +
+ "<p>And all was good.</p>";
+ assert.equal(
+ get_plain_text(copy_div_html),
+ "Hamlet once said\n\ndef func():\n x = 1\n\n y = 2\n\nAnd all was good.",
+ );
+});
+
+run_test("tables", () => {
+ const copy_div_html =
+ "<table>\n<thead>\n<tr>\n<th>Name</th>\n<th>Role</th>\n</tr>\n</thead>\n" +
+ "<tbody>\n<tr>\n<td>Desdemona</td>\n<td>Owner</td>\n</tr>\n</tbody>\n</table>";
+ assert.equal(get_plain_text(copy_div_html), "Name\tRole\nDesdemona\tOwner");
+});