Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Move Anchors: a points value that is not a list is an error

moppeeer3d652b3e3e0c
diff --git a/crates/engine/src/cmd/object.rs b/crates/engine/src/cmd/object.rs
index e155f84eb..b0117eca7 100644
--- a/crates/engine/src/cmd/object.rs
+++ b/crates/engine/src/cmd/object.rs
@@ -3361,7 +3361,13 @@ fn path_close(s: &mut Session, p: &Value) -> Result<Value> {
fn path_move_anchors(s: &mut Session, p: &Value) -> Result<Value> {
// The anchors to move, by path: `points` (several paths) or `id` with `anchors`.
let mut targets: Vec<(ItemId, Vec<(usize, usize)>)> = Vec::new();
- if let Some(points) = p.get("points").and_then(Value::as_array) {
+ // A `points` that isn't a list is an error, never the `id` form (which could move another path).
+ let points = match p.get("points") {
+ None | Some(Value::Null) => None,
+ Some(Value::Array(a)) => Some(a),
+ Some(_) => return Err(bad("path.moveAnchors", "`points` must be a list of [id, subpath, index]")),
+ };
+ if let Some(points) = points {
for v in points {
let n = |i: usize| v.get(i).and_then(Value::as_u64).and_then(|x| usize::try_from(x).ok());
let (Some(id), Some(si), Some(ai)) = (v.get(0).and_then(Value::as_u64), n(1), n(2)) else {
diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs
index baeb1b089..a2903fc6a 100644
--- a/crates/engine/src/tests.rs
+++ b/crates/engine/src/tests.rs
@@ -1705,6 +1705,10 @@ fn selected_points_follow_their_path() {
let (_, si, ai) = st.selection.anchors[0];
let it = st.doc.item(id(l)).unwrap();
assert_eq!((it.xf * it.path.subpaths[si].anchors[ai].p).round(), designcraft_geom::Point::new(200.0, 100.0));
+ // `points` that isn't a list is refused, even with an `id` beside it.
+ for bad in [json!("x"), json!({"0": 1}), json!(5)] {
+ assert!(s.execute("path.moveAnchors", &json!({"points": bad, "id": l, "anchors": [[0, 0]], "dx": 5})).is_err(), "points {bad}");
+ }
for bad in [json!(3), json!(-1), json!("0")] {
assert!(s.execute("path.appendAnchor", &json!({"id": l, "anchor": {"p": [0, 0]}, "subpath": bad})).is_err(), "subpath {bad}");
}