From 35ca6435a8832868fe7f89ea69d08ef4c9c97236 Mon Sep 17 00:00:00 2001 From: alehel Date: Mon, 23 Nov 2026 21:44:52 +0100 Subject: [PATCH] paste: a cut is refused as Paste Link or into filtered-out rows before its source is cleared Paste Link would write links to the cells the cut empties, and a destination over rows the filter hides would skip cells the cut has taken; both lost the cut cells. --- crates/engine/src/cmd/edit/paste_special.rs | 9 ++++++ crates/engine/src/tests.rs | 35 +++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/crates/engine/src/cmd/edit/paste_special.rs b/crates/engine/src/cmd/edit/paste_special.rs index ac1a073ecc..6eed0e77b9 100644 --- a/crates/engine/src/cmd/edit/paste_special.rs +++ b/crates/engine/src/cmd/edit/paste_special.rs @@ -77,6 +77,15 @@ pub(super) fn paste_special(s: &mut Session, p: &Json) -> Result { } let same_doc = s.doc()?.uid == clip.doc_uid; let cut_move = clip.cut && same_doc && what == What::All; + // A cut empties its cells, so pasting it as links would link to the emptied cells, and a + // destination over rows the filter hides would skip cells the cut has taken: both would lose + // the cut cells, so both are refused before the source is cleared. + if cut_move && link { + return Err(EngineError::Other("The cut area can't be pasted as links: paste it without Paste Link.".into())); + } + if cut_move && skip_rows.range(at.row..at.row.saturating_add(tiles_r.saturating_mul(h))).next().is_some() { + return Err(EngineError::Other("The cut area can't be pasted into rows the filter hides.".into())); + } let src_wb = clip.wb.clone(); let Some(src_sheet) = src_wb.sheet(clip.sheet) else { return Err(EngineError::Other("The copied sheet no longer exists.".into())) }; let src_sheet_name = src_sheet.name.clone(); diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 569e8a0230..8b9e3ff77f 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -7124,3 +7124,38 @@ fn history_charges_the_storage_of_empty_comment_replies() { // 500 replies of no text are still 500 elements. assert!(crate::sheet_text_bytes(&sheet) >= 500 * std::mem::size_of::<(String, String)>()); } + +/// Paste Link would write links to the cells a cut empties, losing them: a cut can't be pasted +/// as links, and nothing changes. +#[test] +fn a_cut_is_not_pasted_as_links() { + let mut s = s(); + s.execute("range.setValues", json!({"range": "A1", "values": [[1, 2]]})).unwrap(); + s.execute("edit.cut", json!({"range": "A1:B1"})).unwrap(); + assert!(s.execute("edit.pasteSpecial", json!({"what": "all", "link": true, "at": "E1"})).is_err()); + assert_eq!((v(&s, "A1"), v(&s, "B1"), v(&s, "E1")), (Value::Number(1.0), Value::Number(2.0), Value::Empty)); +} + +/// A cut pasted into a row the filter hides would skip every cell it writes, so the cells would +/// leave the sheet with the cut: it is refused and nothing changes. A visible row takes it. +#[test] +fn a_cut_is_not_pasted_into_filtered_rows() { + let mut s = s(); + let mut rows = vec![json!(["Val", "Tag"])]; + for i in 2..=5 { + rows.push(json!([format!("v{i}"), if i == 3 { "hide" } else { "keep" }])); + } + s.execute("range.setValues", json!({"range": "A1", "values": rows})).unwrap(); + s.execute("range.setValues", json!({"range": "E1", "values": [[1, 2]]})).unwrap(); + s.execute("selection.set", json!({"range": "A1:B1"})).unwrap(); + s.execute("data.filter", json!({})).unwrap(); + s.execute("data.filterBy", json!({"column": "B", "values": ["keep"]})).unwrap(); + s.execute("edit.cut", json!({"range": "E1:F1"})).unwrap(); + s.execute("selection.set", json!({"range": "A3:B3"})).unwrap(); + assert!(s.execute("edit.paste", json!({})).is_err()); + assert_eq!((v(&s, "E1"), v(&s, "F1")), (Value::Number(1.0), Value::Number(2.0))); + assert_eq!(v(&s, "A3"), Value::from("v3")); + s.execute("selection.set", json!({"range": "A4:B4"})).unwrap(); + s.execute("edit.paste", json!({})).unwrap(); + assert_eq!((v(&s, "A4"), v(&s, "B4"), v(&s, "E1")), (Value::Number(1.0), Value::Number(2.0), Value::Empty)); +}