diff --git a/crates/engine/src/cmd/table.rs b/crates/engine/src/cmd/table.rs index 23f591719..930849ef8 100644 --- a/crates/engine/src/cmd/table.rs +++ b/crates/engine/src/cmd/table.rs @@ -321,7 +321,15 @@ fn with_table(s: &mut Session, p: &Value, cmd: &str, f: impl Fn(&mut Table) -> R fn rows_cols(s: &mut Session, p: &Value, op: &str, name: &'static str) -> Result { let (r, c) = at(s, p, name)?; - let keep_width = bool_or(p, "keepWidth", true); + // Absent: keep the table's width; present, it must be a bool, so a mistyped value is refused + // rather than silently taken as the default. + let keep_width = match (op, p.get("keepWidth")) { + (_, None) | (_, Some(Value::Null)) => true, + (_, Some(Value::Bool(b))) => *b, + ("colLeft", Some(_)) => return Err(bad("table.insertColumnLeft", "`keepWidth` must be true or false")), + ("colRight", Some(_)) => return Err(bad("table.insertColumnRight", "`keepWidth` must be true or false")), + _ => true, + }; let op = op.to_string(); with_table(s, p, name, move |t| { match op.as_str() { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 81f9ed82a..d6769a07a 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -6525,6 +6525,21 @@ fn table_insert_column_keeps_the_table_width_and_copies_the_neighbour_format() { assert!((t.width() - 840.0 - 168.0).abs() < 1e-6, "width {}", t.width()); } +/// A `keepWidth` that is not a bool is refused, naming it, and the table is left alone; it used +/// to be read as absent and the columns shrunk as if `true` had been asked. +#[test] +fn table_insert_column_refuses_a_keep_width_that_is_not_a_bool() { + let mut s = session(); + let id = table_at(&mut s, &[280.0, 280.0, 280.0], json!([["a", "b", "c"]])); + for (cmd, bad) in [("table.insertColumnRight", json!("false")), ("table.insertColumnLeft", json!(0))] { + let err = s.execute(cmd, &json!({"id": id.0, "col": 1, "keepWidth": bad})).unwrap_err().to_string(); + assert!(err.contains("keepWidth") && err.contains(cmd), "{err}"); + let t = table_of(&s, id); + assert_eq!(t.n_cols(), 3, "{cmd} changed the table: {:?}", t.cols); + assert!((t.width() - 840.0).abs() < 1e-6, "width {}", t.width()); + } +} + #[test] fn slide_master_view_rejects_invalid_indices() { let mut s = Session::with_new();