From 9e975b0049a433e874e918a1e9bb4720600e7f01 Mon Sep 17 00:00:00 2001 From: scks1 <126534658+scks1@users.noreply.github.com> Date: Mon, 16 Nov 2026 02:46:28 -0500 Subject: [PATCH] engine: arrange.align and arrange.distribute refuse values they do not name (#1974) * engine: arrange.align and arrange.distribute refuse values they do not name arrange.align took an `edge` it did not know as a no-op that still reported its shapes as edited, aligned left when `edge` was left out (the spec does not mark it optional), and read any `to` other than exactly "slide" as the selection. arrange.distribute distributed vertically for any `dir` starting with "v" and horizontally for any other, and also read any `to` other than "slide" as the selection. `edge` must now be left, center, right, top, middle or bottom, `to` slide or selection, and `dir` horizontal or vertical. Anything else, or a missing `edge`, is refused with the accepted values and nothing moves. A missing `to` or `dir` keeps its default. Test: align_and_distribute_refuse_values_they_do_not_name. * engine: arrange.align and arrange.distribute refuse a dir or to that is not a string `align_to` and `distribute` read `to` and `dir` through `str_param`, which returns None for a value that is present but not a string, the same as for a missing one. So `{"dir": 123}` distributed horizontally, and `{"edge": "left", "to": false}` aligned to each command's default reference, moving the selected shapes instead of failing. Both now match on `p.get(..).map(Value::as_str)`: a missing field keeps its default (horizontal for `dir`, each command's own rule for `to`), the named strings work as before, and anything else, null included, is refused with the accepted values. The new test `align_and_distribute_refuse_dir_and_to_that_are_not_strings` sends null, 123, false, {} and [] as `dir` to arrange.distribute and as `to` to both commands, in a fresh three-shape session each time, and checks the error, the revision and every shape's box. --- crates/engine/src/cmd/arrange.rs | 32 ++++++++++---- crates/engine/src/tests.rs | 76 ++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 8 deletions(-) diff --git a/crates/engine/src/cmd/arrange.rs b/crates/engine/src/cmd/arrange.rs index 2f633501c..854758e55 100644 --- a/crates/engine/src/cmd/arrange.rs +++ b/crates/engine/src/cmd/arrange.rs @@ -417,17 +417,28 @@ fn regroup(s: &mut Session, _p: &Value) -> Result { } } +/// `to` of arrange.align and arrange.distribute: `Some(true)` for the slide, `Some(false)` for the +/// selection, `None` when it is left out. Any other value, a non-string included, is refused. +fn align_to(cmd: &str, p: &Value) -> Result> { + match p.get("to").map(Value::as_str) { + None => Ok(None), + Some(Some("slide")) => Ok(Some(true)), + Some(Some("selection")) => Ok(Some(false)), + Some(_) => Err(bad(cmd, "`to` must be slide or selection")), + } +} + fn align(s: &mut Session, p: &Value) -> Result { - let edge = str_param(p, "edge").unwrap_or("left").to_string(); + let edge = match str_param(p, "edge") { + Some(e @ ("left" | "center" | "right" | "top" | "middle" | "bottom")) => e.to_string(), + _ => return Err(bad("arrange.align", "`edge` must be left, center, right, top, middle or bottom")), + }; + let to = align_to("arrange.align", p)?; let ids = targets(s, p)?; let st = s.doc()?; let size = st.doc.slide_size; let boxes: Vec<(ShapeId, Xfrm)> = ids.iter().filter_map(|id| st.shape(*id).map(|x| (*id, xfrm_of(&st.doc, &st.selection, x)))).collect(); - let to_slide = match str_param(p, "to") { - Some("slide") => true, - Some(_) => false, - None => boxes.len() < 2, - }; + let to_slide = to.unwrap_or(boxes.len() < 2); let reference = if to_slide { Rect::new(0.0, 0.0, size.width, size.height) } else { @@ -453,13 +464,18 @@ fn align(s: &mut Session, p: &Value) -> Result { } fn distribute(s: &mut Session, p: &Value) -> Result { - let horiz = !str_param(p, "dir").unwrap_or("horizontal").starts_with('v'); + let horiz = match p.get("dir").map(Value::as_str) { + None | Some(Some("horizontal")) => true, + Some(Some("vertical")) => false, + Some(_) => return Err(bad("arrange.distribute", "`dir` must be horizontal or vertical")), + }; + let to = align_to("arrange.distribute", p)?; let ids = targets(s, p)?; let st = s.doc()?; let size = st.doc.slide_size; let mut boxes: Vec<(ShapeId, Rect)> = ids.iter().filter_map(|id| st.shape(*id).map(|x| (*id, xfrm_of(&st.doc, &st.selection, x).bounds()))).collect(); - let to_slide = str_param(p, "to") == Some("slide") || boxes.len() < 3; + let to_slide = to == Some(true) || boxes.len() < 3; if boxes.is_empty() { return ok(); } diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 21b67310a..66494ea22 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -1184,6 +1184,82 @@ fn distribute_wide_middle_stays_in_selection_bounds() { assert!(xs.iter().all(|x| (*x - [0.0, 10.0, 110.0][xs.iter().position(|y| y == x).unwrap()]).abs() < 1e-6), "{xs:?}"); } +/// An `edge`, `dir` or `to` that the commands do not name, or no `edge` at all, is refused with +/// the accepted values. Before, such a call reported its shapes as edited and moved nothing, +/// aligned to the selection, or fell back to the left edge or to horizontal. +#[test] +fn align_and_distribute_refuse_values_they_do_not_name() { + let mut s = session(); + let mut ids = vec![]; + for (x, y) in [(10.0, 10.0), (200.0, 50.0), (500.0, 200.0)] { + ids.push(s.execute("shape.insert", &json!({"preset": "rect", "rect": [x, y, 40, 40]})).unwrap()["id"].as_u64().unwrap()); + } + s.execute("edit.select", &json!({"ids": ids})).unwrap(); + let before = s.doc().unwrap().revision; + let edges = "`edge` must be left, center, right, top, middle or bottom"; + let dirs = "`dir` must be horizontal or vertical"; + let tos = "`to` must be slide or selection"; + for (cmd, params, want) in [ + ("arrange.align", json!({"edge": "centre"}), edges), + ("arrange.align", json!({"edge": "Left"}), edges), + ("arrange.align", json!({"edge": "hcenter"}), edges), + ("arrange.align", json!({}), edges), + ("arrange.align", json!({"ids": [ids[0]], "edge": "left", "to": "Slide"}), tos), + ("arrange.distribute", json!({"dir": "Vertical"}), dirs), + ("arrange.distribute", json!({"dir": "columns"}), dirs), + ("arrange.distribute", json!({"dir": "v"}), dirs), + ("arrange.distribute", json!({"dir": "vertical", "to": "page"}), tos), + ] { + let err = s.execute(cmd, ¶ms).unwrap_err().to_string(); + assert!(err.contains(want), "{cmd} {params}: {err}"); + } + assert_eq!(s.doc().unwrap().revision, before, "a refused call leaves the slide as it was"); + let pos = |s: &mut Session, id: u64, k: &str| s.execute("shape.inspect", &json!({"id": id})).unwrap()["box"][k].as_f64().unwrap(); + s.execute("arrange.align", &json!({"ids": [ids[1]], "edge": "left", "to": "slide"})).unwrap(); + assert!(pos(&mut s, ids[1], "x").abs() < 1e-6); + // Within the selection's span, 10..240: 120 pt of shapes leaves two 55 pt gaps. + s.execute("arrange.distribute", &json!({"ids": ids, "dir": "vertical", "to": "selection"})).unwrap(); + assert!((pos(&mut s, ids[1], "y") - 105.0).abs() < 1e-6); +} + +/// A `dir` or `to` that is present but is not a string (null, a number, a bool, an object or an +/// array) is refused with the accepted values, and the shapes and the revision stay as they were. +/// Before, such a value counted as left out: `dir` meant horizontal and `to` each command's +/// default, so the call moved the selected shapes. +#[test] +fn align_and_distribute_refuse_dir_and_to_that_are_not_strings() { + let dirs = "`dir` must be horizontal or vertical"; + let tos = "`to` must be slide or selection"; + let mut failures = vec![]; + for v in [json!(null), json!(123), json!(false), json!({}), json!([])] { + for (cmd, params, want) in [ + ("arrange.distribute", json!({"dir": v.clone()}), dirs), + ("arrange.distribute", json!({"dir": "horizontal", "to": v.clone()}), tos), + ("arrange.align", json!({"edge": "left", "to": v.clone()}), tos), + ] { + let mut s = session(); + let mut ids = vec![]; + for (x, y) in [(10.0, 10.0), (200.0, 50.0), (500.0, 200.0)] { + ids.push(s.execute("shape.insert", &json!({"preset": "rect", "rect": [x, y, 40, 40]})).unwrap()["id"].as_u64().unwrap()); + } + s.execute("edit.select", &json!({"ids": ids})).unwrap(); + let boxes = |s: &mut Session| -> Vec { + ids.iter().map(|id| s.execute("shape.inspect", &json!({"id": id})).unwrap()["box"].clone()).collect() + }; + let (rev, before) = (s.doc().unwrap().revision, boxes(&mut s)); + match s.execute(cmd, ¶ms) { + Ok(_) => failures.push(format!("{cmd} {params}: accepted")), + Err(e) if !e.to_string().contains(want) => failures.push(format!("{cmd} {params}: {e}")), + Err(_) => {} + } + if s.doc().unwrap().revision != rev || boxes(&mut s) != before { + failures.push(format!("{cmd} {params}: moved the shapes")); + } + } + } + assert!(failures.is_empty(), "{failures:#?}"); +} + #[test] fn pointer_draw_move_resize() { let mut s = session();