Skip to content

Commit 8aea764

Browse files
amanagrtimabbott
authored andcommitted
copy_and_paste: Fix our logic being confused about selected messages.
If the selection stays inside a message header and there are messages before the message header, our logic thinks `end_id` is message before the header while `start_id` and `end_id` should be that same. It is best to just let browser handle the copy paste in this case.
1 parent 6a811cb commit 8aea764

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

web/src/copy_and_paste.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,21 @@ export function copy_handler(): void {
174174
const skip_same_td_check = analysis.skip_same_td_check;
175175
const $div = $("<div>");
176176

177-
if (start_id === undefined || end_id === undefined) {
177+
if (start_id === undefined || end_id === undefined || start_id > end_id) {
178178
// In this case either the starting message or the ending
179179
// message is not defined, so this is definitely not a
180180
// multi-message selection and we can let the browser handle
181181
// the copy.
182+
//
183+
// Also, if our logic is not sound about the selection range
184+
// (start_id > end_id), we let the browser handle the copy.
185+
//
186+
// NOTE: `startContainer (~ start_id)` and `endContainer (~ end_id)`
187+
// of a `Range` are always from top to bottom in the DOM tree, independent
188+
// of the direction of the selection.
189+
// TODO: Add a reference for this statement, I just tested
190+
// it in console for various selection directions and found this
191+
// to be the case not sure why there is no online reference for it.
182192
document.execCommand("copy");
183193
return;
184194
}
@@ -312,6 +322,10 @@ function get_end_tr_from_endc($endc: JQuery<Node>): JQuery {
312322
}
313323
// If it's not in a .message_row, it's probably in a .message_header and
314324
// we can use the last message from the previous recipient_row.
325+
// NOTE: It is possible that the selection started and ended inside the
326+
// message header and in that case we would be returning the message before
327+
// the selected header if it exists, but that is not the purpose of this
328+
// function to handle.
315329
if ($endc.parents(".message_header").length > 0) {
316330
const $overflow_recipient_row = $endc.parents(".recipient_row").first();
317331
return $overflow_recipient_row.prev(".recipient_row").children(".message_row").last();

0 commit comments

Comments
 (0)