From 2d65676f5d4375fa73e6169d05155fccc2b21846 Mon Sep 17 00:00:00 2001 From: Harrison Booth <73434116+Harrison-Booth@users.noreply.github.com> Date: Sat, 24 Oct 2026 17:04:44 -0500 Subject: [PATCH] Filter: bound a table-header filter by the table, refuse a narrower range that would drop criteria, fail the test helper on errors --- crates/engine/src/cmd/data.rs | 14 ++++++++++---- crates/engine/tests/subtotal-filter-roundtrip.rs | 14 +++++++++++++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/cmd/data.rs b/crates/engine/src/cmd/data.rs index 944323e41..38c3f0cca 100644 --- a/crates/engine/src/cmd/data.rs +++ b/crates/engine/src/cmd/data.rs @@ -552,10 +552,9 @@ fn install_filter(s: &mut Session, p: &Json) -> Result { } // A selected header row filters the data below it (#201). if p.get("range").is_none() && r.height() == 1 { - let region = if sh.table_at(r.start).is_some() { - current_region(sh, r.start) - } else { - crate::selection::current_region_outside_tables(sh, r.start) + let region = match sh.table_at(r.start) { + Some(t) => t.data_range().map(|dr| RangeRef::new(t.range.start, dr.end)).unwrap_or(t.range), + None => crate::selection::current_region_outside_tables(sh, r.start), }; if region.end.row > r.end.row { r = RangeRef::new(r.start, CellRef::new(region.end.row, r.end.col)); @@ -569,6 +568,13 @@ fn install_filter(s: &mut Session, p: &Json) -> Result { if host.is_none() && sh.tables.iter().any(|t| t.range.intersects(&r)) { return Err(bad("data.filter", "a filter range can't overlap a table; filter inside the table instead")); } + // A narrower range would leave out the table's criteria on the columns it doesn't cover, and saving would drop them. + if let Some(t) = host.and_then(|i| sh.tables.get(i)) { + let shift = r.start.col.saturating_sub(t.range.start.col); + if t.filter.iter().any(|(o, _)| o.checked_sub(shift).is_none_or(|c| c >= r.width())) { + return Err(bad("data.filter", "the table is filtered on a column this range leaves out; filter the table's full width")); + } + } edit(s, |cx| { let sh = cx.sheet_mut(sheet)?; let mut criteria = vec![]; diff --git a/crates/engine/tests/subtotal-filter-roundtrip.rs b/crates/engine/tests/subtotal-filter-roundtrip.rs index 83987f826..3f02b7606 100644 --- a/crates/engine/tests/subtotal-filter-roundtrip.rs +++ b/crates/engine/tests/subtotal-filter-roundtrip.rs @@ -34,7 +34,7 @@ fn session(sample: &str, steps: &[(&str, Json)]) -> Session { let mut s = Session::new(); s.execute("file.new", json!({"sample": sample})).unwrap(); for (id, p) in steps { - let _ = s.execute(id, p.clone()); + s.execute(id, p.clone()).unwrap(); } s } @@ -109,6 +109,18 @@ fn rows_hidden_by_hand_without_a_filter_still_count_for_subtotal_9() { assert_eq!(e1_e2(&back), (Value::Number(111111.0), Value::Number(110101.0))); } +#[test] +fn a_narrower_range_inside_a_filtered_table_is_refused_not_dropped() { + let mut s = table_book(); + s.execute("data.filterBy", json!({"column":"A","values":["a"]})).unwrap(); + let back = reopened(wb(&s)); + let mut s = Session::new(); + s.add_document(gridcraft_engine::DocState::new(back, None, "t".into(), gridcraft_engine::Trust::FromFile)); + s.execute("selection.set", json!({"cell":"D1"})).unwrap(); + assert!(s.execute("data.filter", json!({"range":"B1:B7"})).is_err()); + assert_eq!(wb(&s).active().unwrap().tables[0].filter.len(), 1, "the table's criteria are kept"); +} + #[test] fn filtering_a_reopened_table_again_keeps_both_criteria() { let mut s = table_book();